varmod-match.mk revision 1.20 1 1.20 rillig # $NetBSD: varmod-match.mk,v 1.20 2023/12/17 23:19:02 rillig Exp $
2 1.1 rillig #
3 1.18 rillig # Tests for the ':M' modifier, which keeps only those words that match the
4 1.2 rillig # given pattern.
5 1.4 rillig #
6 1.18 rillig # Table of contents
7 1.18 rillig #
8 1.18 rillig # 1. Pattern characters '*', '?' and '\'
9 1.18 rillig # 2. Character lists and character ranges
10 1.18 rillig # 3. Parsing and escaping
11 1.18 rillig # 4. Interaction with other modifiers
12 1.18 rillig # 5. Performance
13 1.18 rillig # 6. Error handling
14 1.18 rillig # 7. Historical bugs
15 1.19 rillig #
16 1.19 rillig # See ApplyModifier_Match, ParseModifier_Match, ModifyWord_Match and
17 1.19 rillig # Str_Match.
18 1.3 rillig
19 1.3 rillig
20 1.18 rillig # 1. Pattern characters '*', '?' and '\'
21 1.5 rillig #
22 1.18 rillig # * matches 0 or more characters
23 1.18 rillig # ? matches 1 character
24 1.18 rillig # \x matches the character 'x'
25 1.3 rillig
26 1.19 rillig # The pattern is anchored both at the beginning and at the end of the word.
27 1.19 rillig # Since the pattern 'e' does not contain any pattern matching characters, it
28 1.19 rillig # matches exactly the word 'e', twice.
29 1.19 rillig .if ${a c e aa cc ee e f g:L:Me} != "e e"
30 1.19 rillig . error
31 1.19 rillig .endif
32 1.19 rillig
33 1.19 rillig # The pattern character '?' matches exactly 1 character, the pattern character
34 1.19 rillig # '*' matches 0 or more characters. The whole pattern matches all words that
35 1.19 rillig # start with 's' and have 3 or more characters.
36 1.19 rillig .if ${One Two Three Four five six seven:L:Ms??*} != "six seven"
37 1.19 rillig . error
38 1.19 rillig .endif
39 1.19 rillig
40 1.19 rillig # Ensure that a pattern without placeholders only matches itself.
41 1.19 rillig .if ${a aa aaa b ba baa bab:L:Ma} != "a"
42 1.19 rillig . error
43 1.19 rillig .endif
44 1.19 rillig
45 1.19 rillig # Ensure that a pattern that ends with '*' is properly anchored at the
46 1.19 rillig # beginning.
47 1.19 rillig .if ${a aa aaa b ba baa bab:L:Ma*} != "a aa aaa"
48 1.19 rillig . error
49 1.19 rillig .endif
50 1.19 rillig
51 1.19 rillig # Ensure that a pattern that starts with '*' is properly anchored at the end.
52 1.19 rillig .if ${a aa aaa b ba baa bab:L:M*a} != "a aa aaa ba baa"
53 1.19 rillig . error
54 1.19 rillig .endif
55 1.4 rillig
56 1.20 rillig # Test the fast code path for '*' followed by a regular character.
57 1.20 rillig .if ${:U file.c file.*c file.h file\.c :M*.c} != "file.c file\\.c"
58 1.20 rillig . error
59 1.20 rillig .endif
60 1.20 rillig # Ensure that the fast code path correctly handles the backslash.
61 1.20 rillig .if ${:U file.c file.*c file.h file\.c :M*\.c} != "file.c file\\.c"
62 1.20 rillig . error
63 1.20 rillig .endif
64 1.20 rillig # Ensure that the fast code path correctly handles '\*'.
65 1.20 rillig .if ${:U file.c file.*c file.h file\.c :M*\*c} != "file.*c"
66 1.20 rillig . error
67 1.20 rillig .endif
68 1.20 rillig # Ensure that the partial match '.c' doesn't confuse the fast code path.
69 1.20 rillig .if ${:U file.c.cc file.cc.cc file.cc.c :M*.cc} != "file.c.cc file.cc.cc"
70 1.20 rillig . error
71 1.20 rillig .endif
72 1.20 rillig # Ensure that the substring '.cc' doesn't confuse the fast code path for '.c'.
73 1.20 rillig .if ${:U file.c.cc file.cc.cc file.cc.c :M*.c} != "file.cc.c"
74 1.20 rillig . error
75 1.20 rillig .endif
76 1.20 rillig
77 1.10 rillig
78 1.18 rillig # 2. Character lists and character ranges
79 1.5 rillig #
80 1.18 rillig # [...] matches 1 character from the listed characters
81 1.18 rillig # [^...] matches 1 character from the unlisted characters
82 1.18 rillig # [a-z] matches 1 character from the range 'a' to 'z'
83 1.18 rillig # [z-a] matches 1 character from the range 'a' to 'z'
84 1.18 rillig
85 1.18 rillig # Only keep words that start with an uppercase letter.
86 1.19 rillig .if ${One Two Three Four five six seven:L:M[A-Z]*} != "One Two Three Four"
87 1.4 rillig . error
88 1.4 rillig .endif
89 1.4 rillig
90 1.18 rillig # Only keep words that start with a character other than an uppercase letter.
91 1.19 rillig .if ${One Two Three Four five six seven:L:M[^A-Z]*} != "five six seven"
92 1.4 rillig . error
93 1.4 rillig .endif
94 1.4 rillig
95 1.7 rillig # [] matches never
96 1.7 rillig .if ${ ab a[]b a[b a b :L:M[]} != ""
97 1.7 rillig . error
98 1.7 rillig .endif
99 1.7 rillig
100 1.7 rillig # a[]b matches never
101 1.7 rillig .if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
102 1.7 rillig . error
103 1.7 rillig .endif
104 1.7 rillig
105 1.7 rillig # [^] matches exactly 1 arbitrary character
106 1.7 rillig .if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
107 1.7 rillig . error
108 1.7 rillig .endif
109 1.7 rillig
110 1.7 rillig # a[^]b matches 'a', then exactly 1 arbitrary character, then 'b'
111 1.7 rillig .if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
112 1.7 rillig . error
113 1.7 rillig .endif
114 1.7 rillig
115 1.7 rillig # [Nn0] matches exactly 1 character from the set 'N', 'n', '0'
116 1.7 rillig .if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
117 1.7 rillig . error
118 1.7 rillig .endif
119 1.7 rillig
120 1.7 rillig # [a-c] matches exactly 1 character from the range 'a' to 'c'
121 1.7 rillig .if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
122 1.7 rillig . error
123 1.7 rillig .endif
124 1.7 rillig
125 1.7 rillig # [c-a] matches the same as [a-c]
126 1.7 rillig .if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
127 1.7 rillig . error
128 1.7 rillig .endif
129 1.7 rillig
130 1.7 rillig # [^a-c67]
131 1.8 rillig # matches a single character, except for 'a', 'b', 'c', '6' or
132 1.8 rillig # '7'
133 1.7 rillig .if ${ A B C a b c d 5 6 7 8 [a-c] [a] :L:M[^a-c67]} != "A B C d 5 8"
134 1.7 rillig . error
135 1.7 rillig .endif
136 1.7 rillig
137 1.18 rillig # [\] matches a single backslash; no escaping takes place in
138 1.18 rillig # character ranges
139 1.18 rillig # Without the 'b' in the below words, the backslash would end a word and thus
140 1.18 rillig # influence how the string is split into words.
141 1.18 rillig WORDS= a\b a[\]b ab a\\b
142 1.11 rillig .if ${WORDS:Ma[\]b} != "a\\b"
143 1.11 rillig . error
144 1.11 rillig .endif
145 1.11 rillig
146 1.18 rillig # [[-]] May look like it would match a single '[', '\' or ']', but
147 1.18 rillig # the inner ']' has two roles: it is the upper bound of the
148 1.18 rillig # character range as well as the closing character of the
149 1.18 rillig # character list. The outer ']' is just a regular character.
150 1.18 rillig WORDS= [ ] [] \] ]]
151 1.18 rillig .if ${WORDS:M[[-]]} != "[] \\] ]]"
152 1.18 rillig . error
153 1.18 rillig .endif
154 1.18 rillig
155 1.18 rillig # [b[-]a]
156 1.18 rillig # Same as for '[[-]]': the character list stops at the first
157 1.18 rillig # ']', and the 'a]' is treated as a literal string.
158 1.18 rillig WORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba]
159 1.18 rillig .if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]"
160 1.18 rillig . error
161 1.18 rillig .endif
162 1.18 rillig
163 1.18 rillig # [-] Matches a single '-' since the '-' only becomes part of a
164 1.18 rillig # character range if it is preceded and followed by another
165 1.18 rillig # character.
166 1.18 rillig WORDS= - -]
167 1.18 rillig .if ${WORDS:M[-]} != "-"
168 1.18 rillig . error
169 1.18 rillig .endif
170 1.18 rillig
171 1.18 rillig # Only keep words that don't start with s and at the same time end with
172 1.18 rillig # either of [ex].
173 1.18 rillig #
174 1.19 rillig # This test case ensures that the negation from the first character list
175 1.19 rillig # '[^s]' does not propagate to the second character list '[ex]'.
176 1.19 rillig .if ${One Two Three Four five six seven:L:M[^s]*[ex]} != "One Three five"
177 1.18 rillig . error
178 1.18 rillig .endif
179 1.18 rillig
180 1.18 rillig
181 1.18 rillig # 3. Parsing and escaping
182 1.18 rillig #
183 1.18 rillig # * matches 0 or more characters
184 1.18 rillig # ? matches 1 character
185 1.18 rillig # \ outside a character list, escapes the following character
186 1.18 rillig # [ starts a character list for matching 1 character
187 1.18 rillig # ] ends a character list for matching 1 character
188 1.18 rillig # - in a character list, forms a character range
189 1.18 rillig # ^ at the beginning of a character list, negates the list
190 1.18 rillig # ( while parsing the pattern, starts a nesting level
191 1.18 rillig # ) while parsing the pattern, ends a nesting level
192 1.18 rillig # { while parsing the pattern, starts a nesting level
193 1.18 rillig # } while parsing the pattern, ends a nesting level
194 1.18 rillig # : while parsing the pattern, terminates the pattern
195 1.18 rillig # $ while parsing the pattern, starts a nested expression
196 1.18 rillig # # in a line except a shell command, starts a comment
197 1.18 rillig
198 1.18 rillig # The pattern can come from an expression. For single-letter
199 1.18 rillig # variables, either the short form or the long form can be used, just as
200 1.18 rillig # everywhere else.
201 1.18 rillig PRIMES= 2 3 5 7 11
202 1.18 rillig n= 2
203 1.18 rillig .if ${PRIMES:M$n} != "2"
204 1.18 rillig . error
205 1.18 rillig .endif
206 1.18 rillig .if ${PRIMES:M${n}} != "2"
207 1.18 rillig . error
208 1.18 rillig .endif
209 1.18 rillig .if ${PRIMES:M${:U2}} != "2"
210 1.18 rillig . error
211 1.18 rillig .endif
212 1.18 rillig
213 1.7 rillig # : terminates the pattern
214 1.7 rillig .if ${ A * :L:M:} != ""
215 1.7 rillig . error
216 1.7 rillig .endif
217 1.7 rillig
218 1.7 rillig # \: matches a colon
219 1.7 rillig .if ${ ${:U\: \:\:} :L:M\:} != ":"
220 1.7 rillig . error
221 1.7 rillig .endif
222 1.7 rillig
223 1.7 rillig # ${:U\:} matches a colon
224 1.7 rillig .if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
225 1.7 rillig . error
226 1.7 rillig .endif
227 1.7 rillig
228 1.18 rillig # To match a dollar sign in a word, double it.
229 1.18 rillig #
230 1.19 rillig # This is different from the :S and :C modifiers, where a '$' has to be
231 1.19 rillig # escaped as '\$'.
232 1.18 rillig .if ${:Ua \$ sign:M*$$*} != "\$"
233 1.7 rillig . error
234 1.18 rillig .endif
235 1.18 rillig
236 1.18 rillig # In the :M modifier, '\$' does not escape a dollar. Instead it is
237 1.18 rillig # interpreted as a backslash followed by whatever expression the
238 1.18 rillig # '$' starts.
239 1.18 rillig #
240 1.19 rillig # This differs from the :S, :C and several other modifiers.
241 1.18 rillig ${:U*}= asterisk
242 1.18 rillig .if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
243 1.7 rillig . error
244 1.7 rillig .endif
245 1.7 rillig
246 1.18 rillig # TODO: ${VAR:M(((}}}}
247 1.18 rillig # TODO: ${VAR:M{{{)))}
248 1.18 rillig # TODO: ${VAR:M${UNBALANCED}}
249 1.18 rillig # TODO: ${VAR:M${:U(((\}\}\}}}
250 1.18 rillig
251 1.18 rillig
252 1.18 rillig # 4. Interaction with other modifiers
253 1.18 rillig
254 1.18 rillig # The modifier ':tW' prevents splitting at whitespace. Even leading and
255 1.18 rillig # trailing whitespace is preserved.
256 1.18 rillig .if ${ plain string :L:tW:M*} != " plain string "
257 1.11 rillig . error
258 1.11 rillig .endif
259 1.11 rillig
260 1.18 rillig # Without the modifier ':tW', the string is split into words. All whitespace
261 1.18 rillig # around and between the words is normalized to a single space.
262 1.18 rillig .if ${ plain string :L:M*} != "plain string"
263 1.11 rillig . error
264 1.11 rillig .endif
265 1.11 rillig
266 1.18 rillig
267 1.18 rillig # 5. Performance
268 1.18 rillig
269 1.18 rillig # Before 2020-06-13, this expression called Str_Match 601,080,390 times.
270 1.18 rillig # Since 2020-06-13, this expression calls Str_Match 1 time.
271 1.18 rillig .if ${:U****************:M****************b}
272 1.11 rillig .endif
273 1.11 rillig
274 1.18 rillig # Before 2023-06-22, this expression called Str_Match 2,621,112 times.
275 1.18 rillig # Adding another '*?' to the pattern called Str_Match 20,630,572 times.
276 1.18 rillig # Adding another '*?' to the pattern called Str_Match 136,405,672 times.
277 1.18 rillig # Adding another '*?' to the pattern called Str_Match 773,168,722 times.
278 1.18 rillig # Adding another '*?' to the pattern called Str_Match 3,815,481,072 times.
279 1.18 rillig # Since 2023-06-22, Str_Match no longer backtracks.
280 1.18 rillig .if ${:U..................................................b:M*?*?*?*?*?a}
281 1.11 rillig .endif
282 1.11 rillig
283 1.18 rillig
284 1.18 rillig # 6. Error handling
285 1.18 rillig
286 1.11 rillig # [ Incomplete empty character list, never matches.
287 1.11 rillig WORDS= a a[
288 1.15 rillig # expect+1: warning: Unfinished character list in pattern 'a[' of modifier ':M'
289 1.11 rillig .if ${WORDS:Ma[} != ""
290 1.11 rillig . error
291 1.11 rillig .endif
292 1.11 rillig
293 1.11 rillig # [^ Incomplete negated empty character list, matches any single
294 1.11 rillig # character.
295 1.11 rillig WORDS= a a[ aX
296 1.15 rillig # expect+1: warning: Unfinished character list in pattern 'a[^' of modifier ':M'
297 1.11 rillig .if ${WORDS:Ma[^} != "a[ aX"
298 1.7 rillig . error
299 1.7 rillig .endif
300 1.7 rillig
301 1.11 rillig # [-x1-3 Incomplete character list, matches those elements that can be
302 1.11 rillig # parsed without lookahead.
303 1.11 rillig WORDS= - + x xx 0 1 2 3 4 [x1-3
304 1.15 rillig # expect+1: warning: Unfinished character list in pattern '[-x1-3' of modifier ':M'
305 1.11 rillig .if ${WORDS:M[-x1-3} != "- x 1 2 3"
306 1.11 rillig . error
307 1.11 rillig .endif
308 1.11 rillig
309 1.14 rillig # *[-x1-3 Incomplete character list after a wildcard, matches those
310 1.14 rillig # words that end with one of the characters from the list.
311 1.14 rillig WORDS= - + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3
312 1.15 rillig # expect+1: warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M'
313 1.14 rillig .if ${WORDS:M*[-x1-3} != "- x xx 1 2 3 01 11 001 011 101 111 [x1-3"
314 1.14 rillig . warning ${WORDS:M*[-x1-3}
315 1.14 rillig .endif
316 1.14 rillig
317 1.11 rillig # [^-x1-3
318 1.11 rillig # Incomplete negated character list, matches any character
319 1.11 rillig # except those elements that can be parsed without lookahead.
320 1.11 rillig WORDS= - + x xx 0 1 2 3 4 [x1-3
321 1.15 rillig # expect+1: warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M'
322 1.11 rillig .if ${WORDS:M[^-x1-3} != "+ 0 4"
323 1.11 rillig . error
324 1.11 rillig .endif
325 1.11 rillig
326 1.11 rillig # [\ Incomplete character list containing a single '\'.
327 1.11 rillig #
328 1.11 rillig # A word can only end with a backslash if the preceding
329 1.11 rillig # character is a backslash as well; in all other cases the final
330 1.11 rillig # backslash would escape the following space, making the space
331 1.11 rillig # part of the word. Only the very last word of a string can be
332 1.11 rillig # '\', as there is no following space that could be escaped.
333 1.11 rillig WORDS= \\ \a ${:Ux\\}
334 1.18 rillig PATTERN= ${:U?[\\}
335 1.18 rillig # expect+1: warning: Unfinished character list in pattern '?[\' of modifier ':M'
336 1.18 rillig .if ${WORDS:M${PATTERN}} != "\\\\ x\\"
337 1.11 rillig . error
338 1.11 rillig .endif
339 1.11 rillig
340 1.11 rillig # [x- Incomplete character list containing an incomplete character
341 1.11 rillig # range, matches only the 'x'.
342 1.11 rillig WORDS= [x- x x- y
343 1.15 rillig # expect+1: warning: Unfinished character range in pattern '[x-' of modifier ':M'
344 1.11 rillig .if ${WORDS:M[x-} != "x"
345 1.11 rillig . error
346 1.11 rillig .endif
347 1.11 rillig
348 1.11 rillig # [^x- Incomplete negated character list containing an incomplete
349 1.11 rillig # character range; matches each word that does not have an 'x'
350 1.11 rillig # at the position of the character list.
351 1.11 rillig #
352 1.11 rillig # XXX: Even matches strings that are longer than a single
353 1.11 rillig # character.
354 1.11 rillig WORDS= [x- x x- y yyyyy
355 1.15 rillig # expect+1: warning: Unfinished character range in pattern '[^x-' of modifier ':M'
356 1.11 rillig .if ${WORDS:M[^x-} != "[x- y yyyyy"
357 1.11 rillig . error
358 1.11 rillig .endif
359 1.7 rillig
360 1.18 rillig # [:] matches never since the ':' starts the next modifier
361 1.18 rillig # expect+3: warning: Unfinished character list in pattern '[' of modifier ':M'
362 1.18 rillig # expect+2: Unknown modifier "]"
363 1.18 rillig # expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
364 1.18 rillig .if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
365 1.7 rillig . error
366 1.18 rillig .else
367 1.7 rillig . error
368 1.7 rillig .endif
369 1.9 rillig
370 1.9 rillig
371 1.18 rillig # 7. Historical bugs
372 1.12 rillig
373 1.12 rillig # Before var.c 1.1031 from 2022-08-24, the following expressions caused an
374 1.12 rillig # out-of-bounds read beyond the indirect ':M' modifiers.
375 1.12 rillig .if ${:U:${:UM\\}} # The ':M' pattern need not be unescaped, the
376 1.12 rillig . error # resulting pattern is '\', it never matches
377 1.12 rillig .endif # anything.
378 1.12 rillig .if ${:U:${:UM\\\:\\}} # The ':M' pattern must be unescaped, the
379 1.12 rillig . error # resulting pattern is ':\', it never matches
380 1.12 rillig .endif # anything.
381