aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <dvlasenk@redhat.com>2010-09-10 10:44:23 +0200
committerDenys Vlasenko <dvlasenk@redhat.com>2010-09-10 10:44:23 +0200
commitd98e5c65c3c69373dab56a7f9af8780c1a8123b2 (patch)
treeeec1243892ce01df02a41934d4beec0674409c62
parentcc461736d6ca3c3b809390d8d7873a136e3b899a (diff)
downloadbusybox-w32-d98e5c65c3c69373dab56a7f9af8780c1a8123b2.tar.gz
busybox-w32-d98e5c65c3c69373dab56a7f9af8780c1a8123b2.tar.bz2
busybox-w32-d98e5c65c3c69373dab56a7f9af8780c1a8123b2.zip
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>
-rw-r--r--shell/hush.c75
1 files changed, 55 insertions, 20 deletions
diff --git a/shell/hush.c b/shell/hush.c
index f49a39b0f..920d9638c 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -3733,16 +3733,34 @@ static int parse_dollar(o_string *as_string,
3733} 3733}
3734 3734
3735#if BB_MMU 3735#if BB_MMU
3736#define parse_stream_dquoted(as_string, dest, input, dquote_end, dquoted) \ 3736# if ENABLE_HUSH_BASH_COMPAT
3737 parse_stream_dquoted(dest, input, dquote_end, dquoted) 3737#define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
3738 encode_string(dest, input, dquote_end, process_bkslash)
3739# else
3740/* only ${var/pattern/repl} (its pattern part) needs additional mode */
3741#define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
3742 encode_string(dest, input, dquote_end)
3743# endif
3738#define as_string NULL 3744#define as_string NULL
3745
3746#else /* !MMU */
3747
3748# if ENABLE_HUSH_BASH_COMPAT
3749/* all parameters are needed, no macro tricks */
3750# else
3751#define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
3752 encode_string(as_string, dest, input, dquote_end)
3753# endif
3739#endif 3754#endif
3740static int parse_stream_dquoted(o_string *as_string, 3755static int encode_string(o_string *as_string,
3741 o_string *dest, 3756 o_string *dest,
3742 struct in_str *input, 3757 struct in_str *input,
3743 int dquote_end, 3758 int dquote_end,
3744 int dquoted) 3759 int process_bkslash)
3745{ 3760{
3761#if !ENABLE_HUSH_BASH_COMPAT
3762 const int process_bkslash = 1;
3763#endif
3746 int ch; 3764 int ch;
3747 int next; 3765 int next;
3748 3766
@@ -3751,7 +3769,7 @@ static int parse_stream_dquoted(o_string *as_string,
3751 if (ch != EOF) 3769 if (ch != EOF)
3752 nommu_addchr(as_string, ch); 3770 nommu_addchr(as_string, ch);
3753 if (ch == dquote_end) { /* may be only '"' or EOF */ 3771 if (ch == dquote_end) { /* may be only '"' or EOF */
3754 debug_printf_parse("parse_stream_dquoted return 0\n"); 3772 debug_printf_parse("encode_string return 0\n");
3755 return 0; 3773 return 0;
3756 } 3774 }
3757 /* note: can't move it above ch == dquote_end check! */ 3775 /* note: can't move it above ch == dquote_end check! */
@@ -3765,7 +3783,7 @@ static int parse_stream_dquoted(o_string *as_string,
3765 } 3783 }
3766 debug_printf_parse("\" ch=%c (%d) escape=%d\n", 3784 debug_printf_parse("\" ch=%c (%d) escape=%d\n",
3767 ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); 3785 ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
3768 if (dquoted && ch == '\\') { 3786 if (process_bkslash && ch == '\\') {
3769 if (next == EOF) { 3787 if (next == EOF) {
3770 syntax_error("\\<eof>"); 3788 syntax_error("\\<eof>");
3771 xfunc_die(); 3789 xfunc_die();
@@ -3775,21 +3793,21 @@ static int parse_stream_dquoted(o_string *as_string,
3775 * only when followed by one of the following characters: 3793 * only when followed by one of the following characters:
3776 * $, `, ", \, or <newline>. A double quote may be quoted 3794 * $, `, ", \, or <newline>. A double quote may be quoted
3777 * within double quotes by preceding it with a backslash." 3795 * within double quotes by preceding it with a backslash."
3778 * NB: in (unquoted) heredoc, above does not apply to ". 3796 * NB: in (unquoted) heredoc, above does not apply to ",
3797 * therefore we check for it by "next == dquote_end" cond.
3779 */ 3798 */
3780 if (next == dquote_end || strchr("$`\\\n", next) != NULL) { 3799 if (next == dquote_end || strchr("$`\\\n", next)) {
3781 ch = i_getch(input); /* eat next */ 3800 ch = i_getch(input); /* eat next */
3782 if (ch == '\n') 3801 if (ch == '\n')
3783 goto again; /* skip \<newline> */ 3802 goto again; /* skip \<newline> */
3784 } /* else: ch remains == '\\', and we double it */ 3803 } /* else: ch remains == '\\', and we double it */
3785 o_addqchr(dest, ch); 3804 o_addqchr(dest, ch); /* \c if c is s glob char, else just c */
3786 nommu_addchr(as_string, ch); 3805 nommu_addchr(as_string, ch);
3787 goto again; 3806 goto again;
3788 } 3807 }
3789 if (ch == '$') { 3808 if (ch == '$') {
3790//CHECK: 0x80? or dquoted?
3791 if (parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80) != 0) { 3809 if (parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80) != 0) {
3792 debug_printf_parse("parse_stream_dquoted return 1: " 3810 debug_printf_parse("encode_string return 1: "
3793 "parse_dollar returned non-0\n"); 3811 "parse_dollar returned non-0\n");
3794 return 1; 3812 return 1;
3795 } 3813 }
@@ -4162,7 +4180,7 @@ static struct pipe *parse_stream(char **pstring,
4162 dest.has_quoted_part = 1; 4180 dest.has_quoted_part = 1;
4163 if (dest.o_assignment == NOT_ASSIGNMENT) 4181 if (dest.o_assignment == NOT_ASSIGNMENT)
4164 dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; 4182 dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS;
4165 if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"', /*dquoted:*/ 1)) 4183 if (encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1))
4166 goto parse_error; 4184 goto parse_error;
4167 dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; 4185 dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS;
4168 break; 4186 break;
@@ -4335,6 +4353,11 @@ static struct pipe *parse_stream(char **pstring,
4335/*** Execution routines ***/ 4353/*** Execution routines ***/
4336 4354
4337/* Expansion can recurse, need forward decls: */ 4355/* Expansion can recurse, need forward decls: */
4356#if !ENABLE_HUSH_BASH_COMPAT
4357/* only ${var/pattern/repl} (its pattern part) needs additional mode */
4358#define expand_string_to_string(str, do_unbackslash) \
4359 expand_string_to_string(str)
4360#endif
4338static char *expand_string_to_string(const char *str, int do_unbackslash); 4361static char *expand_string_to_string(const char *str, int do_unbackslash);
4339static int process_command_subs(o_string *dest, const char *s); 4362static int process_command_subs(o_string *dest, const char *s);
4340 4363
@@ -4387,8 +4410,17 @@ static int expand_on_ifs(o_string *output, int n, const char *str)
4387 * Returns malloced string. 4410 * Returns malloced string.
4388 * As an optimization, we return NULL if expansion is not needed. 4411 * As an optimization, we return NULL if expansion is not needed.
4389 */ 4412 */
4390static char *expand_pseudo_dquoted(const char *str, int dquoted, int do_unbackslash) 4413#if !ENABLE_HUSH_BASH_COMPAT
4414/* only ${var/pattern/repl} (its pattern part) needs additional mode */
4415#define encode_then_expand_string(str, process_bkslash, do_unbackslash) \
4416 encode_then_expand_string(str)
4417#endif
4418static char *encode_then_expand_string(const char *str, int process_bkslash, int do_unbackslash)
4391{ 4419{
4420//#if !ENABLE_HUSH_BASH_COMPAT
4421// const int process_bkslash = 1;
4422// const int do_unbackslash = 1;
4423//#endif
4392 char *exp_str; 4424 char *exp_str;
4393 struct in_str input; 4425 struct in_str input;
4394 o_string dest = NULL_O_STRING; 4426 o_string dest = NULL_O_STRING;
@@ -4406,7 +4438,7 @@ static char *expand_pseudo_dquoted(const char *str, int dquoted, int do_unbacksl
4406 * echo $(($a + `echo 1`)) $((1 + $((2)) )) 4438 * echo $(($a + `echo 1`)) $((1 + $((2)) ))
4407 */ 4439 */
4408 setup_string_in_str(&input, str); 4440 setup_string_in_str(&input, str);
4409 parse_stream_dquoted(NULL, &dest, &input, EOF, dquoted); 4441 encode_string(NULL, &dest, &input, EOF, process_bkslash);
4410 //bb_error_msg("'%s' -> '%s'", str, dest.data); 4442 //bb_error_msg("'%s' -> '%s'", str, dest.data);
4411 exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); 4443 exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash);
4412 //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); 4444 //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)
4424 hooks.lookupvar = get_local_var_value; 4456 hooks.lookupvar = get_local_var_value;
4425 hooks.setvar = set_local_var_from_halves; 4457 hooks.setvar = set_local_var_from_halves;
4426 //hooks.endofname = endofname; 4458 //hooks.endofname = endofname;
4427 exp_str = expand_pseudo_dquoted(arg, /*dquoted:*/ 1, /*unbackslash:*/ 1); 4459 exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
4428 res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); 4460 res = arith(exp_str ? exp_str : arg, errcode_p, &hooks);
4429 free(exp_str); 4461 free(exp_str);
4430 return res; 4462 return res;
@@ -4594,7 +4626,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
4594//TODO: avoid xstrdup unless needed 4626//TODO: avoid xstrdup unless needed
4595// (see HACK ALERT below for an example) 4627// (see HACK ALERT below for an example)
4596 val = to_be_freed = xstrdup(val); 4628 val = to_be_freed = xstrdup(val);
4597 exp_exp_word = expand_pseudo_dquoted(exp_word, /*dquoted:*/ 1, /*unbackslash:*/ 1); 4629 exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
4598 if (exp_exp_word) 4630 if (exp_exp_word)
4599 exp_word = exp_exp_word; 4631 exp_word = exp_exp_word;
4600 loc = scan_and_match(to_be_freed, exp_word, scan_flags); 4632 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
4631 * (note that a*z _pattern_ is never globbed!) 4663 * (note that a*z _pattern_ is never globbed!)
4632 */ 4664 */
4633 char *pattern, *repl, *t; 4665 char *pattern, *repl, *t;
4634 pattern = expand_pseudo_dquoted(exp_word, /*dquoted:*/ 0, /*unbackslash:*/ 0); 4666 pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0);
4635 if (!pattern) 4667 if (!pattern)
4636 pattern = xstrdup(exp_word); 4668 pattern = xstrdup(exp_word);
4637 debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); 4669 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
4639 exp_word = p; 4671 exp_word = p;
4640 p = strchr(p, SPECIAL_VAR_SYMBOL); 4672 p = strchr(p, SPECIAL_VAR_SYMBOL);
4641 *p = '\0'; 4673 *p = '\0';
4642 repl = expand_pseudo_dquoted(exp_word, /*dquoted:*/ arg0 & 0x80, /*unbackslash:*/ 1); 4674 repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ arg0 & 0x80, /*unbackslash:*/ 1);
4643 debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); 4675 debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl);
4644 /* HACK ALERT. We depend here on the fact that 4676 /* HACK ALERT. We depend here on the fact that
4645 * G.global_argv and results of utoa and get_local_var_value 4677 * 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
4724 debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, 4756 debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op,
4725 (exp_save == ':') ? "true" : "false", use_word); 4757 (exp_save == ':') ? "true" : "false", use_word);
4726 if (use_word) { 4758 if (use_word) {
4727 to_be_freed = expand_pseudo_dquoted(exp_word, /*dquoted:*/ 1, /*unbackslash:*/ 1); 4759 to_be_freed = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
4728 if (to_be_freed) 4760 if (to_be_freed)
4729 exp_word = to_be_freed; 4761 exp_word = to_be_freed;
4730 if (exp_op == '?') { 4762 if (exp_op == '?') {
@@ -4998,6 +5030,9 @@ static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
4998 */ 5030 */
4999static char *expand_string_to_string(const char *str, int do_unbackslash) 5031static char *expand_string_to_string(const char *str, int do_unbackslash)
5000{ 5032{
5033#if !ENABLE_HUSH_BASH_COMPAT
5034 const int do_unbackslash = 1;
5035#endif
5001 char *argv[2], **list; 5036 char *argv[2], **list;
5002 5037
5003 debug_printf_expand("string_to_string<='%s'\n", str); 5038 debug_printf_expand("string_to_string<='%s'\n", str);
@@ -5468,7 +5503,7 @@ static void setup_heredoc(struct redir_struct *redir)
5468 5503
5469 expanded = NULL; 5504 expanded = NULL;
5470 if (!(redir->rd_dup & HEREDOC_QUOTED)) { 5505 if (!(redir->rd_dup & HEREDOC_QUOTED)) {
5471 expanded = expand_pseudo_dquoted(heredoc, /*dquoted:*/ 1, /*unbackslash:*/ 1); 5506 expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
5472 if (expanded) 5507 if (expanded)
5473 heredoc = expanded; 5508 heredoc = expanded;
5474 } 5509 }