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