--- libaitcli/src/aitcli.c 2013/10/08 12:04:42 1.9 +++ libaitcli/src/aitcli.c 2020/09/01 23:19:55 1.17 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.9 2013/10/08 12:04:42 misho Exp $ +* $Id: aitcli.c,v 1.17 2020/09/01 23:19:55 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2020 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -106,7 +106,7 @@ printfEOL(linebuffer_t * __restrict buf, int len, int } write(buf->line_out, buf->line_buf, len == -1 ? - buf->line_eol - buf->line_bol: len); + buf->line_eol - buf->line_bol : len); } } @@ -154,7 +154,8 @@ bufCHAR(int idx, void * __restrict cli_buffer) memcpy(buf->line_buf + pos, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); buf->line_buf[buf->line_len - 1] = 0; - write(buf->line_out, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); + if (buf->line_prompt) + write(buf->line_out, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); if (buf->line_mode == LINEMODE_INS) { write(buf->line_out, (const u_char*) buf->line_buf + pos + buf->line_keys[idx].key_len, @@ -977,6 +978,7 @@ cliInit(int fin, int fout, const char *prompt) linebuffer_t *cli_buffer; bindkey_t *keys; register int i; + char szPrompt[STRSIZ] = {[0 ... STRSIZ - 1] = 0}; /* init buffer */ cli_buffer = e_malloc(sizeof(linebuffer_t)); @@ -993,7 +995,9 @@ cliInit(int fin, int fout, const char *prompt) SLIST_INIT(&cli_buffer->line_cmds); 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) { LOGERR; e_free(cli_buffer); @@ -1050,7 +1054,7 @@ cliInit(int fin, int fout, const char *prompt) keys[i].key_func = bufEOL; if (cli_buffer->line_prompt && (i == *K_CTRL_H || i == *K_BACKSPACE)) keys[i].key_func = bufBS; - if (cli_buffer->line_prompt && i == *K_CTRL_C) + if (i == *K_CTRL_C) keys[i].key_func = bufCLR; if (cli_buffer->line_prompt && i == *K_CTRL_A) keys[i].key_func = bufBEGIN; @@ -1058,7 +1062,7 @@ cliInit(int fin, int fout, const char *prompt) keys[i].key_func = bufEND; if (cli_buffer->line_prompt && i == *K_TAB) keys[i].key_func = bufComp; - if (cli_buffer->line_prompt && i == *K_CTRL_Z) + if (i == *K_CTRL_Z) keys[i].key_func = bufEndNode; if (i >= *K_SPACE && i < *K_BACKSPACE) keys[i].key_func = bufCHAR; @@ -1266,12 +1270,13 @@ cliInitLine(linebuffer_t * __restrict cli_buffer) * cliReadLine() - Read line from opened CLI session * * @cli_buffer = CLI buffer + * @timeout = Session timeout (-1 infinit) * return: NULL if error or !=NULL readed line, must be e_free after use! */ 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; struct pollfd fds; char buf[BUFSIZ], *str = NULL; @@ -1279,7 +1284,8 @@ cliReadLine(linebuffer_t * __restrict cli_buffer) if (!cli_buffer) { cli_SetErr(EINVAL, "Invalid input parameters ..."); return NULL; - } + } else if (timeout > 0) + timeout *= 1000; /* convert from sec to ms */ memset(&fds, 0, sizeof fds); fds.fd = cli_buffer->line_in; @@ -1287,24 +1293,25 @@ cliReadLine(linebuffer_t * __restrict cli_buffer) printfCR(cli_buffer, 1); while (42) { - if (poll(&fds, 1, -1) < 1) { - LOGERR; + if ((ret = poll(&fds, 1, timeout)) < 1) { + if (!ret) { + cli_buffer->line_kill = 1; + if (str) { + e_free(str); + str = NULL; + } + } else + LOGERR; return str; } memset(buf, 0, sizeof buf); readLen = read(cli_buffer->line_in, buf, BUFSIZ); - if (readLen == -1) { - LOGERR; - return str; + if (readLen < 1) { + if (readLen) + 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: for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--) @@ -1343,13 +1350,15 @@ recheck: * @cli_buffer = CLI buffer * @csHistFile = History file name * @sock = client socket + * @timeout = Session timeout (-1 infinit) * return: RETCODE_ERR error, RETCODE_OK ok */ 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]; - 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; struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; struct telnetAttrs *a, Attr[10]; @@ -1365,7 +1374,7 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const } else close(sock); - ret = cliLoop(cli_buffer, csHistFile) < 0 ? 1 : 0; + ret = cliLoop(cli_buffer, csHistFile, timeout) < 0 ? 1 : 0; cliEnd(cli_buffer); _exit(ret); @@ -1394,7 +1403,6 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const break; } - r = FD_ISSET(sock, &fds) ? sock : pty; s = FD_ISSET(sock, &fds) ? pty : sock; if (FD_ISSET(sock, &fds)) { @@ -1434,8 +1442,9 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const if (FD_ISSET(pty, &fds)) { memset(buf, 0, BUFSIZ); - if ((ret = read(pty, buf, BUFSIZ)) == -1) { - LOGERR; + if ((ret = read(pty, buf, BUFSIZ)) < 1) { + if (ret) + LOGERR; break; } @@ -1454,20 +1463,82 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const } /* - * cliLoop() - CLI main loop + * cliRun() - CLI run command line * * @cli_buffer = CLI buffer - * @csHistFile = History file name + * @psInput = Input command line + * @prompt = Display prompt after command * return: RETCODE_ERR error, RETCODE_OK ok */ int -cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile) +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 + * + * @cli_buffer = CLI buffer + * @csHistFile = History file name + * @timeout = Session timeout (-1 infinit) + * return: RETCODE_ERR error, RETCODE_OK ok +*/ +int +cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout) +{ + char *line; + int ret = RETCODE_OK; + /* --- main body of CLI --- */ cliInitLine(cli_buffer); @@ -1475,53 +1546,19 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch return RETCODE_ERR; do { - line = cliReadLine(cli_buffer); + line = cliReadLine(cli_buffer, timeout); if (!line) { printfNL(cli_buffer, 0); break; } else 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) { - memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS); - for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && - (*app = strsep(&s, " \t")); *app ? app++ : app); + ret = cliRun(cli_buffer, line, 42); - // 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, "\nCommand '%s' not found!\n", items[0]); - ret = -1; - } else - if (cmd->cmd_func) { - 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_resetHistory(cli_buffer); e_free(line); - } while (ret < 1); + } while (cli_buffer->line_kill || ret < 1); cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES); return ret;