aboutsummaryrefslogtreecommitdiff
path: root/libbb/xfuncs.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/xfuncs.c')
-rw-r--r--libbb/xfuncs.c55
1 files changed, 40 insertions, 15 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 1b3a1667b..0dfb3e2d9 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 output processing (reqd for OLCUC and *NL* 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: input XON/XOFF chars are not special */
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