hush: make expand_vars_to_list() a bit more sane

function                                             old     new   delta
append_str_maybe_ifs_split                             -      64     +64
expand_vars_to_list                                 1167    1139     -28
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 64/-28)             Total: 36 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-07-19 11:16:53 +02:00
parent e36a5894bd
commit 116b50a5c1

View File

@ -5955,6 +5955,24 @@ static char *replace_pattern(char *val, const char *pattern, const char *repl, c
} }
#endif /* BASH_PATTERN_SUBST */ #endif /* BASH_PATTERN_SUBST */
static int append_str_maybe_ifs_split(o_string *output, int *ended_in_ifs, int n,
int first_ch, const char *val)
{
if (!(first_ch & 0x80)) { /* unquoted $VAR */
debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val,
!!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
if (val && val[0])
n = expand_on_ifs(ended_in_ifs, output, n, val);
} else { /* quoted "$VAR" */
output->has_quoted_part = 1;
debug_printf_expand("quoted '%s', output->o_escape:%d\n", val,
!!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
if (val && val[0])
o_addQstr(output, val);
}
return n;
}
/* Helper: /* Helper:
* Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct.
*/ */
@ -6290,11 +6308,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
char first_ch; char first_ch;
char *to_be_freed = NULL;
const char *val = NULL; const char *val = NULL;
#if ENABLE_HUSH_TICK
o_string subst_result = NULL_O_STRING;
#endif
#if ENABLE_FEATURE_SH_MATH #if ENABLE_FEATURE_SH_MATH
char arith_buf[sizeof(arith_t)*3 + 2]; char arith_buf[sizeof(arith_t)*3 + 2];
#endif #endif
@ -6382,7 +6396,9 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
val = SPECIAL_VAR_SYMBOL_STR; val = SPECIAL_VAR_SYMBOL_STR;
break; break;
#if ENABLE_HUSH_TICK #if ENABLE_HUSH_TICK
case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
o_string subst_result = NULL_O_STRING;
*p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */
arg++; arg++;
/* Can't just stuff it into output o_string, /* Can't just stuff it into output o_string,
@ -6392,8 +6408,10 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
G.last_exitcode = process_command_subs(&subst_result, arg); G.last_exitcode = process_command_subs(&subst_result, arg);
G.expand_exitcode = G.last_exitcode; G.expand_exitcode = G.last_exitcode;
debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data);
val = subst_result.data; n = append_str_maybe_ifs_split(output, &ended_in_ifs, n, first_ch, subst_result.data);
goto store_val; o_free_unsafe(&subst_result);
goto restore;
}
#endif #endif
#if ENABLE_FEATURE_SH_MATH #if ENABLE_FEATURE_SH_MATH
case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */
@ -6409,37 +6427,23 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
break; break;
} }
#endif #endif
default: default: {
char *to_be_freed;
val = expand_one_var(&to_be_freed, arg, &p); val = expand_one_var(&to_be_freed, arg, &p);
IF_HUSH_TICK(store_val:) n = append_str_maybe_ifs_split(output, &ended_in_ifs, n, first_ch, val);
if (!(first_ch & 0x80)) { /* unquoted $VAR */ free(to_be_freed);
debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, goto restore;
!!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); } /* default: */
if (val && val[0]) {
n = expand_on_ifs(&ended_in_ifs, output, n, val);
val = NULL;
}
} else { /* quoted $VAR, val will be appended below */
output->has_quoted_part = 1;
debug_printf_expand("quoted '%s', output->o_escape:%d\n", val,
!!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
}
break;
} /* switch (char after <SPECIAL_VAR_SYMBOL>) */ } /* switch (char after <SPECIAL_VAR_SYMBOL>) */
if (val && val[0]) { if (val && val[0]) {
o_addQstr(output, val); o_addQstr(output, val);
} }
free(to_be_freed); restore:
/* Restore NULL'ed SPECIAL_VAR_SYMBOL. /* Restore NULL'ed SPECIAL_VAR_SYMBOL.
* Do the check to avoid writing to a const string. */ * Do the check to avoid writing to a const string. */
if (*p != SPECIAL_VAR_SYMBOL) if (*p != SPECIAL_VAR_SYMBOL)
*p = SPECIAL_VAR_SYMBOL; *p = SPECIAL_VAR_SYMBOL;
#if ENABLE_HUSH_TICK
o_free(&subst_result);
#endif
arg = ++p; arg = ++p;
} /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */