Changeset 7192210e4ed02f4546970eb41f6da3a167b4316a

Show
Ignore:
Timestamp:
02/09/08 18:47:00 (7 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1202582820 +0000
git-parent:

[a7f575acc420cc6e5ca474d23cf07149299a3bbd]

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

delegate hooking into the configuration system to other modules

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_conf.c

    r7cdd67a r7192210  
    1313 
    1414#include "noit_conf.h" 
     15#include "noit_console.h" 
    1516#include "utils/noit_hash.h" 
    1617#include "utils/noit_log.h" 
     
    3839}; 
    3940 
     41void register_console_config_commands(); 
    4042 
    4143void noit_conf_init() { 
     
    4850  xmlInitParser(); 
    4951  xmlXPathInit(); 
     52  register_console_config_commands(); 
    5053} 
    5154 
     
    271274} 
    272275 
     276 
     277static int 
     278noit_console_state_conf_terminal(noit_console_closure_t ncct, 
     279                                 int argc, char **argv, void *state) { 
     280  if(argc) { 
     281    nc_printf(ncct, "extra arguments not expected.\n"); 
     282    return -1; 
     283  } 
     284  noit_console_state_push_state(ncct, state); 
     285  noit_console_state_init(ncct); 
     286  return 0; 
     287} 
     288 
     289static char * 
     290conf_t_prompt(EditLine *el) { 
     291  static char *tl = "noit(conf)# "; 
     292  return tl; 
     293} 
     294 
     295void register_console_config_commands() { 
     296  noit_console_state_t *tl, *_conf_state, *_conf_t_state; 
     297 
     298  tl = noit_console_state_initial(); 
     299 
     300  _conf_t_state = calloc(1, sizeof(*_conf_t_state)); 
     301  _conf_t_state->console_prompt_function = conf_t_prompt; 
     302  noit_console_state_add_cmd(_conf_t_state, &console_command_exit); 
     303 
     304  _conf_state = calloc(1, sizeof(*_conf_state)); 
     305  noit_console_state_add_cmd(_conf_state, 
     306    NCSCMD("terminal", noit_console_state_conf_terminal, _conf_t_state)); 
     307 
     308  noit_console_state_add_cmd(tl, 
     309    NCSCMD("configure", noit_console_state_delegate, _conf_state)); 
     310} 
  • src/noit_console.c

    ra7f575a r7192210  
    139139  if(ncct->outbuf) free(ncct->outbuf); 
    140140  if(ncct->telnet) noit_console_telnet_free(ncct->telnet); 
    141   while(ncct->state) { 
    142     noit_console_state_t *tmp; 
    143     tmp = ncct->state
    144     ncct->state = tmp->stacked
    145     noit_console_state_free(tmp); 
     141  while(ncct->state_stack) { 
     142    noit_console_state_stack_t *tmp; 
     143    tmp = ncct->state_stack
     144    ncct->state_stack = tmp->last
     145    free(tmp); 
    146146  } 
    147147  free(ncct); 
     
    152152  noit_console_closure_t new_ncct; 
    153153  new_ncct = calloc(1, sizeof(*new_ncct)); 
     154  noit_console_state_push_state(new_ncct, noit_console_state_initial()); 
    154155  new_ncct->pty_master = -1; 
    155156  new_ncct->pty_slave = -1; 
     
    241242      ncct->telnet = noit_console_telnet_alloc(ncct); 
    242243      ncct->output_cooker = nc_telnet_cooker; 
    243       ncct->state = noit_console_state_initial(); 
    244244      noit_console_state_init(ncct); 
    245245    } 
  • src/noit_console.h

    ra7f575a r7192210  
    1818 
    1919typedef int (*console_cmd_func_t)(struct __noit_console_closure *, 
    20                                   int, char **); 
     20                                  int, char **, void *); 
    2121typedef char *(*console_prompt_func_t)(EditLine *); 
    2222typedef void (*state_free_func_t)(struct _console_state *); 
     
    2525  const char          *name; 
    2626  console_cmd_func_t   func; 
     27  void                *closure; 
    2728} cmd_info_t; 
     29 
     30/* This performs a pop (exiting if at toplevel) */ 
     31extern cmd_info_t console_command_exit; 
    2832 
    2933typedef struct _console_state { 
    3034  console_prompt_func_t  console_prompt_function; 
    3135  noit_hash_table        cmds; 
    32   struct _console_state *stacked; 
    3336  state_free_func_t      statefree; 
    3437} noit_console_state_t; 
     38 
     39typedef struct _console_state_stack { 
     40  noit_console_state_t *state; 
     41  struct _console_state_stack *last; 
     42} noit_console_state_stack_t; 
    3543 
    3644typedef struct __noit_console_closure { 
     
    4351  History *hist; 
    4452 
    45   noit_console_state_t *state
     53  noit_console_state_stack_t *state_stack
    4654 
    4755  int   pty_master; 
     
    8391  noit_console_state_init(noit_console_closure_t ncct); 
    8492 
    85 API_EXPORT(char *) 
    86   noit_console_state_prompt(EditLine *el); 
     93API_EXPORT(int) 
     94  noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv, 
     95                         void *); 
    8796 
    8897API_EXPORT(int) 
    89   noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv); 
     98  noit_console_state_add_cmd(noit_console_state_t *state, 
     99                             cmd_info_t *cmd); 
     100 
     101API_EXPORT(noit_console_state_t *) 
     102  noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist, 
     103                           state_free_func_t sfreef); 
     104 
     105API_EXPORT(void) 
     106  noit_console_state_push_state(noit_console_closure_t ncct, 
     107                                noit_console_state_t *); 
    90108 
    91109API_EXPORT(noit_console_state_t *) 
     
    98116  noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv); 
    99117 
     118API_EXPORT(int) 
     119  _noit_console_state_do(noit_console_closure_t ncct, 
     120                         noit_console_state_stack_t *stack, 
     121                         int argc, char **argv); 
     122 
     123API_EXPORT(int) 
     124  noit_console_state_delegate(noit_console_closure_t ncct, 
     125                              int argc, char **argv, void *closure); 
     126  
     127API_EXPORT(cmd_info_t *) 
     128  NCSCMD(const char *name, console_cmd_func_t func, void *closure); 
     129 
    100130#endif 
  • src/noit_console_state.c

    ra7f575a r7192210  
    1313#include "noit_tokenizer.h" 
    1414 
    15 cmd_info_t _cit_exit = { "exit", noit_console_state_pop }; 
    16 cmd_info_t *_tl_cmds[] = { 
    17   &_cit_exit, 
    18   NULL 
    19 }; 
     15cmd_info_t console_command_exit = { "exit", noit_console_state_pop, NULL }; 
     16 
     17static char * 
     18noit_console_state_prompt(EditLine *el) { 
     19  static char *tl = "noit# "; 
     20  return tl; 
     21
    2022 
    2123int 
    22 noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv) { 
     24noit_console_state_delegate(noit_console_closure_t ncct, 
     25                            int argc, char **argv, void *closure) { 
     26  noit_console_state_stack_t tmps = { 0 }; 
     27 
     28  if(argc == 0) { 
     29    nc_printf(ncct, "arguments expected\n"); 
     30    return -1; 
     31  } 
     32  tmps.state = closure; 
     33  return _noit_console_state_do(ncct, &tmps, argc, argv); 
     34
     35 
     36int 
     37_noit_console_state_do(noit_console_closure_t ncct, 
     38                       noit_console_state_stack_t *stack, 
     39                       int argc, char **argv) { 
    2340  cmd_info_t *cmd; 
    2441 
    25   if(!argc) return -1; 
    26   if(!noit_hash_retrieve(&ncct->state->cmds, 
     42  if(!argc) { 
     43    nc_printf(ncct, "arguments expected\n"); 
     44    return -1; 
     45  } 
     46  if(!noit_hash_retrieve(&stack->state->cmds, 
    2747                         argv[0], strlen(argv[0]), (void **)&cmd)) { 
    2848    nc_printf(ncct, "No such command: '%s'\n", argv[0]); 
    2949    return -1; 
    3050  } 
    31   return cmd->func(ncct, argc-1, argv+1); 
     51  return cmd->func(ncct, argc-1, argv+1, cmd->closure); 
     52
     53int 
     54noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv) { 
     55  return _noit_console_state_do(ncct, ncct->state_stack, argc, argv); 
    3256} 
    3357 
     
    4266  return strcasecmp(a->name, b->name); 
    4367} 
     68 
     69int 
     70noit_console_state_add_cmd(noit_console_state_t *state, 
     71                           cmd_info_t *cmd) { 
     72  return noit_hash_store(&state->cmds, cmd->name, strlen(cmd->name), cmd); 
     73} 
     74 
    4475noit_console_state_t * 
    4576noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist, 
     
    5788  return state; 
    5889} 
    59 void 
    60 noit_console_state_free(noit_console_state_t *st) { 
    61   noit_hash_destroy(&st->cmds, NULL, NULL); 
     90 
     91cmd_info_t *NCSCMD(const char *name, console_cmd_func_t func, 
     92                   void *closure) { 
     93  cmd_info_t *cmd; 
     94  cmd = calloc(1, sizeof(*cmd)); 
     95  cmd->name = strdup(name); 
     96  cmd->func = func; 
     97  cmd->closure = closure; 
     98  return cmd; 
    6299} 
     100 
    63101noit_console_state_t * 
    64102noit_console_state_initial() { 
    65   return noit_console_state_build(noit_console_state_prompt, _tl_cmds, 
    66                                   noit_console_state_free); 
     103  static noit_console_state_t *_top_level_state = NULL; 
     104  if(!_top_level_state) { 
     105    _top_level_state = calloc(1, sizeof(*_top_level_state)); 
     106    noit_console_state_add_cmd(_top_level_state, &console_command_exit); 
     107  } 
     108  return _top_level_state; 
    67109} 
    68110 
    69 char * 
    70 noit_console_state_prompt(EditLine *el) { 
    71   static char *tl = "noit# "; 
    72   return tl; 
     111void 
     112noit_console_state_push_state(noit_console_closure_t ncct, 
     113                              noit_console_state_t *state) { 
     114  noit_console_state_stack_t *stack; 
     115  stack = calloc(1, sizeof(*stack)); 
     116  stack->last = ncct->state_stack; 
     117  stack->state = state; 
     118  ncct->state_stack = stack; 
    73119} 
    74120 
    75121int 
    76 noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv) { 
    77   noit_console_state_t *current; 
     122noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv, 
     123                       void *unused) { 
     124  noit_console_state_stack_t *current; 
    78125 
    79126  if(argc) { 
     
    81128    return -1; 
    82129  } 
    83   if(!ncct->state || !ncct->state->stacked) { 
     130  if(!ncct->state_stack || !ncct->state_stack->last) { 
    84131    ncct->wants_shutdown = 1; 
    85132    return 0; 
    86133  } 
    87134 
    88   current = ncct->state; 
    89   ncct->state = ncct->state->stacked; 
    90   current->stacked = NULL; 
    91   if(current->statefree) current->statefree(current); 
     135  current = ncct->state_stack; 
     136  ncct->state_stack = current->last; 
     137  current->last = NULL; 
     138  if(current->state->statefree) current->state->statefree(current->state); 
     139  free(current); 
    92140  noit_console_state_init(ncct); 
    93141  return 0; 
     
    96144int 
    97145noit_console_state_init(noit_console_closure_t ncct) { 
    98   if(ncct->el && ncct->state->console_prompt_function) { 
    99     el_set(ncct->el, EL_PROMPT, ncct->state->console_prompt_function); 
     146  if(ncct->el) { 
     147    console_prompt_func_t f; 
     148    f = ncct->state_stack->state->console_prompt_function; 
     149    el_set(ncct->el, EL_PROMPT, f ? f : noit_console_state_prompt); 
    100150  } 
    101151  return 0;