Add AS_FOR, undocumented for now.

* lib/m4sugar/m4sh.m4 (AS_FOR): New macro.
* tests/m4sh.at (AS@&t@_FOR): New test.
Suggested by Paolo Bonzini.

Signed-off-by: Eric Blake <ebb9@byu.net>
This commit is contained in:
Eric Blake 2008-11-15 10:17:06 -07:00
parent 2db8890532
commit fdcbe002e9
3 changed files with 100 additions and 0 deletions

View File

@ -1,3 +1,10 @@
2008-11-15 Eric Blake <ebb9@byu.net>
Add AS_FOR, undocumented for now.
* lib/m4sugar/m4sh.m4 (AS_FOR): New macro.
* tests/m4sh.at (AS@&t@_FOR): New test.
Suggested by Paolo Bonzini.
2008-11-13 Eric Blake <ebb9@byu.net>
Optimize single-argument loop.

View File

@ -533,6 +533,33 @@ m4_define([AS_EXIT],
[{ (exit m4_default([$1], 1)); exit m4_default([$1], 1); }])
# AS_FOR(MACRO, SHELL-VAR, [LIST = "$@"], [BODY = :])
# ---------------------------------------------------
# Expand to a shell loop that assigns SHELL-VAR to each of the
# whitespace-separated entries in LIST (or "$@" if LIST is empty),
# then executes BODY. BODY may call break to abort the loop, or
# continue to proceed with the next element of LIST. Requires that
# IFS be set to the normal space-tab-newline. As an optimization,
# BODY should access MACRO rather than $SHELL-VAR. Normally, MACRO
# expands to $SHELL-VAR, but if LIST contains only a single element
# that needs no additional shell quoting, then MACRO will expand to
# that element, thus providing a direct value rather than a shell
# variable indirection.
#
# Only use the optimization if LIST can be used without additional
# shell quoting in either a literal or double-quoted context (that is,
# we give up on default IFS chars, parameter expansion, command
# substitution, shell quoting, globs, or quadrigraphs). Inline the
# m4_defn for speed.
m4_defun([AS_FOR],
[m4_pushdef([$1], m4_if(m4_translit([$3], ]dnl
m4_dquote(_m4_defn([m4_cr_symbols2]))[[%+=:,./-]), [], [[$3]], [[$$2]]))]dnl
[for $2[]m4_ifval([$3], [ in $3])
do
m4_default([$4], [:])
done[]_m4_popdef([$1])])
# AS_IF(TEST1, [IF-TRUE1 = :]...[IF-FALSE = :])
# ---------------------------------------------
# Expand into

View File

@ -906,6 +906,72 @@ m4_popdef([limit])
AT_CLEANUP
## -------- ##
## AS_FOR. ##
## -------- ##
AT_SETUP([AS@&t@_FOR])
AT_KEYWORDS([m4sh])
AT_DATA_M4SH([script.as], [[dnl
AS_INIT
# Simple checks.
AS_FOR([m4var], [shvar], [a],
[echo "m4var $shvar"])
AS_FOR([m4var], [shvar], [b c],
[echo "m4var $shvar"])
list='d e'
AS_FOR([m4var], [shvar], [$list],
[echo "m4var $shvar"])
AS_FOR([m4var], [shvar], ["$list"],
[echo "m4var $shvar"])
AS_FOR([m4var], [shvar], ['$list'],
[echo "m4var $shvar"])
AS_FOR([m4var], [shvar], [\'],
[echo "m4var $shvar"])
# Syntax checks: cope with empty arguments.
set f g
AS_FOR([], [shvar], [],
[echo "$shvar"])
rm -f file
AS_FOR([], [shvar], [`touch file`])
test -f file || exit 1
# Check that break works.
while :
do
AS_FOR([m4var], [shvar], [h i],
[echo "m4var"; break 2])
exit 1
done
while :
do
AS_FOR([m4var], [shvar], [j],
[echo "m4var"; break 2])
exit 1
done
]])
AT_CHECK_M4SH
AT_CHECK([./script], [0], [[a a
b b
c c
d d
e e
d e d e
$list $list
' '
f
g
h
j
]])
AT_CLEANUP
## --------------- ##
## AS_LITERAL_IF. ##
## --------------- ##