1 .\" $NetBSD: m4.1,v 1.22 2010/05/14 17:14:28 joerg 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
34 .\" $FreeBSD: src/usr.bin/m4/m4.1,v 1.30 2012/11/17 01:54:24 svnexp Exp $
41 .Nd macro language processor
47 .Fl D Ar name Op No = Ar value
61 utility is a macro processor that can be used as a front end to any
62 language (e.g., C, ratfor, fortran, lex, and yacc).
63 If no input files are given,
65 reads from the standard input,
66 otherwise files specified on the command line are
67 processed in the given order.
68 Input files can be regular files, files in the m4 include paths, or a
71 denoting standard input.
74 the processed text to the standard output, unless told otherwise.
76 Macro calls have the form name(argument1[, argument2, ..., argumentN]).
78 There cannot be any space following the macro name and the open
81 If the macro name is not followed by an open
82 parenthesis it is processed with no arguments.
84 Macro names consist of a leading alphabetic or underscore
85 possibly followed by alphanumeric or underscore characters, e.g.,
86 valid macro names match the pattern
87 .Dq [a-zA-Z_][a-zA-Z0-9_]* .
89 In arguments to macros, leading unquoted space, tab, and newline
91 characters are ignored.
92 To quote strings, use left and right single quotes
94 .Sq "\ this is a string with a leading space"
96 You can change the quote characters with the
100 Most built-ins do not make any sense without arguments, and hence are not
101 recognized as special when not followed by an open parenthesis.
103 The options are as follows:
105 .It Fl D Ns Ar name Ns Op Pf = Ns Ar value
108 to have some value (or
113 may hold the following:
116 print macro arguments.
118 print macro expansion over several lines.
120 print result of macro expansion.
122 print filename location.
126 quote arguments and expansion with the current quotes.
128 start with all macros traced.
130 number macro expansions.
135 By default, trace is set to
138 Activate GNU-m4 compatibility mode.
139 In this mode, translit handles simple character
140 ranges (e.g., a-z), regular expressions mimic emacs behavior,
141 multiple m4wrap calls are handled as a stack,
142 the number of diversions is unlimited,
143 empty names for macro definitions are allowed,
147 .It Fl I Ar "dirname"
155 Prefix all built-in macros with
157 For example, instead of writing
162 Output line synchronization directives, suitable for
167 .It Fl "U" Ns Ar "name"
173 provides the following built-in macros.
174 They may be redefined, losing their original meaning.
175 Return values are null unless otherwise stated.
176 .Bl -tag -width changequote
178 Calls a built-in by its
180 overriding possible redefinitions.
181 .It Fn changecom startcomment endcomment
182 Changes the start comment and end comment sequences.
183 Comment sequences may be up to five characters long.
184 The default values are the hash sign
185 and the newline character.
186 .Bd -literal -offset indent
190 With no arguments, comments are turned off.
191 With one single argument, the end comment sequence is set
192 to the newline character.
193 .It Fn changequote beginquote endquote
194 Defines the open quote and close quote sequences.
195 Quote sequences may be up to five characters long.
196 The default values are the backquote character and the quote
198 .Bd -literal -offset indent
199 `Here is a quoted string'
202 With no arguments, the default quotes are restored.
203 With one single argument, the close quote sequence is set
204 to the newline character.
206 Decrements the argument
211 must be a valid numeric string.
212 .It Fn define name value
213 Define a new macro named by the first argument
216 value of the second argument
222 is 0 through 9) is replaced by the
226 is the name of the calling macro.
227 Undefined arguments are replaced by a null string.
229 is replaced by the number of arguments;
231 is replaced by all arguments comma separated;
235 but all arguments are quoted against further expansion.
237 Returns the quoted definition for each argument.
238 This can be used to rename
239 macro definitions (even for built-in macros).
241 There are 10 output queues (numbered 0-9).
242 At the end of processing
244 concatenates all the queues in numerical order to produce the
246 Initially the output queue is 0.
248 macro allows you to select a new output queue (an invalid argument
249 passed to divert causes output to be discarded).
251 Returns the current output queue number.
253 Discard input characters up to and including the next newline.
254 .It Fn dumpdef name ...
255 Prints the names and definitions for the named items, or for everything
256 if no arguments are passed.
258 Prints the first argument on the standard error output stream.
260 Passes its first argument to a shell and returns the shell's standard output.
261 Note that the shell shares its standard input and standard error with
264 Computes the first argument as an arithmetic expression using 32-bit
266 Operators are the standard C ternary, arithmetic, logical,
267 shift, relational, bitwise, and parentheses operators.
269 octal, decimal, and hexadecimal numbers as in C.
270 The second argument (if any)
271 specifies the radix for the result and the third argument (if any)
272 specifies the minimum number of digits in the result.
276 .It Fn format formatstring arg1 ...
279 with escape sequences substituted with
281 and following arguments, in a way similar to
283 This built-in is only available in GNU-m4 compatibility mode, and the only
284 parameters implemented are there for autoconf compatibility:
285 left-padding flag, an optional field width, a maximum field width,
286 *-specified field widths, and the %s and %c data type.
287 .It Fn ifdef name yes no
288 If the macro named by the first argument is defined then return the second
289 argument, otherwise the third.
290 If there is no third argument, the value is
295 .It Fn ifelse a b yes ...
296 If the first argument
298 matches the second argument
305 If the match fails the three arguments are
306 discarded and the next three arguments are used until there is
307 zero or one arguments left, either this last argument or
309 is returned if no other matches were found.
311 Returns the contents of the file specified in the first argument.
312 If the file is not found as is, look through the include path:
313 first the directories specified with
315 on the command line, then the environment variable
317 as a colon-separated list of directories.
318 Include aborts with an error message if the file cannot be included.
320 Increments the argument by 1.
321 The argument must be a valid numeric string.
322 .It Fn index string substring
323 Returns the index of the second argument in the first argument (e.g.,
324 .Ic index(the quick brown fox jumped, fox)
327 argument is not found index returns \-1.
328 .It Fn indir macro arg1 ...
329 Indirectly calls the macro whose name is passed as the first argument,
330 with the remaining arguments passed as first, ... arguments.
332 Returns the number of characters in the first argument.
336 Immediately exits with the return value specified by the first argument,
339 Allows you to define what happens at the final
341 usually for cleanup purposes (e.g.,
342 .Ic m4wrap("cleanup(tempfile)")
343 causes the macro cleanup to be
344 invoked after all other processing is done).
348 get inserted in sequence at the final
350 .It Fn maketemp template
353 on the first argument, and returns the modified string.
354 This can be used to create unique
355 temporary file names.
357 Includes the contents of the file specified by the first argument without
358 any macro processing.
359 Aborts with an error message if the file cannot be
361 .It Fn patsubst string regexp replacement
362 Substitutes a regular expression in a string with a replacement string.
363 Usual substitution patterns apply: an ampersand
365 is replaced by the string matching the regular expression.
370 is a digit, is replaced by the corresponding back-reference.
371 .It Fn popdef arg ...
374 definition for each argument.
375 .It Fn pushdef macro def
376 Takes the same arguments as
378 but it saves the definition on a
379 stack for later retrieval by
381 .It Fn regexp string regexp replacement
382 Finds a regular expression in a string.
383 If no further arguments are given,
384 it returns the first match position or \-1 if no match.
386 is provided, it returns the replacement string, with sub-patterns replaced.
387 .It Fn shift arg1 ...
388 Returns all but the first argument, the remaining arguments are
389 quoted and pushed back with commas in between.
391 nullifies the effect of the extra scan that will subsequently be
396 except it ignores any errors.
400 except it ignores any errors.
401 .It Fn substr string offset length
402 Returns a substring of the first argument starting at the offset specified
403 by the second argument and the length specified by the third argument.
404 If no third argument is present it returns the rest of the string.
406 Passes the first argument to the shell.
409 Returns the return value from the last
411 .It Fn traceon arg ...
412 Enables tracing of macro expansions for the given arguments, or for all
413 macros if no argument is given.
414 .It Fn traceoff arg ...
415 Disables tracing of macro expansions for the given arguments, or for all
416 macros if no argument is given.
417 .It Fn translit string mapfrom mapto
418 Transliterate the characters in the first argument from the set
419 given by the second argument to the set given by the third.
423 .It Fn undefine name1 ...
424 Removes the definition for the macros specified by its arguments.
425 .It Fn undivert arg ...
426 Flushes the named output queues (or all queues if no arguments).
428 A pre-defined macro for testing the OS platform.
430 Returns the current file's line number.
432 Returns the current file's name.
437 utility is compliant with the
457 are extensions to that specification.
459 The output format of tracing and of
461 are not specified in any standard,
462 are likely to change and should not be relied upon.
463 The current format of tracing is closely modelled on
473 handle macro definitions as a stack.
476 interacts with the stack in an undefined way.
477 In this implementation,
479 replaces the top-most definition only.
480 Other implementations may erase all definitions on the stack instead.
482 All built-ins do expand without arguments in many other
487 have dire size limitations with respect to buffer sizes.
490 .An Ozan Yigit Aq Mt oz@sis.yorku.ca
492 .An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU .
494 GNU-m4 compatibility extensions by
495 .An Marc Espie Aq Mt espie@cvs.openbsd.org .