Home | History | Annotate | Line # | Download | only in unit-tests
directive-for-escape.mk revision 1.8
      1 # $NetBSD: directive-for-escape.mk,v 1.8 2021/06/24 23:22:17 rillig Exp $
      2 #
      3 # Test escaping of special characters in the iteration values of a .for loop.
      4 # These values get expanded later using the :U variable modifier, and this
      5 # escaping and unescaping must pass all characters and strings effectively
      6 # unmodified.
      7 
      8 .MAKEFLAGS: -df
      9 
     10 # Even though the .for loops take quotes into account when splitting the
     11 # string into words, the quotes don't need to be balanced, as of 2020-12-31.
     12 # This could be considered a bug.
     13 ASCII=	!"\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
     14 
     15 # XXX: As of 2020-12-31, the '#' is not preserved in the expanded body of
     16 # the loop since it would not need only the escaping for the :U variable
     17 # modifier but also the escaping for the line-end comment.
     18 .for chars in ${ASCII}
     19 .  info ${chars}
     20 .endfor
     21 
     22 # As of 2020-12-31, using 2 backslashes before be '#' would treat the '#'
     23 # as comment character.  Using 3 backslashes doesn't help either since
     24 # then the situation is essentially the same as with 1 backslash.
     25 # This means that a '#' sign cannot be passed in the value of a .for loop
     26 # at all.
     27 ASCII.2020-12-31=	!"\\\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
     28 .for chars in ${ASCII.2020-12-31}
     29 .  info ${chars}
     30 .endfor
     31 
     32 # Cover the code in for_var_len.
     33 #
     34 # XXX: It is unexpected that the variable V gets expanded in the loop body.
     35 # The double '$$' should prevent exactly this.  Probably nobody was
     36 # adventurous enough to use literal dollar signs in the values of a .for
     37 # loop.
     38 V=		value
     39 VALUES=		$$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
     40 .for i in ${VALUES}
     41 .  info $i
     42 .endfor
     43 
     44 # Try to cover the code for nested '{}' in for_var_len, without success.
     45 #
     46 # The value of the variable VALUES is not meant to be a variable expression.
     47 # Instead, it is meant to represent literal text, the only escaping mechanism
     48 # being that each '$' is written as '$$'.
     49 #
     50 # The .for loop splits ${VALUES} into 3 words, at the space characters, since
     51 # these are not escaped.
     52 VALUES=		$${UNDEF:U\$$\$$ {{}} end}
     53 # XXX: Where in the code does the '\$\$' get converted into a single '\$'?
     54 .for i in ${VALUES}
     55 .  info $i
     56 .endfor
     57 
     58 # Second try to cover the code for nested '{}' in for_var_len.
     59 #
     60 # XXX: It is wrong that for_var_len requires the braces to be balanced.
     61 # Each variable modifier has its own inconsistent way of parsing nested
     62 # variable expressions, braces and parentheses.  (Compare ':M', ':S', and
     63 # ':D' for details.)  The only sensible thing to do is therefore to let
     64 # Var_Parse do all the parsing work.
     65 VALUES=		begin<$${UNDEF:Ufallback:N{{{}}}}>end
     66 .for i in ${VALUES}
     67 .  info $i
     68 .endfor
     69 
     70 # A single trailing dollar doesn't happen in practice.
     71 # The dollar sign is correctly passed through to the body of the .for loop.
     72 # There, it is expanded by the .info directive, but even there a trailing
     73 # dollar sign is kept as-is.
     74 .for i in ${:U\$}
     75 .  info ${i}
     76 .endfor
     77 
     78 # As of 2020-12-31, the name of the iteration variable can even contain
     79 # colons, which then affects variable expressions having this exact modifier.
     80 # This is clearly an unintended side effect of the implementation.
     81 NUMBERS=	one two three
     82 .for NUMBERS:M*e in replaced
     83 .  info ${NUMBERS} ${NUMBERS:M*e}
     84 .endfor
     85 
     86 # As of 2020-12-31, the name of the iteration variable can contain braces,
     87 # which gets even more surprising than colons, since it allows to replace
     88 # sequences of variable expressions.  There is no practical use case for
     89 # this, though.
     90 BASENAME=	one
     91 EXT=		.c
     92 .for BASENAME}${EXT in replaced
     93 .  info ${BASENAME}${EXT}
     94 .endfor
     95 
     96 # Demonstrate the various ways to refer to the iteration variable.
     97 i=		outer
     98 i2=		two
     99 i,=		comma
    100 .for i in inner
    101 .  info .        $$i: $i
    102 .  info .      $${i}: ${i}
    103 .  info .   $${i:M*}: ${i:M*}
    104 .  info .      $$(i): $(i)
    105 .  info .   $$(i:M*): $(i:M*)
    106 .  info . $${i$${:U}}: ${i${:U}}
    107 .  info .    $${i\}}: ${i\}}	# XXX: unclear why ForLoop_SubstVarLong needs this
    108 .  info .     $${i2}: ${i2}
    109 .  info .     $${i,}: ${i,}
    110 .  info .  adjacent: $i${i}${i:M*}$i
    111 .endfor
    112 
    113 # The variable name can be a single '$' since there is no check on valid
    114 # variable names. ForLoop_SubstVarShort skips "stupid" variable names though,
    115 # but ForLoop_SubstVarLong naively parses the body of the loop, substituting
    116 # each '${$}' with an actual 'dollar'.
    117 .for $ in dollar
    118 .  info eight $$$$$$$$ and no cents.
    119 .  info eight ${$}${$}${$}${$} and no cents.
    120 .endfor
    121 # Outside a .for loop, '${$}' is interpreted differently. The outer '$' starts
    122 # a variable expression. The inner '$' is followed by a '}' and is thus a
    123 # silent syntax error, the '$' is skipped. The variable name is thus '', and
    124 # since since there is never a variable named '', the whole expression '${$}'
    125 # evaluates to an empty string.
    126 closing-brace=		}		# guard against an
    127 ${closing-brace}=	<closing-brace>	# alternative interpretation
    128 .info eight ${$}${$}${$}${$} and no cents.
    129 
    130 all:
    131