m4 macros

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

m4 macros

Post by Neo »

I need to define a few macros. These macros may have the text pattern that is same as the macro name inside them.
For e.g.

Code: Select all

define(`Fn1',`
Fn1: First line
Fn1: Second line
Fn1: Third Line
')

define(`Fn2',`
Fn2: First Line
Fn1: Second line
Fn1: Third Line
')
and when I call it like this

Code: Select all

Fn1
Fn2 
I get a screen that goes on printing blanks.
How do I do get this to work properly?
Only Human
mystran

Re:m4 macros

Post by mystran »

You need to prevent expansion. The m4 manual tells you that you can do this by quoting. Basicly, add some dummy `' around or inside the token you don't want to match as a macro name. Something like:

Code: Select all

define(`fn1',`
`fn1': First line
')
(oh, and I don't know any M4, just reading the manual here :))
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:m4 macros

Post by Neo »

I did read that too. But used the wrong damn quotes. :o
Anyway there is a sort of problem. If I have many macros defined there and one of them has the text pattern that is the name of another macro, then I get into a nice jam.

Anyone has some idea for a workaround for this?
Only Human
erikgreenwald

Re:m4 macros

Post by erikgreenwald »

Hrm, I must be misunderstanding the current issue with his, because when I do

Code: Select all

define(Fn1,`
`Fn1': Plop
`Fn1': Splort
')
define(Fn2,`
`Fn2': Pop
`Fn1': Boom
`Fn1': Blammo
')
Fn1
Fn2
I get this out of m4...

Code: Select all




Fn1: Plop
Fn1: Splort


Fn2: Pop
Fn1: Boom
Fn1: Blammo

which seems to be what you're asking for?

-Erik
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:m4 macros

Post by Neo »

Which is perfectly correct.
Did you expect something else?
Only Human
erikgreenwald

Re:m4 macros

Post by erikgreenwald »

Actually, it was completely expected... I was (and still am) trying to figure out exactly what you're looking for a workaround to?

-Erik
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:m4 macros

Post by Neo »

Neo wrote:

Code: Select all

define(`Fn1',`
Fn1: First line
Fn1: Second line
Fn1: Third Line
')
.....
I did not use quotes correctly. It is working fine now.
Only Human
Post Reply