Changeset 37cf7d8f9e336376dc0ecdb1b80e4776721a950b

Show
Ignore:
Timestamp:
02/06/08 04:47:35 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1202273255 +0000
git-parent:

[5229ef1f0c47697846621aa99c2b3836b192a769]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1202273255 +0000
Message:

A 'simple' tokenizer to help us with the console

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/Makefile.in

    r57a3273 r37cf7d8  
    2626 
    2727OBJS=noitd.o noit_listener.o noit_console.o noit_check.o \ 
    28         noit_module.o noit_conf.o 
     28        noit_module.o noit_conf.o noit_tokenizer.o 
    2929 
    3030all:    noitd 
     
    4949        $(CC) $(CPPFLAGS) $(CFLAGS) -c $< 
    5050 
     51noit_tokenizer.c:       noit_tokenizer.re 
     52        re2c -o $@ noit_tokenizer.re 
     53 
    5154clean-subdirs: 
    5255        for dir in $(SUBS) ; do \ 
  • src/modules/http.c

    r5229ef1 r37cf7d8  
    257257        services = xmlXPathNodeSetGetLength(pobj->nodesetval); 
    258258  } else { 
    259     noitL(nlerr, "Error in resmon doc: %s\n", ci->body.b); 
     259    if(ci->body.l) 
     260      noitL(nlerr, "Error in resmon doc: %s\n", ci->body.b); 
    260261  } 
    261262 
  • src/noit_console.c

    r57a3273 r37cf7d8  
    88#include <stdio.h> 
    99#include <unistd.h> 
     10#include <errno.h> 
    1011 
    1112#include "eventer/eventer.h" 
     
    1314#include "noit_listener.h" 
    1415#include "noit_console.h" 
     16#include "noit_tokenizer.h" 
     17 
     18struct __noit_console_closure { 
     19  char *outbuf; 
     20  int   outbuf_allocd; 
     21  int   outbuf_len; 
     22  int   outbuf_completed; 
     23}; 
     24 
     25int 
     26nc_printf(noit_console_closure_t ncct, const char *fmt, ...) { 
     27  int len; 
     28  va_list arg; 
     29  va_start(arg, fmt); 
     30  len = nc_vprintf(ncct, fmt, arg); 
     31  va_end(arg); 
     32  return len; 
     33} 
     34int 
     35nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg) { 
     36#ifdef va_copy 
     37  va_list copy; 
     38#endif 
     39  int lenwanted; 
     40   
     41  if(!ncct->outbuf_allocd) { 
     42    ncct->outbuf = malloc(4096); 
     43    if(!ncct->outbuf) return 0; 
     44    ncct->outbuf_allocd = 4096; 
     45  } 
     46  while(1) { 
     47    char *newbuf; 
     48#ifdef va_copy 
     49    va_copy(copy, arg); 
     50    lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len, 
     51                          ncct->outbuf_allocd - ncct->outbuf_len, 
     52                          fmt, copy); 
     53    va_end(copy); 
     54#else 
     55    lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len, 
     56                          ncct->outbuf_allocd - ncct->outbuf_len, 
     57                          fmt, arg); 
     58#endif 
     59    if(ncct->outbuf_len + lenwanted < ncct->outbuf_allocd) { 
     60      /* All went well, things are as we want them. */ 
     61      ncct->outbuf_len += lenwanted; 
     62      return lenwanted; 
     63    } 
     64 
     65    /* We need to enlarge the buffer */ 
     66    lenwanted += ncct->outbuf_len; 
     67    lenwanted /= 4096; 
     68    lenwanted += 1; 
     69    lenwanted *= 4096; 
     70    newbuf = realloc(ncct->outbuf, lenwanted); 
     71    if(!newbuf) { 
     72      return 0; 
     73    } 
     74    ncct->outbuf = newbuf; 
     75    ncct->outbuf_allocd = lenwanted; 
     76  } 
     77  return -1; 
     78} 
     79void 
     80noit_console_closure_free(noit_console_closure_t ncct) { 
     81  if(ncct->outbuf) free(ncct->outbuf); 
     82  free(ncct); 
     83} 
     84 
     85noit_console_closure_t 
     86noit_console_closure_alloc() { 
     87  noit_console_closure_t new_ncct; 
     88  new_ncct = calloc(1, sizeof(*new_ncct)); 
     89  return new_ncct; 
     90} 
     91 
     92int 
     93noit_console_continue_sending(eventer_t e, noit_console_closure_t ncct, 
     94                              int *mask) { 
     95  int len; 
     96  if(!ncct->outbuf_len) return 0; 
     97  while(ncct->outbuf_len > ncct->outbuf_completed) { 
     98    len = e->opset->write(e->fd, ncct->outbuf + ncct->outbuf_completed, 
     99                          ncct->outbuf_len - ncct->outbuf_completed, 
     100                          mask, e); 
     101    if(len < 0) { 
     102      if(errno == EAGAIN) return -1; 
     103      /* Do something else here? */ 
     104      return -1; 
     105    } 
     106    ncct->outbuf_completed += len; 
     107  } 
     108  len = ncct->outbuf_len; 
     109  free(ncct->outbuf); 
     110  ncct->outbuf = NULL; 
     111  ncct->outbuf_allocd = 0; 
     112  ncct->outbuf_len = ncct->outbuf_completed = 0; 
     113  return len; 
     114} 
    15115 
    16116void 
     
    19119} 
    20120 
     121void 
     122noit_console_dispatch(eventer_t e, const char *buffer, 
     123                      noit_console_closure_t ncct) { 
     124  char *cmds[32]; 
     125  int i, cnt = 32; 
     126  nc_printf(ncct, "You said: %s", buffer); 
     127  i = noit_tokenize(buffer, cmds, &cnt); 
     128  if(i>cnt) nc_printf(ncct, "Command length too long.\n"); 
     129  if(i<0) nc_printf(ncct, "Error at offset: %d\n", 0-i); 
     130  for(i=0;i<cnt;i++) { 
     131    nc_printf(ncct, "[%d] '%s'\n", i, cmds[i]); 
     132    free(cmds[i]); 
     133  } 
     134} 
     135 
    21136int 
    22137noit_console_handler(eventer_t e, int mask, void *closure, 
    23138                     struct timeval *now) { 
    24139  int newmask = EVENTER_READ; 
     140  noit_console_closure_t ncct = closure; 
     141 
    25142  if(mask & EVENTER_EXCEPTION) { 
     143    /* Exceptions cause us to simply snip the connection */ 
    26144    eventer_remove_fd(e->fd); 
    27     close(e->fd); 
     145    e->opset->close(e->fd, &newmask, e); 
     146    if(ncct) noit_console_closure_free(ncct); 
    28147    return 0; 
    29148  } 
     149 
     150  if(!ncct) ncct = closure = e->closure = noit_console_closure_alloc(); 
     151 
     152  /* If we still have data to send back to the client, this will take 
     153   * care of that 
     154   */ 
     155  if(noit_console_continue_sending(e, ncct, &newmask) == -1) 
     156    return newmask; 
     157 
    30158  if(mask & EVENTER_READ) { 
    31159    int len; 
     
    37165      return 0; 
    38166    } 
    39     printf("IN: %.*s", len, buffer); 
     167    buffer[len] = '\0'; 
     168    printf("IN: %s", buffer); 
     169    noit_console_dispatch(e, buffer, ncct); 
     170    if(noit_console_continue_sending(e, ncct, &newmask) == -1) 
     171      return newmask; 
    40172  } 
    41173  return newmask | EVENTER_EXCEPTION; 
  • src/noit_console.h

    rcaa7b86 r37cf7d8  
    1010#include "eventer/eventer.h" 
    1111 
     12 
     13struct __noit_console_closure; 
     14typedef struct __noit_console_closure * noit_console_closure_t; 
     15 
    1216API_EXPORT(void) noit_console_init(); 
    1317 
     
    1620                       struct timeval *now); 
    1721 
     22 
     23API_EXPORT(int) 
     24  nc_printf(noit_console_closure_t ncct, const char *fmt, ...); 
     25 
     26API_EXPORT(int) 
     27  nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg); 
     28 
     29 
    1830#endif