m4 Problems

Crist J. Clark cristjc at comcast.net
Fri Aug 27 12:13:32 PDT 2004


I want to do something that I _think_ should be rather simple
within m4(1). I want to test if a macro value has been set.
If it is set, I want to modify it and continue. If it is not
set, I want to produce an error message and bail out. However,
it is not working.

Here is an example of a test script,

  $ cat testerr.mc
  ifdef(`TEST', ``TEST' defined.', `TEST not defined.')

  ifdef(`TEST', define(`TEST', `NEW'TEST), errprint(`TEST not specified, exiting.') m4exit(1))

  The value of `TEST' is TEST.

Now I run it without "TEST" set,

  $ m4 testerr.mc
  TEST not defined.

  TEST not specified, exiting. 
  $ echo $?
  1

And it looks good. But now I try with "TEST" set,

  $ m4 -D TEST=test testerr.mc
  TEST defined.

  TEST not specified, exiting. 
  $ echo $?
  1

And it acts as if it is NOT set. It looks like the third
argument of the second ifdef macro is getting evaluated
even though the first argument is not set.

So, I'll try this test script instead,

  ifdef(`TEST', ``TEST' defined.', `TEST not defined.')

  ifdef(`TEST', define(`TEST', `NEW'TEST), `errprint(`TEST not specified, exiting.') m4exit(1)')

  The value of `TEST' is TEST.

And try the set case,

  $ m4 -D TEST=test testerr.mc
  TEST defined.



  The value of TEST is NEWtest.
  $ echo $?
  0

And it works! OK, now check the case where it is not
set,

  $ m4 testerr.mc
  TEST not defined.



  The value of TEST is NEWTEST.
  $ echo $?
  0

So now this case doesn't work.

What quoting scheme do I need to use here to get this to work
correctly? Or is there some other trick to it?  Can someone
explain m4(1)'s order of evaluation and what actually gets
evaluated in each case?
-- 
Crist J. Clark                     |     cjclark at alum.mit.edu
                                   |     cjclark at jhu.edu
http://people.freebsd.org/~cjc/    |     cjc at freebsd.org


More information about the freebsd-questions mailing list