|
version 1.2.2.3, 2010/06/04 13:51:21
|
version 1.2.2.4, 2010/06/04 16:16:17
|
|
Line 96 printfCR(linebuffer_t * __restrict buf, int prompt)
|
Line 96 printfCR(linebuffer_t * __restrict buf, int prompt)
|
| } |
} |
| |
|
| static inline void |
static inline void |
| |
printfNL(linebuffer_t * __restrict buf, int prompt) |
| |
{ |
| |
if (buf) { |
| |
write(buf->line_out, K_ENTER, 1); |
| |
|
| |
if (prompt) |
| |
if (prompt && buf->line_prompt) |
| |
write(buf->line_out, buf->line_prompt, buf->line_bol); |
| |
} |
| |
} |
| |
|
| |
static inline void |
| printfCLI(linebuffer_t * __restrict buf, const unsigned char *text, int textlen, int prompt) |
printfCLI(linebuffer_t * __restrict buf, const unsigned char *text, int textlen, int prompt) |
| { |
{ |
| if (buf && text && textlen) { |
if (buf && text && textlen) { |
|
Line 374 bufDEL(int idx, void * __restrict buffer)
|
Line 386 bufDEL(int idx, void * __restrict buffer)
|
| // --------------------------------------------------------------- |
// --------------------------------------------------------------- |
| |
|
| /* |
/* |
| |
* cli_Printf() Send message to CLI session |
| |
* @buffer = CLI buffer |
| |
* @fmt = printf format string |
| |
* @... = arguments defined in fmt |
| |
* return: none |
| |
*/ |
| |
inline void |
| |
cli_Printf(linebuffer_t * __restrict buffer, char *fmt, ...) |
| |
{ |
| |
va_list lst; |
| |
FILE *f; |
| |
|
| |
if (fmt) { |
| |
f = fdopen(buffer->line_out, "a"); |
| |
if (!f) { |
| |
LOGERR; |
| |
return; |
| |
} |
| |
|
| |
va_start(lst, fmt); |
| |
vfprintf(f, fmt, lst); |
| |
va_end(lst); |
| |
} else |
| |
cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); |
| |
} |
| |
|
| |
/* |
| * cli_BindKey() Bind function to key |
* cli_BindKey() Bind function to key |
| * @key = key structure |
* @key = key structure |
| * @buffer = CLI buffer |
* @buffer = CLI buffer |
|
Line 403 cli_BindKey(bindkey_t * __restrict key, linebuffer_t *
|
Line 442 cli_BindKey(bindkey_t * __restrict key, linebuffer_t *
|
| /* |
/* |
| * cli_addHistory() Add line to history |
* cli_addHistory() Add line to history |
| * @buffer = CLI buffer |
* @buffer = CLI buffer |
| * @str = Add text | * @str = Add custom text or if NULL use readed line from CLI buffer |
| * return: RETCODE_ERR error, RETCODE_OK ok |
* return: RETCODE_ERR error, RETCODE_OK ok |
| */ |
*/ |
| int |
int |
|
Line 513 cli_loadHistory(linebuffer_t * __restrict buffer, cons
|
Line 552 cli_loadHistory(linebuffer_t * __restrict buffer, cons
|
| strlcpy(szFName, histfile, MAXPATHLEN); |
strlcpy(szFName, histfile, MAXPATHLEN); |
| |
|
| f = fopen(szFName, "r"); |
f = fopen(szFName, "r"); |
| if (!f) { | if (!f) |
| LOGERR; | return RETCODE_OK; |
| return RETCODE_ERR; | |
| } | |
| |
|
| while (fgets(buf, BUFSIZ, f)) { |
while (fgets(buf, BUFSIZ, f)) { |
| if (!*buf || *buf == '#') |
if (!*buf || *buf == '#') |
|
Line 620 void
|
Line 657 void
|
| cliEnd(linebuffer_t * __restrict buffer) |
cliEnd(linebuffer_t * __restrict buffer) |
| { |
{ |
| struct tagHistory *h; |
struct tagHistory *h; |
| |
struct tagCommand *c; |
| |
|
| if (buffer) { |
if (buffer) { |
| |
while ((c = SLIST_FIRST(&buffer->line_cmds))) { |
| |
SLIST_REMOVE_HEAD(&buffer->line_cmds, cmd_next); |
| |
free(c); |
| |
} |
| while ((h = TAILQ_FIRST(&buffer->line_history))) { |
while ((h = TAILQ_FIRST(&buffer->line_history))) { |
| TAILQ_REMOVE(&buffer->line_history, h, hist_next); |
TAILQ_REMOVE(&buffer->line_history, h, hist_next); |
| free(h); |
free(h); |
|
Line 669 cliInit(int fin, int fout, const char *prompt)
|
Line 711 cliInit(int fin, int fout, const char *prompt)
|
| buffer->line_out = fout; |
buffer->line_out = fout; |
| |
|
| TAILQ_INIT(&buffer->line_history); |
TAILQ_INIT(&buffer->line_history); |
| |
SLIST_INIT(&buffer->line_cmds); |
| |
|
| if (prompt) { |
if (prompt) { |
| buffer->line_prompt = strdup(prompt); |
buffer->line_prompt = strdup(prompt); |
|
Line 897 cliInit(int fin, int fout, const char *prompt)
|
Line 940 cliInit(int fin, int fout, const char *prompt)
|
| return buffer; |
return buffer; |
| } |
} |
| |
|
| |
|
| |
|
| /* |
/* |
| * cliNetInit() Initialize Readline if CLI bind to socket | * cliReadLine() Read line from opened CLI session |
| * @csProg = program name | * @buffer = CLI buffer |
| * @pty = Master pty | * return: NULL if error or !=NULL readed line, must be free after use! |
| * @term = stdin termios | |
| * return: none | |
| */ |
*/ |
| /* | char * |
| void cliNetInit(const char *csProg, int pty, struct termios *term) | cliReadLine(linebuffer_t * __restrict buffer) |
| { |
{ |
| struct termios t; | int code, readLen; |
| int on = 1; | register int i; |
| | struct pollfd fds; |
| | char buf[BUFSIZ], *str = NULL; |
| |
|
| memset(&t, 0, sizeof t); | if (!buffer) { |
| if (term) | cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); |
| t = *term; | return NULL; |
| else { | |
| t.c_lflag = TTYDEF_LFLAG; | |
| t.c_iflag = TTYDEF_IFLAG; | |
| t.c_oflag = TTYDEF_OFLAG; | |
| t.c_cflag = TTYDEF_CFLAG; | |
| cfsetspeed(&t, B9600); | |
| } |
} |
| |
|
| t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); | memset(&fds, 0, sizeof fds); |
| // t.c_iflag &= ~(ICRNL | BRKINT | INPCK | ISTRIP | IXON); | fds.fd = buffer->line_in; |
| t.c_iflag &= ~ICRNL; | fds.events = POLLIN; |
| t.c_iflag |= IGNBRK; | |
| t.c_cc[VMIN] = 1; | |
| t.c_cc[VTIME] = 0; | |
| tcsetattr(pty, TCSANOW, &t); | |
| |
|
| ioctl(pty, TIOCPKT, &on); | printfCR(buffer, 1); |
| | while (42) { |
| | if (poll(&fds, 1, -1) < 1) { |
| | LOGERR; |
| | return str; |
| | } |
| |
|
| rl_readline_name = csProg; | memset(buf, 0, sizeof buf); |
| rl_variable_bind("editing-mode", "emacs"); | readLen = read(buffer->line_in, buf, BUFSIZ); |
| | if (readLen == -1) { |
| | LOGERR; |
| | return str; |
| | } |
| | if (!readLen) { |
| | if (buffer->line_buf) |
| | str = strdup(buffer->line_buf); |
| | else |
| | cli_SetErr(EPIPE, "Error:: unknown state ..."); |
| | return str; |
| | } |
| |
|
| rl_instream = fdopen(pty, "r"); | recheck: |
| rl_outstream = NULL; | for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--) |
| | if (readLen >= buffer->line_keys[i].key_len && |
| | !memcmp(buffer->line_keys[i].key_ch, buf, |
| | buffer->line_keys[i].key_len)) { |
| | readLen -= buffer->line_keys[i].key_len; |
| | if (readLen) |
| | memmove(buf, buf + buffer->line_keys[i].key_len, readLen); |
| | else |
| | memset(buf, 0, buffer->line_keys[i].key_len); |
| | |
| | if (buffer->line_keys[i].key_func) |
| | if ((code = buffer->line_keys[i].key_func(i, buffer))) |
| | readLen = 0; |
| | |
| | if (readLen) |
| | goto recheck; |
| | else |
| | break; |
| | } |
| | |
| | if (code) |
| | break; |
| | } |
| | |
| | if (code != RETCODE_ERR && buffer->line_buf) |
| | str = strdup(buffer->line_buf); |
| | return str; |
| } |
} |
| */ | |
| | |
| /* |
/* |
| * cliNetExec() Execute net CLI main loop | * cliNetLoop() CLI network main loop binded to socket |
| * @cmdList = Commands list | * @buffer = CLI buffer |
| * @csPrompt = Prompt text | * @csHistFile = History file name |
| * @sock = client socket |
* @sock = client socket |
| * @term = stdin termios |
* @term = stdin termios |
| * @win = window size of tty |
* @win = window size of tty |
| * return: -1 error, 0 = exit w/^+D, 1 done. | * return: RETCODE_ERR error, RETCODE_OK ok |
| */ |
*/ |
| int |
int |
| cliNetExec(commands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) | cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock, |
| | struct termios *term, struct winsize *win) |
| { |
{ |
| int pty, ret = 0, r, s, alen, attrlen, flg; | u_char buf[BUFSIZ]; |
| | int pty, r, s, alen, attrlen, flg, ret = 0; |
| fd_set fds; |
fd_set fds; |
| struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; |
struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; |
| u_char buf[BUFSIZ]; |
|
| struct telnetAttrs *a, Attr[10]; |
struct telnetAttrs *a, Attr[10]; |
| |
|
| switch (forkpty(&pty, NULL, term, win)) { |
switch (forkpty(&pty, NULL, term, win)) { |
|
Line 965 cliNetExec(commands_t *cmdList, const char *csPrompt,
|
Line 1040 cliNetExec(commands_t *cmdList, const char *csPrompt,
|
| case 0: |
case 0: |
| close(sock); |
close(sock); |
| |
|
| ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; | // buffer = cliInit(STDIN_FILENO, STDOUT_FILENO, csPrompt); |
| | // if (!buffer) |
| | // return RETCODE_ERR; |
| | |
| | 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); |
| */ |
*/ |
| |
|
| _exit(ret); |
_exit(ret); |
| default: |
default: |
| /* spawn Shell mode */ |
/* spawn Shell mode */ |
|
Line 1045 cliNetExec(commands_t *cmdList, const char *csPrompt,
|
Line 1128 cliNetExec(commands_t *cmdList, const char *csPrompt,
|
| } |
} |
| |
|
| /* |
/* |
| * cliExec() Execute CLI main loop | * cliLoop() CLI main loop |
| * @cmdList = Commands list | * @buffer = CLI buffer |
| * @csPrompt = Prompt text | * @csHistFile = History file name |
| * return: -1 error, 0 = exit w/^+D, 1 done. | * return: RETCODE_ERR error, RETCODE_OK ok |
| */ |
*/ |
| /* | int |
| int cliExec(cliCommands_t *cmdList, const char *csPrompt) | cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile) |
| { |
{ |
| char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
| int ret = 0; |
|
| register int i; |
register int i; |
| cliCommands_t *cmd = NULL; | int ret = RETCODE_OK; |
| FILE *out; | struct tagCommand *cmd; |
| |
|
| |
/* |
| inline int inline_help() |
inline int inline_help() |
| { |
{ |
| cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL); |
cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL); |
|
Line 1096 int cliExec(cliCommands_t *cmdList, const char *csProm
|
Line 1179 int cliExec(cliCommands_t *cmdList, const char *csProm
|
| |
|
| return matches; |
return matches; |
| } |
} |
| char *cli_stdCompEntry(const char *ignore, int invoking_key) | |
| { | |
| return NULL; | */ |
| } | |
| */ | |
| /* --- main body of CLI --- */ |
/* --- main body of CLI --- */ |
| /* |
|
| out = rl_outstream; |
|
| if (!out) |
|
| out = stdout; |
|
| |
|
| rl_bind_key('?', inline_help); | if (cli_loadHistory(buffer, csHistFile) == RETCODE_ERR) |
| if (!rl_attempted_completion_function) | return RETCODE_ERR; |
| cliComp(cli_stdCompletion, cli_stdCompEntry); | |
| |
|
| do { |
do { |
| line = readline(csPrompt); | line = cliReadLine(buffer); |
| if (!line) { // ^+d | if (!line) |
| cli_Printf(out, "\n"); | |
| break; |
break; |
| } | else |
| | cli_addHistory(buffer, NULL); |
| // clear whitespaces |
// clear whitespaces |
| for (s = line; isspace(*s); s++); |
for (s = line; isspace(*s); s++); |
| if (*s) { |
if (*s) { |
|
Line 1125 int cliExec(cliCommands_t *cmdList, const char *csProm
|
Line 1202 int cliExec(cliCommands_t *cmdList, const char *csProm
|
| } |
} |
| |
|
| if (*s) { |
if (*s) { |
| add_history(s); |
|
| |
|
| memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS); |
memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS); |
| for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); |
for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); |
| *app ? app++ : app); |
*app ? app++ : app); |
| |
|
| // exec_cmd ... |
// exec_cmd ... |
| for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) | i = 0; |
| if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { | SLIST_FOREACH(cmd, &buffer->line_cmds, cmd_next) { |
| cmd = &cmdList[i]; | if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0]))) |
| break; |
break; |
| } | else |
| | i++; |
| | } |
| if (!cmd) { |
if (!cmd) { |
| cli_Printf(out, "Command '%s' not found!\n", items[0]); | cli_Printf(buffer, "Command '%s' not found!\n", items[0]); |
| ret = -1; |
ret = -1; |
| } else |
} else |
| ret = cmd->cmd_func(cmdList, i, out, items); | ret = cmd->cmd_func(buffer, i, items); |
| } |
} |
| |
|
| |
cli_freeLine(buffer); |
| |
cli_resetHistory(buffer); |
| free(line); |
free(line); |
| } while (ret < 1); |
} while (ret < 1); |
| |
|
| |
cli_saveHistory(buffer, csHistFile, HISTORY_LINES); |
| return ret; |
return ret; |
| } |
} |
| */ |
|