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