root/src/noit_console_state.c

Revision a7f575acc420cc6e5ca474d23cf07149299a3bbd, 2.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

start the framework for a console state machine

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include "eventer/eventer.h"
9 #include "utils/noit_log.h"
10 #include "utils/noit_hash.h"
11 #include "noit_listener.h"
12 #include "noit_console.h"
13 #include "noit_tokenizer.h"
14
15 cmd_info_t _cit_exit = { "exit", noit_console_state_pop };
16 cmd_info_t *_tl_cmds[] = {
17   &_cit_exit,
18   NULL
19 };
20
21 int
22 noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv) {
23   cmd_info_t *cmd;
24
25   if(!argc) return -1;
26   if(!noit_hash_retrieve(&ncct->state->cmds,
27                          argv[0], strlen(argv[0]), (void **)&cmd)) {
28     nc_printf(ncct, "No such command: '%s'\n", argv[0]);
29     return -1;
30   }
31   return cmd->func(ncct, argc-1, argv+1);
32 }
33
34 int cmd_info_comparek(void *akv, void *bv) {
35   char *ak = (char *)akv;
36   cmd_info_t *b = (cmd_info_t *)bv;
37   return strcasecmp(ak, b->name);
38 }
39 int cmd_info_compare(void *av, void *bv) {
40   cmd_info_t *a = (cmd_info_t *)av;
41   cmd_info_t *b = (cmd_info_t *)bv;
42   return strcasecmp(a->name, b->name);
43 }
44 noit_console_state_t *
45 noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist,
46                          state_free_func_t sfreef) {
47   noit_console_state_t *state;
48   state = calloc(1, sizeof(*state));
49   state->console_prompt_function = promptf;
50   while(*clist) {
51     noit_hash_store(&state->cmds,
52                     (*clist)->name, strlen((*clist)->name),
53                     *clist);
54     clist++;
55   }
56   state->statefree = sfreef;
57   return state;
58 }
59 void
60 noit_console_state_free(noit_console_state_t *st) {
61   noit_hash_destroy(&st->cmds, NULL, NULL);
62 }
63 noit_console_state_t *
64 noit_console_state_initial() {
65   return noit_console_state_build(noit_console_state_prompt, _tl_cmds,
66                                   noit_console_state_free);
67 }
68
69 char *
70 noit_console_state_prompt(EditLine *el) {
71   static char *tl = "noit# ";
72   return tl;
73 }
74
75 int
76 noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv) {
77   noit_console_state_t *current;
78
79   if(argc) {
80     nc_printf(ncct, "no arguments allowed to this command.\n");
81     return -1;
82   }
83   if(!ncct->state || !ncct->state->stacked) {
84     ncct->wants_shutdown = 1;
85     return 0;
86   }
87
88   current = ncct->state;
89   ncct->state = ncct->state->stacked;
90   current->stacked = NULL;
91   if(current->statefree) current->statefree(current);
92   noit_console_state_init(ncct);
93   return 0;
94 }
95
96 int
97 noit_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);
100   }
101   return 0;
102 }
Note: See TracBrowser for help on using the browser.