Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / m4 / m4.1
1 .\"     @(#) $OpenBSD: m4.1,v 1.24 2002/04/18 18:57:23 espie Exp $
2 .\" $FreeBSD: src/usr.bin/m4/m4.1,v 1.10.2.9 2003/04/26 14:29:55 schweikh Exp $
3 .\"
4 .Dd April 17, 2002
5 .Dt M4 1
6 .Os
7 .Sh NAME
8 .Nm m4
9 .Nd macro language processor
10 .Sh SYNOPSIS
11 .Nm
12 .Op Fl d Ar flags
13 .Op Fl t Ar name
14 .Op Fl gs
15 .Op Fl D Ar name Ns Op = Ns Ar value
16 .Op Fl U Ar name
17 .Op Fl I Ar dirname
18 .Op Ar
19 .Sh DESCRIPTION
20 The
21 .Nm
22 utility is a macro processor that can be used as a front end to any
23 language (e.g., C, ratfor, fortran, lex, and yacc).
24 .Nm
25 reads from the standard input and writes
26 the processed text to the standard output.
27 .Pp
28 Macro calls have the form
29 .Ic name Ns Pq Ar argument1 Ns Op , Ar argument2 , ... , argumentN .
30 .Pp
31 There cannot be any space following the macro name and the open
32 parenthesis
33 .Pq Ql \&( .
34 If the macro name is not followed by an open
35 parenthesis it is processed with no arguments.
36 .Pp
37 Macro names consist of a leading alphabetic or underscore
38 possibly followed by alphanumeric or underscore characters, e.g.,
39 valid macro names match the pattern
40 .Dq Li [a-zA-Z_][a-zA-Z0-9_]* .
41 .Pp
42 In arguments to macros, leading unquoted space, tab, and newline
43 .Pq Ql \en
44 characters are ignored.
45 To quote strings, use left and right single
46 quotes (e.g.,
47 .Sq "\ this is a string with a leading space" ) .
48 You can change the quote characters with the
49 .Ic changequote
50 built-in macro.
51 .Pp
52 Most built-ins do not make any sense without arguments, and hence are not
53 recognized as special when not followed by an open parenthesis.
54 .Pp
55 The options are as follows:
56 .Bl -tag -width indent
57 .It Fl s
58 Emit
59 .Ic #line
60 directives for
61 .Xr cpp 1 .
62 .It Fl D Ar name Ns Op = Ns Ar value
63 Define the symbol
64 .Ar name
65 to have some value (or
66 .Dv NULL ) .
67 .It Fl U Ar name
68 Undefine the symbol
69 .Ar name .
70 .It Fl I Ar dirname
71 Add directory
72 .Ar dirname
73 to the include path.
74 .It Fl d Ar flags
75 Set trace flags.
76 The
77 .Ar flags
78 argument may hold the following:
79 .Pp
80 .Bl -tag -width indent -compact
81 .It Cm a
82 print macro arguments
83 .It Cm c
84 print macro expansion over several lines
85 .It Cm e
86 print result of macro expansion
87 .It Cm f
88 print filename location
89 .It Cm l
90 print line number
91 .It Cm q
92 quote arguments and expansion with the current quotes
93 .It Cm t
94 start with all macros traced
95 .It Cm x
96 number macro expansions
97 .It Cm V
98 turn on all options
99 .El
100 .Pp
101 By default, trace is set to
102 .Cm eq .
103 .It Fl t Ar macro
104 Turn tracing on for
105 .Ar macro .
106 .It Fl g
107 Activate GNU-m4 compatibility mode.
108 In this mode,
109 .Ic changequote
110 with two empty parameters deactivates quotes,
111 .Ic translit
112 handles simple character ranges (e.g.,
113 .Li a-z ) ,
114 regular expressions mimic
115 .Xr emacs 1
116 behavior,
117 and the number of diversions is unlimited.
118 .El
119 .Sh SYNTAX
120 The
121 .Nm
122 utility provides the following built-in macros.
123 They may be redefined, losing their original meaning.
124 Return values are null unless otherwise stated.
125 .Bl -tag -width ".Ic changequote"
126 .It Ic builtin
127 Calls a built-in by its name, overriding possible redefinitions.
128 .It Ic changecom
129 Changes the start and end comment sequences.
130 The default is the pound sign
131 .Pq Ql #
132 and the newline character.
133 With no arguments, the comment sequence is reset to the default,
134 in GNU
135 .Nm
136 mode, comments are turned off.
137 The maximum length for a comment marker is five characters.
138 .It Ic changequote
139 Defines the quote symbols to be the first and second arguments.
140 The symbols may be up to five characters long.
141 If no arguments are
142 given it restores the default open and close single quotes.
143 .It Ic decr
144 Decrements the argument by 1.
145 The argument must be a valid numeric string.
146 .It Ic define
147 Define a new macro named by the first argument to have the
148 value of the second argument.
149 Each occurrence of
150 .Sq Li $ Ns Ar n
151 (where
152 .Ar n
153 is 0 through 9) is replaced by the
154 .Ar n Ns 'th
155 argument.
156 .Ql $0
157 is the name of the calling macro.
158 Undefined arguments are replaced by a null string.
159 .Ql $#
160 is replaced by the number of arguments;
161 .Ql $*
162 is replaced by all arguments comma separated;
163 .Ql $@
164 is the same as
165 .Ql $*
166 but all arguments are quoted against further expansion.
167 .It Ic defn
168 Returns the quoted definition for each argument.
169 This can be used to rename
170 macro definitions (even for built-in macros).
171 .It Ic divert
172 There are 10 output queues (numbered 0-9).
173 At the end of processing
174 .Nm
175 concatenates all the queues in numerical order to produce the
176 final output.
177 Initially the output queue is 0.
178 The
179 .Ic divert
180 macro allows you to select a new output queue (an invalid argument
181 passed to
182 .Ic divert
183 causes output to be discarded).
184 .It Ic divnum
185 Returns the current output queue number.
186 .It Ic dnl
187 Discards input characters up to and including the next newline.
188 .It Ic dumpdef
189 Prints the names and definitions for the named items, or for everything
190 if no arguments are passed.
191 .It Ic errprint
192 Prints the first argument on the standard error output stream.
193 .It Ic esyscmd
194 Passes its first argument to a shell and returns the shell's standard output.
195 Note that the shell shares its standard input and standard error with
196 .Nm .
197 .It Ic eval
198 Computes the first argument as an arithmetic expression using 32-bit
199 arithmetic.
200 Operators are the standard C ternary, arithmetic, logical,
201 shift, relational, bitwise, and parentheses operators.
202 You can specify
203 octal, decimal, and hexadecimal numbers as in C.
204 The second argument (if any)
205 specifies the radix for the result, and the third argument (if any)
206 specifies the minimum number of digits in the result.
207 .It Ic expr
208 This is an alias for
209 .Ic eval .
210 .It Ic ifdef
211 If the macro named by the first argument is defined then return the second
212 argument, otherwise the third.
213 If there is no third argument, the value is
214 .Dv NULL .
215 The word
216 .Ic unix
217 is predefined.
218 .It Ic ifelse
219 If the first argument matches the second argument then
220 .Ic ifelse
221 returns
222 the third argument.
223 If the match fails, the three arguments are
224 discarded and the next three arguments are used until there is
225 zero or one arguments left, either this last argument or
226 .Dv NULL
227 is returned if no other matches were found.
228 .It Ic include
229 Returns the contents of the file specified in the first argument.
230 If the file is not found as is, look through the include path:
231 first the directories specified with
232 .Fl I
233 on the command line, then the environment variable
234 .Ev M4PATH ,
235 as a colon-separated list of directories.
236 Aborts with an error message if the file cannot be included.
237 .It Ic incr
238 Increments the argument by 1.
239 The argument must be a valid numeric string.
240 .It Ic index
241 Returns the index of the second argument in the first argument (e.g.,
242 .Fn index "the quick brown fox jumped" fox
243 returns 16).
244 If the second
245 argument is not found,
246 .Ic index
247 returns \-1.
248 .It Ic indir
249 Indirectly calls the macro whose name is passed as the first arguments,
250 with the remaining arguments passed as first, etc. arguments.
251 .It Ic len
252 Returns the number of characters in the first argument.
253 Extra arguments
254 are ignored.
255 .It Ic m4exit
256 Immediately exits with the return value specified by the first argument,
257 0 if none.
258 .It Ic m4wrap
259 Allows you to define what happens at the final
260 .Dv EOF ,
261 usually for cleanup purposes (e.g.,
262 .Fn m4wrap cleanup(tempfile)
263 causes the macro
264 .Ic cleanup
265 to be
266 invoked after all other processing is done).
267 .It Ic maketemp
268 Translates the string
269 .Dq Li XXXXX
270 in the first argument with the current process
271 ID leaving other characters alone.
272 This can be used to create unique
273 temporary file names.
274 .It Ic paste
275 Includes the contents of the file specified by the first argument without
276 any macro processing.
277 Aborts with an error message if the file cannot be
278 included.
279 .It Ic patsubst
280 Substitutes a regular expression in a string with a replacement string.
281 Usual substitution patterns apply: an ampersand
282 .Pq Ql &
283 is replaced by the string matching the regular expression.
284 The string
285 .Sq \e Ns Ar # ,
286 where
287 .Ar #
288 is a digit, is replaced by the corresponding back-reference.
289 .It Ic popdef
290 Restores the
291 .Ic pushdef Ns ed
292 definition for each argument.
293 .It Ic pushdef
294 Takes the same arguments as
295 .Ic define ,
296 but it saves the definition on a
297 stack for later retrieval by
298 .Ic popdef .
299 .It Ic regexp
300 Finds a regular expression in a string.
301 If no further arguments are given,
302 it returns the first match position or \-1 if no match.
303 If a third argument
304 is provided, it returns the replacement string, with sub-patterns replaced.
305 .It Ic shift
306 Returns all but the first argument, the remaining arguments are
307 quoted and pushed back with commas in between.
308 The quoting
309 nullifies the effect of the extra scan that will subsequently be
310 performed.
311 .It Ic sinclude
312 Similar to
313 .Ic include ,
314 except it ignores any errors.
315 .It Ic spaste
316 Similar to
317 .Ic paste ,
318 except it ignores any errors.
319 .It Ic substr
320 Returns a substring of the first argument starting at the offset specified
321 by the second argument and the length specified by the third argument.
322 If no third argument is present it returns the rest of the string.
323 .It Ic syscmd
324 Passes the first argument to the shell.
325 Nothing is returned.
326 .It Ic sysval
327 Returns the return value from the last
328 .Ic syscmd .
329 .It Ic traceon
330 Enables tracing of macro expansions for the given arguments, or for all
331 macros if no argument is given.
332 .It Ic traceoff
333 Disables tracing of macro expansions for the given arguments, or for all
334 macros if no argument is given.
335 .It Ic translit
336 Transliterate the characters in the first argument from the set
337 given by the second argument to the set given by the third.
338 You cannot use
339 .Xr tr 1
340 style abbreviations.
341 .It Ic undefine
342 Removes the definition for the macros specified by its arguments.
343 .It Ic undivert
344 Flushes the named output queues (or all queues if no arguments).
345 .It Ic unix
346 A pre-defined macro for testing the OS platform.
347 .It Ic __line__
348 Returns the current file's line number.
349 .It Ic __file__
350 Returns the current file's name.
351 .El
352 .Sh DIAGNOSTICS
353 .Ex -std
354 .Pp
355 The
356 .Ic m4exit
357 macro may be used to change the exit status from the input file.
358 .Sh COMPATIBILITY
359 .Nm
360 follows the Single Unix 2 specification, along with a few extensions taken
361 from GNU-m4.
362 Flags
363 .Fl I , d ,
364 and
365 .Fl t
366 are non-standard.
367 .Pp
368 The output format of tracing and of
369 .Ic dumpdef
370 are not specified in any standard,
371 are likely to change and should not be relied upon.
372 The current format of tracing is closely modeled on GNU-m4,
373 to allow
374 .Nm autoconf
375 to work.
376 .Pp
377 For portability, one should not use the macros
378 .Ic builtin ,
379 .Ic esyscmd ,
380 .Ic expr ,
381 .Ic indir ,
382 .Ic paste ,
383 .Ic patsubst ,
384 .Ic regexp ,
385 .Ic spaste ,
386 .Ic unix ,
387 .Ic __line__ ,
388 and
389 .Ic __file__ .
390 .Pp
391 All built-ins do expand without arguments in many other
392 .Nm
393 implementations.
394 .Pp
395 Many other
396 .Nm
397 implementations have dire size limitations with respect to buffer sizes.
398 .Sh STANDARDS
399 The
400 .Nm
401 utility
402 conforms to
403 .St -p1003.1-2001 .
404 .Sh HISTORY
405 An
406 .Nm
407 command appeared in PWB UNIX.
408 .Sh AUTHORS
409 .An -nosplit
410 .An Ozan Yigit Aq oz@sis.yorku.ca
411 and
412 .An Richard A. O'Keefe Aq ok@goanna.cs.rmit.OZ.AU .
413 GNU-m4 compatibility extensions by
414 .An Marc Espie Aq espie@cvs.openbsd.org .