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