1*0a6a1f1dSLionel Sambuc# $Id: escape.mk,v 1.10 2014/09/09 10:22:27 apb Exp $ 2*0a6a1f1dSLionel Sambuc# 3*0a6a1f1dSLionel Sambuc# Test backslash escaping. 4*0a6a1f1dSLionel Sambuc 5*0a6a1f1dSLionel Sambuc# Extracts from the POSIX 2008 specification 6*0a6a1f1dSLionel Sambuc# <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html>: 7*0a6a1f1dSLionel Sambuc# 8*0a6a1f1dSLionel Sambuc# Comments start with a <number-sign> ( '#' ) and continue until an 9*0a6a1f1dSLionel Sambuc# unescaped <newline> is reached. 10*0a6a1f1dSLionel Sambuc# 11*0a6a1f1dSLionel Sambuc# When an escaped <newline> (one preceded by a <backslash>) is found 12*0a6a1f1dSLionel Sambuc# anywhere in the makefile except in a command line, an include 13*0a6a1f1dSLionel Sambuc# line, or a line immediately preceding an include line, it shall 14*0a6a1f1dSLionel Sambuc# be replaced, along with any leading white space on the following 15*0a6a1f1dSLionel Sambuc# line, with a single <space>. 16*0a6a1f1dSLionel Sambuc# 17*0a6a1f1dSLionel Sambuc# When an escaped <newline> is found in a command line in a 18*0a6a1f1dSLionel Sambuc# makefile, the command line shall contain the <backslash>, the 19*0a6a1f1dSLionel Sambuc# <newline>, and the next line, except that the first character of 20*0a6a1f1dSLionel Sambuc# the next line shall not be included if it is a <tab>. 21*0a6a1f1dSLionel Sambuc# 22*0a6a1f1dSLionel Sambuc# When an escaped <newline> is found in an include line or in a 23*0a6a1f1dSLionel Sambuc# line immediately preceding an include line, the behavior is 24*0a6a1f1dSLionel Sambuc# unspecified. 25*0a6a1f1dSLionel Sambuc# 26*0a6a1f1dSLionel Sambuc# Notice that the behaviour of <backslash><backslash> or 27*0a6a1f1dSLionel Sambuc# <backslash><anything other than newline> is not mentioned. I think 28*0a6a1f1dSLionel Sambuc# this implies that <backslash> should be taken literally everywhere 29*0a6a1f1dSLionel Sambuc# except before <newline>. 30*0a6a1f1dSLionel Sambuc# 31*0a6a1f1dSLionel Sambuc# Our practice, despite what POSIX might say, is that "\#" 32*0a6a1f1dSLionel Sambuc# in a variable assignment stores "#" as part of the value. 33*0a6a1f1dSLionel Sambuc# The "\" is not taken literally, and the "#" does not begin a comment. 34*0a6a1f1dSLionel Sambuc# 35*0a6a1f1dSLionel Sambuc# Also, our practice is that an even number of backslashes before a 36*0a6a1f1dSLionel Sambuc# newline in a variable assignment simply stores the backslashes as part 37*0a6a1f1dSLionel Sambuc# of the value, and treats the newline as though it was not escaped. 38*0a6a1f1dSLionel Sambuc# Similarly, ann even number of backslashes before a newline in a 39*0a6a1f1dSLionel Sambuc# command simply uses the backslashes as part of the command test, but 40*0a6a1f1dSLionel Sambuc# does not escape the newline. This is compatible with GNU make. 41*0a6a1f1dSLionel Sambuc 42*0a6a1f1dSLionel Sambucall: .PHONY 43*0a6a1f1dSLionel Sambuc# We will add dependencies like "all: yet-another-test" later. 44*0a6a1f1dSLionel Sambuc 45*0a6a1f1dSLionel Sambuc# Some variables to be expanded in tests 46*0a6a1f1dSLionel Sambuc# 47*0a6a1f1dSLionel Sambuca = aaa 48*0a6a1f1dSLionel SambucA = ${a} 49*0a6a1f1dSLionel Sambuc 50*0a6a1f1dSLionel Sambuc# Backslash at end of line in a comment\ 51*0a6a1f1dSLionel Sambucshould continue the comment. \ 52*0a6a1f1dSLionel Sambuc# This is also tested in comment.mk. 53*0a6a1f1dSLionel Sambuc 54*0a6a1f1dSLionel Sambuc__printvars: .USE .MADE 55*0a6a1f1dSLionel Sambuc @echo ${.TARGET} 56*0a6a1f1dSLionel Sambuc ${.ALLSRC:@v@ printf "%s=:%s:\n" ${v:Q} ${${v}:Q}; @} 57*0a6a1f1dSLionel Sambuc 58*0a6a1f1dSLionel Sambuc# Embedded backslash in variable should be taken literally. 59*0a6a1f1dSLionel Sambuc# 60*0a6a1f1dSLionel SambucVAR1BS = 111\111 61*0a6a1f1dSLionel SambucVAR1BSa = 111\${a} 62*0a6a1f1dSLionel SambucVAR1BSA = 111\${A} 63*0a6a1f1dSLionel SambucVAR1BSda = 111\$${a} 64*0a6a1f1dSLionel SambucVAR1BSdA = 111\$${A} 65*0a6a1f1dSLionel SambucVAR1BSc = 111\# backslash escapes comment char, so this is part of the value 66*0a6a1f1dSLionel SambucVAR1BSsc = 111\ # This is a comment. Value ends with <backslash><space> 67*0a6a1f1dSLionel Sambuc 68*0a6a1f1dSLionel Sambucall: var-1bs 69*0a6a1f1dSLionel Sambucvar-1bs: .PHONY __printvars VAR1BS VAR1BSa VAR1BSA VAR1BSda VAR1BSdA \ 70*0a6a1f1dSLionel Sambuc VAR1BSc VAR1BSsc 71*0a6a1f1dSLionel Sambuc 72*0a6a1f1dSLionel Sambuc# Double backslash in variable should be taken as two literal backslashes. 73*0a6a1f1dSLionel Sambuc# 74*0a6a1f1dSLionel SambucVAR2BS = 222\\222 75*0a6a1f1dSLionel SambucVAR2BSa = 222\\${a} 76*0a6a1f1dSLionel SambucVAR2BSA = 222\\${A} 77*0a6a1f1dSLionel SambucVAR2BSda = 222\\$${a} 78*0a6a1f1dSLionel SambucVAR2BSdA = 222\\$${A} 79*0a6a1f1dSLionel SambucVAR2BSc = 222\\# backslash does not escape comment char, so this is a comment 80*0a6a1f1dSLionel SambucVAR2BSsc = 222\\ # This is a comment. Value ends with <backslash><backslash> 81*0a6a1f1dSLionel Sambuc 82*0a6a1f1dSLionel Sambucall: var-2bs 83*0a6a1f1dSLionel Sambucvar-2bs: .PHONY __printvars VAR2BS VAR2BSa VAR2BSA VAR2BSda VAR2BSdA \ 84*0a6a1f1dSLionel Sambuc VAR2BSc VAR2BSsc 85*0a6a1f1dSLionel Sambuc 86*0a6a1f1dSLionel Sambuc# Backslash-newline in a variable setting is replaced by a single space. 87*0a6a1f1dSLionel Sambuc# 88*0a6a1f1dSLionel SambucVAR1BSNL = 111\ 89*0a6a1f1dSLionel Sambuc111 90*0a6a1f1dSLionel SambucVAR1BSNLa = 111\ 91*0a6a1f1dSLionel Sambuc${a} 92*0a6a1f1dSLionel SambucVAR1BSNLA = 111\ 93*0a6a1f1dSLionel Sambuc${A} 94*0a6a1f1dSLionel SambucVAR1BSNLda = 111\ 95*0a6a1f1dSLionel Sambuc$${a} 96*0a6a1f1dSLionel SambucVAR1BSNLdA = 111\ 97*0a6a1f1dSLionel Sambuc$${A} 98*0a6a1f1dSLionel SambucVAR1BSNLc = 111\ 99*0a6a1f1dSLionel Sambuc# this should be processed as a comment 100*0a6a1f1dSLionel SambucVAR1BSNLsc = 111\ 101*0a6a1f1dSLionel Sambuc # this should be processed as a comment 102*0a6a1f1dSLionel Sambuc 103*0a6a1f1dSLionel Sambucall: var-1bsnl 104*0a6a1f1dSLionel Sambucvar-1bsnl: .PHONY 105*0a6a1f1dSLionel Sambucvar-1bsnl: .PHONY __printvars \ 106*0a6a1f1dSLionel Sambuc VAR1BSNL VAR1BSNLa VAR1BSNLA VAR1BSNLda VAR1BSNLdA \ 107*0a6a1f1dSLionel Sambuc VAR1BSNLc VAR1BSNLsc 108*0a6a1f1dSLionel Sambuc 109*0a6a1f1dSLionel Sambuc# Double-backslash-newline in a variable setting. 110*0a6a1f1dSLionel Sambuc# Both backslashes should be taken literally, and the newline is NOT escaped. 111*0a6a1f1dSLionel Sambuc# 112*0a6a1f1dSLionel Sambuc# The second lines below each end with '=' so that they will not 113*0a6a1f1dSLionel Sambuc# generate syntax errors regardless of whether or not they are 114*0a6a1f1dSLionel Sambuc# treated as part of the value. 115*0a6a1f1dSLionel Sambuc# 116*0a6a1f1dSLionel SambucVAR2BSNL = 222\\ 117*0a6a1f1dSLionel Sambuc222= 118*0a6a1f1dSLionel SambucVAR2BSNLa = 222\\ 119*0a6a1f1dSLionel Sambuc${a}= 120*0a6a1f1dSLionel SambucVAR2BSNLA = 222\\ 121*0a6a1f1dSLionel Sambuc${A}= 122*0a6a1f1dSLionel SambucVAR2BSNLda = 222\\ 123*0a6a1f1dSLionel Sambuc$${a}= 124*0a6a1f1dSLionel SambucVAR2BSNLdA = 222\\ 125*0a6a1f1dSLionel Sambuc$${A}= 126*0a6a1f1dSLionel SambucVAR2BSNLc = 222\\ 127*0a6a1f1dSLionel Sambuc# this should be processed as a comment 128*0a6a1f1dSLionel SambucVAR2BSNLsc = 222\\ 129*0a6a1f1dSLionel Sambuc # this should be processed as a comment 130*0a6a1f1dSLionel Sambuc 131*0a6a1f1dSLionel Sambucall: var-2bsnl 132*0a6a1f1dSLionel Sambucvar-2bsnl: .PHONY __printvars \ 133*0a6a1f1dSLionel Sambuc VAR2BSNL VAR2BSNLa VAR2BSNLA VAR2BSNLda VAR2BSNLdA \ 134*0a6a1f1dSLionel Sambuc VAR2BSNLc VAR2BSNLsc 135*0a6a1f1dSLionel Sambuc 136*0a6a1f1dSLionel Sambuc# Triple-backslash-newline in a variable setting. 137*0a6a1f1dSLionel Sambuc# First two should be taken literally, and last should escape the newline. 138*0a6a1f1dSLionel Sambuc# 139*0a6a1f1dSLionel Sambuc# The second lines below each end with '=' so that they will not 140*0a6a1f1dSLionel Sambuc# generate syntax errors regardless of whether or not they are 141*0a6a1f1dSLionel Sambuc# treated as part of the value. 142*0a6a1f1dSLionel Sambuc# 143*0a6a1f1dSLionel SambucVAR3BSNL = 333\\\ 144*0a6a1f1dSLionel Sambuc333= 145*0a6a1f1dSLionel SambucVAR3BSNLa = 333\\\ 146*0a6a1f1dSLionel Sambuc${a}= 147*0a6a1f1dSLionel SambucVAR3BSNLA = 333\\\ 148*0a6a1f1dSLionel Sambuc${A}= 149*0a6a1f1dSLionel SambucVAR3BSNLda = 333\\\ 150*0a6a1f1dSLionel Sambuc$${a}= 151*0a6a1f1dSLionel SambucVAR3BSNLdA = 333\\\ 152*0a6a1f1dSLionel Sambuc$${A}= 153*0a6a1f1dSLionel SambucVAR3BSNLc = 333\\\ 154*0a6a1f1dSLionel Sambuc# this should be processed as a comment 155*0a6a1f1dSLionel SambucVAR3BSNLsc = 333\\\ 156*0a6a1f1dSLionel Sambuc # this should be processed as a comment 157*0a6a1f1dSLionel Sambuc 158*0a6a1f1dSLionel Sambucall: var-3bsnl 159*0a6a1f1dSLionel Sambucvar-3bsnl: .PHONY __printvars \ 160*0a6a1f1dSLionel Sambuc VAR3BSNL VAR3BSNLa VAR3BSNLA VAR3BSNLda VAR3BSNLdA \ 161*0a6a1f1dSLionel Sambuc VAR3BSNLc VAR3BSNLsc 162*0a6a1f1dSLionel Sambuc 163*0a6a1f1dSLionel Sambuc# Backslash-newline in a variable setting, plus any amount of white space 164*0a6a1f1dSLionel Sambuc# on the next line, is replaced by a single space. 165*0a6a1f1dSLionel Sambuc# 166*0a6a1f1dSLionel SambucVAR1BSNL00= first line\ 167*0a6a1f1dSLionel Sambuc 168*0a6a1f1dSLionel Sambuc# above line is entirely empty, and this is a comment 169*0a6a1f1dSLionel SambucVAR1BSNL0= first line\ 170*0a6a1f1dSLionel Sambucno space on second line 171*0a6a1f1dSLionel SambucVAR1BSNLs= first line\ 172*0a6a1f1dSLionel Sambuc one space on second line 173*0a6a1f1dSLionel SambucVAR1BSNLss= first line\ 174*0a6a1f1dSLionel Sambuc two spaces on second line 175*0a6a1f1dSLionel SambucVAR1BSNLt= first line\ 176*0a6a1f1dSLionel Sambuc one tab on second line 177*0a6a1f1dSLionel SambucVAR1BSNLtt= first line\ 178*0a6a1f1dSLionel Sambuc two tabs on second line 179*0a6a1f1dSLionel SambucVAR1BSNLxx= first line\ 180*0a6a1f1dSLionel Sambuc many spaces and tabs [ ] on second line 181*0a6a1f1dSLionel Sambuc 182*0a6a1f1dSLionel Sambucall: var-1bsnl-space 183*0a6a1f1dSLionel Sambucvar-1bsnl-space: .PHONY __printvars \ 184*0a6a1f1dSLionel Sambuc VAR1BSNL00 VAR1BSNL0 VAR1BSNLs VAR1BSNLss VAR1BSNLt VAR1BSNLtt \ 185*0a6a1f1dSLionel Sambuc VAR1BSNLxx 186*0a6a1f1dSLionel Sambuc 187*0a6a1f1dSLionel Sambuc# Backslash-newline in a command is retained. 188*0a6a1f1dSLionel Sambuc# 189*0a6a1f1dSLionel Sambuc# The "#" in "# second line without space" makes it a comment instead 190*0a6a1f1dSLionel Sambuc# of a syntax error if the preceding line is parsed incorretly. 191*0a6a1f1dSLionel Sambuc# The ":" in "third line':" makes it look like the start of a 192*0a6a1f1dSLionel Sambuc# target instead of a syntax error if the first line is parsed incorrectly. 193*0a6a1f1dSLionel Sambuc# 194*0a6a1f1dSLionel Sambucall: cmd-1bsnl 195*0a6a1f1dSLionel Sambuccmd-1bsnl: .PHONY 196*0a6a1f1dSLionel Sambuc @echo ${.TARGET} 197*0a6a1f1dSLionel Sambuc echo :'first line\ 198*0a6a1f1dSLionel Sambuc#second line without space\ 199*0a6a1f1dSLionel Sambucthird line': 200*0a6a1f1dSLionel Sambuc echo :'first line\ 201*0a6a1f1dSLionel Sambuc second line spaces should be retained': 202*0a6a1f1dSLionel Sambuc echo :'first line\ 203*0a6a1f1dSLionel Sambuc second line tab should be elided': 204*0a6a1f1dSLionel Sambuc echo :'first line\ 205*0a6a1f1dSLionel Sambuc only one tab should be elided, second tab remains' 206*0a6a1f1dSLionel Sambuc 207*0a6a1f1dSLionel Sambuc# When backslash-newline appears at the end of a command script, 208*0a6a1f1dSLionel Sambuc# both the backslash and the newline should be passed to the shell. 209*0a6a1f1dSLionel Sambuc# The shell should elide the backslash-newline. 210*0a6a1f1dSLionel Sambuc# 211*0a6a1f1dSLionel Sambucall: cmd-1bsnl-eof 212*0a6a1f1dSLionel Sambuccmd-1bsnl-eof: 213*0a6a1f1dSLionel Sambuc @echo ${.TARGET} 214*0a6a1f1dSLionel Sambuc echo :'command ending with backslash-newline'; \ 215*0a6a1f1dSLionel Sambuc 216*0a6a1f1dSLionel Sambuc# above line must be blank 217*0a6a1f1dSLionel Sambuc 218*0a6a1f1dSLionel Sambuc# Double-backslash-newline in a command. 219*0a6a1f1dSLionel Sambuc# Both backslashes are retained, but the newline is not escaped. 220*0a6a1f1dSLionel Sambuc# XXX: This may differ from POSIX, but matches gmake. 221*0a6a1f1dSLionel Sambuc# 222*0a6a1f1dSLionel Sambuc# When make passes two backslashes to the shell, the shell will pass one 223*0a6a1f1dSLionel Sambuc# backslash to the echo commant. 224*0a6a1f1dSLionel Sambuc# 225*0a6a1f1dSLionel Sambucall: cmd-2bsnl 226*0a6a1f1dSLionel Sambuccmd-2bsnl: .PHONY 227*0a6a1f1dSLionel Sambuc @echo ${.TARGET} 228*0a6a1f1dSLionel Sambuc echo take one\\ 229*0a6a1f1dSLionel Sambuc# this should be a comment 230*0a6a1f1dSLionel Sambuc echo take two\\ 231*0a6a1f1dSLionel Sambuc echo take three\\ 232*0a6a1f1dSLionel Sambuc 233*0a6a1f1dSLionel Sambuc# Triple-backslash-newline in a command is retained. 234*0a6a1f1dSLionel Sambuc# 235*0a6a1f1dSLionel Sambucall: cmd-3bsnl 236*0a6a1f1dSLionel Sambuccmd-3bsnl: .PHONY 237*0a6a1f1dSLionel Sambuc @echo ${.TARGET} 238*0a6a1f1dSLionel Sambuc echo :'first line\\\ 239*0a6a1f1dSLionel Sambuc#second line without space\\\ 240*0a6a1f1dSLionel Sambucthird line': 241*0a6a1f1dSLionel Sambuc echo :'first line\\\ 242*0a6a1f1dSLionel Sambuc second line spaces should be retained': 243*0a6a1f1dSLionel Sambuc echo :'first line\\\ 244*0a6a1f1dSLionel Sambuc second line tab should be elided': 245*0a6a1f1dSLionel Sambuc echo :'first line\\\ 246*0a6a1f1dSLionel Sambuc only one tab should be elided, second tab remains' 247