Diff for /libaitcli/src/aitcli.c between versions 1.1.1.1.2.12 and 1.1.1.1.2.16

version 1.1.1.1.2.12, 2010/04/28 15:27:55 version 1.1.1.1.2.16, 2010/06/04 11:25:45
Line 24  cliCommands_t cli_stdCmds[] = { Line 24  cliCommands_t cli_stdCmds[] = {
 int cli_Errno;  int cli_Errno;
 char cli_Error[STRSIZ];  char cli_Error[STRSIZ];
   
   char cli_pending_special_char;
   
 #pragma GCC visibility pop  #pragma GCC visibility pop
   
   
 #ifdef NULL_PREP_TERM  
   
 static void cli_Null_Prep_Term(int meta)  static void cli_Null_Prep_Term(int meta)
 {  {
 }  }
   
   #include <syslog.h>
   static int cli_Net_rl_GetCh(FILE *s)
   {
           int ch = rl_getc(s);
   
           if (!cli_pending_special_char && 0x1b == ch) {
                   cli_pending_special_char = ch;
                   return ch;
           }
           if (cli_pending_special_char && 0x5b == ch) {
                   cli_pending_special_char = ch;
                   return ch;
           }
           if (0x5b == cli_pending_special_char) {
                   cli_pending_special_char = 0;
                   return ch;
           }
   
           syslog(LOG_CRIT, "+++++ getc=%0x\n", ch);
           cli_pending_special_char = 0;
           fputc(ch, rl_outstream);
           fflush(rl_outstream);
           return ch;
   }
   
   #if 0
   static void cli_Line_Handler(char *line)
   {
           int len;
           static char cli_Buffer[BUFSIZ];
           static int cli_BufferLen, cli_BufferPos;
   
           if (!line) {    // EOF
                   fwrite("\x4", 1, 1, rl_outstream);      // ctrl+D
                   goto end;
           } else
                   len = strlen(line);
           if (BUFSIZ - 2 < len)
                   cli_BufferPos = cli_BufferLen = 0;
           else {
                   if (BUFSIZ - 2 < len + cli_BufferLen) {
                           if (BUFSIZ - 2 >= cli_BufferLen - cli_BufferPos + len) {
                                   cli_BufferLen -= cli_BufferPos;
                                   memmove(cli_Buffer, cli_Buffer + cli_BufferPos, cli_BufferLen);
                           } else
                                   cli_BufferLen = 0;
   
                           cli_BufferPos = 0;
                   }
   
                   memcpy(cli_Buffer + cli_BufferLen, line, len);
                   cli_BufferLen += len;
                   cli_Buffer[cli_BufferLen++] = '\r';
                   cli_Buffer[cli_BufferLen++] = '\n';
           }
   
           fwrite(cli_Buffer, 1, cli_BufferLen, rl_outstream);
           if (!cli_pending_special_char) {
                   fwrite("\r", 1, 1, rl_outstream);
                   if (*line)
                           add_history(line);
           }
   
           free(line);
   end:
           rl_callback_handler_remove();
           if (cli_pending_special_char) {
                   fwrite(&cli_pending_special_char, 1, 1, rl_outstream);
                   cli_pending_special_char = 0;
           }
           fflush(rl_outstream);
   }
 #endif  #endif
   
   
Line 171  void cliNetInit(const char *csProg, int pty, struct te Line 243  void cliNetInit(const char *csProg, int pty, struct te
                 t.c_lflag = TTYDEF_LFLAG;                  t.c_lflag = TTYDEF_LFLAG;
                 t.c_iflag = TTYDEF_IFLAG;                  t.c_iflag = TTYDEF_IFLAG;
                 t.c_oflag = TTYDEF_OFLAG;                  t.c_oflag = TTYDEF_OFLAG;
                   t.c_cflag = TTYDEF_CFLAG;
                 cfsetspeed(&t, B9600);                  cfsetspeed(&t, B9600);
         }          }
   
         t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);          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 | BRKINT | INPCK | ISTRIP | IXON);
        t.c_oflag &= ~OPOST;        t.c_iflag &= ~ICRNL;
        t.c_cflag &= ~PARENB;        t.c_iflag |= IGNBRK;
         t.c_cc[VMIN] = 1;          t.c_cc[VMIN] = 1;
         t.c_cc[VTIME] = 0;          t.c_cc[VTIME] = 0;
         tcsetattr(pty, TCSANOW, &t);          tcsetattr(pty, TCSANOW, &t);
