aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <mmpolacek@gmail.com>2010-10-28 21:34:56 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-10-28 21:34:56 +0200
commit7b18107384d950358e146d42bf02b391fab5ffd6 (patch)
tree9994e2bcaa038c3128ed8b3bca99486e22f660fb
parent02788ac7e2a44eee889aa1005e89076f928e964a (diff)
downloadbusybox-w32-7b18107384d950358e146d42bf02b391fab5ffd6.tar.gz
busybox-w32-7b18107384d950358e146d42bf02b391fab5ffd6.tar.bz2
busybox-w32-7b18107384d950358e146d42bf02b391fab5ffd6.zip
*: 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>
-rw-r--r--console-tools/reset.c12
-rw-r--r--console-tools/resize.c2
-rw-r--r--console-tools/showkey.c73
-rw-r--r--libbb/lineedit.c20
-rw-r--r--miscutils/conspy.c13
-rw-r--r--miscutils/less.c14
-rw-r--r--miscutils/watchdog.c2
-rw-r--r--util-linux/more.c14
8 files changed, 78 insertions, 72 deletions
diff --git a/console-tools/reset.c b/console-tools/reset.c
index 7dffdea18..1806ce742 100644
--- a/console-tools/reset.c
+++ b/console-tools/reset.c
@@ -8,11 +8,13 @@
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 */ 9 */
10 10
11#include "libbb.h"
12
13/* BTW, which "standard" package has this utility? It doesn't seem 11/* BTW, which "standard" package has this utility? It doesn't seem
14 * to be ncurses, coreutils, console-tools... then what? */ 12 * to be ncurses, coreutils, console-tools... then what? */
15 13
14#include "libbb.h"
15
16#define ESC "\033"
17
16#if ENABLE_STTY 18#if ENABLE_STTY
17int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 19int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
18#endif 20#endif
@@ -26,15 +28,15 @@ int reset_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
26 28
27 /* no options, no getopt */ 29 /* no options, no getopt */
28 30
29 if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { 31 if (/*isatty(STDIN_FILENO) &&*/ isatty(STDOUT_FILENO)) {
30 /* See 'man 4 console_codes' for details: 32 /* See 'man 4 console_codes' for details:
31 * "ESC c" -- Reset 33 * "ESC c" -- Reset
32 * "ESC ( K" -- Select user mapping 34 * "ESC ( K" -- Select user mapping
33 * "ESC [ J" -- Erase to the end of screen
34 * "ESC [ 0 m" -- Reset all display attributes 35 * "ESC [ 0 m" -- Reset all display attributes
36 * "ESC [ J" -- Erase to the end of screen
35 * "ESC [ ? 25 h" -- Make cursor visible 37 * "ESC [ ? 25 h" -- Make cursor visible
36 */ 38 */
37 printf("\033c\033(K\033[J\033[0m\033[?25h"); 39 printf(ESC"c" ESC"(K" ESC"[0m" ESC"[J" ESC"[?25h");
38 /* http://bugs.busybox.net/view.php?id=1414: 40 /* http://bugs.busybox.net/view.php?id=1414:
39 * people want it to reset echo etc: */ 41 * people want it to reset echo etc: */
40#if ENABLE_STTY 42#if ENABLE_STTY
diff --git a/console-tools/resize.c b/console-tools/resize.c
index 12e50a116..fdfe2a6a0 100644
--- a/console-tools/resize.c
+++ b/console-tools/resize.c
@@ -17,7 +17,7 @@ static void
17onintr(int sig UNUSED_PARAM) 17onintr(int sig UNUSED_PARAM)
18{ 18{
19 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p); 19 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
20 exit(EXIT_FAILURE); 20 _exit(EXIT_FAILURE);
21} 21}
22 22
23int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 23int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
diff --git a/console-tools/showkey.c b/console-tools/showkey.c
index 149ea6465..b29c84d6a 100644
--- a/console-tools/showkey.c
+++ b/console-tools/showkey.c
@@ -10,27 +10,7 @@
10#include "libbb.h" 10#include "libbb.h"
11#include <linux/kd.h> 11#include <linux/kd.h>
12 12
13// set raw tty mode
14// also used by microcom
15// libbb candidates?
16static void xget1(struct termios *t, struct termios *oldt)
17{
18 tcgetattr(STDIN_FILENO, oldt);
19 *t = *oldt;
20 cfmakeraw(t);
21}
22
23static void xset1(struct termios *tio)
24{
25 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, tio);
26 if (ret) {
27 bb_perror_msg("can't tcsetattr for stdin");
28 }
29}
30 13
31/*
32 * GLOBALS
33 */
34struct globals { 14struct globals {
35 int kbmode; 15 int kbmode;
36 struct termios tio, tio0; 16 struct termios tio, tio0;
@@ -44,13 +24,22 @@ struct globals {
44} while (0) 24} while (0)
45 25
46 26
47static void signal_handler(int signo) 27// set raw tty mode
28// also used by microcom
29// libbb candidates?
30static void xget1(struct termios *t, struct termios *oldt)
48{ 31{
49 // restore keyboard and console settings 32 tcgetattr(STDIN_FILENO, oldt);
50 xset1(&tio0); 33 *t = *oldt;
51 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); 34 cfmakeraw(t);
52 // alarmed? -> exit 0 35}
53 exit(SIGALRM == signo); 36
37static void xset1(struct termios *t)
38{
39 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
40 if (ret) {
41 bb_perror_msg("can't tcsetattr for stdin");
42 }
54} 43}
55 44
56int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 45int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -62,20 +51,21 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
62 OPT_s = (1<<2), // display only the raw scan-codes 51 OPT_s = (1<<2), // display only the raw scan-codes
63 }; 52 };
64 53
54 INIT_G();
55
65 // FIXME: aks are all mutually exclusive 56 // FIXME: aks are all mutually exclusive
66 getopt32(argv, "aks"); 57 getopt32(argv, "aks");
67 58
68 INIT_G();
69
70 // get keyboard settings 59 // get keyboard settings
71 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode); 60 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
72 printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n", 61 printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
73 kbmode == K_RAW ? "RAW" : 62 kbmode == K_RAW ? "RAW" :
74 (kbmode == K_XLATE ? "XLATE" : 63 (kbmode == K_XLATE ? "XLATE" :
75 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" : 64 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
76 (kbmode == K_UNICODE ? "UNICODE" : "?UNKNOWN?"))) 65 (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
77 , (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress" 66 , (option_mask32 & OPT_a) ? "on EOF (ctrl-D)" : "10s after last keypress"
78 ); 67 );
68
79 // prepare for raw mode 69 // prepare for raw mode
80 xget1(&tio, &tio0); 70 xget1(&tio, &tio0);
81 // put stdin in raw mode 71 // put stdin in raw mode
@@ -83,34 +73,37 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
83 73
84 if (option_mask32 & OPT_a) { 74 if (option_mask32 & OPT_a) {
85 unsigned char c; 75 unsigned char c;
76
86 // just read stdin char by char 77 // just read stdin char by char
87 while (1 == safe_read(STDIN_FILENO, &c, 1)) { 78 while (1 == read(STDIN_FILENO, &c, 1)) {
88 printf("%3u 0%03o 0x%02x\r\n", c, c, c); 79 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
89 if (04 /*CTRL-D*/ == c) 80 if (04 /*CTRL-D*/ == c)
90 break; 81 break;
91 } 82 }
92 } else { 83 } else {
93 // we should exit on any signal
94 bb_signals(BB_FATAL_SIGS, signal_handler);
95 // set raw keyboard mode 84 // set raw keyboard mode
96 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW)); 85 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
97 86
87 // we should exit on any signal; signals should interrupt read
88 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
89
98 // read and show scancodes 90 // read and show scancodes
99 while (1) { 91 while (!bb_got_signal) {
100 char buf[18]; 92 char buf[18];
101 int i, n; 93 int i, n;
94
102 // setup 10s watchdog 95 // setup 10s watchdog
103 alarm(10); 96 alarm(10);
104 // read scancodes 97 // read scancodes
105 n = read(STDIN_FILENO, buf, sizeof(buf)); 98 n = read(STDIN_FILENO, buf, sizeof(buf));
106 i = 0; 99 i = 0;
107 while (i < n) { 100 while (i < n) {
108 char c = buf[i];
109 // show raw scancodes ordered? ->
110 if (option_mask32 & OPT_s) { 101 if (option_mask32 & OPT_s) {
102 // show raw scancodes
111 printf("0x%02x ", buf[i++]); 103 printf("0x%02x ", buf[i++]);
112 // show interpreted scancodes (default) ? ->
113 } else { 104 } else {
105 // show interpreted scancodes (default)
106 char c = buf[i];
114 int kc; 107 int kc;
115 if (i+2 < n 108 if (i+2 < n
116 && (c & 0x7f) == 0 109 && (c & 0x7f) == 0
@@ -130,9 +123,9 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
130 } 123 }
131 } 124 }
132 125
133 // cleanup 126 // restore keyboard and console settings
134 signal_handler(SIGALRM); 127 xset1(&tio0);
128 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
135 129
136 // should never be here!
137 return EXIT_SUCCESS; 130 return EXIT_SUCCESS;
138} 131}
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 066b569f6..68006ffba 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -94,8 +94,10 @@ static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
94#endif 94#endif
95 95
96 96
97#define SEQ_CLEAR_TILL_END_OF_SCREEN "\033[J" 97#define ESC "\033"
98//#define SEQ_CLEAR_TILL_END_OF_LINE "\033[K" 98
99#define SEQ_CLEAR_TILL_END_OF_SCREEN ESC"[J"
100//#define SEQ_CLEAR_TILL_END_OF_LINE ESC"[K"
99 101
100 102
101enum { 103enum {
@@ -446,7 +448,7 @@ static void input_backward(unsigned num)
446 } while (--num); 448 } while (--num);
447 return; 449 return;
448 } 450 }
449 printf("\033[%uD", num); 451 printf(ESC"[%uD", num);
450 return; 452 return;
451 } 453 }
452 454
@@ -471,7 +473,7 @@ static void input_backward(unsigned num)
471 */ 473 */
472 unsigned sv_cursor; 474 unsigned sv_cursor;
473 /* go to 1st column; go up to first line */ 475 /* go to 1st column; go up to first line */
474 printf("\r" "\033[%uA", cmdedit_y); 476 printf("\r" ESC"[%uA", cmdedit_y);
475 cmdedit_y = 0; 477 cmdedit_y = 0;
476 sv_cursor = cursor; 478 sv_cursor = cursor;
477 put_prompt(); /* sets cursor to 0 */ 479 put_prompt(); /* sets cursor to 0 */
@@ -488,12 +490,12 @@ static void input_backward(unsigned num)
488 cmdedit_x = (width * cmdedit_y - num) % width; 490 cmdedit_x = (width * cmdedit_y - num) % width;
489 cmdedit_y -= lines_up; 491 cmdedit_y -= lines_up;
490 /* go to 1st column; go up */ 492 /* go to 1st column; go up */
491 printf("\r" "\033[%uA", lines_up); 493 printf("\r" ESC"[%uA", lines_up);
492 /* go to correct column. 494 /* go to correct column.
493 * xterm, konsole, Linux VT interpret 0 as 1 below! wow. 495 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
494 * need to *make sure* we skip it if cmdedit_x == 0 */ 496 * need to *make sure* we skip it if cmdedit_x == 0 */
495 if (cmdedit_x) 497 if (cmdedit_x)
496 printf("\033[%uC", cmdedit_x); 498 printf(ESC"[%uC", cmdedit_x);
497 } 499 }
498} 500}
499 501
@@ -501,7 +503,7 @@ static void input_backward(unsigned num)
501static void redraw(int y, int back_cursor) 503static void redraw(int y, int back_cursor)
502{ 504{
503 if (y > 0) /* up y lines */ 505 if (y > 0) /* up y lines */
504 printf("\033[%uA", y); 506 printf(ESC"[%uA", y);
505 bb_putchar('\r'); 507 bb_putchar('\r');
506 put_prompt(); 508 put_prompt();
507 put_till_end_and_adv_cursor(); 509 put_till_end_and_adv_cursor();
@@ -1626,7 +1628,7 @@ static void ask_terminal(void)
1626 pfd.events = POLLIN; 1628 pfd.events = POLLIN;
1627 if (safe_poll(&pfd, 1, 0) == 0) { 1629 if (safe_poll(&pfd, 1, 0) == 0) {
1628 S.sent_ESC_br6n = 1; 1630 S.sent_ESC_br6n = 1;
1629 fputs("\033" "[6n", stdout); 1631 fputs(ESC"[6n", stdout);
1630 fflush_all(); /* make terminal see it ASAP! */ 1632 fflush_all(); /* make terminal see it ASAP! */
1631 } 1633 }
1632} 1634}
@@ -2074,7 +2076,7 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li
2074 case CTRL('L'): 2076 case CTRL('L'):
2075 vi_case(CTRL('L')|VI_CMDMODE_BIT:) 2077 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
2076 /* Control-l -- clear screen */ 2078 /* Control-l -- clear screen */
2077 printf("\033[H"); /* cursor to top,left */ 2079 printf(ESC"[H"); /* cursor to top,left */
2078 redraw(0, command_len - cursor); 2080 redraw(0, command_len - cursor);
2079 break; 2081 break;
2080#if MAX_HISTORY > 0 2082#if MAX_HISTORY > 0
diff --git a/miscutils/conspy.c b/miscutils/conspy.c
index 01928b35f..040fa86f3 100644
--- a/miscutils/conspy.c
+++ b/miscutils/conspy.c
@@ -43,6 +43,9 @@
43#include "libbb.h" 43#include "libbb.h"
44#include <sys/kd.h> 44#include <sys/kd.h>
45 45
46
47#define ESC "\033"
48
46struct screen_info { 49struct screen_info {
47 unsigned char lines, cols, cursor_x, cursor_y; 50 unsigned char lines, cols, cursor_x, cursor_y;
48}; 51};
@@ -70,7 +73,7 @@ struct globals {
70 unsigned col; 73 unsigned col;
71 unsigned line; 74 unsigned line;
72 smallint curoff; // unknown:0 cursor on:-1 cursor off:1 75 smallint curoff; // unknown:0 cursor on:-1 cursor off:1
73 char attrbuf[sizeof("\033[0;1;5;30;40m")]; 76 char attrbuf[sizeof(ESC"[0;1;5;30;40m")];
74 // remote console 77 // remote console
75 struct screen_info remote; 78 struct screen_info remote;
76 // saved local tty terminfo 79 // saved local tty terminfo
@@ -101,7 +104,7 @@ enum {
101static void clrscr(void) 104static void clrscr(void)
102{ 105{
103 // Home, clear till end of screen 106 // Home, clear till end of screen
104 fputs("\033[1;1H" "\033[J", stdout); 107 fputs(ESC"[1;1H" ESC"[J", stdout);
105 G.col = G.line = 0; 108 G.col = G.line = 0;
106} 109}
107 110
@@ -109,7 +112,7 @@ static void set_cursor(int state)
109{ 112{
110 if (G.curoff != state) { 113 if (G.curoff != state) {
111 G.curoff = state; 114 G.curoff = state;
112 fputs("\033[?25", stdout); 115 fputs(ESC"[?25", stdout);
113 bb_putchar("h?l"[1 + state]); 116 bb_putchar("h?l"[1 + state]);
114 } 117 }
115} 118}
@@ -119,7 +122,7 @@ static void gotoxy(int col, int line)
119 if (G.col != col || G.line != line) { 122 if (G.col != col || G.line != line) {
120 G.col = col; 123 G.col = col;
121 G.line = line; 124 G.line = line;
122 printf("\033[%u;%uH", line + 1, col + 1); 125 printf(ESC"[%u;%uH", line + 1, col + 1);
123 } 126 }
124} 127}
125 128
@@ -132,7 +135,7 @@ static void cleanup(int code)
132 } 135 }
133 // Reset attributes 136 // Reset attributes
134 if (!BW) 137 if (!BW)
135 fputs("\033[0m", stdout); 138 fputs(ESC"[0m", stdout);
136 bb_putchar('\n'); 139 bb_putchar('\n');
137 if (code > 1) 140 if (code > 1)
138 kill_myself_with_sig(code); 141 kill_myself_with_sig(code);
diff --git a/miscutils/less.c b/miscutils/less.c
index 500059d2a..9e12c11a7 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -28,13 +28,15 @@
28#include "xregex.h" 28#include "xregex.h"
29#endif 29#endif
30 30
31
32#define ESC "\033"
31/* The escape codes for highlighted and normal text */ 33/* The escape codes for highlighted and normal text */
32#define HIGHLIGHT "\033[7m" 34#define HIGHLIGHT ESC"[7m"
33#define NORMAL "\033[0m" 35#define NORMAL ESC"[0m"
34/* The escape code to home and clear to the end of screen */ 36/* The escape code to home and clear to the end of screen */
35#define CLEAR "\033[H\033[J" 37#define CLEAR ESC"[H\033[J"
36/* The escape code to clear to the end of line */ 38/* The escape code to clear to the end of line */
37#define CLEAR_2_EOL "\033[K" 39#define CLEAR_2_EOL ESC"[K"
38 40
39enum { 41enum {
40/* Absolute max of lines eaten */ 42/* Absolute max of lines eaten */
@@ -165,12 +167,12 @@ static void set_tty_cooked(void)
165 top-left corner of the console */ 167 top-left corner of the console */
166static void move_cursor(int line, int row) 168static void move_cursor(int line, int row)
167{ 169{
168 printf("\033[%u;%uH", line, row); 170 printf(ESC"[%u;%uH", line, row);
169} 171}
170 172
171static void clear_line(void) 173static void clear_line(void)
172{ 174{
173 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2); 175 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
174} 176}
175 177
176static void print_hilite(const char *str) 178static void print_hilite(const char *str)
diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c
index 36902a214..630782660 100644
--- a/miscutils/watchdog.c
+++ b/miscutils/watchdog.c
@@ -24,7 +24,7 @@ static void watchdog_shutdown(int sig UNUSED_PARAM)
24 write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */ 24 write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
25 if (ENABLE_FEATURE_CLEAN_UP) 25 if (ENABLE_FEATURE_CLEAN_UP)
26 close(3); 26 close(3);
27 exit(EXIT_SUCCESS); 27 _exit(EXIT_SUCCESS);
28} 28}
29 29
30int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 30int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
diff --git a/util-linux/more.c b/util-linux/more.c
index 1fd6f9ee8..788609a08 100644
--- a/util-linux/more.c
+++ b/util-linux/more.c
@@ -29,16 +29,20 @@ struct globals {
29#define new_settings (G.new_settings ) 29#define new_settings (G.new_settings )
30#define cin_fileno (G.cin_fileno ) 30#define cin_fileno (G.cin_fileno )
31 31
32#define setTermSettings(fd, argp) do { \ 32#define setTermSettings(fd, argp) \
33 if (ENABLE_FEATURE_USE_TERMIOS) tcsetattr(fd, TCSANOW, argp); \ 33do { \
34 } while (0) 34 if (ENABLE_FEATURE_USE_TERMIOS) \
35 tcsetattr(fd, TCSANOW, argp); \
36} while (0)
35#define getTermSettings(fd, argp) tcgetattr(fd, argp) 37#define getTermSettings(fd, argp) tcgetattr(fd, argp)
36 38
37static void gotsig(int sig UNUSED_PARAM) 39static void gotsig(int sig UNUSED_PARAM)
38{ 40{
39 bb_putchar('\n'); 41 /* bb_putchar_stderr doesn't use stdio buffering,
42 * therefore it is safe in signal handler */
43 bb_putchar_stderr('\n');
40 setTermSettings(cin_fileno, &initial_settings); 44 setTermSettings(cin_fileno, &initial_settings);
41 exit(EXIT_FAILURE); 45 _exit(EXIT_FAILURE);
42} 46}
43 47
44#define CONVERTED_TAB_SIZE 8 48#define CONVERTED_TAB_SIZE 8