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