All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yann E. MORIN <yann.morin.1998@free.fr>
To: buildroot@busybox.net
Subject: [Buildroot] [PATCH 1/7] core/pkg-utils: add macro to escape-and-printf
Date: Mon,  6 Jun 2016 22:43:38 +0200	[thread overview]
Message-ID: <11da03a97bde4e0332be6577a83e1476566d8e9f.1465245732.git.yann.morin.1998@free.fr> (raw)
In-Reply-To: <cover.1465245732.git.yann.morin.1998@free.fr>

In some cases we need to escape make variables and pass them to
printf(1).

This is the case in our fs infra, where we want to shoe-horn the
commands to generate the filesystems in the fakeroot script, or the
devices, permissions and users tables to their respective files.

We currently do so by replacing $(sep) with the literal '\n' but that's
not enough. This does not protect against strings with an embedded '%'
or a backslash.

Add a new macro that properly escapes a string and calls printf(1), so
that we get the expected output.

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
---
 package/pkg-utils.mk | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/package/pkg-utils.mk b/package/pkg-utils.mk
index f88313a..c61b3b6 100644
--- a/package/pkg-utils.mk
+++ b/package/pkg-utils.mk
@@ -104,6 +104,43 @@ define sep
 
 endef
 
+PERCENT = %
+QUOTE = '
+# ' # Meh... syntax-highlighting
+
+# This macro properly escapes a command string, then prints it with printf:
+#
+#   - first, backslash '\' are self-escaped, so that they do not escape
+#     the following char and so that printf properly outputs a backslash;
+#
+#   - next, single quotes are escaped by closing an existing one, adding
+#     an escaped one, and re-openning a new one (see below for the reason);
+#
+#   - then '%' signs are self-escaped so that the printf does not interpret
+#     them as a format specifier, in case the variable contains an actual
+#     printf with a format;
+#
+#   - finally, $(sep) is replaced with the literal '\n' so that make does
+#     not break on the so-expanded variable, but so that the printf does
+#     correctly output an LF.
+#
+# Note: this must be escaped in this order to avoid over-escaping the
+# previously escaped elements.
+#
+# Once everything has been escaped, it is passed between single quotes
+# (that's why the single-quotes are escaped they way they are, above,
+# and why the dollar sign is not escaped) to printf(1). A trailing
+# newline is apended, too.
+#
+# Note: leading or trailing spaces are *not* stripped.
+#
+define PRINTF
+	printf '$(subst $(sep),\n,\
+			$(subst $(PERCENT),$(PERCENT)$(PERCENT),\
+				$(subst $(QUOTE),$(QUOTE)\$(QUOTE)$(QUOTE),\
+					$(subst \,\\,$(1)))))\n'
+endef
+
 # check-deprecated-variable -- throw an error on deprecated variables
 # example:
 #   $(eval $(call check-deprecated-variable,FOO_MAKE_OPT,FOO_MAKE_OPTS))
-- 
2.7.4

  reply	other threads:[~2016-06-06 20:43 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-06 20:43 [Buildroot] [PATCH 0/7] fs: cleanups (branch yem/fs-fixes) Yann E. MORIN
2016-06-06 20:43 ` Yann E. MORIN [this message]
2016-06-06 21:07   ` [Buildroot] [PATCH 1/7] core/pkg-utils: add macro to escape-and-printf Thomas Petazzoni
2016-06-06 21:25     ` Yann E. MORIN
2016-06-06 22:54       ` Arnout Vandecappelle
2016-06-06 20:43 ` [Buildroot] [PATCH 2/7] fs: properly escape commands when generating fakeroot script Yann E. MORIN
2016-06-06 20:43 ` [Buildroot] [PATCH 3/7] fs/cloop: split long line Yann E. MORIN
2016-06-06 21:19   ` Thomas Petazzoni
2016-06-06 20:43 ` [Buildroot] [PATCH 4/7] fs/jffs2: split commands Yann E. MORIN
2016-06-06 20:43 ` [Buildroot] [PATCH 5/7] fs/squashfs: " Yann E. MORIN
2016-06-06 20:43 ` [Buildroot] [PATCH 6/7] fs/ubifs: fix the UBI commands Yann E. MORIN
2016-06-06 20:43 ` [Buildroot] [PATCH 7/7] fs/squashfs: remove useless chmod Yann E. MORIN
2016-06-06 21:01   ` Peter Korsgaard
2016-06-06 21:28     ` Yann E. MORIN
2016-06-06 21:06   ` Thomas Petazzoni
2016-06-06 21:16     ` Yann E. MORIN
2016-06-07 12:51       ` Peter Korsgaard
2016-06-07 19:12         ` Yann E. MORIN
2016-06-07 20:27 ` [Buildroot] [PATCH 0/7] fs: cleanups (branch yem/fs-fixes) Thomas Petazzoni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11da03a97bde4e0332be6577a83e1476566d8e9f.1465245732.git.yann.morin.1998@free.fr \
    --to=yann.morin.1998@free.fr \
    --cc=buildroot@busybox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.