autoconf-patches
[Top][All Lists]
Advanced

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

AS_ESCAPE cleanups


From: Eric Blake
Subject: AS_ESCAPE cleanups
Date: Thu, 13 Nov 2008 19:30:59 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

I'm feeling a bit dumb at the moment.

_AS_VAR_APPEND_PREPARE called AS_ESCAPE([_AS_VAR_APPEND_WORKS]), which 
dutifully shell-quoted the literal string _AS_VAR_APPEND_WORKS (no \ added), 
_then_ performs m4 macro expansion.  Thus, the $as_var inside the "" wasn't 
escaped, the shell sees that "" != x12, and never used +=, even when it is 
available.  Which means we haven't really ever tested whether += actually 
speeds up bash.

Likewise for AS_VAR_ARITH.

I'm still debating about doing more with AS_ESCAPE; I have a pending patch that 
makes AS_ESCAPE noticeably faster if it can m4-quote its output, rather than 
the current behavior of having to rescan output for macro expansions.  This is 
generally the right thing to do, anyway - macro expansion after escapes have 
been added are only safe if the macros do not expand to anything that also 
needed escapes, and if that is the case, the macros could generally be safely 
expanded in advance.  Also, since AS_ESCAPE is (so far) undocumented, we might 
be able to get away with such a semantics change, after fixing places within 
autoconf that currently rely on late macro expansion.  On the other hand, 
autoconf itself has several places that would need to be edited, because it is 
currently relying on late macro expansion.  For example, Paolo's patches to add 
shell functions often do:
  "AS_ESCAPE([AC_INCLUDES_DEFAULT([$4])], [""])"

It currently works because the late expansion of AC_INCLUDES_DEFAULT is either 
$ac_includes_default (which doesn't need quoting) or its input argument (which 
was already escaped).  To avoid late expansion, we could write either:
  "AC_INCLUDES_DEFAULT(AS_ESCAPE([$4], [""]))"
or
  "AC_ESCAPE(m4_expand([AC_INCLUDES_DEFAULT([$4])]), [""])"

Unfortunately, even trying to change AS_VAR_SET to avoid late expansion 
backfired; using [eval "$1=AS_ESCAPE([[$2]])"] caused coreutils to fail, and 
blindly adding m4_expand to do early expansion slows things down.  So I'm not 
sure exactly what will be changing in this regards (for now, I just documented 
that late macro expansion in AS_VAR_SET better not result in shell 
metacharacters).

I'm just committing this, which leaves late expansion in place, and cleans up 
m4sh's use of AS_ESCAPE.


From: Eric Blake <address@hidden>
Date: Thu, 13 Nov 2008 12:17:01 -0700
Subject: [PATCH] Fix AS_ESCAPE usage bugs.

* lib/m4sugar/m4sh.m4 (_AS_VAR_APPEND_PREPARE)
(_AS_VAR_ARITH_PREPARE): Expand macros prior to adding shell
escapes.
(AS_TR_SH, AS_VAR_GET): Use _AS_ESCAPE for speed.
* doc/autoconf.texi (Polymorphic Variables) <AS_VAR_SET>: Document
caveat due to conditional AS_ESCAPE.
* tests/m4sh.at (AS@&address@hidden basics): Enhance test.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog           |   11 +++++++++++
 doc/autoconf.texi   |    4 +++-
 lib/m4sugar/m4sh.m4 |   10 ++++------
 tests/m4sh.at       |   13 +++++++------
 4 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 88ea131..3e9a701 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-11-13  Eric Blake  <address@hidden>
+
+       Fix AS_ESCAPE usage bugs.
+       * lib/m4sugar/m4sh.m4 (_AS_VAR_APPEND_PREPARE)
+       (_AS_VAR_ARITH_PREPARE): Expand macros prior to adding shell
+       escapes.
+       (AS_TR_SH, AS_VAR_GET): Use _AS_ESCAPE for speed.
+       * doc/autoconf.texi (Polymorphic Variables) <AS_VAR_SET>: Document
+       caveat due to conditional AS_ESCAPE.
+       * tests/m4sh.at (AS@&address@hidden basics): Enhance test.
+
 2008-11-12  Eric Blake  <address@hidden>
 
        Whitespace reduction in configure.
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index 4081604..07db254 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -12283,7 +12283,9 @@ Polymorphic Variables
 @var{var} to the shell expansion of @var{value}.  @var{value} is not
 subject to field splitting or file name expansion, so if command
 substitution is used, it may be done with @samp{`""`} rather than using
