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

version 1.1.1.1.2.10, 2010/04/28 11:23:56 version 1.2.2.2, 2010/06/04 12:46:27
Line 11 Line 11
   
 #pragma GCC visibility push(hidden)  #pragma GCC visibility push(hidden)
   
cliCommands_t cli_stdCmds[] = {/*
 commands_t cli_stdCmds[] = {
         { "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename },           { "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, 
         { "-------", NULL, "---------------------", NULL, NULL },           { "-------", NULL, "---------------------", NULL, NULL }, 
         { "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL },           { "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, 
         { "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL },           { "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, 
         { NULL, NULL, NULL, NULL }          { NULL, NULL, NULL, NULL }
 };  };
   */
   
 // ------------------------------------------------  // ------------------------------------------------
   
Line 26  char cli_Error[STRSIZ]; Line 28  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()
 {  {
         return cli_Errno;          return cli_Errno;
 }  }
   
 // io_GetError() Get error text of last operation  // io_GetError() Get error text of last operation
inline const char *cli_GetError()inline const char *
 cli_GetError()
 {  {
         return cli_Error;          return cli_Error;
 }  }
   
 // cli_SetErr() Set error to variables for internal use!!!  // cli_SetErr() Set error to variables for internal use!!!
inline void cli_SetErr(int eno, char *estr, ...)inline void
 cli_SetErr(int eno, char *estr, ...)
 {  {
         va_list lst;          va_list lst;
   
Line 58  inline void cli_SetErr(int eno, char *estr, ...) Line 57  inline void cli_SetErr(int eno, char *estr, ...)
   
 // ------------------------------------------------------------  // ------------------------------------------------------------
   
/*static inline void
 * cli_Printf() Printf CLI featuresclrscrEOL(linebuffer_t * __restrict buf)
 * @out = Output stream 
 * @csFormat = Printf format string 
 * return: -1 error, != -1 printed chars 
*/ 
inline int cli_Printf(FILE *out, const char *csFormat, ...) 
 {  {
        va_list lst;        register int i;
        int ret; 
   
        va_start(lst, csFormat);        if (buf) {
                 write(buf->line_out, K_CR, 1);
   
        ret = vfprintf(out, csFormat, lst);                for (i = 0; i < buf->line_len; i++)
        if (-1 == ret)                        write(buf->line_out, K_SPACE, 1);
                LOGERR;        }
 
        va_end(lst); 
        return ret; 
 }  }
   
static inline void
/*printfEOL(linebuffer_t * __restrict buf, int len, int prompt)
 * cliComp() Initialize completion CLI features 
 * @cmdComplete = Completion function 
 * @cmdEntry = Compentry function 
 * return: none 
*/ 
inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry) 
 {  {
        // command completon        if (buf) {
        rl_attempted_completion_function = cmdComplete;                write(buf->line_out, K_CR, 1);
        rl_completion_entry_function = cmdEntry; 
} 
   
/*                if (prompt && buf->line_prompt)
 * cliTTY() Initialize I/O TTY CLI features                        write(buf->line_out, buf->line_prompt, buf->line_bol);
 * @term = terminal name 
 * @inp = input 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)                write(buf->line_out, buf->line_buf, len == -1 ? buf->line_eol - buf->line_bol: len);
                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; 
 }  }
   
/*static inline void
 * cli_ReadHistory() Read CLI History from fileprintfCR(linebuffer_t * __restrict buf, int prompt)
 * @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);        if (buf) {
}                write(buf->line_out, K_CR, 1);
   
/*                if (prompt)
 * cli_WriteHistory() Write CLI History to file                        if (prompt && buf->line_prompt)
 * @csFile = history file name, if NULL default history name is ".aitcli.history"                                write(buf->line_out, buf->line_prompt, buf->line_bol);
 * @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; 
 }  }
   
/*static inline void
 * cliInit() Initialize ReadlineprintfCLI(linebuffer_t * __restrict buf, const unsigned char *text, int textlen, int prompt)
 * @csProg = program name 
 * return: none 
*/ 
inline void cliInit(const char *csProg) 
 {  {
        rl_readline_name = csProg;        if (buf && text && textlen) {
                 if (prompt && buf->line_prompt)
                         write(buf->line_out, buf->line_prompt, buf->line_bol);
   
        rl_variable_bind("editing-mode", "emacs");                write(buf->line_out, text, textlen);
         }
 }  }
   
   // ------------------------------------------------------------
   
 /*  /*
  * cliNetInit() Initialize Readline if CLI bind to socket   * cliNetInit() Initialize Readline if CLI bind to socket
  * @csProg = program name   * @csProg = program name
Line 167  inline void cliInit(const char *csProg) Line 115  inline void cliInit(const char *csProg)
  * @term = stdin termios   * @term = stdin termios
  * return: none   * return: none
 */  */
   /*
 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;
Line 179  void cliNetInit(const char *csProg, int pty, struct te Line 128  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);
   
         ioctl(pty, TIOCPKT, &on);          ioctl(pty, TIOCPKT, &on);
   
        cliInit(csProg);        rl_readline_name = csProg;
         rl_variable_bind("editing-mode", "emacs");
 
         rl_instream = fdopen(pty, "r");          rl_instream = fdopen(pty, "r");
           rl_outstream = NULL;
 }  }
*/
 /*  /*
  * cliNetExec() Execute net CLI main loop   * cliNetExec() Execute net CLI main loop
  * @cmdList = Commands list   * @cmdList = Commands list
Line 205  void cliNetInit(const char *csProg, int pty, struct te Line 158  void cliNetInit(const char *csProg, int pty, struct te
  * @win = window size of tty   * @win = window size of tty
  * return: -1 error, 0 = exit w/^+D, 1 done.   * 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(commands_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, r, s, 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[5];        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 221  int cliNetExec(cliCommands_t *cmdList, const char *csP Line 174  int cliNetExec(cliCommands_t *cmdList, const char *csP
                 case 0:                  case 0:
                         close(sock);                          close(sock);
   
        //              rl_prep_term_function = cli_Null_Prep_Term;                        ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0;
                        /* spawn Shell mode */
                        cliNetInit(getprogname(), STDIN_FILENO, term);                        /*
//                      cliInit(getprogname()); 
                        cliTTY(NULL, NULL, NULL, win); 
//                        ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; 
                         execl("/bin/tcsh", "tcsh", NULL);                          execl("/bin/tcsh", "tcsh", NULL);
                           */
                         _exit(ret);                          _exit(ret);
                 default:                  default:
                        cliNetInit(getprogname(), pty, term);                        /* spawn Shell mode */
                        telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE);
                        telnet_SetCmd(Attr, DO, TELOPT_TTYPE);                        telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO);
                        telnet_SetCmd(Attr + 1, WILL, TELOPT_SGA);                        telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0);
                        telnet_SetCmd(Attr + 2, DO, TELOPT_LINEMODE);                        telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0);
                        telnet_Set_SubOpt(Attr + 3, TELOPT_LINEMODE, LM_MODE, "\x0", 1);                        telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE);
                        if ((ret = telnetSend(sock, Attr, 4, NULL, 0, 0)) == -1) {                        if ((ret = telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1) {
                                 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 271  int cliNetExec(cliCommands_t *cmdList, const char *csP Line 223  int cliNetExec(cliCommands_t *cmdList, const char *csP
                                         }                                          }
                                         break;                                          break;
                                 }                                  }
                                for (attrlen = i = 0; i < alen; i++) {                                attrlen = 0;
                                        if (TELOPT_TTYPE == a[i].ta_opt && WILL == a[i].ta_cmd)                                if (1 == flg && alen) {
                                                telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_TTYPE, TELQUAL_SEND, NULL, 0);                                        telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_SGA);
                                        if (TELOPT_LINEMODE == a[i].ta_cmd && SB == a[i].ta_cmd) {                                        telnet_SetCmd(&Attr[attrlen++], DO, TELOPT_ECHO);
                                                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 (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)                                  if (a)
                                         free(a);                                          free(a);
#include "syslog.h"
                                int j;                                if ((ret = telnetSend(s, Attr, pty == s ? 0 : attrlen, buf, ret, 0)) == -1) {
                                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();                                          cli_Errno = telnet_GetErrno();
                                         strlcpy(cli_Error, telnet_GetError(), STRSIZ);                                          strlcpy(cli_Error, telnet_GetError(), STRSIZ);
                                         break;                                          break;
                                }                                } else
                                        flg++;
                                /* 
                                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);
Line 317  int cliNetExec(cliCommands_t *cmdList, const char *csP Line 259  int cliNetExec(cliCommands_t *cmdList, const char *csP
  * @csPrompt = Prompt text   * @csPrompt = Prompt text
  * return: -1 error, 0 = exit w/^+D, 1 done.   * return: -1 error, 0 = exit w/^+D, 1 done.
 */  */
   /*
 int cliExec(cliCommands_t *cmdList, const char *csPrompt)  int cliExec(cliCommands_t *cmdList, const char *csPrompt)
 {  {
         char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];          char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
Line 366  int cliExec(cliCommands_t *cmdList, const char *csProm Line 309  int cliExec(cliCommands_t *cmdList, const char *csProm
         {          {
                 return NULL;                  return NULL;
         }          }
*/
         /* --- main body of CLI --- */          /* --- main body of CLI --- */
/*
         out = rl_outstream;          out = rl_outstream;
         if (!out)          if (!out)
                 out = stdout;                  out = stdout;
Line 397  int cliExec(cliCommands_t *cmdList, const char *csProm Line 340  int cliExec(cliCommands_t *cmdList, const char *csProm
                         for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t"));                           for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); 
                                         *app ? app++ : app);                                          *app ? app++ : app);
   
                         /*  
                         for (i = 0; i < MAX_PROMPT_ITEMS; i++)  
                                 cli_Printf(out, "i=%d %s\n", i, items[i]);  
                                 */  
   
                         // exec_cmd ...                          // exec_cmd ...
                         for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)                          for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
                                 if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {                                  if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
Line 420  int cliExec(cliCommands_t *cmdList, const char *csProm Line 358  int cliExec(cliCommands_t *cmdList, const char *csProm
   
         return ret;          return ret;
 }  }
   */

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


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