Line 187  void cliNetInit(const char *csProg, int pty, struct te Line 260  void cliNetInit(const char *csProg, int pty, struct te
         rl_readline_name = csProg;          rl_readline_name = csProg;
         rl_variable_bind("editing-mode", "emacs");          rl_variable_bind("editing-mode", "emacs");
   
 #ifdef NULL_PREP_TERM  
         rl_instream = fdopen(pty, "r");          rl_instream = fdopen(pty, "r");
#endif        rl_outstream = NULL;
 }  }
   
 /*  /*
Line 203  void cliNetInit(const char *csProg, int pty, struct te Line 275  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 cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win)
 {  {
        int pty, ret = 0, r, s, alen, attrlen;        int pty, ret = 0, alen, attrlen, flg;
         fd_set fds;          fd_set fds;
         struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };          struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };
         u_char buf[BUFSIZ];          u_char buf[BUFSIZ];
         struct telnetAttrs *a, Attr[10];          struct telnetAttrs *a, Attr[10];
         register int i;  
   
         switch (forkpty(&pty, NULL, term, win)) {          switch (forkpty(&pty, NULL, term, win)) {
                 case -1:                  case -1:
Line 217  int cliNetExec(cliCommands_t *cmdList, const char *csP Line 288  int cliNetExec(cliCommands_t *cmdList, const char *csP
                 case 0:                  case 0:
                         close(sock);                          close(sock);
   
 #ifdef NULL_PREP_TERM  
                         rl_prep_term_function = cli_Null_Prep_Term;                          rl_prep_term_function = cli_Null_Prep_Term;
#endif                        rl_getc_function = cli_Net_rl_GetCh;
   
                         cliNetInit(getprogname(), STDIN_FILENO, term);                          cliNetInit(getprogname(), STDIN_FILENO, term);
                        cliTTY(NULL, NULL, NULL, win);                        ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0;
//                        ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0;                        /* spawn Shell mode */
                         /*
                         execl("/bin/tcsh", "tcsh", NULL);                          execl("/bin/tcsh", "tcsh", NULL);
                           */
                         _exit(ret);                          _exit(ret);
                 default:                  default:
                        telnet_SetCmd(Attr, DO, TELOPT_TTYPE);                        cliNetInit(getprogname(), pty, term);
                        telnet_SetCmd(Attr + 1, DO, TELOPT_LFLOW);
                         /* 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 + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0);
                        telnet_SetCmd(Attr + 3, DO, TELOPT_BINARY);                        telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0);
                        telnet_SetCmd(Attr + 4, DO, TELOPT_NAWS);                        telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE);
                        telnet_SetCmd(Attr + 5, DO, TELOPT_STATUS);                        */
                        telnet_SetCmd(Attr + 6, DONT, TELOPT_ECHO);                        telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE);
#ifdef TELNET_LINEMODE_ENABLE                        telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO);
                        telnet_SetCmd(Attr + 7, DO, TELOPT_LINEMODE);                        telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0);
                        telnet_Set_SubOpt(Attr + 8, TELOPT_LINEMODE, LM_MODE, "\x0", 1);                        telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0);
                        if ((ret = telnetSend(sock, Attr, 9, NULL, 0, 0)) == -1) {                        telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE);
