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