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
41 .Nd macro language processor
47 .Fl D Ar name Op No = Ar value
50 .Op Fl d Oo Oo +- Oc Ns Ar flags Oc
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 Oo = Ns Ar value Oc , Fl -define Ns = Ns Ar name Ns Oo = Ns Ar value Oc
104 to have some value (or
106 .It Fl d Oo Oo +|- Oc Ns Ar flags Oc , Fl -debug Ns = Ns Oo Oo +|- Oc Ns Ar flags Oc
107 Set or unset trace flags.
108 The trace flags are as follows:
111 print macro arguments.
113 print macro expansion over several lines.
115 print result of macro expansion.
117 print filename location.
121 quote arguments and expansion with the current quotes.
123 start with all macros traced.
125 number macro expansions.
134 is used, the specified flags are added to or removed from the set of
135 active trace flags, respectively; otherwise, the specified flags
136 replace the set of active trace flags.
138 Specifying this option without an argument is equivalent to specifying
142 By default, trace is set to
144 .It Fl E , Fl -fatal-warnings
145 Set warnings to be fatal.
148 flag is specified, if warnings are issued, execution
151 will exit with a non-zero exit status.
154 flags are specified, execution will halt upon issuing the
157 will exit with a non-zero exit status.
158 This behavior matches GNU m4 1.4.9 and later.
159 .It Fl G , Fl -traditional
160 Disable GNU compatibility mode (see
164 Enable GNU compatibility mode.
167 handles simple character ranges (e.g.,
169 regular expressions mimic Emacs behavior,
172 calls are handled as a stack,
173 the number of diversions is unlimited,
174 empty names for macro definitions are allowed,
176 can be used to include files,
182 .It Fl I Ar dirname , Fl -include Ns = Ns Ar dirname
186 .It Fl o Ar filename , Fl -error-output Ns = Ns Ar filename
189 .It Fl P , Fl -prefix-builtins
190 Prefix all built-in macros with
192 For example, instead of writing
196 .It Fl s , Fl -synclines
197 Output line synchronization directives, suitable for
199 .It Fl t Ar macro , Fl -trace Ns = Ns Ar macro
202 .It Fl U Ns Ar name , Fl -undefine Ns = Ns Ar name
208 provides the following built-in macros.
209 They may be redefined, losing their original meaning.
210 Return values are null unless otherwise stated.
211 .Bl -tag -width changequote
213 Calls a built-in by its
215 overriding possible redefinitions.
216 .It Fn changecom startcomment endcomment
217 Changes the start comment and end comment sequences.
218 Comment sequences may be up to five characters long.
219 The default values are the hash sign
220 and the newline character.
221 .Bd -literal -offset indent
225 With no arguments, comments are turned off.
226 With one single argument, the end comment sequence is set
227 to the newline character.
228 .It Fn changequote beginquote endquote
229 Defines the open quote and close quote sequences.
230 Quote sequences may be up to five characters long.
231 The default values are the backquote character and the quote
233 .Bd -literal -offset indent
234 `Here is a quoted string'
237 With no arguments, the default quotes are restored.
238 With one single argument, the close quote sequence is set
239 to the newline character.
241 Decrements the argument
246 must be a valid numeric string.
247 .It Fn define name value
248 Define a new macro named by the first argument
251 value of the second argument
257 is 0 through 9) is replaced by the
261 is the name of the calling macro.
262 Undefined arguments are replaced by a null string.
264 is replaced by the number of arguments;
266 is replaced by all arguments comma separated;
270 but all arguments are quoted against further expansion.
272 Returns the quoted definition for each argument.
273 This can be used to rename
274 macro definitions (even for built-in macros).
276 There are 10 output queues (numbered 0-9).
277 At the end of processing
279 concatenates all the queues in numerical order to produce the
281 Initially the output queue is 0.
283 macro allows you to select a new output queue (an invalid argument
284 passed to divert causes output to be discarded).
286 Returns the current output queue number.
288 Discard input characters up to and including the next newline.
289 .It Fn dumpdef name ...
290 Prints the names and definitions for the named items, or for everything
291 if no arguments are passed.
293 Prints the first argument on the standard error output stream.
295 Passes its first argument to a shell and returns the shell's standard output.
296 Note that the shell shares its standard input and standard error with
298 .It Fn eval expr[,radix[,minimum]]
299 Computes the first argument as an arithmetic expression using 32-bit
301 Operators are the standard C ternary, arithmetic, logical,
302 shift, relational, bitwise, and parentheses operators.
304 octal, decimal, and hexadecimal numbers as in C.
305 The optional second argument
307 specifies the radix for the result and the optional third argument
309 specifies the minimum number of digits in the result.
313 .It Fn format formatstring arg1 ...
316 with escape sequences substituted with
318 and following arguments, in a way similar to
320 This built-in is only available in GNU-m4 compatibility mode, and the only
321 parameters implemented are there for autoconf compatibility:
322 left-padding flag, an optional field width, a maximum field width,
323 *-specified field widths, and the %s and %c data type.
324 .It Fn ifdef name yes no
325 If the macro named by the first argument is defined then return the second
326 argument, otherwise the third.
327 If there is no third argument, the value is
332 .It Fn ifelse a b yes ...
333 If the first argument
335 matches the second argument
342 If the match fails the three arguments are
343 discarded and the next three arguments are used until there is
344 zero or one arguments left, either this last argument or
346 is returned if no other matches were found.
348 Returns the contents of the file specified in the first argument.
349 If the file is not found as is, look through the include path:
350 first the directories specified with
352 on the command line, then the environment variable
354 as a colon-separated list of directories.
355 Include aborts with an error message if the file cannot be included.
357 Increments the argument by 1.
358 The argument must be a valid numeric string.
359 .It Fn index string substring
360 Returns the index of the second argument in the first argument (e.g.,
361 .Ic index(the quick brown fox jumped, fox)
364 argument is not found index returns \-1.
365 .It Fn indir macro arg1 ...
366 Indirectly calls the macro whose name is passed as the first argument,
367 with the remaining arguments passed as first, ... arguments.
369 Returns the number of characters in the first argument.
373 Immediately exits with the return value specified by the first argument,
376 Allows you to define what happens at the final
378 usually for cleanup purposes (e.g.,
379 .Ic m4wrap("cleanup(tempfile)")
380 causes the macro cleanup to be
381 invoked after all other processing is done).
385 get inserted in sequence at the final
387 .It Fn maketemp template
390 .It Fn mkstemp template
393 on the first argument, and returns the modified string.
394 This can be used to create unique
395 temporary file names.
397 Includes the contents of the file specified by the first argument without
398 any macro processing.
399 Aborts with an error message if the file cannot be
401 .It Fn patsubst string regexp replacement
402 Substitutes a regular expression in a string with a replacement string.
403 Usual substitution patterns apply: an ampersand
405 is replaced by the string matching the regular expression.
410 is a digit, is replaced by the corresponding back-reference.
411 .It Fn popdef arg ...
414 definition for each argument.
415 .It Fn pushdef macro def
416 Takes the same arguments as
418 but it saves the definition on a
419 stack for later retrieval by
421 .It Fn regexp string regexp replacement
422 Finds a regular expression in a string.
423 If no further arguments are given,
424 it returns the first match position or \-1 if no match.
426 is provided, it returns the replacement string, with sub-patterns replaced.
427 .It Fn shift arg1 ...
428 Returns all but the first argument, the remaining arguments are
429 quoted and pushed back with commas in between.
431 nullifies the effect of the extra scan that will subsequently be
436 except it ignores any errors.
440 except it ignores any errors.
441 .It Fn substr string offset length
442 Returns a substring of the first argument starting at the offset specified
443 by the second argument and the length specified by the third argument.
444 If no third argument is present it returns the rest of the string.
446 Passes the first argument to the shell.
449 Returns the return value from the last
451 .It Fn traceon arg ...
452 Enables tracing of macro expansions for the given arguments, or for all
453 macros if no argument is given.
454 .It Fn traceoff arg ...
455 Disables tracing of macro expansions for the given arguments, or for all
456 macros if no argument is given.
457 .It Fn translit string mapfrom mapto
458 Transliterate the characters in the first argument from the set
459 given by the second argument to the set given by the third.
463 .It Fn undefine name1 ...
464 Removes the definition for the macros specified by its arguments.
465 .It Fn undivert arg ...
466 Flushes the named output queues (or all queues if no arguments).
468 A pre-defined macro for testing the OS platform.
470 Returns the current file's line number.
472 Returns the current file's name.
479 macro can modify the exit status, as can the
487 .%I AT&T Bell Laboratories
488 .%T The M4 Macro Processor
489 .%R Computing Science Technical Report
496 utility is compliant with the
516 are extensions to that specification.
519 is not supposed to be a synonym for
521 but instead to be an insecure temporary file name creation function.
524 as being obsolescent and should not be used if portability is a concern.
530 are not specified in any standard,
531 are likely to change and should not be relied upon.
532 The current format of tracing is closely modelled on
542 handle macro definitions as a stack.
545 interacts with the stack in an undefined way.
546 In this implementation,
548 replaces the top-most definition only.
549 Other implementations may erase all definitions on the stack instead.
551 All built-ins do expand without arguments in many other
556 have dire size limitations with respect to buffer sizes.
559 .An Ozan Yigit Aq Mt oz@sis.yorku.ca
561 .An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU .
563 GNU-m4 compatibility extensions by
564 .An Marc Espie Aq Mt espie@cvs.openbsd.org .