--- libaitcli/src/aitcli.c 2013/10/08 11:30:00 1.8.2.7 +++ libaitcli/src/aitcli.c 2013/11/20 16:04:42 1.9.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.8.2.7 2013/10/08 11:30:00 misho Exp $ +* $Id: aitcli.c,v 1.9.2.1 2013/11/20 16:04:42 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -506,7 +506,24 @@ bufHelp(int idx, void * __restrict cli_buffer) 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 * @@ -1041,6 +1058,8 @@ 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) + keys[i].key_func = bufEndNode; if (i >= *K_SPACE && i < *K_BACKSPACE) keys[i].key_func = bufCHAR; if (i > *K_BACKSPACE && i < 0xff) @@ -1247,12 +1266,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; @@ -1268,9 +1288,9 @@ cliReadLine(linebuffer_t * __restrict cli_buffer) printfCR(cli_buffer, 1); while (42) { - if (poll(&fds, 1, -1) < 1) { + if ((ret = poll(&fds, 1, timeout)) < 1) { LOGERR; - return str; + return ret ? str : ""; } memset(buf, 0, sizeof buf); @@ -1324,10 +1344,12 @@ 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; @@ -1346,7 +1368,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); @@ -1439,10 +1461,11 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const * * @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) +cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout) { char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; register int i; @@ -1456,7 +1479,7 @@ 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;