converted man page
[unix-history] / usr / src / usr.bin / m4 / m4.1
CommitLineData
b5dc1377 1.\" Copyright (c) 1989, 1990 The Regents of the University of California.
dd9e2e8d
CL
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
27ac7598 4.\"
74692266
KB
5.\" This code is derived from software contributed to Berkeley by
6.\" Ozan Yigit.
7.\"
b5dc1377 8.\" %sccs.include.redist.man%
74692266 9.\"
dd9e2e8d 10.\" @(#)m4.1 6.5 (Berkeley) %G%
74692266 11.\"
b5dc1377
CL
12.Dd
13.Dt M4 1
74692266 14.DA 08 Jan 1986
b5dc1377
CL
15.Sh NAME
16.Nm m4
17.Nd macro language preprocessor for Ratfor and Pascal
18.Sh SYNOPSIS
19.Nm m4
20.Op options
21.Sh DESCRIPTION
22.Nm M4
23is a macro language
24preprocessor
25for Ratfor, Pascal, and similar languages which do not
74692266 26have a built-in macro processing capability.
b5dc1377
CL
27M4 reads standard input, and writes the results to the standard output.
28.Pp
74692266 29The options and their effects are as follows:
b5dc1377
CL
30.Pp
31.Tp Cx Fl D
32.Ar name
33.Op Ar \&=Val
34.Cx
74692266 35Defines
b5dc1377 36.Ar name
74692266 37to
b5dc1377 38.Ar val
74692266 39or to null in
b5dc1377
CL
40the absence of
41.Ar val .
42.Tp Cx Fl U
43.Ar name
44.Cx
74692266 45undefines
b5dc1377
CL
46.Ar name .
47.Tp
48.Pp
74692266 49The
b5dc1377
CL
50.Nm m4
51processor provides a kind of
52.Nm C
53like syntax and
54some of the macro functions will
55be familiar:
56.Tw Ds
57.Tp Ic define
58usage:
59.Ar define(name [, val])
60.br
74692266
KB
61the second argument is installed as the value of the macro
62whose name is the first argument. If there is no second argument,
63the value is null.
64Each occurrence of
b5dc1377
CL
65.Cx Ic $
66.Ar n
67.Cx
74692266
KB
68in the replacement text,
69where
b5dc1377 70.Ar n
74692266
KB
71is a digit,
72is replaced by the
b5dc1377
CL
73.Cx Ar n
74.Cx \'th
75.Cx
74692266
KB
76argument.
77Argument 0 is the name of the macro;
27ac7598 78missing arguments are replaced by the null string.
b5dc1377
CL
79.Tp Ic defn
80usage:
81.Ar defn(name [, name ...])
82.br
74692266
KB
83returns the quoted definition of its argument(s). Useful in renaming
84macros.
b5dc1377
CL
85.Tp Ic undefine
86usage:
87.Ar undefine(name [, name ...])
88.br
74692266
KB
89removes the definition of the macro(s) named. If there is
90more than one definition for the named macro, (due to previous use of
b5dc1377 91.Ic pushdef )
74692266 92all definitions are removed.
b5dc1377
CL
93.Tp Ic pushdef
94usage:
95.Ar pushdef(name [, val])
96.br
74692266 97like
b5dc1377 98.Ic define ,
74692266 99but saves any previous definition by stacking the current definition.
b5dc1377
CL
100.Tp Ic popdef
101usage:
102.Ar popdef(name [, name ...])
103.br
74692266
KB
104removes current definition of its argument(s),
105exposing the previous one if any.
b5dc1377
CL
106.Tp Ic ifdef
107usage:
108.Ar ifdef(name, if-def [, ifnot-def])
109.br
110if the first argument is defined, the value is the second argument,
74692266
KB
111otherwise the third.
112If there is no third argument, the value is null.
b5dc1377
CL
113.Tp Ic shift
114usage:
115.Ar shift(arg, arg, arg, ...)
116.br
74692266
KB
117returns all but its first argument.
118The other arguments are quoted and pushed back with
119commas in between.
120The quoting nullifies the effect of the extra scan that
121will subsequently be performed.
b5dc1377
CL
122.Tp Ic changequote
123usage:
124.Ar changequote(lqchar, rqchar)
125.br
74692266
KB
126change quote symbols to the first and second arguments.
127With no arguments, the quotes are reset back to the default
b5dc1377
CL
128characters. (i.e., \*`\\*').
129.Tp Ic changecom
130usage:
131.Ar changecom(lcchar, rcchar)
132.br
74692266 133change left and right comment markers from the default
b5dc1377
CL
134.Ic #
135and
136.Ic newline .
137With no arguments, the comment mechanism is reset back to
74692266
KB
138the default characters.
139With one argument, the left marker becomes the argument and
140the right marker becomes newline.
141With two arguments, both markers are affected.
b5dc1377
CL
142.Tp Ic divert
143usage:
144.Ar divert(divnum)
145.br
146.Nm m4
74692266 147maintains 10 output streams,
b5dc1377 148numbered 0-9. initially stream 0 is the current stream.
74692266 149The
b5dc1377 150.Ic divert
74692266
KB
151macro changes the current output stream to its (digit-string)
152argument.
153Output diverted to a stream other than 0 through 9
154disappears into bitbucket.
b5dc1377
CL
155.Tp Ic undivert
156usage:
157.Ar undivert([divnum [, divnum ...])
158.br
27ac7598 159causes immediate output of text from diversions named as
74692266 160argument(s), or all diversions if no argument.
27ac7598 161Text may be undiverted into another diversion.
74692266 162Undiverting discards the diverted text. At the end of input processing,
b5dc1377 163.Nm M4
74692266 164forces an automatic
b5dc1377 165.Ic undivert ,
74692266 166unless
b5dc1377 167.Ic m4wrap
74692266 168is defined.
b5dc1377
CL
169.Tp Ic divnum
170usage:
171.Ar divnum()
172.br
27ac7598 173returns the value of the current output stream.
b5dc1377
CL
174.Tp Ic dnl
175usage:
176.Ar dnl()
177.br
27ac7598 178reads and discards characters up to and including the next newline.
b5dc1377
CL
179.Tp Ic ifelse
180usage:
181.Ar ifelse(arg, arg, if-same [, ifnot-same \&| arg,\ arg\ ...])
182.br
27ac7598
KM
183has three or more arguments.
184If the first argument is the same string as the second,
185then the value is the third argument.
b5dc1377 186If not, and if there are more than four arguments, the process is
74692266 187repeated with arguments 4, 5, 6 and 7.
27ac7598
KM
188Otherwise, the value is either the fourth string, or, if it is not present,
189null.
b5dc1377
CL
190.Tp Ic incr
191usage:
192.Ar incr(num)
193.br
27ac7598
KM
194returns the value of its argument incremented by 1.
195The value of the argument is calculated
196by interpreting an initial digit-string as a decimal number.
b5dc1377
CL
197.Tp Ic decr
198usage:
199.Ar decr(num)
200.br
74692266 201returns the value of its argument decremented by 1.
b5dc1377
CL
202.Tp Ic eval
203usage:
204.Ar eval(expression)
205.br
74692266
KB
206evaluates its argument as a constant expression, using integer arithmetic.
207The evaluation mechanism is very similar to that of
b5dc1377
CL
208.Xr cpp
209(#if expression).
74692266
KB
210The expression can involve only integer constants and character constants,
211possibly connected by the binary operators
b5dc1377
CL
212.Ds I
213* / % + - >> << < >
214<= >= == != & ^ &&
215.De
216or the unary operators
217.Ic \&~ \&!
218or by the ternary operator
219.Ic \&? \&: .
74692266
KB
220Parentheses may be used for grouping. Octal numbers may be specified as
221in C.
b5dc1377
CL
222.Tp Ic len
223usage:
224.Ar len(string)
225.br
27ac7598 226returns the number of characters in its argument.
b5dc1377
CL
227.Tp Ic index
228usage:
229.Ar index(search-string, string)
230.br
231returns the position in its first argument where the second argument
74692266
KB
232begins (zero origin),
233or \-1 if the second argument does not occur.
b5dc1377
CL
234.Tp Ic substr
235usage:
236.Ar substr(string, index [, length])
237.br
27ac7598 238returns a substring of its first argument.
74692266
KB
239The second argument is a zero origin
240number selecting the first character (internally treated as an expression);
27ac7598
KM
241the third argument indicates the length of the substring.
242A missing third argument is taken to be large enough to extend to
b5dc1377
CL
243the end of the first string.
244.Tp Ic translit
245usage:
246.Ar translit(source, from [, to])
247.br
27ac7598
KM
248transliterates the characters in its first argument
249from the set given by the second argument to the set given by the third.
74692266
KB
250If the third argument is shorter than the second, all extra characters
251in the second argument are deleted from the first argument. If the third
252argument is missing altogether, all characters in the second argument are
253deleted from the first argument.
b5dc1377
CL
254.Tp Ic include
255usage:
256.Ar include(filename)
257.br
27ac7598 258returns the contents of the file named in the argument.
b5dc1377
CL
259.Tp Ic sinclude
260usage:
261.Ar sinclude(filename)
262.br
74692266 263is identical to
b5dc1377 264.Ic include ,
74692266
KB
265except that it
266says nothing if the file is inaccessible.
b5dc1377
CL
267.Tp Ic paste
268usage:
269.Ar paste(filename)
270.br
74692266 271returns the contents of the file named in the argument without any
b5dc1377
CL
272processing, unlike
273.Ic include .
274.Tp Ic spaste
275usage:
276.Ar spaste(filename)
277.br
27ac7598 278is identical to
b5dc1377 279.Ic paste ,
75953d29 280except that it says nothing if the file is inaccessible.
b5dc1377
CL
281.Tp Ic syscmd
282usage:
283.Ar syscmd(command)
284.br
74692266 285executes the
b5dc1377 286UNIX
74692266 287command given in the first argument.
27ac7598 288No value is returned.
b5dc1377
CL
289.Tp Ic sysval
290usage:
291.Ar sysval()
292.br
74692266 293is the return code from the last call to
b5dc1377
CL
294.Ic syscmd .
295.Tp Ic maketemp
296usage:
297.Ar maketemp(string)
298.br
74692266 299fills in a string of
b5dc1377 300XXXXXX
74692266 301in its argument with the current process
b5dc1377
CL
302ID.
303.Tp Ic m4exit
304usage:
305.Ar m4exit([exitcode])
306.br
74692266 307causes immediate exit from
b5dc1377 308.Nm m4 .
74692266
KB
309Argument 1, if given, is the exit code;
310the default is 0.
b5dc1377
CL
311.Tp Ic m4wrap
312usage:
313.Ar m4wrap(m4-macro-or-built-in)
314.br
74692266 315argument 1 will be pushed back at final
b5dc1377
CL
316.Ic EOF ;
317.Dl example: m4wrap(`dumptable()').
318.Tp Ic errprint "(str
319usage:
320.Ar errprint(str [, str, str, ...])
321.br
74692266
KB
322prints its argument(s) on stderr. If there is more than one argument,
323each argument is separated by a space during the output.
b5dc1377
CL
324.Tp Ic dumpdef
325usage:
326.Ar dumpdef([name, name, ...])
327.br
27ac7598
KM
328prints current names and definitions,
329for the named items, or for all if no arguments are given.
b5dc1377
CL
330.Tp
331.Sh AUTHOR
74692266 332Ozan S. Yigit (oz)
b5dc1377 333.Sh BUGS
74692266
KB
334A sufficiently complex M4 macro set is about as readable
335as
b5dc1377
CL
336.Ar APL .
337.Pp
74692266
KB
338All complex uses of M4 require the ability to program in deep recursion.
339Previous lisp experience is recommended.
b5dc1377 340.Sh EXAMPLES
74692266 341The following macro program illustrates the type of things that
b5dc1377
CL
342can be done with M4.
343.Pp
344.Ds I
345changequote(<,>) define(HASHVAL,99) dnl
346define(hash,<expr(str(substr($1,1),0)%HASHVAL)>) dnl
347define(str,
348 <ifelse($1,",$2,
349 \t<str(substr(<$1>,1),<expr($2+'substr($1,0,1)')>)>)
350 >) dnl
351define(KEYWORD,<$1,hash($1),>) dnl
352define(TSTART,
74692266
KB
353<struct prehash {
354 char *keyword;
355 int hashval;
b5dc1377
CL
356} keytab[] = {>) dnl
357define(TEND,< "",0
358};>)
359dnl
360.De
361.Pp
74692266
KB
362Thus a keyword table containing the keyword string and its pre-calculated
363hash value may be generated thus:
b5dc1377
CL
364.Pp
365.Ds I
74692266
KB
366TSTART
367 KEYWORD("foo")
368 KEYWORD("bar")
369 KEYWORD("baz")
370TEND
b5dc1377
CL
371.De
372.Pp
74692266 373which will expand into:
b5dc1377
CL
374.Pp
375.Ds I
74692266
KB
376struct prehash {
377 char *keyword;
378 int hashval;
379} keytab[] = {
380 "foo",27,
381 "bar",12,
382 "baz",20,
383 "",0
384};
b5dc1377
CL
385.De
386.Pp
74692266
KB
387Presumably, such a table would speed up the installation of the
388keywords into a dynamic hash table. (Note that the above macro
b5dc1377
CL
389cannot be used with
390.Nm m4 ,
391since
392.Ic eval
74692266 393does not handle character constants.)
b5dc1377
CL
394.Sh SEE ALSO
395.Xr cc 1 ,
396.Xr cpp 1 .
397.Xr m4 1 ,
398.Em The M4 Macro Processor
74692266 399by B. W. Kernighan and D. M. Ritchie.
b5dc1377
CL
400.Sh HISTORY
401.Nm M4
402command appeared in Version 7 AT&T UNIX. The
403.Nm m4
404command this page describes is derived from code
405contributed by Ozan S. Yigit.