--- libaitcli/src/aitcli.c 2010/06/04 11:30:31 1.1.1.1.2.17 +++ libaitcli/src/aitcli.c 2010/06/04 11:32:47 1.2 @@ -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 2010/06/04 11:32:47 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); @@ -273,7 +275,7 @@ void cliNetInit(const char *csProg, int pty, struct te */ 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,7 +288,7 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP case 0: close(sock); - rl_prep_term_function = cli_Null_Prep_Term; +// rl_prep_term_function = cli_Null_Prep_Term; rl_getc_function = cli_Net_rl_GetCh; cliNetInit(getprogname(), STDIN_FILENO, term); @@ -297,6 +299,8 @@ int cliNetExec(cliCommands_t *cmdList, const char *csP */ _exit(ret); default: + rl_prep_term_function = cli_Null_Prep_Term; + cliNetInit(getprogname(), pty, term); /* spawn Shell mode */ @@ -330,80 +334,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);