--- libaitcli/src/aitcli.c 2010/04/24 10:56:08 1.1.1.1.2.9 +++ libaitcli/src/aitcli.c 2010/06/04 12:46:27 1.2.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.1.1.1.2.9 2010/04/24 10:56:08 misho Exp $ +* $Id: aitcli.c,v 1.2.2.2 2010/06/04 12:46:27 misho Exp $ * *************************************************************************/ #include "global.h" @@ -11,13 +11,15 @@ #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 ", cli_Comp_Filename }, { "-------", NULL, "---------------------", NULL, NULL }, { "help", cli_Cmd_Help, "Help screen", "help [command] ", NULL }, { "exit", cli_Cmd_Exit, "Exit from console", "exit ", NULL }, { NULL, NULL, NULL, NULL } }; +*/ // ------------------------------------------------ @@ -26,49 +28,23 @@ char cli_Error[STRSIZ]; #pragma GCC visibility pop - -static void cli_Null_Prep_Term(int meta) -{ -} - -static void cli_Null_Deprep_Term() -{ -} - -static int cli_Pre_Input_Change_Mode() -{ - return 0; -} - -static int cli_GetC(FILE *dummy) -{ - int ch = rl_getc(stdin); - - /* - if (is_special_char(ch)) { - pending_special_char = ch; - return '\r'; - } - */ - - return ch; -} - - // cli_GetErrno() Get error code of last operation -inline int cli_GetErrno() +inline int +cli_GetErrno() { return cli_Errno; } // io_GetError() Get error text of last operation -inline const char *cli_GetError() +inline const char * +cli_GetError() { return cli_Error; } // 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; @@ -81,108 +57,57 @@ inline void cli_SetErr(int eno, char *estr, ...) // ------------------------------------------------------------ -/* - * cli_Printf() Printf CLI features - * @out = Output stream - * @csFormat = Printf format string - * return: -1 error, != -1 printed chars -*/ -inline int cli_Printf(FILE *out, const char *csFormat, ...) +static inline void +clrscrEOL(linebuffer_t * __restrict buf) { - va_list lst; - int ret; + register int i; - va_start(lst, csFormat); + if (buf) { + write(buf->line_out, K_CR, 1); - ret = vfprintf(out, csFormat, lst); - if (-1 == ret) - LOGERR; - - va_end(lst); - return ret; + for (i = 0; i < buf->line_len; i++) + write(buf->line_out, K_SPACE, 1); + } } - -/* - * cliComp() Initialize completion CLI features - * @cmdComplete = Completion function - * @cmdEntry = Compentry function - * return: none -*/ -inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry) +static inline void +printfEOL(linebuffer_t * __restrict buf, int len, int prompt) { - // command completon - rl_attempted_completion_function = cmdComplete; - rl_completion_entry_function = cmdEntry; -} + if (buf) { + write(buf->line_out, K_CR, 1); -/* - * cliTTY() Initialize I/O TTY CLI features - * @term = terminal name - * @inp = input handle - * @out = output handle - * @win = window size - * return: -1 error, != -1 ok -*/ -inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win) -{ - if (term) - rl_terminal_name = term; + if (prompt && buf->line_prompt) + write(buf->line_out, buf->line_prompt, buf->line_bol); - if (inp) - rl_instream = inp; - if (out) - rl_outstream = out; - - if (win) - if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) { - LOGERR; - return -1; - } - - return 0; + write(buf->line_out, buf->line_buf, len == -1 ? buf->line_eol - buf->line_bol: len); + } } -/* - * cli_ReadHistory() Read CLI History from file - * @csFile = history file name, if NULL default history name is ".aitcli.history" - * return: -1 error; != -1 readed ok -*/ -inline int cli_ReadHistory(const char *csFile) +static inline void +printfCR(linebuffer_t * __restrict buf, int prompt) { - return read_history(!csFile ? ".aitcli.history" : csFile); -} + if (buf) { + write(buf->line_out, K_CR, 1); -/* - * cli_WriteHistory() Write CLI History to file - * @csFile = history file name, if NULL default history name is ".aitcli.history" - * @lineNum = save number of history entry lines, if -1 all lines saved without limit - * return: -1 error; != -1 readed ok -*/ -inline int cli_WriteHistory(const char *csFile, int lineNum) -{ - int ret; - const char *psFile = !csFile ? ".aitcli.history" : csFile; - - ret = write_history(psFile); - if (-1 != ret && -1 != lineNum) - history_truncate_file(psFile, lineNum); - - return ret; + if (prompt) + if (prompt && buf->line_prompt) + write(buf->line_out, buf->line_prompt, buf->line_bol); + } } -/* - * cliInit() Initialize Readline - * @csProg = program name - * return: none -*/ -inline void cliInit(const char *csProg) +static inline void +printfCLI(linebuffer_t * __restrict buf, const unsigned char *text, int textlen, int prompt) { - rl_readline_name = csProg; + if (buf && text && textlen) { + if (prompt && buf->line_prompt) + write(buf->line_out, buf->line_prompt, buf->line_bol); - rl_variable_bind("editing-mode", "emacs"); + write(buf->line_out, text, textlen); + } } +// ------------------------------------------------------------ + /* * cliNetInit() Initialize Readline if CLI bind to socket * @csProg = program name @@ -190,32 +115,40 @@ inline void cliInit(const char *csProg) * @term = stdin termios * return: none */ +/* void cliNetInit(const char *csProg, int pty, struct termios *term) { struct termios t; + int on = 1; - if (term) { + memset(&t, 0, sizeof t); + if (term) t = *term; - t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); - t.c_iflag &= ~ICRNL; - t.c_iflag |= IGNBRK; - t.c_cc[VMIN] = 1; - t.c_cc[VTIME] = 0; - tcsetattr(pty, TCSANOW, &t); + else { + t.c_lflag = TTYDEF_LFLAG; + t.c_iflag = TTYDEF_IFLAG; + t.c_oflag = TTYDEF_OFLAG; + t.c_cflag = TTYDEF_CFLAG; + cfsetspeed(&t, B9600); } - cliInit(csProg); + 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); - rl_instream = fdopen(pty, "r"); + ioctl(pty, TIOCPKT, &on); - rl_prep_term_function = cli_Null_Prep_Term; - rl_deprep_term_function = cli_Null_Deprep_Term; - rl_pre_input_hook = cli_Pre_Input_Change_Mode; + rl_readline_name = csProg; + rl_variable_bind("editing-mode", "emacs"); - rl_getc_function = cli_GetC; - + rl_instream = fdopen(pty, "r"); + rl_outstream = NULL; } - +*/ /* * cliNetExec() Execute net CLI main loop * @cmdList = Commands list @@ -225,13 +158,14 @@ void cliNetInit(const char *csProg, int pty, struct te * @win = window size of tty * return: -1 error, 0 = exit w/^+D, 1 done. */ -int cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) +int +cliNetExec(commands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) { - int pty, ret = 0, r, s, alen; + int pty, ret = 0, r, s, alen, attrlen, flg; fd_set fds; struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; u_char buf[BUFSIZ]; - struct telnetAttrs *a, Attr[3]; + struct telnetAttrs *a, Attr[10]; switch (forkpty(&pty, NULL, term, win)) { case -1: @@ -240,32 +174,44 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP case 0: close(sock); - cliTTY(NULL, NULL, NULL, win); ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; + /* spawn Shell mode */ + /* + execl("/bin/tcsh", "tcsh", NULL); + */ _exit(ret); default: - cliNetInit(getprogname(), pty, term); - - telnet_SetCmd(Attr, DO, TELOPT_TTYPE); - telnet_SetCmd(Attr + 1, DO, TELOPT_LINEMODE); - telnet_SetCmd(Attr + 2, GA, 0); - if ((ret = telnetSend(sock, Attr, 3, NULL, 0, 0)) == -1) { + /* 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 (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 1) + 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 @@ -277,21 +223,28 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP } break; } - if ((ret = telnetSend(s, NULL, 0, buf, ret, 0)) == -1) { + 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; - } - - /* - if ((ret = read(r, &ch, 1)) < 1) { - if (!ret) - shutdown(r, SHUT_RD); - break; - } - if (write(s, &ch, 1) == -1) - break; - */ + } else + flg++; } close(pty); @@ -306,6 +259,7 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP * @csPrompt = Prompt text * return: -1 error, 0 = exit w/^+D, 1 done. */ +/* int cliExec(cliCommands_t *cmdList, const char *csPrompt) { char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; @@ -355,9 +309,9 @@ int cliExec(cliCommands_t *cmdList, const char *csProm { return NULL; } - +*/ /* --- main body of CLI --- */ - +/* out = rl_outstream; if (!out) out = stdout; @@ -386,11 +340,6 @@ int cliExec(cliCommands_t *cmdList, const char *csProm for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); *app ? app++ : app); - /* - for (i = 0; i < MAX_PROMPT_ITEMS; i++) - cli_Printf(out, "i=%d %s\n", i, items[i]); - */ - // exec_cmd ... for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { @@ -409,3 +358,4 @@ int cliExec(cliCommands_t *cmdList, const char *csProm return ret; } +*/