1 .\" $NetBSD: m4.1,v 1.25 2014/01/07 13:32:21 wiz Exp $
2 .\" @(#) $OpenBSD: m4.1,v 1.56 2009/10/14 17:19:47 sthen 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
39 .Nd macro language processor
45 .Fl D Ar name Op No = Ar value
57 utility is a macro processor that can be used as a front end to any
58 language (e.g., C, ratfor, fortran, lex, and yacc).
59 If no input files are given,
61 reads from the standard input,
62 otherwise files specified on the command line are
63 processed in the given order.
64 Input files can be regular files, files in the m4 include paths, or a
67 denoting standard input.
70 the processed text to the standard output, unless told otherwise.
72 Macro calls have the form name(argument1[, argument2, ..., argumentN]).
74 There cannot be any space following the macro name and the open
77 If the macro name is not followed by an open
78 parenthesis it is processed with no arguments.
80 Macro names consist of a leading alphabetic or underscore
81 possibly followed by alphanumeric or underscore characters, e.g.,
82 valid macro names match the pattern
83 .Dq [a-zA-Z_][a-zA-Z0-9_]* .
85 In arguments to macros, leading unquoted space, tab, and newline
87 characters are ignored.
88 To quote strings, use left and right single quotes
90 .Sq "\ this is a string with a leading space"
92 You can change the quote characters with the
96 Most built-ins don't 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 Pf = 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 Activate GNU-m4 compatibility mode.
135 In this mode, translit handles simple character
136 ranges (e.g., a-z), regular expressions mimic emacs behavior,
137 multiple m4wrap calls are handled as a stack,
138 the number of diversions is unlimited,
139 empty names for macro definitions are allowed,
143 .It Fl I Ar "dirname"
151 Prefix all built-in macros with
153 For example, instead of writing
158 Output line synchronization directives, suitable for
163 .It Fl "U" Ns Ar "name"
169 provides the following built-in macros.
170 They may be redefined, losing their original meaning.
171 Return values are null unless otherwise stated.
172 .Bl -tag -width changequote
174 Calls a built-in by its
176 overriding possible redefinitions.
177 .It Fn changecom startcomment endcomment
178 Changes the start comment and end comment sequences.
179 Comment sequences may be up to five characters long.
180 The default values are the hash sign
181 and the newline character.
182 .Bd -literal -offset indent
186 With no arguments, comments are turned off.
187 With one single argument, the end comment sequence is set
188 to the newline character.
189 .It Fn changequote beginquote endquote
190 Defines the open quote and close quote sequences.
191 Quote sequences may be up to five characters long.
192 The default values are the backquote character and the quote
194 .Bd -literal -offset indent
195 `Here is a quoted string'
198 With no arguments, the default quotes are restored.
199 With one single argument, the close quote sequence is set
200 to the newline character.
202 Decrements the argument
207 must be a valid numeric string.
208 .It Fn define name value
209 Define a new macro named by the first argument
212 value of the second argument
218 is 0 through 9) is replaced by the
222 is the name of the calling macro.
223 Undefined arguments are replaced by a null string.
225 is replaced by the number of arguments;
227 is replaced by all arguments comma separated;
231 but all arguments are quoted against further expansion.
233 Returns the quoted definition for each argument.
234 This can be used to rename
235 macro definitions (even for built-in macros).
237 There are 10 output queues (numbered 0-9).
238 At the end of processing
240 concatenates all the queues in numerical order to produce the
242 Initially the output queue is 0.
244 macro allows you to select a new output queue (an invalid argument
245 passed to divert causes output to be discarded).
247 Returns the current output queue number.
249 Discard input characters up to and including the next newline.
250 .It Fn dumpdef name ...
251 Prints the names and definitions for the named items, or for everything
252 if no arguments are passed.
254 Prints the first argument on the standard error output stream.
256 Passes its first argument to a shell and returns the shell's standard output.
257 Note that the shell shares its standard input and standard error with
259 .It Fn eval expr[,radix[,minimum]]
260 Computes the first argument as an arithmetic expression using 32-bit
262 Operators are the standard C ternary, arithmetic, logical,
263 shift, relational, bitwise, and parentheses operators.
265 octal, decimal, and hexadecimal numbers as in C.
266 The optional second argument
268 specifies the radix for the result and the optional third argument
270 specifies the minimum number of digits in the result.
274 .It Fn format formatstring arg1 ...
277 with escape sequences substituted with
279 and following arguments, in a way similar to
281 This built-in is only available in GNU-m4 compatibility mode, and the only
282 parameters implemented are there for autoconf compatibility:
283 left-padding flag, an optional field width, a maximum field width,
284 *-specified field widths, and the %s and %c data type.
285 .It Fn ifdef name yes no
286 If the macro named by the first argument is defined then return the second
287 argument, otherwise the third.
288 If there is no third argument, the value is
293 .It Fn ifelse a b yes ...
294 If the first argument
296 matches the second argument
303 If the match fails the three arguments are
304 discarded and the next three arguments are used until there is
305 zero or one arguments left, either this last argument or
307 is returned if no other matches were found.
309 Returns the contents of the file specified in the first argument.
310 If the file is not found as is, look through the include path:
311 first the directories specified with
313 on the command line, then the environment variable
315 as a colon-separated list of directories.
316 Include aborts with an error message if the file cannot be included.
318 Increments the argument by 1.
319 The argument must be a valid numeric string.
320 .It Fn index string substring
321 Returns the index of the second argument in the first argument (e.g.,
322 .Ic index(the quick brown fox jumped, fox)
325 argument is not found index returns \-1.
326 .It Fn indir macro arg1 ...
327 Indirectly calls the macro whose name is passed as the first argument,
328 with the remaining arguments passed as first, ... arguments.
330 Returns the number of characters in the first argument.
334 Immediately exits with the return value specified by the first argument,
337 Allows you to define what happens at the final
339 usually for cleanup purposes (e.g.,
340 .Ic m4wrap("cleanup(tempfile)")
341 causes the macro cleanup to be
342 invoked after all other processing is done).
346 get inserted in sequence at the final
348 .It Fn maketemp template
351 on the first argument, and returns the modified string.
352 This can be used to create unique
353 temporary file names.
355 Includes the contents of the file specified by the first argument without
356 any macro processing.
357 Aborts with an error message if the file cannot be
359 .It Fn patsubst string regexp replacement
360 Substitutes a regular expression in a string with a replacement string.
361 Usual substitution patterns apply: an ampersand
363 is replaced by the string matching the regular expression.
368 is a digit, is replaced by the corresponding back-reference.
369 .It Fn popdef arg ...
372 definition for each argument.
373 .It Fn pushdef macro def
374 Takes the same arguments as
376 but it saves the definition on a
377 stack for later retrieval by
379 .It Fn regexp string regexp replacement
380 Finds a regular expression in a string.
381 If no further arguments are given,
382 it returns the first match position or \-1 if no match.
384 is provided, it returns the replacement string, with sub-patterns replaced.
385 .It Fn shift arg1 ...
386 Returns all but the first argument, the remaining arguments are
387 quoted and pushed back with commas in between.
389 nullifies the effect of the extra scan that will subsequently be
394 except it ignores any errors.
398 except it ignores any errors.
399 .It Fn substr string offset length
400 Returns a substring of the first argument starting at the offset specified
401 by the second argument and the length specified by the third argument.
402 If no third argument is present it returns the rest of the string.
404 Passes the first argument to the shell.
407 Returns the return value from the last
409 .It Fn traceon arg ...
410 Enables tracing of macro expansions for the given arguments, or for all
411 macros if no argument is given.
412 .It Fn traceoff arg ...
413 Disables tracing of macro expansions for the given arguments, or for all
414 macros if no argument is given.
415 .It Fn translit string mapfrom mapto
416 Transliterate the characters in the first argument from the set
417 given by the second argument to the set given by the third.
421 .It Fn undefine name1 ...
422 Removes the definition for the macros specified by its arguments.
423 .It Fn undivert arg ...
424 Flushes the named output queues (or all queues if no arguments).
426 A pre-defined macro for testing the OS platform.
428 Returns the current file's line number.
430 Returns the current file's name.
435 utility is compliant with the
455 are extensions to that specification.
457 The output format of tracing and of
459 are not specified in any standard,
460 are likely to change and should not be relied upon.
461 The current format of tracing is closely modelled on
471 handle macro definitions as a stack.
474 interacts with the stack in an undefined way.
475 In this implementation,
477 replaces the top-most definition only.
478 Other implementations may erase all definitions on the stack instead.
480 All built-ins do expand without arguments in many other
485 have dire size limitations with respect to buffer sizes.
488 .An Ozan Yigit Aq Mt oz@sis.yorku.ca
490 .An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU .
492 GNU-m4 compatibility extensions by
493 .An Marc Espie Aq Mt espie@cvs.openbsd.org .