* 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.
This commit is contained in:
Ralf Wildenhues 2006-02-21 09:30:01 +00:00
parent 8ff7792b2a
commit a628e04465
4 changed files with 116 additions and 21 deletions

View File

@ -1,5 +1,12 @@
2006-02-21 Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
* 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.
* doc/autoconf.texi (Prerequisite Macros): State more precisely
where a required macro will be expanded.
(Coding Style): Another reason not to use `m4_define'.

View File

@ -9068,12 +9068,23 @@ details about what this returns and why it is more portable than the
@command{dirname} command.
@end defmac
@defmac AS_IF (@var{test}, @ovar{run-if-true}, @ovar{run-if-false})
@defmac 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,
@example
AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
[test "$foo" != no], [HANDLE_FOO([maybe])],
[echo foo not specified])
@end example
@noindent
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})

View File

@ -437,24 +437,34 @@ m4_define([AS_EXIT],
[{ (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

View File

@ -545,24 +545,91 @@ AT_DATA_M4SH([script.as], [[dnl
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