aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-07-20 19:11:09 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-07-20 19:11:09 +0200
commit4c3c8a1a61e33a8c55991a260ba73d6a75d74810 (patch)
treeb3991eb4ed268a53065ee7aa49cb3b8879c5cfe6
parent1856740ec0b4ffd8f780c3cc1ef9b38c9fc1eead (diff)
downloadbusybox-w32-4c3c8a1a61e33a8c55991a260ba73d6a75d74810.tar.gz
busybox-w32-4c3c8a1a61e33a8c55991a260ba73d6a75d74810.tar.bz2
busybox-w32-4c3c8a1a61e33a8c55991a260ba73d6a75d74810.zip
hush: tidy up code after previous commits
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--shell/hush.c91
1 files changed, 43 insertions, 48 deletions
diff --git a/shell/hush.c b/shell/hush.c
index 2da288c15..dddba5e30 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -4471,7 +4471,6 @@ static int parse_group(struct parse_context *ctx,
4471 4471
4472#if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS 4472#if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS
4473/* Subroutines for copying $(...) and `...` things */ 4473/* Subroutines for copying $(...) and `...` things */
4474static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote);
4475/* '...' */ 4474/* '...' */
4476static int add_till_single_quote(o_string *dest, struct in_str *input) 4475static int add_till_single_quote(o_string *dest, struct in_str *input)
4477{ 4476{
@@ -4486,7 +4485,21 @@ static int add_till_single_quote(o_string *dest, struct in_str *input)
4486 o_addchr(dest, ch); 4485 o_addchr(dest, ch);
4487 } 4486 }
4488} 4487}
4488static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input)
4489{
4490 while (1) {
4491 int ch = i_getch(input);
4492 if (ch == EOF) {
4493 syntax_error_unterm_ch('\'');
4494 return 0;
4495 }
4496 if (ch == '\'')
4497 return 1;
4498 o_addqchr(dest, ch);
4499 }
4500}
4489/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ 4501/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
4502static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote);
4490static int add_till_double_quote(o_string *dest, struct in_str *input) 4503static int add_till_double_quote(o_string *dest, struct in_str *input)
4491{ 4504{
4492 while (1) { 4505 while (1) {
@@ -5599,6 +5612,7 @@ static char *expand_string_to_string(const char *str, int EXP_flags, int do_unba
5599#if ENABLE_HUSH_TICK 5612#if ENABLE_HUSH_TICK
5600static int process_command_subs(o_string *dest, const char *s); 5613static int process_command_subs(o_string *dest, const char *s);
5601#endif 5614#endif
5615static int expand_vars_to_list(o_string *output, int n, char *arg);
5602 5616
5603/* expand_strvec_to_strvec() takes a list of strings, expands 5617/* expand_strvec_to_strvec() takes a list of strings, expands
5604 * all variable references within and returns a pointer to 5618 * all variable references within and returns a pointer to
@@ -5755,6 +5769,13 @@ static char *encode_then_expand_string(const char *str)
5755 return exp_str; 5769 return exp_str;
5756} 5770}
5757 5771
5772/* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}.
5773 * These can contain single- and double-quoted strings,
5774 * and treated as if the ARG string is initially unquoted. IOW:
5775 * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be
5776 * a dquoted string: "${var#"zz"}"), the difference only comes later
5777 * (word splitting and globbing of the ${var...} result).
5778 */
5758#if !BASH_PATTERN_SUBST 5779#if !BASH_PATTERN_SUBST
5759#define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \ 5780#define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \
5760 encode_then_expand_vararg(str, handle_squotes) 5781 encode_then_expand_vararg(str, handle_squotes)
@@ -5782,14 +5803,6 @@ static char *encode_then_expand_vararg(const char *str, int handle_squotes, int
5782 cp++; 5803 cp++;
5783 } 5804 }
5784 5805
5785 /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}.
5786 * These can contain single- and double-quoted strings,
5787 * and treated as if the ARG string is initially unquoted. IOW:
5788 * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be
5789 * a dquoted string: "${var#"zz"}"), the difference only comes later
5790 * (word splitting and globbing of the ${var...} result).
5791 */
5792
5793 setup_string_in_str(&input, str); 5806 setup_string_in_str(&input, str);
5794 dest.data = xzalloc(1); /* start as "", not as NULL */ 5807 dest.data = xzalloc(1); /* start as "", not as NULL */
5795 exp_str = NULL; 5808 exp_str = NULL;
@@ -5798,28 +5811,21 @@ static char *encode_then_expand_vararg(const char *str, int handle_squotes, int
5798 int ch; 5811 int ch;
5799 5812
5800 ch = i_getch(&input); 5813 ch = i_getch(&input);
5801 if (ch == EOF) {
5802 if (dest.o_expflags) { /* EXP_FLAG_ESC_GLOB_CHARS set? */
5803 syntax_error_unterm_ch('"');
5804 goto ret; /* error */
5805 }
5806 break;
5807 }
5808 debug_printf_parse("%s: ch=%c (%d) escape=%d\n", 5814 debug_printf_parse("%s: ch=%c (%d) escape=%d\n",
5809 __func__, ch, ch, !!dest.o_expflags); 5815 __func__, ch, ch, !!dest.o_expflags);
5810 if (ch == '\'' && handle_squotes && !dest.o_expflags) { 5816
5811//quoting version of add_till_single_quote() (try to merge?): 5817 if (!dest.o_expflags) {
5812 for (;;) { 5818 if (ch == EOF)
5813 ch = i_getch(&input); 5819 break;
5814 if (ch == EOF) { 5820 if (handle_squotes && ch == '\'') {
5815 syntax_error_unterm_ch('\''); 5821 if (!add_till_single_quote_dquoted(&dest, &input))
5816 goto ret; /* error */ 5822 goto ret; /* error */
5817 } 5823 continue;
5818 if (ch == '\'')
5819 break;
5820 o_addqchr(&dest, ch);
5821 } 5824 }
5822 continue; 5825 }
5826 if (ch == EOF) {
5827 syntax_error_unterm_ch('"');
5828 goto ret; /* error */
5823 } 5829 }
5824 if (ch == '"') { 5830 if (ch == '"') {
5825 dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; 5831 dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS;
@@ -5872,8 +5878,8 @@ static char *encode_then_expand_vararg(const char *str, int handle_squotes, int
5872 return exp_str; 5878 return exp_str;
5873} 5879}
5874 5880
5875static int expand_vars_to_list(o_string *output, int n, char *arg); 5881/* Expanding ARG in ${var+ARG}, ${var-ARG}
5876 5882 */
5877static int encode_then_append_var_plusminus(o_string *output, int n, 5883static int encode_then_append_var_plusminus(o_string *output, int n,
5878 const char *str, int dquoted) 5884 const char *str, int dquoted)
5879{ 5885{
@@ -5896,8 +5902,6 @@ static int encode_then_append_var_plusminus(o_string *output, int n,
5896 } 5902 }
5897#endif 5903#endif
5898 5904
5899 /* Expanding ARG in ${var+ARG}, ${var-ARG} */
5900
5901 setup_string_in_str(&input, str); 5905 setup_string_in_str(&input, str);
5902 5906
5903 for (;;) { 5907 for (;;) {
@@ -5938,17 +5942,8 @@ static int encode_then_append_var_plusminus(o_string *output, int n,
5938 continue; 5942 continue;
5939 } 5943 }
5940 if (!dquoted && ch == '\'') { 5944 if (!dquoted && ch == '\'') {
5941//quoting version of add_till_single_quote() (try to merge?): 5945 if (!add_till_single_quote_dquoted(&dest, &input))
5942 for (;;) { 5946 goto ret; /* error */
5943 ch = i_getch(&input);
5944 if (ch == EOF) {
5945 syntax_error_unterm_ch('\'');
5946 goto ret; /* error */
5947 }
5948 if (ch == '\'')
5949 break;
5950 o_addqchr(&dest, ch);
5951 }
5952 o_addchr(&dest, SPECIAL_VAR_SYMBOL); 5947 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
5953 o_addchr(&dest, SPECIAL_VAR_SYMBOL); 5948 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
5954 continue; 5949 continue;
@@ -6105,11 +6100,10 @@ static int append_str_maybe_ifs_split(o_string *output, int n,
6105 return n; 6100 return n;
6106} 6101}
6107 6102
6108/* Helper: 6103/* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct.
6109 * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct.
6110 */ 6104 */
6111static NOINLINE int expand_one_var(o_string *output, 6105static NOINLINE int expand_one_var(o_string *output, int n,
6112 int n, int first_ch, char *arg, char **pp) 6106 int first_ch, char *arg, char **pp)
6113{ 6107{
6114 const char *val; 6108 const char *val;
6115 char *to_be_freed; 6109 char *to_be_freed;
@@ -6385,7 +6379,7 @@ static NOINLINE int expand_one_var(o_string *output,
6385 * $ x=; f "${x:='x y' z}" 6379 * $ x=; f "${x:='x y' z}"
6386 * |'x y' z| 6380 * |'x y' z|
6387 * 6381 *
6388 * $ x=x; f ${x:+'x y' z}| 6382 * $ x=x; f ${x:+'x y' z}
6389 * |x y| 6383 * |x y|
6390 * |z| 6384 * |z|
6391 * $ x=x; f "${x:+'x y' z}" 6385 * $ x=x; f "${x:+'x y' z}"
@@ -6620,7 +6614,8 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
6620 arg = ++p; 6614 arg = ++p;
6621 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ 6615 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
6622 6616
6623 if (arg[0]) { 6617 if (*arg) {
6618 /* handle trailing string */
6624 if (output->ended_in_ifs) { 6619 if (output->ended_in_ifs) {
6625 o_addchr(output, '\0'); 6620 o_addchr(output, '\0');
6626 n = o_save_ptr(output, n); 6621 n = o_save_ptr(output, n);