diff options
author | Denys Vlasenko | 2018-04-06 14:50:12 +0200 |
---|---|---|
committer | Denys Vlasenko | 2018-04-06 14:50:12 +0200 |
commit | cf079ffe1c26c2d42a902a60696b20a262e92d87 (patch) | |
tree | 13ab2e6f6c2b4514559e0475415366aaaa8e024c | |
parent | de0262598504f55251e3763e73f259074795de5d (diff) | |
download | busybox-cf079ffe1c26c2d42a902a60696b20a262e92d87.zip busybox-cf079ffe1c26c2d42a902a60696b20a262e92d87.tar.gz |
hush: consolidate handling of setting/unsetting of PSn, LINENO, OPTIND
function old new delta
handle_changed_special_names - 99 +99
unset_local_var 256 155 -101
set_local_var 557 437 -120
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 99/-221) Total: -122 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/hush.c | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/shell/hush.c b/shell/hush.c index 9ea3e3f..7b59b0f 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -2123,6 +2123,27 @@ static const char* FAST_FUNC get_local_var_value(const char *name) return NULL; } +static void handle_changed_special_names(const char *name, unsigned name_len) +{ + if (name_len == 3 && name[0] == 'P' && name[1] == 'S') { + cmdedit_update_prompt(); + return; + } + + if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) + && name_len == 6 + ) { +#if ENABLE_HUSH_LINENO_VAR + if (strncmp(name, "LINENO", 6) == 0) + G.lineno_var = NULL; +#endif +#if ENABLE_HUSH_GETOPTS + if (strncmp(name, "OPTIND", 6) == 0) + G.getopt_count = 0; +#endif + } +} + /* str holds "NAME=VAL" and is expected to be malloced. * We take ownership of it. */ @@ -2264,21 +2285,7 @@ static int set_local_var(char *str, unsigned flags) } free(free_me); - /* Handle special names */ - if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') - cmdedit_update_prompt(); - else - if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) && name_len == 7) { -#if ENABLE_HUSH_LINENO_VAR - if (G.lineno_var && strncmp(cur->varstr, "LINENO", 6) == 0) - G.lineno_var = NULL; -#endif -#if ENABLE_HUSH_GETOPTS - /* defoptindvar is a "OPTIND=..." constant string */ - if (strncmp(cur->varstr, defoptindvar, 7) == 0) - G.getopt_count = 0; -#endif - } + handle_changed_special_names(cur->varstr, name_len - 1); return 0; } @@ -2297,32 +2304,26 @@ static int unset_local_var_len(const char *name, int name_len) if (!name) return EXIT_SUCCESS; - if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) && name_len == 6) { -#if ENABLE_HUSH_GETOPTS - if (strncmp(name, "OPTIND", 6) == 0) - G.getopt_count = 0; -#endif -#if ENABLE_HUSH_LINENO_VAR - if (G.lineno_var && strncmp(name, "LINENO", 6) == 0) - G.lineno_var = NULL; -#endif - } - cur_pp = &G.top_var; while ((cur = *cur_pp) != NULL) { - if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { + if (strncmp(cur->varstr, name, name_len) == 0 + && cur->varstr[name_len] == '=' + ) { if (cur->flg_read_only) { bb_error_msg("%s: readonly variable", name); return EXIT_FAILURE; } + *cur_pp = cur->next; debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); bb_unsetenv(cur->varstr); - if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') - cmdedit_update_prompt(); + + handle_changed_special_names(name, name_len); + if (!cur->max_len) free(cur->varstr); free(cur); + return EXIT_SUCCESS; } cur_pp = &cur->next; |