aboutsummaryrefslogtreecommitdiff
path: root/libbb
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 /libbb
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>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/lineedit.c21
-rw-r--r--libbb/xfuncs.c55
2 files changed, 45 insertions, 31 deletions
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