1 1.24 rillig # $NetBSD: varmod-sysv.mk,v 1.24 2025/03/30 00:35:52 rillig Exp $ 2 1.1 rillig # 3 1.13 rillig # Tests for the variable modifier ':from=to', 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.13 rillig # A typical use case for the modifier ':from=to' 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.13 rillig # The modifier ':from=to' is therefore often combined with the modifier ':M'. 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.13 rillig # Another use case for the modifier ':from=to' 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.13 rillig # matches. The same effect can be achieved with the modifier ':S,$,teen,'. 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.13 rillig # The modifier ':from=to' 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.13 rillig # shell, but that's the job of the modifier ':Q'. 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.13 rillig # When the modifier ':from=to' is parsed, it lasts until the closing brace 46 1.13 rillig # or parenthesis. The ':Q' in the below expression may look like a modifier 47 1.13 rillig # but it 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.16 rillig # In the modifier ':from=to', both parts can contain 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.13 rillig # In the modifier ':from=to', 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.13 rillig # In the modifier ':from=to', 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.16 rillig # of words in the 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.13 rillig # single empty word, or no word at all. The modifier ':from=to' treats it as 79 1.11 rillig # no word at all. 80 1.12 rillig # 81 1.12 rillig # See SysVMatch, which doesn't handle w_len == p_len specially. 82 1.9 rillig .if ${:L:=suffix} != "" 83 1.9 rillig . error 84 1.9 rillig .endif 85 1.9 rillig 86 1.11 rillig # If the variable value is empty, it is debatable whether it consists of a 87 1.12 rillig # single empty word (before 2020-05-06), or no word at all (since 2020-05-06). 88 1.12 rillig # 89 1.12 rillig # See SysVMatch, percent != NULL && w[0] == '\0'. 90 1.11 rillig .if ${:L:%=suffix} != "" 91 1.11 rillig . error 92 1.11 rillig .endif 93 1.11 rillig 94 1.3 rillig # Before 2020-07-19, an ampersand could be used in the replacement part 95 1.4 rillig # of a SysV substitution modifier, and it was replaced with the whole match, 96 1.13 rillig # just like in the modifier ':S'. 97 1.4 rillig # 98 1.4 rillig # This was probably a copy-and-paste mistake since the code for the SysV 99 1.13 rillig # modifier looked a lot like the code for the modifiers ':S' and ':C'. 100 1.4 rillig # The ampersand is not mentioned in the manual page. 101 1.4 rillig .if ${a.bcd.e:L:a.%=%} != "bcd.e" 102 1.4 rillig . error 103 1.4 rillig .endif 104 1.4 rillig # Before 2020-07-19, the result of the expression was "a.bcd.e". 105 1.4 rillig .if ${a.bcd.e:L:a.%=&} != "&" 106 1.4 rillig . error 107 1.4 rillig .endif 108 1.3 rillig 109 1.3 rillig # Before 2020-07-20, when a SysV modifier was parsed, a single dollar 110 1.4 rillig # before the '=' was parsed (but not interpreted) as an anchor. 111 1.4 rillig # Parsing something without then evaluating it accordingly doesn't make 112 1.13 rillig # sense, so this has been fixed. 113 1.4 rillig .if ${value:L:e$=x} != "value" 114 1.4 rillig . error 115 1.4 rillig .endif 116 1.13 rillig # Before 2020-07-20, the modifier ':e$=x' was parsed as having a left-hand 117 1.13 rillig # side 'e' and a right-hand side 'x'. The dollar was parsed (but not 118 1.4 rillig # interpreted) as 'anchor at the end'. Therefore the modifier was equivalent 119 1.13 rillig # to ':e=x', which doesn't match the string "value$". Therefore the whole 120 1.4 rillig # expression evaluated to "value$". 121 1.4 rillig .if ${${:Uvalue\$}:L:e$=x} != "valux" 122 1.4 rillig . error 123 1.4 rillig .endif 124 1.4 rillig .if ${value:L:e=x} != "valux" 125 1.4 rillig . error 126 1.4 rillig .endif 127 1.3 rillig 128 1.3 rillig # Words that don't match are copied unmodified. 129 1.4 rillig .if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h" 130 1.4 rillig . error 131 1.4 rillig .endif 132 1.4 rillig 133 1.4 rillig # The % placeholder can be anywhere in the string, it doesn't have to be at 134 1.4 rillig # the beginning of the pattern. 135 1.4 rillig .if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c" 136 1.4 rillig . error 137 1.4 rillig .endif 138 1.3 rillig 139 1.9 rillig # It's also possible to modify each word by replacing the prefix and adding 140 1.9 rillig # a suffix. 141 1.9 rillig .if ${one two:L:o%=a%w} != "anew two" 142 1.9 rillig . error 143 1.9 rillig .endif 144 1.9 rillig 145 1.7 rillig # Each word gets the suffix "X" appended. 146 1.7 rillig .if ${one two:L:=X} != "oneX twoX" 147 1.7 rillig . error 148 1.7 rillig .endif 149 1.7 rillig 150 1.6 rillig # The suffix "o" is replaced with "X". 151 1.6 rillig .if ${one two:L:o=X} != "one twX" 152 1.4 rillig . error 153 1.4 rillig .endif 154 1.6 rillig 155 1.6 rillig # The suffix "o" is replaced with nothing. 156 1.6 rillig .if ${one two:L:o=} != "one tw" 157 1.4 rillig . error 158 1.4 rillig .endif 159 1.6 rillig 160 1.6 rillig # The suffix "o" is replaced with a literal percent. The percent is only 161 1.6 rillig # a wildcard when it appears on the left-hand side. 162 1.6 rillig .if ${one two:L:o=%} != "one tw%" 163 1.4 rillig . error 164 1.4 rillig .endif 165 1.6 rillig 166 1.6 rillig # Each word with the suffix "o" is replaced with "X". The percent is a 167 1.6 rillig # wildcard even though the right-hand side does not contain another percent. 168 1.6 rillig .if ${one two:L:%o=X} != "one X" 169 1.4 rillig . error 170 1.4 rillig .endif 171 1.6 rillig 172 1.6 rillig # Each word with the prefix "o" is replaced with "X". The percent is a 173 1.6 rillig # wildcard even though the right-hand side does not contain another percent. 174 1.6 rillig .if ${one two:L:o%=X} != "X two" 175 1.4 rillig . error 176 1.4 rillig .endif 177 1.6 rillig 178 1.6 rillig # For each word with the prefix "o" and the suffix "e", the whole word is 179 1.6 rillig # replaced with "X". 180 1.6 rillig .if ${one two oe oxen:L:o%e=X} != "X two X oxen" 181 1.4 rillig . error 182 1.4 rillig .endif 183 1.6 rillig 184 1.6 rillig # Only the first '%' is the wildcard. 185 1.6 rillig .if ${one two o%e other%e:L:o%%e=X} != "one two X X" 186 1.4 rillig . error 187 1.4 rillig .endif 188 1.6 rillig 189 1.6 rillig # In the replacement, only the first '%' is the placeholder, all others 190 1.6 rillig # are literal percent characters. 191 1.6 rillig .if ${one two:L:%=%%} != "one% two%" 192 1.4 rillig . error 193 1.4 rillig .endif 194 1.6 rillig 195 1.6 rillig # In the word "one", only a prefix of the pattern suffix "nes" matches, 196 1.6 rillig # the whole word is too short. Therefore it doesn't match. 197 1.6 rillig .if ${one two:L:%nes=%xxx} != "one two" 198 1.4 rillig . error 199 1.3 rillig .endif 200 1.5 rillig 201 1.13 rillig # The modifier ':from=to' can be used to replace both the prefix and a suffix 202 1.9 rillig # of a word with other strings. This is not possible with a single :S 203 1.9 rillig # modifier, and using a :C modifier for the same task looks more complicated 204 1.9 rillig # in many cases. 205 1.9 rillig .if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s" 206 1.9 rillig . error 207 1.9 rillig .endif 208 1.9 rillig 209 1.16 rillig # This is not a SysV modifier since the nested expression expands 210 1.10 rillig # to an empty string. The '=' in it should be irrelevant during parsing. 211 1.18 rillig # XXX: As of 2024-06-30, this expression generates an "Unfinished modifier" 212 1.12 rillig # error, while the correct error message would be "Unknown modifier" since 213 1.12 rillig # there is no modifier named "fromto". 214 1.23 rillig # expect+1: Unfinished modifier after "from${:D=}to}", expecting "=" 215 1.18 rillig .if ${word216:L:from${:D=}to} 216 1.10 rillig . error 217 1.10 rillig .endif 218 1.10 rillig 219 1.10 rillig # XXX: This specially constructed case demonstrates that the SysV modifier 220 1.10 rillig # lasts longer than expected. The whole expression initially has the value 221 1.10 rillig # "fromto}...". The next modifier is a SysV modifier. ApplyModifier_SysV 222 1.10 rillig # parses the modifier as "from${:D=}to", ending at the '}'. Next, the two 223 1.10 rillig # parts of the modifier are parsed using ParseModifierPart, which scans 224 1.16 rillig # differently, properly handling nested expressions. The two parts 225 1.10 rillig # are now "fromto}..." and "replaced". 226 1.10 rillig .if "${:Ufromto\}...:from${:D=}to}...=replaced}" != "replaced" 227 1.10 rillig . error 228 1.10 rillig .endif 229 1.10 rillig 230 1.5 rillig # As of 2020-10-06, the right-hand side of the SysV modifier is expanded 231 1.5 rillig # twice. The first expansion happens in ApplyModifier_SysV, where the 232 1.5 rillig # modifier is split into its two parts. The second expansion happens 233 1.5 rillig # when each word is replaced in ModifyWord_SYSVSubst. 234 1.5 rillig # XXX: This is unexpected. Add more test case to demonstrate the effects 235 1.5 rillig # of removing one of the expansions. 236 1.5 rillig VALUE= value 237 1.5 rillig INDIRECT= 1:${VALUE} 2:$${VALUE} 4:$$$${VALUE} 238 1.5 rillig .if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}" 239 1.5 rillig . error 240 1.5 rillig .endif 241 1.8 rillig 242 1.13 rillig # Test all relevant combinations of prefix, '%' and suffix in both the pattern 243 1.13 rillig # and the replacement. 244 1.13 rillig !=1>&2 printf '%-24s %-24s %-24s\n' 'word' 'modifier' 'result' 245 1.13 rillig .for from in '' ffix % pre% %ffix pre%ffix 246 1.13 rillig . for to in '' NS % %NS NPre% NPre%NS 247 1.13 rillig . for word in '' suffix prefix pre-middle-suffix 248 1.13 rillig . for mod in ${from:N''}=${to:N''} 249 1.14 rillig !=1>&2 printf '%-24s %-24s "%s"\n' ''${word:Q} ''${mod:Q} ''${word:N'':${mod}:Q} 250 1.13 rillig . endfor 251 1.13 rillig . endfor 252 1.13 rillig . endfor 253 1.13 rillig .endfor 254 1.13 rillig 255 1.17 rillig 256 1.17 rillig # The error case of an unfinished ':from=to' modifier after the '=' requires 257 1.17 rillig # an expression that is missing the closing '}'. 258 1.23 rillig # expect+1: Unfinished modifier after "$(})", expecting "}" 259 1.17 rillig .if ${error:L:from=$(}) 260 1.17 rillig .endif 261 1.17 rillig 262 1.17 rillig 263 1.24 rillig # The various ":t..." modifiers fall back to the ":from=to" modifier. 264 1.24 rillig .if ${:Utarget:target=source} != "source" 265 1.24 rillig . error 266 1.24 rillig .endif 267 1.24 rillig .if ${:Ufile.ts:ts=js} != "file.js" 268 1.24 rillig . error 269 1.24 rillig .endif 270 1.24 rillig .if ${:Ufile.tsx:tsx=jsx} != "file.jsx" 271 1.24 rillig . error 272 1.24 rillig .endif 273 1.24 rillig .if ${:Ufile.ts\\part:ts\part=replaced} != "file.replaced" 274 1.24 rillig . error 275 1.24 rillig .endif 276 1.24 rillig .if ${:Ufile.ts\\123xyz:ts\123xyz=gone} != "file.gone" 277 1.24 rillig . error 278 1.24 rillig .endif 279 1.24 rillig # Since the ":ts=" modifier is a valid form of the ":ts" modifier, don't fall 280 1.24 rillig # back to the ":from=to" modifier. 281 1.24 rillig .if ${:U1 2 3 file.ts:ts=} != "1=2=3=file.ts" 282 1.24 rillig . error 283 1.24 rillig .endif 284 1.24 rillig 285 1.24 rillig 286 1.8 rillig all: 287