From 1eee84b800408e53e659ff303eaa08ee09d67c3b Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 11 Mar 2024 09:12:47 +0000 Subject: make: allow '#' to be escaped with a backslash POSIX doesn't allow the '#' comment marker to be escaped, though some implementations do. As a non-POSIX extension allow '#' to be escaped with a preceding backslash. It isn't necessary to escape '#' in macro expansions or command lines: these cases are covered by an existing extension. Commit 0aceca867 (make: comments in macro expansions and command lines) Adds 16-32 bytes. (pdpmake GitHub issue 38) --- miscutils/make.c | 14 +++++++++++--- testsuite/make.tests | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/miscutils/make.c b/miscutils/make.c index 841daa93e..87f17aad7 100644 --- a/miscutils/make.c +++ b/miscutils/make.c @@ -1373,9 +1373,17 @@ process_line(char *s) // Strip comment // don't treat '#' in macro expansion as a comment - if (!posix) - t = find_char(s, '#'); - else + // nor '#' outside macro expansion preceded by backslash + if (!posix) { + char *u = s; + while ((t = find_char(u, '#')) && t > u && t[-1] == '\\') { + for (u = t; *u; ++u) { + u[-1] = u[0]; + } + *u = '\0'; + u = t; + } + } else t = strchr(s, '#'); if (t) *t = '\0'; diff --git a/testsuite/make.tests b/testsuite/make.tests index 745a840f3..1fc95a8d2 100755 --- a/testsuite/make.tests +++ b/testsuite/make.tests @@ -459,6 +459,15 @@ target: : hash $(hash) hash ' +# A '#' character can be escaped with a backslash +testing "make backslash-escaped hash isn't a comment" \ + "make -f -" \ + ": hash # hash\n" "" ' +hash = \# +target: + : hash $(hash) hash +' + # A '#' character in a command line doesn't start a comment testing "make hash in command line isn't a comment" \ "make -f -" \ -- cgit v1.2.3-55-g6feb