m4-discuss
[Top][All Lists]
Advanced

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

Better way to interpret $N as dollars, not macro argument?


From: Doug McIlroy
Subject: Better way to interpret $N as dollars, not macro argument?
Date: Thu, 14 Aug 2014 10:22:18 -0400
User-agent: Heirloom mailx 12.5 7/5/10

> I ran into a problem where $N is interpreted as a macro argument,
> but I wanted a dollar amount.

A well constructed document likely doesn't interleave macro definitions
with the text that contains the dollar signs you want to protect. In that
case an easy fix is to transliterate the literal dollar signs into another
character during macro processing. Two helpful utilities are tr and sed.

If the definitions are in an include() file, then a wrapper like this
may suffice (quotes protect the dollar signs from the shell):

        tr '$' @ | m4 | tr @ '$'

If the definitions are at the beginning, not protected in an include file,
you could signal the end of definitions with a comment like #ENDDEF or
a dummy definition like define(ENDDEF)dnl() and use sed to process
the rest of the file:

        sed '/ENDDEF/,$y/$/@/' | m4 | sed 'y/@/$/'

Jon Bentley, who often combines m4 with troff, which also uses $
as a metacharacter, does the reverse. He uses @ as the
argument indicator and preprocesses the definitions:

        sed '1,/ENDDEF/y/@/$/' | m4 | troff

Doug



reply via email to

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