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