modmisc.mk revision 1.16 1 1.16 rillig # $Id: modmisc.mk,v 1.16 2020/07/19 20:49:44 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.13 rillig all: mod-S mod-C mod-at-varname mod-at-resolve mod-at-dollar
20 1.12 rillig all: mod-subst-dollar mod-loop-dollar
21 1.14 rillig all: mod-C-limits
22 1.16 rillig all: mod-assign
23 1.1 apb
24 1.1 apb modsysv:
25 1.1 apb @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
26 1.1 apb
27 1.1 apb modvar:
28 1.1 apb @echo "path='${path}'"
29 1.1 apb @echo "path='${path:${MOD_NODOT}}'"
30 1.1 apb @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
31 1.1 apb @echo "path=${path:${MOD_NODOTX}:ts:}"
32 1.1 apb @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
33 1.1 apb
34 1.1 apb .for d in ${path:${MOD_SEP}:N.} /usr/xbin
35 1.1 apb path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
36 1.1 apb paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
37 1.1 apb .endfor
38 1.1 apb
39 1.1 apb modvarloop:
40 1.1 apb @echo "path_/usr/xbin=${path_/usr/xbin}"
41 1.1 apb @echo "paths=${paths}"
42 1.1 apb @echo "PATHS=${paths:tu}"
43 1.2 rillig
44 1.6 rillig PATHNAMES= a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
45 1.4 rillig mod-HTE:
46 1.6 rillig @echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
47 1.6 rillig @echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
48 1.6 rillig @echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
49 1.6 rillig @echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
50 1.7 rillig
51 1.7 rillig # When a modifier is applied to the "" variable, the result is discarded.
52 1.7 rillig emptyvar:
53 1.7 rillig @echo S:${:S,^$,empty,}
54 1.7 rillig @echo C:${:C,^$,empty,}
55 1.7 rillig @echo @:${:@var@${var}@}
56 1.7 rillig
57 1.7 rillig # The :U modifier turns even the "" variable into something that has a value.
58 1.7 rillig # The resulting variable is empty, but is still considered to contain a
59 1.7 rillig # single empty word. This word can be accessed by the :S and :C modifiers,
60 1.7 rillig # but not by the :@ modifier since it explicitly skips empty words.
61 1.7 rillig undefvar:
62 1.7 rillig @echo S:${:U:S,^$,empty,}
63 1.7 rillig @echo C:${:U:C,^$,empty,}
64 1.7 rillig @echo @:${:U:@var@empty@}
65 1.8 rillig
66 1.8 rillig mod-S:
67 1.8 rillig @echo :${:Ua b b c:S,a b,,:Q}:
68 1.8 rillig @echo :${:Ua b b c:S,a b,,1:Q}:
69 1.8 rillig @echo :${:Ua b b c:S,a b,,W:Q}:
70 1.8 rillig @echo :${:Ua b b c:S,b,,g:Q}:
71 1.8 rillig @echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
72 1.8 rillig
73 1.8 rillig mod-C:
74 1.8 rillig @echo :${:Ua b b c:C,a b,,:Q}:
75 1.8 rillig @echo :${:Ua b b c:C,a b,,1:Q}:
76 1.8 rillig @echo :${:Ua b b c:C,a b,,W:Q}:
77 1.8 rillig @echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
78 1.8 rillig @echo :${:Ua b b c:C,b,,g:Q}:
79 1.8 rillig @echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
80 1.9 rillig
81 1.9 rillig # In the :@ modifier, the name of the loop variable can even be generated
82 1.9 rillig # dynamically. There's no practical use-case for this, and hopefully nobody
83 1.9 rillig # will ever depend on this, but technically it's possible.
84 1.9 rillig mod-at-varname:
85 1.9 rillig @echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
86 1.10 rillig
87 1.10 rillig # The :@ modifier resolves the variables a little more often than expected.
88 1.10 rillig # In particular, it resolves _all_ variables from the context, and not only
89 1.10 rillig # the loop variable (in this case v).
90 1.11 rillig #
91 1.11 rillig # The d means direct reference, the i means indirect reference.
92 1.10 rillig RESOLVE= ${RES1} $${RES1}
93 1.11 rillig RES1= 1d${RES2} 1i$${RES2}
94 1.11 rillig RES2= 2d${RES3} 2i$${RES3}
95 1.11 rillig RES3= 3
96 1.10 rillig
97 1.10 rillig mod-at-resolve:
98 1.10 rillig @echo $@:${RESOLVE:@v@w${v}w@:Q}:
99 1.12 rillig
100 1.13 rillig # As of 2020-07-19, the variable name of the :@ modifier may end with one
101 1.13 rillig # or two dollar signs, which are silently ignored. There's no point in
102 1.13 rillig # allowing a dollar sign in that position.
103 1.13 rillig mod-at-dollar:
104 1.13 rillig @echo $@:${1 2 3:L:@v$@($v)@:Q}.
105 1.13 rillig @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
106 1.13 rillig @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
107 1.13 rillig
108 1.12 rillig # No matter how many dollar characters there are, they all get merged
109 1.12 rillig # into a single dollar by the :S modifier.
110 1.12 rillig mod-subst-dollar:
111 1.12 rillig @echo $@:${:U1:S,^,$,:Q}:
112 1.12 rillig @echo $@:${:U2:S,^,$$,:Q}:
113 1.12 rillig @echo $@:${:U3:S,^,$$$,:Q}:
114 1.12 rillig @echo $@:${:U4:S,^,$$$$,:Q}:
115 1.12 rillig @echo $@:${:U5:S,^,$$$$$,:Q}:
116 1.12 rillig @echo $@:${:U6:S,^,$$$$$$,:Q}:
117 1.12 rillig @echo $@:${:U7:S,^,$$$$$$$,:Q}:
118 1.12 rillig @echo $@:${:U8:S,^,$$$$$$$$,:Q}:
119 1.12 rillig # This generates no dollar at all:
120 1.12 rillig @echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
121 1.12 rillig # Here is an alternative way to generate dollar characters.
122 1.12 rillig # It's unexpectedly complicated though.
123 1.12 rillig @echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
124 1.12 rillig
125 1.12 rillig # Demonstrate that it is possible to generate dollar characters using the
126 1.12 rillig # :@ modifier.
127 1.12 rillig #
128 1.12 rillig # These are edge cases that could have resulted in a parse error as well
129 1.12 rillig # since the $@ at the end could have been interpreted as a variable, which
130 1.12 rillig # would mean a missing closing @ delimiter.
131 1.12 rillig mod-loop-dollar:
132 1.12 rillig @echo $@:${:U1:@word@${word}$@:Q}:
133 1.12 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
134 1.12 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
135 1.12 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
136 1.12 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
137 1.12 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
138 1.14 rillig
139 1.14 rillig mod-C-limits:
140 1.14 rillig @echo $@:00-ok:${:U1 23 456:C,..,\0\0,:Q}
141 1.14 rillig @echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
142 1.14 rillig @echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
143 1.14 rillig @echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
144 1.14 rillig @echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
145 1.14 rillig @echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
146 1.15 rillig # The :C modifier only handles single-digit capturing groups,
147 1.15 rillig # which is more than enough for daily use.
148 1.15 rillig @echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
149 1.16 rillig
150 1.16 rillig # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
151 1.16 rillig mod-assign:
152 1.16 rillig @echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
153 1.16 rillig @echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
154 1.16 rillig @echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
155 1.16 rillig @echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
156 1.16 rillig # The assignments happen in the global scope and thus are
157 1.16 rillig # preserved even after the shell command has been run.
158 1.16 rillig @echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
159