Annotation of src/usr.bin/m4/m4.1, Revision 1.40
1.40 ! espie 1: .\" @(#) $OpenBSD: m4.1,v 1.39 2005/09/06 15:33:21 espie Exp $
1.32 jmc 2: .\"
1.29 jmc 3: .\" Copyright (c) 1989, 1993
4: .\" The Regents of the University of California. All rights reserved.
1.1 deraadt 5: .\"
1.29 jmc 6: .\" This code is derived from software contributed to Berkeley by
7: .\" Ozan Yigit at York University.
1.1 deraadt 8: .\"
1.29 jmc 9: .\" Redistribution and use in source and binary forms, with or without
10: .\" modification, are permitted provided that the following conditions
11: .\" are met:
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.
1.31 millert 17: .\" 3. Neither the name of the University nor the names of its contributors
1.29 jmc 18: .\" may be used to endorse or promote products derived from this software
19: .\" without specific prior written permission.
20: .\"
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
31: .\" SUCH DAMAGE.
1.32 jmc 32: .\"
1.1 deraadt 33: .Dd January 26, 1993
1.5 aaron 34: .Dt M4 1
1.1 deraadt 35: .Os
36: .Sh NAME
37: .Nm m4
38: .Nd macro language processor
39: .Sh SYNOPSIS
40: .Nm m4
1.23 espie 41: .Op Fl d Ar flags
42: .Op Fl t Ar name
1.28 espie 43: .Op Fl o Ar filename
1.12 espie 44: .Op Fl g
1.25 espie 45: .Op Fl s
1.1 deraadt 46: .Oo
47: .Fl D Ns Ar name Ns Op Ar =value
48: .Oc
49: .Op Fl U Ns Ar name
1.7 espie 50: .Op Fl I Ar dirname
1.1 deraadt 51: .Sh DESCRIPTION
52: The
53: .Nm m4
54: utility is a macro processor that can be used as a front end to any
55: language (e.g., C, ratfor, fortran, lex, and yacc).
56: .Nm m4
57: reads from the standard input and writes
58: the processed text to the standard output.
59: .Pp
1.8 espie 60: Macro calls have the form name(argument1[, argument2, ..., argumentN]).
1.1 deraadt 61: .Pp
62: There cannot be any space following the macro name and the open
1.10 aaron 63: parenthesis
1.33 jmc 64: .Pq Sq ( .
1.10 aaron 65: If the macro name is not followed by an open
1.5 aaron 66: parenthesis it is processed with no arguments.
1.1 deraadt 67: .Pp
68: Macro names consist of a leading alphabetic or underscore
1.9 espie 69: possibly followed by alphanumeric or underscore characters, e.g.,
1.10 aaron 70: valid macro names match the pattern
71: .Dq [a-zA-Z_][a-zA-Z0-9_]* .
1.1 deraadt 72: .Pp
1.10 aaron 73: In arguments to macros, leading unquoted space, tab, and newline
1.33 jmc 74: .Pq Sq \en
1.10 aaron 75: characters are ignored.
1.33 jmc 76: To quote strings, use left and right single quotes
77: .Po e.g.,\ \&
78: .Sq "\ this is a string with a leading space"
79: .Pc .
1.10 aaron 80: You can change the quote characters with the
1.5 aaron 81: .Ic changequote
82: built-in macro.
1.9 espie 83: .Pp
1.12 espie 84: Most built-ins don't make any sense without arguments, and hence are not
1.9 espie 85: recognized as special when not followed by an open parenthesis.
1.1 deraadt 86: .Pp
87: The options are as follows:
1.20 aaron 88: .Bl -tag -width Ds
1.1 deraadt 89: .It Fl D Ns Ar name Ns Oo
90: .Ar =value
91: .Oc
92: Define the symbol
93: .Ar name
1.10 aaron 94: to have some value (or
95: .Dv NULL ) .
1.1 deraadt 96: .It Fl "U" Ns Ar "name"
97: Undefine the symbol
98: .Ar name .
1.7 espie 99: .It Fl I Ar "dirname"
1.15 aaron 100: Add directory
1.7 espie 101: .Ar dirname
102: to the include path.
1.23 espie 103: .It Fl d Ar "flags"
104: Set trace flags.
1.32 jmc 105: .Ar flags
1.23 espie 106: may hold the following:
107: .Bl -tag -width Ds
108: .It Ar a
109: print macro arguments.
110: .It Ar c
111: print macro expansion over several lines.
112: .It Ar e
113: print result of macro expansion.
114: .It Ar f
115: print filename location.
116: .It Ar l
117: print line number.
118: .It Ar q
119: quote arguments and expansion with the current quotes.
120: .It Ar t
121: start with all macros traced.
122: .It Ar x
123: number macro expansions.
124: .It Ar V
125: turn on all options.
126: .El
127: .Pp
1.32 jmc 128: By default, trace is set to
1.23 espie 129: .Qq eq .
1.28 espie 130: .It Fl o Ar filename
131: Send trace output to
132: .Ar filename .
1.23 espie 133: .It Fl t Ar macro
1.32 jmc 134: Turn tracing on for
1.23 espie 135: .Ar macro .
1.12 espie 136: .It Fl g
1.13 aaron 137: Activate GNU-m4 compatibility mode.
1.40 ! espie 138: In this mode, translit handles simple character
1.19 espie 139: ranges (e.g., a-z), regular expressions mimic emacs behavior,
1.38 espie 140: multiple m4wrap calls are handled as a stack,
1.19 espie 141: and the number of diversions is unlimited.
1.25 espie 142: .It Fl s
143: Output line synchronization directives, suitable for
144: .Xr cpp 1 .
1.17 aaron 145: .El
1.1 deraadt 146: .Sh SYNTAX
147: .Nm m4
1.10 aaron 148: provides the following built-in macros.
149: They may be redefined, losing their original meaning.
150: Return values are null unless otherwise stated.
1.36 espie 151: .Bl -tag -width changequote
152: .It Fn builtin name
1.37 jmc 153: Calls a built-in by its
154: .Fa name ,
1.36 espie 155: overriding possible redefinitions.
156: .It Fn changecom startcomment endcomment
1.39 espie 157: Changes the start comment and end comment sequences.
158: Comment sequences may be up to five characters long.
159: The default values are the pound sign
1.10 aaron 160: and the newline character.
1.39 espie 161: .Bd -literal -offset indent
162: # This is a comment
163: .Ed
164: .Pp
165: With no arguments, comments are turned off.
166: With one single argument, the end comment sequence is set
167: to the newline character.
1.36 espie 168: .It Fn changequote beginquote endquote
1.39 espie 169: Defines the open quote and close quote sequences.
170: Quote sequences may be up to five characters long.
171: The default values are the backquote character and the quote
172: character.
173: .Bd -literal -offset indent
174: `Here is a quoted string'
175: .Ed
176: .Pp
177: With no arguments, the default quotes are restored.
178: With one single argument, the close quote sequence is set
179: to the newline character.
1.36 espie 180: .It Fn decr arg
1.37 jmc 181: Decrements the argument
182: .Fa arg
1.36 espie 183: by 1.
1.37 jmc 184: The argument
1.36 espie 185: .Fa arg
186: must be a valid numeric string.
187: .It Fn define name value
1.37 jmc 188: Define a new macro named by the first argument
189: .Fa name
1.36 espie 190: to have the
191: value of the second argument
192: .Fa value .
1.10 aaron 193: Each occurrence of
1.33 jmc 194: .Sq $n
1.10 aaron 195: (where
196: .Ar n
197: is 0 through 9) is replaced by the
198: .Ar n Ns 'th
199: argument.
1.33 jmc 200: .Sq $0
1.10 aaron 201: is the name of the calling macro.
202: Undefined arguments are replaced by a null string.
1.33 jmc 203: .Sq $#
1.10 aaron 204: is replaced by the number of arguments;
1.33 jmc 205: .Sq $*
1.10 aaron 206: is replaced by all arguments comma separated;
1.33 jmc 207: .Sq $@
1.10 aaron 208: is the same as
1.33 jmc 209: .Sq $*
1.10 aaron 210: but all arguments are quoted against further expansion.
1.36 espie 211: .It Fn defn name ...
1.10 aaron 212: Returns the quoted definition for each argument.
213: This can be used to rename
1.1 deraadt 214: macro definitions (even for built-in macros).
1.36 espie 215: .It Fn divert num
1.1 deraadt 216: There are 10 output queues (numbered 0-9).
217: At the end of processing
218: .Nm m4
219: concatenates all the queues in numerical order to produce the
1.10 aaron 220: final output.
221: Initially the output queue is 0.
222: The divert
1.1 deraadt 223: macro allows you to select a new output queue (an invalid argument
224: passed to divert causes output to be discarded).
1.5 aaron 225: .It Ic divnum
1.1 deraadt 226: Returns the current output queue number.
1.5 aaron 227: .It Ic dnl
1.1 deraadt 228: Discard input characters up to and including the next newline.
1.36 espie 229: .It Fn dumpdef name ...
1.1 deraadt 230: Prints the names and definitions for the named items, or for everything
231: if no arguments are passed.
1.36 espie 232: .It Fn errprint msg
1.1 deraadt 233: Prints the first argument on the standard error output stream.
1.36 espie 234: .It Fn esyscmd cmd
1.26 pvalchev 235: Passes its first argument to a shell and returns the shell's standard output.
1.18 espie 236: Note that the shell shares its standard input and standard error with
1.33 jmc 237: .Nm m4 .
1.36 espie 238: .It Fn eval expr
1.1 deraadt 239: Computes the first argument as an arithmetic expression using 32-bit
1.10 aaron 240: arithmetic.
241: Operators are the standard C ternary, arithmetic, logical,
242: shift, relational, bitwise, and parentheses operators.
243: You can specify
244: octal, decimal, and hexadecimal numbers as in C.
245: The second argument (if any)
246: specifies the radix for the result and the third argument (if any)
247: specifies the minimum number of digits in the result.
1.36 espie 248: .It Fn expr expr
1.5 aaron 249: This is an alias for
250: .Ic eval .
1.36 espie 251: .It Fn ifdef name yes no
1.1 deraadt 252: If the macro named by the first argument is defined then return the second
1.10 aaron 253: argument, otherwise the third.
254: If there is no third argument, the value is
255: .Dv NULL .
256: The word
257: .Qq unix
258: is predefined.
1.36 espie 259: .It Fn ifelse a b yes ...
1.37 jmc 260: If the first argument
1.36 espie 261: .Fa a
1.37 jmc 262: matches the second argument
1.36 espie 263: .Fa b
264: then
265: .Fn ifelse
1.5 aaron 266: returns
1.36 espie 267: the third argument
268: .Fa yes .
1.10 aaron 269: If the match fails the three arguments are
1.1 deraadt 270: discarded and the next three arguments are used until there is
1.10 aaron 271: zero or one arguments left, either this last argument or
272: .Dv NULL
273: is returned if no other matches were found.
1.36 espie 274: .It Fn include name
1.1 deraadt 275: Returns the contents of the file specified in the first argument.
1.7 espie 276: If the file is not found as is, look through the include path:
277: first the directories specified with
278: .Fl I
279: on the command line, then the environment variable
1.10 aaron 280: .Ev M4PATH ,
1.7 espie 281: as a colon-separated list of directories.
1.1 deraadt 282: Include aborts with an error message if the file cannot be included.
1.36 espie 283: .It Fn incr arg
1.10 aaron 284: Increments the argument by 1.
285: The argument must be a valid numeric string.
1.36 espie 286: .It Fn index string substring
1.1 deraadt 287: Returns the index of the second argument in the first argument (e.g.,
1.10 aaron 288: .Ic index(the quick brown fox jumped, fox)
289: returns 16).
290: If the second
291: argument is not found index returns \-1.
1.36 espie 292: .It Fn indir macro arg1 ...
1.26 pvalchev 293: Indirectly calls the macro whose name is passed as the first argument,
1.11 espie 294: with the remaining arguments passed as first, ... arguments.
1.36 espie 295: .It Fn len arg
1.10 aaron 296: Returns the number of characters in the first argument.
297: Extra arguments
1.1 deraadt 298: are ignored.
1.36 espie 299: .It Fn m4exit code
1.1 deraadt 300: Immediately exits with the return value specified by the first argument,
301: 0 if none.
1.36 espie 302: .It Fn m4wrap todo
1.10 aaron 303: Allows you to define what happens at the final
304: .Dv EOF ,
305: usually for cleanup purposes (e.g.,
306: .Ic m4wrap("cleanup(tempfile)")
307: causes the macro cleanup to be
1.16 aaron 308: invoked after all other processing is done).
1.38 espie 309: .Pp
310: Multiple calls to
311: .Fn m4wrap
312: get inserted in sequence at the final
313: .Dv EOF .
1.36 espie 314: .It Fn maketemp template
1.35 miod 315: Invokes
316: .Xr mkstemp 3
317: on the first argument, and returns the modified string.
1.10 aaron 318: This can be used to create unique
1.1 deraadt 319: temporary file names.
1.36 espie 320: .It Fn paste file
1.1 deraadt 321: Includes the contents of the file specified by the first argument without
1.10 aaron 322: any macro processing.
323: Aborts with an error message if the file cannot be
1.1 deraadt 324: included.
1.36 espie 325: .It Fn patsubst string regexp replacement
1.11 espie 326: Substitutes a regular expression in a string with a replacement string.
327: Usual substitution patterns apply: an ampersand
1.33 jmc 328: .Pq Sq \&&
1.11 espie 329: is replaced by the string matching the regular expression.
330: The string
1.33 jmc 331: .Sq \e# ,
1.11 espie 332: where
1.33 jmc 333: .Sq #
1.11 espie 334: is a digit, is replaced by the corresponding back-reference.
1.36 espie 335: .It Fn popdef arg ...
1.5 aaron 336: Restores the
337: .Ic pushdef Ns ed
338: definition for each argument.
1.36 espie 339: .It Fn pushdef macro def
1.5 aaron 340: Takes the same arguments as
341: .Ic define ,
342: but it saves the definition on a
343: stack for later retrieval by
1.36 espie 344: .Fn popdef .
345: .It Fn regexp string regexp replacement
1.13 aaron 346: Finds a regular expression in a string.
347: If no further arguments are given,
1.14 aaron 348: it returns the first match position or \-1 if no match.
349: If a third argument
1.11 espie 350: is provided, it returns the replacement string, with sub-patterns replaced.
1.36 espie 351: .It Fn shift arg1 ...
1.1 deraadt 352: Returns all but the first argument, the remaining arguments are
1.10 aaron 353: quoted and pushed back with commas in between.
354: The quoting
1.1 deraadt 355: nullifies the effect of the extra scan that will subsequently be
356: performed.
1.36 espie 357: .It Fn sinclude file
1.5 aaron 358: Similar to
359: .Ic include ,
360: except it ignores any errors.
1.36 espie 361: .It Fn spaste file
1.5 aaron 362: Similar to
1.36 espie 363: .Fn paste ,
1.5 aaron 364: except it ignores any errors.
1.36 espie 365: .It Fn substr string offset length
1.1 deraadt 366: Returns a substring of the first argument starting at the offset specified
367: by the second argument and the length specified by the third argument.
368: If no third argument is present it returns the rest of the string.
1.36 espie 369: .It Fn syscmd cmd
1.10 aaron 370: Passes the first argument to the shell.
371: Nothing is returned.
1.5 aaron 372: .It Ic sysval
373: Returns the return value from the last
374: .Ic syscmd .
1.36 espie 375: .It Fn traceon arg ...
1.22 espie 376: Enables tracing of macro expansions for the given arguments, or for all
377: macros if no argument is given.
1.36 espie 378: .It Fn traceoff arg ...
1.22 espie 379: Disables tracing of macro expansions for the given arguments, or for all
380: macros if no argument is given.
1.36 espie 381: .It Fn translit string mapfrom mapto
1.1 deraadt 382: Transliterate the characters in the first argument from the set
1.10 aaron 383: given by the second argument to the set given by the third.
1.21 aaron 384: You cannot use
1.1 deraadt 385: .Xr tr 1
386: style abbreviations.
1.36 espie 387: .It Fn undefine name1 ...
1.24 espie 388: Removes the definition for the macros specified by its arguments.
1.36 espie 389: .It Fn undivert arg ...
1.1 deraadt 390: Flushes the named output queues (or all queues if no arguments).
1.5 aaron 391: .It Ic unix
1.1 deraadt 392: A pre-defined macro for testing the OS platform.
1.11 espie 393: .It Ic __line__
394: Returns the current file's line number.
395: .It Ic __file__
396: Returns the current file's name.
1.1 deraadt 397: .El
1.32 jmc 398: .Sh STANDARDS
1.23 espie 399: .Nm
1.39 espie 400: follows the Single Unix 3 specification, along with a few extensions taken
1.32 jmc 401: from
1.23 espie 402: .Nm gnu-m4 .
403: .Pp
404: Flags
405: .Fl I ,
406: .Fl d ,
407: .Fl t
408: are non-standard.
409: .Pp
1.32 jmc 410: The output format of tracing and of
411: .Ic dumpdef
412: are not specified in any standard,
1.23 espie 413: are likely to change and should not be relied upon.
1.32 jmc 414: The current format of tracing is closely modelled on
415: .Nm gnu-m4 ,
1.23 espie 416: to allow
1.32 jmc 417: .Nm autoconf
1.23 espie 418: to work.
1.34 espie 419: .Pp
420: The built-ins
421: .Ic pushdef
422: and
423: .Ic popdef
424: handle macro definitions as a stack.
425: However,
426: .Ic define
427: interacts with the stack in an undefined way.
428: In this implementation,
429: .Ic define
430: replaces the top-most definition only.
431: Other implementations may erase all definitions on the stack instead.
1.23 espie 432: .Pp
433: For portability, one should not use the macros
434: .Ic builtin ,
435: .Ic esycmd ,
436: .Ic expr ,
437: .Ic indir ,
438: .Ic paste ,
439: .Ic patsubst ,
440: .Ic regexp ,
441: .Ic spaste ,
442: .Ic unix ,
443: .Ic __line__ ,
444: .Ic __file__ .
445: .Pp
1.32 jmc 446: All built-ins do expand without arguments in many other
1.23 espie 447: .Nm m4 .
448: .Pp
449: Many other
450: .Nm
451: have dire size limitations with respect to buffer sizes.
1.21 aaron 452: .Sh AUTHORS
1.32 jmc 453: .An Ozan Yigit Aq oz@sis.yorku.ca
454: and
455: .An Richard A. O'Keefe Aq ok@goanna.cs.rmit.OZ.AU .
456: .Pp
457: GNU-m4 compatibility extensions by
458: .An Marc Espie Aq espie@cvs.openbsd.org .