directive-for-escape.mk revision 1.17 1 1.17 rillig # $NetBSD: directive-for-escape.mk,v 1.17 2023/05/08 10:24:07 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Test escaping of special characters in the iteration values of a .for loop.
4 1.1 rillig # These values get expanded later using the :U variable modifier, and this
5 1.1 rillig # escaping and unescaping must pass all characters and strings effectively
6 1.1 rillig # unmodified.
7 1.1 rillig
8 1.1 rillig .MAKEFLAGS: -df
9 1.1 rillig
10 1.7 rillig # Even though the .for loops take quotes into account when splitting the
11 1.7 rillig # string into words, the quotes don't need to be balanced, as of 2020-12-31.
12 1.1 rillig # This could be considered a bug.
13 1.1 rillig ASCII= !"\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
14 1.1 rillig
15 1.1 rillig # XXX: As of 2020-12-31, the '#' is not preserved in the expanded body of
16 1.12 rillig # the loop. Not only would it need the escaping for the variable modifier
17 1.12 rillig # ':U' but also the escaping for the line-end comment.
18 1.1 rillig .for chars in ${ASCII}
19 1.1 rillig . info ${chars}
20 1.1 rillig .endfor
21 1.1 rillig
22 1.1 rillig # As of 2020-12-31, using 2 backslashes before be '#' would treat the '#'
23 1.1 rillig # as comment character. Using 3 backslashes doesn't help either since
24 1.1 rillig # then the situation is essentially the same as with 1 backslash.
25 1.1 rillig # This means that a '#' sign cannot be passed in the value of a .for loop
26 1.1 rillig # at all.
27 1.1 rillig ASCII.2020-12-31= !"\\\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
28 1.1 rillig .for chars in ${ASCII.2020-12-31}
29 1.1 rillig . info ${chars}
30 1.1 rillig .endfor
31 1.1 rillig
32 1.11 rillig # Cover the code in ExprLen.
33 1.1 rillig #
34 1.1 rillig # XXX: It is unexpected that the variable V gets expanded in the loop body.
35 1.11 rillig # The double '$$' should intuitively prevent exactly this. Probably nobody
36 1.11 rillig # was adventurous enough to use literal dollar signs in the values of a .for
37 1.12 rillig # loop, allowing this edge case to go unnoticed for years.
38 1.11 rillig #
39 1.11 rillig # See for.c, function ExprLen.
40 1.1 rillig V= value
41 1.1 rillig VALUES= $$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
42 1.1 rillig .for i in ${VALUES}
43 1.1 rillig . info $i
44 1.1 rillig .endfor
45 1.1 rillig
46 1.16 rillig
47 1.11 rillig # Try to cover the code for nested '{}' in ExprLen, without success.
48 1.1 rillig #
49 1.7 rillig # The value of the variable VALUES is not meant to be a variable expression.
50 1.7 rillig # Instead, it is meant to represent literal text, the only escaping mechanism
51 1.7 rillig # being that each '$' is written as '$$'.
52 1.16 rillig VALUES= $${UNDEF:U\$$\$$ {{}} end}
53 1.5 rillig #
54 1.5 rillig # The .for loop splits ${VALUES} into 3 words, at the space characters, since
55 1.14 rillig # the '$$' is an ordinary character and the spaces are not escaped.
56 1.14 rillig # Word 1 is '${UNDEF:U\$\$'
57 1.14 rillig # Word 2 is '{{}}'
58 1.14 rillig # Word 3 is 'end}'
59 1.16 rillig #
60 1.16 rillig # Each of these words is now inserted in the body of the .for loop.
61 1.16 rillig .for i in ${VALUES}
62 1.16 rillig # $i
63 1.16 rillig .endfor
64 1.16 rillig #
65 1.16 rillig # When these words are injected into the body of the .for loop, each inside a
66 1.16 rillig # '${:U...}' expression, the result is:
67 1.16 rillig #
68 1.16 rillig # expect: For: loop body:
69 1.16 rillig # expect: # ${:U\${UNDEF\:U\\$\\$}
70 1.16 rillig # expect: For: loop body:
71 1.16 rillig # expect: # ${:U{{\}\}}
72 1.16 rillig # expect: For: loop body:
73 1.16 rillig # expect: # ${:Uend\}}
74 1.16 rillig # expect: For: end for 1
75 1.16 rillig #
76 1.16 rillig # The first of these expressions is the most interesting one, due to its many
77 1.16 rillig # special characters. This expression is properly balanced:
78 1.16 rillig #
79 1.16 rillig # Text Meaning Explanation
80 1.16 rillig # \$ $ escaped
81 1.16 rillig # { { ordinary text
82 1.16 rillig # UNDEF UNDEF ordinary text
83 1.16 rillig # \: : escaped
84 1.16 rillig # U U ordinary text
85 1.16 rillig # \\ \ escaped
86 1.16 rillig # $\ (expr) an expression, the variable name is '\'
87 1.16 rillig # \$ $ escaped
88 1.16 rillig #
89 1.16 rillig # To make the expression '$\' visible, define it to an actual word:
90 1.14 rillig ${:U\\}= backslash
91 1.1 rillig .for i in ${VALUES}
92 1.1 rillig . info $i
93 1.1 rillig .endfor
94 1.16 rillig #
95 1.16 rillig # expect-3: ${UNDEF:U\backslash$
96 1.16 rillig # expect-4: {{}}
97 1.16 rillig # expect-5: end}
98 1.16 rillig #
99 1.16 rillig # FIXME: There was no expression '$\' in the original text of the variable
100 1.16 rillig # 'VALUES', that's a surprise in the parser.
101 1.16 rillig
102 1.1 rillig
103 1.11 rillig # Second try to cover the code for nested '{}' in ExprLen.
104 1.5 rillig #
105 1.16 rillig # XXX: It is not the job of ExprLen to parse an expression, it is naive to
106 1.16 rillig # expect ExprLen to get all the details right in just a few lines of code.
107 1.5 rillig # Each variable modifier has its own inconsistent way of parsing nested
108 1.7 rillig # variable expressions, braces and parentheses. (Compare ':M', ':S', and
109 1.7 rillig # ':D' for details.) The only sensible thing to do is therefore to let
110 1.7 rillig # Var_Parse do all the parsing work.
111 1.5 rillig VALUES= begin<$${UNDEF:Ufallback:N{{{}}}}>end
112 1.5 rillig .for i in ${VALUES}
113 1.5 rillig . info $i
114 1.5 rillig .endfor
115 1.5 rillig
116 1.1 rillig # A single trailing dollar doesn't happen in practice.
117 1.1 rillig # The dollar sign is correctly passed through to the body of the .for loop.
118 1.1 rillig # There, it is expanded by the .info directive, but even there a trailing
119 1.1 rillig # dollar sign is kept as-is.
120 1.1 rillig .for i in ${:U\$}
121 1.1 rillig . info ${i}
122 1.1 rillig .endfor
123 1.2 rillig
124 1.17 rillig # Before for.c 1.173 from 2023-05-08, the name of the iteration variable
125 1.17 rillig # could contain colons, which affected variable expressions having this exact
126 1.17 rillig # modifier. This possibility was neither intended nor documented.
127 1.2 rillig NUMBERS= one two three
128 1.17 rillig # expect+1: invalid character ':' in .for loop variable name
129 1.2 rillig .for NUMBERS:M*e in replaced
130 1.2 rillig . info ${NUMBERS} ${NUMBERS:M*e}
131 1.2 rillig .endfor
132 1.2 rillig
133 1.17 rillig # Before for.c 1.173 from 2023-05-08, the name of the iteration variable
134 1.17 rillig # could contain braces, which allowed to replace sequences of variable
135 1.17 rillig # expressions. This possibility was neither intended nor documented.
136 1.2 rillig BASENAME= one
137 1.2 rillig EXT= .c
138 1.17 rillig # expect+1: invalid character '}' in .for loop variable name
139 1.2 rillig .for BASENAME}${EXT in replaced
140 1.2 rillig . info ${BASENAME}${EXT}
141 1.2 rillig .endfor
142 1.3 rillig
143 1.3 rillig # Demonstrate the various ways to refer to the iteration variable.
144 1.3 rillig i= outer
145 1.3 rillig i2= two
146 1.3 rillig i,= comma
147 1.3 rillig .for i in inner
148 1.3 rillig . info . $$i: $i
149 1.3 rillig . info . $${i}: ${i}
150 1.3 rillig . info . $${i:M*}: ${i:M*}
151 1.3 rillig . info . $$(i): $(i)
152 1.3 rillig . info . $$(i:M*): $(i:M*)
153 1.3 rillig . info . $${i$${:U}}: ${i${:U}}
154 1.6 rillig . info . $${i\}}: ${i\}} # XXX: unclear why ForLoop_SubstVarLong needs this
155 1.3 rillig . info . $${i2}: ${i2}
156 1.3 rillig . info . $${i,}: ${i,}
157 1.3 rillig . info . adjacent: $i${i}${i:M*}$i
158 1.3 rillig .endfor
159 1.4 rillig
160 1.17 rillig # Before for.c 1.173 from 2023-05-08, the variable name could be a single '$'
161 1.17 rillig # since there was no check on valid variable names. ForLoop_SubstVarShort
162 1.17 rillig # skipped "stupid" variable names though, but ForLoop_SubstVarLong naively
163 1.17 rillig # parsed the body of the loop, substituting each '${$}' with an actual
164 1.17 rillig # '${:Udollar}'.
165 1.8 rillig .for $ in dollar
166 1.8 rillig . info eight $$$$$$$$ and no cents.
167 1.8 rillig . info eight ${$}${$}${$}${$} and no cents.
168 1.8 rillig .endfor
169 1.8 rillig # Outside a .for loop, '${$}' is interpreted differently. The outer '$' starts
170 1.8 rillig # a variable expression. The inner '$' is followed by a '}' and is thus a
171 1.8 rillig # silent syntax error, the '$' is skipped. The variable name is thus '', and
172 1.8 rillig # since since there is never a variable named '', the whole expression '${$}'
173 1.8 rillig # evaluates to an empty string.
174 1.8 rillig closing-brace= } # guard against an
175 1.8 rillig ${closing-brace}= <closing-brace> # alternative interpretation
176 1.8 rillig .info eight ${$}${$}${$}${$} and no cents.
177 1.8 rillig
178 1.9 rillig # What happens if the values from the .for loop contain a literal newline?
179 1.10 rillig # Before for.c 1.144 from 2021-06-25, the newline was passed verbatim to the
180 1.10 rillig # body of the .for loop, where it was then interpreted as a literal newline,
181 1.10 rillig # leading to syntax errors such as "Unclosed variable expression" in the upper
182 1.10 rillig # line and "Invalid line type" in the lower line.
183 1.9 rillig .for i in "${.newline}"
184 1.9 rillig . info short: $i
185 1.9 rillig . info long: ${i}
186 1.9 rillig .endfor
187 1.9 rillig
188 1.13 rillig # No error since the newline character is not actually used.
189 1.13 rillig .for i in "${.newline}"
190 1.13 rillig .endfor
191 1.13 rillig
192 1.13 rillig # Between for.c 1.161 from 2022-01-08 and before for.c 1.163 from 2022-01-09,
193 1.13 rillig # a newline character in a .for loop led to a crash since at the point where
194 1.13 rillig # the error message including the stack trace is printed, the body of the .for
195 1.13 rillig # loop is assembled, and at that point, ForLoop.nextItem had already been
196 1.13 rillig # advanced.
197 1.13 rillig .MAKEFLAGS: -dp
198 1.13 rillig .for i in "${.newline}"
199 1.13 rillig : $i
200 1.13 rillig .endfor
201 1.13 rillig .MAKEFLAGS: -d0
202 1.13 rillig
203 1.14 rillig .MAKEFLAGS: -df
204 1.14 rillig .for i in \# \\\#
205 1.14 rillig # $i
206 1.14 rillig .endfor
207 1.14 rillig
208 1.14 rillig .for i in $$ $$i $$(i) $${i} $$$$ $$$$$$$$ $${:U\$$\$$}
209 1.14 rillig # $i
210 1.14 rillig .endfor
211 1.14 rillig
212 1.15 rillig # The expression '${.TARGET}' must be preserved as it is one of the 7 built-in
213 1.15 rillig # target-local variables. See for.c 1.45 from 2009-01-14.
214 1.15 rillig .for i in ${.TARGET} $${.TARGET} $$${.TARGET} $$$${.TARGET}
215 1.15 rillig # $i
216 1.15 rillig .endfor
217 1.15 rillig # expect: # ${:U${.TARGET}}
218 1.15 rillig # XXX: Why does '$' result in the same text as '$$'?
219 1.15 rillig # expect: # ${:U${.TARGET}}
220 1.15 rillig # XXX: Why does the '$$' before the '${.TARGET}' lead to an escaped '}'?
221 1.15 rillig # expect: # ${:U$${.TARGET\}}
222 1.15 rillig # XXX: Why does '$' result in the same text as '$$'?
223 1.15 rillig # XXX: Why does the '$$' before the '${.TARGET}' lead to an escaped '}'?
224 1.15 rillig # expect: # ${:U$${.TARGET\}}
225 1.15 rillig
226 1.14 rillig .for i in ((( {{{ ))) }}}
227 1.14 rillig # $i
228 1.14 rillig .endfor
229 1.14 rillig .MAKEFLAGS: -d0
230 1.14 rillig
231 1.4 rillig all:
232