diff options
author | Denys Vlasenko | 2017-01-11 16:17:59 +0100 |
---|---|---|
committer | Denys Vlasenko | 2017-01-11 16:17:59 +0100 |
commit | 01ccdd1d3c5221789f1ac62ced12b7984d910705 (patch) | |
tree | a6eb44f24c1324ddf18bfcec57fd4c3735aa1245 | |
parent | 8944c67b1f61ca6a51a485772d5d1e6a8ff3d83d (diff) | |
download | busybox-01ccdd1d3c5221789f1ac62ced12b7984d910705.zip busybox-01ccdd1d3c5221789f1ac62ced12b7984d910705.tar.gz |
libbb: consolidate the code to set termios unbuffered mode
function old new delta
set_termios_to_raw - 116 +116
count_lines 72 74 +2
powertop_main 1458 1430 -28
top_main 943 914 -29
more_main 759 714 -45
fsck_minix_main 2969 2921 -48
conspy_main 1197 1135 -62
rawmode 99 36 -63
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/6 up/down: 118/-275) Total: -157 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/vi.c | 15 | ||||
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/lineedit.c | 2 | ||||
-rw-r--r-- | libbb/xfuncs.c | 37 | ||||
-rw-r--r-- | loginutils/getty.c | 2 | ||||
-rw-r--r-- | loginutils/vlock.c | 8 | ||||
-rw-r--r-- | miscutils/chat.c | 1 | ||||
-rw-r--r-- | miscutils/conspy.c | 17 | ||||
-rw-r--r-- | miscutils/microcom.c | 1 | ||||
-rw-r--r-- | miscutils/rx.c | 1 | ||||
-rw-r--r-- | procps/powertop.c | 10 | ||||
-rw-r--r-- | procps/top.c | 10 | ||||
-rw-r--r-- | shell/shell_common.c | 2 | ||||
-rw-r--r-- | util-linux/fsck_minix.c | 6 | ||||
-rw-r--r-- | util-linux/more.c | 10 |
15 files changed, 71 insertions, 55 deletions
diff --git a/editors/vi.c b/editors/vi.c index b56b04b..1e5ef44 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -354,7 +354,7 @@ struct globals { #if ENABLE_FEATURE_VI_USE_SIGNALS sigjmp_buf restart; // catch_sig() #endif - struct termios term_orig, term_vi; // remember what the cooked mode was + struct termios term_orig; // remember what the cooked mode was #if ENABLE_FEATURE_VI_COLON char *initial_cmds[3]; // currently 2 entries, NULL terminated #endif @@ -462,7 +462,6 @@ struct globals { #define context_end (G.context_end ) #define restart (G.restart ) #define term_orig (G.term_orig ) -#define term_vi (G.term_vi ) #define initial_cmds (G.initial_cmds ) #define readbuffer (G.readbuffer ) #define scr_out_buf (G.scr_out_buf ) @@ -2731,15 +2730,9 @@ static char *swap_context(char *p) // goto new context for '' command make this //----- Set terminal attributes -------------------------------- static void rawmode(void) { - tcgetattr(0, &term_orig); - term_vi = term_orig; - term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's - term_vi.c_iflag &= (~IXON & ~ICRNL); - term_vi.c_oflag &= (~ONLCR); - term_vi.c_cc[VMIN] = 1; - term_vi.c_cc[VTIME] = 0; - erase_char = term_vi.c_cc[VERASE]; - tcsetattr_stdin_TCSANOW(&term_vi); + // no TERMIOS_CLEAR_ISIG: leave ISIG on - allow signals + set_termios_to_raw(STDIN_FILENO, &term_orig, TERMIOS_RAW_CRNL); + erase_char = term_orig.c_cc[VERASE]; } static void cookmode(void) diff --git a/include/libbb.h b/include/libbb.h index abdc8c2..87f89c7 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1438,6 +1438,10 @@ int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FU int get_terminal_width(int fd) FAST_FUNC; int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC; +#define TERMIOS_CLEAR_ISIG (1 << 0) +#define TERMIOS_RAW_CRNL (1 << 1) +#define TERMIOS_RAW_INPUT (1 << 2) +int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC; /* NB: "unsigned request" is crucial! "int request" will break some arches! */ int ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC; diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 31e3921..2a5d4e7 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -2325,7 +2325,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman /* ~ECHO, ~ECHONL: turn off echoing, including newline echoing */ /* ~ISIG: turn off INTR (ctrl-C), QUIT, SUSP */ new_settings.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG); - /* reads would block only if < 1 char is available */ + /* reads will block only if < 1 char is available */ new_settings.c_cc[VMIN] = 1; /* no timeout (reads block forever) */ new_settings.c_cc[VTIME] = 0; diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 45650ed..98d3531 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -311,6 +311,43 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) return tcsetattr(STDIN_FILENO, TCSANOW, tp); } +int FAST_FUNC set_termios_to_raw(int fd, struct termios *oldterm, int flags) +{ +//TODO: lineedit, microcom and less might be adapted to use this too: +// grep for "tcsetattr" + + struct termios newterm; + + tcgetattr(fd, oldterm); + newterm = *oldterm; + + /* Turn off buffered input (ICANON) + * Turn off echoing (ECHO) + * and separate echoing of newline (ECHONL, normally off anyway) + */ + newterm.c_lflag &= ~(ICANON | ECHO | ECHONL); + if (flags & TERMIOS_CLEAR_ISIG) { + /* dont recognize INT/QUIT/SUSP chars */ + newterm.c_lflag &= ~ISIG; + } + /* reads will block only if < 1 char is available */ + newterm.c_cc[VMIN] = 1; + /* no timeout (reads block forever) */ + newterm.c_cc[VTIME] = 0; + if (flags & TERMIOS_RAW_CRNL) { + /* dont convert CR to NL on input */ + newterm.c_iflag &= ~(IXON | ICRNL); + /* dont convert NL to CR on output */ + newterm.c_oflag &= ~(ONLCR); + } + if (flags & TERMIOS_RAW_INPUT) { + /* dont convert anything on input */ + newterm.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL); + } + + return tcsetattr(fd, TCSANOW, &newterm); +} + pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options) { pid_t r; diff --git a/loginutils/getty.c b/loginutils/getty.c index 162c169..ba6c784 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c @@ -316,7 +316,7 @@ static void init_tty_attrs(int speed) /* non-raw output; add CR to each NL */ G.tty_attrs.c_oflag = OPOST | ONLCR; - /* reads would block only if < 1 char is available */ + /* reads will block only if < 1 char is available */ G.tty_attrs.c_cc[VMIN] = 1; /* no timeout (reads block forever) */ G.tty_attrs.c_cc[VTIME] = 0; diff --git a/loginutils/vlock.c b/loginutils/vlock.c index 52ae607..5ba6a87 100644 --- a/loginutils/vlock.c +++ b/loginutils/vlock.c @@ -105,12 +105,12 @@ int vlock_main(int argc UNUSED_PARAM, char **argv) ioctl(STDIN_FILENO, VT_SETMODE, &vtm); #endif +//TODO: use set_termios_to_raw() tcgetattr(STDIN_FILENO, &oterm); term = oterm; - term.c_iflag &= ~BRKINT; - term.c_iflag |= IGNBRK; - term.c_lflag &= ~ISIG; - term.c_lflag &= ~(ECHO | ECHOCTL); + term.c_iflag |= IGNBRK; /* ignore serial break (why? VTs don't have breaks, right?) */ + term.c_iflag &= ~BRKINT; /* redundant? "dont translate break to SIGINT" */ + term.c_lflag &= ~(ISIG | ECHO | ECHOCTL); /* ignore ^C ^Z, echo off */ tcsetattr_stdin_TCSANOW(&term); while (1) { diff --git a/miscutils/chat.c b/miscutils/chat.c index dc85f82..8df1945 100644 --- a/miscutils/chat.c +++ b/miscutils/chat.c @@ -213,6 +213,7 @@ int chat_main(int argc UNUSED_PARAM, char **argv) , signal_handler); #if ENABLE_FEATURE_CHAT_TTY_HIFI +//TODO: use set_termios_to_raw() tcgetattr(STDIN_FILENO, &tio); tio0 = tio; cfmakeraw(&tio); diff --git a/miscutils/conspy.c b/miscutils/conspy.c index d9d09d4..1f0278b 100644 --- a/miscutils/conspy.c +++ b/miscutils/conspy.c @@ -363,7 +363,6 @@ int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int conspy_main(int argc UNUSED_PARAM, char **argv) { char tty_name[sizeof(DEV_TTY "NN")]; - struct termios termbuf; unsigned opts; unsigned ttynum; int poll_timeout_ms; @@ -414,16 +413,14 @@ int conspy_main(int argc UNUSED_PARAM, char **argv) bb_signals(BB_FATAL_SIGS, cleanup); - // All characters must be passed through to us unaltered G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY); - tcgetattr(G.kbd_fd, &G.term_orig); - termbuf = G.term_orig; - termbuf.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL); - //termbuf.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n - termbuf.c_lflag &= ~(ISIG|ICANON|ECHO); - termbuf.c_cc[VMIN] = 1; - termbuf.c_cc[VTIME] = 0; - tcsetattr(G.kbd_fd, TCSANOW, &termbuf); + + // All characters must be passed through to us unaltered + set_termios_to_raw(G.kbd_fd, &G.term_orig, 0 + | TERMIOS_CLEAR_ISIG // no signals on ^C ^Z etc + | TERMIOS_RAW_INPUT // turn off all input conversions + ); + //Note: termios.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n poll_timeout_ms = 250; while (1) { diff --git a/miscutils/microcom.c b/miscutils/microcom.c index 04605d8..5a4bbef 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c @@ -33,6 +33,7 @@ // set raw tty mode static void xget1(int fd, struct termios *t, struct termios *oldt) { +//TODO: use set_termios_to_raw() tcgetattr(fd, oldt); *t = *oldt; cfmakeraw(t); diff --git a/miscutils/rx.c b/miscutils/rx.c index 660f66a..36fc20a 100644 --- a/miscutils/rx.c +++ b/miscutils/rx.c @@ -263,6 +263,7 @@ int rx_main(int argc UNUSED_PARAM, char **argv) termios_err = tcgetattr(read_fd, &tty); if (termios_err == 0) { +//TODO: use set_termios_to_raw() orig_tty = tty; cfmakeraw(&tty); tcsetattr(read_fd, TCSAFLUSH, &tty); diff --git a/procps/powertop.c b/procps/powertop.c index ce85f41..ee80616 100644 --- a/procps/powertop.c +++ b/procps/powertop.c @@ -683,7 +683,6 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv) ullong cur_duration[MAX_CSTATE_COUNT]; char cstate_lines[MAX_CSTATE_COUNT + 2][64]; #if ENABLE_FEATURE_USE_TERMIOS - struct termios new_settings; struct pollfd pfd[1]; pfd[0].fd = 0; @@ -707,14 +706,11 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv) puts("Collecting data for "DEFAULT_SLEEP_STR" seconds"); #if ENABLE_FEATURE_USE_TERMIOS - tcgetattr(0, (void *)&G.init_settings); - memcpy(&new_settings, &G.init_settings, sizeof(new_settings)); - /* Turn on unbuffered input, turn off echoing */ - new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); + /* Turn on unbuffered input; turn off echoing, ^C ^Z etc */ + set_termios_to_raw(STDIN_FILENO, &G.init_settings, TERMIOS_CLEAR_ISIG); + bb_signals(BB_FATAL_SIGS, sig_handler); /* So we don't forget to reset term settings */ atexit(reset_term); - bb_signals(BB_FATAL_SIGS, sig_handler); - tcsetattr_stdin_TCSANOW(&new_settings); #endif /* Collect initial data */ diff --git a/procps/top.c b/procps/top.c index 491acb5..91bb8a8 100644 --- a/procps/top.c +++ b/procps/top.c @@ -1089,9 +1089,6 @@ int top_main(int argc UNUSED_PARAM, char **argv) unsigned interval; char *str_interval, *str_iterations; unsigned scan_mask = TOP_MASK; -#if ENABLE_FEATURE_USE_TERMIOS - struct termios new_settings; -#endif INIT_G(); @@ -1141,11 +1138,8 @@ int top_main(int argc UNUSED_PARAM, char **argv) } #if ENABLE_FEATURE_USE_TERMIOS else { - tcgetattr(0, (void *) &initial_settings); - memcpy(&new_settings, &initial_settings, sizeof(new_settings)); - /* unbuffered input, turn off echo */ - new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); - tcsetattr_stdin_TCSANOW(&new_settings); + /* Turn on unbuffered input; turn off echoing, ^C ^Z etc */ + set_termios_to_raw(STDIN_FILENO, &initial_settings, TERMIOS_CLEAR_ISIG); } bb_signals(BB_FATAL_SIGS, sig_catcher); diff --git a/shell/shell_common.c b/shell/shell_common.c index 98d8627..549b17c 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -143,7 +143,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), // Setting it to more than 1 breaks poll(): // it blocks even if there's data. !?? //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255; - /* reads would block only if < 1 char is available */ + /* reads will block only if < 1 char is available */ tty.c_cc[VMIN] = 1; /* no timeout (reads block forever) */ tty.c_cc[VTIME] = 0; diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c index 0eaac17..2ab7530 100644 --- a/util-linux/fsck_minix.c +++ b/util-linux/fsck_minix.c @@ -1226,7 +1226,6 @@ void check2(void); int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int fsck_minix_main(int argc UNUSED_PARAM, char **argv) { - struct termios tmp; int retcode = 0; xfunc_error_retval = 8; @@ -1271,10 +1270,7 @@ int fsck_minix_main(int argc UNUSED_PARAM, char **argv) read_tables(); if (OPT_manual) { - tcgetattr(0, &sv_termios); - tmp = sv_termios; - tmp.c_lflag &= ~(ICANON | ECHO); - tcsetattr_stdin_TCSANOW(&tmp); + set_termios_to_raw(STDIN_FILENO, &sv_termios, 0); termios_set = 1; } diff --git a/util-linux/more.c b/util-linux/more.c index 7fa60bd..debad81 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -43,7 +43,6 @@ struct globals { unsigned terminal_width; unsigned terminal_height; struct termios initial_settings; - struct termios new_settings; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { setup_common_bufsiz(); } while (0) @@ -101,12 +100,9 @@ int more_main(int argc UNUSED_PARAM, char **argv) return bb_cat(argv); G.tty_fileno = fileno(tty); - tcgetattr(G.tty_fileno, &G.initial_settings); - G.new_settings = G.initial_settings; - G.new_settings.c_lflag &= ~(ICANON | ECHO); - G.new_settings.c_cc[VMIN] = 1; - G.new_settings.c_cc[VTIME] = 0; - tcsetattr_tty_TCSANOW(&G.new_settings); + + /* Turn on unbuffered input; turn off echoing */ + set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0); bb_signals(BB_FATAL_SIGS, gotsig); do { |