Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Aug 2004 12:13:24 -0700
From:      "Crist J. Clark" <cristjc@comcast.net>
To:        questions@freebsd.org
Subject:   m4 Problems
Message-ID:  <20040827191324.GA59475@blossom.cjclark.org>

next in thread | raw e-mail | index | archive | help
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@alum.mit.edu
                                   |     cjclark@jhu.edu
http://people.freebsd.org/~cjc/    |     cjc@freebsd.org



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040827191324.GA59475>