hush: better function and parameter names

Also, trim code which isn't needed for non-bash compat.
No logic changes.

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
Denys Vlasenko 2010-09-10 10:44:23 +02:00
parent cc461736d6
commit d98e5c65c3

View File

@ -3733,16 +3733,34 @@ static int parse_dollar(o_string *as_string,
} }
#if BB_MMU #if BB_MMU
#define parse_stream_dquoted(as_string, dest, input, dquote_end, dquoted) \ # if ENABLE_HUSH_BASH_COMPAT
parse_stream_dquoted(dest, input, dquote_end, dquoted) #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
encode_string(dest, input, dquote_end, process_bkslash)
# else
/* only ${var/pattern/repl} (its pattern part) needs additional mode */
#define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
encode_string(dest, input, dquote_end)
# endif
#define as_string NULL #define as_string NULL
#else /* !MMU */
# if ENABLE_HUSH_BASH_COMPAT
/* all parameters are needed, no macro tricks */
# else
#define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
encode_string(as_string, dest, input, dquote_end)
# endif
#endif #endif
static int parse_stream_dquoted(o_string *as_string, static int encode_string(o_string *as_string,
o_string *dest, o_string *dest,
struct in_str *input, struct in_str *input,
int dquote_end, int dquote_end,
int dquoted) int process_bkslash)
{ {
#if !ENABLE_HUSH_BASH_COMPAT
const int process_bkslash = 1;
#endif
int ch; int ch;
int next; int next;
@ -3751,7 +3769,7 @@ static int parse_stream_dquoted(o_string *as_string,
if (ch != EOF) if (ch != EOF)
nommu_addchr(as_string, ch); nommu_addchr(as_string, ch);
if (ch == dquote_end) { /* may be only '"' or EOF */ if (ch == dquote_end) { /* may be only '"' or EOF */
debug_printf_parse("parse_stream_dquoted return 0\n"); debug_printf_parse("encode_string return 0\n");
return 0; return 0;
} }
/* note: can't move it above ch == dquote_end check! */ /* note: can't move it above ch == dquote_end check! */
@ -3765,7 +3783,7 @@ static int parse_stream_dquoted(o_string *as_string,
} }
debug_printf_parse("\" ch=%c (%d) escape=%d\n", debug_printf_parse("\" ch=%c (%d) escape=%d\n",
ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
if (dquoted && ch == '\\') { if (process_bkslash && ch == '\\') {
if (next == EOF) { if (next == EOF) {
syntax_error("\\<eof>"); syntax_error("\\<eof>");
xfunc_die(); xfunc_die();
@ -3775,21 +3793,21 @@ static int parse_stream_dquoted(o_string *as_string,
* only when followed by one of the following characters: * only when followed by one of the following characters:
* $, `, ", \, or <newline>. A double quote may be quoted * $, `, ", \, or <newline>. A double quote may be quoted
* within double quotes by preceding it with a backslash." * within double quotes by preceding it with a backslash."
* NB: in (unquoted) heredoc, above does not apply to ". * NB: in (unquoted) heredoc, above does not apply to ",
* therefore we check for it by "next == dquote_end" cond.
*/ */
if (next == dquote_end || strchr("$`\\\n", next) != NULL) { if (next == dquote_end || strchr("$`\\\n", next)) {
ch = i_getch(input); /* eat next */ ch = i_getch(input); /* eat next */
if (ch == '\n') if (ch == '\n')
goto again; /* skip \<newline> */ goto again; /* skip \<newline> */
} /* else: ch remains == '\\', and we double it */ } /* else: ch remains == '\\', and we double it */
o_addqchr(dest, ch); o_addqchr(dest, ch); /* \c if c is s glob char, else just c */
nommu_addchr(as_string, ch); nommu_addchr(as_string, ch);
goto again; goto again;
} }
if (ch == '$') { if (ch == '$') {
//CHECK: 0x80? or dquoted?
if (parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80) != 0) { if (parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80) != 0) {
debug_printf_parse("parse_stream_dquoted return 1: " debug_printf_parse("encode_string return 1: "
"parse_dollar returned non-0\n"); "parse_dollar returned non-0\n");
return 1; return 1;
} }
@ -4162,7 +4180,7 @@ static struct pipe *parse_stream(char **pstring,
dest.has_quoted_part = 1; dest.has_quoted_part = 1;
if (dest.o_assignment == NOT_ASSIGNMENT) if (dest.o_assignment == NOT_ASSIGNMENT)
dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS;
if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"', /*dquoted:*/ 1)) if (encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1))
goto parse_error; goto parse_error;
dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS;
break; break;
@ -4335,6 +4353,11 @@ static struct pipe *parse_stream(char **pstring,
/*** Execution routines ***/ /*** Execution routines ***/
/* Expansion can recurse, need forward decls: */ /* Expansion can recurse, need forward decls: */
#if !ENABLE_HUSH_BASH_COMPAT
/* only ${var/pattern/repl} (its pattern part) needs additional mode */
#define expand_string_to_string(str, do_unbackslash) \
expand_string_to_string(str)
#endif
static char *expand_string_to_string(const char *str, int do_unbackslash); static char *expand_string_to_string(const char *str, int do_unbackslash);
static int process_command_subs(o_string *dest, const char *s); static int process_command_subs(o_string *dest, const char *s);
@ -4387,8 +4410,17 @@ static int expand_on_ifs(o_string *output, int n, const char *str)
* Returns malloced string. * Returns malloced string.
* As an optimization, we return NULL if expansion is not needed. * As an optimization, we return NULL if expansion is not needed.
*/ */
static char *expand_pseudo_dquoted(const char *str, int dquoted, int do_unbackslash) #if !ENABLE_HUSH_BASH_COMPAT
/* only ${var/pattern/repl} (its pattern part) needs additional mode */
#define encode_then_expand_string(str, process_bkslash, do_unbackslash) \
encode_then_expand_string(str)
#endif
static char *encode_then_expand_string(const char *str, int process_bkslash, int do_unbackslash)
{ {
//#if !ENABLE_HUSH_BASH_COMPAT
// const int process_bkslash = 1;
// const int do_unbackslash = 1;
//#endif
char *exp_str; char *exp_str;
struct in_str input; struct in_str input;
o_string dest = NULL_O_STRING; o_string dest = NULL_O_STRING;
@ -4406,7 +4438,7 @@ static char *expand_pseudo_dquoted(const char *str, int dquoted, int do_unbacksl
* echo $(($a + `echo 1`)) $((1 + $((2)) )) * echo $(($a + `echo 1`)) $((1 + $((2)) ))
*/ */
setup_string_in_str(&input, str); setup_string_in_str(&input, str);
parse_stream_dquoted(NULL, &dest, &input, EOF, dquoted); encode_string(NULL, &dest, &input, EOF, process_bkslash);
//bb_error_msg("'%s' -> '%s'", str, dest.data); //bb_error_msg("'%s' -> '%s'", str, dest.data);
exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash);
//bb_error_msg("'%s' -> '%s'", dest.data, exp_str); //bb_error_msg("'%s' -> '%s'", dest.data, exp_str);
@ -4424,7 +4456,7 @@ static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p)
hooks.lookupvar = get_local_var_value; hooks.lookupvar = get_local_var_value;
hooks.setvar = set_local_var_from_halves; hooks.setvar = set_local_var_from_halves;
//hooks.endofname = endofname; //hooks.endofname = endofname;
exp_str = expand_pseudo_dquoted(arg, /*dquoted:*/ 1, /*unbackslash:*/ 1); exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); res = arith(exp_str ? exp_str : arg, errcode_p, &hooks);
free(exp_str); free(exp_str);
return res; return res;
@ -4594,7 +4626,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
//TODO: avoid xstrdup unless needed //TODO: avoid xstrdup unless needed
// (see HACK ALERT below for an example) // (see HACK ALERT below for an example)
val = to_be_freed = xstrdup(val); val = to_be_freed = xstrdup(val);
exp_exp_word = expand_pseudo_dquoted(exp_word, /*dquoted:*/ 1, /*unbackslash:*/ 1); exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
if (exp_exp_word) if (exp_exp_word)
exp_word = exp_exp_word; exp_word = exp_exp_word;
loc = scan_and_match(to_be_freed, exp_word, scan_flags); loc = scan_and_match(to_be_freed, exp_word, scan_flags);
@ -4631,7 +4663,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
* (note that a*z _pattern_ is never globbed!) * (note that a*z _pattern_ is never globbed!)
*/ */
char *pattern, *repl, *t; char *pattern, *repl, *t;
pattern = expand_pseudo_dquoted(exp_word, /*dquoted:*/ 0, /*unbackslash:*/ 0); pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0);
if (!pattern) if (!pattern)
pattern = xstrdup(exp_word); pattern = xstrdup(exp_word);
debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern);
@ -4639,7 +4671,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
exp_word = p; exp_word = p;
p = strchr(p, SPECIAL_VAR_SYMBOL); p = strchr(p, SPECIAL_VAR_SYMBOL);
*p = '\0'; *p = '\0';
repl = expand_pseudo_dquoted(exp_word, /*dquoted:*/ arg0 & 0x80, /*unbackslash:*/ 1); repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ arg0 & 0x80, /*unbackslash:*/ 1);
debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl);
/* HACK ALERT. We depend here on the fact that /* HACK ALERT. We depend here on the fact that
* G.global_argv and results of utoa and get_local_var_value * G.global_argv and results of utoa and get_local_var_value
@ -4724,7 +4756,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op,
(exp_save == ':') ? "true" : "false", use_word); (exp_save == ':') ? "true" : "false", use_word);
if (use_word) { if (use_word) {
to_be_freed = expand_pseudo_dquoted(exp_word, /*dquoted:*/ 1, /*unbackslash:*/ 1); to_be_freed = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
if (to_be_freed) if (to_be_freed)
exp_word = to_be_freed; exp_word = to_be_freed;
if (exp_op == '?') { if (exp_op == '?') {
@ -4998,6 +5030,9 @@ static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
*/ */
static char *expand_string_to_string(const char *str, int do_unbackslash) static char *expand_string_to_string(const char *str, int do_unbackslash)
{ {
#if !ENABLE_HUSH_BASH_COMPAT
const int do_unbackslash = 1;
#endif
char *argv[2], **list; char *argv[2], **list;
debug_printf_expand("string_to_string<='%s'\n", str); debug_printf_expand("string_to_string<='%s'\n", str);
@ -5468,7 +5503,7 @@ static void setup_heredoc(struct redir_struct *redir)
expanded = NULL; expanded = NULL;
if (!(redir->rd_dup & HEREDOC_QUOTED)) { if (!(redir->rd_dup & HEREDOC_QUOTED)) {
expanded = expand_pseudo_dquoted(heredoc, /*dquoted:*/ 1, /*unbackslash:*/ 1); expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
if (expanded) if (expanded)
heredoc = expanded; heredoc = expanded;
} }