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