--- libaitcli/src/aitcli.c 2020/09/01 23:19:55 1.17 +++ 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.17 2020/09/01 23:19:55 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 - 2020 +Copyright 2004 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -978,7 +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}; + char szPrompt[STRSIZ + 16] = {[0 ... STRSIZ + 15] = 0}; /* init buffer */ cli_buffer = e_malloc(sizeof(linebuffer_t)); @@ -1246,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; @@ -1267,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 @@ -1538,9 +1554,10 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch { char *line; int ret = RETCODE_OK; + 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; @@ -1561,5 +1578,9 @@ cliLoop(linebuffer_t * __restrict cli_buffer, const ch } while (cli_buffer->line_kill || ret < 1); cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES); + + /* --- restore tty --- */ + cliResetLine(cli_buffer, &t); + return ret; }