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>
This commit is contained in:
Denys Vlasenko 2017-09-15 17:14:01 +02:00
parent e58b44755d
commit aaaaaa5ad6
5 changed files with 53 additions and 48 deletions

View File

@ -1582,6 +1582,7 @@ int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC;
#define TERMIOS_CLEAR_ISIG (1 << 0) #define TERMIOS_CLEAR_ISIG (1 << 0)
#define TERMIOS_RAW_CRNL (1 << 1) #define TERMIOS_RAW_CRNL (1 << 1)
#define TERMIOS_RAW_INPUT (1 << 2) #define TERMIOS_RAW_INPUT (1 << 2)
int get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags) FAST_FUNC;
int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC; 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! */ /* NB: "unsigned request" is crucial! "int request" will break some arches! */

View File

@ -2259,7 +2259,7 @@ static int32_t reverse_i_search(int timeout)
*/ */
int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize) int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
{ {
int len; int len, n;
int timeout; int timeout;
#if ENABLE_FEATURE_TAB_COMPLETION #if ENABLE_FEATURE_TAB_COMPLETION
smallint lastWasTab = 0; smallint lastWasTab = 0;
@ -2274,9 +2274,10 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman
INIT_S(); INIT_S();
if (tcgetattr(STDIN_FILENO, &initial_settings) < 0 n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
|| (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
) { );
if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
/* Happens when e.g. stty -echo was run before. /* Happens when e.g. stty -echo was run before.
* But if ICANON is not set, we don't come here. * But if ICANON is not set, we don't come here.
* (example: interactive python ^Z-backgrounded, * (example: interactive python ^Z-backgrounded,
@ -2329,18 +2330,6 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman
#endif #endif
#define command command_must_not_be_used #define command command_must_not_be_used
new_settings = initial_settings;
/* ~ICANON: unbuffered input (most c_cc[] are disabled, VMIN/VTIME are enabled) */
/* ~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 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;
/* Should be not needed if ISIG is off: */
/* Turn off CTRL-C */
/* new_settings.c_cc[VINTR] = _POSIX_VDISABLE; */
tcsetattr_stdin_TCSANOW(&new_settings); tcsetattr_stdin_TCSANOW(&new_settings);
#if ENABLE_USERNAME_OR_HOMEDIR #if ENABLE_USERNAME_OR_HOMEDIR

View File

@ -311,40 +311,65 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
return tcsetattr(STDIN_FILENO, TCSANOW, tp); return tcsetattr(STDIN_FILENO, TCSANOW, tp);
} }
int FAST_FUNC set_termios_to_raw(int fd, struct termios *oldterm, int flags) int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags)
{ {
//TODO: lineedit, microcom, slattach, less might be adapted to use this too: //TODO: slattach, shell read might be adapted to use this too: grep for "tcsetattr", "[VTIME] = 0"
// grep for "tcsetattr" int r;
struct termios newterm; memset(oldterm, 0, sizeof(*oldterm)); /* paranoia */
r = tcgetattr(fd, oldterm);
tcgetattr(fd, oldterm); *newterm = *oldterm;
newterm = *oldterm;
/* Turn off buffered input (ICANON) /* Turn off buffered input (ICANON)
* Turn off echoing (ECHO) * Turn off echoing (ECHO)
* and separate echoing of newline (ECHONL, normally off anyway) * and separate echoing of newline (ECHONL, normally off anyway)
*/ */
newterm.c_lflag &= ~(ICANON | ECHO | ECHONL); newterm->c_lflag &= ~(ICANON | ECHO | ECHONL);
if (flags & TERMIOS_CLEAR_ISIG) { if (flags & TERMIOS_CLEAR_ISIG) {
/* dont recognize INT/QUIT/SUSP chars */ /* dont recognize INT/QUIT/SUSP chars */
newterm.c_lflag &= ~ISIG; newterm->c_lflag &= ~ISIG;
} }
/* reads will block only if < 1 char is available */ /* reads will block only if < 1 char is available */
newterm.c_cc[VMIN] = 1; newterm->c_cc[VMIN] = 1;
/* no timeout (reads block forever) */ /* no timeout (reads block forever) */
newterm.c_cc[VTIME] = 0; newterm->c_cc[VTIME] = 0;
if (flags & TERMIOS_RAW_CRNL) { if (flags & TERMIOS_RAW_CRNL) {
/* IXON, IXOFF, and IXANY:
* IXOFF=1: sw flow control is enabled on input queue:
* tty transmits a STOP char when input queue is close to full
* and transmits a START char when input queue is nearly empty.
* IXON=1: sw flow control is enabled on output queue:
* tty will stop sending if STOP char is received,
* and resume sending if START is received, or if any char
* is received and IXANY=1.
*/
/* IXON=0: XON/XOFF chars are treated as normal chars (why we do this?) */
/* dont convert CR to NL on input */ /* dont convert CR to NL on input */
newterm.c_iflag &= ~(IXON | ICRNL); newterm->c_iflag &= ~(IXON | ICRNL);
/* dont convert NL to CR on output */ /* dont convert NL to CR+NL on output */
newterm.c_oflag &= ~(ONLCR); newterm->c_oflag &= ~(ONLCR);
/* Maybe clear more c_oflag bits? usually, only OPOST and ONLCR are set.
* OPOST Enable implementation-defined output processing (is this reqd for all other bits to work?)
* OLCUC Map lowercase characters to uppercase on output.
* OCRNL Map CR to NL on output.
* ONOCR Don't output CR at column 0.
* ONLRET Don't output CR.
*/
} }
if (flags & TERMIOS_RAW_INPUT) { if (flags & TERMIOS_RAW_INPUT) {
/* IXOFF=0: disable sending XON/XOFF if input buf is full */
/* IXON=0: XON/XOFF chars are treated as normal chars */
/* dont convert anything on input */ /* dont convert anything on input */
newterm.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL); newterm->c_iflag &= ~(IXOFF|IXON|IXANY|BRKINT|INLCR|ICRNL|IUCLC|IMAXBEL);
} }
return r;
}
int FAST_FUNC set_termios_to_raw(int fd, struct termios *oldterm, int flags)
{
struct termios newterm;
get_termios_and_make_raw(fd, &newterm, oldterm, flags);
return tcsetattr(fd, TCSANOW, &newterm); return tcsetattr(fd, TCSANOW, &newterm);
} }

