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