897372695c2cfb6de3add91a96f813d9d7170abb
[openwrt/svn-archive/archive.git] / package / busybox / patches / 000-upstream-hush.patch
1 --- a/shell/hush.c
2 +++ b/shell/hush.c
3 @@ -1853,7 +1853,7 @@ static void o_addblock_duplicate_backsla
4 while (len) {
5 o_addchr(o, *str);
6 if (*str++ == '\\'
7 - && (*str != '*' && *str != '?' && *str != '[')
8 +// && (*str != '*' && *str != '?' && *str != '[')
9 ) {
10 o_addchr(o, '\\');
11 }
12 @@ -2834,18 +2834,22 @@ static NOINLINE int expand_vars_to_list(
13 return n;
14 }
15
16 -static char **expand_variables(char **argv, int or_mask)
17 +enum {
18 + EXPVAR_FLAG_GLOB = 0x200,
19 + EXPVAR_FLAG_ESCAPE_VARS = 0x100,
20 + EXPVAR_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */
21 +};
22 +static char **expand_variables(char **argv, unsigned or_mask)
23 {
24 int n;
25 char **list;
26 char **v;
27 o_string output = NULL_O_STRING;
28
29 - if (or_mask & 0x100) {
30 - output.o_escape = 1; /* protect against globbing for "$var" */
31 - /* (unquoted $var will temporarily switch it off) */
32 - output.o_glob = 1;
33 - }
34 + /* protect against globbing for "$var"? */
35 + /* (unquoted $var will temporarily switch it off) */
36 + output.o_escape = 1 & (or_mask / EXPVAR_FLAG_ESCAPE_VARS);
37 + output.o_glob = 1 & (or_mask / EXPVAR_FLAG_GLOB);
38
39 n = 0;
40 v = argv;
41 @@ -2863,13 +2867,13 @@ static char **expand_variables(char **ar
42
43 static char **expand_strvec_to_strvec(char **argv)
44 {
45 - return expand_variables(argv, 0x100);
46 + return expand_variables(argv, EXPVAR_FLAG_GLOB | EXPVAR_FLAG_ESCAPE_VARS);
47 }
48
49 #if ENABLE_HUSH_BASH_COMPAT
50 static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
51 {
52 - return expand_variables(argv, 0x80);
53 + return expand_variables(argv, EXPVAR_FLAG_SINGLEWORD);
54 }
55 #endif
56
57 @@ -2909,15 +2913,15 @@ static char **expand_strvec_to_strvec_si
58 #endif
59
60 /* Used for expansion of right hand of assignments */
61 -/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
62 - * "v=/bin/c*" */
63 +/* NB: should NOT do globbing!
64 + * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" */
65 static char *expand_string_to_string(const char *str)
66 {
67 char *argv[2], **list;
68
69 argv[0] = (char*)str;
70 argv[1] = NULL;
71 - list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */
72 + list = expand_variables(argv, EXPVAR_FLAG_ESCAPE_VARS | EXPVAR_FLAG_SINGLEWORD);
73 if (HUSH_DEBUG)
74 if (!list[0] || list[1])
75 bb_error_msg_and_die("BUG in varexp2");
76 @@ -2933,7 +2937,7 @@ static char* expand_strvec_to_string(cha
77 {
78 char **list;
79
80 - list = expand_variables(argv, 0x80);
81 + list = expand_variables(argv, EXPVAR_FLAG_SINGLEWORD);
82 /* Convert all NULs to spaces */
83 if (list[0]) {
84 int n = 1;
85 --- /dev/null
86 +++ b/shell/hush_test/hush-vars/var_unbackslash.right
87 @@ -0,0 +1,9 @@
88 +b1=-qwerty-t-\-"---z-*-?-
89 +b1=-qwerty-t-\-"---z-*-?-
90 +b2=-$a-\t-\\-\"-\--\z-\*-\?-
91 +b2=-$a-\t-\\-\"-\--\z-\*-\?-
92 +c=-$a-\t-\\-\"-\--\z-\*-\?-
93 +c=-$a-\t-\\-\"-\--\z-\*-\?-
94 +c=-$a-\t-\\-\"-\--\z-\*-\?-
95 +c=-$a-\t-\\-\"-\--\z-\*-\?-
96 +Done: 0
97 --- /dev/null
98 +++ b/shell/hush_test/hush-vars/var_unbackslash.tests
99 @@ -0,0 +1,20 @@
100 +# Test for correct handling of backslashes
101 +a=qwerty
102 +
103 +b=-$a-\t-\\-\"-\--\z-\*-\?-
104 +echo b1=$b
105 +echo "b1=$b"
106 +b='-$a-\t-\\-\"-\--\z-\*-\?-'
107 +echo b2=$b
108 +echo "b2=$b"
109 +
110 +c=$b
111 +echo "c=$c"
112 +c=${b}
113 +echo "c=$c"
114 +c="$b"
115 +echo "c=$c"
116 +c="${b}"
117 +echo "c=$c"
118 +
119 +echo "Done: $?"