Re: Having Trouble with m4 Input (m4 version 1.4.13)

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Organization Red Hat
Message-ID <[email protected]>
On 09/03/2010 09:56 AM, Tom Browder wrote:
> I am trying to use m4 (version 1.4.13) to generate a C header file and
> am approaching it cautiously because my experience with m4 has not
> been comforting.

You may want to consider upgrading to 1.4.15, although I don't think 
that will impact your usage issue.

>
> My initial m4 input file has these lines:
>
> =====>
> divert(-1)
> define(`hashdef', `#define')dnl
> define(`Min', include(`conf/MINOR'))
> divert(0)dnl
> hashdef __PROGVER_MINOR__ Min
> <====
>
> When I execute "m4<m4 input file>" I get:
>
> ====>
> #define __PROGVER_MINOR__ Min
> <====
>
> Notice 'Min is not expanded.

> Is the '#' stopping it?

Correct.  And that's because, by default, # is the m4 comment character, 
and once hashdef is expanded, you have turned the rest of the line into 
an m4 comment.

>  If so, how can I get the line I want.

By either disabling m4 comments:

changecom()

or by using proper quoting (so that hashdef no longer expands to a 
comment character, but a quoted string that happens to contain #):

define(`hashdef', ``#'define')dnl

Also, to be robust to non-GNU m4, you should probably also double-quote 
define, or use other tricks to ensure that you don't inadvertently end 
up expanding the define built-in with zero arguments (GNU m4 explicitly 
documents that the define built-in when invoked with zero arguments 
expands to it's own name, as if it hadn't been a macro, but POSIX leaves 
that bit unspecified):

define(`hashdef', ``#define'')dnl

-- 
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.