root/src/noit_console_state.c

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

delegate hooking into the configuration system to other modules

  • 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 console_command_exit = { "exit", noit_console_state_pop, NULL };
16
17 static char *
18 noit_console_state_prompt(EditLine *el) {
19   static char *tl = "noit# ";
20   return tl;
21 }
22
23 int
24 noit_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
36 int
37 _noit_console_state_do(noit_console_closure_t ncct,
38                        noit_console_state_stack_t *stack,
39                        int argc, char **argv) {
40   cmd_info_t *cmd;
41
42   if(!argc) {
43     nc_printf(ncct, "arguments expected\n");
44     return -1;
45   }
46   if(!noit_hash_retrieve(&stack->state->cmds,
47                          argv[0], strlen(argv[0]), (void **)&cmd)) {
48     nc_printf(ncct, "No such command: '%s'\n", argv[0]);
49     return -1;
50   }
51   return cmd->func(ncct, argc-1, argv+1, cmd->closure);
52 }
53 int
54 noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv) {
55   return _noit_console_state_do(ncct, ncct->state_stack, argc, argv);
56 }
57
58 int cmd_info_comparek(void *akv, void *bv) {
59   char *ak = (char *)akv;
60   cmd_info_t *b = (cmd_info_t *)bv;
61   return strcasecmp(ak, b->name);
62 }
63 int cmd_info_compare(void *av, void *bv) {
64   cmd_info_t *a = (cmd_info_t *)av;
65   cmd_info_t *b = (cmd_info_t *)bv;
66   return strcasecmp(a->name, b->name);
67 }
68
69 int
70 noit_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
75 noit_console_state_t *
76 noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist,
77                          state_free_func_t sfreef) {
78   noit_console_state_t *state;
79   state = calloc(1, sizeof(*state));
80   state->console_prompt_function = promptf;
81   while(*clist) {
82     noit_hash_store(&state->cmds,
83                     (*clist)->name, strlen((*clist)->name),
84                     *clist);
85     clist++;
86   }
87   state->statefree = sfreef;
88   return state;
89 }
90
91 cmd_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;
99 }
100
101 noit_console_state_t *
102 noit_console_state_initial() {
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;
109 }
110
111 void
112 noit_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;
119 }
120
121 int
122 noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv,
123                        void *unused) {
124   noit_console_state_stack_t *current;
125
126   if(argc) {
127     nc_printf(ncct, "no arguments allowed to this command.\n");
128     return -1;
129   }
130   if(!ncct->state_stack || !ncct->state_stack->last) {
131     ncct->wants_shutdown = 1;
132     return 0;
133   }
134
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);
140   noit_console_state_init(ncct);
141   return 0;
142 }
143
144 int
145 noit_console_state_init(noit_console_closure_t ncct) {
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);
150   }
151   return 0;
152 }
Note: See TracBrowser for help on using the browser.