View File

@ -1824,15 +1824,9 @@ int less_main(int argc, char **argv)
G.kbd_fd_orig_flags = ndelay_on(tty_fd); G.kbd_fd_orig_flags = ndelay_on(tty_fd);
kbd_fd = tty_fd; /* save in a global */ kbd_fd = tty_fd; /* save in a global */
tcgetattr(kbd_fd, &term_orig); get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL);
term_less = term_orig;
term_less.c_lflag &= ~(ICANON | ECHO);
term_less.c_iflag &= ~(IXON | ICRNL);
/*term_less.c_oflag &= ~ONLCR;*/
term_less.c_cc[VMIN] = 1;
term_less.c_cc[VTIME] = 0;
IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line); IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line);
/* 20: two tabstops + 4 */ /* 20: two tabstops + 4 */
if (width < 20 || max_displayed_line < 3) if (width < 20 || max_displayed_line < 3)
return bb_cat(argv); return bb_cat(argv);

View File

@ -33,15 +33,11 @@
// set raw tty mode // set raw tty mode
static void xget1(int fd, struct termios *t, struct termios *oldt) static void xget1(int fd, struct termios *t, struct termios *oldt)
{ {
//TODO: use set_termios_to_raw() get_termios_and_make_raw(fd, t, oldt, 0
tcgetattr(fd, oldt); | TERMIOS_CLEAR_ISIG /* ^C is ASCII char 3, not "interrupt me!" */
*t = *oldt; | TERMIOS_RAW_INPUT /* pass all chars verbatim, no special handling or translating CR->NL */
cfmakeraw(t); | TERMIOS_RAW_CRNL /* dont convert NL<->CR on output too */
// t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN); );
// t->c_iflag &= ~(BRKINT|IXON|ICRNL);
// t->c_oflag &= ~(ONLCR);
// t->c_cc[VMIN] = 1;
// t->c_cc[VTIME] = 0;
} }
static int xset1(int fd, struct termios *tio, const char *device) static int xset1(int fd, struct termios *tio, const char *device)