On Sat, Jan 31, 2026 at 09:19:00PM -0300, Bento Borges Schirmer wrote:
> Hey,
> 
> The following evaluates to "Hello, world!":
> 
> define(`a b',`Hello, world!')defn(`a b')
> 
> Is this standard? Or is it a GNU extension? Can I rely on this
> behavior? Sounds useful to fill the macro namespace without worrying.

GNU m4 has always supported arbitrary names.  And in fact, the manual
mentions "Arrays and associative arrays can be simulated by using
non-standard macro names." along with an example of "define(`array',
`defn(format(``array[%d]'', `$1'))')" as a nice use case for such
non-standard names.  But the wording in the manual says it is a GNU
extension.

A close read of POSIX:

https://pubs.opengroup.org/onlinepubs/9799919799/utilities/m4.html

does not offer much clarity.  It merely states:

"Name tokens shall consist of the longest possible sequence of
letters, digits, and underscores, where the first character is not a
digit.
...
define
    The second argument shall become the defining text of the macro
    whose name is the first argument."

but does not explicitly state whether or not the first argument must
be a name token or a quoted string that resolves to a name token,
vs. any arbitrary string that may serve as the name of a macro even if
it can't be recognized as a standalone macro name.

In practice, I've tested with BSD m4, which behaves almost like GNU m4
in allowing arbitrary strings to be defined as a macro, and then later
retrieved using defn, ifdef, or dumpdef, but with one caveat - BSD m4
will not support the empty string as a macro name, even though GNU m4
has no problem with that.  But beyond that, I can't definitively state
whether there are other implementations of m4 that will choke on
attempting to define an atypical macro name.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org


Reply via email to