diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-10-27 10:34:06 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-10-27 10:34:06 +0100 |
commit | eb62d7c383320923e8050a24a725277a1fd5c695 (patch) | |
tree | efd94b1d7b368ee58bc679a286f33a491f271e96 | |
parent | 1502a8b4cebe66870d188cda393a620fd48f0f3b (diff) | |
download | busybox-w32-eb62d7c383320923e8050a24a725277a1fd5c695.tar.gz busybox-w32-eb62d7c383320923e8050a24a725277a1fd5c695.tar.bz2 busybox-w32-eb62d7c383320923e8050a24a725277a1fd5c695.zip |
libbb/lineedit: try to make FEATURE_EDITING_ASK_TERMINAL less ugly
function old new delta
read_line_input 4809 4829 +20
lineedit_read_key 207 223 +16
put_prompt 110 117 +7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 43/0) Total: 43 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/lineedit.c | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 2e1bc5d2e..b4c3fd35c 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -140,6 +140,9 @@ struct lineedit_statics { | |||
140 | smallint newdelflag; /* whether delbuf should be reused yet */ | 140 | smallint newdelflag; /* whether delbuf should be reused yet */ |
141 | CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */ | 141 | CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */ |
142 | #endif | 142 | #endif |
143 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL | ||
144 | smallint sent_ESC_br_n6; | ||
145 | #endif | ||
143 | 146 | ||
144 | /* Formerly these were big buffers on stack: */ | 147 | /* Formerly these were big buffers on stack: */ |
145 | #if ENABLE_FEATURE_TAB_COMPLETION | 148 | #if ENABLE_FEATURE_TAB_COMPLETION |
@@ -371,7 +374,8 @@ static void put_prompt(void) | |||
371 | { | 374 | { |
372 | out1str(cmdedit_prompt); | 375 | out1str(cmdedit_prompt); |
373 | fflush(NULL); | 376 | fflush(NULL); |
374 | if (ENABLE_FEATURE_EDITING_ASK_TERMINAL) { | 377 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL |
378 | { | ||
375 | /* Ask terminal where is the cursor now. | 379 | /* Ask terminal where is the cursor now. |
376 | * lineedit_read_key handles response and corrects | 380 | * lineedit_read_key handles response and corrects |
377 | * our idea of current cursor position. | 381 | * our idea of current cursor position. |
@@ -405,10 +409,12 @@ static void put_prompt(void) | |||
405 | pfd.fd = STDIN_FILENO; | 409 | pfd.fd = STDIN_FILENO; |
406 | pfd.events = POLLIN; | 410 | pfd.events = POLLIN; |
407 | if (safe_poll(&pfd, 1, 0) == 0) { | 411 | if (safe_poll(&pfd, 1, 0) == 0) { |
412 | S.sent_ESC_br_n6 = 1; | ||
408 | out1str("\033" "[6n"); | 413 | out1str("\033" "[6n"); |
409 | fflush(NULL); /* make terminal see it ASAP! */ | 414 | fflush(NULL); /* make terminal see it ASAP! */ |
410 | } | 415 | } |
411 | } | 416 | } |
417 | #endif | ||
412 | cursor = 0; | 418 | cursor = 0; |
413 | { | 419 | { |
414 | unsigned w = cmdedit_termw; /* volatile var */ | 420 | unsigned w = cmdedit_termw; /* volatile var */ |
@@ -1666,20 +1672,24 @@ static int lineedit_read_key(char *read_key_buffer) | |||
1666 | /* Note: read_key sets errno to 0 on success: */ | 1672 | /* Note: read_key sets errno to 0 on success: */ |
1667 | ic = read_key(STDIN_FILENO, read_key_buffer); | 1673 | ic = read_key(STDIN_FILENO, read_key_buffer); |
1668 | 1674 | ||
1669 | if (ENABLE_FEATURE_EDITING_ASK_TERMINAL | 1675 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL |
1670 | && cursor == 0 /* otherwise it may be bogus */ | 1676 | if ((int32_t)ic == KEYCODE_CURSOR_POS |
1671 | && (int32_t)ic == KEYCODE_CURSOR_POS | 1677 | && S.sent_ESC_br_n6 |
1672 | ) { | 1678 | ) { |
1673 | int col = ((ic >> 32) & 0x7fff) - 1; | 1679 | S.sent_ESC_br_n6 = 0; |
1674 | if (col > cmdedit_prmt_len) { | 1680 | if (cursor == 0) { /* otherwise it may be bogus */ |
1675 | cmdedit_x += (col - cmdedit_prmt_len); | 1681 | int col = ((ic >> 32) & 0x7fff) - 1; |
1676 | while (cmdedit_x >= cmdedit_termw) { | 1682 | if (col > cmdedit_prmt_len) { |
1677 | cmdedit_x -= cmdedit_termw; | 1683 | cmdedit_x += (col - cmdedit_prmt_len); |
1678 | cmdedit_y++; | 1684 | while (cmdedit_x >= cmdedit_termw) { |
1685 | cmdedit_x -= cmdedit_termw; | ||
1686 | cmdedit_y++; | ||
1687 | } | ||
1679 | } | 1688 | } |
1680 | } | 1689 | } |
1681 | goto poll_again; | 1690 | goto poll_again; |
1682 | } | 1691 | } |
1692 | #endif | ||
1683 | 1693 | ||
1684 | #if ENABLE_FEATURE_ASSUME_UNICODE | 1694 | #if ENABLE_FEATURE_ASSUME_UNICODE |
1685 | { | 1695 | { |
@@ -2192,6 +2202,21 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li | |||
2192 | #endif | 2202 | #endif |
2193 | } /* while (1) */ | 2203 | } /* while (1) */ |
2194 | 2204 | ||
2205 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL | ||
2206 | if (S.sent_ESC_br_n6) { | ||
2207 | /* "sleep 1; busybox ash" + hold [Enter] to trigger. | ||
2208 | * We sent "ESC [ 6 n", but got '\n' first, and | ||
2209 | * KEYCODE_CURSOR_POS response is now buffered from terminal. | ||
2210 | * It's bad already and not much can be done with it | ||
2211 | * (it _will_ be visible for the next process to read stdin), | ||
2212 | * but without this delay it even shows up on the screen | ||
2213 | * as garbage because we restore echo settings with tcsetattr | ||
2214 | * before it comes in. UGLY! | ||
2215 | */ | ||
2216 | usleep(20*1000); | ||
2217 | } | ||
2218 | #endif | ||
2219 | |||
2195 | /* Stop bug catching using "command_must_not_be_used" trick */ | 2220 | /* Stop bug catching using "command_must_not_be_used" trick */ |
2196 | #undef command | 2221 | #undef command |
2197 | 2222 | ||