Next: , Previous: Diversion support, Up: Programming in M4sugar


8.3.4 Conditional constructs

The following macros provide additional conditional contructs, as convenience wrappers around m4_if.

— Macro: m4_bmatch (string, regex-1, value-1, [regex-2], [value-2], ..., [default])

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.

— Macro: m4_bpatsubsts (string, regex-1, subst-1, [regex-2], [subst-2], ...)

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.

— Macro: m4_case (string, value-1, if-value-1, [value-2], [if-value-2], ..., [default])

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])
     
— Macro: m4_cond (test-1, value-1, if-value-1, [test-2], [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; while m4_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 expands m4_index once, instead of five times, for faster computation if this is a common case for `$1'. Notice that every third argument is unquoted for m4_if, and quoted for m4_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])
     
— Macro: m4_default (expr-1, expr-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.

— Macro: m4_ifndef (macro, if-not-defined, [if-defined])

This is shorthand for:

          m4_ifdef([macro], [if-defined], [if-not-defined])
     
— Macro: m4_ifset (macro, [if-true], [if-false])

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.

— Macro: m4_ifval (cond, [if-true], [if-false])

Expands to if-true if cond is not empty, otherwise to if-false. This is shorthand for:

          m4_if([cond], [], [if-true], [if-false])
     
— Macro: m4_ifvaln (cond, [if-true], [if-false])

Similar to m4_ifval, except guarantee that a newline is present after any non-empty expansion.

— Macro: m4_n (text)

Expand to text, and add a newline if text is not empty.