The following macros are useful in implementing recursive algorithms in
M4, including loop operations. An M4 list is formed by quoting a list
of quoted elements; generally the lists are comma-separated, although
m4_foreach_w is whitespace-separated. For example, the list
`[[a], [b,c]]' contains two elements: `[a]' and `[b,c]'.
It is common to see lists with unquoted elements when those elements are
not likely to be macro names, as in `[fputc_unlocked,
Expands to a quoted list of all but the first element of the comma-separated quoted list, or the empty string if list had only one element. Generally, when using quoted lists of quoted elements,
m4_cdrshould be called without any extra quotes.
For example, this is a simple implementation of
m4_map; note how each iteration checks for the end of recursion, then merely applies the first argument to the first element of the list, then repeats with the rest of the list. (The actual implementation in M4sugar is a bit more involved, to gain some speed and share code with
m4_map_sep).m4_define([m4_map], [m4_ifval([$2], [m4_apply([$1], m4_car($2))$0([$1], m4_cdr($2))])])dnl m4_map([ m4_eval], [[], [[1+1]], [,]]) => 1 2 a
m4_foreach([myvar], [[foo], [bar, baz]], [echo myvar ])dnl =>echo foo =>echo bar, baz
The deprecated macro
AC_FOREACHis an alias of
Loop over the comma separated quoted list of argument descriptions in list, and invoke macro with the arguments. An argument description is in turn a comma-separated quoted list of quoted elements, suitable for
m4_apply, making it possible to invoke macro without arguments if an argument description is empty.
m4_map_sepadditionally outputs separator between macro invocations, with no additional expansion of the separator.m4_map([m4_count], ) => m4_map([ m4_count], [, [], [, ]]) => 0 1 2 m4_map_sep([m4_eval], [,], [[[1+2]], [, ]]) =>3,a
m4_shiftnperforms count iterations of
m4_shift, along with validation that enough arguments were passed in to match the shift count, and that the count is positive.
m4_shift3are specializations of
m4_shiftn, introduced in Autoconf 2.62, and are more efficient for two and three shifts, respectively.