modmisc.mk revision 1.26 1 1.26 rillig # $Id: modmisc.mk,v 1.26 2020/07/26 13:09:53 rillig Exp $
2 1.1 apb #
3 1.1 apb # miscellaneous modifier tests
4 1.1 apb
5 1.1 apb # do not put any dirs in this list which exist on some
6 1.1 apb # but not all target systems - an exists() check is below.
7 1.1 apb path=:/bin:/tmp::/:.:/no/such/dir:.
8 1.1 apb # strip cwd from path.
9 1.1 apb MOD_NODOT=S/:/ /g:N.:ts:
10 1.1 apb # and decorate, note that $'s need to be doubled. Also note that
11 1.1 apb # the modifier_variable can be used with other modifiers.
12 1.1 apb MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
13 1.1 apb # another mod - pretend it is more interesting
14 1.1 apb MOD_HOMES=S,/home/,/homes/,
15 1.1 apb MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
16 1.1 apb MOD_SEP=S,:, ,g
17 1.1 apb
18 1.7 rillig all: modvar modvarloop modsysv mod-HTE emptyvar undefvar
19 1.23 rillig all: mod-subst
20 1.23 rillig all: mod-regex
21 1.23 rillig all: mod-loop-varname mod-loop-resolve mod-loop-varname-dollar
22 1.12 rillig all: mod-subst-dollar mod-loop-dollar
23 1.23 rillig all: mod-regex-limits
24 1.23 rillig all: mod-regex-errors
25 1.16 rillig all: mod-assign
26 1.18 rillig all: mod-assign-nested
27 1.17 rillig all: mod-tu-space
28 1.23 rillig all: mod-quote
29 1.22 rillig all: mod-break-many-words
30 1.1 apb
31 1.23 rillig # See also sysv.mk.
32 1.1 apb modsysv:
33 1.1 apb @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
34 1.1 apb
35 1.23 rillig # Demonstrates modifiers that are given indirectly from a variable.
36 1.1 apb modvar:
37 1.1 apb @echo "path='${path}'"
38 1.1 apb @echo "path='${path:${MOD_NODOT}}'"
39 1.1 apb @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
40 1.1 apb @echo "path=${path:${MOD_NODOTX}:ts:}"
41 1.1 apb @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
42 1.1 apb
43 1.1 apb .for d in ${path:${MOD_SEP}:N.} /usr/xbin
44 1.1 apb path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
45 1.1 apb paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
46 1.1 apb .endfor
47 1.1 apb
48 1.1 apb modvarloop:
49 1.1 apb @echo "path_/usr/xbin=${path_/usr/xbin}"
50 1.1 apb @echo "paths=${paths}"
51 1.1 apb @echo "PATHS=${paths:tu}"
52 1.2 rillig
53 1.6 rillig PATHNAMES= a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
54 1.4 rillig mod-HTE:
55 1.6 rillig @echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
56 1.6 rillig @echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
57 1.6 rillig @echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
58 1.6 rillig @echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
59 1.7 rillig
60 1.7 rillig # When a modifier is applied to the "" variable, the result is discarded.
61 1.7 rillig emptyvar:
62 1.7 rillig @echo S:${:S,^$,empty,}
63 1.7 rillig @echo C:${:C,^$,empty,}
64 1.7 rillig @echo @:${:@var@${var}@}
65 1.7 rillig
66 1.7 rillig # The :U modifier turns even the "" variable into something that has a value.
67 1.7 rillig # The resulting variable is empty, but is still considered to contain a
68 1.7 rillig # single empty word. This word can be accessed by the :S and :C modifiers,
69 1.7 rillig # but not by the :@ modifier since it explicitly skips empty words.
70 1.7 rillig undefvar:
71 1.7 rillig @echo S:${:U:S,^$,empty,}
72 1.7 rillig @echo C:${:U:C,^$,empty,}
73 1.7 rillig @echo @:${:U:@var@empty@}
74 1.8 rillig
75 1.24 rillig WORDS= sequences of letters
76 1.26 rillig .if ${WORDS:S,,,} != ${WORDS}
77 1.26 rillig .warning The empty pattern matches something.
78 1.26 rillig .endif
79 1.25 rillig .if ${WORDS:S,e,*,1} != "s*quences of letters"
80 1.26 rillig .warning The :S modifier flag '1' is not applied exactly once.
81 1.24 rillig .endif
82 1.24 rillig .if ${WORDS:S,e,*,} != "s*quences of l*tters"
83 1.26 rillig .warning The :S modifier does not replace every first match per word.
84 1.24 rillig .endif
85 1.24 rillig .if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
86 1.26 rillig .warning The :S modifier flag 'g' does not replace every occurrence.
87 1.24 rillig .endif
88 1.24 rillig .if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
89 1.26 rillig .warning The :S modifier fails for a short match anchored at the start.
90 1.24 rillig .endif
91 1.24 rillig .if ${WORDS:S,^of,with,} != "sequences with letters"
92 1.26 rillig .warning The :S modifier fails for an exact match anchored at the start.
93 1.24 rillig .endif
94 1.24 rillig .if ${WORDS:S,^office,does not match,} != ${WORDS}
95 1.26 rillig .warning The :S modifier matches a too long pattern anchored at the start.
96 1.26 rillig .endif
97 1.26 rillig .if ${WORDS:S,f$,r,} != "sequences or letters"
98 1.26 rillig .warning The :S modifier fails for a short match anchored at the end.
99 1.26 rillig .endif
100 1.26 rillig .if ${WORDS:S,s$,,} != "sequence of letter"
101 1.26 rillig .warning The :S modifier fails to replace one occurrence per word.
102 1.26 rillig .endif
103 1.26 rillig .if ${WORDS:S,of$,,} != "sequences letters"
104 1.26 rillig .warning The :S modifier fails for an exact match anchored at the end.
105 1.26 rillig .endif
106 1.26 rillig .if ${WORDS:S,eof$,,} != ${WORDS}
107 1.26 rillig .warning The :S modifier matches a too long pattern anchored at the end.
108 1.26 rillig .endif
109 1.26 rillig .if ${WORDS:S,^of$,,} != "sequences letters"
110 1.26 rillig .warning The :S modifier does not match a word anchored at both ends.
111 1.26 rillig .endif
112 1.26 rillig .if ${WORDS:S,^o$,,} != ${WORDS}
113 1.26 rillig .warning The :S modifier matches a prefix anchored at both ends.
114 1.26 rillig .endif
115 1.26 rillig .if ${WORDS:S,^f$,,} != ${WORDS}
116 1.26 rillig .warning The :S modifier matches a suffix anchored at both ends.
117 1.26 rillig .endif
118 1.26 rillig .if ${WORDS:S,^eof$,,} != ${WORDS}
119 1.26 rillig .warning The :S modifier matches a too long prefix anchored at both ends.
120 1.26 rillig .endif
121 1.26 rillig .if ${WORDS:S,^office$,,} != ${WORDS}
122 1.26 rillig .warning The :S modifier matches a too long suffix anchored at both ends.
123 1.24 rillig .endif
124 1.24 rillig
125 1.23 rillig mod-subst:
126 1.20 rillig @echo $@:
127 1.8 rillig @echo :${:Ua b b c:S,a b,,:Q}:
128 1.8 rillig @echo :${:Ua b b c:S,a b,,1:Q}:
129 1.8 rillig @echo :${:Ua b b c:S,a b,,W:Q}:
130 1.8 rillig @echo :${:Ua b b c:S,b,,g:Q}:
131 1.8 rillig @echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
132 1.20 rillig @echo ${:U12345:S,,sep,g:Q}
133 1.8 rillig
134 1.23 rillig mod-regex:
135 1.20 rillig @echo $@:
136 1.8 rillig @echo :${:Ua b b c:C,a b,,:Q}:
137 1.8 rillig @echo :${:Ua b b c:C,a b,,1:Q}:
138 1.8 rillig @echo :${:Ua b b c:C,a b,,W:Q}:
139 1.8 rillig @echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
140 1.8 rillig @echo :${:Ua b b c:C,b,,g:Q}:
141 1.8 rillig @echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
142 1.9 rillig
143 1.9 rillig # In the :@ modifier, the name of the loop variable can even be generated
144 1.9 rillig # dynamically. There's no practical use-case for this, and hopefully nobody
145 1.9 rillig # will ever depend on this, but technically it's possible.
146 1.23 rillig mod-loop-varname:
147 1.9 rillig @echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
148 1.10 rillig
149 1.10 rillig # The :@ modifier resolves the variables a little more often than expected.
150 1.10 rillig # In particular, it resolves _all_ variables from the context, and not only
151 1.10 rillig # the loop variable (in this case v).
152 1.11 rillig #
153 1.11 rillig # The d means direct reference, the i means indirect reference.
154 1.10 rillig RESOLVE= ${RES1} $${RES1}
155 1.11 rillig RES1= 1d${RES2} 1i$${RES2}
156 1.11 rillig RES2= 2d${RES3} 2i$${RES3}
157 1.11 rillig RES3= 3
158 1.10 rillig
159 1.23 rillig mod-loop-resolve:
160 1.10 rillig @echo $@:${RESOLVE:@v@w${v}w@:Q}:
161 1.12 rillig
162 1.23 rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
163 1.23 rillig # or two dollar signs, which were silently ignored.
164 1.23 rillig # There's no point in allowing a dollar sign in that position.
165 1.23 rillig mod-loop-varname-dollar:
166 1.13 rillig @echo $@:${1 2 3:L:@v$@($v)@:Q}.
167 1.13 rillig @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
168 1.13 rillig @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
169 1.13 rillig
170 1.12 rillig # No matter how many dollar characters there are, they all get merged
171 1.12 rillig # into a single dollar by the :S modifier.
172 1.12 rillig mod-subst-dollar:
173 1.12 rillig @echo $@:${:U1:S,^,$,:Q}:
174 1.12 rillig @echo $@:${:U2:S,^,$$,:Q}:
175 1.12 rillig @echo $@:${:U3:S,^,$$$,:Q}:
176 1.12 rillig @echo $@:${:U4:S,^,$$$$,:Q}:
177 1.12 rillig @echo $@:${:U5:S,^,$$$$$,:Q}:
178 1.12 rillig @echo $@:${:U6:S,^,$$$$$$,:Q}:
179 1.12 rillig @echo $@:${:U7:S,^,$$$$$$$,:Q}:
180 1.12 rillig @echo $@:${:U8:S,^,$$$$$$$$,:Q}:
181 1.12 rillig # This generates no dollar at all:
182 1.12 rillig @echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
183 1.12 rillig # Here is an alternative way to generate dollar characters.
184 1.12 rillig # It's unexpectedly complicated though.
185 1.12 rillig @echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
186 1.12 rillig
187 1.12 rillig # Demonstrate that it is possible to generate dollar characters using the
188 1.12 rillig # :@ modifier.
189 1.12 rillig #
190 1.12 rillig # These are edge cases that could have resulted in a parse error as well
191 1.12 rillig # since the $@ at the end could have been interpreted as a variable, which
192 1.12 rillig # would mean a missing closing @ delimiter.
193 1.12 rillig mod-loop-dollar:
194 1.12 rillig @echo $@:${:U1:@word@${word}$@:Q}:
195 1.12 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
196 1.12 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
197 1.12 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
198 1.12 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
199 1.12 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
200 1.14 rillig
201 1.23 rillig mod-regex-limits:
202 1.14 rillig @echo $@:00-ok:${:U1 23 456:C,..,\0\0,:Q}
203 1.14 rillig @echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
204 1.14 rillig @echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
205 1.14 rillig @echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
206 1.14 rillig @echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
207 1.14 rillig @echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
208 1.15 rillig # The :C modifier only handles single-digit capturing groups,
209 1.15 rillig # which is more than enough for daily use.
210 1.15 rillig @echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
211 1.16 rillig
212 1.23 rillig mod-regex-errors:
213 1.21 rillig @echo $@: ${UNDEF:Uvalue:C,[,,}
214 1.21 rillig
215 1.16 rillig # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
216 1.16 rillig mod-assign:
217 1.16 rillig @echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
218 1.16 rillig @echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
219 1.16 rillig @echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
220 1.16 rillig @echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
221 1.16 rillig # The assignments happen in the global scope and thus are
222 1.16 rillig # preserved even after the shell command has been run.
223 1.16 rillig @echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
224 1.17 rillig
225 1.18 rillig mod-assign-nested:
226 1.18 rillig @echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
227 1.18 rillig @echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
228 1.18 rillig @echo $@: ${SINK3:Q}
229 1.18 rillig @echo $@: ${SINK4:Q}
230 1.18 rillig SINK3:= ${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
231 1.18 rillig SINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
232 1.18 rillig
233 1.17 rillig mod-tu-space:
234 1.17 rillig # The :tu and :tl modifiers operate on the variable value
235 1.17 rillig # as a single string, not as a list of words. Therefore,
236 1.17 rillig # the adjacent spaces are preserved.
237 1.17 rillig @echo $@: ${a b:L:tu:Q}
238 1.19 rillig
239 1.23 rillig mod-quote:
240 1.19 rillig @echo $@: new${.newline:Q}${.newline:Q}line
241 1.22 rillig
242 1.22 rillig # Cover the bmake_realloc in brk_string.
243 1.22 rillig mod-break-many-words:
244 1.22 rillig @echo $@: ${UNDEF:U:range=500:[#]}
245