directive-for-escape.mk revision 1.8 1 1.8 rillig # $NetBSD: directive-for-escape.mk,v 1.8 2021/06/24 23:22:17 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.1 rillig # the loop since it would not need only the escaping for the :U variable
17 1.1 rillig # modifier 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.1 rillig # Cover the code in for_var_len.
33 1.1 rillig #
34 1.1 rillig # XXX: It is unexpected that the variable V gets expanded in the loop body.
35 1.1 rillig # The double '$$' should prevent exactly this. Probably nobody was
36 1.7 rillig # adventurous enough to use literal dollar signs in the values of a .for
37 1.1 rillig # loop.
38 1.1 rillig V= value
39 1.1 rillig VALUES= $$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
40 1.1 rillig .for i in ${VALUES}
41 1.1 rillig . info $i
42 1.1 rillig .endfor
43 1.1 rillig
44 1.5 rillig # Try to cover the code for nested '{}' in for_var_len, without success.
45 1.1 rillig #
46 1.7 rillig # The value of the variable VALUES is not meant to be a variable expression.
47 1.7 rillig # Instead, it is meant to represent literal text, the only escaping mechanism
48 1.7 rillig # being that each '$' is written as '$$'.
49 1.5 rillig #
50 1.5 rillig # The .for loop splits ${VALUES} into 3 words, at the space characters, since
51 1.5 rillig # these are not escaped.
52 1.1 rillig VALUES= $${UNDEF:U\$$\$$ {{}} end}
53 1.7 rillig # XXX: Where in the code does the '\$\$' get converted into a single '\$'?
54 1.1 rillig .for i in ${VALUES}
55 1.1 rillig . info $i
56 1.1 rillig .endfor
57 1.1 rillig
58 1.5 rillig # Second try to cover the code for nested '{}' in for_var_len.
59 1.5 rillig #
60 1.5 rillig # XXX: It is wrong that for_var_len requires the braces to be balanced.
61 1.5 rillig # Each variable modifier has its own inconsistent way of parsing nested
62 1.7 rillig # variable expressions, braces and parentheses. (Compare ':M', ':S', and
63 1.7 rillig # ':D' for details.) The only sensible thing to do is therefore to let
64 1.7 rillig # Var_Parse do all the parsing work.
65 1.5 rillig VALUES= begin<$${UNDEF:Ufallback:N{{{}}}}>end
66 1.5 rillig .for i in ${VALUES}
67 1.5 rillig . info $i
68 1.5 rillig .endfor
69 1.5 rillig
70 1.1 rillig # A single trailing dollar doesn't happen in practice.
71 1.1 rillig # The dollar sign is correctly passed through to the body of the .for loop.
72 1.1 rillig # There, it is expanded by the .info directive, but even there a trailing
73 1.1 rillig # dollar sign is kept as-is.
74 1.1 rillig .for i in ${:U\$}
75 1.1 rillig . info ${i}
76 1.1 rillig .endfor
77 1.2 rillig
78 1.2 rillig # As of 2020-12-31, the name of the iteration variable can even contain
79 1.2 rillig # colons, which then affects variable expressions having this exact modifier.
80 1.2 rillig # This is clearly an unintended side effect of the implementation.
81 1.2 rillig NUMBERS= one two three
82 1.2 rillig .for NUMBERS:M*e in replaced
83 1.2 rillig . info ${NUMBERS} ${NUMBERS:M*e}
84 1.2 rillig .endfor
85 1.2 rillig
86 1.2 rillig # As of 2020-12-31, the name of the iteration variable can contain braces,
87 1.2 rillig # which gets even more surprising than colons, since it allows to replace
88 1.2 rillig # sequences of variable expressions. There is no practical use case for
89 1.2 rillig # this, though.
90 1.2 rillig BASENAME= one
91 1.2 rillig EXT= .c
92 1.2 rillig .for BASENAME}${EXT in replaced
93 1.2 rillig . info ${BASENAME}${EXT}
94 1.2 rillig .endfor
95 1.3 rillig
96 1.3 rillig # Demonstrate the various ways to refer to the iteration variable.
97 1.3 rillig i= outer
98 1.3 rillig i2= two
99 1.3 rillig i,= comma
100 1.3 rillig .for i in inner
101 1.3 rillig . info . $$i: $i
102 1.3 rillig . info . $${i}: ${i}
103 1.3 rillig . info . $${i:M*}: ${i:M*}
104 1.3 rillig . info . $$(i): $(i)
105 1.3 rillig . info . $$(i:M*): $(i:M*)
106 1.3 rillig . info . $${i$${:U}}: ${i${:U}}
107 1.6 rillig . info . $${i\}}: ${i\}} # XXX: unclear why ForLoop_SubstVarLong needs this
108 1.3 rillig . info . $${i2}: ${i2}
109 1.3 rillig . info . $${i,}: ${i,}
110 1.3 rillig . info . adjacent: $i${i}${i:M*}$i
111 1.3 rillig .endfor
112 1.4 rillig
113 1.8 rillig # The variable name can be a single '$' since there is no check on valid
114 1.8 rillig # variable names. ForLoop_SubstVarShort skips "stupid" variable names though,
115 1.8 rillig # but ForLoop_SubstVarLong naively parses the body of the loop, substituting
116 1.8 rillig # each '${$}' with an actual 'dollar'.
117 1.8 rillig .for $ in dollar
118 1.8 rillig . info eight $$$$$$$$ and no cents.
119 1.8 rillig . info eight ${$}${$}${$}${$} and no cents.
120 1.8 rillig .endfor
121 1.8 rillig # Outside a .for loop, '${$}' is interpreted differently. The outer '$' starts
122 1.8 rillig # a variable expression. The inner '$' is followed by a '}' and is thus a
123 1.8 rillig # silent syntax error, the '$' is skipped. The variable name is thus '', and
124 1.8 rillig # since since there is never a variable named '', the whole expression '${$}'
125 1.8 rillig # evaluates to an empty string.
126 1.8 rillig closing-brace= } # guard against an
127 1.8 rillig ${closing-brace}= <closing-brace> # alternative interpretation
128 1.8 rillig .info eight ${$}${$}${$}${$} and no cents.
129 1.8 rillig
130 1.4 rillig all:
131