Re: Is it possible to expand macro just once?

Eric Blake <[email protected]> Mon, 12 Oct 2020 07:24:22 -0500
Newsgroups gmane.comp.gnu.m4.general
Organization Red Hat, Inc.
Message-ID <[email protected]>
On 10/12/20 5:27 AM, Wolf wrote:
> Hello,
> 
> Not subscribed to the list, so please CC.

That's standard practice anyway ;)

> 
> I have m4 template that contains macro, that I do not know the value of
> (provided by user). That can lead to user setting the macro to name of
> another macro, like:
> 
> 	$ echo foo | m4 -Dfoo=bar -Dbar=baz
> 	baz
> 
> Now, I understand why it happens and that it is expected. However, due
> to reason stated above, I need the macro to expand just once, so that
> the snippet above would produce `bar`. Is there a way to do so?

In general, if the user is providing you text rather than something that 
should expand as a macro that needs further recursive interpretation, 
you should use defn() to get at that text:

$ echo 'changequote([,])defn([foo])' | m4 -Dfoo=bar -Dbar=baz
bar

But keep in mind that defn does NOT perform any interpolation of $, 
which may introduce its own oddities if the user supplied a macro 
definition containing $ that you aren't expanding as a macro.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org