root/src/noit_console.h

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

fixes #126

  • 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_shutdown;
72 extern cmd_info_t console_command_restart;
73
74 typedef struct {
75   char                      *name;
76   void                      *data;
77   state_userdata_free_func_t freefunc;
78 } noit_console_userdata_t;
79
80 API_EXPORT(void)
81   noit_console_userdata_set(struct __noit_console_closure *,
82                             const char *name, void *data,
83                             state_userdata_free_func_t freefunc);
84 API_EXPORT(void *)
85   noit_console_userdata_get(struct __noit_console_closure *,
86                             const char *name);
87
88 typedef struct _console_state {
89   console_prompt_func_t      console_prompt_function;
90   /*noit_hash_table            cmds; */
91   noit_skiplist              cmds;
92   state_free_func_t          statefree;
93 } noit_console_state_t;
94
95 typedef struct _console_state_stack {
96   char *name;
97   noit_console_state_t *state;
98   void *userdata;
99   struct _console_state_stack *last;
100 } noit_console_state_stack_t;
101
102 typedef struct __noit_console_closure {
103   int initialized;
104   char feed_path[128];
105   eventer_t e;           /* The event it is attached to.  This
106                           * is needed so it can write itself out */
107   int   wants_shutdown;  /* Set this to 1 to have it die */
108
109   /* nice console support */
110   EditLine *el;
111   History *hist;
112   noit_hash_table userdata;
113   /* This is console completion magic */
114   int noit_edit_complete_cmdnum;
115   int rl_point;
116   int rl_end;
117
118   noit_console_state_stack_t *state_stack;
119
120   int   pty_master;
121   int   pty_slave;
122
123   /* Output buffer for non-blocking sends */
124   char *outbuf;
125   int   outbuf_allocd;
126   int   outbuf_len;
127   int   outbuf_cooked;
128   int   outbuf_completed;
129
130   /* This tracks telnet protocol state (if we're doing telnet) */
131   noit_console_telnet_closure_t telnet;
132   void (*output_cooker)(struct __noit_console_closure *);
133 } * noit_console_closure_t;
134
135 API_EXPORT(void) noit_console_init(const char *);
136
137 API_EXPORT(void) noit_console_set_default_prompt(const char *);
138
139 API_EXPORT(int)
140   noit_console_handler(eventer_t e, int mask, void *closure,
141                        struct timeval *now);
142
143
144 API_EXPORT(int)
145   nc_printf(noit_console_closure_t ncct, const char *fmt, ...);
146
147 API_EXPORT(int)
148   nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg);
149
150 API_EXPORT(int)
151   nc_write(noit_console_closure_t ncct, const void *buf, int len);
152
153 API_EXPORT(int)
154   noit_console_continue_sending(noit_console_closure_t ncct,
155                                 int *mask);
156
157 API_EXPORT(int)
158   noit_console_state_init(noit_console_closure_t ncct);
159
160 API_EXPORT(int)
161   noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv,
162                          noit_console_state_t *, void *);
163
164 API_EXPORT(int)
165   noit_console_help(noit_console_closure_t ncct, int argc, char **argv,
166                     noit_console_state_t *, void *);
167
168 API_EXPORT(int)
169   noit_console_shutdown(noit_console_closure_t ncct, int argc, char **argv,
170                         noit_console_state_t *, void *);
171
172 API_EXPORT(int)
173   noit_console_restart(noit_console_closure_t ncct, int argc, char **argv,
174                        noit_console_state_t *, void *);
175
176 API_EXPORT(int)
177   noit_console_state_add_cmd(noit_console_state_t *state,
178                              cmd_info_t *cmd);
179
180 API_EXPORT(cmd_info_t *)
181   noit_console_state_get_cmd(noit_console_state_t *state,
182                              const char *name);
183
184 API_EXPORT(noit_console_state_t *)
185   noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist,
186                            state_free_func_t sfreef);
187
188 API_EXPORT(void)
189   noit_console_state_push_state(noit_console_closure_t ncct,
190                                 noit_console_state_t *);
191
192 API_EXPORT(noit_console_state_t *)
193   noit_console_state_initial();
194
195 API_EXPORT(noit_console_state_t *)
196   noit_console_state_alloc(void);
197
198 API_EXPORT(void)
199   noit_console_state_free(noit_console_state_t *st);
200
201 API_EXPORT(int)
202   noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv);
203
204 API_EXPORT(int)
205   _noit_console_state_do(noit_console_closure_t ncct,
206                          noit_console_state_stack_t *stack,
207                          int argc, char **argv);
208
209 API_EXPORT(int)
210   noit_console_state_delegate(noit_console_closure_t ncct,
211                               int argc, char **argv,
212                               noit_console_state_t *dstate,
213                               void *closure);
214  
215 API_EXPORT(cmd_info_t *)
216   NCSCMD(const char *name, console_cmd_func_t func, console_opt_func_t ac,
217          noit_console_state_t *dstate, void *closure);
218
219 API_EXPORT(int)
220   noit_console_write_xml(void *vncct, const char *buffer, int len);
221
222 API_EXPORT(int)
223   noit_console_close_xml(void *vncct);
224
225 API_EXPORT(void)
226   noit_console_add_help(const char *topic, console_cmd_func_t topic_func,
227                         console_opt_func_t autocomplete);
228
229 API_EXPORT(unsigned char)
230   noit_edit_complete(EditLine *el, int invoking_key);
231
232 API_EXPORT(char *)
233   noit_console_opt_delegate(noit_console_closure_t ncct,
234                             noit_console_state_stack_t *stack,
235                             noit_console_state_t *state,
236                             int argc, char **argv,
237                             int idx);
238
239 #endif
Note: See TracBrowser for help on using the browser.