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>
This commit is contained in:
Denys Vlasenko 2009-10-27 10:34:06 +01:00
parent 1502a8b4ce
commit eb62d7c383

View File

@ -140,6 +140,9 @@ struct lineedit_statics {
smallint newdelflag; /* whether delbuf should be reused yet */ smallint newdelflag; /* whether delbuf should be reused yet */
CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */ CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
#endif #endif
#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
smallint sent_ESC_br_n6;
#endif
/* Formerly these were big buffers on stack: */ /* Formerly these were big buffers on stack: */
#if ENABLE_FEATURE_TAB_COMPLETION #if ENABLE_FEATURE_TAB_COMPLETION
@ -371,7 +374,8 @@ static void put_prompt(void)
{ {
out1str(cmdedit_prompt); out1str(cmdedit_prompt);
fflush(NULL); fflush(NULL);
if (ENABLE_FEATURE_EDITING_ASK_TERMINAL) { #if ENABLE_FEATURE_EDITING_ASK_TERMINAL
{
/* Ask terminal where is the cursor now. /* Ask terminal where is the cursor now.
* lineedit_read_key handles response and corrects * lineedit_read_key handles response and corrects
* our idea of current cursor position. * our idea of current cursor position.
@ -405,10 +409,12 @@ static void put_prompt(void)
pfd.fd = STDIN_FILENO; pfd.fd = STDIN_FILENO;
pfd.events = POLLIN; pfd.events = POLLIN;
if (safe_poll(&pfd, 1, 0) == 0) { if (safe_poll(&pfd, 1, 0) == 0) {
S.sent_ESC_br_n6 = 1;
out1str("\033" "[6n"); out1str("\033" "[6n");
fflush(NULL); /* make terminal see it ASAP! */ fflush(NULL); /* make terminal see it ASAP! */
} }
} }
#endif
cursor = 0; cursor = 0;
{ {
unsigned w = cmdedit_termw; /* volatile var */ unsigned w = cmdedit_termw; /* volatile var */
@ -1666,10 +1672,12 @@ static int lineedit_read_key(char *read_key_buffer)
/* Note: read_key sets errno to 0 on success: */ /* Note: read_key sets errno to 0 on success: */
ic = read_key(STDIN_FILENO, read_key_buffer); ic = read_key(STDIN_FILENO, read_key_buffer);
if (ENABLE_FEATURE_EDITING_ASK_TERMINAL #if ENABLE_FEATURE_EDITING_ASK_TERMINAL
&& cursor == 0 /* otherwise it may be bogus */ if ((int32_t)ic == KEYCODE_CURSOR_POS
&& (int32_t)ic == KEYCODE_CURSOR_POS && S.sent_ESC_br_n6
) { ) {
S.sent_ESC_br_n6 = 0;
if (cursor == 0) { /* otherwise it may be bogus */
int col = ((ic >> 32) & 0x7fff) - 1; int col = ((ic >> 32) & 0x7fff) - 1;
if (col > cmdedit_prmt_len) { if (col > cmdedit_prmt_len) {
cmdedit_x += (col - cmdedit_prmt_len); cmdedit_x += (col - cmdedit_prmt_len);
@ -1678,8 +1686,10 @@ static int lineedit_read_key(char *read_key_buffer)
cmdedit_y++; cmdedit_y++;
} }
} }
}
goto poll_again; goto poll_again;
} }
#endif
#if ENABLE_FEATURE_ASSUME_UNICODE #if ENABLE_FEATURE_ASSUME_UNICODE
{ {
@ -2192,6 +2202,21 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li
#endif #endif
} /* while (1) */ } /* while (1) */
#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
if (S.sent_ESC_br_n6) {
/* "sleep 1; busybox ash" + hold [Enter] to trigger.
* We sent "ESC [ 6 n", but got '\n' first, and
* KEYCODE_CURSOR_POS response is now buffered from terminal.
* It's bad already and not much can be done with it
* (it _will_ be visible for the next process to read stdin),
* but without this delay it even shows up on the screen
* as garbage because we restore echo settings with tcsetattr
* before it comes in. UGLY!
*/
usleep(20*1000);
}
#endif
/* Stop bug catching using "command_must_not_be_used" trick */ /* Stop bug catching using "command_must_not_be_used" trick */
#undef command #undef command