1 # $NetBSD: varmod-sysv.mk,v 1.12 2020/12/05 13:01:33 rillig Exp $
3 # Tests for the ${VAR:from=to} variable modifier, which replaces the suffix
4 # "from" with "to". It can also use '%' as a wildcard.
6 # This modifier is applied when the other modifiers don't match exactly.
8 # See ApplyModifier_SysV.
10 # A typical use case for the :from=to modifier is conversion of filename
12 .if ${src.c:L:.c=.o} != "src.o"
16 # The modifier applies to each word on its own.
17 .if ${one.c two.c three.c:L:.c=.o} != "one.o two.o three.o"
21 # Words that don't match the pattern are passed unmodified.
22 .if ${src.c src.h:L:.c=.o} != "src.o src.h"
26 # The :from=to modifier is therefore often combined with the :M modifier.
27 .if ${src.c src.h:L:M*.c:.c=.o} != "src.o"
31 # Another use case for the :from=to modifier is to append a suffix to each
32 # word. In this case, the "from" string is empty, therefore it always
33 # matches. The same effect can be achieved with the :S,$,teen, modifier.
34 .if ${four six seven nine:L:=teen} != "fourteen sixteen seventeen nineteen"
38 # The :from=to modifier can also be used to surround each word by strings.
39 # It might be tempting to use this for enclosing a string in quotes for the
40 # shell, but that's the job of the :Q modifier.
41 .if ${one two three:L:%=(%)} != "(one) (two) (three)"
45 # When the :from=to modifier is parsed, it lasts until the closing brace
46 # or parenthesis. The :Q in the below expression may look like a modifier
47 # but isn't. It is part of the replacement string.
48 .if ${a b c d e:L:%a=x:Q} != "x:Q b c d e"
52 # In the :from=to modifier, both parts can contain variable expressions.
53 .if ${one two:L:${:Uone}=${:U1}} != "1 two"
57 # In the :from=to modifier, the "from" part is expanded exactly once.
58 .if ${:U\$ \$\$ \$\$\$\$:${:U\$\$\$\$}=4} != "\$ \$\$ 4"
62 # In the :from=to modifier, the "to" part is expanded exactly twice.
63 # XXX: The right-hand side should be expanded only once.
64 # XXX: It's hard to get the escaping correct here, and to read that.
65 # XXX: It's not intuitive why the closing brace must be escaped but not
67 .if ${:U1 2 4:4=${:Uonce\${\:Utwice\}}} != "1 2 oncetwice"
71 # The replacement string can contain spaces, thereby changing the number
72 # of words in the variable expression.
73 .if ${In:L:%=% ${:Uthe Sun}} != "In the Sun"
77 # If the variable value is empty, it is debatable whether it consists of a
78 # single empty word, or no word at all. The :from=to modifier treats it as
81 # See SysVMatch, which doesn't handle w_len == p_len specially.
82 .if ${:L:=suffix} != ""
86 # If the variable value is empty, it is debatable whether it consists of a
87 # single empty word (before 2020-05-06), or no word at all (since 2020-05-06).
89 # See SysVMatch, percent != NULL && w[0] == '\0'.
90 .if ${:L:%=suffix} != ""
94 # Before 2020-07-19, an ampersand could be used in the replacement part
95 # of a SysV substitution modifier, and it was replaced with the whole match,
96 # just like in the :S modifier.
98 # This was probably a copy-and-paste mistake since the code for the SysV
99 # modifier looked a lot like the code for the :S and :C modifiers.
100 # The ampersand is not mentioned in the manual page.
101 .if ${a.bcd.e:L:a.%=%} != "bcd.e"
104 # Before 2020-07-19, the result of the expression was "a.bcd.e".
105 .if ${a.bcd.e:L:a.%=&} != "&"
109 # Before 2020-07-20, when a SysV modifier was parsed, a single dollar
110 # before the '=' was parsed (but not interpreted) as an anchor.
111 # Parsing something without then evaluating it accordingly doesn't make
113 .if ${value:L:e$=x} != "value"
116 # Before 2020-07-20, the modifier ":e$=x" was parsed as having a left-hand
117 # side "e" and a right-hand side "x". The dollar was parsed (but not
118 # interpreted) as 'anchor at the end'. Therefore the modifier was equivalent
119 # to ":e=x", which doesn't match the string "value$". Therefore the whole
120 # expression evaluated to "value$".
121 .if ${${:Uvalue\$}:L:e$=x} != "valux"
124 .if ${value:L:e=x} != "valux"
128 # Words that don't match are copied unmodified.
129 .if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
133 # The % placeholder can be anywhere in the string, it doesn't have to be at
134 # the beginning of the pattern.
135 .if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c"
139 # It's also possible to modify each word by replacing the prefix and adding
141 .if ${one two:L:o%=a%w} != "anew two"
145 # Each word gets the suffix "X" appended.
146 .if ${one two:L:=X} != "oneX twoX"
150 # The suffix "o" is replaced with "X".
151 .if ${one two:L:o=X} != "one twX"
155 # The suffix "o" is replaced with nothing.
156 .if ${one two:L:o=} != "one tw"
160 # The suffix "o" is replaced with a literal percent. The percent is only
161 # a wildcard when it appears on the left-hand side.
162 .if ${one two:L:o=%} != "one tw%"
166 # Each word with the suffix "o" is replaced with "X". The percent is a
167 # wildcard even though the right-hand side does not contain another percent.
168 .if ${one two:L:%o=X} != "one X"
172 # Each word with the prefix "o" is replaced with "X". The percent is a
173 # wildcard even though the right-hand side does not contain another percent.
174 .if ${one two:L:o%=X} != "X two"
178 # For each word with the prefix "o" and the suffix "e", the whole word is
180 .if ${one two oe oxen:L:o%e=X} != "X two X oxen"
184 # Only the first '%' is the wildcard.
185 .if ${one two o%e other%e:L:o%%e=X} != "one two X X"
189 # In the replacement, only the first '%' is the placeholder, all others
190 # are literal percent characters.
191 .if ${one two:L:%=%%} != "one% two%"
195 # In the word "one", only a prefix of the pattern suffix "nes" matches,
196 # the whole word is too short. Therefore it doesn't match.
197 .if ${one two:L:%nes=%xxx} != "one two"
201 # The :from=to modifier can be used to replace both the prefix and a suffix
202 # of a word with other strings. This is not possible with a single :S
203 # modifier, and using a :C modifier for the same task looks more complicated
205 .if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s"
209 # This is not a SysV modifier since the nested variable expression expands
210 # to an empty string. The '=' in it should be irrelevant during parsing.
211 # XXX: As of 2020-12-05, this expression generates an "Unfinished modifier"
212 # error, while the correct error message would be "Unknown modifier" since
213 # there is no modifier named "fromto".
214 .if ${word214:L:from${:D=}to}
218 # XXX: This specially constructed case demonstrates that the SysV modifier
219 # lasts longer than expected. The whole expression initially has the value
220 # "fromto}...". The next modifier is a SysV modifier. ApplyModifier_SysV
221 # parses the modifier as "from${:D=}to", ending at the '}'. Next, the two
222 # parts of the modifier are parsed using ParseModifierPart, which scans
223 # differently, properly handling nested variable expressions. The two parts
224 # are now "fromto}..." and "replaced".
225 .if "${:Ufromto\}...:from${:D=}to}...=replaced}" != "replaced"
229 # As of 2020-10-06, the right-hand side of the SysV modifier is expanded
230 # twice. The first expansion happens in ApplyModifier_SysV, where the
231 # modifier is split into its two parts. The second expansion happens
232 # when each word is replaced in ModifyWord_SYSVSubst.
233 # XXX: This is unexpected. Add more test case to demonstrate the effects
234 # of removing one of the expansions.
236 INDIRECT= 1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
237 .if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"