]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
Add AS_FOR, undocumented for now.
authorEric Blake <ebb9@byu.net>
Sat, 15 Nov 2008 17:17:06 +0000 (10:17 -0700)
committerEric Blake <ebb9@byu.net>
Sat, 15 Nov 2008 17:41:19 +0000 (10:41 -0700)
* 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>
ChangeLog
lib/m4sugar/m4sh.m4
tests/m4sh.at

index 31dd4f2b1203a4aca52d44837d2ed87288578bf7..f99863f041f0a6d3482ef1f840de36fdb008ce67 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
index a39f702387de3a4b915496981339fe75202d97c0..f106e2db55a65d706f23038752209ee60f9e9030 100644 (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
index a8acf56d0d3cc3222e7ef2649b9fec9a79626a77..383d8211d3c36624aa739ed919e548c28bf66d8d 100644 (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.  ##
 ## --------------- ##