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>
This commit is contained in:
Denys Vlasenko 2018-04-06 14:50:12 +02:00
parent de02625985
commit cf079ffe1c

View File

@ -2123,6 +2123,27 @@ static const char* FAST_FUNC get_local_var_value(const char *name)
return NULL; 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. /* str holds "NAME=VAL" and is expected to be malloced.
* We take ownership of it. * We take ownership of it.
*/ */
@ -2264,21 +2285,7 @@ static int set_local_var(char *str, unsigned flags)
} }
free(free_me); free(free_me);
/* Handle special names */ handle_changed_special_names(cur->varstr, name_len - 1);
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
}
return 0; return 0;
} }
@ -2297,32 +2304,26 @@ static int unset_local_var_len(const char *name, int name_len)
if (!name) if (!name)
return EXIT_SUCCESS; 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; cur_pp = &G.top_var;
while ((cur = *cur_pp) != NULL) { 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) { if (cur->flg_read_only) {
bb_error_msg("%s: readonly variable", name); bb_error_msg("%s: readonly variable", name);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
*cur_pp = cur->next; *cur_pp = cur->next;
debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr);
bb_unsetenv(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) if (!cur->max_len)
free(cur->varstr); free(cur->varstr);
free(cur); free(cur);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
cur_pp = &cur->next; cur_pp = &cur->next;