summaryrefslogtreecommitdiffhomepage
path: root/libbb/xfuncs.c
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/xfuncs.c
parente58b44755dbac7c55bf602f7f76dfb37b47323f5 (diff)
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/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..d7647704e 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -311,40 +311,65 @@ 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)
+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:
-// grep for "tcsetattr"
-
- struct termios newterm;
+//TODO: slattach, shell read might be adapted to use this too: grep for "tcsetattr", "[VTIME] = 0"
+ int r;
- tcgetattr(fd, oldterm);
- newterm = *oldterm;
+ memset(oldterm, 0, sizeof(*oldterm)); /* paranoia */
+ r = 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);
+ newterm->c_lflag &= ~(ICANON | ECHO | ECHONL);
if (flags & TERMIOS_CLEAR_ISIG) {
/* dont recognize INT/QUIT/SUSP chars */
- newterm.c_lflag &= ~ISIG;
+ newterm->c_lflag &= ~ISIG;
}
/* reads will block only if < 1 char is available */
- newterm.c_cc[VMIN] = 1;
+ newterm->c_cc[VMIN] = 1;
/* no timeout (reads block forever) */
- newterm.c_cc[VTIME] = 0;
+ newterm->c_cc[VTIME] = 0;
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 */
- newterm.c_iflag &= ~(IXON | ICRNL);
- /* dont convert NL to CR on output */
- newterm.c_oflag &= ~(ONLCR);
+ newterm->c_iflag &= ~(IXON | ICRNL);
+ /* dont convert NL to CR+NL on output */
+ 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) {
+ /* 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 */
- 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);
}