--- libaitcli/src/aitcli.c 2010/06/04 11:30:31 1.1.1.1.2.17 +++ libaitcli/src/aitcli.c 2010/06/04 11:46:40 1.2.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.1.1.1.2.17 2010/06/04 11:30:31 misho Exp $ +* $Id: aitcli.c,v 1.2.2.1 2010/06/04 11:46:40 misho Exp $ * *************************************************************************/ #include "global.h" @@ -33,6 +33,7 @@ static void cli_Null_Prep_Term(int meta) { } +#include static int cli_Net_rl_GetCh(FILE *s) { int ch = rl_getc(s); @@ -50,6 +51,7 @@ static int cli_Net_rl_GetCh(FILE *s) return ch; } + syslog(LOG_CRIT, "+++++ getc=%0x\n", ch); cli_pending_special_char = 0; fputc(ch, rl_outstream); fflush(rl_outstream); @@ -271,9 +273,10 @@ 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(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) { - int pty, ret = 0, alen, attrlen, flg; + int pty, ret = 0, r, s, alen, attrlen, flg; fd_set fds; struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; u_char buf[BUFSIZ]; @@ -286,10 +289,6 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP case 0: close(sock); - rl_prep_term_function = cli_Null_Prep_Term; - rl_getc_function = cli_Net_rl_GetCh; - - cliNetInit(getprogname(), STDIN_FILENO, term); ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; /* spawn Shell mode */ /* @@ -297,21 +296,12 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP */ _exit(ret); default: - cliNetInit(getprogname(), pty, term); - /* 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); - */ - 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); @@ -330,80 +320,46 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP break; } - if (FD_ISSET(sock, &fds)) { - memset(buf, 0, BUFSIZ); - if ((ret = telnetRecv(sock, &a, &alen, buf, BUFSIZ)) < 0) { - if (a) - free(a); + r = FD_ISSET(sock, &fds) ? sock : pty; + s = FD_ISSET(sock, &fds) ? pty : sock; - if (-2 == ret) - continue; - // EOF - if (-3 == ret) - shutdown(sock, SHUT_RD); - else { - cli_Errno = telnet_GetErrno(); - strlcpy(cli_Error, telnet_GetError(), STRSIZ); - } - break; - } + if ((ret = telnetRecv(r, &a, &alen, buf, BUFSIZ)) < 0) { if (a) free(a); - if (alen) { - 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 ((ret = telnetSend(sock, Attr, attrlen, buf, ret, 0)) == -1) { - cli_Errno = telnet_GetErrno(); - strlcpy(cli_Error, telnet_GetError(), STRSIZ); - break; - } - } - if ((ret = write(pty, buf, ret)) == -1) { - LOGERR; - break; - } - } - - if (FD_ISSET(pty, &fds)) { - memset(buf, 0, BUFSIZ); - if ((ret = read(pty, buf, BUFSIZ)) < 1) { - if (!ret) - shutdown(sock, SHUT_WR); - else - LOGERR; - 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 ((ret = telnetSend(sock, Attr, attrlen, buf, ret, 0)) == -1) { + if (-2 == ret) + continue; + // EOF + if (-3 == ret) + shutdown(r, SHUT_RD); + else { cli_Errno = telnet_GetErrno(); strlcpy(cli_Error, telnet_GetError(), STRSIZ); - 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++; } close(pty);