|
|
The more general solution is to delay the expansion of the arguments of define by quoting them. Any text surrounded by left and right single quotes is not expanded immediately, but has the quotes stripped off as the arguments are collected. The value of the quoted string is the string stripped of the quotes.
define(N, 100) define(M, `N')defines M as the string N, not 100.
The general rule is that m4 always strips off one level of single quotes whenever it evaluates something. This is true even outside of macros. If the word define is to appear in the output, the word must be quoted in the input:
`define' = 1;It's usually best to quote the arguments of a macro to assure that what you are assigning to the macro name actually gets assigned. To redefine N, for example, you delay its evaluation by quoting:
define(N, 100) ... define(`N', 200)Otherwise
define(N, 100) ... define(N, 200)the N in the second definition is immediately replaced by 100. The effect is the same as saying
define(100, 200)Note that this statement will be ignored by m4 because only things that look like names can be defined.
If left and right single quotes are not convenient for some reason, the quote characters can be changed with the built-in macro changequote:
changequote([, ])In this example the macro makes the ``quote'' characters the left and right brackets instead of the left and right single quotes. The quote symbols can be up to five characters long. The original characters can be restored by using changequote without arguments:
changequoteundefine removes the definition of a macro or built-in:
undefine(`N')Here the macro removes the definition of N. Be sure to quote the argument to undefine. Built-ins can be removed with undefine as well:
undefine(`define')Note that once a built-in is removed or redefined, its original definition cannot be reused.
Macros can be renamed with defn. Suppose you want the built-in define to be called XYZ. You specify
define(XYZ, defn(`define')) undefine(`define')After this, XYZ takes on the original meaning of define.
XYZ(A, 100)defines A to be 100.
The built-in ifdef provides a way to determine if a macro is currently defined. Depending on the system, a definition appropriate for the particular machine can be made as follows:
ifdef(`pdp11', `define(wordsize,16)') ifdef(`u3b', `define(wordsize,32)')
The ifdef macro permits three arguments. If the first argument is defined, the value of ifdef is the second argument. If the first argument is not defined, the value of ifdef is the third argument:
ifdef(`unix', on UNIX, not on UNIX)If there is no third argument, the value of ifdef is null.