--- libaitcli/src/aitcli.c 2013/10/08 12:04:42 1.9 +++ libaitcli/src/aitcli.c 2013/11/20 16:26:51 1.10 @@ -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.10 2013/11/20 16:26:51 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -1266,12 +1266,14 @@ cliInitLine(linebuffer_t * __restrict cli_buffer) * cliReadLine() - Read line from opened CLI session * * @cli_buffer = CLI buffer + * @timeout = Session timeout (-1 infinit) + * @cmd_name = If timeout reached, we should call with this cmd_name (default name is "exit") * 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, const char *cmd_name) { - int code, readLen; + int code, readLen, ret; register int i; struct pollfd fds; char buf[BUFSIZ], *str = NULL; @@ -1279,7 +1281,8 @@ cliReadLine(linebuffer_t * __restrict cli_buffer) if (!cli_buffer) { cli_SetErr(EINVAL, "Invalid input parameters ..."); return NULL; - } + } else + timeout *= 1000; /* convert from sec to ms */ memset(&fds, 0, sizeof fds); fds.fd = cli_buffer->line_in; @@ -1287,8 +1290,13 @@ 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) { + if (str) + e_free(str); + str = e_strdup(cmd_name ? cmd_name : "exit"); + } else + LOGERR; return str; } @@ -1343,10 +1351,13 @@ recheck: * @cli_buffer = CLI buffer * @csHistFile = History file name * @sock = client socket + * @timeout = Session timeout (-1 infinit) + * @cmd_name = If timeout reached, we should call with this cmd_name (default name is "exit") * 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, const char *cmd_name) { u_char buf[BUFSIZ]; int pid, stat, pty, r, s, alen, flg, attrlen = 0, ret = 0; @@ -1365,7 +1376,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, cmd_name) < 0 ? 1 : 0; cliEnd(cli_buffer); _exit(ret); @@ -1458,10 +1469,13 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const * * @cli_buffer = CLI buffer * @csHistFile = History file name + * @timeout = Session timeout (-1 infinit) + * @cmd_name = If timeout reached, we should call with this cmd_name (default name is "exit") * 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, const char *cmd_name) { char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; register int i; @@ -1475,7 +1489,7 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch return RETCODE_ERR; do { - line = cliReadLine(cli_buffer); + line = cliReadLine(cli_buffer, timeout, cmd_name); if (!line) { printfNL(cli_buffer, 0); break;