varmod-sysv.mk revision 1.11 1 1.11 rillig # $NetBSD: varmod-sysv.mk,v 1.11 2020/11/01 22:28:52 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the ${VAR:from=to} variable modifier, which replaces the suffix
4 1.2 rillig # "from" with "to". It can also use '%' as a wildcard.
5 1.2 rillig #
6 1.2 rillig # This modifier is applied when the other modifiers don't match exactly.
7 1.6 rillig #
8 1.6 rillig # See ApplyModifier_SysV.
9 1.1 rillig
10 1.8 rillig # A typical use case for the :from=to modifier is conversion of filename
11 1.8 rillig # extensions.
12 1.8 rillig .if ${src.c:L:.c=.o} != "src.o"
13 1.8 rillig . error
14 1.8 rillig .endif
15 1.8 rillig
16 1.8 rillig # The modifier applies to each word on its own.
17 1.8 rillig .if ${one.c two.c three.c:L:.c=.o} != "one.o two.o three.o"
18 1.8 rillig . error
19 1.8 rillig .endif
20 1.8 rillig
21 1.8 rillig # Words that don't match the pattern are passed unmodified.
22 1.8 rillig .if ${src.c src.h:L:.c=.o} != "src.o src.h"
23 1.8 rillig . error
24 1.8 rillig .endif
25 1.8 rillig
26 1.8 rillig # The :from=to modifier is therefore often combined with the :M modifier.
27 1.8 rillig .if ${src.c src.h:L:M*.c:.c=.o} != "src.o"
28 1.8 rillig . error
29 1.8 rillig .endif
30 1.8 rillig
31 1.8 rillig # Another use case for the :from=to modifier is to append a suffix to each
32 1.8 rillig # word. In this case, the "from" string is empty, therefore it always
33 1.8 rillig # matches. The same effect can be achieved with the :S,$,teen, modifier.
34 1.8 rillig .if ${four six seven nine:L:=teen} != "fourteen sixteen seventeen nineteen"
35 1.8 rillig . error
36 1.8 rillig .endif
37 1.1 rillig
38 1.9 rillig # The :from=to modifier can also be used to surround each word by strings.
39 1.9 rillig # It might be tempting to use this for enclosing a string in quotes for the
40 1.9 rillig # shell, but that's the job of the :Q modifier.
41 1.9 rillig .if ${one two three:L:%=(%)} != "(one) (two) (three)"
42 1.9 rillig . error
43 1.9 rillig .endif
44 1.9 rillig
45 1.8 rillig # When the :from=to modifier is parsed, it lasts until the closing brace
46 1.8 rillig # or parenthesis. The :Q in the below expression may look like a modifier
47 1.8 rillig # but isn't. It is part of the replacement string.
48 1.4 rillig .if ${a b c d e:L:%a=x:Q} != "x:Q b c d e"
49 1.4 rillig . error
50 1.4 rillig .endif
51 1.3 rillig
52 1.9 rillig # In the :from=to modifier, both parts can contain variable expressions.
53 1.9 rillig .if ${one two:L:${:Uone}=${:U1}} != "1 two"
54 1.9 rillig . error
55 1.9 rillig .endif
56 1.9 rillig
57 1.9 rillig # In the :from=to modifier, the "from" part is expanded exactly once.
58 1.9 rillig .if ${:U\$ \$\$ \$\$\$\$:${:U\$\$\$\$}=4} != "\$ \$\$ 4"
59 1.9 rillig . error
60 1.9 rillig .endif
61 1.9 rillig
62 1.9 rillig # In the :from=to modifier, the "to" part is expanded exactly twice.
63 1.9 rillig # XXX: The right-hand side should be expanded only once.
64 1.9 rillig # XXX: It's hard to get the escaping correct here, and to read that.
65 1.9 rillig # XXX: It's not intuitive why the closing brace must be escaped but not
66 1.9 rillig # the opening brace.
67 1.9 rillig .if ${:U1 2 4:4=${:Uonce\${\:Utwice\}}} != "1 2 oncetwice"
68 1.9 rillig . error
69 1.9 rillig .endif
70 1.9 rillig
71 1.9 rillig # The replacement string can contain spaces, thereby changing the number
72 1.9 rillig # of words in the variable expression.
73 1.9 rillig .if ${In:L:%=% ${:Uthe Sun}} != "In the Sun"
74 1.9 rillig . error
75 1.9 rillig .endif
76 1.9 rillig
77 1.11 rillig # If the variable value is empty, it is debatable whether it consists of a
78 1.11 rillig # single empty word, or no word at all. The :from=to modifier treats it as
79 1.11 rillig # no word at all.
80 1.9 rillig .if ${:L:=suffix} != ""
81 1.9 rillig . error
82 1.9 rillig .endif
83 1.9 rillig
84 1.11 rillig # If the variable value is empty, it is debatable whether it consists of a
85 1.11 rillig # single empty word, or no word at all. The :from=to modifier treats it as
86 1.11 rillig # no word at all.
87 1.11 rillig .if ${:L:%=suffix} != ""
88 1.11 rillig . error
89 1.11 rillig .endif
90 1.11 rillig
91 1.3 rillig # Before 2020-07-19, an ampersand could be used in the replacement part
92 1.4 rillig # of a SysV substitution modifier, and it was replaced with the whole match,
93 1.4 rillig # just like in the :S modifier.
94 1.4 rillig #
95 1.4 rillig # This was probably a copy-and-paste mistake since the code for the SysV
96 1.4 rillig # modifier looked a lot like the code for the :S and :C modifiers.
97 1.4 rillig # The ampersand is not mentioned in the manual page.
98 1.4 rillig .if ${a.bcd.e:L:a.%=%} != "bcd.e"
99 1.4 rillig . error
100 1.4 rillig .endif
101 1.4 rillig # Before 2020-07-19, the result of the expression was "a.bcd.e".
102 1.4 rillig .if ${a.bcd.e:L:a.%=&} != "&"
103 1.4 rillig . error
104 1.4 rillig .endif
105 1.3 rillig
106 1.3 rillig # Before 2020-07-20, when a SysV modifier was parsed, a single dollar
107 1.4 rillig # before the '=' was parsed (but not interpreted) as an anchor.
108 1.4 rillig # Parsing something without then evaluating it accordingly doesn't make
109 1.4 rillig # sense.
110 1.4 rillig .if ${value:L:e$=x} != "value"
111 1.4 rillig . error
112 1.4 rillig .endif
113 1.4 rillig # Before 2020-07-20, the modifier ":e$=x" was parsed as having a left-hand
114 1.4 rillig # side "e" and a right-hand side "x". The dollar was parsed (but not
115 1.4 rillig # interpreted) as 'anchor at the end'. Therefore the modifier was equivalent
116 1.4 rillig # to ":e=x", which doesn't match the string "value$". Therefore the whole
117 1.4 rillig # expression evaluated to "value$".
118 1.4 rillig .if ${${:Uvalue\$}:L:e$=x} != "valux"
119 1.4 rillig . error
120 1.4 rillig .endif
121 1.4 rillig .if ${value:L:e=x} != "valux"
122 1.4 rillig . error
123 1.4 rillig .endif
124 1.3 rillig
125 1.3 rillig # Words that don't match are copied unmodified.
126 1.4 rillig .if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
127 1.4 rillig . error
128 1.4 rillig .endif
129 1.4 rillig
130 1.4 rillig # The % placeholder can be anywhere in the string, it doesn't have to be at
131 1.4 rillig # the beginning of the pattern.
132 1.4 rillig .if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c"
133 1.4 rillig . error
134 1.4 rillig .endif
135 1.3 rillig
136 1.9 rillig # It's also possible to modify each word by replacing the prefix and adding
137 1.9 rillig # a suffix.
138 1.9 rillig .if ${one two:L:o%=a%w} != "anew two"
139 1.9 rillig . error
140 1.9 rillig .endif
141 1.9 rillig
142 1.7 rillig # Each word gets the suffix "X" appended.
143 1.7 rillig .if ${one two:L:=X} != "oneX twoX"
144 1.7 rillig . error
145 1.7 rillig .endif
146 1.7 rillig
147 1.6 rillig # The suffix "o" is replaced with "X".
148 1.6 rillig .if ${one two:L:o=X} != "one twX"
149 1.4 rillig . error
150 1.4 rillig .endif
151 1.6 rillig
152 1.6 rillig # The suffix "o" is replaced with nothing.
153 1.6 rillig .if ${one two:L:o=} != "one tw"
154 1.4 rillig . error
155 1.4 rillig .endif
156 1.6 rillig
157 1.6 rillig # The suffix "o" is replaced with a literal percent. The percent is only
158 1.6 rillig # a wildcard when it appears on the left-hand side.
159 1.6 rillig .if ${one two:L:o=%} != "one tw%"
160 1.4 rillig . error
161 1.4 rillig .endif
162 1.6 rillig
163 1.6 rillig # Each word with the suffix "o" is replaced with "X". The percent is a
164 1.6 rillig # wildcard even though the right-hand side does not contain another percent.
165 1.6 rillig .if ${one two:L:%o=X} != "one X"
166 1.4 rillig . error
167 1.4 rillig .endif
168 1.6 rillig
169 1.6 rillig # Each word with the prefix "o" is replaced with "X". The percent is a
170 1.6 rillig # wildcard even though the right-hand side does not contain another percent.
171 1.6 rillig .if ${one two:L:o%=X} != "X two"
172 1.4 rillig . error
173 1.4 rillig .endif
174 1.6 rillig
175 1.6 rillig # For each word with the prefix "o" and the suffix "e", the whole word is
176 1.6 rillig # replaced with "X".
177 1.6 rillig .if ${one two oe oxen:L:o%e=X} != "X two X oxen"
178 1.4 rillig . error
179 1.4 rillig .endif
180 1.6 rillig
181 1.6 rillig # Only the first '%' is the wildcard.
182 1.6 rillig .if ${one two o%e other%e:L:o%%e=X} != "one two X X"
183 1.4 rillig . error
184 1.4 rillig .endif
185 1.6 rillig
186 1.6 rillig # In the replacement, only the first '%' is the placeholder, all others
187 1.6 rillig # are literal percent characters.
188 1.6 rillig .if ${one two:L:%=%%} != "one% two%"
189 1.4 rillig . error
190 1.4 rillig .endif
191 1.6 rillig
192 1.6 rillig # In the word "one", only a prefix of the pattern suffix "nes" matches,
193 1.6 rillig # the whole word is too short. Therefore it doesn't match.
194 1.6 rillig .if ${one two:L:%nes=%xxx} != "one two"
195 1.4 rillig . error
196 1.3 rillig .endif
197 1.5 rillig
198 1.9 rillig # The :from=to modifier can be used to replace both the prefix and a suffix
199 1.9 rillig # of a word with other strings. This is not possible with a single :S
200 1.9 rillig # modifier, and using a :C modifier for the same task looks more complicated
201 1.9 rillig # in many cases.
202 1.9 rillig .if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s"
203 1.9 rillig . error
204 1.9 rillig .endif
205 1.9 rillig
206 1.10 rillig # This is not a SysV modifier since the nested variable expression expands
207 1.10 rillig # to an empty string. The '=' in it should be irrelevant during parsing.
208 1.10 rillig # As of 2020-11-01, this seemingly correct modifier leads to a parse error.
209 1.10 rillig # XXX
210 1.10 rillig .if ${word203:L:from${:D=}to}
211 1.10 rillig . error
212 1.10 rillig .endif
213 1.10 rillig
214 1.10 rillig # XXX: This specially constructed case demonstrates that the SysV modifier
215 1.10 rillig # lasts longer than expected. The whole expression initially has the value
216 1.10 rillig # "fromto}...". The next modifier is a SysV modifier. ApplyModifier_SysV
217 1.10 rillig # parses the modifier as "from${:D=}to", ending at the '}'. Next, the two
218 1.10 rillig # parts of the modifier are parsed using ParseModifierPart, which scans
219 1.10 rillig # differently, properly handling nested variable expressions. The two parts
220 1.10 rillig # are now "fromto}..." and "replaced".
221 1.10 rillig .if "${:Ufromto\}...:from${:D=}to}...=replaced}" != "replaced"
222 1.10 rillig . error
223 1.10 rillig .endif
224 1.10 rillig
225 1.5 rillig # As of 2020-10-06, the right-hand side of the SysV modifier is expanded
226 1.5 rillig # twice. The first expansion happens in ApplyModifier_SysV, where the
227 1.5 rillig # modifier is split into its two parts. The second expansion happens
228 1.5 rillig # when each word is replaced in ModifyWord_SYSVSubst.
229 1.5 rillig # XXX: This is unexpected. Add more test case to demonstrate the effects
230 1.5 rillig # of removing one of the expansions.
231 1.5 rillig VALUE= value
232 1.5 rillig INDIRECT= 1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
233 1.5 rillig .if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"
234 1.5 rillig . error
235 1.5 rillig .endif
236 1.8 rillig
237 1.8 rillig all:
238