Changeset 1fe89bbf2c59cbd0895409389eb9df047e96d692
- Timestamp:
- 02/10/08 16:20:40
(5 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1202660440 +0000
- git-parent:
[efa89659d10da4930e2af74b0d0a6ca18c4c74b2]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1202660440 +0000
- Message:
userdata for states, track a path and prompt
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r7192210 |
r1fe89bb |
|
| 274 | 274 | } |
|---|
| 275 | 275 | |
|---|
| 276 | | |
|---|
| | 276 | static void |
|---|
| | 277 | conf_t_userdata_free(void *data) { |
|---|
| | 278 | noit_conf_t_userdata_t *info = data; |
|---|
| | 279 | if(info) { |
|---|
| | 280 | if(info->path) free(info->path); |
|---|
| | 281 | free(info); |
|---|
| | 282 | } |
|---|
| | 283 | } |
|---|
| 277 | 284 | static int |
|---|
| 278 | 285 | noit_console_state_conf_terminal(noit_console_closure_t ncct, |
|---|
| 279 | 286 | int argc, char **argv, void *state) { |
|---|
| | 287 | noit_conf_t_userdata_t *info; |
|---|
| 280 | 288 | if(argc) { |
|---|
| 281 | 289 | nc_printf(ncct, "extra arguments not expected.\n"); |
|---|
| 282 | 290 | return -1; |
|---|
| 283 | 291 | } |
|---|
| | 292 | info = calloc(1, sizeof(*info)); |
|---|
| | 293 | info->path = strdup("/"); |
|---|
| | 294 | noit_console_userdata_set(ncct, NOIT_CONF_T_USERDATA, info, |
|---|
| | 295 | conf_t_userdata_free); |
|---|
| 284 | 296 | noit_console_state_push_state(ncct, state); |
|---|
| 285 | 297 | noit_console_state_init(ncct); |
|---|
| … | … | |
| 289 | 301 | static char * |
|---|
| 290 | 302 | conf_t_prompt(EditLine *el) { |
|---|
| | 303 | noit_console_closure_t ncct; |
|---|
| | 304 | noit_conf_t_userdata_t *info; |
|---|
| 291 | 305 | static char *tl = "noit(conf)# "; |
|---|
| 292 | | return tl; |
|---|
| | 306 | static char *pfmt = "noit(conf:%s%s)# "; |
|---|
| | 307 | int path_len, max_len; |
|---|
| | 308 | |
|---|
| | 309 | el_get(el, EL_USERDATA, (void *)&ncct); |
|---|
| | 310 | if(!ncct) return tl; |
|---|
| | 311 | info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); |
|---|
| | 312 | if(!info) return tl; |
|---|
| | 313 | |
|---|
| | 314 | path_len = strlen(info->path); |
|---|
| | 315 | max_len = strlen(pfmt) - 4 /* %s%s */ - 1 /* \0 */; |
|---|
| | 316 | if(path_len > max_len) |
|---|
| | 317 | snprintf(info->prompt, sizeof(info->prompt), |
|---|
| | 318 | pfmt, "...", info->path + max_len - 3 /* ... */); |
|---|
| | 319 | else |
|---|
| | 320 | snprintf(info->prompt, sizeof(info->prompt), pfmt, "", info->path); |
|---|
| | 321 | return info->prompt; |
|---|
| 293 | 322 | } |
|---|
| 294 | 323 | |
|---|
| re102af3 |
r1fe89bb |
|
| 12 | 12 | typedef enum { noit_true, noit_false } noit_conf_boolean; |
|---|
| 13 | 13 | typedef void * noit_conf_section_t; |
|---|
| | 14 | |
|---|
| | 15 | #define NOIT_CONF_T_USERDATA "noit::state::conf_t" |
|---|
| | 16 | typedef struct { |
|---|
| | 17 | char *path; |
|---|
| | 18 | char prompt[40]; |
|---|
| | 19 | } noit_conf_t_userdata_t; |
|---|
| 14 | 20 | |
|---|
| 15 | 21 | API_EXPORT(void) noit_conf_init(); |
|---|
| refa8965 |
r1fe89bb |
|
| 131 | 131 | } |
|---|
| 132 | 132 | |
|---|
| | 133 | static void |
|---|
| | 134 | noit_console_userdata_free(void *data) { |
|---|
| | 135 | noit_console_userdata_t *userdata = data; |
|---|
| | 136 | if(userdata) { |
|---|
| | 137 | if(userdata->name) free(userdata->name); |
|---|
| | 138 | if(userdata->freefunc) |
|---|
| | 139 | userdata->freefunc(userdata->data); |
|---|
| | 140 | free(userdata); |
|---|
| | 141 | } |
|---|
| | 142 | } |
|---|
| 133 | 143 | void |
|---|
| 134 | 144 | noit_console_closure_free(noit_console_closure_t ncct) { |
|---|
| … | … | |
| 139 | 149 | if(ncct->outbuf) free(ncct->outbuf); |
|---|
| 140 | 150 | if(ncct->telnet) noit_console_telnet_free(ncct->telnet); |
|---|
| | 151 | noit_hash_destroy(&ncct->userdata, NULL, noit_console_userdata_free); |
|---|
| 141 | 152 | while(ncct->state_stack) { |
|---|
| 142 | 153 | noit_console_state_stack_t *tmp; |
|---|
| … | … | |
| 152 | 163 | noit_console_closure_t new_ncct; |
|---|
| 153 | 164 | new_ncct = calloc(1, sizeof(*new_ncct)); |
|---|
| | 165 | noit_hash_init(&new_ncct->userdata); |
|---|
| 154 | 166 | noit_console_state_push_state(new_ncct, noit_console_state_initial()); |
|---|
| 155 | 167 | new_ncct->pty_master = -1; |
|---|
| … | … | |
| 157 | 169 | return new_ncct; |
|---|
| 158 | 170 | } |
|---|
| | 171 | |
|---|
| | 172 | void |
|---|
| | 173 | noit_console_userdata_set(struct __noit_console_closure *ncct, |
|---|
| | 174 | const char *name, void *data, |
|---|
| | 175 | state_userdata_free_func_t freefunc) { |
|---|
| | 176 | noit_console_userdata_t *item; |
|---|
| | 177 | item = calloc(1, sizeof(*item)); |
|---|
| | 178 | item->name = strdup(name); |
|---|
| | 179 | item->data = data; |
|---|
| | 180 | item->freefunc = freefunc; |
|---|
| | 181 | noit_hash_replace(&ncct->userdata, item->name, strlen(item->name), |
|---|
| | 182 | item, free, noit_console_userdata_free); |
|---|
| | 183 | } |
|---|
| | 184 | |
|---|
| | 185 | void * |
|---|
| | 186 | noit_console_userdata_get(struct __noit_console_closure *ncct, |
|---|
| | 187 | const char *name) { |
|---|
| | 188 | noit_console_userdata_t *item; |
|---|
| | 189 | if(noit_hash_retrieve(&ncct->userdata, name, strlen(name), |
|---|
| | 190 | (void **)&item)) |
|---|
| | 191 | return item->data; |
|---|
| | 192 | return NULL; |
|---|
| | 193 | } |
|---|
| | 194 | |
|---|
| 159 | 195 | |
|---|
| 160 | 196 | int |
|---|
| … | … | |
| 237 | 273 | history(ncct->hist, &ev, H_SETSIZE, 500); |
|---|
| 238 | 274 | ncct->el = el_init("noitd", ncct->pty_master, e->fd, e->fd); |
|---|
| | 275 | el_set(ncct->el, EL_USERDATA, ncct); |
|---|
| 239 | 276 | el_set(ncct->el, EL_EDITOR, "emacs"); |
|---|
| 240 | 277 | el_set(ncct->el, EL_HIST, history, ncct->hist); |
|---|
| r7192210 |
r1fe89bb |
|
| 21 | 21 | typedef char *(*console_prompt_func_t)(EditLine *); |
|---|
| 22 | 22 | typedef void (*state_free_func_t)(struct _console_state *); |
|---|
| | 23 | typedef void (*state_userdata_free_func_t)(void *); |
|---|
| 23 | 24 | |
|---|
| 24 | | typedef struct cmd_info { |
|---|
| | 25 | typedef struct { |
|---|
| 25 | 26 | const char *name; |
|---|
| 26 | 27 | console_cmd_func_t func; |
|---|
| … | … | |
| 31 | 32 | extern cmd_info_t console_command_exit; |
|---|
| 32 | 33 | |
|---|
| | 34 | typedef struct { |
|---|
| | 35 | char *name; |
|---|
| | 36 | void *data; |
|---|
| | 37 | state_userdata_free_func_t freefunc; |
|---|
| | 38 | } noit_console_userdata_t; |
|---|
| | 39 | |
|---|
| | 40 | API_EXPORT(void) |
|---|
| | 41 | noit_console_userdata_set(struct __noit_console_closure *, |
|---|
| | 42 | const char *name, void *data, |
|---|
| | 43 | state_userdata_free_func_t freefunc); |
|---|
| | 44 | API_EXPORT(void *) |
|---|
| | 45 | noit_console_userdata_get(struct __noit_console_closure *, |
|---|
| | 46 | const char *name); |
|---|
| | 47 | |
|---|
| 33 | 48 | typedef struct _console_state { |
|---|
| 34 | | console_prompt_func_t console_prompt_function; |
|---|
| 35 | | noit_hash_table cmds; |
|---|
| 36 | | state_free_func_t statefree; |
|---|
| | 49 | console_prompt_func_t console_prompt_function; |
|---|
| | 50 | noit_hash_table cmds; |
|---|
| | 51 | state_free_func_t statefree; |
|---|
| 37 | 52 | } noit_console_state_t; |
|---|
| 38 | 53 | |
|---|
| 39 | 54 | typedef struct _console_state_stack { |
|---|
| 40 | 55 | noit_console_state_t *state; |
|---|
| | 56 | void *userdata; |
|---|
| 41 | 57 | struct _console_state_stack *last; |
|---|
| 42 | 58 | } noit_console_state_stack_t; |
|---|
| … | … | |
| 50 | 66 | EditLine *el; |
|---|
| 51 | 67 | History *hist; |
|---|
| | 68 | noit_hash_table userdata; |
|---|
| 52 | 69 | |
|---|
| 53 | 70 | noit_console_state_stack_t *state_stack; |
|---|