libbb: shrink lineedit_read_key()

function                                             old     new   delta
lineedit_read_key                                    237     231      -6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2022-01-18 00:31:27 +01:00
parent 8ad2acf352
commit 1e825acf8d
7 changed files with 25 additions and 16 deletions

View File

@ -1900,6 +1900,8 @@ enum {
* (unless fd is in non-blocking mode), * (unless fd is in non-blocking mode),
* subsequent reads will time out after a few milliseconds. * subsequent reads will time out after a few milliseconds.
* Return of -1 means EOF or error (errno == 0 on EOF). * Return of -1 means EOF or error (errno == 0 on EOF).
* Nonzero errno is not preserved across the call:
* if there was no error, errno will be cleared to 0.
* buffer[0] is used as a counter of buffered chars and must be 0 * buffer[0] is used as a counter of buffered chars and must be 0
* on first call. * on first call.
* timeout: * timeout:

View File

@ -2155,7 +2155,7 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
#endif #endif
fflush_all(); fflush_all();
while (1) { for (;;) {
/* Wait for input. TIMEOUT = -1 makes read_key wait even /* Wait for input. TIMEOUT = -1 makes read_key wait even
* on nonblocking stdin, TIMEOUT = 50 makes sure we won't * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
* insist on full MB_CUR_MAX buffer to declare input like * insist on full MB_CUR_MAX buffer to declare input like
@ -2167,24 +2167,30 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
* *
* Note: read_key sets errno to 0 on success. * Note: read_key sets errno to 0 on success.
*/ */
do { for (;;) {
if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) { if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) {
errno = EINTR; errno = EINTR;
return -1; return -1;
} }
//FIXME: still races here with signals, but small window to poll() inside read_key //FIXME: still races here with signals, but small window to poll() inside read_key
IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;) IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
/* errno = 0; - read_key does this itself */
ic = read_key(STDIN_FILENO, read_key_buffer, timeout); ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;) IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
} while (!(state->flags & LI_INTERRUPTIBLE) && errno == EINTR); if (errno != EINTR)
break;
if (errno) { if (state->flags & LI_INTERRUPTIBLE) {
/* LI_INTERRUPTIBLE can bail out with EINTR here, /* LI_INTERRUPTIBLE bails out on EINTR,
* but nothing really guarantees that bb_got_signal * but nothing really guarantees that bb_got_signal
* is nonzero. Follow the least surprise principle: * is nonzero. Follow the least surprise principle:
*/ */
if (errno == EINTR && bb_got_signal == 0) if (bb_got_signal == 0)
bb_got_signal = 255; /* something nonzero */ bb_got_signal = 255;
goto ret;
}
}
if (errno) {
#if ENABLE_UNICODE_SUPPORT #if ENABLE_UNICODE_SUPPORT
if (errno == EAGAIN && unicode_idx != 0) if (errno == EAGAIN && unicode_idx != 0)
goto pushback; goto pushback;
@ -2251,7 +2257,7 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
#endif #endif
break; break;
} }
ret:
return ic; return ic;
} }

View File

@ -291,6 +291,7 @@ int64_t FAST_FUNC safe_read_key(int fd, char *buffer, int timeout)
{ {
int64_t r; int64_t r;
do { do {
/* errno = 0; - read_key does this itself */
r = read_key(fd, buffer, timeout); r = read_key(fd, buffer, timeout);
} while (errno == EINTR); } while (errno == EINTR);
return r; return r;