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