[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: safe define
From: |
Eric Blake |
Subject: |
Re: safe define |
Date: |
Mon, 11 May 2015 08:24:47 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
On 05/11/2015 04:08 AM, Werner LEMBERG wrote:
>
> Folks,
>
>
> has someone ever written a `safe define' macro that makes the defined
> macro expands to itself if called without any argument?
Yes, the manual even has a section on how to do it:
https://www.gnu.org/software/m4/manual/m4.html#Composition
>
> sdefine({foo}, {bar})
>
> foo => foo
> foo() => bar
>
Looks like you have done changequote({,}) so I'll keep with that in my
response.
> I tried the following, and I failed (due to lack of experience
> w.r.t. quoting tricks, I think) – I have difficulties to delay the
> expansion of `$#'...
>
> Below, I've marked the problematic spots with (nonexisting)
> pseudo-functions.
>
> define({sdefine},
> {define({$1},
> {ifelse(delay_expansion_of({$#}),
> {0},
> {{$1}},
> {expand_at_definition_time(shift($@))})})})
define({sdefine}, {_$0({$1}, {$2}, {$}{#}, {$}{0})})
define({_sdefine}, {define({$1}, {ifelse({$3}, 0, {{$4}}, {$2})})})
The trick is realizing you need two functions: the outer sdefine that
uses quoting tricks to pass a literal $# and $0 to the inner function,
and the inner function that then does the desired define using the
literal strings in the right place.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
- safe define, Werner LEMBERG, 2015/05/11
- Re: safe define,
Eric Blake <=