1# $NetBSD: varmod-sysv.mk,v 1.14 2021/04/12 16:09:57 rillig Exp $
2#
3# Tests for the variable modifier ':from=to', which replaces the suffix
4# "from" with "to".  It can also use '%' as a wildcard.
5#
6# This modifier is applied when the other modifiers don't match exactly.
7#
8# See ApplyModifier_SysV.
9
10# A typical use case for the modifier ':from=to' is conversion of filename
11# extensions.
12.if ${src.c:L:.c=.o} != "src.o"
13.  error
14.endif
15
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"
18.  error
19.endif
20
21# Words that don't match the pattern are passed unmodified.
22.if ${src.c src.h:L:.c=.o} != "src.o src.h"
23.  error
24.endif
25
26# The modifier ':from=to' is therefore often combined with the modifier ':M'.
27.if ${src.c src.h:L:M*.c:.c=.o} != "src.o"
28.  error
29.endif
30
31# Another use case for the modifier ':from=to' 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 modifier ':S,$,teen,'.
34.if ${four six seven nine:L:=teen} != "fourteen sixteen seventeen nineteen"
35.  error
36.endif
37
38# The modifier ':from=to' 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 modifier ':Q'.
41.if ${one two three:L:%=(%)} != "(one) (two) (three)"
42.  error
43.endif
44
45# When the modifier ':from=to' is parsed, it lasts until the closing brace
46# or parenthesis.  The ':Q' in the below expression may look like a modifier
47# but it 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"
49.  error
50.endif
51
52# In the modifier ':from=to', both parts can contain variable expressions.
53.if ${one two:L:${:Uone}=${:U1}} != "1 two"
54.  error
55.endif
56
57# In the modifier ':from=to', the "from" part is expanded exactly once.
58.if ${:U\$ \$\$ \$\$\$\$:${:U\$\$\$\$}=4} != "\$ \$\$ 4"
59.  error
60.endif
61
62# In the modifier ':from=to', 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
66#      the opening brace.
67.if ${:U1 2 4:4=${:Uonce\${\:Utwice\}}} != "1 2 oncetwice"
68.  error
69.endif
70
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"
74.  error
75.endif
76
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 modifier ':from=to' treats it as
79# no word at all.
80#
81# See SysVMatch, which doesn't handle w_len == p_len specially.
82.if ${:L:=suffix} != ""
83.  error
84.endif
85
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).
88#
89# See SysVMatch, percent != NULL && w[0] == '\0'.
90.if ${:L:%=suffix} != ""
91.  error
92.endif
93
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 modifier ':S'.
97#
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 modifiers ':S' and ':C'.
100# The ampersand is not mentioned in the manual page.
101.if ${a.bcd.e:L:a.%=%} != "bcd.e"
102.  error
103.endif
104# Before 2020-07-19, the result of the expression was "a.bcd.e".
105.if ${a.bcd.e:L:a.%=&} != "&"
106.  error
107.endif
108
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
112# sense, so this has been fixed.
113.if ${value:L:e$=x} != "value"
114.  error
115.endif
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"
122.  error
123.endif
124.if ${value:L:e=x} != "valux"
125.  error
126.endif
127
128# Words that don't match are copied unmodified.
129.if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
130.  error
131.endif
132
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"
136.  error
137.endif
138
139# It's also possible to modify each word by replacing the prefix and adding
140# a suffix.
141.if ${one two:L:o%=a%w} != "anew two"
142.  error
143.endif
144
145# Each word gets the suffix "X" appended.
146.if ${one two:L:=X} != "oneX twoX"
147.  error
148.endif
149
150# The suffix "o" is replaced with "X".
151.if ${one two:L:o=X} != "one twX"
152.  error
153.endif
154
155# The suffix "o" is replaced with nothing.
156.if ${one two:L:o=} != "one tw"
157.  error
158.endif
159
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%"
163.  error
164.endif
165
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"
169.  error
170.endif
171
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"
175.  error
176.endif
177
178# For each word with the prefix "o" and the suffix "e", the whole word is
179# replaced with "X".
180.if ${one two oe oxen:L:o%e=X} != "X two X oxen"
181.  error
182.endif
183
184# Only the first '%' is the wildcard.
185.if ${one two o%e other%e:L:o%%e=X} != "one two X X"
186.  error
187.endif
188
189# In the replacement, only the first '%' is the placeholder, all others
190# are literal percent characters.
191.if ${one two:L:%=%%} != "one% two%"
192.  error
193.endif
194
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"
198.  error
199.endif
200
201# The modifier ':from=to' 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
204# in many cases.
205.if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s"
206.  error
207.endif
208
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}
215.  error
216.endif
217
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"
226.  error
227.endif
228
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.
235VALUE=		value
236INDIRECT=	1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
237.if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"
238.  error
239.endif
240
241# Test all relevant combinations of prefix, '%' and suffix in both the pattern
242# and the replacement.
243!=1>&2	printf '%-24s %-24s %-24s\n' 'word' 'modifier' 'result'
244.for from in '' ffix % pre% %ffix pre%ffix
245.  for to in '' NS % %NS NPre% NPre%NS
246.    for word in '' suffix prefix pre-middle-suffix
247.      for mod in ${from:N''}=${to:N''}
248!=1>&2	printf '%-24s %-24s "%s"\n' ''${word:Q} ''${mod:Q} ''${word:N'':${mod}:Q}
249.      endfor
250.    endfor
251.  endfor
252.endfor
253
254all:
255