mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-23 06:29:10 +00:00
system/readline: restore terminal attributes safely
Track successful mode changes so failed setup cannot corrupt terminal state. Signed-off-by: raiden00pl <raiden00@railab.me> Assisted-by: Claude Code
This commit is contained in:
parent
247a8c03cb
commit
96f6e7d460
1 changed files with 11 additions and 7 deletions
|
|
@ -30,6 +30,7 @@
|
|||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include "system/readline.h"
|
||||
|
|
@ -223,16 +224,19 @@ ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd)
|
|||
|
||||
struct readline_s vtbl;
|
||||
struct termios cfg;
|
||||
struct termios newcfg;
|
||||
bool restore_termios = false;
|
||||
ssize_t ret;
|
||||
|
||||
if (isatty(infd))
|
||||
if (isatty(infd) && tcgetattr(infd, &cfg) == 0 &&
|
||||
(cfg.c_lflag & ICANON) != 0)
|
||||
{
|
||||
tcgetattr(infd, &cfg);
|
||||
if (cfg.c_lflag & ICANON)
|
||||
newcfg = cfg;
|
||||
newcfg.c_lflag &= ~ICANON;
|
||||
|
||||
if (tcsetattr(infd, TCSANOW, &newcfg) == 0)
|
||||
{
|
||||
cfg.c_lflag &= ~ICANON;
|
||||
tcsetattr(infd, TCSANOW, &cfg);
|
||||
cfg.c_lflag |= ICANON;
|
||||
restore_termios = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -251,7 +255,7 @@ ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd)
|
|||
|
||||
ret = readline_common(&vtbl.vtbl, buf, buflen);
|
||||
|
||||
if (isatty(infd) && (cfg.c_lflag & ICANON))
|
||||
if (restore_termios)
|
||||
{
|
||||
tcsetattr(infd, TCSANOW, &cfg);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue