|
version 1.1.1.1.2.1, 2010/04/18 20:42:23
|
version 1.2.2.2, 2010/06/04 12:46:27
|
|
Line 11
|
Line 11
|
| |
|
| #pragma GCC visibility push(hidden) |
#pragma GCC visibility push(hidden) |
| |
|
| cliCommands_t cli_stdCmds[] = { | /* |
| | commands_t cli_stdCmds[] = { |
| { "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, |
{ "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, |
| { "-------", NULL, "---------------------", NULL, NULL }, |
{ "-------", NULL, "---------------------", NULL, NULL }, |
| { "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, |
{ "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, |
| { "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, |
{ "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, |
| { NULL, NULL, NULL, NULL } |
{ NULL, NULL, NULL, NULL } |
| }; |
}; |
| |
*/ |
| |
|
| // ------------------------------------------------ |
// ------------------------------------------------ |
| |
|
|
Line 26 char cli_Error[STRSIZ];
|
Line 28 char cli_Error[STRSIZ];
|
| |
|
| #pragma GCC visibility pop |
#pragma GCC visibility pop |
| |
|
| |
|
| // cli_GetErrno() Get error code of last operation |
// cli_GetErrno() Get error code of last operation |
| inline int cli_GetErrno() | inline int |
| | cli_GetErrno() |
| { |
{ |
| return cli_Errno; |
return cli_Errno; |
| } |
} |
| |
|
| // io_GetError() Get error text of last operation |
// io_GetError() Get error text of last operation |
| inline const char *cli_GetError() | inline const char * |
| | cli_GetError() |
| { |
{ |
| return cli_Error; |
return cli_Error; |
| } |
} |
| |
|
| // cli_SetErr() Set error to variables for internal use!!! |
// cli_SetErr() Set error to variables for internal use!!! |
| inline void cli_SetErr(int eno, char *estr, ...) | inline void |
| | cli_SetErr(int eno, char *estr, ...) |
| { |
{ |
| va_list lst; |
va_list lst; |
| |
|
|
Line 53 inline void cli_SetErr(int eno, char *estr, ...)
|
Line 57 inline void cli_SetErr(int eno, char *estr, ...)
|
| |
|
| // ------------------------------------------------------------ |
// ------------------------------------------------------------ |
| |
|
| /* | static inline void |
| * cli_Printf() Printf CLI features | clrscrEOL(linebuffer_t * __restrict buf) |
| * @out = Output stream | |
| * @csFormat = Printf format string | |
| * return: none | |
| */ | |
| inline int cli_Printf(FILE *out, const char *csFormat, ...) | |
| { |
{ |
| va_list lst; | register int i; |
| int ret; | |
| |
|
| va_start(lst, csFormat); | if (buf) { |
| | write(buf->line_out, K_CR, 1); |
| |
|
| ret = vfprintf(out, csFormat, lst); | for (i = 0; i < buf->line_len; i++) |
| if (-1 == ret) | write(buf->line_out, K_SPACE, 1); |
| LOGERR; | } |
| | } |
| |
|
| va_end(lst); | static inline void |
| return ret; | printfEOL(linebuffer_t * __restrict buf, int len, int prompt) |
| | { |
| | if (buf) { |
| | write(buf->line_out, K_CR, 1); |
| | |
| | if (prompt && buf->line_prompt) |
| | write(buf->line_out, buf->line_prompt, buf->line_bol); |
| | |
| | write(buf->line_out, buf->line_buf, len == -1 ? buf->line_eol - buf->line_bol: len); |
| | } |
| } |
} |
| |
|
| |
static inline void |
| |
printfCR(linebuffer_t * __restrict buf, int prompt) |
| |
{ |
| |
if (buf) { |
| |
write(buf->line_out, K_CR, 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) |
| |
{ |
| |
if (buf && text && textlen) { |
| |
if (prompt && buf->line_prompt) |
| |
write(buf->line_out, buf->line_prompt, buf->line_bol); |
| |
|
| |
write(buf->line_out, text, textlen); |
| |
} |
| |
} |
| |
|
| |
// ------------------------------------------------------------ |
| |
|
| /* |
/* |
| * cliComp() Initialize completion CLI features | * cliNetInit() Initialize Readline if CLI bind to socket |
| * @cmdComplete = Completion function | * @csProg = program name |
| * @cmdEntry = Compentry function | * @pty = Master pty |
| | * @term = stdin termios |
| * return: none |
* return: none |
| */ |
*/ |
| inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry) | /* |
| | void cliNetInit(const char *csProg, int pty, struct termios *term) |
| { |
{ |
| // command completon | struct termios t; |
| rl_attempted_completion_function = cmdComplete; | int on = 1; |
| rl_completion_entry_function = cmdEntry; | |
| } | |
| |
|
| |
memset(&t, 0, sizeof t); |
| |
if (term) |
| |
t = *term; |
| |
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); |
| |
// t.c_iflag &= ~(ICRNL | BRKINT | INPCK | ISTRIP | IXON); |
| |
t.c_iflag &= ~ICRNL; |
| |
t.c_iflag |= IGNBRK; |
| |
t.c_cc[VMIN] = 1; |
| |
t.c_cc[VTIME] = 0; |
| |
tcsetattr(pty, TCSANOW, &t); |
| |
|
| |
ioctl(pty, TIOCPKT, &on); |
| |
|
| |
rl_readline_name = csProg; |
| |
rl_variable_bind("editing-mode", "emacs"); |
| |
|
| |
rl_instream = fdopen(pty, "r"); |
| |
rl_outstream = NULL; |
| |
} |
| |
*/ |
| /* |
/* |
| * cliTTY() Initialize I/O TTY CLI features | * cliNetExec() Execute net CLI main loop |
| * @inp = input handle | * @cmdList = Commands list |
| * @out = output handle | * @csPrompt = Prompt text |
| * return: none | * @sock = client socket |
| | * @term = stdin termios |
| | * @win = window size of tty |
| | * return: -1 error, 0 = exit w/^+D, 1 done. |
| */ |
*/ |
| inline void cliTTY(FILE *inp, FILE *out) | int |
| | cliNetExec(commands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) |
| { |
{ |
| rl_outstream = inp; | int pty, ret = 0, r, s, alen, attrlen, flg; |
| rl_outstream = out; | fd_set fds; |
| | struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; |
| | u_char buf[BUFSIZ]; |
| | struct telnetAttrs *a, Attr[10]; |
| | |
| | switch (forkpty(&pty, NULL, term, win)) { |
| | case -1: |
| | LOGERR; |
| | return -1; |
| | case 0: |
| | close(sock); |
| | |
| | ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; |
| | /* spawn Shell mode */ |
| | /* |
| | execl("/bin/tcsh", "tcsh", NULL); |
| | */ |
| | _exit(ret); |
| | default: |
| | /* spawn Shell mode */ |
| | telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE); |
| | telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO); |
| | telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0); |
| | telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0); |
| | telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE); |
| | if ((ret = telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1) { |
| | cli_Errno = telnet_GetErrno(); |
| | strlcpy(cli_Error, telnet_GetError(), STRSIZ); |
| | return -1; |
| | } else |
| | flg = 0; |
| | |
| | while (42) { |
| | FD_ZERO(&fds); |
| | FD_SET(sock, &fds); |
| | FD_SET(pty, &fds); |
| | if ((ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv)) < 1) { |
| | if (!ret) |
| | cli_SetErr(ETIMEDOUT, "Client session timeout ..."); |
| | |
| | break; |
| | } |
| | |
| | r = FD_ISSET(sock, &fds) ? sock : pty; |
| | s = FD_ISSET(sock, &fds) ? pty : sock; |
| | |
| | if ((ret = telnetRecv(r, &a, &alen, buf, BUFSIZ)) < 0) { |
| | if (a) |
| | free(a); |
| | |
| | if (-2 == ret) |
| | continue; |
| | // EOF |
| | if (-3 == ret) |
| | shutdown(r, SHUT_RD); |
| | else { |
| | cli_Errno = telnet_GetErrno(); |
| | strlcpy(cli_Error, telnet_GetError(), STRSIZ); |
| | } |
| | break; |
| | } |
| | attrlen = 0; |
| | if (1 == flg && alen) { |
| | telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_SGA); |
| | telnet_SetCmd(&Attr[attrlen++], DO, TELOPT_ECHO); |
| | } |
| | if (2 == flg && alen) { |
| | telnet_SetCmd(&Attr[attrlen++], WILL, TELOPT_ECHO); |
| | telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW, |
| | LFLOW_OFF, NULL, 0); |
| | telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW, |
| | LFLOW_RESTART_XON, NULL, 0); |
| | telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_LINEMODE); |
| | } |
| | if (a) |
| | free(a); |
| | |
| | if ((ret = telnetSend(s, Attr, pty == s ? 0 : attrlen, buf, ret, 0)) == -1) { |
| | cli_Errno = telnet_GetErrno(); |
| | strlcpy(cli_Error, telnet_GetError(), STRSIZ); |
| | break; |
| | } else |
| | flg++; |
| | } |
| | |
| | close(pty); |
| | } |
| | |
| | return ret; |
| } |
} |
| |
|
| /* |
/* |
| * cliExec() Execute CLI main loop |
* cliExec() Execute CLI main loop |
| * @cmdList = Commands list |
* @cmdList = Commands list |
| * @out = Output handle |
|
| * @csPrompt = Prompt text |
* @csPrompt = Prompt text |
| * return: -1 error, 0 = exit w/^+D, 1 done. |
* return: -1 error, 0 = exit w/^+D, 1 done. |
| */ |
*/ |
| int cliExec(cliCommands_t *cmdList, FILE *out, const char *csPrompt) | /* |
| | int cliExec(cliCommands_t *cmdList, const char *csPrompt) |
| { |
{ |
| char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
| int ret = 0; |
int ret = 0; |
| register int i; |
register int i; |
| cliCommands_t *cmd = NULL; |
cliCommands_t *cmd = NULL; |
| |
FILE *out; |
| |
|
| inline int inline_help() |
inline int inline_help() |
| { |
{ |
|
Line 155 int cliExec(cliCommands_t *cmdList, FILE *out, const c
|
Line 309 int cliExec(cliCommands_t *cmdList, FILE *out, const c
|
| { |
{ |
| return NULL; |
return NULL; |
| } |
} |
| */ |
| /* --- main body of CLI --- */ |
/* --- main body of CLI --- */ |
| |
/* |
| |
out = rl_outstream; |
| |
if (!out) |
| |
out = stdout; |
| |
|
| rl_bind_key('?', inline_help); |
rl_bind_key('?', inline_help); |
| if (!rl_attempted_completion_function) |
if (!rl_attempted_completion_function) |
|
Line 182 int cliExec(cliCommands_t *cmdList, FILE *out, const c
|
Line 340 int cliExec(cliCommands_t *cmdList, FILE *out, const c
|
| 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); |
| |
|
| /* |
|
| for (i = 0; i < MAX_PROMPT_ITEMS; i++) |
|
| cli_Printf(out, "i=%d %s\n", i, items[i]); |
|
| */ |
|
| |
|
| // exec_cmd ... |
// exec_cmd ... |
| for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) |
for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) |
| if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { |
if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { |
|
Line 205 int cliExec(cliCommands_t *cmdList, FILE *out, const c
|
Line 358 int cliExec(cliCommands_t *cmdList, FILE *out, const c
|
| |
|
| return ret; |
return ret; |
| } |
} |
| |
*/ |