-an intermediate variable (@pxref{Shell Substitutions}).
+an intermediate variable (@pxref{Shell Substitutions}).  However,
address@hidden does undergo rescanning for additional macro names; behavior
+is unspecified if late expansion results in any shell meta-characters.
 @end defmac
 
 @defmac AS_VAR_SET_IF (@var{var}, @ovar{if-set}, @ovar{if-undef})
diff --git a/lib/m4sugar/m4sh.m4 b/lib/m4sugar/m4sh.m4
index 2790d8a..a39f702 100644
--- a/lib/m4sugar/m4sh.m4
+++ b/lib/m4sugar/m4sh.m4
@@ -1602,8 +1602,7 @@ m4_defun_init([AS_TR_SH],
 m4_dquote(m4_dquote(m4_defn([m4_cr_not_symbols2])))[[,
                                 [pp[]]]]dnl
 m4_dquote(m4_dquote(m4_for(,1,255,,[[_]])))[[)],
-             [`AS_ECHO(["m4_bpatsubst(m4_dquote(m4_expand([$1])),
-                                      [[\\`]], [\\\&])"]) | $as_tr_sh`])])
+  [`AS_ECHO(["_AS_ESCAPE(m4_dquote(m4_expand([$1])), [\`])"]) | $as_tr_sh`])])
 
 
 # _AS_TR_CPP_PREPARE
@@ -1668,7 +1667,7 @@ m4_defun([_AS_VAR_APPEND_PREPARE],
 VAR.  Take advantage of any shell optimizations that allow amortized
 linear growth over repeated appends, instead of the typical quadratic
 growth present in naive implementations.])
-AS_IF([_AS_RUN(["AS_ESCAPE([_AS_VAR_APPEND_WORKS])"])],
+AS_IF([_AS_RUN(["AS_ESCAPE(m4_quote(_AS_VAR_APPEND_WORKS))"])],
 [eval 'as_func_append ()
   {
     eval $[]1+=\$[]2
@@ -1709,7 +1708,7 @@ m4_defun([_AS_VAR_ARITH_PREPARE],
 [Perform arithmetic evaluation on the ARGs, and store the result in
 the global $as_val.  Take advantage of shells that can avoid forks.
 The arguments must be portable across $(()) and expr.])
-AS_IF([_AS_RUN(["AS_ESCAPE([_AS_VAR_ARITH_WORKS])"])],
+AS_IF([_AS_RUN(["AS_ESCAPE(m4_quote(_AS_VAR_ARITH_WORKS))"])],
 [eval 'as_func_arith ()
   {
     as_val=$(( $[]* ))
@@ -1761,8 +1760,7 @@ m4_define([AS_VAR_COPY],
 m4_define([AS_VAR_GET],
 [AS_LITERAL_IF([$1],
               [$$1],
-              [`eval 'as_val=${'m4_bpatsubst([$1], [[\\`]], [\\\&])'};dnl
-AS_ECHO(["$as_val"])'`])])
+  [`eval 'as_val=${'_AS_ESCAPE([[$1]], [\`])'};AS_ECHO(["$as_val"])'`])])
 
 
 # AS_VAR_IF(VARIABLE, VALUE, IF-TRUE, IF-FALSE)
diff --git a/tests/m4sh.at b/tests/m4sh.at
index 214b555..a8acf56 100644
--- a/tests/m4sh.at
+++ b/tests/m4sh.at
@@ -952,6 +952,7 @@ AT_KEYWORDS([AS@&address@hidden AS@&address@hidden)
 
 AT_DATA_M4SH([script.as], [[dnl
 AS_INIT
+ m4_define([with], [WITH])
 # Literals.
 dnl AS_VAR_SET_IF also covers AS_VAR_TEST_SET
 AS_VAR_SET_IF([foo], [echo oops]) && echo ok
@@ -1009,26 +1010,26 @@ m4_ifdef([tmp], [echo oops])
 
 AT_CHECK_M4SH
 AT_CHECK([./script], [], [[ok
-\a  "weird" `value` with; $fun 'characters
+\a  "weird" `value` WITH; $fun 'characters
 -
-\a  "weird" `value` with; $fun 'characters
+\a  "weird" `value` WITH; $fun 'characters
 -
 ok
 ok
 ok
 ====
 ok
-\a  "weird" `value` with; $fun 'characters
+\a  "weird" `value` WITH; $fun 'characters
 -
-\a  "weird" `value` with; $fun 'characters-
+\a  "weird" `value` WITH; $fun 'characters-
 ok
 ok
 ok
 ====
 ok
-\a  "weird" `value` with; $fun 'characters
+\a  "weird" `value` WITH; $fun 'characters
 -
-\a  "weird" `value` with; $fun 'characters-
+\a  "weird" `value` WITH; $fun 'characters-
 ok
 ok
 ok
-- 
1.6.0.2








reply via email to

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