mailarchive of the ptxdist mailing list
 help / color / mirror / Atom feed
* [ptxdist] [PATCH] ptxd_install_replace_figlet: Use sed instead of awk to quote backslashes
@ 2018-09-19  8:43 Uwe Kleine-König
  2018-10-05 15:50 ` Robert Schwebel
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2018-09-19  8:43 UTC (permalink / raw)
  To: ptxdist

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-10-06 10:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-19  8:43 [ptxdist] [PATCH] ptxd_install_replace_figlet: Use sed instead of awk to quote backslashes Uwe Kleine-König
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox