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 $
4 .\" Copyright (c) 1989, 1993
5 .\" The Regents of the University of California. All rights reserved.
7 .\" This code is derived from software contributed to Berkeley by
8 .\" Ozan Yigit at York University.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
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.
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
36 .Dd $Mdocdate: June 15 2017 $
41 .Nd macro language processor
47 .Fl D Ar name Op No = Ar value
59 utility is a macro processor that can be used as a front end to any
60 language (e.g., C, ratfor, fortran, lex, and yacc).
61 If no input files are given,
63 reads from the standard input,
64 otherwise files specified on the command line are
65 processed in the given order.
66 Input files can be regular files, files in the m4 include paths, or a
69 denoting standard input.
72 the processed text to the standard output, unless told otherwise.
74 Macro calls have the form name(argument1[, argument2, ..., argumentN]).
76 There cannot be any space following the macro name and the open
79 If the macro name is not followed by an open
80 parenthesis it is processed with no arguments.
82 Macro names consist of a leading alphabetic or underscore
83 possibly followed by alphanumeric or underscore characters, e.g.,
84 valid macro names match the pattern
85 .Dq [a-zA-Z_][a-zA-Z0-9_]* .
87 In arguments to macros, leading unquoted space, tab, and newline
89 characters are ignored.
90 To quote strings, use left and right single quotes
91 .Pq e.g., Sq \ \&this is a string with a leading space .
92 You can change the quote characters with the
96 Most built-ins do not make any sense without arguments, and hence are not
97 recognized as special when not followed by an open parenthesis.
99 The options are as follows:
101 .It Fl D Ns Ar name Ns Op = Ns Ar value
104 to have some value (or
109 may hold the following:
112 print macro arguments.
114 print macro expansion over several lines.
116 print result of macro expansion.
118 print filename location.
122 quote arguments and expansion with the current quotes.
124 start with all macros traced.
126 number macro expansions.
131 By default, trace is set to
134 Set warnings to be fatal.
137 flag is specified, if warnings are issued, execution
140 will exit with a non-zero exit status.
143 flags are specified, execution will halt upon issuing the
146 will exit with a non-zero exit status.
147 This behaviour matches GNU-m4 1.4.9 and later.
149 Activate GNU-m4 compatibility mode.
150 In this mode, translit handles simple character
151 ranges (e.g., a-z), regular expressions mimic emacs behavior,
152 multiple m4wrap calls are handled as a stack,
153 the number of diversions is unlimited,
154 empty names for macro definitions are allowed,
158 .It Fl I Ar "dirname"
166 Prefix all built-in macros with
168 For example, instead of writing
173 Output line synchronization directives, suitable for
178 .It Fl "U" Ns Ar "name"
184 provides the following built-in macros.
185 They may be redefined, losing their original meaning.
186 Return values are null unless otherwise stated.
187 .Bl -tag -width changequote
189 Calls a built-in by its
191 overriding possible redefinitions.
192 .It Fn changecom startcomment endcomment
193 Changes the start comment and end comment sequences.
194 Comment sequences may be up to five characters long.
195 The default values are the hash sign
196 and the newline character.
197 .Bd -literal -offset indent
201 With no arguments, comments are turned off.
202 With one single argument, the end comment sequence is set
203 to the newline character.
204 .It Fn changequote beginquote endquote
205 Defines the open quote and close quote sequences.
206 Quote sequences may be up to five characters long.
207 The default values are the backquote character and the quote
209 .Bd -literal -offset indent
210 `Here is a quoted string'
213 With no arguments, the default quotes are restored.
214 With one single argument, the close quote sequence is set
215 to the newline character.
217 Decrements the argument
222 must be a valid numeric string.
223 .It Fn define name value
224 Define a new macro named by the first argument
227 value of the second argument
233 is 0 through 9) is replaced by the
237 is the name of the calling macro.
238 Undefined arguments are replaced by a null string.
240 is replaced by the number of arguments;
242 is replaced by all arguments comma separated;
246 but all arguments are quoted against further expansion.
248 Returns the quoted definition for each argument.
249 This can be used to rename
250 macro definitions (even for built-in macros).
252 There are 10 output queues (numbered 0-9).
253 At the end of processing
255 concatenates all the queues in numerical order to produce the
257 Initially the output queue is 0.
259 macro allows you to select a new output queue (an invalid argument
260 passed to divert causes output to be discarded).
262 Returns the current output queue number.
264 Discard input characters up to and including the next newline.
265 .It Fn dumpdef name ...
266 Prints the names and definitions for the named items, or for everything
267 if no arguments are passed.
269 Prints the first argument on the standard error output stream.
271 Passes its first argument to a shell and returns the shell's standard output.
272 Note that the shell shares its standard input and standard error with
274 .It Fn eval expr[,radix[,minimum]]
275 Computes the first argument as an arithmetic expression using 32-bit
277 Operators are the standard C ternary, arithmetic, logical,
278 shift, relational, bitwise, and parentheses operators.
280 octal, decimal, and hexadecimal numbers as in C.
281 The optional second argument
283 specifies the radix for the result and the optional third argument
285 specifies the minimum number of digits in the result.
289 .It Fn format formatstring arg1 ...
292 with escape sequences substituted with
294 and following arguments, in a way similar to
296 This built-in is only available in GNU-m4 compatibility mode, and the only
297 parameters implemented are there for autoconf compatibility:
298 left-padding flag, an optional field width, a maximum field width,
299 *-specified field widths, and the %s and %c data type.
300 .It Fn ifdef name yes no
301 If the macro named by the first argument is defined then return the second
302 argument, otherwise the third.
303 If there is no third argument, the value is
308 .It Fn ifelse a b yes ...
309 If the first argument
311 matches the second argument
318 If the match fails the three arguments are
319 discarded and the next three arguments are used until there is
320 zero or one arguments left, either this last argument or
322 is returned if no other matches were found.
324 Returns the contents of the file specified in the first argument.
325 If the file is not found as is, look through the include path:
326 first the directories specified with
328 on the command line, then the environment variable
330 as a colon-separated list of directories.
331 Include aborts with an error message if the file cannot be included.
333 Increments the argument by 1.
334 The argument must be a valid numeric string.
335 .It Fn index string substring
336 Returns the index of the second argument in the first argument (e.g.,
337 .Ic index(the quick brown fox jumped, fox)
340 argument is not found index returns \-1.
341 .It Fn indir macro arg1 ...
342 Indirectly calls the macro whose name is passed as the first argument,
343 with the remaining arguments passed as first, ... arguments.
345 Returns the number of characters in the first argument.
349 Immediately exits with the return value specified by the first argument,
352 Allows you to define what happens at the final
354 usually for cleanup purposes (e.g.,
355 .Ic m4wrap("cleanup(tempfile)")
356 causes the macro cleanup to be
357 invoked after all other processing is done).
361 get inserted in sequence at the final
363 .It Fn maketemp template
366 .It Fn mkstemp template
369 on the first argument, and returns the modified string.
370 This can be used to create unique
371 temporary file names.
373 Includes the contents of the file specified by the first argument without
374 any macro processing.
375 Aborts with an error message if the file cannot be
377 .It Fn patsubst string regexp replacement
378 Substitutes a regular expression in a string with a replacement string.
379 Usual substitution patterns apply: an ampersand
381 is replaced by the string matching the regular expression.
386 is a digit, is replaced by the corresponding back-reference.
387 .It Fn popdef arg ...
390 definition for each argument.
391 .It Fn pushdef macro def
392 Takes the same arguments as
394 but it saves the definition on a
395 stack for later retrieval by
397 .It Fn regexp string regexp replacement
398 Finds a regular expression in a string.
399 If no further arguments are given,
400 it returns the first match position or \-1 if no match.
402 is provided, it returns the replacement string, with sub-patterns replaced.
403 .It Fn shift arg1 ...
404 Returns all but the first argument, the remaining arguments are
405 quoted and pushed back with commas in between.
407 nullifies the effect of the extra scan that will subsequently be
412 except it ignores any errors.
416 except it ignores any errors.
417 .It Fn substr string offset length
418 Returns a substring of the first argument starting at the offset specified
419 by the second argument and the length specified by the third argument.
420 If no third argument is present it returns the rest of the string.
422 Passes the first argument to the shell.
425 Returns the return value from the last
427 .It Fn traceon arg ...
428 Enables tracing of macro expansions for the given arguments, or for all
429 macros if no argument is given.
430 .It Fn traceoff arg ...
431 Disables tracing of macro expansions for the given arguments, or for all
432 macros if no argument is given.
433 .It Fn translit string mapfrom mapto
434 Transliterate the characters in the first argument from the set
435 given by the second argument to the set given by the third.
439 .It Fn undefine name1 ...
440 Removes the definition for the macros specified by its arguments.
441 .It Fn undivert arg ...
442 Flushes the named output queues (or all queues if no arguments).
444 A pre-defined macro for testing the OS platform.
446 Returns the current file's line number.
448 Returns the current file's name.
455 macro can modify the exit status, as can the
461 utility is compliant with the
481 are extensions to that specification.
484 is not supposed to be a synonym for
486 but instead to be an insecure temporary file name creation function.
489 as being obsolescent and should not be used if portability is a concern.
495 are not specified in any standard,
496 are likely to change and should not be relied upon.
497 The current format of tracing is closely modelled on
507 handle macro definitions as a stack.
510 interacts with the stack in an undefined way.
511 In this implementation,
513 replaces the top-most definition only.
514 Other implementations may erase all definitions on the stack instead.
516 All built-ins do expand without arguments in many other
521 have dire size limitations with respect to buffer sizes.
524 .An Ozan Yigit Aq Mt oz@sis.yorku.ca
526 .An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU .
528 GNU-m4 compatibility extensions by
529 .An Marc Espie Aq Mt espie@cvs.openbsd.org .