m4-discuss
[Top][All Lists]
Advanced

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

Re: Comma in the output of esyscmd


From: Eric Blake
Subject: Re: Comma in the output of esyscmd
Date: Wed, 24 Sep 2008 19:20:26 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080708 Thunderbird/2.0.0.16 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Vladimir Panov on 9/24/2008 2:45 PM:
> $ pwd
> /home/gbr/ala, bala
> 
> What I'd like is the result to be:
> `/home/gbr/ala, bala
> '

This is a general problem with m4 - there is no generic way to treat truly
arbitrary text as a single quoted string, because there are no escape
sequences.  In short, you have to guarantee that esyscmd will never
generate text with unbalanced quotes if you plan on feeding it as an
argument to another macro.

If you don't mind losing spaces, then your example works if you use quote
to combine multiple arguments into one, then dquote to double-quote the
resulting string:

$ m4 -I /usr/share/doc/m4/examples/
include(`quote.m4')dnl
dquote(quote(esyscmd(`pwd')))
=> `/home/gbr/ala,bala
=> '

If you want to keep the spaces, then we have to resort to changequote, by
choosing a secondary set of quotes, also unlikely to appear in your output
(by the way, this is roughly how m4_expand works in autoconf):

$ m4 -I /usr/share/doc/m4/examples/
include(`quote.m4')dnl
define(`expand',`_$0(-=<{($1)}>=-)')dnl
define(`_expand',`changequote(`-=<{(',`)}>=-')$1changequote`'')dnl
dquote(expand(`esyscmd(`pwd')'))
=> `/home/gbr/ala, bala
=> '

(and if you don't see how that works right away, don't worry - it took me
several months to get it right in autoconf.  'm4 -daeqt' might be
instructive, to show you what's going on there.)

If you plan on using lots of [e]syscmd, consider using changequote to
select something nicer than ` and ', to make writing properly-quoted shell
commands easier (for example, autoconf's m4sugar layer on top of m4 uses [
and ], because those are less likely to appear unbalanced in shell scripts).

This would be a lot easier with the addition of the qindir builtin
(planned for m4 1.6), which can add a level of quoting around the
expansion of any builtin, but even that still won't help if esyscmd
resulted in unbalanced quotes (hence the reason that autoconf introduced
the notion of quadrigraphs, where the m4sugar wrapper language
post-processes m4 output to allow the generation of unbalanced [ or ]).
Maybe someday the post-processing of quadrigraphs can also be built into
m4 (ie. introduce quadrigraphs as m4's escape sequence).

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkja51oACgkQ84KuGfSFAYDmRACgpvIGIQMU6cUcEpixtA3io9K3
op8An2PDf60Ki6N58grIMlJinVF+PJTT
=9Up2
-----END PGP SIGNATURE-----




reply via email to

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