--- libaitcli/src/aitcli.c 2010/06/08 12:06:47 1.2.2.16 +++ libaitcli/src/aitcli.c 2010/12/07 15:15:34 1.2.2.19 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.2.2.16 2010/06/08 12:06:47 misho Exp $ +* $Id: aitcli.c,v 1.2.2.19 2010/12/07 15:15:34 misho Exp $ * *************************************************************************/ #include "global.h" @@ -906,11 +906,9 @@ cliInit(int fin, int fout, const char *prompt) linebuffer_t *buffer; bindkey_t *keys; register int i; - struct termios t; - memset(&t, 0, sizeof t); /* init buffer */ - buffer = malloc(sizeof (linebuffer_t)); + buffer = malloc(sizeof(linebuffer_t)); if (!buffer) { LOGERR; return NULL; @@ -1148,15 +1146,27 @@ cliInit(int fin, int fout, const char *prompt) memcpy(keys[i].key_ch, K_F12, keys[i].key_len); i++; + buffer->line_keys = keys; + return buffer; +} + +/* + * cliInitLine() Init CLI input line terminal + * @buffer = CLI buffer + * return: none +*/ +int +cliInitLine(linebuffer_t * __restrict buffer) +{ + struct termios t; + + memset(&t, 0, sizeof t); tcgetattr(buffer->line_in, &t); t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); t.c_iflag |= IGNBRK; t.c_cc[VMIN] = 1; t.c_cc[VTIME] = 0; - tcsetattr(buffer->line_in, TCSANOW, &t); - - buffer->line_keys = keys; - return buffer; + return tcsetattr(buffer->line_in, TCSANOW, &t); } /* @@ -1238,36 +1248,32 @@ recheck: * @buffer = CLI buffer * @csHistFile = History file name * @sock = client socket - * @term = stdin termios - * @win = window size of tty * return: RETCODE_ERR error, RETCODE_OK ok */ int -cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock, - struct termios *term, struct winsize *win) +cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock) { u_char buf[BUFSIZ]; - int pty, r, s, alen, attrlen, flg, ret = 0; + int pty, r, s, alen, flg, attrlen = 0, ret = 0; fd_set fds; struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; struct telnetAttrs *a, Attr[10]; - switch (forkpty(&pty, NULL, term, win)) { + switch (forkpty(&pty, NULL, NULL, NULL)) { case -1: LOGERR; return -1; case 0: - close(sock); - - if (buffer) - ret = cliLoop(buffer, csHistFile) < 0 ? 1 : 0; - else + if (!buffer) { cli_SetErr(EINVAL, "Error:: invalid input parameters ..."); + return -1; + } else + close(sock); - /* spawn Shell mode */ - //execl("/bin/tcsh", "tcsh", NULL); + ret = cliLoop(buffer, csHistFile) < 0 ? 1 : 0; + cliEnd(buffer); - _exit(ret); + exit(ret); default: telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE); telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO); @@ -1295,43 +1301,59 @@ cliNetLoop(linebuffer_t * __restrict buffer, const cha r = FD_ISSET(sock, &fds) ? sock : pty; s = FD_ISSET(sock, &fds) ? pty : sock; - if ((ret = telnetRecv(r, &a, &alen, buf, BUFSIZ)) < 0) { + if (FD_ISSET(sock, &fds)) { + memset(buf, 0, BUFSIZ); + if ((ret = telnetRecv(sock, &a, &alen, buf, BUFSIZ)) < 0) { + if (a) + free(a); + + if (-2 == ret) + continue; + // EOF + if (-3 == ret) + shutdown(sock, 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 (-2 == ret) - continue; - // EOF - if (-3 == ret) - shutdown(r, SHUT_RD); - else { - cli_Errno = telnet_GetErrno(); - strlcpy(cli_Error, telnet_GetError(), STRSIZ); + if ((ret = write(pty, buf, ret)) == -1) { + LOGERR; + break; } - 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++; + if (FD_ISSET(pty, &fds)) { + memset(buf, 0, BUFSIZ); + if ((ret = read(pty, buf, BUFSIZ)) == -1) { + LOGERR; + break; + } + + if ((ret = telnetSend(sock, Attr, pty == s ? 0 : attrlen, buf, ret, 0)) == -1) { + cli_Errno = telnet_GetErrno(); + strlcpy(cli_Error, telnet_GetError(), STRSIZ); + break; + } else + flg++; + } } close(pty); @@ -1355,6 +1377,7 @@ cliLoop(linebuffer_t * __restrict buffer, const char * struct tagCommand *cmd; /* --- main body of CLI --- */ + cliInitLine(buffer); if (cli_loadHistory(buffer, csHistFile) == RETCODE_ERR) return RETCODE_ERR;