root/src/noit_console.h

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

make the delegate state a first-class citizen and preserve closure for future use. show checks? seriously?

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #ifndef _NOIT_CONSOLE_H
7 #define _NOIT_CONSOLE_H
8
9 #include "noit_defines.h"
10 #include "eventer/eventer.h"
11 #include "noitedit/histedit.h"
12 #include "noit_console_telnet.h"
13 #include "utils/noit_hash.h"
14 #include <stdarg.h>
15
16 struct _console_state;
17 struct __noit_console_closure;
18
19 typedef int (*console_cmd_func_t)(struct __noit_console_closure *,
20                                   int, char **,
21                                   struct _console_state *, void *);
22 typedef char *(*console_prompt_func_t)(EditLine *);
23 typedef void (*state_free_func_t)(struct _console_state *);
24 typedef void (*state_userdata_free_func_t)(void *);
25
26 typedef struct {
27   const char            *name;
28   console_cmd_func_t     func;
29   struct _console_state *dstate;
30   void                  *closure;
31 } cmd_info_t;
32
33 /* This performs a pop (exiting if at toplevel) */
34 extern cmd_info_t console_command_exit;
35
36 typedef struct {
37   char                      *name;
38   void                      *data;
39   state_userdata_free_func_t freefunc;
40 } noit_console_userdata_t;
41
42 API_EXPORT(void)
43   noit_console_userdata_set(struct __noit_console_closure *,
44                             const char *name, void *data,
45                             state_userdata_free_func_t freefunc);
46 API_EXPORT(void *)
47   noit_console_userdata_get(struct __noit_console_closure *,
48                             const char *name);
49
50 typedef struct _console_state {
51   console_prompt_func_t      console_prompt_function;
52   noit_hash_table            cmds;
53   state_free_func_t          statefree;
54 } noit_console_state_t;
55
56 typedef struct _console_state_stack {
57   noit_console_state_t *state;
58   void *userdata;
59   struct _console_state_stack *last;
60 } noit_console_state_stack_t;
61
62 typedef struct __noit_console_closure {
63   eventer_t e;           /* The event it is attached to.  This
64                           * is needed so it can write itself out */
65   int   wants_shutdown;  /* Set this to 1 to have it die */
66
67   /* nice console support */
68   EditLine *el;
69   History *hist;
70   noit_hash_table userdata;
71
72   noit_console_state_stack_t *state_stack;
73
74   int   pty_master;
75   int   pty_slave;
76
77   /* Output buffer for non-blocking sends */
78   char *outbuf;
79   int   outbuf_allocd;
80   int   outbuf_len;
81   int   outbuf_cooked;
82   int   outbuf_completed;
83
84   /* This tracks telnet protocol state (if we're doing telnet) */
85   noit_console_telnet_closure_t telnet;
86   void (*output_cooker)(struct __noit_console_closure *);
87 } * noit_console_closure_t;
88
89 API_EXPORT(void) noit_console_init();
90
91 API_EXPORT(int)
92   noit_console_handler(eventer_t e, int mask, void *closure,
93                        struct timeval *now);
94
95
96 API_EXPORT(int)
97   nc_printf(noit_console_closure_t ncct, const char *fmt, ...);
98
99 API_EXPORT(int)
100   nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg);
101
102 API_EXPORT(int)
103   nc_write(noit_console_closure_t ncct, void *buf, int len);
104
105 API_EXPORT(int)
106   noit_console_continue_sending(noit_console_closure_t ncct,
107                                 int *mask);
108
109 API_EXPORT(int)
110   noit_console_state_init(noit_console_closure_t ncct);
111
112 API_EXPORT(int)
113   noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv,
114                          noit_console_state_t *, void *);
115
116 API_EXPORT(int)
117   noit_console_state_add_cmd(noit_console_state_t *state,
118                              cmd_info_t *cmd);
119
120 API_EXPORT(cmd_info_t *)
121   noit_console_state_get_cmd(noit_console_state_t *state,
122                              const char *name);
123
124 API_EXPORT(noit_console_state_t *)
125   noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist,
126                            state_free_func_t sfreef);
127
128 API_EXPORT(void)
129   noit_console_state_push_state(noit_console_closure_t ncct,
130                                 noit_console_state_t *);
131
132 API_EXPORT(noit_console_state_t *)
133   noit_console_state_initial();
134
135 API_EXPORT(void)
136   noit_console_state_free(noit_console_state_t *st);
137
138 API_EXPORT(int)
139   noit_console_state_do(noit_console_closure_t ncct, int argc, char **argv);
140
141 API_EXPORT(int)
142   _noit_console_state_do(noit_console_closure_t ncct,
143                          noit_console_state_stack_t *stack,
144                          int argc, char **argv);
145
146 API_EXPORT(int)
147   noit_console_state_delegate(noit_console_closure_t ncct,
148                               int argc, char **argv,
149                               noit_console_state_t *dstate,
150                               void *closure);
151  
152 API_EXPORT(cmd_info_t *)
153   NCSCMD(const char *name, console_cmd_func_t func,
154          noit_console_state_t *dstate, void *closure);
155
156 #endif
Note: See TracBrowser for help on using the browser.