xref: /netbsd-src/usr.bin/m4/m4.1 (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1.\"	$NetBSD: m4.1,v 1.27 2016/01/17 11:24:28 wiz Exp $
2.\"	@(#) $OpenBSD: m4.1,v 1.56 2009/10/14 17:19:47 sthen Exp $
3.\"
4.\" Copyright (c) 1989, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" Ozan Yigit at York University.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd January 16, 2015
35.Dt M4 1
36.Os
37.Sh NAME
38.Nm m4
39.Nd macro language processor
40.Sh SYNOPSIS
41.Nm m4
42.Op Fl EGgiPQsv
43.Oo
44.Sm off
45.Fl D Ar name Op No = Ar value
46.Sm on
47.Oc
48.Op Fl d Ar flags
49.Op Fl e Ar filename
50.Op Fl F Ar filename
51.Op Fl I Ar dirname
52.Op Fl L Ar number
53.Op Fl o Ar filename
54.Op Fl R Ar filename
55.Op Fl t Ar macro
56.Op Fl U Ns Ar name
57.Op Ar
58.Sh DESCRIPTION
59The
60.Nm m4
61utility is a macro processor that can be used as a front end to any
62language (e.g., C, ratfor, fortran, lex, and yacc).
63If no input files are given,
64.Nm m4
65reads from the standard input,
66otherwise files specified on the command line are
67processed in the given order.
68Input files can be regular files, files in the m4 include paths, or a
69single dash
70.Pq Sq - ,
71denoting standard input.
72.Nm m4
73writes
74the processed text to the standard output, unless told otherwise.
75.Pp
76Macro calls have the form name(argument1[, argument2, ..., argumentN]).
77.Pp
78There cannot be any space following the macro name and the open
79parenthesis
80.Sq \&( .
81If the macro name is not followed by an open
82parenthesis it is processed with no arguments.
83.Pp
84Macro names consist of a leading alphabetic or underscore
85possibly followed by alphanumeric or underscore characters, e.g.,
86valid macro names match the pattern
87.Dq [a-zA-Z_][a-zA-Z0-9_]* .
88.Pp
89In arguments to macros, leading unquoted space, tab, and newline
90.Pq Sq \en
91characters are ignored.
92To quote strings, use left and right single quotes
93.Po e.g.,\ \&
94.Sq "\ this is a string with a leading space"
95.Pc .
96You can change the quote characters with the
97.Ic changequote
98built-in macro.
99.Pp
100Most built-ins don't make any sense without arguments, and hence are not
101recognized as special when not followed by an open parenthesis.
102.Pp
103The options are as follows:
104.Bl -tag -width Ds
105.It Fl D , Fl Fl define Ar name Ns Op Pf = Ns Ar value
106Define the symbol
107.Ar name
108to have some value (or
109.Dv NULL ) .
110.It Fl d , Fl Fl debug Ar "flags"
111Set trace flags.
112.Ar flags
113may hold the following:
114.Bl -tag -width Ds
115.It Ar a
116print macro arguments.
117.It Ar c
118print macro expansion over several lines.
119.It Ar e
120print result of macro expansion.
121.It Ar f
122print filename location.
123.It Ar l
124print line number.
125.It Ar q
126quote arguments and expansion with the current quotes.
127.It Ar t
128start with all macros traced.
129.It Ar x
130number macro expansions.
131.It Ar V
132turn on all options.
133.El
134.Pp
135By default, trace is set to
136.Qq eq .
137.It Fl E , Fl Fl fatal-warnings
138Warnings make
139.Nm
140exit.
141.It Fl e , Fl Fl error-output Ar filename
142Redirect error output to filename.
143.It Fl F , Fl Fl freeze-state Ar filename
144Save the input state to
145.Ar filename .
146.It Fl G , Fl Fl traditional
147Disable GNU-m4 extensions.
148.It Fl g , Fl Fl gnu
149Activate GNU-m4 compatibility mode.
150In this mode, translit handles simple character
151ranges (e.g., a-z), regular expressions mimic emacs behavior,
152multiple m4wrap calls are handled as a stack,
153the number of diversions is unlimited,
154empty names for macro definitions are allowed,
155and eval understands
156.Sq 0rbase:value
157numbers.
158.It Fl Fl help
159Print help message and exit.
160.It Fl I , Fl Fl include Ar "dirname"
161Add directory
162.Ar dirname
163to the include path.
164.It Fl i , Fl Fl interactive
165Set unbuffered output, disable tty signals.
166.It Fl L , Fl Fl nesting-limit
167Set the nesting limit in macro expansions.
168This is unimplemented and unlimited.
169.It Fl o Ar filename
170Send trace output to
171.Ar filename .
172.It Fl P , Fl Fl prefix-builtins
173Prefix all built-in macros with
174.Sq m4_ .
175For example, instead of writing
176.Ic define ,
177use
178.Ic m4_define .
179.It Fl Q , Fl Fl quiet , Fl Fl silent
180Don't print warnings.
181.It Fl R , Fl Fl reload-state Ar filename
182Reload a previously saved state from
183.Ar filename .
184.It Fl s , Fl Fl synclines
185Output line synchronization directives, suitable for
186.Xr cpp 1 .
187.It Fl t , Fl Fl trace Ar macro
188Turn tracing on for
189.Ar macro .
190.It Fl U , Fl Fl undefine Ar "name"
191Undefine the symbol
192.Ar name .
193.It Fl v , Fl Fl version
194Print the version and exit.
195.El
196.Sh SYNTAX
197.Nm m4
198provides the following built-in macros.
199They may be redefined, losing their original meaning.
200Return values are null unless otherwise stated.
201.Bl -tag -width changequote
202.It Fn builtin name
203Calls a built-in by its
204.Fa name ,
205overriding possible redefinitions.
206.It Fn changecom startcomment endcomment
207Changes the start comment and end comment sequences.
208Comment sequences may be up to five characters long.
209The default values are the hash sign
210and the newline character.
211.Bd -literal -offset indent
212# This is a comment
213.Ed
214.Pp
215With no arguments, comments are turned off.
216With one single argument, the end comment sequence is set
217to the newline character.
218.It Fn changequote beginquote endquote
219Defines the open quote and close quote sequences.
220Quote sequences may be up to five characters long.
221The default values are the backquote character and the quote
222character.
223.Bd -literal -offset indent
224`Here is a quoted string'
225.Ed
226.Pp
227With no arguments, the default quotes are restored.
228With one single argument, the close quote sequence is set
229to the newline character.
230.It Fn decr arg
231Decrements the argument
232.Fa arg
233by 1.
234The argument
235.Fa arg
236must be a valid numeric string.
237.It Fn define name value
238Define a new macro named by the first argument
239.Fa name
240to have the
241value of the second argument
242.Fa value .
243Each occurrence of
244.Sq $n
245(where
246.Ar n
247is 0 through 9) is replaced by the
248.Ar n Ns 'th
249argument.
250.Sq $0
251is the name of the calling macro.
252Undefined arguments are replaced by a null string.
253.Sq $#
254is replaced by the number of arguments;
255.Sq $*
256is replaced by all arguments comma separated;
257.Sq $@
258is the same as
259.Sq $*
260but all arguments are quoted against further expansion.
261.It Fn defn name ...
262Returns the quoted definition for each argument.
263This can be used to rename
264macro definitions (even for built-in macros).
265.It Fn divert num
266There are 10 output queues (numbered 0-9).
267At the end of processing
268.Nm m4
269concatenates all the queues in numerical order to produce the
270final output.
271Initially the output queue is 0.
272The divert
273macro allows you to select a new output queue (an invalid argument
274passed to divert causes output to be discarded).
275.It Ic divnum
276Returns the current output queue number.
277.It Ic dnl
278Discard input characters up to and including the next newline.
279.It Fn dumpdef name ...
280Prints the names and definitions for the named items, or for everything
281if no arguments are passed.
282.It Fn errprint msg
283Prints the first argument on the standard error output stream.
284.It Fn esyscmd cmd
285Passes its first argument to a shell and returns the shell's standard output.
286Note that the shell shares its standard input and standard error with
287.Nm m4 .
288.It Fn eval expr[,radix[,minimum]]
289Computes the first argument as an arithmetic expression using 32-bit
290arithmetic.
291Operators are the standard C ternary, arithmetic, logical,
292shift, relational, bitwise, and parentheses operators.
293You can specify
294octal, decimal, and hexadecimal numbers as in C.
295The optional second argument
296.Fa radix
297specifies the radix for the result and the optional third argument
298.Fa minimum
299specifies the minimum number of digits in the result.
300.It Fn expr expr
301This is an alias for
302.Ic eval .
303.It Fn format formatstring arg1 ...
304Returns
305.Fa formatstring
306with escape sequences substituted with
307.Fa arg1
308and following arguments, in a way similar to
309.Xr printf 3 .
310This built-in is only available in GNU-m4 compatibility mode, and the only
311parameters implemented are there for autoconf compatibility:
312left-padding flag, an optional field width, a maximum field width,
313*-specified field widths, and the %s and %c data type.
314.It Fn ifdef name yes no
315If the macro named by the first argument is defined then return the second
316argument, otherwise the third.
317If there is no third argument, the value is
318.Dv NULL .
319The word
320.Qq unix
321is predefined.
322.It Fn ifelse a b yes ...
323If the first argument
324.Fa a
325matches the second argument
326.Fa b
327then
328.Fn ifelse
329returns
330the third argument
331.Fa yes .
332If the match fails the three arguments are
333discarded and the next three arguments are used until there is
334zero or one arguments left, either this last argument or
335.Dv NULL
336is returned if no other matches were found.
337.It Fn include name
338Returns the contents of the file specified in the first argument.
339If the file is not found as is, look through the include path:
340first the directories specified with
341.Fl I
342on the command line, then the environment variable
343.Ev M4PATH ,
344as a colon-separated list of directories.
345Include aborts with an error message if the file cannot be included.
346.It Fn incr arg
347Increments the argument by 1.
348The argument must be a valid numeric string.
349.It Fn index string substring
350Returns the index of the second argument in the first argument (e.g.,
351.Ic index(the quick brown fox jumped, fox)
352returns 16).
353If the second
354argument is not found index returns \-1.
355.It Fn indir macro arg1 ...
356Indirectly calls the macro whose name is passed as the first argument,
357with the remaining arguments passed as first, ... arguments.
358.It Fn len arg
359Returns the number of characters in the first argument.
360Extra arguments
361are ignored.
362.It Fn m4exit code
363Immediately exits with the return value specified by the first argument,
3640 if none.
365.It Fn m4wrap todo
366Allows you to define what happens at the final
367.Dv EOF ,
368usually for cleanup purposes (e.g.,
369.Ic m4wrap("cleanup(tempfile)")
370causes the macro cleanup to be
371invoked after all other processing is done).
372.Pp
373Multiple calls to
374.Fn m4wrap
375get inserted in sequence at the final
376.Dv EOF .
377.It Fn maketemp template
378Invokes
379.Xr mkstemp 3
380on the first argument, and returns the modified string.
381This can be used to create unique
382temporary file names.
383.It Fn paste file
384Includes the contents of the file specified by the first argument without
385any macro processing.
386Aborts with an error message if the file cannot be
387included.
388.It Fn patsubst string regexp replacement
389Substitutes a regular expression in a string with a replacement string.
390Usual substitution patterns apply: an ampersand
391.Pq Sq \&&
392is replaced by the string matching the regular expression.
393The string
394.Sq \e# ,
395where
396.Sq #
397is a digit, is replaced by the corresponding back-reference.
398.It Fn popdef arg ...
399Restores the
400.Ic pushdef Ns ed
401definition for each argument.
402.It Fn pushdef macro def
403Takes the same arguments as
404.Ic define ,
405but it saves the definition on a
406stack for later retrieval by
407.Fn popdef .
408.It Fn regexp string regexp replacement
409Finds a regular expression in a string.
410If no further arguments are given,
411it returns the first match position or \-1 if no match.
412If a third argument
413is provided, it returns the replacement string, with sub-patterns replaced.
414.It Fn shift arg1 ...
415Returns all but the first argument, the remaining arguments are
416quoted and pushed back with commas in between.
417The quoting
418nullifies the effect of the extra scan that will subsequently be
419performed.
420.It Fn sinclude file
421Similar to
422.Ic include ,
423except it ignores any errors.
424.It Fn spaste file
425Similar to
426.Fn paste ,
427except it ignores any errors.
428.It Fn substr string offset length
429Returns a substring of the first argument starting at the offset specified
430by the second argument and the length specified by the third argument.
431If no third argument is present it returns the rest of the string.
432.It Fn syscmd cmd
433Passes the first argument to the shell.
434Nothing is returned.
435.It Ic sysval
436Returns the return value from the last
437.Ic syscmd .
438.It Fn traceon arg ...
439Enables tracing of macro expansions for the given arguments, or for all
440macros if no argument is given.
441.It Fn traceoff arg ...
442Disables tracing of macro expansions for the given arguments, or for all
443macros if no argument is given.
444.It Fn translit string mapfrom mapto
445Transliterate the characters in the first argument from the set
446given by the second argument to the set given by the third.
447You cannot use
448.Xr tr 1
449style abbreviations.
450.It Fn undefine name1 ...
451Removes the definition for the macros specified by its arguments.
452.It Fn undivert arg ...
453Flushes the named output queues (or all queues if no arguments).
454.It Ic unix
455A pre-defined macro for testing the OS platform.
456.It Ic __line__
457Returns the current file's line number.
458.It Ic __file__
459Returns the current file's name.
460.El
461.Sh STANDARDS
462The
463.Nm
464utility is compliant with the
465.St -p1003.1-2008
466specification.
467.Pp
468The flags
469.Op Fl dgIot
470and the macros
471.Ic builtin ,
472.Ic esyscmd ,
473.Ic expr ,
474.Ic format ,
475.Ic indir ,
476.Ic paste ,
477.Ic patsubst ,
478.Ic regexp ,
479.Ic spaste ,
480.Ic unix ,
481.Ic __line__ ,
482and
483.Ic __file__
484are extensions to that specification.
485.Pp
486The output format of tracing and of
487.Ic dumpdef
488are not specified in any standard,
489are likely to change and should not be relied upon.
490The current format of tracing is closely modelled on
491.Nm gnu-m4 ,
492to allow
493.Nm autoconf
494to work.
495.Pp
496The built-ins
497.Ic pushdef
498and
499.Ic popdef
500handle macro definitions as a stack.
501However,
502.Ic define
503interacts with the stack in an undefined way.
504In this implementation,
505.Ic define
506replaces the top-most definition only.
507Other implementations may erase all definitions on the stack instead.
508.Pp
509All built-ins do expand without arguments in many other
510.Nm m4 .
511.Pp
512Many other
513.Nm
514have dire size limitations with respect to buffer sizes.
515.Sh AUTHORS
516.An -nosplit
517.An Ozan Yigit Aq Mt oz@sis.yorku.ca
518and
519.An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU .
520.Pp
521GNU-m4 compatibility extensions by
522.An Marc Espie Aq Mt espie@cvs.openbsd.org .
523