Diff for /libaitcli/src/aitcli.c between versions 1.12.2.2 and 1.17.2.2

version 1.12.2.2, 2013/12/03 20:32:45 version 1.17.2.2, 2022/10/17 22:03:47
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013Copyright 2004 - 2022
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 978  cliInit(int fin, int fout, const char *prompt) Line 978  cliInit(int fin, int fout, const char *prompt)
         linebuffer_t *cli_buffer;          linebuffer_t *cli_buffer;
         bindkey_t *keys;          bindkey_t *keys;
         register int i;          register int i;
           char szPrompt[STRSIZ + 16] = {[0 ... STRSIZ + 15] = 0};
   
         /* init buffer */          /* init buffer */
         cli_buffer = e_malloc(sizeof(linebuffer_t));          cli_buffer = e_malloc(sizeof(linebuffer_t));
Line 994  cliInit(int fin, int fout, const char *prompt) Line 995  cliInit(int fin, int fout, const char *prompt)
                 SLIST_INIT(&cli_buffer->line_cmds);                  SLIST_INIT(&cli_buffer->line_cmds);
   
                 if (prompt) {                  if (prompt) {
                        cli_buffer->line_prompt = e_strdup(prompt);                        strlcpy(cli_buffer->line_porigin, prompt, sizeof cli_buffer->line_porigin);
                         snprintf(szPrompt, sizeof szPrompt, "%s{%d}> ", cli_buffer->line_porigin, cli_buffer->line_level);
                         cli_buffer->line_prompt = e_strdup(szPrompt);
                         if (!cli_buffer->line_prompt) {                          if (!cli_buffer->line_prompt) {
                                 LOGERR;                                  LOGERR;
                                 e_free(cli_buffer);                                  e_free(cli_buffer);
Line 1243  cliInit(int fin, int fout, const char *prompt) Line 1246  cliInit(int fin, int fout, const char *prompt)
 }  }
   
 /*  /*
 * cliInitLine() - Init CLI input line terminal * cliSetLine() - Set CLI input line terminal
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
 * return: none * @old = Old terminal settings
  * return: -1 error or 0 ok
 */  */
 int  int
cliInitLine(linebuffer_t * __restrict cli_buffer)cliSetLine(linebuffer_t * __restrict cli_buffer, struct termios * __restrict old)
 {  {
         struct termios t;          struct termios t;
   
         memset(&t, 0, sizeof t);          memset(&t, 0, sizeof t);
         tcgetattr(cli_buffer->line_in, &t);          tcgetattr(cli_buffer->line_in, &t);
           if (old)
                   memcpy(old, &t, sizeof(struct termios));
         t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO |           t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | 
                         ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);                          ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
         t.c_iflag |= IGNBRK;          t.c_iflag |= IGNBRK;
Line 1264  cliInitLine(linebuffer_t * __restrict cli_buffer) Line 1270  cliInitLine(linebuffer_t * __restrict cli_buffer)
 }  }
   
 /*  /*
    * cliResetLine() - Reset CLI input line terminal
    *
    * @cli_buffer = CLI buffer
    * @old = Original terminal settings
    * return: -1 error or 0 ok
   */
   int
   cliResetLine(linebuffer_t * __restrict cli_buffer, struct termios * __restrict orig)
   {
           return tcsetattr(cli_buffer->line_in, TCSANOW, orig);
   }
   
   /*
  * cliReadLine() - Read line from opened CLI session   * cliReadLine() - Read line from opened CLI session
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
Line 1304  cliReadLine(linebuffer_t * __restrict cli_buffer, int  Line 1323  cliReadLine(linebuffer_t * __restrict cli_buffer, int 
   
                 memset(buf, 0, sizeof buf);                  memset(buf, 0, sizeof buf);
                 readLen = read(cli_buffer->line_in, buf, BUFSIZ);                  readLen = read(cli_buffer->line_in, buf, BUFSIZ);
                if (readLen == -1) {                if (readLen < 1) {
                        LOGERR;                        if (readLen)
                        return str;                                LOGERR;
                         return NULL;
                 }                  }
                 if (!readLen) {  
                         if (cli_buffer->line_buf)  
                                 str = e_strdup(cli_buffer->line_buf);  
                         else  
                                 cli_SetErr(EPIPE, "Unknown state ...");  
                         return str;  
                 }  
   
 recheck:  recheck:
                 for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--)                  for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--)
Line 1361  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1374  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
                 int sock, int timeout)                  int sock, int timeout)
 {  {
         u_char buf[BUFSIZ];          u_char buf[BUFSIZ];
        int pid, stat, pty, r, s, alen, flg, attrlen = 0, ret = 0;        int pid, stat, pty, s, alen, flg, attrlen = 0, ret = 0;
         fd_set fds;          fd_set fds;
         struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };          struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };
         struct telnetAttrs *a, Attr[10];          struct telnetAttrs *a, Attr[10];
Line 1406  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1419  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
                                         break;                                          break;
                                 }                                  }
   
                                 r = FD_ISSET(sock, &fds) ? sock : pty;  
                                 s = FD_ISSET(sock, &fds) ? pty : sock;                                  s = FD_ISSET(sock, &fds) ? pty : sock;
   
                                 if (FD_ISSET(sock, &fds)) {                                  if (FD_ISSET(sock, &fds)) {
Line 1446  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1458  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
   
                                 if (FD_ISSET(pty, &fds)) {                                  if (FD_ISSET(pty, &fds)) {
                                         memset(buf, 0, BUFSIZ);                                          memset(buf, 0, BUFSIZ);
                                        if ((ret = read(pty, buf, BUFSIZ)) == -1) {                                        if ((ret = read(pty, buf, BUFSIZ)) < 1) {
                                                LOGERR;                                                if (ret)
                                                         LOGERR;
                                                 break;                                                  break;
                                         }                                          }
   
Line 1466  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1479  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
 }  }
   
 /*  /*
    * cliRun() - CLI run command line
    *
    * @cli_buffer = CLI buffer
    * @psInput = Input command line
    * @prompt = Display prompt after command
    * return: RETCODE_ERR error, RETCODE_OK ok
   */
   int
   cliRun(linebuffer_t * __restrict cli_buffer, char *psInput, int prompt)
   {
           char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
           register int i;
           int ret = RETCODE_OK;
           struct tagCommand *cmd;
   
           if (!psInput)
                   return RETCODE_ERR;
           else
                   line = psInput;
   
           // clear whitespaces
           for (s = line; isspace((int) *s); s++);
           if (*s) {
                   for (t = s + strlen(s) - 1; t > s && isspace((int) *t); t--);
                   *++t = 0;
           }
   
           if (*s) {
                   memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
                   for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && 
                                   (*app = strsep(&s, " \t")); *app ? app++ : app);
   
                   // exec_cmd ...
                   i = 0;
                   SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) {
                           if (!(cmd->cmd_level & (1 << cli_buffer->line_level)))
                                   continue;
                           if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0])))
                                   break;
                           else
                                   i++;
                   }
   
                   if (!cmd) {
                           cli_Printf(cli_buffer, "%sCommand '%s' not found!\n", 
                                           cli_buffer->line_prompt ? "\n" : "", items[0]);
                           ret = RETCODE_ERR;
                   } else
                           if (cmd->cmd_func) {
                                   if (prompt && cli_buffer->line_prompt)
                                           cli_Printf(cli_buffer, "\n");
                                   ret = cmd->cmd_func(cli_buffer, 
                                                   cli_buffer->line_level, items);
                           } else if (prompt) {
                                   clrscrEOL(cli_buffer);
                                   printfCR(cli_buffer, 1);
                           }
           }
   
           return ret;
   }
   
   /*
  * cliLoop() - CLI main loop   * cliLoop() - CLI main loop
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
Line 1476  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1552  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
 int  int
 cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout)  cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout)
 {  {
        char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];        char *line;
        register int i; 
         int ret = RETCODE_OK;          int ret = RETCODE_OK;
        struct tagCommand *cmd;        struct termios t;
   
         /* --- main body of CLI --- */          /* --- main body of CLI --- */
        cliInitLine(cli_buffer);        cliSetLine(cli_buffer, &t);
   
         if (cli_loadHistory(cli_buffer, csHistFile) == RETCODE_ERR)          if (cli_loadHistory(cli_buffer, csHistFile) == RETCODE_ERR)
                 return RETCODE_ERR;                  return RETCODE_ERR;
