[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Make AS_IF n-ary
From: |
Ralf Wildenhues |
Subject: |
Make AS_IF n-ary |
Date: |
Fri, 17 Feb 2006 18:00:35 +0100 |
User-agent: |
Mutt/1.5.11 |
2) Made AS_IF n-ary. Then, I extended the test suite because I tripped
in the "why in the world should AS_IF need to be m4_defun'ed?" trap
and don't want that to happen again.
1) Add a couple of notes to the docs to this end. I'm not certain
whether they are too confusing to the (ahem) casual reader.
OK to apply both patches?
BTW, I have left the m4_ifvaln in the AS_IF implementation; dunno what
its use (over simply an additional newline) ever was in this case,
though.
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.949
diff -u -r1.949 autoconf.texi
--- doc/autoconf.texi 15 Feb 2006 07:00:29 -0000 1.949
+++ doc/autoconf.texi 16 Feb 2006 22:21:18 -0000
@@ -9373,8 +9374,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:
address@hidden 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 @code{FOO}. For instance, this definition of macros:
@example
@group
@@ -9680,10 +9683,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}, it 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.949
diff -u -r1.949 autoconf.texi
--- doc/autoconf.texi 15 Feb 2006 07:00:29 -0000 1.949
+++ doc/autoconf.texi 16 Feb 2006 22:40:52 -0000
@@ -9024,11 +9024,12 @@
@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}
+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.
@end defmac
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 16 Feb 2006 22:40:55 -0000
@@ -437,24 +438,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 16 Feb 2006 22:40: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
- Make AS_IF n-ary,
Ralf Wildenhues <=