diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-16 20:55:52 -0700 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-16 20:55:52 -0700 |
commit | 2bb651ae10e36999949812d3ac4f7789ed665037 (patch) | |
tree | 01474cd07b98a67d419078a114b65aeb1c44c3a5 | |
parent | 6f69f2dbc6dec739a576d6403551784453db59f3 (diff) | |
download | busybox-w32-2bb651ae10e36999949812d3ac4f7789ed665037.tar.gz busybox-w32-2bb651ae10e36999949812d3ac4f7789ed665037.tar.bz2 busybox-w32-2bb651ae10e36999949812d3ac4f7789ed665037.zip |
vi: code shrink; save/restore errno in signal handlers
function old new delta
query_screen_dimensions - 54 +54
suspend_sig 50 64 +14
cont_sig 65 66 +1
catch_sig 42 32 -10
winch_sig 88 60 -28
edit_file 719 671 -48
refresh 848 767 -81
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/4 up/down: 69/-167) Total: -98 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/vi.c | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/editors/vi.c b/editors/vi.c index 633d42a8c..94d36ae99 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -502,6 +502,19 @@ static int init_text_buffer(char *fn) | |||
502 | return rc; | 502 | return rc; |
503 | } | 503 | } |
504 | 504 | ||
505 | #if ENABLE_FEATURE_VI_WIN_RESIZE | ||
506 | static void query_screen_dimensions(void) | ||
507 | { | ||
508 | get_terminal_width_height(STDIN_FILENO, &columns, &rows); | ||
509 | if (rows > MAX_SCR_ROWS) | ||
510 | rows = MAX_SCR_ROWS; | ||
511 | if (columns > MAX_SCR_COLS) | ||
512 | columns = MAX_SCR_COLS; | ||
513 | } | ||
514 | #else | ||
515 | # define query_screen_dimensions() ((void)0) | ||
516 | #endif | ||
517 | |||
505 | static void edit_file(char *fn) | 518 | static void edit_file(char *fn) |
506 | { | 519 | { |
507 | #if ENABLE_FEATURE_VI_YANKMARK | 520 | #if ENABLE_FEATURE_VI_YANKMARK |
@@ -518,11 +531,7 @@ static void edit_file(char *fn) | |||
518 | rows = 24; | 531 | rows = 24; |
519 | columns = 80; | 532 | columns = 80; |
520 | size = 0; | 533 | size = 0; |
521 | if (ENABLE_FEATURE_VI_WIN_RESIZE) { | 534 | query_screen_dimensions(); |
522 | get_terminal_width_height(0, &columns, &rows); | ||
523 | if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS; | ||
524 | if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS; | ||
525 | } | ||
526 | new_screen(rows, columns); // get memory for virtual screen | 535 | new_screen(rows, columns); // get memory for virtual screen |
527 | init_text_buffer(fn); | 536 | init_text_buffer(fn); |
528 | 537 | ||
@@ -537,7 +546,7 @@ static void edit_file(char *fn) | |||
537 | ccol = 0; | 546 | ccol = 0; |
538 | 547 | ||
539 | #if ENABLE_FEATURE_VI_USE_SIGNALS | 548 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
540 | catch_sig(0); | 549 | signal(SIGINT, catch_sig); |
541 | signal(SIGWINCH, winch_sig); | 550 | signal(SIGWINCH, winch_sig); |
542 | signal(SIGTSTP, suspend_sig); | 551 | signal(SIGTSTP, suspend_sig); |
543 | sig = sigsetjmp(restart, 1); | 552 | sig = sigsetjmp(restart, 1); |
@@ -563,7 +572,7 @@ static void edit_file(char *fn) | |||
563 | char *p, *q; | 572 | char *p, *q; |
564 | int n = 0; | 573 | int n = 0; |
565 | 574 | ||
566 | while ((p = initial_cmds[n])) { | 575 | while ((p = initial_cmds[n]) != NULL) { |
567 | do { | 576 | do { |
568 | q = p; | 577 | q = p; |
569 | p = strchr(q, '\n'); | 578 | p = strchr(q, '\n'); |
@@ -2143,50 +2152,51 @@ static void cookmode(void) | |||
2143 | tcsetattr_stdin_TCSANOW(&term_orig); | 2152 | tcsetattr_stdin_TCSANOW(&term_orig); |
2144 | } | 2153 | } |
2145 | 2154 | ||
2146 | //----- Come here when we get a window resize signal --------- | ||
2147 | #if ENABLE_FEATURE_VI_USE_SIGNALS | 2155 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
2156 | //----- Come here when we get a window resize signal --------- | ||
2148 | static void winch_sig(int sig UNUSED_PARAM) | 2157 | static void winch_sig(int sig UNUSED_PARAM) |
2149 | { | 2158 | { |
2159 | int save_errno = errno; | ||
2150 | // FIXME: do it in main loop!!! | 2160 | // FIXME: do it in main loop!!! |
2151 | signal(SIGWINCH, winch_sig); | 2161 | signal(SIGWINCH, winch_sig); |
2152 | if (ENABLE_FEATURE_VI_WIN_RESIZE) { | 2162 | query_screen_dimensions(); |
2153 | get_terminal_width_height(0, &columns, &rows); | ||
2154 | if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS; | ||
2155 | if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS; | ||
2156 | } | ||
2157 | new_screen(rows, columns); // get memory for virtual screen | 2163 | new_screen(rows, columns); // get memory for virtual screen |
2158 | redraw(TRUE); // re-draw the screen | 2164 | redraw(TRUE); // re-draw the screen |
2165 | errno = save_errno; | ||
2159 | } | 2166 | } |
2160 | 2167 | ||
2161 | //----- Come here when we get a continue signal ------------------- | 2168 | //----- Come here when we get a continue signal ------------------- |
2162 | static void cont_sig(int sig UNUSED_PARAM) | 2169 | static void cont_sig(int sig UNUSED_PARAM) |
2163 | { | 2170 | { |
2171 | int save_errno = errno; | ||
2164 | rawmode(); // terminal to "raw" | 2172 | rawmode(); // terminal to "raw" |
2165 | last_status_cksum = 0; // force status update | 2173 | last_status_cksum = 0; // force status update |
2166 | redraw(TRUE); // re-draw the screen | 2174 | redraw(TRUE); // re-draw the screen |
2167 | 2175 | ||
2168 | signal(SIGTSTP, suspend_sig); | 2176 | signal(SIGTSTP, suspend_sig); |
2169 | signal(SIGCONT, SIG_DFL); | 2177 | signal(SIGCONT, SIG_DFL); |
2170 | kill(my_pid, SIGCONT); // huh? why? we are already "continued"... | 2178 | //kill(my_pid, SIGCONT); // huh? why? we are already "continued"... |
2179 | errno = save_errno; | ||
2171 | } | 2180 | } |
2172 | 2181 | ||
2173 | //----- Come here when we get a Suspend signal ------------------- | 2182 | //----- Come here when we get a Suspend signal ------------------- |
2174 | static void suspend_sig(int sig UNUSED_PARAM) | 2183 | static void suspend_sig(int sig UNUSED_PARAM) |
2175 | { | 2184 | { |
2185 | int save_errno = errno; | ||
2176 | go_bottom_and_clear_to_eol(); | 2186 | go_bottom_and_clear_to_eol(); |
2177 | cookmode(); // terminal to "cooked" | 2187 | cookmode(); // terminal to "cooked" |
2178 | 2188 | ||
2179 | signal(SIGCONT, cont_sig); | 2189 | signal(SIGCONT, cont_sig); |
2180 | signal(SIGTSTP, SIG_DFL); | 2190 | signal(SIGTSTP, SIG_DFL); |
2181 | kill(my_pid, SIGTSTP); | 2191 | kill(my_pid, SIGTSTP); |
2192 | errno = save_errno; | ||
2182 | } | 2193 | } |
2183 | 2194 | ||
2184 | //----- Come here when we get a signal --------------------------- | 2195 | //----- Come here when we get a signal --------------------------- |
2185 | static void catch_sig(int sig) | 2196 | static void catch_sig(int sig) |
2186 | { | 2197 | { |
2187 | signal(SIGINT, catch_sig); | 2198 | signal(SIGINT, catch_sig); |
2188 | if (sig) | 2199 | siglongjmp(restart, sig); |
2189 | siglongjmp(restart, sig); | ||
2190 | } | 2200 | } |
2191 | #endif /* FEATURE_VI_USE_SIGNALS */ | 2201 | #endif /* FEATURE_VI_USE_SIGNALS */ |
2192 | 2202 | ||
@@ -2781,9 +2791,7 @@ static void refresh(int full_screen) | |||
2781 | 2791 | ||
2782 | if (ENABLE_FEATURE_VI_WIN_RESIZE) { | 2792 | if (ENABLE_FEATURE_VI_WIN_RESIZE) { |
2783 | unsigned c = columns, r = rows; | 2793 | unsigned c = columns, r = rows; |
2784 | get_terminal_width_height(0, &columns, &rows); | 2794 | query_screen_dimensions(); |
2785 | if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS; | ||
2786 | if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS; | ||
2787 | full_screen |= (c - columns) | (r - rows); | 2795 | full_screen |= (c - columns) | (r - rows); |
2788 | } | 2796 | } |
2789 | sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot") | 2797 | sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot") |