1 .\" $NetBSD: m4.1,v 1.23 2012/04/08 22:00:39 wiz Exp $
3 .\" Copyright (c) 1989, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" Ozan Yigit at York University.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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
38 .Nd macro language processor
44 .Fl D Ar name Op No = Ar value
47 .Op Fl d Oo Oo +- Oc Ns Ar flags Oc
56 utility is a macro processor that can be used as a front end to any
57 language (e.g., C, ratfor, fortran, lex, and yacc).
58 If no input files are given,
60 reads from the standard input,
61 otherwise files specified on the command line are
62 processed in the given order.
63 Input files can be regular files, files in the m4 include paths, or a
66 denoting standard input.
69 the processed text to the standard output, unless told otherwise.
71 Macro calls have the form name(argument1[, argument2, ..., argumentN]).
73 There cannot be any space following the macro name and the open
76 If the macro name is not followed by an open
77 parenthesis it is processed with no arguments.
79 Macro names consist of a leading alphabetic or underscore
80 possibly followed by alphanumeric or underscore characters, e.g.,
81 valid macro names match the pattern
82 .Dq [a-zA-Z_][a-zA-Z0-9_]* .
84 In arguments to macros, leading unquoted space, tab, and newline
86 characters are ignored.
87 To quote strings, use left and right single quotes
88 .Pq e.g., Sq \ \&this is a string with a leading space .
89 You can change the quote characters with the
93 Most built-ins do not make any sense without arguments, and hence are not
94 recognized as special when not followed by an open parenthesis.
96 The options are as follows:
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
101 to have some value (or
103 .It Fl d Oo Oo +|- Oc Ns Ar flags Oc , Fl -debug Ns = Ns Oo Oo +|- Oc Ns Ar flags Oc
104 Set or unset trace flags.
105 The trace flags are as follows:
108 print macro arguments.
110 print macro expansion over several lines.
112 print result of macro expansion.
114 print filename location.
118 quote arguments and expansion with the current quotes.
120 start with all macros traced.
122 number macro expansions.
131 is used, the specified flags are added to or removed from the set of
132 active trace flags, respectively; otherwise, the specified flags
133 replace the set of active trace flags.
135 Specifying this option without an argument is equivalent to specifying
139 By default, trace is set to
141 .It Fl E , Fl -fatal-warnings
142 Set warnings to be fatal.
145 flag is specified, if warnings are issued, execution
148 will exit with a non-zero exit status.
151 flags are specified, execution will halt upon issuing the
154 will exit with a non-zero exit status.
155 This behavior matches GNU m4 1.4.9 and later.
156 .It Fl G , Fl -traditional
157 Disable GNU compatibility mode (see
161 Enable GNU compatibility mode.
164 handles simple character ranges (e.g.,
166 regular expressions mimic Emacs behavior,
169 calls are handled as a stack,
170 the number of diversions is unlimited,
171 empty names for macro definitions are allowed,
173 can be used to include files,
179 .It Fl I Ar dirname , Fl -include Ns = Ns Ar dirname
183 .It Fl o Ar filename , Fl -error-output Ns = Ns Ar filename
186 .It Fl P , Fl -prefix-builtins
187 Prefix all built-in macros with
189 For example, instead of writing
193 .It Fl s , Fl -synclines
194 Output line synchronization directives, suitable for
196 .It Fl t Ar macro , Fl -trace Ns = Ns Ar macro
199 .It Fl U Ns Ar name , Fl -undefine Ns = Ns Ar name
205 provides the following built-in macros.
206 They may be redefined, losing their original meaning.
207 Return values are null unless otherwise stated.
208 .Bl -tag -width changequote
210 Calls a built-in by its
212 overriding possible redefinitions.
213 .It Fn changecom startcomment endcomment
214 Changes the start comment and end comment sequences.
215 Comment sequences may be up to five characters long.
216 The default values are the hash sign
217 and the newline character.
218 .Bd -literal -offset indent
222 With no arguments, comments are turned off.
223 With one single argument, the end comment sequence is set
224 to the newline character.
225 .It Fn changequote beginquote endquote
226 Defines the open quote and close quote sequences.
227 Quote sequences may be up to five characters long.
228 The default values are the backquote character and the quote
230 .Bd -literal -offset indent
231 `Here is a quoted string'
234 With no arguments, the default quotes are restored.
235 With one single argument, the close quote sequence is set
236 to the newline character.
238 Decrements the argument
243 must be a valid numeric string.
244 .It Fn define name value
245 Define a new macro named by the first argument
248 value of the second argument
254 is 0 through 9) is replaced by the
258 is the name of the calling macro.
259 Undefined arguments are replaced by a null string.
261 is replaced by the number of arguments;
263 is replaced by all arguments comma separated;
267 but all arguments are quoted against further expansion.
269 Returns the quoted definition for each argument.
270 This can be used to rename
271 macro definitions (even for built-in macros).
273 There are 10 output queues (numbered 0-9).
274 At the end of processing
276 concatenates all the queues in numerical order to produce the
278 Initially the output queue is 0.
280 macro allows you to select a new output queue (an invalid argument
281 passed to divert causes output to be discarded).
283 Returns the current output queue number.
285 Discard input characters up to and including the next newline.
286 .It Fn dumpdef name ...
287 Prints the names and definitions for the named items, or for everything
288 if no arguments are passed.
290 Prints the first argument on the standard error output stream.
292 Passes its first argument to a shell and returns the shell's standard output.
293 Note that the shell shares its standard input and standard error with
295 .It Fn eval expr[,radix[,minimum]]
296 Computes the first argument as an arithmetic expression using 32-bit
298 Operators are the standard C ternary, arithmetic, logical,
299 shift, relational, bitwise, and parentheses operators.
301 octal, decimal, and hexadecimal numbers as in C.
302 The optional second argument
304 specifies the radix for the result and the optional third argument
306 specifies the minimum number of digits in the result.
310 .It Fn format formatstring arg1 ...
313 with escape sequences substituted with
315 and following arguments, in a way similar to
317 This built-in is only available in GNU-m4 compatibility mode, and the only
318 parameters implemented are there for autoconf compatibility:
319 left-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
322 If the macro named by the first argument is defined then return the second
323 argument, otherwise the third.
324 If there is no third argument, the value is
329 .It Fn ifelse a b yes ...
330 If the first argument
332 matches the second argument
339 If the match fails the three arguments are
340 discarded and the next three arguments are used until there is
341 zero or one arguments left, either this last argument or
343 is returned if no other matches were found.
345 Returns the contents of the file specified in the first argument.
346 If the file is not found as is, look through the include path:
347 first the directories specified with
349 on the command line, then the environment variable
351 as a colon-separated list of directories.
352 Include aborts with an error message if the file cannot be included.
354 Increments the argument by 1.
355 The argument must be a valid numeric string.
356 .It Fn index string substring
357 Returns the index of the second argument in the first argument (e.g.,
358 .Ic index(the quick brown fox jumped, fox)
361 argument is not found index returns \-1.
362 .It Fn indir macro arg1 ...
363 Indirectly calls the macro whose name is passed as the first argument,
364 with the remaining arguments passed as first, ... arguments.
366 Returns the number of characters in the first argument.
370 Immediately exits with the return value specified by the first argument,
373 Allows you to define what happens at the final
375 usually for cleanup purposes (e.g.,
376 .Ic m4wrap("cleanup(tempfile)")
377 causes the macro cleanup to be
378 invoked after all other processing is done).
382 get inserted in sequence at the final
384 .It Fn maketemp template
387 .It Fn mkstemp template
390 on the first argument, and returns the modified string.
391 This can be used to create unique
392 temporary file names.
394 Includes the contents of the file specified by the first argument without
395 any macro processing.
396 Aborts with an error message if the file cannot be
398 .It Fn patsubst string regexp replacement
399 Substitutes a regular expression in a string with a replacement string.
400 Usual substitution patterns apply: an ampersand
402 is replaced by the string matching the regular expression.
407 is a digit, is replaced by the corresponding back-reference.
408 .It Fn popdef arg ...
411 definition for each argument.
412 .It Fn pushdef macro def
413 Takes the same arguments as
415 but it saves the definition on a
416 stack for later retrieval by
418 .It Fn regexp string regexp replacement
419 Finds a regular expression in a string.
420 If no further arguments are given,
421 it returns the first match position or \-1 if no match.
423 is provided, it returns the replacement string, with sub-patterns replaced.
424 .It Fn shift arg1 ...
425 Returns all but the first argument, the remaining arguments are
426 quoted and pushed back with commas in between.
428 nullifies the effect of the extra scan that will subsequently be
433 except it ignores any errors.
437 except it ignores any errors.
438 .It Fn substr string offset length
439 Returns a substring of the first argument starting at the offset specified
440 by the second argument and the length specified by the third argument.
441 If no third argument is present it returns the rest of the string.
443 Passes the first argument to the shell.
446 Returns the return value from the last
448 .It Fn traceon arg ...
449 Enables tracing of macro expansions for the given arguments, or for all
450 macros if no argument is given.
451 .It Fn traceoff arg ...
452 Disables tracing of macro expansions for the given arguments, or for all
453 macros if no argument is given.
454 .It Fn translit string mapfrom mapto
455 Transliterate the characters in the first argument from the set
456 given by the second argument to the set given by the third.
460 .It Fn undefine name1 ...
461 Removes the definition for the macros specified by its arguments.
462 .It Fn undivert arg ...
463 Flushes the named output queues (or all queues if no arguments).
465 A pre-defined macro for testing the OS platform.
467 Returns the current file's line number.
469 Returns the current file's name.
476 macro can modify the exit status, as can the
484 .%I AT&T Bell Laboratories
485 .%T The M4 Macro Processor
486 .%R Computing Science Technical Report
493 utility is compliant with the
513 are extensions to that specification.
516 is not supposed to be a synonym for
518 but instead to be an insecure temporary file name creation function.
521 as being obsolescent and should not be used if portability is a concern.
527 are not specified in any standard,
528 are likely to change and should not be relied upon.
529 The current format of tracing is closely modelled on
539 handle macro definitions as a stack.
542 interacts with the stack in an undefined way.
543 In this implementation,
545 replaces the top-most definition only.
546 Other implementations may erase all definitions on the stack instead.
548 All built-ins do expand without arguments in many other
553 have dire size limitations with respect to buffer sizes.
556 .An Ozan Yigit Aq Mt oz@sis.yorku.ca
558 .An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU .
560 GNU-m4 compatibility extensions by
561 .An Marc Espie Aq Mt espie@cvs.openbsd.org .