--- libaitcli/src/aitcli.c 2016/08/18 09:12:00 1.14.2.1 +++ libaitcli/src/aitcli.c 2022/10/17 22:04:24 1.18 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.14.2.1 2016/08/18 09:12:00 misho Exp $ +* $Id: aitcli.c,v 1.18 2022/10/17 22:04:24 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 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -978,6 +978,7 @@ cliInit(int fin, int fout, const char *prompt) linebuffer_t *cli_buffer; bindkey_t *keys; register int i; + char szPrompt[STRSIZ + 16] = {[0 ... STRSIZ + 15] = 0}; /* init buffer */ cli_buffer = e_malloc(sizeof(linebuffer_t)); @@ -994,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); @@ -1243,18 +1246,21 @@ cliInit(int fin, int fout, const char *prompt) } /* - * cliInitLine() - Init CLI input line terminal + * cliSetLine() - Set CLI input line terminal * * @cli_buffer = CLI buffer - * return: none + * @old = Old terminal settings + * return: -1 error or 0 ok */ int -cliInitLine(linebuffer_t * __restrict cli_buffer) +cliSetLine(linebuffer_t * __restrict cli_buffer, struct termios * __restrict old) { struct termios t; memset(&t, 0, sizeof t); tcgetattr(cli_buffer->line_in, &t); + if (old) + memcpy(old, &t, sizeof(struct termios)); t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); t.c_iflag |= IGNBRK; @@ -1264,6 +1270,19 @@ cliInitLine(linebuffer_t * __restrict cli_buffer) } /* + * cliResetLine() - Reset CLI input line terminal + * + * @cli_buffer = CLI buffer + * @old = Original terminal settings + * return: -1 error or 0 ok +*/ +int +cliResetLine(linebuffer_t * __restrict cli_buffer, struct termios * __restrict orig) +{ + return tcsetattr(cli_buffer->line_in, TCSANOW, orig); +} + +/* * cliReadLine() - Read line from opened CLI session * * @cli_buffer = CLI buffer @@ -1400,7 +1419,6 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const break; } - FD_ISSET(sock, &fds) ? sock : pty; s = FD_ISSET(sock, &fds) ? pty : sock; if (FD_ISSET(sock, &fds)) { @@ -1461,6 +1479,69 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const } /* + * cliRun() - CLI run command line + * + * @cli_buffer = CLI buffer + * @psInput = Input command line + * @prompt = Display prompt after command + * return: RETCODE_ERR error, RETCODE_OK ok +*/ +int +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 @@ -1471,13 +1552,12 @@ cliNetLoop(linebuffer_t * __restrict cli_buffer, const int cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout) { - char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; - register int i; + char *line; int ret = RETCODE_OK; - struct tagCommand *cmd; + struct termios t; /* --- main body of CLI --- */ - cliInitLine(cli_buffer); + cliSetLine(cli_buffer, &t); if (cli_loadHistory(cli_buffer, csHistFile) == RETCODE_ERR) return RETCODE_ERR; @@ -1489,50 +1569,18 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch 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, "%sCommand '%s' not found!\n", - cli_buffer->line_prompt ? "\n" : "", items[0]); - ret = -1; - } else - if (cmd->cmd_func) { - if (cli_buffer->line_prompt) - 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 (cli_buffer->line_kill || ret < 1); cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES); + + /* --- restore tty --- */ + cliResetLine(cli_buffer, &t); + return ret; }