m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is it possible to expand macro just once?


From: M Douglas McIlroy
Subject: Re: Is it possible to expand macro just once?
Date: Mon, 12 Oct 2020 21:28:10 -0400

echo foo | m4 -Dfoo=\`bar\' -Dbar=baz


On Mon, Oct 12, 2020 at 8:24 AM Eric Blake <eblake@redhat.com> wrote:
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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]