Next: Looping constructs, Previous: Diversion support, Up: Programming in M4sugar
The following macros provide additional conditional contructs, as
convenience wrappers around m4_if
.
The string string is repeatedly compared against a series of regex arguments; if a match is found, the expansion is the corresponding value, otherwise, the macro moves on to the next regex. If no regex match, then the result is the optional default, or nothing.
The string string is altered by regex-1 and subst-1, as if by:
m4_bpatsubst([[string]], [regex], [subst])The result of the substitution is then passed through the next set of regex and subst, and so forth. An empty subst implies deletion of any matched portions in the current string. Note that this macro over-quotes string; this behavior is intentional, so that the result of each step of the recursion remains as a quoted string. However, it means that anchors (`^' and `$' in the regex will line up with the extra quotations, and not the characters of the original string. The overquoting is removed after the final substitution.
Test string against multiple value possibilities, resulting in the first if-value for a match, or in the optional default. This is shorthand for:
m4_if([string], [value-1], [if-value-1], [string], [value-2], [if-value-2], ..., [default])
This macro was introduced in Autoconf 2.62. Similar to
m4_if
, except that each test is expanded only when it is encountered. This is useful for short-circuiting expensive tests; whilem4_if
requires all its strings to be expanded up front before doing comparisons,m4_cond
only expands a test when all earlier tests have failed.For an example, these two sequences give the same result, but in the case where `$1' does not contain a backslash, the
m4_cond
version only expandsm4_index
once, instead of five times, for faster computation if this is a common case for `$1'. Notice that every third argument is unquoted form4_if
, and quoted form4_cond
:m4_if(m4_index([$1], [\]), [-1], [$2], m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2], m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2], m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3], m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3], [$2]) m4_cond([m4_index([$1], [\])], [-1], [$2], [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2], [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2], [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3], [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3], [$2])
If expr-1 is not empty, use it. Otherwise, expand to expr-2. Useful for providing a fixed default if the expression that results in expr-1 would otherwise be empty.
m4_ifdef([macro], [if-defined], [if-not-defined])
If macro is undefined, or is defined as the empty string, expand to if-false. Otherwise, expands to if-true. Similar to:
m4_ifval(m4_defn([macro]), [if-true], [if-false])except that it is not an error if macro is undefined.
Expands to if-true if cond is not empty, otherwise to if-false. This is shorthand for:
m4_if([cond], [], [if-true], [if-false])