root/src/noit_console.h

Revision 0da4c1a673ca6585bae7e7403509d1fb4ce2ac23, 8.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 months ago)

support a crash command (for debugging) -- no more harmful than the restart

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _NOIT_CONSOLE_H
34 #define _NOIT_CONSOLE_H
35
36 #include "noit_defines.h"
37 #include "eventer/eventer.h"
38 #include "noitedit/histedit.h"
39 #include "noit_console_telnet.h"
40 #include "utils/noit_hash.h"
41 #include "utils/noit_skiplist.h"
42 #include <stdarg.h>
43
44 struct _console_state;
45 struct _console_state_stack;
46 struct __noit_console_closure;
47
48 typedef int (*console_cmd_func_t)(struct __noit_console_closure *,
49                                   int, char **,
50                                   struct _console_state *, void *);
51 typedef char * (*console_opt_func_t)(struct __noit_console_closure *,
52                                      struct _console_state_stack *stack,
53                                      struct _console_state *state,
54                                      int argc, char **argv,
55                                      int idx);
56 typedef char *(*console_prompt_func_t)(EditLine *);
57 typedef void (*state_free_func_t)(struct _console_state *);
58 typedef void (*state_userdata_free_func_t)(void *);
59
60 typedef struct {
61   const char            *name;
62   console_cmd_func_t     func;
63   console_opt_func_t     autocomplete;
64   struct _console_state *dstate;
65   void                  *closure;
66 } cmd_info_t;
67
68 /* This performs a pop (exiting if at toplevel) */
69 extern cmd_info_t console_command_exit;
70 extern cmd_info_t console_command_help;
71 extern cmd_info_t console_command_crash;
72 extern cmd_info_t console_command_shutdown;
73 extern cmd_info_t console_command_restart;
74
75 typedef struct {
76   char                      *name;
77   void                      *data;
78   state_userdata_free_func_t freefunc;
79 } noit_console_userdata_t;
80
81 API_EXPORT(void)
82   noit_console_userdata_set(struct __noit_console_closure *,
83                             const char *name, void *data,
84                             state_userdata_free_func_t freefunc);
85 API_EXPORT(void *)
86   noit_console_userdata_get(struct __noit_console_closure *,
87                             const char *name);
88
89 typedef struct _console_state {
90   console_prompt_func_t      console_prompt_function;
91   /*noit_hash_table            cmds; */
92   noit_skiplist              cmds;
93   state_free_func_t          statefree;
94 } noit_console_state_t;
95
96 typedef struct _console_state_stack {
97   char *name;
98   noit_console_state_t *state;
99   void *userdata;
100   struct _console_state_stack *last;
101 } noit_console_state_stack_t;
102
103 typedef struct __noit_console_closure {
104   int initialized;
105   char feed_path[128];
106   eventer_t e;           /* The event it is attached to.  This
107                           * is needed so it can write itself out */
108   int   wants_shutdown;  /* Set this to 1 to have it die */
109
110   /* nice console support */
111   EditLine *el;
112   History *hist;
113   noit_hash_table userdata;
114   /* This is console completion magic */
115   int noit_edit_complete_cmdnum;
116   int rl_point;
117   int rl_end;
118
119   noit_console_state_stack_t *state_stack;
120
121   int   pty_master;
122   int   pty_slave;
123
124   /* Output buffer for non-blocking sends */
125   char *outbuf;
126   int   outbuf_allocd;
127   int   outbuf_len;
128   int   outbuf_cooked;
129   int   outbuf_completed;
130
131   /* This tracks telnet protocol state (if we're doing telnet) */
132   noit_console_telnet_closure_t telnet;
133   void (*output_cooker)(struct __noit_console_closure *);
134 } * noit_console_closure_t;
135
136 API_EXPORT(void) noit_console_init(const char *);
137
138 API_EXPORT(void) noit_console_set_default_prompt(const char *);
139
140 API_EXPORT(int)
141   noit_console_handler(eventer_t e, int mask, void *closure,
142                        struct timeval *now);
143
144
145 API_EXPORT(int)
146   nc_printf(noit_console_closure_t ncct, const char *fmt, ...);
147
148 API_EXPORT(int)
149   nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg);
150
151 API_EXPORT(int)
152   nc_write(noit_console_closure_t ncct, const void *buf, int len);
153
154 API_EXPORT(int)
155   noit_console_continue_sending(noit_console_closure_t ncct,
156                                 int *mask);
157
158 API_EXPORT(int)
159   noit_console_state_init(noit_console_closure_t ncct);
160
161 API_EXPORT(int)
162   noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv,
163                          noit_console_state_t *, void *);
164
165 API_EXPORT(int)
166   noit_console_help(noit_console_closure_t ncct, int argc, char **argv,
167                     noit_console_state_t *, void *);
168
169 API_EXPORT(int)
170   noit_console_crash(noit_console_closure_t ncct, int argc, char **argv,
171                      noit_console_state_t *, void *);
172
173 API_EXPORT(int)
174   noit_console_shutdown(noit_console_closure_t ncct, int argc, char **argv,
175                         noit_console_state_t *, void *);
176
177 API_EXPORT(int)
178   noit_console_restart(noit_console_closure_t ncct, int argc, char **argv,
179                        noit_console_state_t *, void *);
180
181 API_EXPORT(int)
182   noit_console_state_add_cmd(noit_console_state_t *state,
183                              cmd_info_t *cmd);
184
185 API_EXPORT(cmd_info_t *)
186   noit_console_state_get_cmd(noit_console_state_t *state,
187                              const char *name);
188
189 API_EXPORT(noit_console_state_t *)
190   noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist,
191                            state_free_func_t sfreef);
192
193 API_EXPORT(void)
194   noit_console_state_push_state(noit_console_closure_t ncct,
195                                 noit_console_state_t *);
196
197 API_EXPORT(noit_console_state_t *)
198   noit_console_state_initial();
199
200 API_EXPORT(noit_console_state_t *)
201   noit_console_state_alloc(void);
202
203 API_EXPORT(void)
204   noit_console_state_free(noit_console_state_t *st);
205
206 API_EXPORT(int)
207   noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv);
208
209 API_EXPORT(int)
210   _noit_console_state_do(noit_console_closure_t ncct,
211                          noit_console_state_stack_t *stack,
212                          int argc, char **argv);
213
214 API_EXPORT(int)
215   noit_console_state_delegate(noit_console_closure_t ncct,
216                               int argc, char **argv,
217                               noit_console_state_t *dstate,
218                               void *closure);
219  
220 API_EXPORT(cmd_info_t *)
221   NCSCMD(const char *name, console_cmd_func_t func, console_opt_func_t ac,
222          noit_console_state_t *dstate, void *closure);
223
224 API_EXPORT(int)
225   noit_console_write_xml(void *vncct, const char *buffer, int len);
226
227 API_EXPORT(int)
228   noit_console_close_xml(void *vncct);
229
230 API_EXPORT(void)
231   noit_console_add_help(const char *topic, console_cmd_func_t topic_func,
232                         console_opt_func_t autocomplete);
233
234 API_EXPORT(unsigned char)
235   noit_edit_complete(EditLine *el, int invoking_key);
236
237 API_EXPORT(char *)
238   noit_console_opt_delegate(noit_console_closure_t ncct,
239                             noit_console_state_stack_t *stack,
240                             noit_console_state_t *state,
241                             int argc, char **argv,
242                             int idx);
243
244 #endif
Note: See TracBrowser for help on using the browser.