m4: Macro Arguments
4.3 Macro arguments
===================
When a name is seen, and it has a macro definition, it will be expanded
as a macro.
If the name is followed by an opening parenthesis, the arguments will
be collected before the macro is called. If too few arguments are
supplied, the missing arguments are taken to be the empty string.
However, some builtins are documented to behave differently for a
missing optional argument than for an explicit empty string. If there
are too many arguments, the excess arguments are ignored. Unquoted
leading whitespace is stripped off all arguments, but whitespace
generated by a macro expansion or occurring after a macro that expanded
to an empty string remains intact. Whitespace includes space, tab,
newline, carriage return, vertical tab, and formfeed.
define(`macro', `$1')
=>
macro( unquoted leading space lost)
=>unquoted leading space lost
macro(` quoted leading space kept')
=> quoted leading space kept
macro(
divert `unquoted space kept after expansion')
=> unquoted space kept after expansion
macro(macro(`
')`whitespace from expansion kept')
=>
=>whitespace from expansion kept
macro(`unquoted trailing whitespace kept'
)
=>unquoted trailing whitespace kept
=>
Normally 'm4' will issue warnings if a builtin macro is called with
an inappropriate number of arguments, but it can be suppressed with the
'--quiet' command line option (or '--silent', or '-Q', ⇒Invoking
m4 Operation modes.). For user defined macros, there is no check of
the number of arguments given.
$ m4
index(`abc')
error->m4:stdin:1: Warning: too few arguments to builtin `index'
=>0
index(`abc',)
=>0
index(`abc', `b', `ignored')
error->m4:stdin:3: Warning: excess arguments to builtin `index' ignored
=>1
$ m4 -Q
index(`abc')
=>0
index(`abc',)
=>0
index(`abc', `b', `ignored')
=>1
Macros are expanded normally during argument collection, and whatever
commas, quotes and parentheses that might show up in the resulting
expanded text will serve to define the arguments as well. Thus, if FOO
expands to ', b, c', the macro call
bar(a foo, d)
is a macro call with four arguments, which are 'a ', 'b', 'c' and 'd'.
To understand why the first argument contains whitespace, remember that
unquoted leading whitespace is never part of an argument, but trailing
whitespace always is.
It is possible for a macro's definition to change during argument
collection, in which case the expansion uses the definition that was in
effect at the time the opening '(' was seen.
define(`f', `1')
=>
f(define(`f', `2'))
=>1
f
=>2
It is an error if the end of file occurs while collecting arguments.
hello world
=>hello world
define(
^D
error->m4:stdin:2: ERROR: end of file in argument list