Next: , Previous: , Up: Programming in M4sugar   [Contents][Index]

15.6.4 Conditional constructs

The following macros provide additional conditional constructs 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. The overquoting is removed after the final substitution.

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)
Macro: m4_default_quoted (expr-1, expr-2)
Macro: m4_default_nblank (expr-1, [expr-2])
Macro: m4_default_nblank_quoted (expr-1, [expr-2])

If expr-1 contains text, use it. Otherwise, select expr-2. m4_default expands the result, while m4_default_quoted does not. Useful for providing a fixed default if the expression that results in expr-1 would otherwise be empty. The difference between m4_default and m4_default_nblank is whether an argument consisting of just blanks (space, tab, newline) is significant. When using the expanding versions, note that an argument may contain text but still expand to an empty string.

m4_define([active], [ACTIVE])dnl
m4_define([empty], [])dnl
m4_define([demo1], [m4_default([$1], [$2])])dnl
m4_define([demo2], [m4_default_quoted([$1], [$2])])dnl
m4_define([demo3], [m4_default_nblank([$1], [$2])])dnl
m4_define([demo4], [m4_default_nblank_quoted([$1], [$2])])dnl
demo1([active], [default])
⇒ACTIVE
demo1([], [active])
⇒ACTIVE
demo1([empty], [text])
⇒
-demo1([ ], [active])-
⇒- -
demo2([active], [default])
⇒active
demo2([], [active])
⇒active
demo2([empty], [text])
⇒empty
-demo2([ ], [active])-
⇒- -
demo3([active], [default])
⇒ACTIVE
demo3([], [active])
⇒ACTIVE
demo3([empty], [text])
⇒
-demo3([ ], [active])-
⇒-ACTIVE-
demo4([active], [default])
⇒active
demo4([], [active])
⇒active
demo4([empty], [text])
⇒empty
-demo4([ ], [active])-
⇒-active-
Macro: m4_define_default (macro, [default-definition])

If macro does not already have a definition, then define it to default-definition.

Macro: m4_ifblank (cond, [if-blank], [if-text])
Macro: m4_ifnblank (cond, [if-text], [if-blank])

If cond is empty or consists only of blanks (space, tab, newline), then expand if-blank; otherwise, expand if-text. Two variants exist, in order to make it easier to select the correct logical sense when using only two parameters. Note that this is more efficient than the equivalent behavior of:

m4_ifval(m4_normalize([cond]), if-text, if-blank)
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-false], [if-true])
Macro: m4_ifvaln (cond, [if-true], [if-false])

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

Macro: m4_n (text)

Expand to text, and add a newline if text is not empty. Often followed by dnl.

Next: , Previous: , Up: Programming in M4sugar   [Contents][Index]