diff options
author | Rob Landley <rob@landley.net> | 2018-08-17 23:02:00 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2018-08-17 23:02:00 -0500 |
commit | 17a2d5783ac38d1b79f37e2524d390ac7386dbf4 (patch) | |
tree | 1595c2a1c488c44241174fadd9ab55a756dc4053 | |
parent | 151e3f782333acbe349fdaac3ec843e42e95be3f (diff) | |
download | toybox-17a2d5783ac38d1b79f37e2524d390ac7386dbf4.tar.gz |
Make microcom use set_terminal() and move speed setting into set_terminal().
-rw-r--r-- | lib/interestingtimes.c | 30 | ||||
-rw-r--r-- | lib/lib.h | 4 | ||||
-rw-r--r-- | lib/password.c | 2 | ||||
-rw-r--r-- | toys/example/demo_scankey.c | 2 | ||||
-rw-r--r-- | toys/net/microcom.c | 30 | ||||
-rw-r--r-- | toys/other/hexedit.c | 2 | ||||
-rw-r--r-- | toys/posix/ps.c | 2 |
7 files changed, 33 insertions, 39 deletions
diff --git a/lib/interestingtimes.c b/lib/interestingtimes.c index 073a67dd..bc8e0fea 100644 --- a/lib/interestingtimes.c +++ b/lib/interestingtimes.c @@ -80,12 +80,14 @@ int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy) } // Reset terminal to known state, saving copy of old state if old != NULL. -int set_terminal(int fd, int raw, struct termios *old) +int set_terminal(int fd, int raw, int speed, struct termios *old) { struct termios termio; + int i = tcgetattr(fd, &termio); // Fetch local copy of old terminfo, and copy struct contents to *old if set - if (!tcgetattr(fd, &termio) && old) *old = termio; + if (i) return i; + if (old) *old = termio; // the following are the bits set for an xterm. Linux text mode TTYs by // default add two additional bits that only matter for serial processing @@ -110,12 +112,28 @@ int set_terminal(int fd, int raw, struct termios *old) if (raw) cfmakeraw(&termio); - return tcsetattr(fd, TCSANOW, &termio); + if (speed) { + int i, speeds[] = {50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, + 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, + 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, + 2500000, 3000000, 3500000, 4000000}; + + // Find speed in table, adjust to constant + for (i = 0; i < ARRAY_LEN(speeds); i++) if (speeds[i] == speed) break; + if (i == ARRAY_LEN(speeds)) error_exit("unknown speed: %d", speed); + cfsetspeed(&termio, i+1+4081*(i>15)); + } + + return tcsetattr(fd, TCSAFLUSH, &termio); } -void xset_terminal(int fd, int raw, struct termios *old) +void xset_terminal(int fd, int raw, int speed, struct termios *old) { - if (-1 == set_terminal(fd, raw, old)) perror_exit("bad tty fd#%d", fd); + if (-1 != set_terminal(fd, raw, speed, old)) return; + + sprintf(libbuf, "/proc/self/fd/%d", fd); + libbuf[readlink0(libbuf, libbuf, sizeof(libbuf))] = 0; + perror_exit("tcsetattr %s", libbuf); } struct scan_key_list { @@ -226,7 +244,7 @@ void tty_jump(int x, int y) void tty_reset(void) { - set_terminal(0, 0, 0); + set_terminal(0, 0, 0, 0); tty_esc("?25h"); tty_esc("0m"); tty_jump(0, 999); @@ -285,8 +285,8 @@ int tty_fd(void); int terminal_size(unsigned *xx, unsigned *yy); int terminal_probesize(unsigned *xx, unsigned *yy); int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy); -int set_terminal(int fd, int raw, struct termios *old); -void xset_terminal(int fd, int raw, struct termios *old); +int set_terminal(int fd, int raw, int speed, struct termios *old); +void xset_terminal(int fd, int raw, int speed, struct termios *old); int scan_key(char *scratch, int miliwait); void tty_esc(char *s); void tty_jump(int x, int y); diff --git a/lib/password.c b/lib/password.c index 20c2f481..a02bc542 100644 --- a/lib/password.c +++ b/lib/password.c @@ -59,7 +59,7 @@ int read_password(char *buf, int buflen, char *mesg) sigaction(SIGINT, &sa, &oldsa); tcflush(0, TCIFLUSH); - set_terminal(0, 1, &oldtermio); + xset_terminal(0, 1, 0, &oldtermio); xprintf("%s", mesg); diff --git a/toys/example/demo_scankey.c b/toys/example/demo_scankey.c index d21490c8..cc625092 100644 --- a/toys/example/demo_scankey.c +++ b/toys/example/demo_scankey.c @@ -33,7 +33,7 @@ void demo_scankey_main(void) tty_esc("?25l"); // hide cursor tty_esc("0m"); // reset color to default tty_esc("2J"); // Clear screen - xset_terminal(1, 1, 0); // Raw mode + xset_terminal(1, 1, 0, 0); // Raw mode for (;;) { tty_jump(x, y); diff --git a/toys/net/microcom.c b/toys/net/microcom.c index d34e1fee..62fe85e6 100644 --- a/toys/net/microcom.c +++ b/toys/net/microcom.c @@ -26,22 +26,7 @@ GLOBALS( struct termios original_stdin_state, original_fd_state; ) -// Puts `fd` into raw mode, setting the baud rate if `speed` != 0, -// and saving the original terminal state. -static void xraw(int fd, const char *name, speed_t speed, - struct termios *original) -{ - struct termios t; - - if (tcgetattr(fd, &t)) perror_exit("tcgetattr %s", name); - *original = t; - - cfmakeraw(&t); - if (speed) cfsetspeed(&t, speed); - - if (tcsetattr(fd, TCSAFLUSH, &t)) perror_exit("tcsetattr %s", name); -} - +// TODO: tty_sigreset outputs ansi escape sequences, how to disable? static void restore_states(int i) { tcsetattr(0, TCSAFLUSH, &TT.original_stdin_state); @@ -50,29 +35,20 @@ static void restore_states(int i) void microcom_main(void) { - int speeds[] = {50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, - 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, - 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, - 2500000, 3000000, 3500000, 4000000}; struct pollfd fds[2]; int i, speed; if (!TT.s) speed = 115200; else speed = atoi(TT.s); - // Find speed in table, adjust to constant - for (i = 0; i < ARRAY_LEN(speeds); i++) if (speeds[i] == speed) break; - if (i == ARRAY_LEN(speeds)) error_exit("unknown speed: %s", TT.s); - speed = i+1+4081*(i>15); - // Open with O_NDELAY, but switch back to blocking for reads. TT.fd = xopen(*toys.optargs, O_RDWR | O_NOCTTY | O_NDELAY); if (-1==(i = fcntl(TT.fd, F_GETFL, 0)) || fcntl(TT.fd, F_SETFL, i&~O_NDELAY)) perror_exit_raw(*toys.optargs); // Set both input and output to raw mode. - xraw(TT.fd, "fd", speed, &TT.original_fd_state); - xraw(0, "stdin", 0, &TT.original_stdin_state); + xset_terminal(TT.fd, 1, speed, &TT.original_fd_state); + set_terminal(0, 1, 0, &TT.original_stdin_state); // ...and arrange to restore things, however we may exit. sigatexit(restore_states); diff --git a/toys/other/hexedit.c b/toys/other/hexedit.c index 3c5ada3a..06a96a22 100644 --- a/toys/other/hexedit.c +++ b/toys/other/hexedit.c @@ -132,7 +132,7 @@ void hexedit_main(void) tty_esc("0m"); tty_esc("?25l"); fflush(0); - xset_terminal(1, 1, 0); + xset_terminal(1, 1, 0, 0); if ((TT.len = fdlength(fd))<1) error_exit("bad length"); if (sizeof(long)==32 && TT.len>SIZE_MAX) TT.len = SIZE_MAX; diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 43c4ab1b..5133b69b 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1730,7 +1730,7 @@ static void top_setup(char *defo, char *defk) // Grab starting time, make terminal raw, switch off cursor, // set signal handler to put terminal/cursor back to normal at exit. TT.time = millitime(); - set_terminal(0, 1, 0); + set_terminal(0, 1, 0, 0); sigatexit(tty_sigreset); xsignal(SIGWINCH, generic_signal); printf("\033[?25l\033[0m"); |