--- libaitcli/src/aitcli.c 2013/11/20 16:40:02 1.11 +++ libaitcli/src/aitcli.c 2016/08/18 09:27:37 1.15 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.11 2013/11/20 16:40:02 misho Exp $ +* $Id: aitcli.c,v 1.15 2016/08/18 09:27:37 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 - 2016 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, @@ -1050,7 +1051,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 +1059,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; @@ -1267,11 +1268,10 @@ cliInitLine(linebuffer_t * __restrict cli_buffer) * * @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, int timeout, const char *cmd_name) +cliReadLine(linebuffer_t * __restrict cli_buffer, int timeout) { int code, readLen, ret; register int i; @@ -1292,9 +1292,11 @@ cliReadLine(linebuffer_t * __restrict cli_buffer, int while (42) { if ((ret = poll(&fds, 1, timeout)) < 1) { if (!ret) { - if (str) + cli_buffer->line_kill = 1; + if (str) { e_free(str); - str = e_strdup(cmd_name ? cmd_name : "exit"); + str = NULL; + } } else LOGERR; return str; @@ -1302,17 +1304,11 @@ cliReadLine(linebuffer_t * __restrict cli_buffer, int 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--) @@ -1352,15 +1348,14 @@ recheck: * @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, int timeout, const char *cmd_name) + 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]; @@ -1376,7 +1371,7 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const } else close(sock); - ret = cliLoop(cli_buffer, csHistFile, timeout, cmd_name) < 0 ? 1 : 0; + ret = cliLoop(cli_buffer, csHistFile, timeout) < 0 ? 1 : 0; cliEnd(cli_buffer); _exit(ret); @@ -1405,7 +1400,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)) { @@ -1445,8 +1439,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; } @@ -1470,12 +1465,10 @@ 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, - int timeout, const char *cmd_name) +cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout) { char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; register int i; @@ -1489,7 +1482,7 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch return RETCODE_ERR; do { - line = cliReadLine(cli_buffer, timeout, cmd_name); + line = cliReadLine(cli_buffer, timeout); if (!line) { printfNL(cli_buffer, 0); break; @@ -1519,11 +1512,13 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch } if (!cmd) { - cli_Printf(cli_buffer, "\nCommand '%s' not found!\n", items[0]); + cli_Printf(cli_buffer, "%sCommand '%s' not found!\n", + cli_buffer->line_prompt ? "\n" : "", items[0]); ret = -1; } else if (cmd->cmd_func) { - cli_Printf(cli_buffer, "\n"); + if (cli_buffer->line_prompt) + cli_Printf(cli_buffer, "\n"); ret = cmd->cmd_func(cli_buffer, cli_buffer->line_level, items); } else { @@ -1535,7 +1530,7 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch 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;