m4-discuss
[Top][All Lists]
Advanced

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

Re: New Feature : Divert2 to custom named file


From: Eric Blake
Subject: Re: New Feature : Divert2 to custom named file
Date: Mon, 30 Jan 2023 14:23:10 -0600
User-agent: NeoMutt/20220429

On Fri, Jan 27, 2023 at 07:33:35PM +0100, Denis Valois wrote:
> Hello André,
> 
> If I understand well, the function you request is already available.
> 
> Ref GM4 documentation section "10.2 Undiverting output".
> It states "As a GNU extension, diversions may contain non-numeric strings,
> which are treated as the names of files to copy into the output without
> expansion. A warning is issued if a file could not be opened."
> 
> In short, you divert as usual, into one or several divnum.  Then after you
> undivert into your specific files.

You've read that backwards.  undivert(`from_file') says to replay the
contents of "from_file" into m4's output stream unexpanded (contrasted
with include(`from_file') which says to include the contents of
"from_file" for further macro expansion).  But the original request
here is to redirect (a portion of) m4's output stream to a named file
"to_file" under the m4 script's control; there is nothing in m4 1.4.x
that directly does this.

> 
> Regards,
> 
> Denis/
> 
> On 27/01/2023 16:44, André Miville wrote:
> > Hello
> > I would like to have a divert2 function where one could write to a file,
> > not a temp file.

Because of m4's age, it is now a very high bar to add a new builtin
into m4.  There is an experimental branch for an eventual m4 2.0 that
would add loadable modules, where the backwards-compatibility aspects
of having new builtins is less important (put your new builtin in a
module that isn't loaded by default - code that wants to use it opts
in to loading that module), but no one has been actively developing on
that experiment for more than 10 years.  Current development is on the
m4-1.4 branch for stable releases, where introducing a new builtin
macro name risks breaking existing m4 scripts that assume such a word
is not a builtin macro.

A little less evil would be adding new meaning to the existing
divert() builtin, such that divert(`non-numeric') creates the file
"non-numeric" and collects output into it, without any intention of
rediverting it back into the main m4 output stream.  The reason this
might work is because existing m4 code is unlikely to call divert with
a non-numeric argument, because m4 has already been issuing a warning
about that.  Still, that seems like something better to introduce in a
new version such as 1.16.0 where it is obvious that features may be
present, than into a 1.14.x maintenance release.

> > I use M4 for CPPP C prepreprocessing
> > For instance one could use the divert2 function to transform a matrix.m4
> > into a matrix.h and a matrix.c
> > 
> > I am quite confused by sources and can't manage to do it by myself,
> > anyone to help me ?

It may be possible to do what you want by (ab)using the syscmd
builtin; something along the lines of the following (note that when I
write m4 code to interact with shell snippets, I prefer to use
changequote to something less likely to trip up on shell syntax than
m4's default of `'):

changequote([, ])dnl
define([body_h], ...m4 code to produce contents for file.h...)dnl
define([body_c], ...m4 code to produce contents for file.c...)dnl
syscmd([cat > body.h <<\EOF
]defn([body_h])[
EOF])ifelse(sysval, 0, , [handle error...])
syscmd([cat > body.c <<\EOF
]defn([body_c])[
EOF])ifelse(sysval, 0, , [handle error...])

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




reply via email to

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