1# $NetBSD: dollar.mk,v 1.4 2020/11/03 18:21:36 rillig Exp $
2#
3# Test the various places where a dollar sign can appear and
4# see what happens.  There are lots of surprises here.
5#
6
7LIST=               plain 'single' "double" 'mix'"ed" back\ slashed
8WORD=               word
9
10DOLLAR1=  $
11DOLLAR2=  $$
12DOLLAR4=  $$$$
13
14X=                  VAR_X
15DOLLAR_XY=          $$XY
16DOLLAR_AXY=         $$AXY
17
18H=        @header() { printf '\n%s\n\n' "$$*"; }; header
19T=        @testcase()         { printf '%23s => <%s>\n' "$$@"; }; testcase
20C=        @comment()          { printf '%s\n' "$$*"; }; comment
21
22# These variable values are not accessed.
23# The trailing dollar in the '1 dollar literal eol' test case accesses
24# the empty variable instead, which is always guaranteed to be empty.
25${:U }=                       space-var-value
26${:U${.newline}}=   newline-var-value
27# But this one is accessed.
28${:U'}=                       single-quote-var-value'
29
30all:
31          $H 'Printing dollar from literals and variables'
32
33          $C 'To survive the parser, a dollar sign must be doubled.'
34          $T        '1 dollar literal'  '$'
35          $T        '1 dollar literal eol'        ''$
36          $T        '2 dollar literal'  '$$'
37          $T        '4 dollar literal'  '$$$$'
38
39          $C 'Some hungry part of make eats all the dollars after a :U modifier.'
40          $T        '1 dollar default'  ''${:U$:Q}
41          $T        '2 dollar default'  ''${:U$$:Q}
42          $T        '4 dollar default'  ''${:U$$$$:Q}
43
44          $C 'This works as expected.'
45          $T        '1 dollar variable' ''${DOLLAR1:Q}
46          $T        '2 dollar variable' ''${DOLLAR2:Q}
47          $T        '4 dollar variable' ''${DOLLAR4:Q}
48
49          $C 'Some hungry part of make eats all the dollars after a :U modifier.'
50          $T        '1 dollar var-default'        ''${:U${DOLLAR1}:Q}
51          $T        '2 dollar var-default'        ''${:U${DOLLAR2}:Q}
52          $T        '4 dollar var-default'        ''${:U${DOLLAR4}:Q}
53
54          $H 'Dollar in :S pattern'
55
56          $T        'S,$$,word,'                  ''${DOLLAR_XY:S,$,word,:Q}
57          $T        'S,$$X,word,'                 ''${DOLLAR_XY:S,$X,word,:Q}
58          $T        'S,$$$$X,word,'               ''${DOLLAR_XY:S,$$X,word,:Q}
59          $T        'S,$$$$$$X,word,'   ''${DOLLAR_XY:S,$$$X,word,:Q}
60
61          $T        'S,$$X,replaced,'   ''${X:S,$X,replaced,:Q}
62          $T        'S,$$$$X,replaced,' ''${X:S,$$X,replaced,:Q}
63          $T        'S,$$$$$$X,replaced,'         ''${X:S,$$$X,replaced,:Q}
64
65          $H 'Dollar in :C character class'
66
67          $C 'The A is replaced because the $$$$ is reduced to a single $$,'
68          $C 'which is then resolved to the variable X with the value VAR_X.'
69          $C 'The effective character class becomes [VAR_XY].'
70          $T        'C,[$$$$XY],<&>,g'  ''${DOLLAR_AXY:C,[$$XY],<&>,g:Q}
71
72          $H 'Dollar in :C pattern'
73          $C 'For some reason, multiple dollars are folded into one.'
74          $T        'C,$$,dollar,g'               ''${DOLLAR:C,$,dollar,g:Q}
75          $T        'C,$$$$,dollar,g'   ''${DOLLAR:C,$$,dollar,g:Q}
76
77          $H 'Dollar in :S replacement'
78          $C 'For some reason, multiple dollars are folded into one.'
79          $T        'S,word,a$$Xo,'               ''${WORD:S,word,a$Xo,:Q}
80          $T        'S,word,a$$$$Xo,'   ''${WORD:S,word,a$$Xo,:Q}
81          $T        'S,word,a$$$$$$Xo,' ''${WORD:S,word,a$$$Xo,:Q}
82