Line 1494  cliLoop(linebuffer_t * __restrict cli_buffer, const ch Line 1569  cliLoop(linebuffer_t * __restrict cli_buffer, const ch
                         break;                          break;
                 } else                  } else
                         cli_addHistory(cli_buffer, NULL);                          cli_addHistory(cli_buffer, NULL);
                 // clear whitespaces  
                 for (s = line; isspace((int) *s); s++);  
                 if (*s) {  
                         for (t = s + strlen(s) - 1; t > s && isspace((int) *t); t--);  
                         *++t = 0;  
                 }  
   
                if (*s) {                ret = cliRun(cli_buffer, line, 42);
                        memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS); 
                        for (app = items; app < items + MAX_PROMPT_ITEMS - 1 &&  
                                        (*app = strsep(&s, " \t")); *app ? app++ : app); 
   
                         // exec_cmd ...  
                         i = 0;  
                         SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) {  
                                 if (!(cmd->cmd_level & (1 << cli_buffer->line_level)))  
                                         continue;  
                                 if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0])))  
                                         break;  
                                 else  
                                         i++;  
                         }  
   
                         if (!cmd) {  
                                 cli_Printf(cli_buffer, "%sCommand '%s' not found!\n",   
                                                 cli_buffer->line_prompt ? "\n" : "", items[0]);  
                                 ret = -1;  
                         } else  
                                 if (cmd->cmd_func) {  
                                         if (cli_buffer->line_prompt)  
                                                 cli_Printf(cli_buffer, "\n");  
                                         ret = cmd->cmd_func(cli_buffer,   
                                                         cli_buffer->line_level, items);  
                                 } else {  
                                         clrscrEOL(cli_buffer);  
                                         printfCR(cli_buffer, 1);  
                                 }  
                 }  
   
                 cli_freeLine(cli_buffer);                  cli_freeLine(cli_buffer);
                 cli_resetHistory(cli_buffer);                  cli_resetHistory(cli_buffer);
                 e_free(line);                  e_free(line);
         } while (cli_buffer->line_kill || ret < 1);          } while (cli_buffer->line_kill || ret < 1);
   
         cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES);          cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES);
   
           /* --- restore tty --- */
           cliResetLine(cli_buffer, &t);
   
         return ret;          return ret;
 }  }

Removed from v.1.12.2.2  
changed lines
  Added in v.1.17.2.2


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