diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-05-31 15:55:03 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-05-31 15:55:03 +0000 |
| commit | a1d24a0b6e18352930701bdff68c7f88e474c4cf (patch) | |
| tree | 6ec43100b42c3fb66eb5997158b49abbc3cf3a90 /miscutils | |
| parent | 199c0d542b51b55b99dddc1742c23325288f79eb (diff) | |
| download | busybox-w32-a1d24a0b6e18352930701bdff68c7f88e474c4cf.tar.gz busybox-w32-a1d24a0b6e18352930701bdff68c7f88e474c4cf.tar.bz2 busybox-w32-a1d24a0b6e18352930701bdff68c7f88e474c4cf.zip | |
less: stop using data/bss. Code got smaller too.
# size busybox_old busybox_unstripped
text data bss dec hex filename
700719 2896 17880 721495 b0257 busybox_old
700547 2832 17432 720811 affab busybox_unstripped
# size */*/less.o
text data bss dec hex filename
6861 50 344 7255 1c57 busybox.t0/miscutils/less.o
6657 0 0 6657 1a01 busybox.t1/miscutils/less.o
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/less.c | 129 |
1 files changed, 82 insertions, 47 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 3df1f3785..d6b71b9f2 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
| @@ -72,27 +72,6 @@ enum { | |||
| 72 | TILDES = 1, | 72 | TILDES = 1, |
| 73 | }; | 73 | }; |
| 74 | 74 | ||
| 75 | static unsigned max_displayed_line; | ||
| 76 | static unsigned width; | ||
| 77 | static const char *empty_line_marker = "~"; | ||
| 78 | |||
| 79 | static char *filename; | ||
| 80 | static char **files; | ||
| 81 | static unsigned num_files = 1; | ||
| 82 | static unsigned current_file = 1; | ||
| 83 | static const char **buffer; | ||
| 84 | static const char **flines; | ||
| 85 | static int cur_fline; /* signed */ | ||
| 86 | static unsigned max_fline; | ||
| 87 | static unsigned max_lineno; /* this one tracks linewrap */ | ||
| 88 | |||
| 89 | static ssize_t eof_error = 1; /* eof if 0, error if < 0 */ | ||
| 90 | static char terminated = 1; | ||
| 91 | static size_t readpos; | ||
| 92 | static size_t readeof; | ||
| 93 | /* last position in last line, taking into account tabs */ | ||
| 94 | static size_t linepos; | ||
| 95 | |||
| 96 | /* Command line options */ | 75 | /* Command line options */ |
| 97 | enum { | 76 | enum { |
| 98 | FLAG_E = 1, | 77 | FLAG_E = 1, |
| @@ -104,25 +83,79 @@ enum { | |||
| 104 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, | 83 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, |
| 105 | }; | 84 | }; |
| 106 | 85 | ||
| 107 | #if ENABLE_FEATURE_LESS_MARKS | 86 | #if !ENABLE_FEATURE_LESS_REGEXP |
| 108 | static unsigned mark_lines[15][2]; | 87 | enum { pattern_valid = 0 }; |
| 109 | static unsigned num_marks; | ||
| 110 | #endif | 88 | #endif |
| 111 | 89 | ||
| 90 | struct globals { | ||
| 91 | int cur_fline; /* signed */ | ||
| 92 | int kbd_fd; /* fd to get input from */ | ||
| 93 | /* last position in last line, taking into account tabs */ | ||
| 94 | size_t linepos; | ||
| 95 | unsigned max_displayed_line; | ||
| 96 | unsigned max_fline; | ||
| 97 | unsigned max_lineno; /* this one tracks linewrap */ | ||
| 98 | unsigned width; | ||
| 99 | ssize_t eof_error; /* eof if 0, error if < 0 */ | ||
| 100 | size_t readpos; | ||
| 101 | size_t readeof; | ||
| 102 | const char **buffer; | ||
| 103 | const char **flines; | ||
| 104 | const char *empty_line_marker; | ||
| 105 | unsigned num_files; | ||
| 106 | unsigned current_file; | ||
| 107 | char *filename; | ||
| 108 | char **files; | ||
| 109 | #if ENABLE_FEATURE_LESS_MARKS | ||
| 110 | unsigned num_marks; | ||
| 111 | unsigned mark_lines[15][2]; | ||
| 112 | #endif | ||
| 112 | #if ENABLE_FEATURE_LESS_REGEXP | 113 | #if ENABLE_FEATURE_LESS_REGEXP |
| 113 | static unsigned *match_lines; | 114 | unsigned *match_lines; |
| 114 | static int match_pos; /* signed! */ | 115 | int match_pos; /* signed! */ |
| 115 | static unsigned num_matches; | 116 | unsigned num_matches; |
| 116 | static regex_t pattern; | 117 | regex_t pattern; |
| 117 | static unsigned pattern_valid; | 118 | smallint pattern_valid; |
| 118 | #else | ||
| 119 | enum { pattern_valid = 0 }; | ||
| 120 | #endif | 119 | #endif |
| 121 | 120 | smallint terminated; | |
| 122 | static struct termios term_orig, term_vi; | 121 | struct termios term_orig, term_less; |
| 123 | 122 | }; | |
| 124 | /* File pointer to get input from */ | 123 | #define G (*ptr_to_globals) |
| 125 | static int kbd_fd; | 124 | #define cur_fline (G.cur_fline ) |
| 125 | #define kbd_fd (G.kbd_fd ) | ||
| 126 | #define linepos (G.linepos ) | ||
| 127 | #define max_displayed_line (G.max_displayed_line) | ||
| 128 | #define max_fline (G.max_fline ) | ||
| 129 | #define max_lineno (G.max_lineno ) | ||
| 130 | #define width (G.width ) | ||
| 131 | #define eof_error (G.eof_error ) | ||
| 132 | #define readpos (G.readpos ) | ||
| 133 | #define readeof (G.readeof ) | ||
| 134 | #define buffer (G.buffer ) | ||
| 135 | #define flines (G.flines ) | ||
| 136 | #define empty_line_marker (G.empty_line_marker ) | ||
| 137 | #define num_files (G.num_files ) | ||
| 138 | #define current_file (G.current_file ) | ||
| 139 | #define filename (G.filename ) | ||
| 140 | #define files (G.files ) | ||
| 141 | #define num_marks (G.num_marks ) | ||
| 142 | #define mark_lines (G.mark_lines ) | ||
| 143 | #define match_lines (G.match_lines ) | ||
| 144 | #define match_pos (G.match_pos ) | ||
| 145 | #define num_matches (G.num_matches ) | ||
| 146 | #define pattern (G.pattern ) | ||
| 147 | #define pattern_valid (G.pattern_valid ) | ||
| 148 | #define terminated (G.terminated ) | ||
| 149 | #define term_orig (G.term_orig ) | ||
| 150 | #define term_less (G.term_less ) | ||
| 151 | #define INIT_G() do { \ | ||
| 152 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ | ||
| 153 | empty_line_marker = "~"; \ | ||
| 154 | num_files = 1; \ | ||
| 155 | current_file = 1; \ | ||
| 156 | eof_error = 1; \ | ||
| 157 | terminated = 1; \ | ||
| 158 | } while (0) | ||
| 126 | 159 | ||
| 127 | /* Reset terminal input to normal */ | 160 | /* Reset terminal input to normal */ |
| 128 | static void set_tty_cooked(void) | 161 | static void set_tty_cooked(void) |
| @@ -380,12 +413,12 @@ static void cap_cur_fline(int nlines) | |||
| 380 | } | 413 | } |
| 381 | } | 414 | } |
| 382 | 415 | ||
| 383 | static char controls[] = | 416 | static const char controls[] = |
| 384 | /* NUL: never encountered; TAB: not converted */ | 417 | /* NUL: never encountered; TAB: not converted */ |
| 385 | /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f" | 418 | /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f" |
| 386 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" | 419 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" |
| 387 | "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */ | 420 | "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */ |
| 388 | static char ctrlconv[] = | 421 | static const char ctrlconv[] = |
| 389 | /* '\n': it's a former NUL - subst with '@', not 'J' */ | 422 | /* '\n': it's a former NUL - subst with '@', not 'J' */ |
| 390 | "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f" | 423 | "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f" |
| 391 | "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"; | 424 | "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"; |
| @@ -604,7 +637,7 @@ static void getch_nowait(char* input, int sz) | |||
| 604 | FD_SET(0, &readfds); | 637 | FD_SET(0, &readfds); |
| 605 | } | 638 | } |
| 606 | FD_SET(kbd_fd, &readfds); | 639 | FD_SET(kbd_fd, &readfds); |
| 607 | tcsetattr(kbd_fd, TCSANOW, &term_vi); | 640 | tcsetattr(kbd_fd, TCSANOW, &term_less); |
| 608 | select(kbd_fd + 1, &readfds, NULL, NULL, NULL); | 641 | select(kbd_fd + 1, &readfds, NULL, NULL, NULL); |
| 609 | 642 | ||
| 610 | input[0] = '\0'; | 643 | input[0] = '\0'; |
| @@ -668,7 +701,7 @@ static char* less_gets(int sz) | |||
| 668 | 701 | ||
| 669 | /* I be damned if I know why is it needed *repeatedly*, | 702 | /* I be damned if I know why is it needed *repeatedly*, |
| 670 | * but it is needed. Is it because of stdio? */ | 703 | * but it is needed. Is it because of stdio? */ |
| 671 | tcsetattr(kbd_fd, TCSANOW, &term_vi); | 704 | tcsetattr(kbd_fd, TCSANOW, &term_less); |
| 672 | 705 | ||
| 673 | read(kbd_fd, &c, 1); | 706 | read(kbd_fd, &c, 1); |
| 674 | if (c == 0x0d) | 707 | if (c == 0x0d) |
| @@ -1244,6 +1277,8 @@ int less_main(int argc, char **argv) | |||
| 1244 | { | 1277 | { |
| 1245 | int keypress; | 1278 | int keypress; |
| 1246 | 1279 | ||
| 1280 | INIT_G(); | ||
| 1281 | |||
| 1247 | /* TODO: -x: do not interpret backspace, -xx: tab also */ | 1282 | /* TODO: -x: do not interpret backspace, -xx: tab also */ |
| 1248 | /* -xxx: newline also */ | 1283 | /* -xxx: newline also */ |
| 1249 | /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */ | 1284 | /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */ |
| @@ -1283,16 +1318,16 @@ int less_main(int argc, char **argv) | |||
| 1283 | tcgetattr(kbd_fd, &term_orig); | 1318 | tcgetattr(kbd_fd, &term_orig); |
| 1284 | signal(SIGTERM, sig_catcher); | 1319 | signal(SIGTERM, sig_catcher); |
| 1285 | signal(SIGINT, sig_catcher); | 1320 | signal(SIGINT, sig_catcher); |
| 1286 | term_vi = term_orig; | 1321 | term_less = term_orig; |
| 1287 | term_vi.c_lflag &= ~(ICANON | ECHO); | 1322 | term_less.c_lflag &= ~(ICANON | ECHO); |
| 1288 | term_vi.c_iflag &= ~(IXON | ICRNL); | 1323 | term_less.c_iflag &= ~(IXON | ICRNL); |
| 1289 | /*term_vi.c_oflag &= ~ONLCR;*/ | 1324 | /*term_less.c_oflag &= ~ONLCR;*/ |
| 1290 | term_vi.c_cc[VMIN] = 1; | 1325 | term_less.c_cc[VMIN] = 1; |
| 1291 | term_vi.c_cc[VTIME] = 0; | 1326 | term_less.c_cc[VTIME] = 0; |
| 1292 | 1327 | ||
| 1293 | /* Want to do it just once, but it doesn't work, */ | 1328 | /* Want to do it just once, but it doesn't work, */ |
| 1294 | /* so we are redoing it (see code above). Mystery... */ | 1329 | /* so we are redoing it (see code above). Mystery... */ |
| 1295 | /*tcsetattr(kbd_fd, TCSANOW, &term_vi);*/ | 1330 | /*tcsetattr(kbd_fd, TCSANOW, &term_less);*/ |
| 1296 | 1331 | ||
| 1297 | reinitialize(); | 1332 | reinitialize(); |
| 1298 | while (1) { | 1333 | while (1) { |
