ba5584c5a80d58a24318ba8f23d94ab68b1ecf8e
[unix-history] / usr / src / usr.bin / m4 / m4.1
.\" 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
.\" Ozan Yigit.
.\"
.\" %sccs.include.redist.man%
.\"
.\" @(#)m4.1 6.5 (Berkeley) %G%
.\"
.Dd
.Dt M4 1
.DA 08 Jan 1986
.Sh NAME
.Nm m4
.Nd macro language preprocessor for Ratfor and Pascal
.Sh SYNOPSIS
.Nm m4
.Op options
.Sh DESCRIPTION
.Nm M4
is a macro language
preprocessor
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.
.Pp
The options and their effects are as follows:
.Pp
.Tp Cx Fl D
.Ar name
.Op Ar \&=Val
.Cx
Defines
.Ar name
to
.Ar val
or to null in
the absence of
.Ar val .
.Tp Cx Fl U
.Ar name
.Cx
undefines
.Ar name .
.Tp
.Pp
The
.Nm m4
processor provides a kind of
.Nm C
like syntax and
some of the macro functions will
be familiar:
.Tw Ds
.Tp Ic define
usage:
.Ar define(name [, val])
.br
the second argument is installed as the value of the macro
whose name is the first argument. If there is no second argument,
the value is null.
Each occurrence of
.Cx Ic $
.Ar n
.Cx
in the replacement text,
where
.Ar n
is a digit,
is replaced by the
.Cx Ar n
.Cx \'th
.Cx
argument.
Argument 0 is the name of the macro;
missing arguments are replaced by the null string.
.Tp Ic defn
usage:
.Ar defn(name [, name ...])
.br
returns the quoted definition of its argument(s). Useful in renaming
macros.
.Tp Ic undefine
usage:
.Ar undefine(name [, name ...])
.br
removes the definition of the macro(s) named. If there is
more than one definition for the named macro, (due to previous use of
.Ic pushdef )
all definitions are removed.
.Tp Ic pushdef
usage:
.Ar pushdef(name [, val])
.br
like
.Ic define ,
but saves any previous definition by stacking the current definition.
.Tp Ic popdef
usage:
.Ar popdef(name [, name ...])
.br
removes current definition of its argument(s),
exposing the previous one if any.
.Tp Ic ifdef
usage:
.Ar ifdef(name, if-def [, ifnot-def])
.br
if the first argument is defined, the value is the second argument,
otherwise the third.
If there is no third argument, the value is null.
.Tp Ic shift
usage:
.Ar shift(arg, arg, arg, ...)
.br
returns all but its first argument.
The other arguments are quoted and pushed back with
commas in between.
The quoting nullifies the effect of the extra scan that
will subsequently be performed.
.Tp Ic changequote
usage:
.Ar changequote(lqchar, rqchar)
.br
change quote symbols to the first and second arguments.
With no arguments, the quotes are reset back to the default
characters. (i.e., \*`\\*').
.Tp Ic changecom
usage:
.Ar changecom(lcchar, rcchar)
.br
change left and right comment markers from the default
.Ic #
and
.Ic newline .
With no arguments, the comment mechanism is reset back to
the default characters.
With one argument, the left marker becomes the argument and
the right marker becomes newline.
With two arguments, both markers are affected.
.Tp Ic divert
usage:
.Ar divert(divnum)
.br
.Nm m4
maintains 10 output streams,
numbered 0-9. initially stream 0 is the current stream.
The
.Ic divert
macro changes the current output stream to its (digit-string)
argument.
Output diverted to a stream other than 0 through 9
disappears into bitbucket.
.Tp Ic undivert
usage:
.Ar undivert([divnum [, divnum ...])
.br
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,
.Nm M4
forces an automatic
.Ic undivert ,
unless
.Ic m4wrap
is defined.
.Tp Ic divnum
usage:
.Ar divnum()
.br
returns the value of the current output stream.
.Tp Ic dnl
usage:
.Ar dnl()
.br
reads and discards characters up to and including the next newline.
.Tp Ic ifelse
usage:
.Ar ifelse(arg, arg, if-same [, ifnot-same \&| arg,\ arg\ ...])
.br
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,
null.
.Tp Ic incr
usage:
.Ar incr(num)
.br
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.
.Tp Ic decr
usage:
.Ar decr(num)
.br
returns the value of its argument decremented by 1.
.Tp Ic eval
usage:
.Ar eval(expression)
.br
evaluates its argument as a constant expression, using integer arithmetic.
The evaluation mechanism is very similar to that of
.Xr cpp
(#if expression).
The expression can involve only integer constants and character constants,
possibly connected by the binary operators
.Ds I
* / % + - >> << < >
<= >= == != & ^ &&
.De
or the unary operators
.Ic \&~ \&!
or by the ternary operator
.Ic \&? \&: .
Parentheses may be used for grouping. Octal numbers may be specified as
in C.
.Tp Ic len
usage:
.Ar len(string)
.br
returns the number of characters in its argument.
.Tp Ic index
usage:
.Ar index(search-string, string)
.br
returns the position in its first argument where the second argument
begins (zero origin),
or \-1 if the second argument does not occur.
.Tp Ic substr
usage:
.Ar substr(string, index [, length])
.br
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.
.Tp Ic translit
usage:
.Ar translit(source, from [, to])
.br
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.
.Tp Ic include
usage:
.Ar include(filename)
.br
returns the contents of the file named in the argument.
.Tp Ic sinclude
usage:
.Ar sinclude(filename)
.br
is identical to
.Ic include ,
except that it
says nothing if the file is inaccessible.
.Tp Ic paste
usage:
.Ar paste(filename)
.br
returns the contents of the file named in the argument without any
processing, unlike
.Ic include .
.Tp Ic spaste
usage:
.Ar spaste(filename)
.br
is identical to
.Ic paste ,
except that it says nothing if the file is inaccessible.
.Tp Ic syscmd
usage:
.Ar syscmd(command)
.br
executes the
UNIX
command given in the first argument.
No value is returned.
.Tp Ic sysval
usage:
.Ar sysval()
.br
is the return code from the last call to
.Ic syscmd .
.Tp Ic maketemp
usage:
.Ar maketemp(string)
.br
fills in a string of
XXXXXX
in its argument with the current process
ID.
.Tp Ic m4exit
usage:
.Ar m4exit([exitcode])
.br
causes immediate exit from
.Nm m4 .
Argument 1, if given, is the exit code;
the default is 0.
.Tp Ic m4wrap
usage:
.Ar m4wrap(m4-macro-or-built-in)
.br
argument 1 will be pushed back at final
.Ic EOF ;
.Dl example: m4wrap(`dumptable()').
.Tp Ic errprint "(str
usage:
.Ar errprint(str [, str, str, ...])
.br
prints its argument(s) on stderr. If there is more than one argument,
each argument is separated by a space during the output.
.Tp Ic dumpdef
usage:
.Ar dumpdef([name, name, ...])
.br
prints current names and definitions,
for the named items, or for all if no arguments are given.
.Tp
.Sh AUTHOR
Ozan S. Yigit (oz)
.Sh BUGS
A sufficiently complex M4 macro set is about as readable
as
.Ar APL .
.Pp
All complex uses of M4 require the ability to program in deep recursion.
Previous lisp experience is recommended.
.Sh EXAMPLES
The following macro program illustrates the type of things that
can be done with M4.
.Pp
.Ds I
changequote(<,>) define(HASHVAL,99) dnl
define(hash,<expr(str(substr($1,1),0)%HASHVAL)>) dnl
define(str,
<ifelse($1,",$2,
\t<str(substr(<$1>,1),<expr($2+'substr($1,0,1)')>)>)
>) dnl
define(KEYWORD,<$1,hash($1),>) dnl
define(TSTART,
<struct prehash {
char *keyword;
int hashval;
} keytab[] = {>) dnl
define(TEND,< "",0
};>)
dnl
.De
.Pp
Thus a keyword table containing the keyword string and its pre-calculated
hash value may be generated thus:
.Pp
.Ds I
TSTART
KEYWORD("foo")
KEYWORD("bar")
KEYWORD("baz")
TEND
.De
.Pp
which will expand into:
.Pp
.Ds I
struct prehash {
char *keyword;
int hashval;
} keytab[] = {
"foo",27,
"bar",12,
"baz",20,
"",0
};
.De
.Pp
Presumably, such a table would speed up the installation of the
keywords into a dynamic hash table. (Note that the above macro
cannot be used with
.Nm m4 ,
since
.Ic eval
does not handle character constants.)
.Sh SEE ALSO
.Xr cc 1 ,
.Xr cpp 1 .
.Xr m4 1 ,
.Em The M4 Macro Processor
by B. W. Kernighan and D. M. Ritchie.
.Sh HISTORY
.Nm M4
command appeared in Version 7 AT&T UNIX. The
.Nm m4
command this page describes is derived from code
contributed by Ozan S. Yigit.