diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-02 17:27:28 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-02 17:27:28 +0200 |
commit | 84ea60ed65f6ea6fd3b2170e44bbff5de410a78b (patch) | |
tree | 54837341ed6ed021d9b20400b8a8a5aea7bd3b62 | |
parent | dd4b446f76736c0a13a61a38d7d816b6e6b5fca2 (diff) | |
download | busybox-w32-84ea60ed65f6ea6fd3b2170e44bbff5de410a78b.tar.gz busybox-w32-84ea60ed65f6ea6fd3b2170e44bbff5de410a78b.tar.bz2 busybox-w32-84ea60ed65f6ea6fd3b2170e44bbff5de410a78b.zip |
line editing: make read_line_input() not take timeout param
It's almost always -1.
function old new delta
read_line_input 3902 3912 +10
new_line_input_t 24 31 +7
pgetc 583 585 +2
save_command_ps_at_cur_history 80 78 -2
read_line 76 74 -2
fgetc_interactive 246 244 -2
addLines 84 82 -2
doCommands 2226 2222 -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/5 up/down: 19/-12) Total: 7 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/ed.c | 6 | ||||
-rw-r--r-- | include/libbb.h | 11 | ||||
-rw-r--r-- | libbb/lineedit.c | 23 | ||||
-rw-r--r-- | shell/ash.c | 5 | ||||
-rw-r--r-- | shell/hush.c | 3 | ||||
-rw-r--r-- | util-linux/fdisk.c | 2 |
6 files changed, 30 insertions, 20 deletions
diff --git a/editors/ed.c b/editors/ed.c index 7f21ded92..05797692c 100644 --- a/editors/ed.c +++ b/editors/ed.c | |||
@@ -360,7 +360,7 @@ static void addLines(int num) | |||
360 | * 0 on ctrl-C, | 360 | * 0 on ctrl-C, |
361 | * >0 length of input string, including terminating '\n' | 361 | * >0 length of input string, including terminating '\n' |
362 | */ | 362 | */ |
363 | len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1); | 363 | len = read_line_input(NULL, "", buf, sizeof(buf)); |
364 | if (len <= 0) { | 364 | if (len <= 0) { |
365 | /* Previously, ctrl-C was exiting to shell. | 365 | /* Previously, ctrl-C was exiting to shell. |
366 | * Now we exit to ed prompt. Is in important? */ | 366 | * Now we exit to ed prompt. Is in important? */ |
@@ -789,7 +789,7 @@ static void doCommands(void) | |||
789 | * 0 on ctrl-C, | 789 | * 0 on ctrl-C, |
790 | * >0 length of input string, including terminating '\n' | 790 | * >0 length of input string, including terminating '\n' |
791 | */ | 791 | */ |
792 | len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1); | 792 | len = read_line_input(NULL, ": ", buf, sizeof(buf)); |
793 | if (len <= 0) | 793 | if (len <= 0) |
794 | return; | 794 | return; |
795 | while (len && isspace(buf[--len])) | 795 | while (len && isspace(buf[--len])) |
@@ -892,7 +892,7 @@ static void doCommands(void) | |||
892 | } | 892 | } |
893 | if (!dirty) | 893 | if (!dirty) |
894 | return; | 894 | return; |
895 | len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1); | 895 | len = read_line_input(NULL, "Really quit? ", buf, 16); |
896 | /* read error/EOF - no way to continue */ | 896 | /* read error/EOF - no way to continue */ |
897 | if (len < 0) | 897 | if (len < 0) |
898 | return; | 898 | return; |
diff --git a/include/libbb.h b/include/libbb.h index 9aba71949..46180c5aa 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1639,9 +1639,9 @@ enum { | |||
1639 | * buffer[0] is used as a counter of buffered chars and must be 0 | 1639 | * buffer[0] is used as a counter of buffered chars and must be 0 |
1640 | * on first call. | 1640 | * on first call. |
1641 | * timeout: | 1641 | * timeout: |
1642 | * -2: do not poll for input; | 1642 | * -2: do not poll(-1) for input - read() it, return on EAGAIN at once |
1643 | * -1: poll(-1) (i.e. block); | 1643 | * -1: poll(-1) (i.e. block even on NONBLOCKed fd) |
1644 | * >=0: poll for TIMEOUT milliseconds, return -1/EAGAIN on timeout | 1644 | * >=0: poll() for TIMEOUT milliseconds, return -1/EAGAIN on timeout |
1645 | */ | 1645 | */ |
1646 | int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC; | 1646 | int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC; |
1647 | void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; | 1647 | void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; |
@@ -1657,6 +1657,7 @@ unsigned size_from_HISTFILESIZE(const char *hp) FAST_FUNC; | |||
1657 | # endif | 1657 | # endif |
1658 | typedef struct line_input_t { | 1658 | typedef struct line_input_t { |
1659 | int flags; | 1659 | int flags; |
1660 | int timeout; | ||
1660 | const char *path_lookup; | 1661 | const char *path_lookup; |
1661 | # if MAX_HISTORY | 1662 | # if MAX_HISTORY |
1662 | int cnt_history; | 1663 | int cnt_history; |
@@ -1692,7 +1693,7 @@ line_input_t *new_line_input_t(int flags) FAST_FUNC; | |||
1692 | * 0 on ctrl-C (the line entered is still returned in 'command'), | 1693 | * 0 on ctrl-C (the line entered is still returned in 'command'), |
1693 | * >0 length of input string, including terminating '\n' | 1694 | * >0 length of input string, including terminating '\n' |
1694 | */ | 1695 | */ |
1695 | int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout) FAST_FUNC; | 1696 | int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize) FAST_FUNC; |
1696 | void show_history(const line_input_t *st) FAST_FUNC; | 1697 | void show_history(const line_input_t *st) FAST_FUNC; |
1697 | # if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT | 1698 | # if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
1698 | void save_history(line_input_t *st); | 1699 | void save_history(line_input_t *st); |
@@ -1700,7 +1701,7 @@ void save_history(line_input_t *st); | |||
1700 | #else | 1701 | #else |
1701 | #define MAX_HISTORY 0 | 1702 | #define MAX_HISTORY 0 |
1702 | int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC; | 1703 | int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC; |
1703 | #define read_line_input(state, prompt, command, maxsize, timeout) \ | 1704 | #define read_line_input(state, prompt, command, maxsize) \ |
1704 | read_line_input(prompt, command, maxsize) | 1705 | read_line_input(prompt, command, maxsize) |
1705 | #endif | 1706 | #endif |
1706 | 1707 | ||
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index e5721b063..0106093a1 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -1267,6 +1267,7 @@ line_input_t* FAST_FUNC new_line_input_t(int flags) | |||
1267 | { | 1267 | { |
1268 | line_input_t *n = xzalloc(sizeof(*n)); | 1268 | line_input_t *n = xzalloc(sizeof(*n)); |
1269 | n->flags = flags; | 1269 | n->flags = flags; |
1270 | n->timeout = -1; | ||
1270 | #if MAX_HISTORY > 0 | 1271 | #if MAX_HISTORY > 0 |
1271 | n->max_history = MAX_HISTORY; | 1272 | n->max_history = MAX_HISTORY; |
1272 | #endif | 1273 | #endif |
@@ -2130,7 +2131,7 @@ enum { | |||
2130 | * Backspace deletes last matched char. | 2131 | * Backspace deletes last matched char. |
2131 | * Control keys exit search and return to normal editing (at current history line). | 2132 | * Control keys exit search and return to normal editing (at current history line). |
2132 | */ | 2133 | */ |
2133 | static int32_t reverse_i_search(void) | 2134 | static int32_t reverse_i_search(int timeout) |
2134 | { | 2135 | { |
2135 | char match_buf[128]; /* for user input */ | 2136 | char match_buf[128]; /* for user input */ |
2136 | char read_key_buffer[KEYCODE_BUFFER_SIZE]; | 2137 | char read_key_buffer[KEYCODE_BUFFER_SIZE]; |
@@ -2152,8 +2153,8 @@ static int32_t reverse_i_search(void) | |||
2152 | int h; | 2153 | int h; |
2153 | unsigned match_buf_len = strlen(match_buf); | 2154 | unsigned match_buf_len = strlen(match_buf); |
2154 | 2155 | ||
2155 | //FIXME: correct timeout? | 2156 | //FIXME: correct timeout? (i.e. count it down?) |
2156 | ic = lineedit_read_key(read_key_buffer, -1); | 2157 | ic = lineedit_read_key(read_key_buffer, timeout); |
2157 | 2158 | ||
2158 | switch (ic) { | 2159 | switch (ic) { |
2159 | case CTRL('R'): /* searching for the next match */ | 2160 | case CTRL('R'): /* searching for the next match */ |
@@ -2256,9 +2257,10 @@ static int32_t reverse_i_search(void) | |||
2256 | * (in both cases the cursor remains on the input line, '\n' is not printed) | 2257 | * (in both cases the cursor remains on the input line, '\n' is not printed) |
2257 | * >0 length of input string, including terminating '\n' | 2258 | * >0 length of input string, including terminating '\n' |
2258 | */ | 2259 | */ |
2259 | int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout) | 2260 | int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize) |
2260 | { | 2261 | { |
2261 | int len; | 2262 | int len; |
2263 | int timeout; | ||
2262 | #if ENABLE_FEATURE_TAB_COMPLETION | 2264 | #if ENABLE_FEATURE_TAB_COMPLETION |
2263 | smallint lastWasTab = 0; | 2265 | smallint lastWasTab = 0; |
2264 | #endif | 2266 | #endif |
@@ -2297,8 +2299,15 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman | |||
2297 | maxsize = MAX_LINELEN; | 2299 | maxsize = MAX_LINELEN; |
2298 | S.maxsize = maxsize; | 2300 | S.maxsize = maxsize; |
2299 | 2301 | ||
2300 | /* With zero flags, no other fields are ever used */ | 2302 | timeout = -1; |
2301 | state = st ? st : (line_input_t*) &const_int_0; | 2303 | /* Make state->flags == 0 if st is NULL. |
2304 | * With zeroed flags, no other fields are ever referenced. | ||
2305 | */ | ||
2306 | state = (line_input_t*) &const_int_0; | ||
2307 | if (st) { | ||
2308 | state = st; | ||
2309 | timeout = st->timeout; | ||
2310 | } | ||
2302 | #if MAX_HISTORY > 0 | 2311 | #if MAX_HISTORY > 0 |
2303 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY | 2312 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY |
2304 | if (state->hist_file) | 2313 | if (state->hist_file) |
@@ -2510,7 +2519,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman | |||
2510 | } | 2519 | } |
2511 | #if ENABLE_FEATURE_REVERSE_SEARCH | 2520 | #if ENABLE_FEATURE_REVERSE_SEARCH |
2512 | case CTRL('R'): | 2521 | case CTRL('R'): |
2513 | ic = ic_raw = reverse_i_search(); | 2522 | ic = ic_raw = reverse_i_search(timeout); |
2514 | goto again; | 2523 | goto again; |
2515 | #endif | 2524 | #endif |
2516 | 2525 | ||
diff --git a/shell/ash.c b/shell/ash.c index 78baa9aac..b285e3d33 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -10185,8 +10185,8 @@ preadfd(void) | |||
10185 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) | 10185 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
10186 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); | 10186 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
10187 | else { | 10187 | else { |
10188 | int timeout = -1; | ||
10189 | # if ENABLE_ASH_IDLE_TIMEOUT | 10188 | # if ENABLE_ASH_IDLE_TIMEOUT |
10189 | int timeout = -1; | ||
10190 | if (iflag) { | 10190 | if (iflag) { |
10191 | const char *tmout_var = lookupvar("TMOUT"); | 10191 | const char *tmout_var = lookupvar("TMOUT"); |
10192 | if (tmout_var) { | 10192 | if (tmout_var) { |
@@ -10195,12 +10195,13 @@ preadfd(void) | |||
10195 | timeout = -1; | 10195 | timeout = -1; |
10196 | } | 10196 | } |
10197 | } | 10197 | } |
10198 | line_input_state->timeout = timeout; | ||
10198 | # endif | 10199 | # endif |
10199 | # if ENABLE_FEATURE_TAB_COMPLETION | 10200 | # if ENABLE_FEATURE_TAB_COMPLETION |
10200 | line_input_state->path_lookup = pathval(); | 10201 | line_input_state->path_lookup = pathval(); |
10201 | # endif | 10202 | # endif |
10202 | reinit_unicode_for_ash(); | 10203 | reinit_unicode_for_ash(); |
10203 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ, timeout); | 10204 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ); |
10204 | if (nr == 0) { | 10205 | if (nr == 0) { |
10205 | /* ^C pressed, "convert" to SIGINT */ | 10206 | /* ^C pressed, "convert" to SIGINT */ |
10206 | write(STDOUT_FILENO, "^C", 2); | 10207 | write(STDOUT_FILENO, "^C", 2); |
diff --git a/shell/hush.c b/shell/hush.c index 93ed0bc0b..6fa4e1630 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -2411,8 +2411,7 @@ static int get_user_input(struct in_str *i) | |||
2411 | /* buglet: SIGINT will not make new prompt to appear _at once_, | 2411 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
2412 | * only after <Enter>. (^C works immediately) */ | 2412 | * only after <Enter>. (^C works immediately) */ |
2413 | r = read_line_input(G.line_input_state, prompt_str, | 2413 | r = read_line_input(G.line_input_state, prompt_str, |
2414 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, | 2414 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
2415 | /*timeout*/ -1 | ||
2416 | ); | 2415 | ); |
2417 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ | 2416 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
2418 | if (r == 0) | 2417 | if (r == 0) |
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index e00f85864..4828c0a51 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c | |||
@@ -644,7 +644,7 @@ read_line(const char *prompt) | |||
644 | { | 644 | { |
645 | int sz; | 645 | int sz; |
646 | 646 | ||
647 | sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer), /*timeout*/ -1); | 647 | sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer)); |
648 | if (sz <= 0) | 648 | if (sz <= 0) |
649 | exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */ | 649 | exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */ |
650 | 650 | ||