*: use _exit() in sighandlers; showkey: do not use exit-thru-sighandler

While at it, make ESC sequences more readable; and removed check for
isatty(stdin) in reset. Code shrink:

   text    data     bss     dec     hex filename
 884771     936   17216  902923   dc70b busybox_old
 884723     936   17216  902875   dc6db busybox_unstripped

Signed-off-by: Marek Polacek <mmpolacek@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Marek Polacek
2010-10-28 21:34:56 +02:00
committed by Denys Vlasenko
parent 02788ac7e2
commit 7b18107384
8 changed files with 78 additions and 72 deletions

View File

@@ -8,11 +8,13 @@
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#include "libbb.h"
/* BTW, which "standard" package has this utility? It doesn't seem
* to be ncurses, coreutils, console-tools... then what? */
#include "libbb.h"
#define ESC "\033"
#if ENABLE_STTY
int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
#endif
@@ -26,15 +28,15 @@ int reset_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
/* no options, no getopt */
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
if (/*isatty(STDIN_FILENO) &&*/ isatty(STDOUT_FILENO)) {
/* See 'man 4 console_codes' for details:
* "ESC c" -- Reset
* "ESC ( K" -- Select user mapping
* "ESC [ J" -- Erase to the end of screen
* "ESC [ 0 m" -- Reset all display attributes
* "ESC [ J" -- Erase to the end of screen
* "ESC [ ? 25 h" -- Make cursor visible
*/
printf("\033c\033(K\033[J\033[0m\033[?25h");
printf(ESC"c" ESC"(K" ESC"[0m" ESC"[J" ESC"[?25h");
/* http://bugs.busybox.net/view.php?id=1414:
* people want it to reset echo etc: */
#if ENABLE_STTY

View File

@@ -17,7 +17,7 @@ static void
onintr(int sig UNUSED_PARAM)
{
tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
exit(EXIT_FAILURE);
_exit(EXIT_FAILURE);
}
int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;

View File

@@ -10,27 +10,7 @@
#include "libbb.h"
#include <linux/kd.h>
// set raw tty mode
// also used by microcom
// libbb candidates?
static void xget1(struct termios *t, struct termios *oldt)
{
tcgetattr(STDIN_FILENO, oldt);
*t = *oldt;
cfmakeraw(t);
}
static void xset1(struct termios *tio)
{
int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, tio);
if (ret) {
bb_perror_msg("can't tcsetattr for stdin");
}
}
/*
* GLOBALS
*/
struct globals {
int kbmode;
struct termios tio, tio0;
@@ -44,13 +24,22 @@ struct globals {
} while (0)
static void signal_handler(int signo)
// set raw tty mode
// also used by microcom
// libbb candidates?
static void xget1(struct termios *t, struct termios *oldt)
{
// restore keyboard and console settings
xset1(&tio0);
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
// alarmed? -> exit 0
exit(SIGALRM == signo);
tcgetattr(STDIN_FILENO, oldt);
*t = *oldt;
cfmakeraw(t);
}
static void xset1(struct termios *t)
{
int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
if (ret) {
bb_perror_msg("can't tcsetattr for stdin");
}
}
int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -62,20 +51,21 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
OPT_s = (1<<2), // display only the raw scan-codes
};
INIT_G();
// FIXME: aks are all mutually exclusive
getopt32(argv, "aks");
INIT_G();
// get keyboard settings
xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
kbmode == K_RAW ? "RAW" :
(kbmode == K_XLATE ? "XLATE" :
(kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
(kbmode == K_UNICODE ? "UNICODE" : "?UNKNOWN?")))
, (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress"
(kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
, (option_mask32 & OPT_a) ? "on EOF (ctrl-D)" : "10s after last keypress"
);
// prepare for raw mode
xget1(&tio, &tio0);
// put stdin in raw mode
@@ -83,34 +73,37 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
if (option_mask32 & OPT_a) {
unsigned char c;
// just read stdin char by char
while (1 == safe_read(STDIN_FILENO, &c, 1)) {
while (1 == read(STDIN_FILENO, &c, 1)) {
printf("%3u 0%03o 0x%02x\r\n", c, c, c);
if (04 /*CTRL-D*/ == c)
break;
}
} else {
// we should exit on any signal
bb_signals(BB_FATAL_SIGS, signal_handler);
// set raw keyboard mode
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
// we should exit on any signal; signals should interrupt read
bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
// read and show scancodes
while (1) {
while (!bb_got_signal) {
char buf[18];
int i, n;
// setup 10s watchdog
alarm(10);
// read scancodes
n = read(STDIN_FILENO, buf, sizeof(buf));
i = 0;
while (i < n) {
char c = buf[i];
// show raw scancodes ordered? ->
if (option_mask32 & OPT_s) {
// show raw scancodes
printf("0x%02x ", buf[i++]);
// show interpreted scancodes (default) ? ->
} else {
// show interpreted scancodes (default)
char c = buf[i];
int kc;
if (i+2 < n
&& (c & 0x7f) == 0
@@ -130,9 +123,9 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
}
}
// cleanup
signal_handler(SIGALRM);
// restore keyboard and console settings
xset1(&tio0);
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
// should never be here!
return EXIT_SUCCESS;
}