Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define a macro with the same name as its expansion in m4?

I am attempting to replace if with if( using GNU m4 1.4.14 and I am receiving ERROR: end of file in argument list when trying:

define(`if', `if(')
define(`then', `){')
define(`fi', `}')

if foo then bar() fi

I have tried escaping the parentheses but that caused m4 to error after a brief period of time saying it's out of memory. Scanning through the manual, I found nothing related to this problem.

Upon changing the name of the macro to 'IF' or something other than 'if', it works as expected, which leads me to believe it's evaluating itself repeatedly.

If so, how can I define a macro that is evaluated only once? Otherwise, what should I look into to fix this?

EDIT I found a way around this issue by processing twice, once to convert if to _IF and the next to convert _IF to if(. I assume there's a better way to do this, so this is only a temporary solution in my eyes.

like image 461
Jimmio92 Avatar asked Oct 06 '22 03:10

Jimmio92


1 Answers

You need to prevent m4 from attempting to re-expanding the replacements. Do so by double quoting:

define(`if', ``if('')
define(`then', `){')
define(`fi', `}')

if foo then bar() fi
like image 183
William Pursell Avatar answered Oct 10 '22 03:10

William Pursell