aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-09-15 17:14:01 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-09-15 17:14:01 +0200
commitaaaaaa5ad6a93101d38800467fe3750b35fed6ea (patch)
tree8cd7b7561f3e923a382e5f97b4bd0fbe5fd68c39
parente58b44755dbac7c55bf602f7f76dfb37b47323f5 (diff)
downloadbusybox-w32-aaaaaa5ad6a93101d38800467fe3750b35fed6ea.tar.gz
busybox-w32-aaaaaa5ad6a93101d38800467fe3750b35fed6ea.tar.bz2
busybox-w32-aaaaaa5ad6a93101d38800467fe3750b35fed6ea.zip
less,microcom,lineedit: use common routine to set raw termios
function old new delta get_termios_and_make_raw - 139 +139 xget1 39 8 -31 read_line_input 3912 3867 -45 less_main 2525 2471 -54 set_termios_to_raw 116 36 -80 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/4 up/down: 139/-210) Total: -71 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/lineedit.c21
-rw-r--r--libbb/xfuncs.c55
-rw-r--r--miscutils/less.c12
-rw-r--r--miscutils/microcom.c14
5 files changed, 54 insertions, 49 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 06f887732..aff2825ac 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1582,6 +1582,7 @@ int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC;
1582#define TERMIOS_CLEAR_ISIG (1 << 0) 1582#define TERMIOS_CLEAR_ISIG (1 << 0)
1583#define TERMIOS_RAW_CRNL (1 << 1) 1583#define TERMIOS_RAW_CRNL (1 << 1)
1584#define TERMIOS_RAW_INPUT (1 << 2) 1584#define TERMIOS_RAW_INPUT (1 << 2)
1585int get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags) FAST_FUNC;
1585int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC; 1586int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC;
1586 1587
1587/* NB: "unsigned request" is crucial! "int request" will break some arches! */ 1588/* NB: "unsigned request" is crucial! "int request" will break some arches! */
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 17766a126..3a092ffe2 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2259,7 +2259,7 @@ static int32_t reverse_i_search(int timeout)
2259 */ 2259 */
2260int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize) 2260int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
2261{ 2261{
2262 int len; 2262 int len, n;
2263 int timeout; 2263 int timeout;
2264#if ENABLE_FEATURE_TAB_COMPLETION 2264#if ENABLE_FEATURE_TAB_COMPLETION
2265 smallint lastWasTab = 0; 2265 smallint lastWasTab = 0;
@@ -2274,9 +2274,10 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman
2274 2274
2275 INIT_S(); 2275 INIT_S();
2276 2276
2277 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0 2277 n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
2278 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON 2278 | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
2279 ) { 2279 );
2280 if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
2280 /* Happens when e.g. stty -echo was run before. 2281 /* Happens when e.g. stty -echo was run before.
2281 * But if ICANON is not set, we don't come here. 2282 * But if ICANON is not set, we don't come here.
2282 * (example: interactive python ^Z-backgrounded, 2283 * (example: interactive python ^Z-backgrounded,
@@ -2329,18 +2330,6 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman
2329#endif 2330#endif
2330#define command command_must_not_be_used 2331#define command command_must_not_be_used
2331 2332
2332 new_settings = initial_settings;
2333 /* ~ICANON: unbuffered input (most c_cc[] are disabled, VMIN/VTIME are enabled) */
2334 /* ~ECHO, ~ECHONL: turn off echoing, including newline echoing */
2335 /* ~ISIG: turn off INTR (ctrl-C), QUIT, SUSP */
2336 new_settings.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG);
2337 /* reads will block only if < 1 char is available */
2338 new_settings.c_cc[VMIN] = 1;
2339 /* no timeout (reads block forever) */
2340 new_settings.c_cc[VTIME] = 0;
2341 /* Should be not needed if ISIG is off: */
2342 /* Turn off CTRL-C */
2343 /* new_settings.c_cc[VINTR] = _POSIX_VDISABLE; */
2344 tcsetattr_stdin_TCSANOW(&new_settings); 2333 tcsetattr_stdin_TCSANOW(&new_settings);
2345 2334
2346#if ENABLE_USERNAME_OR_HOMEDIR 2335#if ENABLE_USERNAME_OR_HOMEDIR
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 1b3a1667b..d7647704e 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -311,40 +311,65 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
311 return tcsetattr(STDIN_FILENO, TCSANOW, tp); 311 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
312} 312}
313 313
314int FAST_FUNC set_termios_to_raw(int fd, struct termios *oldterm, int flags) 314int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags)
315{ 315{
316//TODO: lineedit, microcom, slattach, less might be adapted to use this too: 316//TODO: slattach, shell read might be adapted to use this too: grep for "tcsetattr", "[VTIME] = 0"
317// grep for "tcsetattr" 317 int r;
318
319 struct termios newterm;
320 318
321 tcgetattr(fd, oldterm); 319 memset(oldterm, 0, sizeof(*oldterm)); /* paranoia */
322 newterm = *oldterm; 320 r = tcgetattr(fd, oldterm);
321 *newterm = *oldterm;
323 322
324 /* Turn off buffered input (ICANON) 323 /* Turn off buffered input (ICANON)
325 * Turn off echoing (ECHO) 324 * Turn off echoing (ECHO)
326 * and separate echoing of newline (ECHONL, normally off anyway) 325 * and separate echoing of newline (ECHONL, normally off anyway)
327 */ 326 */
328 newterm.c_lflag &= ~(ICANON | ECHO | ECHONL); 327 newterm->c_lflag &= ~(ICANON | ECHO | ECHONL);
329 if (flags & TERMIOS_CLEAR_ISIG) { 328 if (flags & TERMIOS_CLEAR_ISIG) {
330 /* dont recognize INT/QUIT/SUSP chars */ 329 /* dont recognize INT/QUIT/SUSP chars */
331 newterm.c_lflag &= ~ISIG; 330 newterm->c_lflag &= ~ISIG;
332 } 331 }
333 /* reads will block only if < 1 char is available */ 332 /* reads will block only if < 1 char is available */
334 newterm.c_cc[VMIN] = 1; 333 newterm->c_cc[VMIN] = 1;
335 /* no timeout (reads block forever) */ 334 /* no timeout (reads block forever) */
336 newterm.c_cc[VTIME] = 0; 335 newterm->c_cc[VTIME] = 0;
337 if (flags & TERMIOS_RAW_CRNL) { 336 if (flags & TERMIOS_RAW_CRNL) {
337/* IXON, IXOFF, and IXANY:
338 * IXOFF=1: sw flow control is enabled on input queue:
339 * tty transmits a STOP char when input queue is close to full
340 * and transmits a START char when input queue is nearly empty.
341 * IXON=1: sw flow control is enabled on output queue:
342 * tty will stop sending if STOP char is received,
343 * and resume sending if START is received, or if any char
344 * is received and IXANY=1.
345 */
346 /* IXON=0: XON/XOFF chars are treated as normal chars (why we do this?) */
338 /* dont convert CR to NL on input */ 347 /* dont convert CR to NL on input */
339 newterm.c_iflag &= ~(IXON | ICRNL); 348 newterm->c_iflag &= ~(IXON | ICRNL);
340 /* dont convert NL to CR on output */ 349 /* dont convert NL to CR+NL on output */
341 newterm.c_oflag &= ~(ONLCR); 350 newterm->c_oflag &= ~(ONLCR);
351 /* Maybe clear more c_oflag bits? usually, only OPOST and ONLCR are set.
352 * OPOST Enable implementation-defined output processing (is this reqd for all other bits to work?)
353 * OLCUC Map lowercase characters to uppercase on output.
354 * OCRNL Map CR to NL on output.
355 * ONOCR Don't output CR at column 0.
356 * ONLRET Don't output CR.
357 */
342 } 358 }
343 if (flags & TERMIOS_RAW_INPUT) { 359 if (flags & TERMIOS_RAW_INPUT) {
360 /* IXOFF=0: disable sending XON/XOFF if input buf is full */
361 /* IXON=0: XON/XOFF chars are treated as normal chars */
344 /* dont convert anything on input */ 362 /* dont convert anything on input */
345 newterm.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL); 363 newterm->c_iflag &= ~(IXOFF|IXON|IXANY|BRKINT|INLCR|ICRNL|IUCLC|IMAXBEL);
346 } 364 }
365 return r;
366}
367
368int FAST_FUNC set_termios_to_raw(int fd, struct termios *oldterm, int flags)
369{
370 struct termios newterm;
347 371
372 get_termios_and_make_raw(fd, &newterm, oldterm, flags);
348 return tcsetattr(fd, TCSANOW, &newterm); 373 return tcsetattr(fd, TCSANOW, &newterm);
349} 374}
350 375
diff --git a/miscutils/less.c b/miscutils/less.c
index d524b6c87..c6c158a51 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -1824,15 +1824,9 @@ int less_main(int argc, char **argv)
1824 G.kbd_fd_orig_flags = ndelay_on(tty_fd); 1824 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
1825 kbd_fd = tty_fd; /* save in a global */ 1825 kbd_fd = tty_fd; /* save in a global */
1826 1826
1827 tcgetattr(kbd_fd, &term_orig); 1827 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL);
1828 term_less = term_orig; 1828
1829 term_less.c_lflag &= ~(ICANON | ECHO); 1829 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line);
1830 term_less.c_iflag &= ~(IXON | ICRNL);
1831 /*term_less.c_oflag &= ~ONLCR;*/
1832 term_less.c_cc[VMIN] = 1;
1833 term_less.c_cc[VTIME] = 0;
1834
1835 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1836 /* 20: two tabstops + 4 */ 1830 /* 20: two tabstops + 4 */
1837 if (width < 20 || max_displayed_line < 3) 1831 if (width < 20 || max_displayed_line < 3)
1838 return bb_cat(argv); 1832 return bb_cat(argv);
diff --git a/miscutils/microcom.c b/miscutils/microcom.c
index b87f3273f..fa090057e 100644
--- a/miscutils/microcom.c
+++ b/miscutils/microcom.c
@@ -33,15 +33,11 @@
33// set raw tty mode 33// set raw tty mode
34static void xget1(int fd, struct termios *t, struct termios *oldt) 34static void xget1(int fd, struct termios *t, struct termios *oldt)
35{ 35{
36//TODO: use set_termios_to_raw() 36 get_termios_and_make_raw(fd, t, oldt, 0
37 tcgetattr(fd, oldt); 37 | TERMIOS_CLEAR_ISIG /* ^C is ASCII char 3, not "interrupt me!" */
38 *t = *oldt; 38 | TERMIOS_RAW_INPUT /* pass all chars verbatim, no special handling or translating CR->NL */
39 cfmakeraw(t); 39 | TERMIOS_RAW_CRNL /* dont convert NL<->CR on output too */
40// t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN); 40 );
41// t->c_iflag &= ~(BRKINT|IXON|ICRNL);
42// t->c_oflag &= ~(ONLCR);
43// t->c_cc[VMIN] = 1;
44// t->c_cc[VTIME] = 0;
45} 41}
46 42
47static int xset1(int fd, struct termios *tio, const char *device) 43static int xset1(int fd, struct termios *tio, const char *device)