[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCHv3] m4sugar: factor away _AS_ECHO_PREPARE.

From: Gary V. Vaughan
Subject: [PATCHv3] m4sugar: factor away _AS_ECHO_PREPARE.
Date: Tue, 29 Jan 2013 13:17:29 +0700

Incorporating feedback from Paul and Paul.  Thank you both :)

Okay to push?

"printf '%s\n' ..." has been a fine replacement for plain "echo"
for at least 5 years (probably more like 10), even with most
museum-piece shells.
* lib/m4sugar/m4sh.m4 (_AS_ECHO_PREPARE): Remove.
(_AS_SHELL_SANITIZE): Keep as_nl setting originally from
_AS_ECHO_PREPARE here where it more properly belongs.
(AS_ECHO, AS_ECHO_N): Use printf unconditionally.
* doc/autoconf.texi (Limitations of Shell Builtins): Document
preference for 'printf' over working around 'echo' bugs.
* NEWS: Updated.
Reported by Jim Meyering.
 NEWS                |  4 ++++
 doc/autoconf.texi   | 19 +++++++----------
 lib/m4sugar/m4sh.m4 | 60 +++++++----------------------------------------------
 3 files changed, 19 insertions(+), 64 deletions(-)

diff --git a/NEWS b/NEWS
index a9b2226..3934a06 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,10 @@ GNU Autoconf NEWS - User visible changes.
 ** Older version of automake and aclocal (< 1.8) are no longer supported
    by autoreconf.
+** Use of printf is now recommended instead of working around bugs in
+   echo.  Macros AS_ECHO and AS_ECHO_N now expand unconditionally to
+   "printf '%s\n'" and "printf %s".
 ** Macros
 - New macro AC_C__GENERIC.
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index bb83443..7011da6 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -17494,12 +17494,14 @@ characters to @command{echo}.  For example, 
@samp{echo "$foo"} is safe
 only if you know that @var{foo}'s value cannot contain backslashes and
 cannot start with @samp{-}.
-If this may not be true, @command{printf} is in general safer and
-easier to use than @command{echo} and @command{echo -n}.  Thus, scripts
-where portability is not a major concern should use @command{printf
-'%s\n'} whenever @command{echo} could fail, and similarly use
address@hidden %s} instead of @command{echo -n}. For portable shell
-scripts, instead, it is suggested to use a here-document like this:
+Normally, @command{printf} is safer and easier to use than @command{echo}
+and @command{echo -n}.  Thus, you should use @command{printf '%s\n'}
+instead of @command{echo}, and similarly use @command{printf %s} instead
+of @command{echo -n}.
+Older scripts, written before @command{printf} was portable,
+sometimes used a here-document as a safer alternative to @command{echo},
+like this:
 cat <<EOF
@@ -17507,11 +17509,6 @@ $foo
 @end example
-Alternatively, M4sh provides @code{AS_ECHO} and @code{AS_ECHO_N} macros
-which choose between various portable implementations: @samp{echo}
-or @samp{print} where they work, @command{printf} if it is available,
-or else other creative tricks in order to work around the above problems.
 @item @command{eval}
 @c -----------------
diff --git a/lib/m4sugar/m4sh.m4 b/lib/m4sugar/m4sh.m4
index 25b4fe3..479c2d9 100644
--- a/lib/m4sugar/m4sh.m4
+++ b/lib/m4sugar/m4sh.m4
@@ -453,7 +453,6 @@ m4_defun([_AS_SHELL_SANITIZE],
 [m4_text_box([M4sh Initialization.])
 # IFS
@@ -461,6 +460,9 @@ _AS_PATH_SEPARATOR_PREPARE
 # there to prevent editors from complaining about space-tab.
 # (If _AS_PATH_WALK were called with IFS unset, it would disable word
 # splitting by setting IFS to empty value.)
+export as_nl
 IFS=" ""       $as_nl"
 # Find who we are.  Look in the path if we contain no directory separator.
@@ -1023,63 +1025,15 @@ fi
 # Output WORD followed by a newline.  WORD must be a single shell word
 # (typically a quoted string).  The bytes of WORD are output as-is, even
 # if it starts with "-" or contains "\".
-[$as_echo $1])
+[printf '%s\n' $1])
 # ---------------
 # Like AS_ECHO(WORD), except do not output the trailing newline.
-[$as_echo_n $1])
-# ----------------
-# Arrange for $as_echo 'FOO' to echo FOO without escape-interpretation;
-# and similarly for $as_echo_n, which omits the trailing newline.
-# 'FOO' is an optional single argument; a missing FOO is treated as empty.
-export as_nl
-# Printing a long string crashes Solaris 7 /usr/bin/printf.
-# Prefer a ksh shell builtin over an external printf program on Solaris,
-# but without wasting forks for bash or zsh.
-    && (test "X`print -r -- $as_echo`" = "X$as_echo") 2>/dev/null; then
-  as_echo='print -r --'
-  as_echo_n='print -rn --'
-elif (test "X`printf %s $as_echo`" = "X$as_echo") 2>/dev/null; then
-  as_echo='printf %s\n'
-  as_echo_n='printf %s'
-  if test "X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`" = "X-n $as_echo"; 
-    as_echo_body='eval /usr/ucb/echo -n "$][1$as_nl"'
-    as_echo_n='/usr/ucb/echo -n'
-  else
-    as_echo_body='eval expr "X$][1" : "X\\(.*\\)"'
-    as_echo_n_body='eval
-      arg=$][1;
-      case $arg in @%:@(
-      *"$as_nl"*)
-       expr "X$arg" : "X\\(.*\\)$as_nl";
-       arg=`expr "X$arg" : ".*$as_nl\\(.*\\)"`;;
-      esac;
-      expr "X$arg" : "X\\(.*\\)" | tr -d "$as_nl"
-    '
-    export as_echo_n_body
-    as_echo_n='sh -c $as_echo_n_body as_echo'
-  fi
-  export as_echo_body
-  as_echo='sh -c $as_echo_body as_echo'
+[printf %s $1])

reply via email to

[Prev in Thread] Current Thread [Next in Thread]