diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-06 17:58:21 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-06 17:58:21 +0200 |
commit | f5018dac21df54647d0982ed4bebd0286d77cc56 (patch) | |
tree | 1ee25cd13897d36d192281f7f4d990529c98410a /shell/hush.c | |
parent | d1d6d9c5d8022bcd8f2e7fbd470d293f73adae58 (diff) | |
download | busybox-w32-f5018dac21df54647d0982ed4bebd0286d77cc56.tar.gz busybox-w32-f5018dac21df54647d0982ed4bebd0286d77cc56.tar.bz2 busybox-w32-f5018dac21df54647d0982ed4bebd0286d77cc56.zip |
hush: fix "unset PS1/PS2", and put them into initial variable set
"unset PS1/PS2" causes prompts to be empty strings
function old new delta
hush_main 1031 1089 +58
goto_new_line 27 33 +6
fgetc_interactive 244 245 +1
unset_local_var 155 149 -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/1 up/down: 65/-6) Total: 59 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to '')
-rw-r--r-- | shell/hush.c | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/shell/hush.c b/shell/hush.c index 971a16aff..34d905397 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -828,7 +828,7 @@ struct globals { | |||
828 | * _AND_ if we decided to act interactively */ | 828 | * _AND_ if we decided to act interactively */ |
829 | int interactive_fd; | 829 | int interactive_fd; |
830 | const char *PS1; | 830 | const char *PS1; |
831 | const char *PS2; | 831 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
832 | # define G_interactive_fd (G.interactive_fd) | 832 | # define G_interactive_fd (G.interactive_fd) |
833 | #else | 833 | #else |
834 | # define G_interactive_fd 0 | 834 | # define G_interactive_fd 0 |
@@ -1394,7 +1394,7 @@ static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) | |||
1394 | #endif | 1394 | #endif |
1395 | 1395 | ||
1396 | 1396 | ||
1397 | #if ENABLE_HUSH_INTERACTIVE | 1397 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
1398 | static void cmdedit_update_prompt(void); | 1398 | static void cmdedit_update_prompt(void); |
1399 | #else | 1399 | #else |
1400 | # define cmdedit_update_prompt() ((void)0) | 1400 | # define cmdedit_update_prompt() ((void)0) |
@@ -2125,7 +2125,9 @@ static const char* FAST_FUNC get_local_var_value(const char *name) | |||
2125 | 2125 | ||
2126 | static void handle_changed_special_names(const char *name, unsigned name_len) | 2126 | static void handle_changed_special_names(const char *name, unsigned name_len) |
2127 | { | 2127 | { |
2128 | if (name_len == 3 && name[0] == 'P' && name[1] == 'S') { | 2128 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
2129 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' | ||
2130 | ) { | ||
2129 | cmdedit_update_prompt(); | 2131 | cmdedit_update_prompt(); |
2130 | return; | 2132 | return; |
2131 | } | 2133 | } |
@@ -2305,9 +2307,6 @@ static int unset_local_var_len(const char *name, int name_len) | |||
2305 | struct variable *cur; | 2307 | struct variable *cur; |
2306 | struct variable **cur_pp; | 2308 | struct variable **cur_pp; |
2307 | 2309 | ||
2308 | if (!name) | ||
2309 | return EXIT_SUCCESS; | ||
2310 | |||
2311 | cur_pp = &G.top_var; | 2310 | cur_pp = &G.top_var; |
2312 | while ((cur = *cur_pp) != NULL) { | 2311 | while ((cur = *cur_pp) != NULL) { |
2313 | if (strncmp(cur->varstr, name, name_len) == 0 | 2312 | if (strncmp(cur->varstr, name, name_len) == 0 |
@@ -2321,17 +2320,18 @@ static int unset_local_var_len(const char *name, int name_len) | |||
2321 | *cur_pp = cur->next; | 2320 | *cur_pp = cur->next; |
2322 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); | 2321 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
2323 | bb_unsetenv(cur->varstr); | 2322 | bb_unsetenv(cur->varstr); |
2324 | |||
2325 | handle_changed_special_names(name, name_len); | ||
2326 | |||
2327 | if (!cur->max_len) | 2323 | if (!cur->max_len) |
2328 | free(cur->varstr); | 2324 | free(cur->varstr); |
2329 | free(cur); | 2325 | free(cur); |
2330 | 2326 | ||
2331 | return EXIT_SUCCESS; | 2327 | break; |
2332 | } | 2328 | } |
2333 | cur_pp = &cur->next; | 2329 | cur_pp = &cur->next; |
2334 | } | 2330 | } |
2331 | |||
2332 | /* Handle "unset PS1" et al even if did not find the variable to unset */ | ||
2333 | handle_changed_special_names(name, name_len); | ||
2334 | |||
2335 | return EXIT_SUCCESS; | 2335 | return EXIT_SUCCESS; |
2336 | } | 2336 | } |
2337 | 2337 | ||
@@ -2459,36 +2459,36 @@ static void reinit_unicode_for_hush(void) | |||
2459 | * \ | 2459 | * \ |
2460 | * It exercises a lot of corner cases. | 2460 | * It exercises a lot of corner cases. |
2461 | */ | 2461 | */ |
2462 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT | ||
2462 | static void cmdedit_update_prompt(void) | 2463 | static void cmdedit_update_prompt(void) |
2463 | { | 2464 | { |
2464 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { | 2465 | G.PS1 = get_local_var_value("PS1"); |
2465 | G.PS1 = get_local_var_value("PS1"); | 2466 | if (G.PS1 == NULL) |
2466 | if (G.PS1 == NULL) | 2467 | G.PS1 = ""; |
2467 | G.PS1 = "\\w \\$ "; | 2468 | G.PS2 = get_local_var_value("PS2"); |
2468 | G.PS2 = get_local_var_value("PS2"); | ||
2469 | } else { | ||
2470 | G.PS1 = NULL; | ||
2471 | } | ||
2472 | if (G.PS2 == NULL) | 2469 | if (G.PS2 == NULL) |
2473 | G.PS2 = "> "; | 2470 | G.PS2 = ""; |
2474 | } | 2471 | } |
2472 | # endif | ||
2475 | static const char *setup_prompt_string(int promptmode) | 2473 | static const char *setup_prompt_string(int promptmode) |
2476 | { | 2474 | { |
2477 | const char *prompt_str; | 2475 | const char *prompt_str; |
2476 | |||
2478 | debug_printf("setup_prompt_string %d ", promptmode); | 2477 | debug_printf("setup_prompt_string %d ", promptmode); |
2479 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { | 2478 | |
2480 | /* Set up the prompt */ | 2479 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
2481 | if (promptmode == 0) { /* PS1 */ | 2480 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
2481 | if (promptmode == 0) { /* PS1 */ | ||
2482 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { | ||
2483 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ | ||
2482 | free((char*)G.PS1); | 2484 | free((char*)G.PS1); |
2483 | /* bash uses $PWD value, even if it is set by user. | 2485 | /* bash uses $PWD value, even if it is set by user. |
2484 | * It uses current dir only if PWD is unset. | 2486 | * It uses current dir only if PWD is unset. |
2485 | * We always use current dir. */ | 2487 | * We always use current dir. */ |
2486 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); | 2488 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
2487 | prompt_str = G.PS1; | 2489 | } |
2488 | } else | 2490 | prompt_str = G.PS1; |
2489 | prompt_str = G.PS2; | 2491 | } |
2490 | } else | ||
2491 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; | ||
2492 | debug_printf("prompt_str '%s'\n", prompt_str); | 2492 | debug_printf("prompt_str '%s'\n", prompt_str); |
2493 | return prompt_str; | 2493 | return prompt_str; |
2494 | } | 2494 | } |
@@ -9207,6 +9207,14 @@ int hush_main(int argc, char **argv) | |||
9207 | /* Export PWD */ | 9207 | /* Export PWD */ |
9208 | set_pwd_var(SETFLAG_EXPORT); | 9208 | set_pwd_var(SETFLAG_EXPORT); |
9209 | 9209 | ||
9210 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT | ||
9211 | /* Set (but not export) PS1/2 unless already set */ | ||
9212 | if (!get_local_var_value("PS1")) | ||
9213 | set_local_var_from_halves("PS1", "\\w \\$ "); | ||
9214 | if (!get_local_var_value("PS2")) | ||
9215 | set_local_var_from_halves("PS2", "> "); | ||
9216 | #endif | ||
9217 | |||
9210 | #if BASH_HOSTNAME_VAR | 9218 | #if BASH_HOSTNAME_VAR |
9211 | /* Set (but not export) HOSTNAME unless already set */ | 9219 | /* Set (but not export) HOSTNAME unless already set */ |
9212 | if (!get_local_var_value("HOSTNAME")) { | 9220 | if (!get_local_var_value("HOSTNAME")) { |
@@ -9245,8 +9253,6 @@ int hush_main(int argc, char **argv) | |||
9245 | * OPTERR=1 | 9253 | * OPTERR=1 |
9246 | * OPTIND=1 | 9254 | * OPTIND=1 |
9247 | * IFS=$' \t\n' | 9255 | * IFS=$' \t\n' |
9248 | * PS1='\s-\v\$ ' | ||
9249 | * PS2='> ' | ||
9250 | * PS4='+ ' | 9256 | * PS4='+ ' |
9251 | */ | 9257 | */ |
9252 | #endif | 9258 | #endif |