#else                        if ((ret = telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1) {
                        if ((ret = telnetSend(sock, Attr, 7, NULL, 0, 0)) == -1) { 
#endif 
                                 cli_Errno = telnet_GetErrno();                                  cli_Errno = telnet_GetErrno();
                                 strlcpy(cli_Error, telnet_GetError(), STRSIZ);                                  strlcpy(cli_Error, telnet_GetError(), STRSIZ);
                                 return -1;                                  return -1;
                        }                        } else
                                 flg = 0;
   
                         while (42) {                          while (42) {
                                 FD_ZERO(&fds);                                  FD_ZERO(&fds);
Line 257  int cliNetExec(cliCommands_t *cmdList, const char *csP Line 332  int cliNetExec(cliCommands_t *cmdList, const char *csP
                                         break;                                          break;
                                 }                                  }
   
                                r = FD_ISSET(sock, &fds) ? sock : pty;                                if (FD_ISSET(sock, &fds)) {
                                s = FD_ISSET(sock, &fds) ? pty : sock;                                        memset(buf, 0, BUFSIZ);
                                         if ((ret = telnetRecv(sock, &a, &alen, buf, BUFSIZ)) < 0) {
                                                 if (a)
                                                         free(a);
   
                                if ((ret = telnetRecv(r, &a, &alen, buf, BUFSIZ)) < 0) {                                                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 (a)                                          if (a)
                                                 free(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;
                                                   }
                                           }
   syslog(LOG_CRIT, "alen=%d ret=%d b0=%x b1=%x b2=%x\n", alen, ret, buf[0], buf[1], buf[2]);
   
                                        if (-2 == ret)                                        if ((ret = write(pty, buf, ret)) == -1) {
                                                continue;                                                LOGERR;
                                        // EOF                                                break;
                                        if (-3 == ret)                                        }
                                                shutdown(r, SHUT_RD);                                }
                                        else {
                                 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;
                                         }
 syslog(LOG_CRIT, "ret=%d b0=%x b1=%x b2=%x\n", ret, buf[0], buf[1], buf[2]);
                                         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();                                                  cli_Errno = telnet_GetErrno();
                                                 strlcpy(cli_Error, telnet_GetError(), STRSIZ);                                                  strlcpy(cli_Error, telnet_GetError(), STRSIZ);
                                                   break;
                                         }                                          }
                                         break;  
                                 }                                  }
                                 for (attrlen = i = 0; i < alen; i++) {  
                                         if (TELOPT_TTYPE == a[i].ta_opt && WILL == a[i].ta_cmd)  
                                                 telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_TTYPE, TELQUAL_SEND, NULL, 0);  
 #ifdef TELNET_LINEMODE_ENABLE  
                                         if (TELOPT_LINEMODE == a[i].ta_cmd && SB == a[i].ta_cmd) {  
                                                 telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LINEMODE, LM_SLC,   
                                                                 "\x1\x0\x0\x3\xe2\x3\x4\x0\x0\x5\x0\x0\x7\xe2\x1c"  
                                                                 "\x8\x82\x4\x9\x0\x0\xa\x82\x7f\xb\x82\x15\xc\x82\x17"  
                                                                 "\xd\x82\x12\xe\x82\x16\xf\x82\x11\x10\x82\x13", 42);  
                                         }  
 #endif  
                                 }  
                                 if (a)  
                                         free(a);  
   
                                 /*  
                                 if (s == sock && ret > 2 && 0x1b == buf[0] && 0x5b == buf[1]) {  
                                         memmove(buf, buf + 3, ret);  
                                         ret -= 3;  
                                 }  
                                 */  
 #include "syslog.h"  
                                 int j;  
                                 for (j = 0; j < ret; j++)  
                                         syslog(LOG_CRIT, "prepare to send %d = %X", j, buf[j]);  
                                 syslog(LOG_CRIT, "send packet %d === %s ========", ret, r == pty ? "pty -> sock" : "sock -> pty");  
                                 if ((ret = telnetSend(s, Attr, 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;  
                                 */  
                         }                          }
   
                         close(pty);                          close(pty);

Removed from v.1.1.1.1.2.12  
changed lines
  Added in v.1.1.1.1.2.16


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>