mailarchive of the ptxdist mailing list
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: ptxdist@pengutronix.de
Subject: [ptxdist] [PATCH] ptxd_install_replace_figlet: Use sed instead of awk to quote backslashes
Date: Wed, 19 Sep 2018 10:43:36 +0200	[thread overview]
Message-ID: <20180919084336.4357-1-u.kleine-koenig@pengutronix.de> (raw)

TL;DR: awk and backslashes in the replacement argument of gsub is ugly.

With GNU Awk 4.0.2 we have:

	$ echo '\' | awk '{ gsub("\\\\", "\\\\\\\\\\\\\\\\"); print }'
	\\\\\\\\

and with GNU Awk 4.1.4 (and newer) we have:

	$ echo '\' | awk '{ gsub("\\\\", "\\\\\\\\\\\\\\\\"); print }'
	\\\\

This results in too much backslashes in /etc/issues for users of the
former version.

For the glory details I recommend reading through

	https://www.gnu.org/software/gawk/manual/html_node/Gory-Details.html
	https://bugs.debian.org/909147

As it's awkward to come up with an expression that does the same on both
versions (e.g. using --posix) use sed instead.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 scripts/lib/ptxd_make_xpkg_pkg.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/scripts/lib/ptxd_make_xpkg_pkg.sh b/scripts/lib/ptxd_make_xpkg_pkg.sh
index 5e69cefdc85d..685f4e549446 100644
--- a/scripts/lib/ptxd_make_xpkg_pkg.sh
+++ b/scripts/lib/ptxd_make_xpkg_pkg.sh
@@ -584,13 +584,13 @@ install replace figlet:
         local escapemode="$2"
         figlet -d "${PTXDIST_SYSROOT_HOST}/share/figlet" -- "${value}" | \
         case "$escapemode" in
-            # a lot of leaning toothpicks because we need to escape a literal
-            # '\' with '\\' on multiple levels:
-            # - one level for the string inside awk: \\\\\\\\\\\\\\\\ -> \\\\\\\\
-            # - one level for the shell string after sed -e:          -> \\\\
-            # - one level for the s expression inside sed:            -> \\
-            # - and finally, one level for /etc/issue:                -> \
-            etcissue)	awk '{ gsub("\\\\", "\\\\\\\\\\\\\\\\"); print }' ;;
+            # /etc/issue needs each backslash quoted by another backslash. As
+            # the string is interpreted by the shell once more below, another
+            # level of quoting is needed such that every \ in the output of
+            # figlet needs to be replaced by \\\\. As a \ in sed needs to be
+            # quoted, too, this results in eight backslashes in the replacement
+            # string.
+            etcissue)	sed 's,\\,\\\\\\\\,';;
             *)		;;
         esac | \
         awk '{ if ($0 !~ "^ *$") printf("%s\\n", $0) }'  # newlines for sed
-- 
2.19.0


_______________________________________________
ptxdist mailing list
ptxdist@pengutronix.de

             reply	other threads:[~2018-09-19  8:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-19  8:43 Uwe Kleine-König [this message]
2018-10-05 15:50 ` Robert Schwebel
2018-10-05 20:52   ` [ptxdist] [PATCH] ptxd_install_replace_figlet: replace all backslashes, not only the first Uwe Kleine-König
2018-10-06 10:30     ` Robert Schwebel

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=20180919084336.4357-1-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=ptxdist@pengutronix.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox