Diff for /libaitcli/src/aitcli.c between versions 1.8.2.6 and 1.11.2.2

version 1.8.2.6, 2013/10/08 09:41:20 version 1.11.2.2, 2013/11/22 14:46:01
Line 426  bufComp(int idx, void * __restrict cli_buffer) Line 426  bufComp(int idx, void * __restrict cli_buffer)
   
                 if (i) {                  if (i) {
                         SLIST_FOREACH(cmd, &buf->line_cmds, cmd_next) {                          SLIST_FOREACH(cmd, &buf->line_cmds, cmd_next) {
                                if (cmd->cmd_level == buf->line_level &&                                 if (cmd->cmd_level & (1 << buf->line_level) && 
                                                 !strncmp(cmd->cmd_name, items[0],                                                   !strncmp(cmd->cmd_name, items[0], 
                                                         strlen(items[0]))) {                                                          strlen(items[0]))) {
                                         if (strncmp(cmd->cmd_name, CLI_CMD_SEP,                                           if (strncmp(cmd->cmd_name, CLI_CMD_SEP, 
Line 452  bufComp(int idx, void * __restrict cli_buffer) Line 452  bufComp(int idx, void * __restrict cli_buffer)
         } else {          } else {
                 /* we on 0 position of prompt, show commands for this level */                  /* we on 0 position of prompt, show commands for this level */
                 SLIST_FOREACH(cmd, &buf->line_cmds, cmd_next) {                  SLIST_FOREACH(cmd, &buf->line_cmds, cmd_next) {
                        if (cmd->cmd_level == buf->line_level)                        if (cmd->cmd_level & (1 << buf->line_level))
                                 if (strncmp(cmd->cmd_name, CLI_CMD_SEP, strlen(CLI_CMD_SEP))) {                                  if (strncmp(cmd->cmd_name, CLI_CMD_SEP, strlen(CLI_CMD_SEP))) {
                                         j++;                                          j++;
                                         c = cmd;                                          c = cmd;
Line 506  bufHelp(int idx, void * __restrict cli_buffer) Line 506  bufHelp(int idx, void * __restrict cli_buffer)
         return RETCODE_OK;          return RETCODE_OK;
 }  }
   
   static int
   bufEndNode(int idx, void * __restrict cli_buffer)
   {
           linebuffer_t *buf = cli_buffer;
   
           if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
                   return RETCODE_ERR;
   
           if (buf->line_level > 0) {
                   printfNL(cli_buffer, 0);
                   buf->line_level--;
                   cli_Printf(buf, "Enter to config level %d\n", buf->line_level);
           }
   
           return bufCLR(idx, cli_buffer);
   }
   
   
 /*  /*
  * cli_Printf() - Send message to CLI session   * cli_Printf() - Send message to CLI session
  *   *
Line 586  cli_BindKey(bindkey_t * __restrict key, linebuffer_t * Line 603  cli_BindKey(bindkey_t * __restrict key, linebuffer_t *
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
  * @csCmd = Command name   * @csCmd = Command name
 * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ... * @cliLevel = Level in CLI, -1 view from all levels, 0 hidden, >0 mask levels
  * @funcCmd = Callback function when user call command   * @funcCmd = Callback function when user call command
  * @csInfo = Inline information for command   * @csInfo = Inline information for command
  * @csHelp = Help line when call help   * @csHelp = Help line when call help
Line 627  cli_addCommand(linebuffer_t * __restrict cli_buffer, c Line 644  cli_addCommand(linebuffer_t * __restrict cli_buffer, c
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
  * @csCmd = Command name   * @csCmd = Command name
 * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ... * @cliLevel = Level in CLI, -1 view from all levels, 0 hidden, >0 mask levels
  * return: RETCODE_ERR error, RETCODE_OK ok   * return: RETCODE_ERR error, RETCODE_OK ok
 */  */
 int  int
Line 657  cli_delCommand(linebuffer_t * __restrict cli_buffer, c Line 674  cli_delCommand(linebuffer_t * __restrict cli_buffer, c
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
  * @csCmd = Command name   * @csCmd = Command name
 * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ... * @cliLevel = Level in CLI, -1 view from all levels, 0 hidden, >0 mask levels
  * @funcCmd = Callback function when user call command   * @funcCmd = Callback function when user call command
  * @csInfo = Inline information for command   * @csInfo = Inline information for command
  * @csHelp = Help line when call help   * @csHelp = Help line when call help
Line 676  cli_updCommand(linebuffer_t * __restrict cli_buffer, c Line 693  cli_updCommand(linebuffer_t * __restrict cli_buffer, c
                 return RETCODE_ERR;                  return RETCODE_ERR;
         }          }
   
        SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next)         SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next)
                if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) {                if ((!cmd->cmd_level || cmd->cmd_level == cliLevel) && 
                        ret = 1;                                !strcmp(cmd->cmd_name, csCmd)) {
                        if (!cmd->cmd_level)
                                 cmd->cmd_level = cliLevel;
                         if (funcCmd)                          if (funcCmd)
                                 cmd->cmd_func = funcCmd;                                  cmd->cmd_func = funcCmd;
                         if (csInfo)                          if (csInfo)
Line 1009  cliInit(int fin, int fout, const char *prompt) Line 1027  cliInit(int fin, int fout, const char *prompt)
                 memset(keys, 0, sizeof(bindkey_t) * (MAX_BINDKEY + 1));                  memset(keys, 0, sizeof(bindkey_t) * (MAX_BINDKEY + 1));
   
         /* add helper functions */          /* add helper functions */
        cli_addCommand(cli_buffer, "exit", 0, cli_Cmd_Exit, "exit <cr>", "Exit from console");        cli_addCommand(cli_buffer, "exit", 1, cli_Cmd_Exit, "exit <cr>", "Exit from console");
        cli_addCommand(cli_buffer, "help", 0, cli_Cmd_Help, "help [command] <cr>", "Help screen");        cli_addCommand(cli_buffer, "help", -1, cli_Cmd_Help, "help [command] <cr>", "Help screen");
        cli_addCommand(cli_buffer, "-------", 0, NULL, "-------------------------", NULL);        cli_addCommand(cli_buffer, "-------", -1, NULL, "-------------------------", NULL);
         cli_addCommand(cli_buffer, "where", -1, cli_Cmd_WhereAmI, "where <cr>", 
                         "Query current level of console");
         cli_addCommand(cli_buffer, "top", -1, cli_Cmd_Top, "top <cr>", "Top level of console");
         cli_addCommand(cli_buffer, "end", -1, cli_Cmd_End, "end <cr>", "End level of console");
         cli_addCommand(cli_buffer, "config", -1, cli_Cmd_Config, "config <cr>", 
                         "Config next level of console");
         cli_addCommand(cli_buffer, "-------", -1, NULL, "-------------------------", NULL);
   
         /* fill key bindings */          /* fill key bindings */
         /* ascii chars & ctrl+chars */          /* ascii chars & ctrl+chars */
Line 1033  cliInit(int fin, int fout, const char *prompt) Line 1058  cliInit(int fin, int fout, const char *prompt)
                         keys[i].key_func = bufEND;                          keys[i].key_func = bufEND;
                 if (cli_buffer->line_prompt && i == *K_TAB)                  if (cli_buffer->line_prompt && i == *K_TAB)
                         keys[i].key_func = bufComp;                          keys[i].key_func = bufComp;
                   if (cli_buffer->line_prompt && i == *K_CTRL_Z)
                           keys[i].key_func = bufEndNode;
                 if (i >= *K_SPACE && i < *K_BACKSPACE)                  if (i >= *K_SPACE && i < *K_BACKSPACE)
                         keys[i].key_func = bufCHAR;                          keys[i].key_func = bufCHAR;
                 if (i > *K_BACKSPACE && i < 0xff)                  if (i > *K_BACKSPACE && i < 0xff)
Line 1239  cliInitLine(linebuffer_t * __restrict cli_buffer) Line 1266  cliInitLine(linebuffer_t * __restrict cli_buffer)
  * cliReadLine() - Read line from opened CLI session   * cliReadLine() - Read line from opened CLI session
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
    * @timeout = Session timeout (-1 infinit)
  * return: NULL if error or !=NULL readed line, must be e_free after use!   * return: NULL if error or !=NULL readed line, must be e_free after use!
 */  */
 char *  char *
cliReadLine(linebuffer_t * __restrict cli_buffer)cliReadLine(linebuffer_t * __restrict cli_buffer, int timeout)
 {  {
        int code, readLen;        int code, readLen, ret;
         register int i;          register int i;
         struct pollfd fds;          struct pollfd fds;
         char buf[BUFSIZ], *str = NULL;          char buf[BUFSIZ], *str = NULL;
Line 1252  cliReadLine(linebuffer_t * __restrict cli_buffer) Line 1280  cliReadLine(linebuffer_t * __restrict cli_buffer)
         if (!cli_buffer) {          if (!cli_buffer) {
                 cli_SetErr(EINVAL, "Invalid input parameters ...");                  cli_SetErr(EINVAL, "Invalid input parameters ...");
                 return NULL;                  return NULL;
        }        } else if (timeout > 0)
                 timeout *= 1000;        /* convert from sec to ms */
   
         memset(&fds, 0, sizeof fds);          memset(&fds, 0, sizeof fds);
         fds.fd = cli_buffer->line_in;          fds.fd = cli_buffer->line_in;
Line 1260  cliReadLine(linebuffer_t * __restrict cli_buffer) Line 1289  cliReadLine(linebuffer_t * __restrict cli_buffer)
   
         printfCR(cli_buffer, 1);          printfCR(cli_buffer, 1);
         while (42) {          while (42) {
                if (poll(&fds, 1, -1) < 1) {                if ((ret = poll(&fds, 1, timeout)) < 1) {
                        LOGERR;                        if (!ret) {
                                 cli_buffer->line_kill = 1;
                                 if (str) {
                                         e_free(str);
                                         str = NULL;
                                 }
                         } else
                                 LOGERR;
                         return str;                          return str;
                 }                  }
   
Line 1316  recheck: Line 1352  recheck:
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
  * @csHistFile = History file name   * @csHistFile = History file name
  * @sock = client socket   * @sock = client socket
    * @timeout = Session timeout (-1 infinit)
  * return: RETCODE_ERR error, RETCODE_OK ok   * return: RETCODE_ERR error, RETCODE_OK ok
 */  */
 int  int
cliNetLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int sock)cliNetLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, 
                 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, r, s, alen, flg, attrlen = 0, ret = 0;
Line 1338  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1376  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
                         } else                          } else
                                 close(sock);                                  close(sock);
   
                        ret = cliLoop(cli_buffer, csHistFile) < 0 ? 1 : 0;                        ret = cliLoop(cli_buffer, csHistFile, timeout) < 0 ? 1 : 0;
                         cliEnd(cli_buffer);                          cliEnd(cli_buffer);
   
                         _exit(ret);                          _exit(ret);
Line 1431  cliNetLoop(linebuffer_t * __restrict cli_buffer, const Line 1469  cliNetLoop(linebuffer_t * __restrict cli_buffer, const
  *   *
  * @cli_buffer = CLI buffer   * @cli_buffer = CLI buffer
  * @csHistFile = History file name   * @csHistFile = History file name
    * @timeout = Session timeout (-1 infinit)
  * return: RETCODE_ERR error, RETCODE_OK ok   * return: RETCODE_ERR error, RETCODE_OK ok
 */  */
 int  int
cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile)cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout)
 {  {
         char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];          char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
         register int i;          register int i;
Line 1448  cliLoop(linebuffer_t * __restrict cli_buffer, const ch Line 1487  cliLoop(linebuffer_t * __restrict cli_buffer, const ch
                 return RETCODE_ERR;                  return RETCODE_ERR;
   
         do {          do {
                line = cliReadLine(cli_buffer);                line = cliReadLine(cli_buffer, timeout);
                 if (!line) {                  if (!line) {
                         printfNL(cli_buffer, 0);                          printfNL(cli_buffer, 0);
                         break;                          break;
Line 1469  cliLoop(linebuffer_t * __restrict cli_buffer, const ch Line 1508  cliLoop(linebuffer_t * __restrict cli_buffer, const ch
                         // exec_cmd ...                          // exec_cmd ...
                         i = 0;                          i = 0;
                         SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) {                          SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) {
                                if (cmd->cmd_level != cli_buffer->line_level)                                if (!(cmd->cmd_level & (1 << cli_buffer->line_level)))
                                         continue;                                          continue;
                                 if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0])))                                  if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0])))
                                         break;                                          break;
Line 1494  cliLoop(linebuffer_t * __restrict cli_buffer, const ch Line 1533  cliLoop(linebuffer_t * __restrict cli_buffer, const ch
                 cli_freeLine(cli_buffer);                  cli_freeLine(cli_buffer);
                 cli_resetHistory(cli_buffer);                  cli_resetHistory(cli_buffer);
                 e_free(line);                  e_free(line);
        } while (ret < 1);        } while (cli_buffer->line_kill || ret < 1);
   
         cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES);          cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES);
         return ret;          return ret;

Removed from v.1.8.2.6  
changed lines
  Added in v.1.11.2.2


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