|
|
| version 1.2.2.5, 2010/06/07 09:04:46 | version 1.2.2.11, 2010/06/07 13:55:38 |
|---|---|
| Line 7 | Line 7 |
| * | * |
| *************************************************************************/ | *************************************************************************/ |
| #include "global.h" | #include "global.h" |
| #include "cli.h" | |
| #pragma GCC visibility push(hidden) | #pragma GCC visibility push(hidden) |
| Line 383 bufDEL(int idx, void * __restrict buffer) | Line 384 bufDEL(int idx, void * __restrict buffer) |
| return RETCODE_OK; | return RETCODE_OK; |
| } | } |
| static int | |
| bufComp(int idx, void * __restrict buffer) | |
| { | |
| linebuffer_t *buf = buffer; | |
| char *str, *s, **app, *items[MAX_PROMPT_ITEMS]; | |
| register int i; | |
| if (!buffer || idx < 0 || idx > MAX_BINDKEY) | |
| return RETCODE_ERR; | |
| str = strdup(buf->line_buf); | |
| if (!str) | |
| return RETCODE_ERR; | |
| else { | |
| s = str; | |
| io_TrimStr((u_char*) s); | |
| } | |
| if (*s) { | |
| memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS); | |
| for (app = items, i = 0; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); | |
| *app ? i++ : i, *app ? app++ : app); | |
| // SLIST_FOREACH(cmd, &buf->line_cmds; cmd_next) | |
| // if (!strncmp(cmd->cmd_name, items[i - 1], strlen(items[i - 1]))) | |
| } | |
| free(str); | |
| return RETCODE_OK; | |
| } | |
| static int | |
| bufHelp(int idx, void * __restrict buffer) | |
| { | |
| linebuffer_t *buf = buffer; | |
| if (!buffer || idx < 0 || idx > MAX_BINDKEY) | |
| return RETCODE_ERR; | |
| cli_Cmd_Help(buf, -1, NULL); | |
| printfCR(buf, 1); | |
| return RETCODE_OK; | |
| } | |
| // --------------------------------------------------------------- | // --------------------------------------------------------------- |
| /* | /* |
| Line 440 cli_BindKey(bindkey_t * __restrict key, linebuffer_t * | Line 485 cli_BindKey(bindkey_t * __restrict key, linebuffer_t * |
| /* | /* |
| * cli_addCommand() Add command to CLI session | |
| * @buffer = CLI buffer | |
| * @csCmd = Command name | |
| * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ... | |
| * @funcCmd = Callback function when user call command | |
| * @csInfo = Inline information for command | |
| * @csHelp = Help line when call help | |
| * @anComp = Completion array terminated with NULL element, -1 complete commands, NULL nothing | |
| * return: RETCODE_ERR error, RETCODE_OK ok | |
| */ | |
| int | |
| cli_addCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd, | |
| const char *csInfo, const char *csHelp, const char **anComp) | |
| { | |
| struct tagCommand *cmd; | |
| if (!buffer || !csCmd || !funcCmd) { | |
| cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); | |
| return RETCODE_ERR; | |
| } | |
| cmd = malloc(sizeof(struct tagCommand)); | |
| if (!cmd) { | |
| LOGERR; | |
| return RETCODE_ERR; | |
| } else | |
| memset(cmd, 0, sizeof(struct tagCommand)); | |
| cmd->cmd_level = cliLevel; | |
| cmd->cmd_func = funcCmd; | |
| cmd->cmd_comp = (char**) anComp; | |
| cmd->cmd_len = strlcpy(cmd->cmd_name, csCmd, STRSIZ); | |
| if (csInfo) | |
| strlcpy(cmd->cmd_info, csInfo, STRSIZ); | |
| if (csHelp) | |
| strlcpy(cmd->cmd_help, csHelp, STRSIZ); | |
| SLIST_INSERT_HEAD(&buffer->line_cmds, cmd, cmd_next); | |
| return RETCODE_OK; | |
| } | |
| /* | |
| * cli_delCommand() Delete command from CLI session | |
| * @buffer = CLI buffer | |
| * @csCmd = Command name | |
| * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ... | |
| * return: RETCODE_ERR error, RETCODE_OK ok | |
| */ | |
| int | |
| cli_delCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel) | |
| { | |
| struct tagCommand *cmd; | |
| int ret = RETCODE_OK; | |
| if (!buffer || !csCmd) { | |
| cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); | |
| return RETCODE_ERR; | |
| } | |
| SLIST_FOREACH(cmd, &buffer->line_cmds, cmd_next) | |
| if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) { | |
| ret = 1; | |
| SLIST_REMOVE(&buffer->line_cmds, cmd, tagCommand, cmd_next); | |
| free(cmd); | |
| break; | |
| } | |
| return ret; | |
| } | |
| /* | |
| * cli_updCommand() Update command in CLI session | |
| * @buffer = CLI buffer | |
| * @csCmd = Command name | |
| * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ... | |
| * @funcCmd = Callback function when user call command | |
| * @csInfo = Inline information for command | |
| * @csHelp = Help line when call help | |
| * @anComp = Completion array terminated with NULL element, -1 complete commands, NULL nothing, | |
| * update only if funcCmd is not NULL | |
| * return: RETCODE_ERR error, RETCODE_OK ok | |
| */ | |
| int | |
| cli_updCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd, | |
| const char *csInfo, const char *csHelp, const char **anComp) | |
| { | |
| struct tagCommand *cmd; | |
| int ret = RETCODE_OK; | |
| if (!buffer || !csCmd) { | |
| cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); | |
| return RETCODE_ERR; | |
| } | |
| SLIST_FOREACH(cmd, &buffer->line_cmds, cmd_next) | |
| if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) { | |
| ret = 1; | |
| if (funcCmd) { | |
| cmd->cmd_func = funcCmd; | |
| cmd->cmd_comp = (char**) anComp; | |
| } | |
| if (csInfo) | |
| strlcpy(cmd->cmd_info, csInfo, STRSIZ); | |
| if (csHelp) | |
| strlcpy(cmd->cmd_help, csHelp, STRSIZ); | |
| break; | |
| } | |
| return ret; | |
| } | |
| /* | |
| * cli_addHistory() Add line to history | * cli_addHistory() Add line to history |
| * @buffer = CLI buffer | * @buffer = CLI buffer |
| * @str = Add custom text or if NULL use readed line from CLI buffer | * @str = Add custom text or if NULL use readed line from CLI buffer |
| Line 745 cliInit(int fin, int fout, const char *prompt) | Line 904 cliInit(int fin, int fout, const char *prompt) |
| } else | } else |
| memset(keys, 0, sizeof(bindkey_t) * (MAX_BINDKEY + 1)); | memset(keys, 0, sizeof(bindkey_t) * (MAX_BINDKEY + 1)); |
| /* add helper functions */ | |
| cli_addCommand(buffer, "exit", 0, cli_Cmd_Exit, "exit <cr>", "Exit from console", NULL); | |
| cli_addCommand(buffer, "help", 0, cli_Cmd_Help, "help [command] <cr>", "Help screen", | |
| (const char**) -1); | |
| /* fill key bindings */ | /* fill key bindings */ |
| // ascii chars & ctrl+chars | // ascii chars & ctrl+chars |
| for (i = 0; i < 256; i++) { | for (i = 0; i < 256; i++) { |
| Line 763 cliInit(int fin, int fout, const char *prompt) | Line 927 cliInit(int fin, int fout, const char *prompt) |
| keys[i].key_func = bufBEGIN; | keys[i].key_func = bufBEGIN; |
| if (i == *K_CTRL_E) | if (i == *K_CTRL_E) |
| keys[i].key_func = bufEND; | keys[i].key_func = bufEND; |
| if (i == *K_TAB) | |
| keys[i].key_func = bufComp; | |
| if (i >= *K_SPACE && i < *K_BACKSPACE) | if (i >= *K_SPACE && i < *K_BACKSPACE) |
| keys[i].key_func = bufCHAR; | keys[i].key_func = bufCHAR; |
| if (i > *K_BACKSPACE && i < 0xff) | if (i > *K_BACKSPACE && i < 0xff) |
| keys[i].key_func = bufCHAR; | keys[i].key_func = bufCHAR; |
| if (i == '?') | |
| keys[i].key_func = bufHelp; | |
| } | } |
| // alt+chars | // alt+chars |
| for (i = 256; i < 512; i++) { | for (i = 256; i < 512; i++) { |
| Line 1040 cliNetLoop(linebuffer_t * __restrict buffer, const cha | Line 1208 cliNetLoop(linebuffer_t * __restrict buffer, const cha |
| case 0: | case 0: |
| close(sock); | close(sock); |
| // buffer = cliInit(STDIN_FILENO, STDOUT_FILENO, csPrompt); | if (buffer) { |
| // if (!buffer) | ret = cliLoop(buffer, csHistFile) < 0 ? 1 : 0; |
| // return RETCODE_ERR; | cliEnd(buffer); |
| } else | |
| cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); | |
| ret = cliLoop(buffer, csHistFile) < 0 ? 1 : 0; | |
| // cliEnd(buffer); | |
| /* spawn Shell mode */ | /* spawn Shell mode */ |
| /* | /* |
| execl("/bin/tcsh", "tcsh", NULL); | execl("/bin/tcsh", "tcsh", NULL); |
| Line 1142 cliLoop(linebuffer_t * __restrict buffer, const char * | Line 1308 cliLoop(linebuffer_t * __restrict buffer, const char * |
| struct tagCommand *cmd; | struct tagCommand *cmd; |
| /* | /* |
| inline int inline_help() | |
| { | |
| cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL); | |
| rl_on_new_line(); | |
| return 0; | |
| } | |
| char **cli_stdCompletion(const char *text, int start, int end) | char **cli_stdCompletion(const char *text, int start, int end) |
| { | { |