M4(1) M4(1) NAME m4 - macro processor SYNOPSIS m4 [ option ... ] [ file ... ] DESCRIPTION M4 is a macro processor intended as a front end for C and other languages. Each of the argument files is processed in order; if there are no files, or if a file name is -, the standard input is read. The processed text is written on the standard output. The options and their effects are as follows: -e Operate interactively. Interrupts are ignored and the output is unbuffered. Using this mode requires a spe- cial state of mind. -s Enable line sync output for the C preprocessor, (#line ...) -Bint Change the size of the push-back and argument collec- tion buffers from the default of 4,096. -Hint Change the size of the symbol table hash array from the default of 199. The size should be prime. -Sint Change the size of the call stack from the default of 100 slots. Macros take three slots, and non-macro arguments take one. -Tint Change the size of the token buffer from the default of 512 bytes. The preceding options must appear before any file names or -D or -U options. -Dname[=val] Defines name to val or to null if val is missing. -Uname undefines name. Macro calls have the form: M4(1) M4(1) `name(arg1,arg2, ..., argn)' The ( must immediately follow the name of the macro. If a defined macro name is not followed by a (, it is deemed to have no arguments. Leading unquoted blanks, tabs, and new- lines are ignored while collecting arguments. Potential macro names consist of alphabetic letters, digits, and underscore _, where the first character is not a digit. Left and right single quotes are used to quote strings. The value of a quoted string is the string stripped of the quotes. When a macro name is recognized, its arguments are collected by searching for a matching right parenthesis. Macro evalu- ation proceeds normally during the collection of the argu- ments, and any commas or right parentheses which happen to turn up within the value of a nested call are as effective as those in the original input text. After argument collec- tion, the value of the macro is pushed back onto the input stream and rescanned. The value of a macro is obtained by replacing each occur- rence of $n in the replacement text, where n is a digit, with the n-th argument. Argument 0 is the name of the macro; missing arguments are replaced by the null string; $# is replaced by the number of arguments; $* is replaced by a list of all the arguments separated by commas; $@ is like $*, but each argument is quoted (with the current quotes). M4 makes available the following built-in macros. They may be redefined, but once this is done the original meaning is lost. Their values are null unless otherwise stated. define the second argument is installed as the replace- ment text of the macro whose name is the first argument. undefine Remove the definition of the macro named in the argument. defn Return the quoted definition of the argument(s); useful for renaming macros, especially built- ins. pushdef Like define, but save any previous definition. popdef Remove current definition of the argument(s), exposing the previous one if any. ifdef If the first argument is defined, the value is the second argument, otherwise the third. If M4(1) M4(1) there is no third argument, the value is null. The word `unix' is predefined on UNIX versions of m4. shift Return all but the first argument. The other arguments pushed back with commas in between and quoted to nullify the effect of the extra scan. changequote Change quote symbols to the first and second arguments. The symbols may be up to five char- acters long. Changequote without arguments restores the original values (i.e., ``''). changecom Change left and right comment markers from the default # and new-line. With no arguments, the comment mechanism is effectively disabled. With one argument, the left marker becomes the argu- ment and the right marker becomes new-line. With two arguments, both markers are affected. Comment markers may be up to five characters long. divert m4 Switch output to one of 10 streams, numbered 0-9 designated by the argument. The final out- put is the concatenation of the streams in numerical order; stream 0 is the current ini- tially. Output to a stream other than 0 through 9 is discarded. undivert Cause immediate output of text from diversions named as arguments, or all diversions if no argument. Text may be undiverted into another diversion. Once undiverted, the diverted text is no longer contained in that diversion. divnum Return the name of the current output stream. dnl reads and discards characters up to and includ- ing the next new-line. ifelse If the first argument is the same string as the second, then the value is the third argument. If not, and if there are more than four argu- ments, the process is repeated with arguments 4, 5, 6 and 7. Otherwise, the value is either the fourth string, or, if that is not present, null. incr Return the value of the argument incremented by 1. The value of the argument is calculated by interpreting an initial digit-string as a deci- mal number. M4(1) M4(1) decr Return the value of the argument decremented by 1. eval Evaluate the argument as an arithmetic expres- sion, using 32-bit arithmetic. C-like operators include +-*/%, bitwise &|^~; relationals; paren- theses. Octal and hex numbers may be specified as in C. The second argument specifies the radix for the result; the default is 10. The third argument may be used to specify the mini- mum number of digits in the result. len Returns the number of characters in the argu- ment. index Return the position in the first argument where the second argument begins (zero origin), or -1 if the second argument does not occur. substr Return a substring of the first argument. The second argument is a zero origin number select- ing the first character; the third argument indicates the length of the substring. A miss- ing third argument is taken to be large enough to extend to the end of the first string. translit Transliterate the characters in the first argu- ment from the set given by the second argument to the set given by the third, deleting charac- ters that lack a correspondent in the third set. There is no character-range notation. include Return the contents of the file named in the argument. sinclude Same, but give no diagnostic if the file is inaccessible. syscmd Execute the UNIX command given in the first argument. No value is returned. sysval The return code from the last call to syscmd. maketemp Fill in a string of `X' characters in the argu- ment with the current process id. m4exit Exit immediately from m4. Argument 1, if given, is the exit code; the default is 0. m4wrap Push the argument back at the end of the input. Example: `m4wrap(`cleanup()')' M4(1) M4(1) errprint Prints the argument on the standard error file. dumpdef Print current names and definitions, for the named items, or for all if no arguments are given. traceon If there are no arguments, turn on tracing for all macros (including built-ins). Otherwise, turn on tracing for named macros. traceoff Turn off trace globally and for any macros spec- ified. Macros specifically traced by traceon can be untraced only by specific calls to traceoff. EXAMPLES define(fib,`ifelse(define(`n',eval($1))n,0,1,n,1,1,dnl() `eval(fib(n-1)+fib($1-2))')')dnl() fib(2*3) Recursively evaluate a Fibonacci number. The inner define avoids some reevaluations.