diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2025-08-13 17:30:48 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2025-08-13 17:30:48 +0200 |
| commit | f29ab91763acc2a43975540971d88f962e1a5cbc (patch) | |
| tree | 2a9d3f6340d99a00ffcc31bc64d67a6f5ab486c6 /shell | |
| parent | 187f24b4907e76fa15cd4f96ac090dd92772d21b (diff) | |
| download | busybox-w32-f29ab91763acc2a43975540971d88f962e1a5cbc.tar.gz busybox-w32-f29ab91763acc2a43975540971d88f962e1a5cbc.tar.bz2 busybox-w32-f29ab91763acc2a43975540971d88f962e1a5cbc.zip | |
hush: shrink i_getch, rename two functions
Final link with: <none>
function old new delta
i_getch_interactive - 308 +308
i_getch 102 85 -17
fgetc_interactive 309 - -309
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/1 up/down: 308/-326) Total: -18 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
| -rw-r--r-- | shell/hush.c | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/shell/hush.c b/shell/hush.c index ecdb5eea9..85f49c844 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
| @@ -2743,20 +2743,16 @@ static const char *setup_prompt_string(void) | |||
| 2743 | debug_printf("prompt_str '%s'\n", prompt_str); | 2743 | debug_printf("prompt_str '%s'\n", prompt_str); |
| 2744 | return prompt_str; | 2744 | return prompt_str; |
| 2745 | } | 2745 | } |
| 2746 | static int get_interactive_input(struct in_str *i) | 2746 | static int show_prompt_and_get_stdin(struct in_str *i) |
| 2747 | { | 2747 | { |
| 2748 | # if ENABLE_FEATURE_EDITING | 2748 | # if ENABLE_FEATURE_EDITING |
| 2749 | /* In EDITING case, this function reads next input line, | 2749 | /* In EDITING case, this function reads next input line, |
| 2750 | * saves it in i->p, then returns 1st char of it. | 2750 | * saves it in i->p, then returns 1st char of it. |
| 2751 | */ | 2751 | */ |
| 2752 | int r; | ||
| 2753 | const char *prompt_str; | ||
| 2754 | |||
| 2755 | prompt_str = setup_prompt_string(); | ||
| 2756 | for (;;) { | 2752 | for (;;) { |
| 2757 | reinit_unicode_for_hush(); | 2753 | int r; |
| 2758 | G.flag_SIGINT = 0; | ||
| 2759 | 2754 | ||
| 2755 | G.flag_SIGINT = 0; | ||
| 2760 | bb_got_signal = 0; | 2756 | bb_got_signal = 0; |
| 2761 | if (!sigisemptyset(&G.pending_set)) { | 2757 | if (!sigisemptyset(&G.pending_set)) { |
| 2762 | /* Whoops, already got a signal, do not call read_line_input */ | 2758 | /* Whoops, already got a signal, do not call read_line_input */ |
| @@ -2776,6 +2772,8 @@ static int get_interactive_input(struct in_str *i) | |||
| 2776 | * #^^^ prints "T", prints prompt, repeats | 2772 | * #^^^ prints "T", prints prompt, repeats |
| 2777 | * #(bash 5.0.17 exits after first "T", looks like a bug) | 2773 | * #(bash 5.0.17 exits after first "T", looks like a bug) |
| 2778 | */ | 2774 | */ |
| 2775 | const char *prompt_str = setup_prompt_string(); | ||
| 2776 | reinit_unicode_for_hush(); | ||
| 2779 | r = read_line_input(G.line_input_state, prompt_str, | 2777 | r = read_line_input(G.line_input_state, prompt_str, |
| 2780 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 | 2778 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
| 2781 | ); | 2779 | ); |
| @@ -2806,7 +2804,7 @@ static int get_interactive_input(struct in_str *i) | |||
| 2806 | /* it was a signal: go back, read another input line */ | 2804 | /* it was a signal: go back, read another input line */ |
| 2807 | } | 2805 | } |
| 2808 | i->p = G.user_input_buf; | 2806 | i->p = G.user_input_buf; |
| 2809 | return (unsigned char)*i->p++; | 2807 | return (unsigned char)*i->p++; /* can't be NUL */ |
| 2810 | # else | 2808 | # else |
| 2811 | /* In !EDITING case, this function gets called for every char. | 2809 | /* In !EDITING case, this function gets called for every char. |
| 2812 | * Buffering happens deeper in the call chain, in hfgetc(i->file). | 2810 | * Buffering happens deeper in the call chain, in hfgetc(i->file). |
| @@ -2847,13 +2845,13 @@ static int get_interactive_input(struct in_str *i) | |||
| 2847 | } | 2845 | } |
| 2848 | /* This is the magic location that prints prompts | 2846 | /* This is the magic location that prints prompts |
| 2849 | * and gets data back from the user */ | 2847 | * and gets data back from the user */ |
| 2850 | static int fgetc_interactive(struct in_str *i) | 2848 | static int i_getch_interactive(struct in_str *i) |
| 2851 | { | 2849 | { |
| 2852 | int ch; | 2850 | int ch; |
| 2853 | /* If it's interactive stdin, get new line. */ | 2851 | /* If it's interactive stdin, get new line. */ |
| 2854 | if (G_interactive_fd && i->file == G.HFILE_stdin) { | 2852 | if (G_interactive_fd && i->file == G.HFILE_stdin) { |
| 2855 | /* Returns first char (or EOF), the rest is in i->p[] */ | 2853 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2856 | ch = get_interactive_input(i); | 2854 | ch = show_prompt_and_get_stdin(i); |
| 2857 | G.promptmode = 1; /* PS2 */ | 2855 | G.promptmode = 1; /* PS2 */ |
| 2858 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); | 2856 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 2859 | } else { | 2857 | } else { |
| @@ -2863,7 +2861,7 @@ static int fgetc_interactive(struct in_str *i) | |||
| 2863 | return ch; | 2861 | return ch; |
| 2864 | } | 2862 | } |
| 2865 | #else /* !INTERACTIVE */ | 2863 | #else /* !INTERACTIVE */ |
| 2866 | static ALWAYS_INLINE int fgetc_interactive(struct in_str *i) | 2864 | static ALWAYS_INLINE int i_getch_interactive(struct in_str *i) |
| 2867 | { | 2865 | { |
| 2868 | int ch; | 2866 | int ch; |
| 2869 | do ch = hfgetc(i->file); while (ch == '\0'); | 2867 | do ch = hfgetc(i->file); while (ch == '\0'); |
| @@ -2875,26 +2873,24 @@ static int i_getch(struct in_str *i) | |||
| 2875 | { | 2873 | { |
| 2876 | int ch; | 2874 | int ch; |
| 2877 | 2875 | ||
| 2878 | if (!i->file) { | 2876 | if (i->p) { |
| 2879 | /* string-based in_str */ | 2877 | /* string-based in_str, or line editing buffer */ |
| 2880 | ch = (unsigned char)*i->p; | 2878 | ch = (unsigned char)*i->p; |
| 2881 | if (ch != '\0') { | 2879 | if (ch != '\0') { |
| 2882 | i->p++; | 2880 | i->p++; |
| 2883 | goto out1; | 2881 | goto out; |
| 2882 | } | ||
| 2883 | /* If string-based in_str, end-of-string is EOF */ | ||
| 2884 | if (!i->file) { | ||
| 2885 | debug_printf("i_getch: got EOF from string\n"); | ||
| 2886 | return EOF; | ||
| 2884 | } | 2887 | } |
| 2885 | return EOF; | ||
| 2886 | } | 2888 | } |
| 2887 | 2889 | ||
| 2888 | /* FILE-based in_str */ | 2890 | /* FILE-based in_str */ |
| 2889 | 2891 | ||
| 2890 | #if ENABLE_FEATURE_EDITING | 2892 | /* Use what i_peek / i_peek2 saved (if anything) */ |
| 2891 | /* This can be stdin, check line editing char[] buffer */ | 2893 | /* peek_buf[] is an int array, not char - can contain EOF */ |
| 2892 | if (i->p && *i->p != '\0') { | ||
| 2893 | ch = (unsigned char)*i->p++; | ||
| 2894 | goto out; | ||
| 2895 | } | ||
| 2896 | #endif | ||
| 2897 | /* peek_buf[] is an int array, not char. Can contain EOF. */ | ||
| 2898 | ch = i->peek_buf[0]; | 2894 | ch = i->peek_buf[0]; |
| 2899 | if (ch != 0) { | 2895 | if (ch != 0) { |
| 2900 | int ch2 = i->peek_buf[1]; | 2896 | int ch2 = i->peek_buf[1]; |
| @@ -2905,10 +2901,9 @@ static int i_getch(struct in_str *i) | |||
| 2905 | goto out; | 2901 | goto out; |
| 2906 | } | 2902 | } |
| 2907 | 2903 | ||
| 2908 | ch = fgetc_interactive(i); | 2904 | ch = i_getch_interactive(i); |
| 2909 | out: | 2905 | out: |
| 2910 | debug_printf("file_get: got '%c' %d\n", ch, ch); | 2906 | debug_printf("i_getch: got '%c' %d\n", ch, ch); |
| 2911 | out1: | ||
| 2912 | #if ENABLE_HUSH_LINENO_VAR | 2907 | #if ENABLE_HUSH_LINENO_VAR |
| 2913 | if (ch == '\n') { | 2908 | if (ch == '\n') { |
| 2914 | G.parse_lineno++; | 2909 | G.parse_lineno++; |
| @@ -2942,7 +2937,7 @@ static int i_peek(struct in_str *i) | |||
| 2942 | return ch; | 2937 | return ch; |
| 2943 | 2938 | ||
| 2944 | /* Need to get a new char */ | 2939 | /* Need to get a new char */ |
| 2945 | ch = fgetc_interactive(i); | 2940 | ch = i_getch_interactive(i); |
| 2946 | debug_printf("file_peek: got '%c' %d\n", ch, ch); | 2941 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2947 | 2942 | ||
| 2948 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ | 2943 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| @@ -8229,7 +8224,7 @@ static void restore_redirects(struct squirrel *sq) | |||
| 8229 | * Redirect moves ->fd to e.g. 10, | 8224 | * Redirect moves ->fd to e.g. 10, |
| 8230 | * and it is not restored above (we do not restore script fds | 8225 | * and it is not restored above (we do not restore script fds |
| 8231 | * after redirects, we just use new, "moved" fds). | 8226 | * after redirects, we just use new, "moved" fds). |
| 8232 | * However for stdin, get_interactive_input() -> read_line_input(), | 8227 | * However for stdin, show_prompt_and_get_stdin() -> read_line_input(), |
| 8233 | * and read builtin, depend on fd == STDIN_FILENO. | 8228 | * and read builtin, depend on fd == STDIN_FILENO. |
| 8234 | */ | 8229 | */ |
| 8235 | debug_printf_redir("restoring %d to stdin\n", G.HFILE_stdin->fd); | 8230 | debug_printf_redir("restoring %d to stdin\n", G.HFILE_stdin->fd); |
