Annotation of src/usr.bin/m4/m4.1, Revision 1.34
1.34 ! espie 1: .\" @(#) $OpenBSD: m4.1,v 1.33 2003/06/27 21:22:52 jmc 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.10 aaron 284: Translates the string
1.15 aaron 285: .Dq XXXXX
1.10 aaron 286: in the first argument with the current process
287: ID leaving other characters alone.
288: This can be used to create unique
1.1 deraadt 289: temporary file names.
1.5 aaron 290: .It Ic paste
1.1 deraadt 291: Includes the contents of the file specified by the first argument without
1.10 aaron 292: any macro processing.
293: Aborts with an error message if the file cannot be
1.1 deraadt 294: included.
1.11 espie 295: .It Ic patsubst
296: Substitutes a regular expression in a string with a replacement string.
297: Usual substitution patterns apply: an ampersand
1.33 jmc 298: .Pq Sq \&&
1.11 espie 299: is replaced by the string matching the regular expression.
300: The string
1.33 jmc 301: .Sq \e# ,
1.11 espie 302: where
1.33 jmc 303: .Sq #
1.11 espie 304: is a digit, is replaced by the corresponding back-reference.
1.5 aaron 305: .It Ic popdef
306: Restores the
307: .Ic pushdef Ns ed
308: definition for each argument.
309: .It Ic pushdef
310: Takes the same arguments as
311: .Ic define ,
312: but it saves the definition on a
313: stack for later retrieval by
314: .Ic popdef .
1.11 espie 315: .It Ic regexp
1.13 aaron 316: Finds a regular expression in a string.
317: If no further arguments are given,
1.14 aaron 318: it returns the first match position or \-1 if no match.
319: If a third argument
1.11 espie 320: is provided, it returns the replacement string, with sub-patterns replaced.
1.5 aaron 321: .It Ic shift
1.1 deraadt 322: Returns all but the first argument, the remaining arguments are
1.10 aaron 323: quoted and pushed back with commas in between.
324: The quoting
1.1 deraadt 325: nullifies the effect of the extra scan that will subsequently be
326: performed.
1.5 aaron 327: .It Ic sinclude
328: Similar to
329: .Ic include ,
330: except it ignores any errors.
331: .It Ic spaste
332: Similar to
333: .Ic paste ,
334: except it ignores any errors.
335: .It Ic substr
1.1 deraadt 336: Returns a substring of the first argument starting at the offset specified
337: by the second argument and the length specified by the third argument.
338: If no third argument is present it returns the rest of the string.
1.5 aaron 339: .It Ic syscmd
1.10 aaron 340: Passes the first argument to the shell.
341: Nothing is returned.
1.5 aaron 342: .It Ic sysval
343: Returns the return value from the last
344: .Ic syscmd .
1.22 espie 345: .It Ic traceon
346: Enables tracing of macro expansions for the given arguments, or for all
347: macros if no argument is given.
348: .It Ic traceoff
349: Disables tracing of macro expansions for the given arguments, or for all
350: macros if no argument is given.
1.5 aaron 351: .It Ic translit
1.1 deraadt 352: Transliterate the characters in the first argument from the set
1.10 aaron 353: given by the second argument to the set given by the third.
1.21 aaron 354: You cannot use
1.1 deraadt 355: .Xr tr 1
356: style abbreviations.
1.5 aaron 357: .It Ic undefine
1.24 espie 358: Removes the definition for the macros specified by its arguments.
1.5 aaron 359: .It Ic undivert
1.1 deraadt 360: Flushes the named output queues (or all queues if no arguments).
1.5 aaron 361: .It Ic unix
1.1 deraadt 362: A pre-defined macro for testing the OS platform.
1.11 espie 363: .It Ic __line__
364: Returns the current file's line number.
365: .It Ic __file__
366: Returns the current file's name.
1.1 deraadt 367: .El
1.32 jmc 368: .Sh STANDARDS
1.23 espie 369: .Nm
370: follows the Single Unix 2 specification, along with a few extensions taken
1.32 jmc 371: from
1.23 espie 372: .Nm gnu-m4 .
373: .Pp
374: The
375: .Fl s
376: option
377: .Po
378: .Xr cpp 1 's
379: #line directives
380: .Pc
381: is currently not supported.
382: Flags
383: .Fl I ,
384: .Fl d ,
385: .Fl t
386: are non-standard.
387: .Pp
1.32 jmc 388: The output format of tracing and of
389: .Ic dumpdef
390: are not specified in any standard,
1.23 espie 391: are likely to change and should not be relied upon.
1.32 jmc 392: The current format of tracing is closely modelled on
393: .Nm gnu-m4 ,
1.23 espie 394: to allow
1.32 jmc 395: .Nm autoconf
1.23 espie 396: to work.
1.34 ! espie 397: .Pp
! 398: The built-ins
! 399: .Ic pushdef
! 400: and
! 401: .Ic popdef
! 402: handle macro definitions as a stack.
! 403: However,
! 404: .Ic define
! 405: interacts with the stack in an undefined way.
! 406: In this implementation,
! 407: .Ic define
! 408: replaces the top-most definition only.
! 409: Other implementations may erase all definitions on the stack instead.
1.23 espie 410: .Pp
411: For portability, one should not use the macros
412: .Ic builtin ,
413: .Ic esycmd ,
414: .Ic expr ,
415: .Ic indir ,
416: .Ic paste ,
417: .Ic patsubst ,
418: .Ic regexp ,
419: .Ic spaste ,
420: .Ic unix ,
421: .Ic __line__ ,
422: .Ic __file__ .
423: .Pp
1.32 jmc 424: All built-ins do expand without arguments in many other
1.23 espie 425: .Nm m4 .
426: .Pp
427: Many other
428: .Nm
429: have dire size limitations with respect to buffer sizes.
1.21 aaron 430: .Sh AUTHORS
1.32 jmc 431: .An Ozan Yigit Aq oz@sis.yorku.ca
432: and
433: .An Richard A. O'Keefe Aq ok@goanna.cs.rmit.OZ.AU .
434: .Pp
435: GNU-m4 compatibility extensions by
436: .An Marc Espie Aq espie@cvs.openbsd.org .