Diff for /libaitcli/src/aitcli.c between versions 1.1.1.1.2.5 and 1.2.2.1

version 1.1.1.1.2.5, 2010/04/20 12:09:48 version 1.2.2.1, 2010/06/04 11:46:40
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
   
   
Line 31  static void cli_Null_Prep_Term(int meta) Line 33  static void cli_Null_Prep_Term(int meta)
 {  {
 }  }
   
static void cli_Null_Deprep_Term()#include <syslog.h>
 static int cli_Net_rl_GetCh(FILE *s)
 {  {
}        int ch = rl_getc(s);
   
static int cli_Pre_Input_Change_Mode()        if (!cli_pending_special_char && 0x1b == ch) {
{                cli_pending_special_char = ch;
        return 0;                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;
 }  }
   
static int cli_GetC(FILE *dummy)#if 0
 static void cli_Line_Handler(char *line)
 {  {
        int ch = rl_getc(stdin);        int len;
         static char cli_Buffer[BUFSIZ];
         static int cli_BufferLen, cli_BufferPos;
   
        /*        if (!line) {        // EOF
        if (is_special_char(ch)) {                fwrite("\x4", 1, 1, rl_outstream);      // ctrl+D
                pending_special_char = ch;                goto end;
                return '\r';        } 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';
         }          }
         */  
   
        return ch;        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
   
   
 // cli_GetErrno() Get error code of last operation  // cli_GetErrno() Get error code of last operation
Line 156  inline int cli_ReadHistory(const char *csFile) Line 209  inline int cli_ReadHistory(const char *csFile)
 /*  /*
  * cli_WriteHistory() Write CLI History to file   * cli_WriteHistory() Write CLI History to file
  * @csFile = history file name, if NULL default history name is ".aitcli.history"   * @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   * return: -1 error; != -1 readed ok
 */  */
inline int cli_WriteHistory(const char *csFile)inline int cli_WriteHistory(const char *csFile, int lineNum)
 {  {
        return write_history(!csFile ? ".aitcli.history" : csFile);        int ret;
}        const char *psFile = !csFile ? ".aitcli.history" : csFile;
   
/*        ret = write_history(psFile);
 * cliInit() Initialize Readline        if (-1 != ret && -1 != lineNum)
 * @csProg = program name                history_truncate_file(psFile, lineNum);
 * return: none 
*/ 
inline void cliInit(const char *csProg) 
{ 
        rl_readline_name = csProg; 
   
        rl_variable_bind("editing-mode", "emacs");        return ret;
 }  }
   
 /*  /*
Line 185  inline void cliInit(const char *csProg) Line 234  inline void cliInit(const char *csProg)
 void cliNetInit(const char *csProg, int pty, struct termios *term)  void cliNetInit(const char *csProg, int pty, struct termios *term)
 {  {
         struct termios t;          struct termios t;
           int on = 1;
   
        if (term) {        memset(&t, 0, sizeof t);
         if (term)
                 t = *term;                  t = *term;
                t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);        else {
                t.c_iflag &= ~ICRNL;                t.c_lflag = TTYDEF_LFLAG;
                t.c_iflag |= IGNBRK;                t.c_iflag = TTYDEF_IFLAG;
                t.c_cc[VMIN] = 1;                t.c_oflag = TTYDEF_OFLAG;
                t.c_cc[VTIME] = 0;                t.c_cflag = TTYDEF_CFLAG;
                tcsetattr(pty, TCSANOW, &t);                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);
   
           ioctl(pty, TIOCPKT, &on);
   
           rl_readline_name = csProg;
           rl_variable_bind("editing-mode", "emacs");
   
         rl_instream = fdopen(pty, "r");          rl_instream = fdopen(pty, "r");
           rl_outstream = NULL;
   }
   
        rl_prep_term_function = cli_Null_Prep_Term;/*
        rl_deprep_term_function = cli_Null_Deprep_Term; * cliNetExec() Execute net CLI main loop
        rl_pre_input_hook = cli_Pre_Input_Change_Mode; * @cmdList = Commands list
  * @csPrompt = Prompt text
  * @sock = client socket
  * @term = stdin termios
  * @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 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[10];
   
        rl_getc_function = cli_GetC;        switch (forkpty(&pty, NULL, term, win)) {
                 case -1:
                         LOGERR;
                         return -1;
                 case 0:
                         close(sock);
   
                           ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0;
                           /* spawn Shell mode */
                           /*
                           execl("/bin/tcsh", "tcsh", NULL);
                           */
                           _exit(ret);
                   default:
                           /* 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 ((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
                                           if (-3 == ret)
                                                   shutdown(r, 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 ((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);
           }
   
           return ret;
 }  }
   
 /*  /*

Removed from v.1.1.1.1.2.5  
changed lines
  Added in v.1.2.2.1


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