Diff for /libaitcli/src/aitcli.c between versions 1.1.1.1.2.3 and 1.1.1.1.2.10

version 1.1.1.1.2.3, 2010/04/19 23:02:47 version 1.1.1.1.2.10, 2010/04/28 11:23:56
Line 27  char cli_Error[STRSIZ]; Line 27  char cli_Error[STRSIZ];
 #pragma GCC visibility pop  #pragma GCC visibility pop
   
   
   static void cli_Null_Prep_Term(int meta)
   {
   }
   
   
 // cli_GetErrno() Get error code of last operation  // cli_GetErrno() Get error code of last operation
 inline int cli_GetErrno()  inline int cli_GetErrno()
 {  {
Line 57  inline void cli_SetErr(int eno, char *estr, ...) Line 62  inline void cli_SetErr(int eno, char *estr, ...)
  * cli_Printf() Printf CLI features   * cli_Printf() Printf CLI features
  * @out = Output stream   * @out = Output stream
  * @csFormat = Printf format string   * @csFormat = Printf format string
 * return: none * return: -1 error, != -1 printed chars
 */  */
 inline int cli_Printf(FILE *out, const char *csFormat, ...)  inline int cli_Printf(FILE *out, const char *csFormat, ...)
 {  {
Line 93  inline void cliComp(cli_Completion_t *cmdComplete, cli Line 98  inline void cliComp(cli_Completion_t *cmdComplete, cli
  * @term = terminal name   * @term = terminal name
  * @inp = input handle   * @inp = input handle
  * @out = output handle   * @out = output handle
    * @win = window size
    * return: -1 error, != -1 ok
   */
   inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win)
   {
           if (term)
                   rl_terminal_name = term;
   
           if (inp)
                   rl_instream = inp;
           if (out)
                   rl_outstream = out;
   
           if (win)
                  if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) {
                          LOGERR;
                          return -1;
                  }
   
           return 0;
   }
   
   /*
    * cli_ReadHistory() Read CLI History from file
    * @csFile = history file name, if NULL default history name is ".aitcli.history"
    * return: -1 error; != -1 readed ok
   */
   inline int cli_ReadHistory(const char *csFile)
   {
           return read_history(!csFile ? ".aitcli.history" : csFile);
   }
   
   /*
    * cli_WriteHistory() Write CLI History to file
    * @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
   */
   inline int cli_WriteHistory(const char *csFile, int lineNum)
   {
           int ret;
           const char *psFile = !csFile ? ".aitcli.history" : csFile;
   
           ret = write_history(psFile);
           if (-1 != ret && -1 != lineNum)
                   history_truncate_file(psFile, lineNum);
   
           return ret;
   }
   
   /*
    * cliInit() Initialize Readline
    * @csProg = program name
  * return: none   * return: none
 */  */
inline void cliTTY(const char *term, FILE *inp, FILE *out)inline void cliInit(const char *csProg)
 {  {
        rl_terminal_name = term;        rl_readline_name = csProg;
        rl_outstream = inp;
        rl_outstream = out;        rl_variable_bind("editing-mode", "emacs");
 }
 
 /*
  * cliNetInit() Initialize Readline if CLI bind to socket
  * @csProg = program name
  * @pty = Master pty
  * @term = stdin termios
  * return: none
 */
 void cliNetInit(const char *csProg, int pty, struct termios *term)
 {
         struct termios t;
         int on = 1;
 
         memset(&t, 0, sizeof t);
         if (term)
                 t = *term;
         else {
                 t.c_lflag = TTYDEF_LFLAG;
                 t.c_iflag = TTYDEF_IFLAG;
                 t.c_oflag = TTYDEF_OFLAG;
                 cfsetspeed(&t, B9600);
         }
 
         t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
         t.c_iflag &= ~(ICRNL | BRKINT | INPCK | ISTRIP | IXON);
         t.c_oflag &= ~OPOST;
         t.c_cflag &= ~PARENB;
         t.c_cc[VMIN] = 1;
         t.c_cc[VTIME] = 0;
         tcsetattr(pty, TCSANOW, &t);
 
         ioctl(pty, TIOCPKT, &on);
 
         cliInit(csProg);
         rl_instream = fdopen(pty, "r");
 }
 
 /*
  * cliNetExec() Execute net CLI main loop
  * @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;
         fd_set fds;
         struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };
         u_char buf[BUFSIZ];
         struct telnetAttrs *a, Attr[5];
         register int i;
 
         switch (forkpty(&pty, NULL, term, win)) {
                 case -1:
                         LOGERR;
                         return -1;
                 case 0:
                         close(sock);
 
         //              rl_prep_term_function = cli_Null_Prep_Term;
 
                         cliNetInit(getprogname(), STDIN_FILENO, term);
 //                      cliInit(getprogname());
                         cliTTY(NULL, NULL, NULL, win);
 //                      ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0;
                         execl("/bin/tcsh", "tcsh", NULL);
                         _exit(ret);
                 default:
                         cliNetInit(getprogname(), pty, term);
 
                         telnet_SetCmd(Attr, DO, TELOPT_TTYPE);
                         telnet_SetCmd(Attr + 1, WILL, TELOPT_SGA);
                         telnet_SetCmd(Attr + 2, DO, TELOPT_LINEMODE);
                         telnet_Set_SubOpt(Attr + 3, TELOPT_LINEMODE, LM_MODE, "\x0", 1);
                         if ((ret = telnetSend(sock, Attr, 4, NULL, 0, 0)) == -1) {
                                 cli_Errno = telnet_GetErrno();
                                 strlcpy(cli_Error, telnet_GetError(), STRSIZ);
                                 return -1;
                         }
 
                         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;
                                 }
                                 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);
                                         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);
                                         }
                                 }
                                 if (a)
                                         free(a);
 #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 ===========", ret);
                                 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);
         }
 
         return ret;
 }  }
   
 /*  /*

Removed from v.1.1.1.1.2.3  
changed lines
  Added in v.1.1.1.1.2.10


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