ESH

Executive Shell
git clone http://git.omkov.net/ESH
Log | Tree | Refs | README | Download

AuthorJamozed <[email protected]>
Date2021-04-01 07:33:43
Commitba8ec312979ec7102c224dff6130d4ec2ad78218
Parentd0caacdd3e6a39d32c007d41fb9afc2f23275d24

Ensure terminal state is restored on exit

Diffstat

M src/lineread.c | 10 ++++++----

1 files changed, 6 insertions, 4 deletions

diff --git a/src/lineread.c b/src/lineread.c
index cf2f415..ce20d9b 100644
--- a/src/lineread.c
+++ b/src/lineread.c
@@ -50,7 +50,7 @@ struct line {
 };
 
 static struct termios tco, tcn;
-static bool atexitset = false;
+static bool rawflag = false, ateflag = false;
 
 static char *lineedit(void);
 static void lineESC(struct line *l, register int c);
@@ -167,8 +167,10 @@ static void lineESC(struct line *l, register int c) {
 
 /* Put stdin into raw mode */
 static inline void tcraw(void) {
+	if (rawflag) { return; }
+	
 	tcgetattr(STDIN_FILENO, &tco); tcn = tco;
-	if (!atexitset) { atexit(tcrestore); atexitset = true; }
+	if (!ateflag) { atexit(tcrestore); ateflag = true; }
 
 	/* No break, no CR to NL, no parity check, no strip bit, no flow control */
 	tcn.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
@@ -179,12 +181,12 @@ static inline void tcraw(void) {
 	/* Read every byte with no delay */
 	tcn.c_cc[VMIN] = 1; tcn.c_cc[VTIME] = 0;
 
-	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcn); return;
+	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcn); rawflag = true; return;
 }
 
 /* Restore stdin to canonical mode */
 static inline void tcrestore(void) {
-	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tco); return;
+	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tco); rawflag = false; return;
 }
 
 /* Get the number of columns in the terminal */