[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Make AS_IF n-ary
From: |
Ralf Wildenhues |
Subject: |
Re: Make AS_IF n-ary |
Date: |
Mon, 20 Feb 2006 20:57:38 +0100 |
User-agent: |
Mutt/1.5.9i |
Hi Paul,
* Paul Eggert wrote on Mon, Feb 20, 2006 at 11:09:29AM CET:
> Ralf Wildenhues <address@hidden> writes:
>
> > address@hidden be more precise, the required macro will be
> > +expanded before the outermost @code{AC_DEFUN}'d macro in the current
> > +expansion stack.}
>
> Can you please reword this so that it doesn't need a footnote?
> Footnotes are pretty awkward in GNU documentation.
Agreed. It may be ok to just put it at the point in the normal text.
> > @xref{Macro Definitions}, for details on how to define a macro. If a
> > +macro doesn't use @code{AC_REQUIRE}, it is expected to never be the
> > +object of an @code{AC_REQUIRE} directive
>
> That doesn't sound right.
Indeed. The part was there before, but I accidentally removed its
original meaning. It should have been:
| If a macro doesn't use AC_REQUIRE, is expected to never .., and
| ..not.., then ..
so I have applied s/it // in the patch below to (hopefully) make this
clearer now.
Furthermore, I previously overlooked the missing but necessary M4 quotes
in the following sentence:
| In particular, @samp{AC_REQUIRE([FOO])} ..
> An example for n-ary AS_IF would help, in the manual.
I'm not proud of the one I added, but if hopefully conveys the
semantics.
> The code looks fine to me (not that I followed everything!).
Thanks for the review! Updated patches below.
Cheers,
Ralf
* doc/autoconf.texi (Prerequisite Macros): State more precisely
where a required macro will be expanded.
(Coding Style): Another reason not to use `m4_define'.
Index: doc/autoconf.texi
===================================================================
RCS file: /cvsroot/autoconf/autoconf/doc/autoconf.texi,v
retrieving revision 1.950
diff -u -r1.950 autoconf.texi
--- doc/autoconf.texi 20 Feb 2006 08:41:53 -0000 1.950
+++ doc/autoconf.texi 20 Feb 2006 19:57:55 -0000
@@ -9373,8 +9384,10 @@
@code{AC_REQUIRE} is often misunderstood. It really implements
dependencies between macros in the sense that if one macro depends upon
another, the latter will be expanded @emph{before} the body of the
-former. In particular, @samp{AC_REQUIRE(FOO)} is not replaced with the
-body of @code{FOO}. For instance, this definition of macros:
+former. To be more precise, the required macro will be expanded before
+the outermost @code{AC_DEFUN}'d macro in the current expansion stack.
+In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
address@hidden For instance, this definition of macros:
@example
@group
@@ -9680,10 +9693,12 @@
to the shell and are less likely to occur in file names.
@xref{Macro Definitions}, for details on how to define a macro. If a
-macro doesn't use @code{AC_REQUIRE} and it is expected to never be the
-object of an @code{AC_REQUIRE} directive, then use @code{m4_define}. In
-case of doubt, use @code{AC_DEFUN}. All the @code{AC_REQUIRE}
-statements should be at the beginning of the macro, @code{dnl}'ed.
+macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
+of an @code{AC_REQUIRE} directive, and macros required by other macros
+inside arguments will not need to be expanded before this macro, then
+use @code{m4_define}. In case of doubt, use @code{AC_DEFUN}.
+All the @code{AC_REQUIRE} statements should be at the beginning of the
+macro, @code{dnl}'ed.
You should not rely on the number of arguments: instead of checking
whether an argument is missing, test that it is not empty. It provides
###
* lib/m4sugar/m4sh.m4 (AS_IF): Extend to allow more than one
test, as in `if tests; then cmd1; elif ...; else ...; fi'.
* doc/autoconf.texi (Programming in M4sh): Adjusted.
* tests/m4sh.at (AS_IF and AS_CASE): Test this. Also make sure
both macros are defun'ed so that required macros are evaluated
outside.
Index: doc/autoconf.texi
===================================================================
RCS file: /cvsroot/autoconf/autoconf/doc/autoconf.texi,v
retrieving revision 1.950
diff -u -r1.950 autoconf.texi
--- doc/autoconf.texi 20 Feb 2006 08:41:53 -0000 1.950
+++ doc/autoconf.texi 20 Feb 2006 19:57:55 -0000
@@ -9024,12 +9024,23 @@
@command{dirname} command.
@end defmac
address@hidden AS_IF (@var{test}, @ovar{run-if-true}, @ovar{run-if-false})
address@hidden AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{},
@ovar{run-if-false})
@asindex{IF}
-Run shell code @var{test}. If @var{test} exits with a zero status then
-run shell code @var{run-if-true}, else run shell code @var{run-if-false},
-with simplifications if either @var{run-if-true} or @var{run-if-false}
-is empty.
+Run shell code @var{test1}. If @var{test1} exits with a zero status then
+run shell code @var{run-if-true1}, else examine further tests. If no test
+exits with a zero status, run shell code @var{run-if-false}, with
+simplifications if either @var{run-if-true1} or @var{run-if-false1}
+is empty. For example,
+
address@hidden
+AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
+ [test "$foo" != no], [HANDLE_FOO([maybe])],
+ [echo foo not specified])
address@hidden example
+
address@hidden
+will make sure any @code{AC_REQUIRE}'s macros of @code{HANDLE_FOO} will
+be expanded before the first test.
@end defmac
@defmac AS_MKDIR_P (@var{file-name})
Index: lib/m4sugar/m4sh.m4
===================================================================
RCS file: /cvsroot/autoconf/autoconf/lib/m4sugar/m4sh.m4,v
retrieving revision 1.159
diff -u -r1.159 m4sh.m4
--- lib/m4sugar/m4sh.m4 15 Feb 2006 07:00:29 -0000 1.159
+++ lib/m4sugar/m4sh.m4 20 Feb 2006 19:57:55 -0000
@@ -437,24 +437,34 @@
[{ (exit m4_default([$1], 1)); exit m4_default([$1], 1); }])
-# AS_IF(TEST, [IF-TRUE], [IF-FALSE])
-# ----------------------------------
+# AS_IF(TEST1, [IF-TRUE1]...[IF-FALSE])
+# -------------------------------------
# Expand into
-# | if TEST; then
-# | IF-TRUE
+# | if TEST1; then
+# | IF-TRUE1
+# | elif TEST2; then
+# | IF-TRUE2
+# [...]
# | else
# | IF-FALSE
# | fi
-# with simplifications is IF-TRUE and/or IF-FALSE is empty.
+# with simplifications if IF-TRUE1 and/or IF-FALSE is empty.
#
-# FIXME: Be n-ary, just as m4_if.
+m4_define([_AS_IF],
+[m4_ifval([$2$3],
+[elif $1; then
+ m4_default([$2], [:])
+m4_ifval([$3], [$0(m4_shiftn(2, $@))])],
+[m4_ifvaln([$1],
+[else
+ $1])dnl
+])dnl
+])# _AS_IF
m4_defun([AS_IF],
[m4_ifval([$2$3],
[if $1; then
m4_default([$2], [:])
-m4_ifvaln([$3],
-[else
- $3])dnl
+m4_ifval([$3], [_$0(m4_shiftn(2, $@))])[]dnl
fi
])dnl
])# AS_IF
Index: tests/m4sh.at
===================================================================
RCS file: /cvsroot/autoconf/autoconf/tests/m4sh.at,v
retrieving revision 1.44
diff -u -r1.44 m4sh.at
--- tests/m4sh.at 15 Feb 2006 07:00:29 -0000 1.44
+++ tests/m4sh.at 20 Feb 2006 19:57:55 -0000
@@ -545,24 +545,91 @@
AS_INIT
# Syntax checks: cope with empty arguments.
AS_IF([:], [], [echo wrong])
-AS_IF([:], [:], [echo wrong])
-AS_IF([false], [echo wrong], [:])
+AS_IF([:], [echo one], [echo wrong])
+AS_IF([false], [echo wrong], [echo two])
AS_IF([false], [echo wrong])
+# n-ary version
+AS_IF([false], [echo wrong],
+ [:], [echo three])
+AS_IF([false], [echo wrong],
+ [:], [echo four],
+ [echo wrong])
+AS_IF([false], [echo wrong],
+ [false], [echo wrong])
+AS_IF([false], [echo wrong],
+ [false], [echo wrong],
+ [echo five])
+AS_IF([false], [echo wrong],
+ [false], [echo wrong],
+ [:], [echo six],
+ [echo wrong])
AS_CASE([foo])
-AS_CASE([foo], [:])
+AS_CASE([foo], [echo seven])
AS_CASE([foo],
- [foo], [:],
+ [foo], [echo eight],
[echo wrong])
AS_CASE([foo],
- [foo], [:],
+ [foo], [echo nine],
[*], [echo wrong])
AS_CASE([foo],
[bar], [echo wrong],
- [foo], [:],
+ [foo], [echo ten],
[*], [echo wrong])
+
+# check that require works correctly
+m4_for([n], 1, 9, [],
+[m4_defun([FOO]n, [foo]n[=]n)dnl
+m4_defun([BAR]n,
+ [m4_require([FOO]]n[)dnl
+bar]n[=]n)[]dnl
+])
+
+AS_IF([:], [BAR1])
+echo "foo1=$foo1 bar1=$bar1"
+AS_IF([:], [], [BAR2])
+echo "foo2=$foo2 bar2=$bar2"
+AS_IF([false], [BAR3])
+echo "foo3=$foo3 bar3=$bar3"
+AS_IF([false], [], [BAR4])
+echo "foo4=$foo4 bar4=$bar4"
+AS_CASE([x], [x], [BAR5])
+echo "foo5=$foo5 bar5=$bar5"
+AS_CASE([x], [y], [BAR6])
+echo "foo6=$foo6 bar6=$bar6"
+AS_CASE([x],
+ [x], [:],
+ [BAR7])
+echo "foo7=$foo7 bar7=$bar7"
+AS_CASE([x],
+ [y], [:],
+ [BAR8])
+echo "foo8=$foo8 bar8=$bar8"
+AS_CASE([x],
+ [y], [:],
+ [x], [BAR9])
+echo "foo9=$foo9 bar9=$bar9"
]])
AT_CHECK_M4SH
-AT_CHECK([./script])
+AT_CHECK([./script], [0], [[one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+foo1=1 bar1=1
+foo2=2 bar2=
+foo3=3 bar3=
+foo4=4 bar4=4
+foo5=5 bar5=5
+foo6=6 bar6=
+foo7=7 bar7=
+foo8=8 bar8=8
+foo9=9 bar9=9
+]])
AT_CLEANUP