Annotation of src/usr.bin/m4/m4.1, Revision 1.35
1.35 ! miod 1: .\" @(#) $OpenBSD: m4.1,v 1.34 2003/06/29 11:04:03 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.15 aaron 138: In this mode, changequote with
1.12 espie 139: two empty parameters deactivates quotes, translit handles simple character
1.19 espie 140: ranges (e.g., a-z), regular expressions mimic emacs behavior,
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.1 deraadt 151: .Bl -tag -width changequotexxx
1.11 espie 152: .It Ic builtin
153: Calls a built-in by its name, overriding possible redefinitions.
1.5 aaron 154: .It Ic changecom
1.10 aaron 155: Change the start and end comment sequences.
156: The default is the pound sign
1.33 jmc 157: .Pq Sq #
1.10 aaron 158: and the newline character.
159: With no arguments comments are turned off.
160: The maximum length for a comment marker is five characters.
1.5 aaron 161: .It Ic changequote
1.1 deraadt 162: Defines the quote symbols to be the first and second arguments.
1.10 aaron 163: The symbols may be up to five characters long.
164: If no arguments are
1.1 deraadt 165: given it restores the default open and close single quotes.
1.5 aaron 166: .It Ic decr
1.10 aaron 167: Decrements the argument by 1.
168: The argument must be a valid numeric string.
1.5 aaron 169: .It Ic define
1.1 deraadt 170: Define a new macro named by the first argument to have the
1.10 aaron 171: value of the second argument.
172: Each occurrence of
1.33 jmc 173: .Sq $n
1.10 aaron 174: (where
175: .Ar n
176: is 0 through 9) is replaced by the
177: .Ar n Ns 'th
178: argument.
1.33 jmc 179: .Sq $0
1.10 aaron 180: is the name of the calling macro.
181: Undefined arguments are replaced by a null string.
1.33 jmc 182: .Sq $#
1.10 aaron 183: is replaced by the number of arguments;
1.33 jmc 184: .Sq $*
1.10 aaron 185: is replaced by all arguments comma separated;
1.33 jmc 186: .Sq $@
1.10 aaron 187: is the same as
1.33 jmc 188: .Sq $*
1.10 aaron 189: but all arguments are quoted against further expansion.
1.5 aaron 190: .It Ic defn
1.10 aaron 191: Returns the quoted definition for each argument.
192: This can be used to rename
1.1 deraadt 193: macro definitions (even for built-in macros).
1.5 aaron 194: .It Ic divert
1.1 deraadt 195: There are 10 output queues (numbered 0-9).
196: At the end of processing
197: .Nm m4
198: concatenates all the queues in numerical order to produce the
1.10 aaron 199: final output.
200: Initially the output queue is 0.
201: The divert
1.1 deraadt 202: macro allows you to select a new output queue (an invalid argument
203: passed to divert causes output to be discarded).
1.5 aaron 204: .It Ic divnum
1.1 deraadt 205: Returns the current output queue number.
1.5 aaron 206: .It Ic dnl
1.1 deraadt 207: Discard input characters up to and including the next newline.
1.5 aaron 208: .It Ic dumpdef
1.1 deraadt 209: Prints the names and definitions for the named items, or for everything
210: if no arguments are passed.
1.5 aaron 211: .It Ic errprint
1.1 deraadt 212: Prints the first argument on the standard error output stream.
1.18 espie 213: .It Ic esyscmd
1.26 pvalchev 214: Passes its first argument to a shell and returns the shell's standard output.
1.18 espie 215: Note that the shell shares its standard input and standard error with
1.33 jmc 216: .Nm m4 .
1.5 aaron 217: .It Ic eval
1.1 deraadt 218: Computes the first argument as an arithmetic expression using 32-bit
1.10 aaron 219: arithmetic.
220: Operators are the standard C ternary, arithmetic, logical,
221: shift, relational, bitwise, and parentheses operators.
222: You can specify
223: octal, decimal, and hexadecimal numbers as in C.
224: The second argument (if any)
225: specifies the radix for the result and the third argument (if any)
226: specifies the minimum number of digits in the result.
1.5 aaron 227: .It Ic expr
228: This is an alias for
229: .Ic eval .
230: .It Ic ifdef
1.1 deraadt 231: If the macro named by the first argument is defined then return the second
1.10 aaron 232: argument, otherwise the third.
233: If there is no third argument, the value is
234: .Dv NULL .
235: The word
236: .Qq unix
237: is predefined.
1.5 aaron 238: .It Ic ifelse
239: If the first argument matches the second argument then
240: .Ic ifelse
241: returns
1.10 aaron 242: the third argument.
243: If the match fails the three arguments are
1.1 deraadt 244: discarded and the next three arguments are used until there is
1.10 aaron 245: zero or one arguments left, either this last argument or
246: .Dv NULL
247: is returned if no other matches were found.
1.5 aaron 248: .It Ic include
1.1 deraadt 249: Returns the contents of the file specified in the first argument.
1.7 espie 250: If the file is not found as is, look through the include path:
251: first the directories specified with
252: .Fl I
253: on the command line, then the environment variable
1.10 aaron 254: .Ev M4PATH ,
1.7 espie 255: as a colon-separated list of directories.
1.1 deraadt 256: Include aborts with an error message if the file cannot be included.
1.5 aaron 257: .It Ic incr
1.10 aaron 258: Increments the argument by 1.
259: The argument must be a valid numeric string.
1.5 aaron 260: .It Ic index
1.1 deraadt 261: Returns the index of the second argument in the first argument (e.g.,
1.10 aaron 262: .Ic index(the quick brown fox jumped, fox)
263: returns 16).
264: If the second
265: argument is not found index returns \-1.
1.11 espie 266: .It Ic indir
1.26 pvalchev 267: Indirectly calls the macro whose name is passed as the first argument,
1.11 espie 268: with the remaining arguments passed as first, ... arguments.
1.5 aaron 269: .It Ic len
1.10 aaron 270: Returns the number of characters in the first argument.
271: Extra arguments
1.1 deraadt 272: are ignored.
1.5 aaron 273: .It Ic m4exit
1.1 deraadt 274: Immediately exits with the return value specified by the first argument,
275: 0 if none.
1.5 aaron 276: .It Ic m4wrap
1.10 aaron 277: Allows you to define what happens at the final
278: .Dv EOF ,
279: usually for cleanup purposes (e.g.,
280: .Ic m4wrap("cleanup(tempfile)")
281: causes the macro cleanup to be
1.16 aaron 282: invoked after all other processing is done).
1.5 aaron 283: .It Ic maketemp
1.35 ! miod 284: Invokes
! 285: .Xr mkstemp 3
! 286: on the first argument, and returns the modified string.
1.10 aaron 287: This can be used to create unique
1.1 deraadt 288: temporary file names.
1.5 aaron 289: .It Ic paste
1.1 deraadt 290: Includes the contents of the file specified by the first argument without
1.10 aaron 291: any macro processing.
292: Aborts with an error message if the file cannot be
1.1 deraadt 293: included.
1.11 espie 294: .It Ic patsubst
295: Substitutes a regular expression in a string with a replacement string.
296: Usual substitution patterns apply: an ampersand
1.33 jmc 297: .Pq Sq \&&
1.11 espie 298: is replaced by the string matching the regular expression.
299: The string
1.33 jmc 300: .Sq \e# ,
1.11 espie 301: where
1.33 jmc 302: .Sq #
1.11 espie 303: is a digit, is replaced by the corresponding back-reference.
1.5 aaron 304: .It Ic popdef
305: Restores the
306: .Ic pushdef Ns ed
307: definition for each argument.
308: .It Ic pushdef
309: Takes the same arguments as
310: .Ic define ,
311: but it saves the definition on a
312: stack for later retrieval by
313: .Ic popdef .
1.11 espie 314: .It Ic regexp
1.13 aaron 315: Finds a regular expression in a string.
316: If no further arguments are given,
1.14 aaron 317: it returns the first match position or \-1 if no match.
318: If a third argument
1.11 espie 319: is provided, it returns the replacement string, with sub-patterns replaced.
1.5 aaron 320: .It Ic shift
1.1 deraadt 321: Returns all but the first argument, the remaining arguments are
1.10 aaron 322: quoted and pushed back with commas in between.
323: The quoting
1.1 deraadt 324: nullifies the effect of the extra scan that will subsequently be
325: performed.
1.5 aaron 326: .It Ic sinclude
327: Similar to
328: .Ic include ,
329: except it ignores any errors.
330: .It Ic spaste
331: Similar to
332: .Ic paste ,
333: except it ignores any errors.
334: .It Ic substr
1.1 deraadt 335: Returns a substring of the first argument starting at the offset specified
336: by the second argument and the length specified by the third argument.
337: If no third argument is present it returns the rest of the string.
1.5 aaron 338: .It Ic syscmd
1.10 aaron 339: Passes the first argument to the shell.
340: Nothing is returned.
1.5 aaron 341: .It Ic sysval
342: Returns the return value from the last
343: .Ic syscmd .
1.22 espie 344: .It Ic traceon
345: Enables tracing of macro expansions for the given arguments, or for all
346: macros if no argument is given.
347: .It Ic traceoff
348: Disables tracing of macro expansions for the given arguments, or for all
349: macros if no argument is given.
1.5 aaron 350: .It Ic translit
1.1 deraadt 351: Transliterate the characters in the first argument from the set
1.10 aaron 352: given by the second argument to the set given by the third.
1.21 aaron 353: You cannot use
1.1 deraadt 354: .Xr tr 1
355: style abbreviations.
1.5 aaron 356: .It Ic undefine
1.24 espie 357: Removes the definition for the macros specified by its arguments.
1.5 aaron 358: .It Ic undivert
1.1 deraadt 359: Flushes the named output queues (or all queues if no arguments).
1.5 aaron 360: .It Ic unix
1.1 deraadt 361: A pre-defined macro for testing the OS platform.
1.11 espie 362: .It Ic __line__
363: Returns the current file's line number.
364: .It Ic __file__
365: Returns the current file's name.
1.1 deraadt 366: .El
1.32 jmc 367: .Sh STANDARDS
1.23 espie 368: .Nm
369: follows the Single Unix 2 specification, along with a few extensions taken
1.32 jmc 370: from
1.23 espie 371: .Nm gnu-m4 .
372: .Pp
373: The
374: .Fl s
375: option
376: .Po
377: .Xr cpp 1 's
378: #line directives
379: .Pc
380: is currently not supported.
381: Flags
382: .Fl I ,
383: .Fl d ,
384: .Fl t
385: are non-standard.
386: .Pp
1.32 jmc 387: The output format of tracing and of
388: .Ic dumpdef
389: are not specified in any standard,
1.23 espie 390: are likely to change and should not be relied upon.
1.32 jmc 391: The current format of tracing is closely modelled on
392: .Nm gnu-m4 ,
1.23 espie 393: to allow
1.32 jmc 394: .Nm autoconf
1.23 espie 395: to work.
1.34 espie 396: .Pp
397: The built-ins
398: .Ic pushdef
399: and
400: .Ic popdef
401: handle macro definitions as a stack.
402: However,
403: .Ic define
404: interacts with the stack in an undefined way.
405: In this implementation,
406: .Ic define
407: replaces the top-most definition only.
408: Other implementations may erase all definitions on the stack instead.
1.23 espie 409: .Pp
410: For portability, one should not use the macros
411: .Ic builtin ,
412: .Ic esycmd ,
413: .Ic expr ,
414: .Ic indir ,
415: .Ic paste ,
416: .Ic patsubst ,
417: .Ic regexp ,
418: .Ic spaste ,
419: .Ic unix ,
420: .Ic __line__ ,
421: .Ic __file__ .
422: .Pp
1.32 jmc 423: All built-ins do expand without arguments in many other
1.23 espie 424: .Nm m4 .
425: .Pp
426: Many other
427: .Nm
428: have dire size limitations with respect to buffer sizes.
1.21 aaron 429: .Sh AUTHORS
1.32 jmc 430: .An Ozan Yigit Aq oz@sis.yorku.ca
431: and
432: .An Richard A. O'Keefe Aq ok@goanna.cs.rmit.OZ.AU .
433: .Pp
434: GNU-m4 compatibility extensions by
435: .An Marc Espie Aq espie@cvs.openbsd.org .