All known gettys in PTXdist allow inserting variables into /etc/issue by
prefixing certain characters with backslashes (e.g. a literal '\v'
expands to the kernel version from uname -v). In this case, '\'s in the
figlet output must be escaped accordingly so they are displayed as '\'s
on the screen.

For readability, refactor the code into a new local function. Then
add a new parameter describing the escape mode ('etcissue' or none, the
default) as a new, fifth parameter to install_replace_figlet. Currently,
install_replace_figlet is only used in rootfs.make on /etc/issue, and
the chance that we break existing code with the new default should be
small.

Signed-off-by: Roland Hieber <roh...@rohieb.name>
---
 rules/post/install.make           |  4 +++-
 scripts/lib/ptxd_make_xpkg_pkg.sh | 20 ++++++++++++++++++--
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/rules/post/install.make b/rules/post/install.make
index c40e36eb6..c0e24569e 100644
--- a/rules/post/install.make
+++ b/rules/post/install.make
@@ -301,14 +301,16 @@ install_replace = \
 # $2: filename
 # $3: placeholder
 # $4: value
+# $5: escape mode (empty or 'etcissue')
 #
 install_replace_figlet = \
        XPKG=$(subst _,-,$(strip $(1)));                                        
                \
        FILE=$(strip $(2));                                                     
                \
        PLACEHOLDER=$(strip $(3));                                              
                \
        VALUE=$(strip $(4));                                                    
                \
+       ESCAPEMODE=$(strip $(5));                                               
                        \
        $(call install_check, install_replace);                                 
                \
-       echo "ptxd_install_replace_figlet '$$FILE' '$$PLACEHOLDER' '$$VALUE'" 
>> "$(STATEDIR)/$$XPKG.cmds"
+       echo "ptxd_install_replace_figlet '$$FILE' '$$PLACEHOLDER' '$$VALUE' 
'$$ESCAPEMODE'" >> "$(STATEDIR)/$$XPKG.cmds"
 
 #
 # install_script_replace
diff --git a/scripts/lib/ptxd_make_xpkg_pkg.sh 
b/scripts/lib/ptxd_make_xpkg_pkg.sh
index 4efc31630..eb3452e3a 100644
--- a/scripts/lib/ptxd_make_xpkg_pkg.sh
+++ b/scripts/lib/ptxd_make_xpkg_pkg.sh
@@ -567,6 +567,7 @@ ptxd_install_replace_figlet() {
     local dst="$1"
     local placeholder="$2"
     local value="$3"
+    local escapemode="$4"
     local -a dirs ndirs pdirs sdirs ddirs
     local mod_nfs mod_rw
 
@@ -578,8 +579,23 @@ install replace figlet:
 " &&
 
     ptxd_exist "${dirs[@]/%/${dst}}" &&
-    figlet="$(figlet -d "${PTXDIST_SYSROOT_HOST}/share/figlet" -- "${value}" | 
\
-       awk '{ gsub("\\\\", "`"); if ($0 !~ "^ *$") printf("%s\\n", $0) }')" && 
#`
+    ptxd_figlet_helper() {
+        local value="$1"
+        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 }' ;;
+            *)         ;;
+        esac | \
+        awk '{ if ($0 !~ "^ *$") printf("%s\\n", $0) }'  # newlines for sed
+    } &&
+    figlet="$(ptxd_figlet_helper "$value" "$escapemode")" &&
     sed -i -e "s#${placeholder}#${figlet}#g" "${dirs[@]/%/${dst}}" ||
 
     ptxd_install_error "install_replace failed!"
-- 
2.15.0


_______________________________________________
ptxdist mailing list
ptxdist@pengutronix.de

Reply via email to