.\" Copyright (c) 1989, 1990 The Regents of the University of California.
.\" All rights reserved. The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\" This code is derived from software contributed to Berkeley by
.\" %sccs.include.redist.man%
.\" @(#)m4.1 6.5 (Berkeley) %G%
.Nd macro language preprocessor for Ratfor and Pascal
for Ratfor, Pascal, and similar languages which do not
have a built-in macro processing capability.
M4 reads standard input, and writes the results to the standard output.
The options and their effects are as follows:
processor provides a kind of
some of the macro functions will
the second argument is installed as the value of the macro
whose name is the first argument. If there is no second argument,
Argument 0 is the name of the macro;
missing arguments are replaced by the null string.
.Ar defn(name [, name ...])
returns the quoted definition of its argument(s). Useful in renaming
.Ar undefine(name [, name ...])
removes the definition of the macro(s) named. If there is
more than one definition for the named macro, (due to previous use of
all definitions are removed.
.Ar pushdef(name [, val])
but saves any previous definition by stacking the current definition.
.Ar popdef(name [, name ...])
removes current definition of its argument(s),
exposing the previous one if any.
.Ar ifdef(name, if-def [, ifnot-def])
if the first argument is defined, the value is the second argument,
If there is no third argument, the value is null.
.Ar shift(arg, arg, arg, ...)
returns all but its first argument.
The other arguments are quoted and pushed back with
The quoting nullifies the effect of the extra scan that
will subsequently be performed.
.Ar changequote(lqchar, rqchar)
change quote symbols to the first and second arguments.
With no arguments, the quotes are reset back to the default
characters. (i.e., \*`\\*').
.Ar changecom(lcchar, rcchar)
change left and right comment markers from the default
With no arguments, the comment mechanism is reset back to
With one argument, the left marker becomes the argument and
the right marker becomes newline.
With two arguments, both markers are affected.
maintains 10 output streams,
numbered 0-9. initially stream 0 is the current stream.
macro changes the current output stream to its (digit-string)
Output diverted to a stream other than 0 through 9
disappears into bitbucket.
.Ar undivert([divnum [, divnum ...])
causes immediate output of text from diversions named as
argument(s), or all diversions if no argument.
Text may be undiverted into another diversion.
Undiverting discards the diverted text. At the end of input processing,
returns the value of the current output stream.
reads and discards characters up to and including the next newline.
.Ar ifelse(arg, arg, if-same [, ifnot-same \&| arg,\ arg\ ...])
has three or more arguments.
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 arguments, the process is
repeated with arguments 4, 5, 6 and 7.
Otherwise, the value is either the fourth string, or, if it is not present,
returns the value of its argument incremented by 1.
The value of the argument is calculated
by interpreting an initial digit-string as a decimal number.
returns the value of its argument decremented by 1.
evaluates its argument as a constant expression, using integer arithmetic.
The evaluation mechanism is very similar to that of
The expression can involve only integer constants and character constants,
possibly connected by the binary operators
or by the ternary operator
Parentheses may be used for grouping. Octal numbers may be specified as
returns the number of characters in its argument.
.Ar index(search-string, string)
returns the position in its first argument where the second argument
or \-1 if the second argument does not occur.
.Ar substr(string, index [, length])
returns a substring of its first argument.
The second argument is a zero origin
number selecting the first character (internally treated as an expression);
the third argument indicates the length of the substring.
A missing third argument is taken to be large enough to extend to
the end of the first string.
.Ar translit(source, from [, to])
transliterates the characters in its first argument
from the set given by the second argument to the set given by the third.
If the third argument is shorter than the second, all extra characters
in the second argument are deleted from the first argument. If the third
argument is missing altogether, all characters in the second argument are
deleted from the first argument.
returns the contents of the file named in the argument.
says nothing if the file is inaccessible.
returns the contents of the file named in the argument without any
except that it says nothing if the file is inaccessible.
command given in the first argument.
is the return code from the last call to
in its argument with the current process
causes immediate exit from
Argument 1, if given, is the exit code;
.Ar m4wrap(m4-macro-or-built-in)
argument 1 will be pushed back at final
.Dl example: m4wrap(`dumptable()').
.Ar errprint(str [, str, str, ...])
prints its argument(s) on stderr. If there is more than one argument,
each argument is separated by a space during the output.
.Ar dumpdef([name, name, ...])
prints current names and definitions,
for the named items, or for all if no arguments are given.
A sufficiently complex M4 macro set is about as readable
All complex uses of M4 require the ability to program in deep recursion.
Previous lisp experience is recommended.
The following macro program illustrates the type of things that
changequote(<,>) define(HASHVAL,99) dnl
define(hash,<expr(str(substr($1,1),0)%HASHVAL)>) dnl
\t<str(substr(<$1>,1),<expr($2+'substr($1,0,1)')>)>)
define(KEYWORD,<$1,hash($1),>) dnl
Thus a keyword table containing the keyword string and its pre-calculated
hash value may be generated thus:
Presumably, such a table would speed up the installation of the
keywords into a dynamic hash table. (Note that the above macro
does not handle character constants.)
.Em The M4 Macro Processor
by B. W. Kernighan and D. M. Ritchie.
command appeared in Version 7 AT&T UNIX. The
command this page describes is derived from code
contributed by Ozan S. Yigit.