Subject: Re: bug#18987: the bourne shell printf-vs-\xHH portability trap

From: Norihiro Tanaka <[email-masked]>
Date: Sun, 09 Nov 2014 08:38:18 +0900

On Sat, 8 Nov 2014 07:56:48 -0800
Jim Meyering <[email-masked]> wrote:
> Thank you for working on that.
> I've improved your patch: update the now-shared hex_printf_
> rather than making a copy, use a better definition of that function
> (knowing that "printf %s a b c d e" reuses the format string and
> prints just 5 bytes helps), also update word-multibyte to work
> with the new definition, and rewrite the commit log.
> 
> I'll push after you ACK:

Thanks for the review.  I added to a change the patch as word-multibyte
uses \OOO in printf simply, because use it in other test module e.g.
euc-mb.
From e7e8db4c7ab12ff952ad7a5d00958ef18c56b94e Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Sat, 8 Nov 2014 07:49:27 -0800
Subject: [PATCH] tests: avoid awk+printf+\xHH portability trap

* tests/init.cfg (hex_printf_): Rewrite in terms of printf and sed.
Using awk's printf with \xHH in the format string was not portable
to the awk of Solaris 10, AIX 7 or HP-UX 11.23, as reported in
http://debbugs.gnu.org/18987.
* tests/word-multibyte: Now that hex_printf_ can no longer handle
a trailing \n in the format string, adjust by removing it, and
instead invoking echo.
* tests/multibyte-white-space: Use to \OOO in printf instead of
hex_printf_.
---
 tests/init.cfg              | 8 ++++----
 tests/multibyte-white-space | 4 ++--
 tests/word-multibyte        | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/tests/init.cfg b/tests/init.cfg
index 9342d3d..1fb25f5 100644
--- a/tests/init.cfg
+++ b/tests/init.cfg
@@ -155,10 +155,10 @@ or use the shortcut target of the toplevel Makefile,
   fi
 }
 
-# Like printf with a single argument.
-# The difference is that while some printf implementations fail to
-# handle \xHH escapes, no awk implementation has that problem.
+# Like printf with a single argument, but that argument must be a
+# sequence of four-byte strings \xHH where each H is a hexadecimal byte.
 hex_printf_()
 {
-   ${AWK-awk} 'BEGIN { printf "'"$1"'" }' </dev/null
+  printf $(printf '\\%o' \
+    $(printf %s "$1"|sed 's,\\x\([0-9a-fA-f][0-9a-fA-f]\), 0x\1,g'))
 }
diff --git a/tests/multibyte-white-space b/tests/multibyte-white-space
index bb1bdaf..abd4dc5 100755
--- a/tests/multibyte-white-space
+++ b/tests/multibyte-white-space
@@ -58,9 +58,9 @@ EOF
 fail=0
 
 for i in $utf8_space_characters; do
-  hex_printf_ "$i\n" | grep -q '^\s$' \
+  hex_printf_ "$i" | grep -q '^\s$' \
       || { warn_ "$i FAILED to match \\s"; fail=1; }
-  hex_printf_ "$i\n" | grep -q '\S'
+  hex_printf_ "$i" | grep -q '\S'
   test $? = 1 \
       || { warn_ "$i vs. \\S FAILED"; fail=1; }
 done
diff --git a/tests/word-multibyte b/tests/word-multibyte
index f2caf97..4717c9e 100755
--- a/tests/word-multibyte
+++ b/tests/word-multibyte
@@ -4,7 +4,7 @@
 
 require_en_utf8_locale_
 
-hex_printf_ '\xc3\xa1\n' > in || framework_failure_
+printf '\303\241\n' > in || framework_failure_
 LC_ALL=en_US.UTF-8
 export LC_ALL
 
-- 
2.1.3