* doc/m4.texinfo (Invoking m4, Debug Output): Fix wording to be
[m4/ericb.git] / doc / m4.texinfo
blobca2fb01f80c10f3d07577f5dc08f7b6cd876f218
1 \input texinfo @c -*- texinfo -*-
2 @comment ========================================================
3 @comment %**start of header
4 @setfilename m4.info
5 @settitle GNU M4 macro processor
6 @setchapternewpage odd
7 @ifnothtml
8 @setcontentsaftertitlepage
9 @end ifnothtml
10 @finalout
12 @include version.texi
14 @c @tabchar{}
15 @c ----------
16 @c The testsuite expects literal tab output in some examples, but
17 @c literal tabs in texinfo lead to formatting issues.
18 @macro tabchar
19 @       @c
20 @end macro
22 @c @ovar{ARG}
23 @c -------------------
24 @c The ARG is an optional argument.  To be used for macro arguments in
25 @c their documentation.
26 @macro ovar{varname}
27 @r{[}@var{\varname\}@r{]}
28 @end macro
30 @c @dvar{ARG, DEFAULT}
31 @c -------------------
32 @c The ARG is an optional argument, defaulting to DEFAULT.  To be used
33 @c for macro arguments in their documentation.
34 @macro dvar{varname, default}
35 @r{[}@var{\varname\} = @samp{\default\}@r{]}
36 @end macro
38 @comment %**end of header
39 @comment ========================================================
41 @copying
43 This manual is for @acronym{GNU} M4 (version @value{VERSION}, @value{UPDATED}),
44 a package containing an implementation of the m4 macro language.
46 Copyright @copyright{} 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005,
47 2006 Free Software Foundation, Inc.
49 @quotation
50 Permission is granted to copy, distribute and/or modify this document
51 under the terms of the @acronym{GNU} Free Documentation License,
52 Version 1.2 or any later version published by the Free Software
53 Foundation; with no Invariant Sections, no Front-Cover Texts, and no
54 Back-Cover Texts.  A copy of the license is included in the section
55 entitled ``@acronym{GNU} Free Documentation License.''
56 @end quotation
57 @end copying
59 @dircategory GNU programming tools
60 @direntry
61 * M4: (m4).                     A powerful macro processor.
62 @end direntry
64 @titlepage
65 @title GNU M4, version @value{VERSION}
66 @subtitle A powerful macro processor
67 @subtitle Edition @value{EDITION}, @value{UPDATED}
68 @author by Ren@'e Seindal
70 @page
71 @vskip 0pt plus 1filll
72 @insertcopying
73 @end titlepage
75 @contents
77 @ifnottex
78 @node Top
79 @top GNU M4
80 @insertcopying
81 @end ifnottex
83 @acronym{GNU} @code{m4} is an implementation of the traditional UNIX macro
84 processor.  It is mostly SVR4 compatible, although it has some
85 extensions (for example, handling more than 9 positional parameters
86 to macros).  @code{m4} also has builtin functions for including
87 files, running shell commands, doing arithmetic, etc.  Autoconf needs
88 @acronym{GNU} @code{m4} for generating @file{configure} scripts, but not for
89 running them.
91 @acronym{GNU} @code{m4} was originally written by Ren@'e Seindal, with
92 subsequent changes by Fran@,{c}ois Pinard and other volunteers
93 on the Internet.  All names and email addresses can be found in the
94 files @file{AUTHORS} and @file{THANKS} from the @acronym{GNU} M4 distribution.
96 This is release @value{VERSION}.  It is now considered stable:  future
97 releases in the 1.4.x series are only meant to fix bugs, increase speed,
98 or improve documentation.  However@dots{}
100 An experimental feature, which would improve @code{m4} usefulness,
101 allows for changing the syntax for what is a @dfn{word} in @code{m4}.
102 You should use:
103 @comment ignore
104 @example
105 ./configure --enable-changeword
106 @end example
107 @noindent
108 if you want this feature compiled in.  The current implementation
109 slows down @code{m4} considerably and is hardly acceptable.  In the
110 future, @code{m4} 2.0 will come with a different set of new features
111 that provide similar capabilities, but without the inefficiencies, so
112 changeword will go away and @emph{you should not count on it}.
114 @menu
115 * Preliminaries::               Introduction and preliminaries
116 * Syntax::                      Lexical and syntactic conventions
118 * Macros::                      How to invoke macros
119 * Definitions::                 How to define new macros
120 * Conditionals::                Conditionals, loops, and recursion
122 * Debugging::                   How to debug macros and input
124 * Input Control::               Input control
125 * File Inclusion::              File inclusion
126 * Diversions::                  Diverting and undiverting output
128 * Text handling::               Macros for text handling
129 * Arithmetic::                  Macros for doing arithmetic
130 * Shell commands::              Macros for running shell commands
131 * Miscellaneous::               Miscellaneous builtin macros
132 * Frozen files::                Fast loading of frozen state
134 * Compatibility::               Compatibility with other versions of m4
135 * Answers::                     Correct version of some examples
136 * Copying This Manual::         How to make copies of this manual
137 * Indices::                     Indices of concepts and macros
139 @detailmenu
140  --- The Detailed Node Listing ---
142 Introduction and preliminaries
144 * Intro::                       Introduction to @code{m4}
145 * History::                     Historical references
146 * Invoking m4::                 Invoking @code{m4}
147 * Bugs::                        Problems and bugs
148 * Manual::                      Using this manual
150 Lexical and syntactic conventions
152 * Names::                       Macro names
153 * Quoted strings::              Quoting input to m4
154 * Other tokens::                Other kinds of input tokens
155 * Comments::                    Comments in m4 input
156 * Input processing::            How m4 copies input to output
158 How to invoke macros
160 * Invocation::                  Macro invocation
161 * Inhibiting Invocation::       Preventing macro invocation
162 * Macro Arguments::             Macro arguments
163 * Quoting Arguments::           On Quoting Arguments to macros
164 * Macro expansion::             Expanding macros
166 How to define new macros
168 * Define::                      Defining a new macro
169 * Arguments::                   Arguments to macros
170 * Pseudo Arguments::            Pseudo arguments to macros
171 * Undefine::                    Deleting a macro
172 * Defn::                        Renaming macros
173 * Pushdef::                     Temporarily redefining macros
175 * Indir::                       Indirect call of macros
176 * Builtin::                     Indirect call of builtins
178 Conditionals, loops, and recursion
180 * Ifdef::                       Testing if a macro is defined
181 * Ifelse::                      If-else construct, or multibranch
182 * Loops::                       Loops and recursion in m4
184 How to debug macros and input
186 * Dumpdef::                     Displaying macro definitions
187 * Trace::                       Tracing macro calls
188 * Debug Levels::                Controlling debugging output
189 * Debug Output::                Saving debugging output
191 Input control
193 * Dnl::                         Deleting whitespace in input
194 * Changequote::                 Changing the quote characters
195 * Changecom::                   Changing the comment delimiters
196 * Changeword::                  Changing the lexical structure of words
197 * M4wrap::                      Saving input until end of input
199 File inclusion
201 * Include::                     Including named files
202 * Search Path::                 Searching for include files
204 Diverting and undiverting output
206 * Divert::                      Diverting output
207 * Undivert::                    Undiverting output
208 * Divnum::                      Diversion numbers
209 * Cleardiv::                    Discarding diverted text
211 Macros for text handling
213 * Len::                         Calculating length of strings
214 * Index macro::                 Searching for substrings
215 * Regexp::                      Searching for regular expressions
216 * Substr::                      Extracting substrings
217 * Translit::                    Translating characters
218 * Patsubst::                    Substituting text by regular expression
219 * Format::                      Formatting strings (printf-like)
221 Macros for doing arithmetic
223 * Incr::                        Decrement and increment operators
224 * Eval::                        Evaluating integer expressions
226 Running shell commands
228 * Platform macros::             Determining the platform
229 * Syscmd::                      Executing simple commands
230 * Esyscmd::                     Reading the output of commands
231 * Sysval::                      Exit status
232 * Maketemp::                    Making names for temporary files
234 Miscellaneous builtin macros
236 * Errprint::                    Printing error messages
237 * Location::                    Printing current location
238 * M4exit::                      Exiting from m4
240 Fast loading of frozen state
242 * Using frozen files::          Using frozen files
243 * Frozen file format::          Frozen file format
245 Compatibility with other versions of @code{m4}
247 * Extensions::                  Extensions in @acronym{GNU} M4
248 * Incompatibilities::           Facilities in System V m4 not in GNU M4
249 * Other Incompatibilities::     Other incompatibilities
251 Copying This Manual
253 * GNU Free Documentation License::  License for copying this manual
255 Indices
257 * Concept index::               Index for many concepts
258 * Macro index::                 Index for all m4 macros
260 @end detailmenu
261 @end menu
263 @node Preliminaries
264 @chapter Introduction and preliminaries
266 This first chapter explains what @acronym{GNU} @code{m4} is, where @code{m4}
267 comes from, how to read and use this documentation, how to call the
268 @code{m4} program, and how to report bugs about it.  It concludes by
269 giving tips for reading the remainder of the manual.
271 The following chapters then detail all the features of the @code{m4}
272 language.
274 @menu
275 * Intro::                       Introduction to @code{m4}
276 * History::                     Historical references
277 * Invoking m4::                 Invoking @code{m4}
278 * Bugs::                        Problems and bugs
279 * Manual::                      Using this manual
280 @end menu
282 @node Intro
283 @section Introduction to @code{m4}
285 @code{m4} is a macro processor, in the sense that it copies its
286 input to the output, expanding macros as it goes.  Macros are either
287 builtin or user-defined, and can take any number of arguments.
288 Besides just doing macro expansion, @code{m4} has builtin functions
289 for including named files, running shell commands, doing integer
290 arithmetic, manipulating text in various ways, performing recursion,
291 etc.@dots{}  @code{m4} can be used either as a front-end to a compiler,
292 or as a macro processor in its own right.
294 The @code{m4} macro processor is widely available on all UNIXes, and has
295 been standardized by @acronym{POSIX}.
296 Usually, only a small percentage of users are aware of its existence.
297 However, those who find it often become committed users.  The
298 popularity of @acronym{GNU} Autoconf, which requires @acronym{GNU}
299 @code{m4} for @emph{generating} @file{configure} scripts, is an incentive
300 for many to install it, while these people will not themselves
301 program in @code{m4}.  @acronym{GNU} @code{m4} is mostly compatible with the
302 System V, Release 3 version, except for some minor differences.
303 @xref{Compatibility}, for more details.
305 Some people find @code{m4} to be fairly addictive.  They first use
306 @code{m4} for simple problems, then take bigger and bigger challenges,
307 learning how to write complex sets of @code{m4} macros along the way.
308 Once really addicted, users pursue writing of sophisticated @code{m4}
309 applications even to solve simple problems, devoting more time
310 debugging their @code{m4} scripts than doing real work.  Beware that
311 @code{m4} may be dangerous for the health of compulsive programmers.
313 @node History
314 @section Historical references
316 @code{GPM} was an important ancestor of @code{m4}.  See
317 C. Stratchey: ``A General Purpose Macro generator'', Computer Journal
318 8,3 (1965), pp. 225 ff.  @code{GPM} is also succinctly described into
319 David Gries classic ``Compiler Construction for Digital Computers''.
321 The classic B. Kernighan and P.J. Plauger: ``Software Tools'',
322 Addison-Wesley, Inc. (1976) describes and implements a Unix
323 macro-processor language, which inspired Dennis Ritchie to write
324 @code{m3}, a macro processor for the AP-3 minicomputer.
326 Kernighan and Ritchie then joined forces to develop the original
327 @code{m4}, as described in ``The M4 Macro Processor'', Bell
328 Laboratories (1977).  It had only 21 builtin macros.
330 While @code{GPM} was more @emph{pure}, @code{m4} is meant to deal with
331 the true intricacies of real life: macros can be recognized without
332 being pre-announced, skipping whitespace or end-of-lines is easier,
333 more constructs are builtin instead of derived, etc.
335 Originally, the Kernighan and Plauger macro-processor, and then
336 @code{m3}, formed the engine for the Rational FORTRAN preprocessor,
337 that is, the @code{Ratfor} equivalent of @code{cpp}.  Later, @code{m4}
338 was used as a frontend for @code{Ratfor}, @code{C} and @code{Cobol}.
340 Ren@'e Seindal released his implementation of @code{m4}, @acronym{GNU}
341 @code{m4},
342 in 1990, with the aim of removing the artificial limitations in many
343 of the traditional @code{m4} implementations, such as maximum line
344 length, macro size, or number of macros.
346 The late Professor A. Dain Samples described and implemented a further
347 evolution in the form of @code{M5}: ``User's Guide to the M5 Macro
348 Language: 2nd edition'', Electronic Announcement on comp.compilers
349 newsgroup (1992).
351 Fran@,{c}ois Pinard took over maintenance of @acronym{GNU} @code{m4} in
352 1992, until 1994 when he released @acronym{GNU} @code{m4} 1.4, which was
353 the stable release for 10 years.  It was at this time that @acronym{GNU}
354 Autoconf decided to require @acronym{GNU} @code{m4} as its underlying
355 engine, since all other implementations of @code{m4} had too many
356 limitations.
358 More recently, in 2004, Paul Eggert released 1.4.1 and 1.4.2 which
359 addressed some long standing bugs in the venerable 1.4 release.
360 Then in 2005 Gary V. Vaughan collected together the many
361 patches to @acronym{GNU} @code{m4} 1.4 that were floating around the net and
362 released 1.4.3 and 1.4.4.  And in 2006, Eric Blake joined the team and
363 prepared patches for the release of 1.4.5 and 1.4.6.
365 Meanwhile, development has continued on new features for @code{m4}, such
366 as dynamic module loading and additional builtins.  When complete,
367 @acronym{GNU} @code{m4} 2.0 will start a new series of releases.
369 @node Invoking m4
370 @section Invoking @code{m4}
372 The format of the @code{m4} command is:
374 @comment ignore
375 @example
376 @code{m4} @r{[}@var{option}@dots{}@r{]} @r{[}@var{file}@dots{}@r{]}
377 @end example
379 @cindex command line, options
380 @cindex options, command line
381 All options begin with @samp{-}, or if long option names are used, with
382 a @samp{--}.  A long option name need not be written completely, any
383 unambiguous prefix is sufficient.  Options may be intermixed with files,
384 use @option{--} as a marker to denote the end of options.  @code{m4}
385 understands the following options, grouped by functionality.
387 Several options control the overall operation of @code{m4}:
389 @table @code
390 @item --help
391 Print a help summary on standard output, then immediately exit
392 @code{m4} without reading any input files.
394 @item --version
395 Print the version number of the program on standard output, then
396 immediately exit @code{m4} without reading any input files.
398 @item -E
399 @itemx --fatal-warnings
400 Stop execution and exit @code{m4} once the first warning has been
401 issued, considering all of them to be fatal.
403 @item -e
404 @itemx --interactive
405 Makes this invocation of @code{m4} interactive.  This means that all
406 output will be unbuffered, and interrupts will be ignored.
408 @item -P
409 @itemx --prefix-builtins
410 Internally modify @emph{all} builtin macro names so they all start with
411 the prefix @samp{m4_}.  For example, using this option, one should write
412 @samp{m4_define} instead of @samp{define}, and @samp{m4___file__}
413 instead of @samp{__file__}.  This option has no effect if @option{-R}
414 is also specified.
416 @item -Q
417 @itemx --quiet
418 @itemx --silent
419 Suppress warnings, such as missing or superfluous arguments in macro
420 calls, or treating the empty string as zero.
422 @item -W @var{REGEXP}
423 @itemx --word-regexp=@var{REGEXP}
424 Use @var{REGEXP} as an alternative syntax for macro names.  This
425 experimental option will not be present on all @acronym{GNU} @code{m4}
426 implementations (@pxref{Changeword}).
427 @end table
429 @cindex macro definitions, on the command line
430 @cindex command line, macro definitions on the
431 Several options allow @code{m4} to behave more like a preprocessor.
432 Macro definitions and deletions can be made on the command line, the
433 search path can be altered, and the output file can track where the
434 input came from.  These features occur with the following options:
436 @table @code
437 @item -D @var{NAME}@r{[}=@var{VALUE}@r{]}
438 @itemx --define=@var{NAME}@r{[}=@var{VALUE}@r{]}
439 This enters @var{NAME} into the symbol table, before any input files are
440 read.  If @samp{=@var{VALUE}} is missing, the value is taken to be the
441 empty string.  The @var{VALUE} can be any string, and the macro can be
442 defined to take arguments, just as if it was defined from within the
443 input.  This option may be given more than once; order is significant,
444 and redefining the same @var{NAME} loses the previous value.
446 @item -I @var{DIRECTORY}
447 @itemx --include=@var{DIRECTORY}
448 Make @code{m4} search @var{DIRECTORY} for included files that are not
449 found in the current working directory.  @xref{Search Path}, for more
450 details.  This option may be given more than once.
452 @item -s
453 @itemx --synclines
454 Generate synchronization lines, for use by the C preprocessor or other
455 similar tools.  This is useful, for example, when @code{m4} is used as a
456 front end to a compiler.  Source file name and line number information
457 is conveyed by directives of the form @samp{#line @var{linenum}
458 "@var{file}"}, which are inserted as needed into the middle of the
459 output.  Such directives mean that the following line originated or was
460 expanded from the contents of input file @var{file} at line
461 @var{linenum}.  The @samp{"@var{file}"} part is often omitted when
462 the file name did not change from the previous directive.
464 Synchronization directives are always given on complete lines by
465 themselves.  When a synchronization discrepancy occurs in the middle of
466 an output line, the associated synchronization directive is delayed
467 until the beginning of the next generated line.
469 @item -U @var{NAME}
470 @itemx --undefine=@var{NAME}
471 This deletes any predefined meaning @var{NAME} might have.  Obviously,
472 only predefined macros can be deleted in this way.  This option may be
473 given more than once; undefining a @var{NAME} that does not have a
474 definition is silently ignored.
475 @end table
477 There are some limits within @code{m4} that can be tuned.  For
478 compatibility, @code{m4} also accepts some options that control limits
479 in other implementations, but which are automatically unbounded (limited
480 only by your hardware constraints) in @acronym{GNU} @code{m4}.
482 @table @code
483 @item -G
484 @itemx --traditional
485 Suppress all the extensions made in this implementation, compared to the
486 System V version.  @xref{Compatibility}, for a list of these.
488 @item -H @var{NUM}
489 @itemx --hashsize=@var{NUM}
490 Make the internal hash table for symbol lookup be @var{NUM} entries big.
491 For better performance, the number should be prime, but this is not
492 checked.  The default is 509 entries.  It should not be necessary to
493 increase this value, unless you define an excessive number of macros.
495 @item -L @var{NUM}
496 @itemx --nesting-limit=@var{NUM}
497 Artificially limit the nesting of macro calls to @var{NUM} levels,
498 stopping program execution if this limit is ever exceeded.  When not
499 specified, nesting is limited to 1024 levels.
501 The precise effect of this option might be more correctly associated
502 with textual nesting than dynamic recursion.  It has been useful
503 when some complex @code{m4} input was generated by mechanical means.
504 Most users would never need this option.  If shown to be obtrusive,
505 this option (which is still experimental) might well disappear.
507 This option does @emph{not} have the ability to break endless
508 rescanning loops, since these do not necessarily consume much memory
509 or stack space.  Through clever usage of rescanning loops, one can
510 request complex, time-consuming computations from @code{m4} with useful
511 results.  Putting limitations in this area would break @code{m4} power.
512 There are many pathological cases: @w{@samp{define(`a', `a')a}} is
513 only the simplest example (but @pxref{Compatibility}).  Expecting @acronym{GNU}
514 @code{m4} to detect these would be a little like expecting a compiler
515 system to detect and diagnose endless loops: it is a quite @emph{hard}
516 problem in general, if not undecidable!
518 @item -B @var{NUM}
519 @itemx -S @var{NUM}
520 @itemx -T @var{NUM}
521 These options are present for compatibility with System V @code{m4}, but
522 do nothing in this implementation.
524 @item -N @var{NUM}
525 @itemx --diversions=@var{NUM}
526 These options are present only for compatibility with previous
527 versions of @acronym{GNU} @code{m4}, and were controlling the number of
528 possible diversions which could be used at the same time.  They do nothing,
529 because there is no fixed limit anymore.
530 @end table
532 @acronym{GNU} @code{m4} comes with a feature of freezing internal state
533 (@pxref{Frozen files}).  This can be used to speed up @code{m4}
534 execution when reusing a common initialization script.
536 @table @code
537 @item -F @var{FILE}
538 @itemx --freeze-state=@var{FILE}
539 Once execution is finished, write out the frozen state on the specified
540 @var{FILE}.  It is conventional, but not required, for @var{FILE} to end
541 in @samp{.m4f}.
543 @item -R @var{FILE}
544 @itemx --reload-state=@var{FILE}
545 Before execution starts, recover the internal state from the specified
546 frozen @var{FILE}.  The options @option{-D}, @option{-U}, and
547 @option{-t} take effect after state is reloaded, but before the input
548 files are read.
549 @end table
551 Finally, there are several options for aiding in debugging @code{m4}
552 scripts.
554 @table @code
555 @item -d@r{[}@var{FLAGS}@r{]}
556 @itemx --debug@r{[}=@var{FLAGS}@r{]}
557 Set the debug-level according to the flags @var{FLAGS}.  The debug-level
558 controls the format and amount of information presented by the debugging
559 functions.  @xref{Debug Levels}, for more details on the format and
560 meaning of @var{FLAGS}.  If omitted, @var{FLAGS} defaults to @samp{aeq}.
562 @item -l @var{NUM}
563 @itemx --arglength=@var{NUM}
564 Restrict the size of the output generated by macro tracing to @var{NUM}
565 characters per trace line.  If unspecified or zero, output is
566 unlimited.  @xref{Debug Levels}, for more details.
568 @item -o @var{FILE}
569 @itemx --error-output=@var{FILE}
570 Redirect @code{dumpdef} output, debug messages, and trace output to the
571 named @var{FILE}.  Warnings, error messages, and @code{errprint} output
572 are still printed to standard error.  If unspecified, debug output goes
573 to standard error; if empty, debug output is discarded.  @xref{Debug
574 Output}, for more details.
576 @item -t @var{NAME}
577 @itemx --trace=@var{NAME}
578 This enables tracing for the macro @var{NAME}, at any point where it is
579 defined.  @var{NAME} need not be defined when this option is given.
580 This option may be given more than once.  @xref{Trace}, for more details.
581 @end table
583 @cindex command line, file names on the
584 @cindex file names, on the command line
585 The remaining arguments on the command line are taken to be input file
586 names.  If no names are present, the standard input is read.  A file
587 name of @file{-} is taken to mean the standard input.  It is
588 conventional, but not required, for input files to end in @samp{.m4}.
590 The input files are read in the sequence given.  The standard input can
591 only be read once, so the file name @file{-} should only appear once on
592 the command line.  It is an error if an input file ends in the middle of
593 argument collection, a comment, or a quoted string.
595 If none of the input files invoked @code{m4exit} (@pxref{M4exit}), the
596 exit status of @code{m4} will be 0 for success, 1 for general failure
597 (such as problems with reading an input file), and 63 for version
598 mismatch (@pxref{Using frozen files}).
600 If you need to read a file whose name starts with a @file{-}, you can
601 specify it as @samp{./-file}, or use @option{--} to mark the end of
602 options.
604 @node Bugs
605 @section Problems and bugs
607 If you have problems with @acronym{GNU} @code{m4} or think you've found a bug,
608 please report it.  Before reporting a bug, make sure you've actually
609 found a real bug.  Carefully reread the documentation and see if it
610 really says you can do what you're trying to do.  If it's not clear
611 whether you should be able to do something or not, report that too; it's
612 a bug in the documentation!
614 Before reporting a bug or trying to fix it yourself, try to isolate it
615 to the smallest possible input file that reproduces the problem.  Then
616 send us the input file and the exact results @code{m4} gave you.  Also
617 say what you expected to occur; this will help us decide whether the
618 problem was really in the documentation.
620 Once you've got a precise problem, send e-mail to (Internet)
621 @email{bug-m4@@gnu.org}.  Please include the version number of @code{m4}
622 you are using.  You can get this information with the command @samp{m4
623 --version}.  Also provide details about the platform you are executing
626 Non-bug suggestions are always welcome as well.  If you have questions
627 about things that are unclear in the documentation or are just obscure
628 features, please report them too.
630 @node Manual
631 @section Using this manual
633 This manual contains a number of examples of @code{m4} input and output,
634 and a simple notation is used to distinguish input, output and error
635 messages from @code{m4}.  Examples are set out from the normal text, and
636 shown in a fixed width font, like this
638 @comment ignore
639 @example
640 This is an example of an example!
641 @end example
643 To distinguish input from output, all output from @code{m4} is prefixed
644 by the string @samp{@result{}}, and all error messages by the string
645 @samp{@error{}}.  Thus
647 @comment ignore
648 @example
649 Example of input line
650 @result{}Output line from m4
651 @error{}and an error message
652 @end example
654 The sequence @samp{^D} in an example indicates the end of the input file.
655 The majority of these examples are self-contained, and you can run them
656 with similar results by invoking @kbd{m4 -d}.  In fact, the testsuite
657 that is bundled in the @acronym{GNU} M4 package consists of the examples
658 in this document!
660 As each of the predefined macros in @code{m4} is described, a prototype
661 call of the macro will be shown, giving descriptive names to the
662 arguments, e.g.,
664 @deffn Composite example (@var{string}, @dvar{count, 1}, @
665   @ovar{argument}@dots{})
666 This is a sample prototype.  There is not really a macro named
667 @code{example}, but this documents that if there were, it would be a
668 Composite macro, rather than a Builtin.  It requires at least one
669 argument, @var{string}.  Remember that in @code{m4}, there must not be a
670 space between the macro name and the opening parenthesis, unless it was
671 intended to call the macro without any arguments.  The brackets around
672 @var{count} and @var{argument} show that these arguments are optional.
673 If @var{count} is omitted, the macro behaves as if count were @samp{1},
674 whereas if @var{argument} is omitted, the macro behaves as if it were
675 the empty string.  A blank argument is not the same as an omitted
676 argument.  For example, @samp{example(`a')}, @samp{example(`a',`1')},
677 and @samp{example(`a',`1',)} would behave identically with @var{count}
678 set to @samp{1}; while @samp{example(`a',)} and @samp{example(`a',`')}
679 would explicitly pass the empty string for @var{count}.  The ellipses
680 (@samp{@dots{}}) show that the macro processes additional arguments
681 after @var{argument}, rather than ignoring them.
682 @end deffn
684 All macro arguments in @code{m4} are strings, but some are given
685 special interpretation, e.g., as numbers, file names, regular
686 expressions, etc.  The documentation for each macro will state how the
687 parameters are interpreted, and what happens if the argument cannot be
688 parsed according to the desired interpretation.  Unless specified
689 otherwise, a parameter specified to be a number is parsed as a decimal,
690 even if the argument has leading zeros; and parsing the empty string as
691 a number results in 0 rather than an error, although a warning will be
692 issued.
694 This document consistently writes and uses @dfn{builtin}, without a
695 hyphen, as if it were an English word.  This is how the @code{builtin}
696 primitive is spelled within @code{m4}.
698 @node Syntax
699 @chapter Lexical and syntactic conventions
701 @cindex input tokens
702 @cindex tokens
703 As @code{m4} reads its input, it separates it into @dfn{tokens}.  A
704 token is either a name, a quoted string, or any single character, that
705 is not a part of either a name or a string.  Input to @code{m4} can also
706 contain comments.  @acronym{GNU} @code{m4} does not yet understand
707 locales; all operations are byte-oriented rather than
708 character-oriented.  However, @code{m4} is eight-bit clean, so you can
709 use non-ASCII characters in quoted strings (@pxref{Changequote}),
710 comments (@pxref{Changecom}), and macro names (@pxref{Indir}), with the
711 exception of the NUL character (the zero byte).
713 @menu
714 * Names::                       Macro names
715 * Quoted strings::              Quoting input to m4
716 * Other tokens::                Other kinds of input tokens
717 * Comments::                    Comments in m4 input
718 * Input processing::            How m4 copies input to output
719 @end menu
721 @node Names
722 @section Names
724 @cindex names
725 A name is any sequence of letters, digits, and the character @kbd{_}
726 (underscore), where the first character is not a digit.  If a
727 name has a macro definition, it will be subject to macro expansion
728 (@pxref{Macros}).
730 Examples of legal names are: @samp{foo}, @samp{_tmp}, and @samp{name01}.
732 @node Quoted strings
733 @section Quoted strings
735 @cindex quoted string
736 A quoted string is a sequence of characters surrounded by the quotes
737 @kbd{`} and @kbd{'}, where the nested begin and end quotes within the
738 string are balanced.  The value of a string token is the text, with one
739 level of quotes stripped off.  Thus
741 @comment ignore
742 @example
744 @result{}
745 @end example
747 @noindent
748 is the empty string, and double-quoting turns into single-quoting.
750 @comment ignore
751 @example
752 ``quoted''
753 @result{}`quoted'
754 @end example
756 The quote characters can be changed at any time, using the builtin macro
757 @code{changequote}.  @xref{Changequote}, for more information.
759 @node Other tokens
760 @section Other tokens
762 Any character, that is neither a part of a name, nor of a quoted string,
763 is a token by itself.
765 @node Comments
766 @section Comments
768 @cindex comments
769 Comments in @code{m4} are normally delimited by the characters @samp{#}
770 and newline.  All characters between the comment delimiters are ignored,
771 but the entire comment (including the delimiters) is passed through to
772 the output---comments are @emph{not} discarded by @code{m4}.
774 Comments cannot be nested, so the first newline after a @samp{#} ends
775 the comment.  The commenting effect of the begin-comment string
776 can be inhibited by quoting it.
778 @example
779 `quoted text' # `commented text'
780 @result{}quoted text # `commented text'
781 `quoting inhibits' `#' `comments'
782 @result{}quoting inhibits # comments
783 @end example
785 The comment delimiters can be changed to any string at any time, using
786 the builtin macro @code{changecom}.  @xref{Changecom}, for more
787 information.
789 @node Input processing
790 @section Input Processing
792 As @code{m4} reads the input token by token, it will copy each token
793 directly to the output immediately.
795 The exception is when it finds a word with a macro definition.  In that
796 case @code{m4} will calculate the macro's expansion, possibly reading
797 more input to get the arguments.  It then inserts the expansion in front
798 of the remaining input.  In other words, the resulting text from a macro
799 call will be read and parsed into tokens again.
801 @code{m4} expands a macro as soon as possible.  If it finds a macro call
802 when collecting the arguments to another, it will expand the second
803 call first.  If the input is
805 @comment ignore
806 @example
807 format(`Result is %d', eval(`2**15'))
808 @end example
810 @noindent
811 @code{m4} will first expand @samp{eval(2**15)} to @samp{32768}, and only
812 then expand the resulting call
814 @comment ignore
815 @example
816 format(`Result is %d', 32768)
817 @end example
819 @noindent
820 which will give the output
822 @comment ignore
823 @example
824 Result is 32768
825 @end example
827 The order in which @code{m4} expands the macros can be explored using
828 the @ref{Trace} facilities of @acronym{GNU} @code{m4}.
830 This process continues until there are no more macro calls to expand and
831 all the input has been consumed.
833 @node Macros
834 @chapter How to invoke macros
836 This chapter covers macro invocation, macro arguments and how macro
837 expansion is treated.
839 @menu
840 * Invocation::                  Macro invocation
841 * Inhibiting Invocation::       Preventing macro invocation
842 * Macro Arguments::             Macro arguments
843 * Quoting Arguments::           On Quoting Arguments to macros
844 * Macro expansion::             Expanding macros
845 @end menu
847 @node Invocation
848 @section Macro invocation
850 @cindex macro invocation
851 Macro invocations has one of the forms
853 @comment ignore
854 @example
855 name
856 @end example
858 @noindent
859 which is a macro invocation without any arguments, or
861 @comment ignore
862 @example
863 name(arg1, arg2, @dots{}, arg@var{n})
864 @end example
866 @noindent
867 which is a macro invocation with @var{n} arguments.  Macros can have any
868 number of arguments.  All arguments are strings, but different macros
869 might interpret the arguments in different ways.
871 The opening parenthesis @emph{must} follow the @var{name} directly, with
872 no spaces in between.  If it does not, the macro is called with no
873 arguments at all.
875 For a macro call to have no arguments, the parentheses @emph{must} be
876 left out.  The macro call
878 @comment ignore
879 @example
880 name()
881 @end example
883 @noindent
884 is a macro call with one argument, which is the empty string, not a call
885 with no arguments.
887 @node Inhibiting Invocation
888 @section Preventing macro invocation
890 An innovation of the @code{m4} language, compared to some of its
891 predecessors (like Stratchey's @code{GPM}, for example), is the ability
892 to recognize macro calls without resorting to any special, prefixed
893 invocation character.  While generally useful, this feature might
894 sometimes be the source of spurious, unwanted macro calls.  So, @acronym{GNU}
895 @code{m4} offers several mechanisms or techniques for inhibiting the
896 recognition of names as macro calls.
898 First of all, many builtin macros cannot meaningfully be called
899 without arguments.  For any of these macros, whenever an opening
900 parenthesis does not immediately follow their name, the builtin macro
901 call is not triggered.  This solves the most usual cases, like for
902 @samp{include} or @samp{eval}.  Later in this document, the sentence
903 ``This macro is recognized only with parameters'' refers to this
904 specific provision.
906 There is also a command line option (@option{--prefix-builtins}, or
907 @option{-P}, @pxref{Invoking m4}) which requires all builtin macro names
908 to be prefixed
909 by @samp{m4_} for them to be recognized.  The option has no effect
910 whatsoever on user defined macros.  For example, with this option,
911 one has to write @code{m4_dnl} and even @code{m4_m4exit}.
913 If your version of @acronym{GNU} @code{m4} has the @code{changeword} feature
914 compiled in, it offers far more flexibility in specifying the
915 syntax of macro names, both builtin or user-defined.  @xref{Changeword},
916 for more information on this experimental feature.
918 Of course, the simplest way to prevent a name from being interpreted
919 as a call to an existing macro is to quote it.  The remainder of
920 this section studies a little more deeply how quoting affects macro
921 invocation, and how quoting can be used to inhibit macro invocation.
923 Even if quoting is usually done over the whole macro name, it can also
924 be done over only a few characters of this name (provided, of course,
925 that the unquoted portions are not also a macro).  It is also possible
926 to quote the empty string, but this works only @emph{inside} the name.
927 For example:
929 @comment ignore
930 @example
931 `divert'
932 @result{}divert
933 `d'ivert
934 @result{}divert
935 di`ver't
936 @result{}divert
937 div`'ert
938 @result{}divert
939 @end example
941 @noindent
942 all yield the string @samp{divert}.  While in both:
944 @comment ignore
945 @example
946 `'divert
947 @result{}
948 divert`'
949 @result{}
950 @end example
952 @noindent
953 the @code{divert} builtin macro will be called, which expands to the
954 empty string.
956 The output of macro evaluations is always rescanned.  The following
957 example would yield the string @samp{de}, exactly as if @code{m4}
958 has been given @w{@samp{substr(`abcde', `3', `2')}} as input:
960 @example
961 define(`x', `substr(ab')
962 @result{}
963 define(`y', `cde, `3', `2')')
964 @result{}
965 x`'y
966 @result{}de
967 @end example
969 Unquoted strings on either side of a quoted string are subject to
970 being recognized as macro names.  In the following example, quoting the
971 empty string allows for the second @code{macro} to be recognized as such:
973 @example
974 define(`macro', `m')
975 @result{}
976 macro(`m')macro
977 @result{}mmacro
978 macro(`m')`'macro
979 @result{}mm
980 @end example
982 Quoting may prevent recognizing as a macro name the concatenation of a
983 macro expansion with the surrounding characters.  In this example:
985 @example
986 define(`macro', `di$1')
987 @result{}
988 macro(`v')`ert'
989 @result{}divert
990 macro(`v')ert
991 @result{}
992 @end example
994 @noindent
995 the input will produce the string @samp{divert}.  When the quotes were
996 removed, the @code{divert} builtin was called instead.
998 @node Macro Arguments
999 @section Macro arguments
1001 @cindex macros, arguments to
1002 @cindex arguments to macros
1003 When a name is seen, and it has a macro definition, it will be expanded
1004 as a macro.
1006 If the name is followed by an opening parenthesis, the arguments will be
1007 collected before the macro is called.  If too few arguments are
1008 supplied, the missing arguments are taken to be the empty string.
1009 However, some builtins are documented to behave differently for a
1010 missing optional argument than for an explicit empty string.  If
1011 there are too many arguments, the excess arguments are ignored.
1013 Normally @code{m4} will issue warnings if a builtin macro is called
1014 with an inappropriate number of arguments, but it can be suppressed with
1015 the @option{-Q} command line option (@pxref{Invoking m4}).  For user
1016 defined macros, there is no check of the number of arguments given.
1018 Macros are expanded normally during argument collection, and whatever
1019 commas, quotes and parentheses that might show up in the resulting
1020 expanded text will serve to define the arguments as well.  Thus, if
1021 @var{foo} expands to @samp{, b, c}, the macro call
1023 @comment ignore
1024 @example
1025 bar(a foo, d)
1026 @end example
1028 @noindent
1029 is a macro call with four arguments, which are @samp{a }, @samp{b},
1030 @samp{c} and @samp{d}.  To understand why the first argument contains
1031 whitespace, remember that leading unquoted whitespace is never part
1032 of an argument, but trailing whitespace always is.
1034 It is possible for a macro's definition to change during argument
1035 collection, in which case the expansion uses the definition that was in
1036 effect at the time the opening @samp{(} was seen.
1038 @example
1039 define(`f', `1')
1040 @result{}
1041 f(define(`f', `2'))
1042 @result{}1
1044 @result{}2
1045 @end example
1047 It is an error if the end of file occurs while collecting arguments.
1049 @example
1050 define(
1052 @error{}m4:stdin:1: ERROR: end of file in argument list
1053 @end example
1055 @node Quoting Arguments
1056 @section Quoting macro arguments
1058 @cindex quoted macro arguments
1059 @cindex macros, quoted arguments to
1060 @cindex arguments, quoted macro
1061 Each argument has leading unquoted whitespace removed.  Within each
1062 argument, all unquoted parentheses must match.  For example, if
1063 @var{foo} is a macro,
1065 @comment ignore
1066 @example
1067 foo(() (`(') `(')
1068 @end example
1070 @noindent
1071 is a macro call, with one argument, whose value is @samp{() (() (}.
1072 Commas separate arguments, except when they occur inside quotes,
1073 comments, or unquoted parentheses, @xref{Pseudo Arguments}, for
1074 examples.
1076 It is common practice to quote all arguments to macros, unless you are
1077 sure you want the arguments expanded.  Thus, in the above
1078 example with the parentheses, the `right' way to do it is like this:
1080 @comment ignore
1081 @example
1082 foo(`() (() (')
1083 @end example
1085 It is, however, in certain cases necessary or convenient to leave out
1086 quotes for some arguments, and there is nothing wrong in doing it.  It
1087 just makes life a bit harder, if you are not careful.  For consistency,
1088 this manual follows the rule of thumb that each layer of parentheses
1089 introduces another layer of single quoting, except when showing the
1090 consequences of quoting rules.  This is done even when the quoted string
1091 cannot be a macro, such as with integers.
1093 @node Macro expansion
1094 @section Macro expansion
1096 @cindex macros, expansion of
1097 @cindex expansion of macros
1098 When the arguments, if any, to a macro call have been collected, the
1099 macro is expanded, and the expansion text is pushed back onto the input
1100 (unquoted), and reread.  The expansion text from one macro call might
1101 therefore result in more macros being called, if the calls are included,
1102 completely or partially, in the first macro calls' expansion.
1104 Taking a very simple example, if @var{foo} expands to @samp{bar}, and
1105 @var{bar} expands to @samp{Hello world}, the input
1107 @comment ignore
1108 @example
1110 @end example
1112 @noindent
1113 will expand first to @samp{bar}, and when this is reread and
1114 expanded, into @samp{Hello world}.
1116 @node Definitions
1117 @chapter How to define new macros
1119 @cindex macros, how to define new
1120 @cindex defining new macros
1121 Macros can be defined, redefined and deleted in several different ways.
1122 Also, it is possible to redefine a macro without losing a previous
1123 value, and bring back the original value at a later time.
1125 @menu
1126 * Define::                      Defining a new macro
1127 * Arguments::                   Arguments to macros
1128 * Pseudo Arguments::            Pseudo arguments to macros
1129 * Undefine::                    Deleting a macro
1130 * Defn::                        Renaming macros
1131 * Pushdef::                     Temporarily redefining macros
1133 * Indir::                       Indirect call of macros
1134 * Builtin::                     Indirect call of builtins
1135 @end menu
1137 @node Define
1138 @section Defining a macro
1140 The normal way to define or redefine macros is to use the builtin
1141 @code{define}:
1143 @deffn Builtin define (@var{name}, @ovar{expansion})
1144 Defines @var{name} to expand to @var{expansion}.  If
1145 @var{expansion} is not given, it is taken to be empty.
1147 The expansion of @code{define} is void.
1148 The macro @code{define} is recognized only with parameters.
1149 @end deffn
1151 The following example defines the macro @var{foo} to expand to the text
1152 @samp{Hello World.}.
1154 @example
1155 define(`foo', `Hello world.')
1156 @result{}
1158 @result{}Hello world.
1159 @end example
1161 The empty line in the output is there because the newline is not
1162 a part of the macro definition, and it is consequently copied to
1163 the output.  This can be avoided by use of the macro @code{dnl}.
1164 @xref{Dnl}, for details.
1166 The first argument to @code{define} should be quoted; otherwise, if the
1167 macro is already defined, you will be defining a different macro.  This
1168 example shows the problems with underquoting, since we did not want to
1169 redefine @code{one}:
1171 @example
1172 define(foo, one)
1173 @result{}
1174 define(foo, two)
1175 @result{}
1177 @result{}two
1178 @end example
1180 As a @acronym{GNU} extension, the first argument to @code{define} does
1181 not have to be a simple word.
1182 It can be any text string, even the empty string.  A macro with a
1183 non-standard name cannot be invoked in the normal way, as the name is
1184 not recognised.  It can only be referenced by the builtins @ref{Indir}
1185 and @ref{Defn}.
1187 @cindex arrays
1188 Arrays and associative arrays can be simulated by using this trick.
1190 @example
1191 define(`array', `defn(format(``array[%d]'', `$1'))')
1192 @result{}
1193 define(`array_set', `define(format(``array[%d]'', `$1'), `$2')')
1194 @result{}
1195 array_set(`4', `array element no. 4')
1196 @result{}
1197 array_set(`17', `array element no. 17')
1198 @result{}
1199 array(`4')
1200 @result{}array element no. 4
1201 array(eval(`10 + 7'))
1202 @result{}array element no. 17
1203 @end example
1205 Change the @code{%d} to @code{%s} and it is an associative array.
1207 @node Arguments
1208 @section Arguments to macros
1210 @cindex macros, arguments to
1211 @cindex Arguments to macros
1212 Macros can have arguments.  The @var{n}th argument is denoted by
1213 @code{$n} in the expansion text, and is replaced by the @var{n}th actual
1214 argument, when the macro is expanded.  Replacement of arguments happens
1215 before rescanning, regardless of how many nesting levels of quoting
1216 appear in the expansion.  Here is an example of a macro with
1217 two arguments.  It simply exchanges the order of the two arguments.
1219 @example
1220 define(`exch', `$2, $1')
1221 @result{}
1222 exch(`arg1', `arg2')
1223 @result{}arg2, arg1
1224 @end example
1226 This can be used, for example, if you like the arguments to
1227 @code{define} to be reversed.
1229 @example
1230 define(`exch', `$2, $1')
1231 @result{}
1232 define(exch(``expansion text'', ``macro''))
1233 @result{}
1234 macro
1235 @result{}expansion text
1236 @end example
1238 @xref{Quoting Arguments}, for an explanation of the double quotes.
1239 (You should try and improve this example so that clients of exch do not
1240 have to double quote.  @pxref{Answers})
1242 @cindex @acronym{GNU} extensions
1243 @acronym{GNU} @code{m4} allows the number following the @samp{$} to
1244 consist of one
1245 or more digits, allowing macros to have any number of arguments.  This
1246 is not so in UNIX implementations of @code{m4}, which only recognize
1247 one digit.
1249 As a special case, the zeroth argument, @code{$0}, is always the name
1250 of the macro being expanded.
1252 @example
1253 define(`test', ``Macro name: $0'')
1254 @result{}
1255 test
1256 @result{}Macro name: test
1257 @end example
1259 If you want quoted text to appear as part of the expansion text,
1260 remember that quotes can be nested in quoted strings.  Thus, in
1262 @example
1263 define(`foo', `This is macro `foo'.')
1264 @result{}
1266 @result{}This is macro foo.
1267 @end example
1269 @noindent
1270 The @samp{foo} in the expansion text is @emph{not} expanded, since it is
1271 a quoted string, and not a name.
1273 @node Pseudo Arguments
1274 @section Special arguments to macros
1276 @cindex special arguments to macros
1277 @cindex macros, special arguments to
1278 @cindex arguments to macros, special
1279 There is a special notation for the number of actual arguments supplied,
1280 and for all the actual arguments.
1282 The number of actual arguments in a macro call is denoted by @code{$#}
1283 in the expansion text.  Thus, a macro to display the number of arguments
1284 given can be
1286 @example
1287 define(`nargs', `$#')
1288 @result{}
1289 nargs
1290 @result{}0
1291 nargs()
1292 @result{}1
1293 nargs(`arg1', `arg2', `arg3')
1294 @result{}3
1295 nargs(`commas can be quoted, like this')
1296 @result{}1
1297 nargs(arg1#inside comments, commas do not separate arguments
1298 still arg1)
1299 @result{}1
1300 nargs((unquoted parentheses, like this, group arguments))
1301 @result{}1
1302 @end example
1304 The notation @code{$*} can be used in the expansion text to denote all
1305 the actual arguments, unquoted, with commas in between.  For example
1307 @example
1308 define(`echo', `$*')
1309 @result{}
1310 echo(arg1,    arg2, arg3 , arg4)
1311 @result{}arg1,arg2,arg3 ,arg4
1312 @end example
1314 Often each argument should be quoted, and the notation @code{$@@} handles
1315 that.  It is just like @code{$*}, except that it quotes each argument.
1316 A simple example of that is:
1318 @example
1319 define(`echo', `$@@')
1320 @result{}
1321 echo(arg1,    arg2, arg3 , arg4)
1322 @result{}arg1,arg2,arg3 ,arg4
1323 @end example
1325 Where did the quotes go?  Of course, they were eaten, when the expanded
1326 text were reread by @code{m4}.  To show the difference, try
1328 @example
1329 define(`echo1', `$*')
1330 @result{}
1331 define(`echo2', `$@@')
1332 @result{}
1333 define(`foo', `This is macro `foo'.')
1334 @result{}
1335 echo1(foo)
1336 @result{}This is macro This is macro foo..
1337 echo1(`foo')
1338 @result{}This is macro foo.
1339 echo2(foo)
1340 @result{}This is macro foo.
1341 echo2(`foo')
1342 @result{}foo
1343 @end example
1345 @noindent
1346 @xref{Trace}, if you do not understand this.  As another example of the
1347 difference, remember that comments encountered in arguments are passed
1348 untouched to the macro, and that quoting disables comments.
1350 @example
1351 define(`echo1', `$*')
1352 @result{}
1353 define(`echo2', `$@')
1354 @result{}
1355 define(`foo', `bar')
1356 @result{}
1357 echo1(#foo'foo
1358 foo)
1359 @result{}#foo'foo
1360 @result{}bar
1361 echo2(#foo'foo
1362 foo)
1363 @result{}#foobar
1364 @result{}bar'
1365 @end example
1367 A @samp{$} sign in the expansion text, that is not followed by anything
1368 @code{m4} understands, is simply copied to the macro expansion, as any
1369 other text is.
1371 @example
1372 define(`foo', `$$$ hello $$$')
1373 @result{}
1375 @result{}$$$ hello $$$
1376 @end example
1378 If you want a macro to expand to something like @samp{$12}, put a pair
1379 of quotes after the @code{$}.  This will prevent @code{m4} from
1380 interpreting the @code{$} sign as a reference to an argument.
1382 @node Undefine
1383 @section Deleting a macro
1385 @cindex macros, how to delete
1386 @cindex deleting macros
1387 @cindex undefining macros
1388 A macro definition can be removed with @code{undefine}:
1390 @deffn Builtin undefine (@var{name}@dots{})
1391 For each argument, remove the macro @var{name}.  The macro names must
1392 necessarily be quoted, since they will be expanded otherwise.
1394 The expansion of @code{undefine} is void.
1395 The macro @code{undefine} is recognized only with parameters.
1396 @end deffn
1398 @example
1399 foo bar blah
1400 @result{}foo bar blah
1401 define(`foo', `some')define(`bar', `other')define(`blah', `text')
1402 @result{}
1403 foo bar blah
1404 @result{}some other text
1405 undefine(`foo')
1406 @result{}
1407 foo bar blah
1408 @result{}foo other text
1409 undefine(`bar', `blah')
1410 @result{}
1411 foo bar blah
1412 @result{}foo bar blah
1413 @end example
1415 Undefining a macro inside that macro's expansion is safe; the macro
1416 still expands to the definition that was in effect at the @samp{(}.
1418 @example
1419 define(`f', ``$0':$1')
1420 @result{}
1421 f(f(f(undefine(`f')`hello world')))
1422 @result{}f:f:f:hello world
1423 f(`bye')
1424 @result{}f(bye)
1425 @end example
1427 It is not an error for @var{name} to have no macro definition.  In that
1428 case, @code{undefine} does nothing.
1430 @node Defn
1431 @section Renaming macros
1433 @cindex macros, how to rename
1434 @cindex renaming macros
1435 It is possible to rename an already defined macro.  To do this, you need
1436 the builtin @code{defn}:
1438 @deffn Builtin defn (@var{name})
1439 Expands to the @emph{quoted definition} of @var{name}.  If the
1440 argument is not a defined macro, the expansion is void.
1442 If @var{name} is a user-defined macro, the quoted definition is simply
1443 the quoted expansion text.  If, instead, @var{name} is a builtin, the
1444 expansion is a special token, which points to the builtin's internal
1445 definition.  This token is only meaningful as the second argument to
1446 @code{define} (and @code{pushdef}), and is ignored in any other context.
1448 The macro @code{defn} is recognized only with parameters.
1449 @end deffn
1451 Its normal use is best understood through an example, which shows how to
1452 rename @code{undefine} to @code{zap}:
1454 @example
1455 define(`zap', defn(`undefine'))
1456 @result{}
1457 zap(`undefine')
1458 @result{}
1459 undefine(`zap')
1460 @result{}undefine(zap)
1461 @end example
1463 In this way, @code{defn} can be used to copy macro definitions, and also
1464 definitions of builtin macros.  Even if the original macro is removed,
1465 the other name can still be used to access the definition.
1467 The fact that macro definitions can be transferred also explains why you
1468 should use @code{$0}, rather than retyping a macro's name in its
1469 definition:
1471 @example
1472 define(`foo', `This is `$0'')
1473 @result{}
1474 define(`bar', defn(`foo'))
1475 @result{}
1477 @result{}This is bar
1478 @end example
1480 Macros used as string variables should be referred through @code{defn},
1481 to avoid unwanted expansion of the text:
1483 @example
1484 define(`string', `The macro dnl is very useful
1486 @result{}
1487 string
1488 @result{}The macro @comment
1489 defn(`string')
1490 @result{}The macro dnl is very useful
1491 @result{}
1492 @end example
1494 However, it is important to remember that @code{m4} rescanning is purely
1495 textual.  If an unbalanced end-quote string occurs in a macro
1496 definition, the rescan will see that embedded quote as the termination
1497 of the quoted string, and the remainder of the macro's definition will
1498 be rescanned unquoted.  Thus it is a good idea to avoid unbalanced
1499 end-quotes in macro definitions or arguments to macros.
1501 @example
1502 define(`foo', a'a)
1503 @result{}
1504 define(`a', `A')
1505 @result{}
1506 define(`echo', `$@')
1507 @result{}
1509 @result{}A'A
1510 defn(`foo')
1511 @result{}aA'
1512 echo(foo)
1513 @result{}AA'
1514 @end example
1516 @node Pushdef
1517 @section Temporarily redefining macros
1519 @cindex macros, temporary redefinition of
1520 @cindex temporary redefinition of macros
1521 @cindex redefinition of macros, temporary
1522 It is possible to redefine a macro temporarily, reverting to the
1523 previous definition at a later time.  This is done with the builtins
1524 @code{pushdef} and @code{popdef}:
1526 @deffn Builtin pushdef (@var{name}, @ovar{expansion})
1527 @deffnx Builtin popdef (@var{name}@dots{})
1528 Analogous to @code{define} and @code{undefine}.
1530 These macros work in a stack-like fashion.  A macro is temporarily
1531 redefined with @code{pushdef}, which replaces an existing definition of
1532 @var{name}, while saving the previous definition, before the new one is
1533 installed.  If there is no previous definition, @code{pushdef} behaves
1534 exactly like @code{define}.
1536 If a macro has several definitions (of which only one is accessible),
1537 the topmost definition can be removed with @code{popdef}.  If there is
1538 no previous definition, @code{popdef} behaves like @code{undefine}.
1540 The expansion of both @code{pushdef} and @code{popdef} is void.
1541 The macros @code{pushdef} and @code{popdef} are recognized only with
1542 parameters.
1543 @end deffn
1545 @example
1546 define(`foo', `Expansion one.')
1547 @result{}
1549 @result{}Expansion one.
1550 pushdef(`foo', `Expansion two.')
1551 @result{}
1553 @result{}Expansion two.
1554 pushdef(`foo', `Expansion three.')
1555 @result{}
1556 pushdef(`foo', `Expansion four.')
1557 @result{}
1558 popdef(`foo')
1559 @result{}
1561 @result{}Expansion three.
1562 popdef(`foo', `foo')
1563 @result{}
1565 @result{}Expansion one.
1566 popdef(`foo')
1567 @result{}
1569 @result{}foo
1570 @end example
1572 If a macro with several definitions is redefined with @code{define}, the
1573 topmost definition is @emph{replaced} with the new definition.  If it is
1574 removed with @code{undefine}, @emph{all} the definitions are removed,
1575 and not only the topmost one.
1577 @example
1578 define(`foo', `Expansion one.')
1579 @result{}
1581 @result{}Expansion one.
1582 pushdef(`foo', `Expansion two.')
1583 @result{}
1585 @result{}Expansion two.
1586 define(`foo', `Second expansion two.')
1587 @result{}
1589 @result{}Second expansion two.
1590 undefine(`foo')
1591 @result{}
1593 @result{}foo
1594 @end example
1596 @cindex local variables
1597 @cindex variables, local
1598 Local variables within macros are made with @code{pushdef} and
1599 @code{popdef}.  At the start of the macro a new definition is pushed,
1600 within the macro it is manipulated and at the end it is popped,
1601 revealing the former definition.
1603 It is possible to temporarily redefine a builtin with @code{pushdef}
1604 and @code{defn}.
1606 @node Indir
1607 @section Indirect call of macros
1609 @cindex indirect call of macros
1610 @cindex call of macros, indirect
1611 @cindex macros, indirect call of
1612 @cindex @acronym{GNU} extensions
1613 Any macro can be called indirectly with @code{indir}:
1615 @deffn Builtin indir (@var{name}, @dots{})
1616 Results in a call to the macro @var{name}, which is passed the
1617 rest of the arguments.  If @var{name} is not defined, an error message
1618 is printed, and the expansion is void.
1620 The macro @code{indir} is recognized only with parameters.
1621 @end deffn
1623 This can be used to call macros with ``invalid''
1624 names (@code{define} allows such names to be defined):
1626 @example
1627 define(`$$internal$macro', `Internal macro (name `$0')')
1628 @result{}
1629 $$internal$macro
1630 @result{}$$internal$macro
1631 indir(`$$internal$macro')
1632 @result{}Internal macro (name $$internal$macro)
1633 @end example
1635 The point is, here, that larger macro packages can have private macros
1636 defined, that will not be called by accident.  They can @emph{only} be
1637 called through the builtin @code{indir}.
1639 @node Builtin
1640 @section Indirect call of builtins
1642 @cindex indirect call of builtins
1643 @cindex call of builtins, indirect
1644 @cindex builtins, indirect call of
1645 @cindex @acronym{GNU} extensions
1646 Builtin macros can be called indirectly with @code{builtin}:
1648 @deffn Builtin builtin (@var{name}, @dots{})
1649 Results in a call to the builtin @var{name}, which is passed the
1650 rest of the arguments.  If @var{name} does not name a builtin, an error
1651 message is printed, and the expansion is void.
1653 The macro @code{builtin} is recognized only with parameters.
1654 @end deffn
1656 This can be used even if @var{name} has been given another definition
1657 that has covered the original, or been undefined so that no macro
1658 maps to the builtin.
1660 @example
1661 pushdef(`define', `hidden')
1662 @result{}
1663 undefine(`undefine')
1664 @result{}
1665 define(`foo', `bar')
1666 @result{}hidden
1668 @result{}foo
1669 builtin(`define', `foo', `BAR')
1670 @result{}
1672 @result{}BAR
1673 undefine(`foo')
1674 @result{}undefine(foo)
1676 @result{}BAR
1677 builtin(`undefine', `foo')
1678 @result{}
1680 @result{}foo
1681 @end example
1683 Note that this can be used to invoke builtins without arguments, even
1684 when they normally require parameters to be recognized; but it will
1685 provoke a warning, and result in a void expansion.
1687 @example
1688 builtin
1689 @result{}builtin
1690 builtin()
1691 @error{}m4:stdin:2: undefined builtin `'
1692 @result{}
1693 builtin(`builtin')
1694 @error{}m4:stdin:3: Warning: too few arguments to builtin `builtin'
1695 @result{}
1696 builtin(`builtin',)
1697 @error{}m4:stdin:4: undefined builtin `'
1698 @result{}
1699 @end example
1701 @node Conditionals
1702 @chapter Conditionals, loops, and recursion
1704 Macros, expanding to plain text, perhaps with arguments, are not quite
1705 enough.  We would like to have macros expand to different things, based
1706 on decisions taken at run-time.  For that, we need some kind of conditionals.
1707 Also, we would like to have some kind of loop construct, so we could do
1708 something a number of times, or while some condition is true.
1710 @menu
1711 * Ifdef::                       Testing if a macro is defined
1712 * Ifelse::                      If-else construct, or multibranch
1713 * Loops::                       Loops and recursion in m4
1714 @end menu
1716 @node Ifdef
1717 @section Testing macro definitions
1719 @cindex conditionals
1720 There are two different builtin conditionals in @code{m4}.  The first is
1721 @code{ifdef}:
1723 @deffn Builtin ifdef (@var{name}, @var{string-1}, @ovar{string-2})
1724 If @var{name} is defined as a macro, @code{ifdef} expands to
1725 @var{string-1}, otherwise to @var{string-2}.  If @var{string-2} is
1726 omitted, it is taken to be the empty string (according to the normal
1727 rules).
1729 The macro @code{ifdef} is recognized only with parameters.
1730 @end deffn
1732 @example
1733 ifdef(`foo', ``foo' is defined', ``foo' is not defined')
1734 @result{}foo is not defined
1735 define(`foo', `')
1736 @result{}
1737 ifdef(`foo', ``foo' is defined', ``foo' is not defined')
1738 @result{}foo is defined
1739 ifdef(`no_such_macro', `yes', `no', `extra argument')
1740 @error{}m4:stdin:4: Warning: excess arguments to builtin `ifdef' ignored
1741 @result{}no
1742 @end example
1744 @node Ifelse
1745 @section Comparing strings
1747 @cindex comparing strings
1748 The other conditional, @code{ifelse}, is much more powerful.  It can be
1749 used as a way to introduce a long comment, as an if-else construct, or
1750 as a multibranch, depending on the number of arguments supplied:
1752 @deffn Builtin ifelse (@var{comment})
1753 @deffnx Builtin ifelse (@var{string-1}, @var{string-2}, @var{equal}, @
1754   @ovar{not-equal})
1755 @deffnx Builtin ifelse (@var{string-1}, @var{string-2}, @var{equal-1}, @
1756   @var{string-3}, @var{string-4}, @var{equal-2}, @dots{})
1757 Used with only one argument, the @code{ifelse} simply discards it and
1758 produces no output.
1760 If called with three or four arguments, @code{ifelse} expands into
1761 @var{equal}, if @var{string-1} and @var{string-2} are equal (character
1762 for character), otherwise it expands to @var{not-equal}.
1764 If called with six or more arguments, and @var{string-1} and
1765 @var{string-2} are equal, @code{ifelse} expands into @var{equal},
1766 otherwise the first three arguments are discarded and the processing
1767 starts again.
1769 The macro @code{ifelse} is recognized only with parameters.
1770 @end deffn
1772 Using only one argument is a common @code{m4} idiom for introducing a
1773 block comment, as an alternative to repeatedly using @code{dnl}.  This
1774 special usage is recognized by @acronym{GNU} @code{m4}, so that in this
1775 case, the warning about missing arguments is never triggered.
1777 @example
1778 ifelse(`some comments')
1779 @result{}
1780 ifelse(`foo', `bar')
1781 @error{}m4:stdin:2: Warning: too few arguments to builtin `ifelse'
1782 @result{}
1783 @end example
1785 Using three or four arguments provides decision points.
1787 @example
1788 ifelse(`foo', `bar', `true')
1789 @result{}
1790 ifelse(`foo', `foo', `true')
1791 @result{}true
1792 define(`foo', `bar')
1793 @result{}
1794 ifelse(foo, `bar', `true', `false')
1795 @result{}true
1796 ifelse(foo, `foo', `true', `false')
1797 @result{}false
1798 @end example
1800 Notice how the first argument was used unquoted; it is common to compare
1801 the expansion of a macro with a string.  With this macro, you can now
1802 reproduce the behavior of many of the builtins, where the macro is
1803 recognized only with arguments.
1805 @example
1806 define(`foo', `ifelse(`$#', `0', ``$0'', `arguments:$#')')
1807 @result{}
1809 @result{}foo
1810 foo()
1811 @result{}arguments:1
1812 foo(`a', `b', `c')
1813 @result{}arguments:3
1814 @end example
1816 @cindex multibranches
1817 However, @code{ifelse} can take more than four arguments.  If given more
1818 than four arguments, @code{ifelse} works like a @code{case} or @code{switch}
1819 statement in traditional programming languages.  If @var{string-1} and
1820 @var{string-2} are equal, @code{ifelse} expands into @var{equal-1}, otherwise
1821 the procedure is repeated with the first three arguments discarded.  This
1822 calls for an example:
1824 @example
1825 ifelse(`foo', `bar', `third', `gnu', `gnats', `sixth', `seventh')
1826 @result{}seventh
1827 @end example
1829 Naturally, the normal case will be slightly more advanced than these
1830 examples.  A common use of @code{ifelse} is in macros implementing loops
1831 of various kinds.
1833 @node Loops
1834 @section Loops and recursion
1836 @cindex recursive macros
1837 @cindex macros, recursive
1838 There is no direct support for loops in @code{m4}, but macros can be
1839 recursive.  There is no limit on the number of recursion levels, other
1840 than those enforced by your hardware and operating system.
1842 @cindex loops
1843 Loops can be programmed using recursion and the conditionals described
1844 previously.
1846 There is a builtin macro, @code{shift}, which can, among other things,
1847 be used for iterating through the actual arguments to a macro:
1849 @deffn Builtin shift (@dots{})
1850 Takes any number of arguments, and expands to all but the first
1851 argument, separated by commas, with each argument quoted.
1853 The macro @code{shift} is recognized only with parameters.
1854 @end deffn
1856 @example
1857 shift
1858 @result{}shift
1859 shift(`bar')
1860 @result{}
1861 shift(`foo', `bar', `baz')
1862 @result{}bar,baz
1863 @end example
1865 An example of the use of @code{shift} is this macro:
1867 @deffn Composite reverse (@dots{})
1868 Takes any number of arguments, and reverse their order.
1869 @end deffn
1871 It is implemented as:
1873 @example
1874 define(`reverse', `ifelse(`$#', `0', , `$#', `1', ``$1'',
1875                           `reverse(shift($@@)), `$1'')')
1876 @result{}
1877 reverse
1878 @result{}
1879 reverse(`foo')
1880 @result{}foo
1881 reverse(`foo', `bar', `gnats', `and gnus')
1882 @result{}and gnus, gnats, bar, foo
1883 @end example
1885 While not a very interesting macro, it does show how simple loops can be
1886 made with @code{shift}, @code{ifelse} and recursion.
1888 @cindex for loops
1889 @cindex loops, counting
1890 @cindex counting loops
1891 Here is an example of a loop macro that implements a simple for loop.
1893 @deffn Composite forloop (@var{iterator}, @var{start}, @var{end}, @var{text})
1894 Takes the name in @var{iterator}, which must be a valid macro name, and
1895 successively assign it each integer value from @var{start} to @var{end},
1896 inclusive.  For each assignment to @var{iterator}, append @var{text} to
1897 the expansion of the @code{forloop}.  @var{text} may refer to
1898 @var{iterator}.  Any definition of @var{iterator} prior to this
1899 invocation is restored.
1900 @end deffn
1902 It can, for example, be used for simple counting:
1904 @example
1905 include(`forloop.m4')
1906 @result{}
1907 forloop(`i', `1', `8', `i ')
1908 @result{}1 2 3 4 5 6 7 8 @comment
1909 @end example
1911 For-loops can be nested, like:
1913 @example
1914 include(`forloop.m4')
1915 @result{}
1916 forloop(`i', `1', `4', `forloop(`j', `1', `8', ` (i, j)')
1918 @result{} (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8)
1919 @result{} (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8)
1920 @result{} (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8)
1921 @result{} (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8)
1922 @result{}
1923 @end example
1925 The implementation of the @code{forloop} macro is fairly
1926 straightforward.  The @code{forloop} macro itself is simply a wrapper,
1927 which saves the previous definition of the first argument, calls the
1928 internal macro @code{_forloop}, and re-establishes the saved definition of
1929 the first argument.
1931 The macro @code{_forloop} expands the fourth argument once, and tests
1932 to see if it is finished.  If it has not finished, it increments
1933 the iteration variable (using the predefined macro @code{incr},
1934 @pxref{Incr}), and recurses.
1936 Here is the actual implementation of @code{forloop}, distributed as
1937 @file{examples/@/forloop.m4} in this package:
1939 @example
1940 undivert(`forloop.m4')
1941 @result{}divert(`-1')
1942 @result{}# forloop(var, from, to, stmt)
1943 @result{}define(`forloop',
1944 @result{}  `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
1945 @result{}define(`_forloop',
1946 @result{}  `$4`'ifelse($1, `$3', ,
1947 @result{}    `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
1948 @result{}divert`'dnl
1949 @result{}
1950 @end example
1952 Notice the careful use of quotes.  Only three macro arguments are
1953 unquoted, each for its own reason.  Try to find out @emph{why} these
1954 three arguments are left unquoted, and see what happens if they are
1955 quoted.
1957 Now, even though these two macros are useful, they are still not robust
1958 enough for general use. They lack even basic error handling of cases
1959 like start value less than final value, and the first argument not being
1960 a name.  Correcting these errors are left as an exercise to the reader.
1962 @node Debugging
1963 @chapter How to debug macros and input
1965 When writing macros for @code{m4}, they often do not work as intended on
1966 the first try (as is the case with most programming languages).
1967 Fortunately, there is support for macro debugging in @code{m4}.
1969 @menu
1970 * Dumpdef::                     Displaying macro definitions
1971 * Trace::                       Tracing macro calls
1972 * Debug Levels::                Controlling debugging output
1973 * Debug Output::                Saving debugging output
1974 @end menu
1976 @node Dumpdef
1977 @section Displaying macro definitions
1979 @cindex displaying macro definitions
1980 @cindex macros, displaying definitions
1981 @cindex definitions, displaying macro
1982 If you want to see what a name expands into, you can use the builtin
1983 @code{dumpdef}:
1985 @deffn Builtin dumpdef (@dots{})
1986 Accepts any number of arguments.  If called without any arguments,
1987 it displays the definitions of all known names, otherwise it displays
1988 the definitions of the names given.  The output is printed to the
1989 current debug file (usually standard error), and is sorted by name.  If
1990 an unknown name is encountered, a warning is printed.
1992 The expansion of @code{dumpdef} is void.
1993 @end deffn
1995 @example
1996 define(`foo', `Hello world.')
1997 @result{}
1998 dumpdef(`foo')
1999 @error{}foo:@tabchar{}`Hello world.'
2000 @result{}
2001 dumpdef(`define')
2002 @error{}define:@tabchar{}<define>
2003 @result{}
2004 @end example
2006 The last example shows how builtin macros definitions are displayed.
2007 The definition that is dumped corresponds to what would occur if the
2008 macro were to be called at that point, even if other definitions are
2009 still live due to redefining a macro during argument collection.
2011 @example
2012 pushdef(`f', ``$0'1')pushdef(`f', ``$0'2')
2013 @result{}
2014 f(popdef(`f')dumpdef(`f'))
2015 @error{}f:@tabchar{}``$0'1'
2016 @result{}f2
2017 f(popdef(`f')dumpdef(`f'))
2018 @error{}m4:stdin:3: undefined macro `f'
2019 @result{}f1
2020 @end example
2022 @xref{Debug Levels}, for information on controlling the details of the
2023 display.
2025 @node Trace
2026 @section Tracing macro calls
2028 @cindex tracing macro expansion
2029 @cindex macro expansion, tracing
2030 @cindex expansion, tracing macro
2031 It is possible to trace macro calls and expansions through the builtins
2032 @code{traceon} and @code{traceoff}:
2034 @deffn Builtin traceon (@dots{})
2035 @deffnx Builtin traceoff (@dots{})
2036 When called without any arguments, @code{traceon} and @code{traceoff}
2037 will turn tracing on and off, respectively, for all defined macros.
2038 When called with arguments, only the named macros are affected, whether
2039 or not they are currently defined.
2041 The expansion of @code{traceon} and @code{traceoff} is void.
2042 @end deffn
2044 Whenever a traced macro is called and the arguments have been collected,
2045 the call is displayed.  If the expansion of the macro call is not void,
2046 the expansion can be displayed after the call.  The output is printed
2047 to the current debug file (usually standard error).
2049 @example
2050 define(`foo', `Hello World.')
2051 @result{}
2052 define(`echo', `$@@')
2053 @result{}
2054 traceon(`foo', `echo')
2055 @result{}
2057 @error{}m4trace: -1- foo -> `Hello World.'
2058 @result{}Hello World.
2059 echo(gnus, and gnats)
2060 @error{}m4trace: -1- echo(`gnus', `and gnats') -> ``gnus',`and gnats''
2061 @result{}gnus,and gnats
2062 @end example
2064 The number between dashes is the depth of the expansion.  It is one most
2065 of the time, signifying an expansion at the outermost level, but it
2066 increases when macro arguments contain unquoted macro calls.  The
2067 maximum number that will appear between dashes is controlled by the
2068 option @option{--nesting-limit} (@pxref{Invoking m4}).
2070 Tracing by name is an attribute that is preserved whether the macro is
2071 defined or not.  This allows the @option{-t} option to select macros to
2072 trace before those macros are defined.
2074 @example
2075 traceoff(`foo')
2076 @result{}
2077 traceon(`foo')
2078 @result{}
2080 @result{}foo
2081 define(`foo', `bar')
2082 @result{}
2084 @error{}m4trace: -1- foo -> `bar'
2085 @result{}bar
2086 undefine(`foo')
2087 @result{}
2088 ifdef(`foo', `yes', `no')
2089 @result{}no
2090 indir(`foo')
2091 @error{}m4:stdin:8: undefined macro `foo'
2092 @result{}
2093 define(`foo', `blah')
2094 @result{}
2096 @error{}m4trace: -1- foo -> `blah'
2097 @result{}blah
2098 traceoff
2099 @result{}
2101 @result{}blah
2102 @end example
2104 Tracing even works on builtins.  However, @command{defn} (@pxref{Defn})
2105 does not transfer tracing status.
2107 @example
2108 traceon(`eval', `m4_divnum')
2109 @result{}
2110 define(`m4_eval', defn(`eval'))
2111 @result{}
2112 define(`m4_divnum', defn(`divnum'))
2113 @result{}
2114 eval(divnum)
2115 @error{}m4trace: -1- eval(`0') -> `0'
2116 @result{}0
2117 m4_eval(m4_divnum)
2118 @error{}m4trace: -2- m4_divnum -> `0'
2119 @result{}0
2120 @end example
2122 @xref{Debug Levels}, for information on controlling the details of the
2123 display.
2125 @node Debug Levels
2126 @section Controlling debugging output
2128 @cindex controlling debugging output
2129 @cindex debugging output, controlling
2130 The @option{-d} option to @code{m4} (@pxref{Invoking m4}) controls the
2131 amount of details presented, when using the macros described in the
2132 preceding sections.
2134 The @var{flags} following the option can be one or more of the
2135 following:
2137 @table @code
2138 @item a
2139 Show the actual arguments in each macro call.  This applies to all macro
2140 calls if the @samp{t} flag is used, otherwise only the macros covered by
2141 calls of @code{traceon}.
2143 @item c
2144 Show several trace lines for each macro call.  A line is shown when the
2145 macro is seen, but before the arguments are collected; a second line
2146 when the arguments have been collected and a third line after the call
2147 has completed.
2149 @item e
2150 Show the expansion of each macro call, if it is not void.  This applies
2151 to all macro calls if the @samp{t} flag is used, otherwise only the
2152 macros covered by calls of @code{traceon}.
2154 @item f
2155 Show the name of the current input file in each trace output line.
2157 @item i
2158 Print a message each time the current input file is changed, giving file
2159 name and input line number.
2161 @item l
2162 Show the current input line number in each trace output line.
2164 @item p
2165 Print a message when a named file is found through the path search
2166 mechanism (@pxref{Search Path}), giving the actual file name used.
2168 @item q
2169 Quote actual arguments and macro expansions in the display with the
2170 current quotes.
2172 @item t
2173 Trace all macro calls made in this invocation of @code{m4}.
2175 @item x
2176 Add a unique `macro call id' to each line of the trace output.  This is
2177 useful in connection with the @samp{c} flag above.
2179 @item V
2180 A shorthand for all of the above flags.
2181 @end table
2183 If no flags are specified with the @option{-d} option, the default is
2184 @samp{aeq}.  The examples throughout this manual assume the default
2185 flags.
2187 @cindex @acronym{GNU} extensions
2188 There is a builtin macro @code{debugmode}, which allows on-the-fly control of
2189 the debugging output format:
2191 @deffn Builtin debugmode (@ovar{flags})
2192 The argument @var{flags} should be a subset of the letters listed above.
2193 As special cases, if the argument starts with a @samp{+}, the flags are
2194 added to the current debug flags, and if it starts with a @samp{-}, they
2195 are removed.  If no argument is present, all debugging flags are cleared
2196 (as if no @option{-d} was given), and with an empty argument the flags
2197 are reset to the default of @samp{aeq}.
2199 The expansion of @code{debugmode} is void.
2200 @end deffn
2202 @example
2203 define(`foo', `FOO')
2204 @result{}
2205 traceon(`foo')
2206 @result{}
2207 debugmode()
2208 @result{}
2210 @error{}m4trace: -1- foo -> `FOO'
2211 @result{}FOO
2212 debugmode
2213 @result{}
2215 @error{}m4trace: -1- foo
2216 @result{}FOO
2217 debugmode(`+l')
2218 @result{}
2220 @error{}m4trace:8: -1- foo
2221 @result{}FOO
2222 @end example
2224 @node Debug Output
2225 @section Saving debugging output
2227 @cindex saving debugging output
2228 @cindex debugging output, saving
2229 @cindex output, saving debugging
2230 @cindex @acronym{GNU} extensions
2231 Debug and tracing output can be redirected to files using either the
2232 @option{-o} option to @code{m4} (@pxref{Invoking m4}), or with the
2233 builtin macro @code{debugfile}:
2235 @deffn Builtin debugfile (@ovar{file})
2236 Sends all further debug and trace output to @var{file}.  If
2237 @var{file} is empty, debug and trace output are discarded.  If
2238 @code{debugfile} is called without any arguments, debug and trace output
2239 are sent to standard error.  This does not affect warnings, error
2240 messages, or @code{errprint} output, which are
2241 always sent to standard error.  If @var{file} cannot be opened, the
2242 current debug file is unchanged.
2244 The expansion of @code{debugfile} is void.
2245 @end deffn
2247 @example
2248 traceon(`divnum')
2249 @result{}
2250 divnum(`extra')
2251 @error{}m4:stdin:2: Warning: excess arguments to builtin `divnum' ignored
2252 @error{}m4trace: -1- divnum(`extra') -> `0'
2253 @result{}0
2254 debugfile()
2255 @result{}
2256 divnum(`extra')
2257 @error{}m4:stdin:4: Warning: excess arguments to builtin `divnum' ignored
2258 @result{}0
2259 debugfile
2260 @result{}
2261 divnum
2262 @error{}m4trace: -1- divnum -> `0'
2263 @result{}0
2264 @end example
2266 @node Input Control
2267 @chapter Input control
2269 This chapter describes various builtin macros for controlling the input
2270 to @code{m4}.
2272 @menu
2273 * Dnl::                         Deleting whitespace in input
2274 * Changequote::                 Changing the quote characters
2275 * Changecom::                   Changing the comment delimiters
2276 * Changeword::                  Changing the lexical structure of words
2277 * M4wrap::                      Saving input until end of input
2278 @end menu
2280 @node Dnl
2281 @section Deleting whitespace in input
2283 @cindex deleting whitespace in input
2284 The builtin @code{dnl} stands for ``Discard to Next Line'':
2286 @deffn Builtin dnl
2287 All characters, up to and including the next newline, are discarded
2288 without performing any macro expansion.
2290 The expansion of @code{dnl} is void.
2291 @end deffn
2293 It is often used in connection with @code{define}, to remove the
2294 newline that follows the call to @code{define}.  Thus
2296 @example
2297 define(`foo', `Macro `foo'.')dnl A very simple macro, indeed.
2299 @result{}Macro foo.
2300 @end example
2302 The input up to and including the next newline is discarded, as opposed
2303 to the way comments are treated (@pxref{Comments}).
2305 Usually, @code{dnl} is immediately followed by an end of line or some
2306 other whitespace.  @acronym{GNU} @code{m4} will produce a warning diagnostic if
2307 @code{dnl} is followed by an open parenthesis.  In this case, @code{dnl}
2308 will collect and process all arguments, looking for a matching close
2309 parenthesis.  All predictable side effects resulting from this
2310 collection will take place.  @code{dnl} will return no output.  The
2311 input following the matching close parenthesis up to and including the
2312 next newline, on whatever line containing it, will still be discarded.
2314 @example
2315 dnl(`args are ignored, but side effects occur',
2316 define(`foo', `like this')) while this text is ignored: undefine(`foo')
2317 @error{}m4:stdin:2: Warning: excess arguments to builtin `dnl' ignored
2318 See how `foo' was defined, foo?
2319 @result{}See how foo was defined, like this?
2320 @end example
2322 If the end of file is encountered without a newline character, a
2323 warning is issued and dnl stops consuming input.
2325 @example
2326 define(`hi', `HI')
2327 @result{}
2328 m4wrap(`m4wrap(`2 hi
2329 ')0 hi dnl 1 hi')
2330 @result{}
2332 @error{}m4: Warning: end of file treated as newline
2333 @result{}0 HI 2 HI
2334 @end example
2336 @node Changequote
2337 @section Changing the quote characters
2339 @cindex changing the quote delimiters
2340 @cindex quote delimiters, changing the
2341 The default quote delimiters can be changed with the builtin
2342 @code{changequote}:
2344 @deffn Builtin changequote (@dvar{start, `}, @dvar{end, '})
2345 This sets @var{start} as the new begin-quote delimiter and @var{end} as the
2346 new end-quote delimiter.  If any of the arguments are missing, the default
2347 quotes (@code{`} and @code{'}) are used instead of the void arguments.
2348 @comment FIXME POSIX requires that with one argument, the closing quote
2349 @comment be set to newline, not '.
2351 The expansion of @code{changequote} is void.
2352 @end deffn
2354 @example
2355 changequote(`[', `]')
2356 @result{}
2357 define([foo], [Macro [foo].])
2358 @result{}
2360 @result{}Macro foo.
2361 @end example
2363 The quotation strings can safely contain eight-bit characters.
2364 @ignore
2365 Yuck.  I know of no clean way to render an 8-bit character in both info
2366 and dvi.  This example uses the `open-guillemot' and `close-guillemot'
2367 characters of the Latin-1 character set.
2369 @example
2370 define(`a', `b')
2371 @result{}
2372 «a»
2373 @result{}«b»
2374 changequote(`«', `»')
2375 @result{}
2376 «a»
2377 @result{}a
2378 @end example
2379 @end ignore
2380 If no single character is appropriate, @var{start} and @var{end} can be
2381 of any length.
2383 @example
2384 changequote(`[[[', `]]]')
2385 @result{}
2386 define([[[foo]]], [[[Macro [[[[[foo]]]]].]]])
2387 @result{}
2389 @result{}Macro [[foo]].
2390 @end example
2392 Changing the quotes to the empty strings will effectively disable the
2393 quoting mechanism, leaving no way to quote text.
2395 @example
2396 define(`foo', `Macro `FOO'.')
2397 @result{}
2398 changequote(, )
2399 @result{}
2401 @result{}Macro `FOO'.
2402 `foo'
2403 @result{}`Macro `FOO'.'
2404 @end example
2406 There is no way in @code{m4} to quote a string containing an unmatched
2407 begin-quote, except using @code{changequote} to change the current
2408 quotes.
2410 If the quotes should be changed from, say, @samp{[} to @samp{[[},
2411 temporary quote characters have to be defined.  To achieve this, two
2412 calls of @code{changequote} must be made, one for the temporary quotes
2413 and one for the new quotes.
2415 Macros are recognized in preference to the begin-quote string, so if a
2416 prefix of @var{start} can be recognized as a potential macro name, the
2417 quoting mechanism is effectively disabled.  Unless you use
2418 @code{changeword} (@pxref{Changeword}), this means that @var{start}
2419 should not begin with a letter or @samp{_} (underscore).
2421 @example
2422 define(`hi', `HI')
2423 @result{}
2424 changequote(`q', `Q')
2425 @result{}
2426 q hi Q hi
2427 @result{}q HI Q HI
2428 changequote
2429 @result{}
2430 changequote(`-', `EOF')
2431 @result{}
2432 - hi EOF hi
2433 @result{} hi  HI
2434 @end example
2436 Quotes are recognized in preference to argument collection.  In
2437 particular, if @var{start} is a single @samp{(}, then argument
2438 collection is effectively disabled.  For portability with other
2439 implementations, it is a good idea to avoid @samp{(}, @samp{,}, and
2440 @samp{)} as the first character in @var{start}.
2442 @example
2443 define(`echo', `$#:$@:')
2444 @result{}
2445 define(`hi', `HI')
2446 @result{}
2447 changequote(`(',`)')
2448 @result{}
2449 echo(hi)
2450 @result{}0::hi
2451 changequote
2452 @result{}
2453 changequote(`((', `))')
2454 @result{}
2455 echo(hi)
2456 @result{}1:HI:
2457 echo((hi))
2458 @result{}0::hi
2459 changequote
2460 @result{}
2461 changequote(`,', `)')
2462 @result{}
2463 echo(hi,hi)bye)
2464 @result{}1:HIhibye:
2465 @end example
2467 If @var{end} is a prefix of @var{start}, the end-quote will be
2468 recognized in preference to a nested begin-quote.  In particular,
2469 changing the quotes to have the same string for @var{start} and
2470 @var{end} disables nesting of quotes.  When quote nesting is disabled,
2471 it is impossible to double-quote strings across macro expansions, so
2472 using the same string is not done very often.
2474 @example
2475 define(`hi', `HI')
2476 @result{}
2477 changequote(`""', `"')
2478 @result{}
2479 ""hi"""hi"
2480 @result{}hihi
2481 ""hi" ""hi"
2482 @result{}hi hi
2483 ""hi"" "hi"
2484 @result{}hi" "HI"
2485 changequote
2486 @result{}
2487 `hi`hi'hi'
2488 @result{}hi`hi'hi
2489 changequote(`"', `"')
2490 @result{}
2491 "hi"hi"hi"
2492 @result{}hiHIhi
2493 @end example
2495 It is an error if the end of file occurs within a quoted string.
2497 @example
2498 `dangling quote
2500 @error{}m4:stdin:1: ERROR: end of file in string
2501 @end example
2503 @node Changecom
2504 @section Changing comment delimiters
2506 @cindex changing comment delimiters
2507 @cindex comment delimiters, changing
2508 The default comment delimiters can be changed with the builtin
2509 macro @code{changecom}:
2511 @deffn Builtin changecom (@ovar{start}, @ovar{end})
2512 This sets @var{start} as the new begin-comment delimiter and @var{end} as
2513 the new end-comment delimiter.  If only one argument is provided,
2514 newline becomes the new end-comment delimiter.  The comment delimiters
2515 can be of any length.  Omitting the first argument, or using the empty
2516 string as the first argument, disables comments.
2518 The expansion of @code{changecom} is void.
2519 @end deffn
2521 @example
2522 define(`comment', `COMMENT')
2523 @result{}
2524 # A normal comment
2525 @result{}# A normal comment
2526 changecom(`/*', `*/')
2527 @result{}
2528 # Not a comment anymore
2529 @result{}# Not a COMMENT anymore
2530 But: /* this is a comment now */ while this is not a comment
2531 @result{}But: /* this is a comment now */ while this is not a COMMENT
2532 @end example
2534 @cindex comments, copied to output
2535 Note how comments are copied to the output, much as if they were quoted
2536 strings.  If you want the text inside a comment expanded, quote the
2537 begin-comment delimiter.
2539 Calling @code{changecom} without any arguments, or with an empty string
2540 for the first argument, disables the commenting mechanism completely.
2541 To restore the original comment start of @samp{#}, you must explicitly
2542 ask for it.
2544 @example
2545 define(`comment', `COMMENT')
2546 @result{}
2547 changecom
2548 @result{}
2549 # Not a comment anymore
2550 @result{}# Not a COMMENT anymore
2551 changecom(`#')
2552 @result{}
2553 # comment again
2554 @result{}# comment again
2555 @end example
2557 The comment strings can safely contain eight-bit characters.
2558 @ignore
2559 Yuck.  I know of no clean way to render an 8-bit character in both info
2560 and dvi.  This example uses the `open-guillemot' and `close-guillemot'
2561 characters of the Latin-1 character set.
2563 @example
2564 define(`a', `b')
2565 @result{}
2566 «a»
2567 @result{}«b»
2568 changecom(`«', `»')
2569 @result{}
2570 «a»
2571 @result{}«a»
2572 @end example
2573 @end ignore
2575 Comments are recognized in preference to macros.  However, this is not
2576 compatible with other implementations, where macros and even quoting
2577 takes precedence over comments, so it may change in a future release.
2578 For portability, this means that @var{start} should not begin with a
2579 letter or @samp{_} (underscore), and that neither the start-quote nor
2580 the start-comment string should be a prefix of the other.
2582 @example
2583 define(`hi', `HI')
2584 @result{}
2585 changecom(`q', `Q')
2586 @result{}
2587 q hi Q hi
2588 @result{}q hi Q HI
2589 @end example
2591 Comments are recognized in preference to argument collection.  In
2592 particular, if @var{start} is a single @samp{(}, then argument
2593 collection is effectively disabled.  For portability with other
2594 implementations, it is a good idea to avoid @samp{(}, @samp{,}, and
2595 @samp{)} as the first character in @var{start}.
2597 @example
2598 define(`echo', `$#:$@:')
2599 @result{}
2600 define(`hi', `HI')
2601 @result{}
2602 changecom(`(',`)')
2603 @result{}
2604 echo(hi)
2605 @result{}0::(hi)
2606 changecom
2607 @result{}
2608 changecom(`((', `))')
2609 @result{}
2610 echo(hi)
2611 @result{}1:HI:
2612 echo((hi))
2613 @result{}0::((hi))
2614 changecom(`,', `)')
2615 @result{}
2616 echo(hi,hi)bye)
2617 @result{}1:HI,hi)bye:
2618 @end example
2620 It is an error if the end of file occurs within a comment.
2622 @example
2623 changecom(`/*', `*/')
2624 @result{}
2625 /*dangling comment
2627 @error{}m4:stdin:1: ERROR: end of file in comment
2628 @end example
2630 @node Changeword
2631 @section Changing the lexical structure of words
2633 @cindex lexical structure of words
2634 @cindex words, lexical structure of
2635 @quotation
2636 The macro @code{changeword} and all associated functionality is
2637 experimental.  It is only available if the @option{--enable-changeword}
2638 option was given to @code{configure}, at @acronym{GNU} @code{m4} installation
2639 time.  The functionality will go away in the future, to be replaced by
2640 other new features that are more efficient at providing the same
2641 capabilities.  @emph{Do not rely on it}.  Please direct your comments
2642 about it the same way you would do for bugs.
2643 @end quotation
2645 A file being processed by @code{m4} is split into quoted strings, words
2646 (potential macro names) and simple tokens (any other single character).
2647 Initially a word is defined by the following regular expression:
2649 @comment ignore
2650 @example
2651 [_a-zA-Z][_a-zA-Z0-9]*
2652 @end example
2654 Using @code{changeword}, you can change this regular expression:
2656 @deffn {Optional builtin} changeword (@var{regex})
2657 Changes the regular expression for recognizing macro names to be
2658 @var{regex}.  If @var{regex} is empty, use
2659 @samp{[_a-zA-Z][_a-zA-Z0-9]*}.  @var{regex} must obey the constraint
2660 that every prefix of the desired final pattern is also accepted by the
2661 regular expression.  If @var{regex} contains grouping parentheses, the
2662 macro invoked is the portion that matched the first group, rather than
2663 the entire matching string.
2665 The expansion of @code{changeword} is void.
2666 The macro @code{changeword} is recognized only with parameters.
2667 @end deffn
2669 Relaxing the lexical rules of @code{m4} might be useful (for example) if
2670 you wanted to apply translations to a file of numbers:
2672 @example
2673 ifdef(`changeword', `', `m4exit(`77')')
2674 @result{}
2675 changeword(`[_a-zA-Z0-9]+')
2676 @result{}
2677 define(`1', `0')1
2678 @result{}0
2679 @end example
2681 Tightening the lexical rules is less useful, because it will generally
2682 make some of the builtins unavailable.  You could use it to prevent
2683 accidental call of builtins, for example:
2685 @example
2686 ifdef(`changeword', `', `m4exit(`77')')
2687 @result{}
2688 define(`_indir', defn(`indir'))
2689 @result{}
2690 changeword(`_[_a-zA-Z0-9]*')
2691 @result{}
2692 esyscmd(`foo')
2693 @result{}esyscmd(foo)
2694 _indir(`esyscmd', `echo hi')
2695 @result{}hi
2696 @result{}
2697 @end example
2699 Because @code{m4} constructs its words a character at a time, there
2700 is a restriction on the regular expressions that may be passed to
2701 @code{changeword}.  This is that if your regular expression accepts
2702 @samp{foo}, it must also accept @samp{f} and @samp{fo}.
2704 @code{changeword} has another function.  If the regular expression
2705 supplied contains any grouped subexpressions, then text outside
2706 the first of these is discarded before symbol lookup.  So:
2708 @example
2709 ifdef(`changeword', `', `m4exit(`77')')
2710 @result{}
2711 changecom(`/*', `*/')dnl
2712 define(`foo', `bar')dnl
2713 changeword(`#\([_a-zA-Z0-9]*\)')
2714 @result{}
2715 #esyscmd(`echo foo \#foo')
2716 @result{}foo bar
2717 @result{}
2718 @end example
2720 @code{m4} now requires a @samp{#} mark at the beginning of every
2721 macro invocation, so one can use @code{m4} to preprocess plain
2722 text without losing various words like @samp{divert}.
2724 In @code{m4}, macro substitution is based on text, while in @TeX{}, it
2725 is based on tokens.  @code{changeword} can throw this difference into
2726 relief.  For example, here is the same idea represented in @TeX{} and
2727 @code{m4}.  First, the @TeX{} version:
2729 @comment ignore
2730 @example
2731 \def\a@{\message@{Hello@}@}
2732 \catcode`\@@=0
2733 \catcode`\\=12
2735 @@bye
2736 @result{}Hello
2737 @end example
2739 @noindent
2740 Then, the @code{m4} version:
2742 @example
2743 ifdef(`changeword', `', `m4exit(`77')')
2744 @result{}
2745 define(`a', `errprint(`Hello')')dnl
2746 changeword(`@@\([_a-zA-Z0-9]*\)')
2747 @result{}
2749 @result{}errprint(Hello)
2750 @end example
2752 In the @TeX{} example, the first line defines a macro @code{a} to
2753 print the message @samp{Hello}.  The second line defines @key{@@} to
2754 be usable instead of @key{\} as an escape character.  The third line
2755 defines @key{\} to be a normal printing character, not an escape.
2756 The fourth line invokes the macro @code{a}.  So, when @TeX{} is run
2757 on this file, it displays the message @samp{Hello}.
2759 When the @code{m4} example is passed through @code{m4}, it outputs
2760 @samp{errprint(Hello)}.  The reason for this is that @TeX{} does
2761 lexical analysis of macro definition when the macro is @emph{defined}.
2762 @code{m4} just stores the text, postponing the lexical analysis until
2763 the macro is @emph{used}.
2765 You should note that using @code{changeword} will slow @code{m4} down
2766 by a factor of about seven, once it is changed to something other
2767 than the default regular expression.  You can invoke @code{changeword}
2768 with the empty string to restore the default word definition, and regain
2769 the parsing speed.
2771 @node M4wrap
2772 @section Saving input
2774 @cindex saving input
2775 @cindex input, saving
2776 It is possible to `save' some text until the end of the normal input has
2777 been seen.  Text can be saved, to be read again by @code{m4} when the
2778 normal input has been exhausted.  This feature is normally used to
2779 initiate cleanup actions before normal exit, e.g., deleting temporary
2780 files.
2782 To save input text, use the builtin @code{m4wrap}:
2784 @deffn Builtin m4wrap (@ovar{string}, @dots{})
2785 Stores @var{string} in a safe place, to be reread when end of input is
2786 reached.  As a @acronym{GNU} extension, additional arguments are
2787 concatenated with a space to the @var{string}.
2789 The expansion of @code{m4wrap} is void.
2790 The macro @code{m4wrap} is recognized only with parameters.
2791 @end deffn
2793 @example
2794 define(`cleanup', `This is the `cleanup' action.
2796 @result{}
2797 m4wrap(`cleanup')
2798 @result{}
2799 This is the first and last normal input line.
2800 @result{}This is the first and last normal input line.
2802 @result{}This is the cleanup action.
2803 @end example
2805 The saved input is only reread when the end of normal input is seen, and
2806 not if @code{m4exit} is used to exit @code{m4}.
2808 @comment FIXME: this contradicts POSIX, which requires that "If the
2809 @comment m4wrap macro is used multiple times, the arguments specified
2810 @comment shall be processed in the order in which the m4wrap macros were
2811 @comment processed."
2812 It is safe to call @code{m4wrap} from saved text, but then the order in
2813 which the saved text is reread is undefined.  If @code{m4wrap} is not used
2814 recursively, the saved pieces of text are reread in the opposite order
2815 in which they were saved (LIFO---last in, first out).  However, this
2816 behavior is likely to change in a future release, to match
2817 @acronym{POSIX}, so you should not depend on this order.
2819 Here is an example of implementing a factorial function using
2820 @code{m4wrap}:
2822 @example
2823 define(`f', `ifelse(`$1', `0', `Answer: 0!=1
2824 ', eval(`$1>1'), `0', `Answer: $2$1=eval(`$2$1')
2825 ', `m4wrap(`f(decr(`$1'), `$2$1*')')')')
2826 @result{}
2827 f(`10')
2828 @result{}
2830 @result{}Answer: 10*9*8*7*6*5*4*3*2*1=3628800
2831 @end example
2833 Invocations of @code{m4wrap} at the same recursion level are
2834 concatenated and rescanned as usual:
2836 @example
2837 define(`aa', `AA
2839 @result{}
2840 m4wrap(`a')m4wrap(`a')
2841 @result{}
2843 @result{}AA
2844 @end example
2846 @noindent
2847 however, the transition between recursion levels behaves like an end of
2848 file condition between two input files.
2850 @example
2851 m4wrap(`m4wrap(`)')len(abc')
2852 @result{}
2854 @error{}m4: ERROR: end of file in argument list
2855 @end example
2857 @node File Inclusion
2858 @chapter File inclusion
2860 @cindex file inclusion
2861 @cindex inclusion, of files
2863 @code{m4} allows you to include named files at any point in the input.
2865 @menu
2866 * Include::                     Including named files
2867 * Search Path::                 Searching for include files
2868 @end menu
2870 @node Include
2871 @section Including named files
2873 There are two builtin macros in @code{m4} for including files:
2875 @deffn Builtin include (@var{file})
2876 @deffnx Builtin sinclude (@var{file})
2877 Both macros cause the file named @var{file} to be read by
2878 @code{m4}.  When the end of the file is reached, input is resumed from
2879 the previous input file.
2881 The expansion of @code{include} and @code{sinclude} is therefore the
2882 contents of @var{file}.
2884 If @var{file} does not exist (or cannot be read), the expansion is void,
2885 and @code{include} will fail with an error while @code{sinclude} is
2886 silent.  The empty string counts as a file that does not exist.
2888 The macros @code{include} and @code{sinclude} are recognized only with
2889 parameters.
2890 @end deffn
2892 @example
2893 include(`none')
2894 @result{}
2895 @error{}m4:stdin:1: cannot open `none': No such file or directory
2896 include()
2897 @result{}
2898 @error{}m4:stdin:2: cannot open `': No such file or directory
2899 sinclude(`none')
2900 @result{}
2901 sinclude()
2902 @result{}
2903 @end example
2905 The rest of this section assumes that @code{m4} is invoked with the
2906 @option{-I} option (@pxref{Invoking m4}) pointing to the @file{examples}
2907 directory shipped as part of the @acronym{GNU} @code{m4} package.  The
2908 file @file{examples/@/incl.m4} in the distribution contains the lines:
2909 @comment ignore
2910 @example
2911 Include file start
2913 Include file end
2914 @end example
2915 @noindent
2916 Normally file inclusion is used to insert the contents of a file
2917 into the input stream.  The contents of the file will be read by
2918 @code{m4} and macro calls in the file will be expanded:
2920 @example
2921 define(`foo', `FOO')
2922 @result{}
2923 include(`incl.m4')
2924 @result{}Include file start
2925 @result{}FOO
2926 @result{}Include file end
2927 @result{}
2928 @end example
2930 The fact that @code{include} and @code{sinclude} expand to the contents
2931 of the file can be used to define macros that operate on entire files.
2932 Here is an example, which defines @samp{bar} to expand to the contents
2933 of @file{incl.m4}:
2935 @example
2936 define(`bar', include(`incl.m4'))
2937 @result{}
2938 This is `bar':  >>bar<<
2939 @result{}This is bar:  >>Include file start
2940 @result{}foo
2941 @result{}Include file end
2942 @result{}<<
2943 @end example
2945 This use of @code{include} is not trivial, though, as files can contain
2946 quotes, commas, and parentheses, which can interfere with the way the
2947 @code{m4} parser works.  @acronym{GNU} @code{m4} seamlessly concatenates
2948 the file
2949 contents with the next character, even if the included file ended in
2950 the middle of a comment, string, or macro call.  These conditions are
2951 only treated as end of file errors if specified as input files on the
2952 command line.
2954 @node Search Path
2955 @section Searching for include files
2957 @cindex search path for included files
2958 @cindex included files, search path for
2959 @cindex @acronym{GNU} extensions
2960 @acronym{GNU} @code{m4} allows included files to be found in other directories
2961 than the current working directory.
2963 If a file is not found in the current working directory, and the file
2964 name is not absolute, the file will be looked for in a specified search
2965 path.  First, the directories specified with the @option{-I} option will
2966 be searched, in the order found on the command line (@pxref{Invoking
2967 m4}).  Second, if the
2968 @env{M4PATH} environment variable is set, it is expected to contain a
2969 colon-separated list of directories, which will be searched in order.
2971 If the automatic search for include-files causes trouble, the @samp{p}
2972 debug flag (@pxref{Debug Levels}) can help isolate the problem.
2974 @node Diversions
2975 @chapter Diverting and undiverting output
2977 Diversions are a way of temporarily saving output.  The output of
2978 @code{m4} can at any time be diverted to a temporary file, and be
2979 reinserted into the output stream, @dfn{undiverted}, again at a later
2980 time.
2982 Numbered diversions are counted from 0 upwards, diversion number 0
2983 being the normal output stream.  The number of simultaneous diversions
2984 is limited mainly by the memory used to describe them, because @acronym{GNU}
2985 @code{m4} tries to keep diversions in memory.  However, there is a
2986 limit to the overall memory usable by all diversions taken altogether
2987 (512K, currently).  When this maximum is about to be exceeded,
2988 a temporary file is opened to receive the contents of the biggest
2989 diversion still in memory, freeing this memory for other diversions.
2990 So, it is theoretically possible that the number of diversions be
2991 limited by the number of available file descriptors.
2993 @c FIXME: need some explanation here why this is a useful feature, not
2994 @c just how you use it.
2996 @menu
2997 * Divert::                      Diverting output
2998 * Undivert::                    Undiverting output
2999 * Divnum::                      Diversion numbers
3000 * Cleardiv::                    Discarding diverted text
3001 @end menu
3003 @node Divert
3004 @section Diverting output
3006 @cindex diverting output to files
3007 @cindex output, diverting to files
3008 @cindex files, diverting output to
3009 Output is diverted using @code{divert}:
3011 @deffn Builtin divert (@dvar{number, 0})
3012 The current diversion is changed to @var{number}.  If @var{number} is left
3013 out or empty, it is assumed to be zero.  If @var{number} cannot be
3014 parsed, the diversion is unchanged.
3016 The expansion of @code{divert} is void.
3017 @end deffn
3019 When all the @code{m4} input will have been processed, all existing
3020 diversions are automatically undiverted, in numerical order.
3022 @example
3023 divert(`1')
3024 This text is diverted.
3025 divert
3026 @result{}
3027 This text is not diverted.
3028 @result{}This text is not diverted.
3030 @result{}
3031 @result{}This text is diverted.
3032 @end example
3034 Several calls of @code{divert} with the same argument do not overwrite
3035 the previous diverted text, but append to it.  Diversions are printed
3036 after any wrapped text is expanded.
3038 @example
3039 define(`text', `TEXT')
3040 @result{}
3041 divert(`1')`diverted text.'
3042 divert
3043 @result{}
3044 m4wrap(`Wrapped text preceeds ')
3045 @result{}
3047 @result{}Wrapped TEXT preceeds diverted text.
3048 @end example
3050 If output is diverted to a non-existent diversion, it is simply
3051 discarded.  This can be used to suppress unwanted output.  A common
3052 example of unwanted output is the trailing newlines after macro
3053 definitions.  Here is how to avoid them.
3055 @example
3056 divert(`-1')
3057 define(`foo', `Macro `foo'.')
3058 define(`bar', `Macro `bar'.')
3059 divert
3060 @result{}
3061 @end example
3063 This is a common programming idiom in @code{m4}.
3065 Note that @code{divert} is an English word, but also an active macro
3066 without arguments.  When processing plain text, the word might appear in
3067 normal text and be unintentionally swallowed as a macro invocation.  One
3068 way to avoid this is to use the @option{-P} option to rename all
3069 builtins (@pxref{Invoking m4}).  Another is to write a wrapper that
3070 requires a parameter to be recognized.
3072 @example
3073 We decided to divert the stream for irrigation.
3074 @result{}We decided to  the stream for irrigation.
3075 define(`divert', `ifelse(`$#', `0', ``$0'', `builtin(`$0', $@@)')')
3076 @result{}
3077 divert(-1)
3078 Ignored text.
3079 divert(0)
3080 @result{}
3081 We decided to divert the stream for irrigation.
3082 @result{}We decided to divert the stream for irrigation.
3083 @end example
3085 @node Undivert
3086 @section Undiverting output
3088 Diverted text can be undiverted explicitly using the builtin
3089 @code{undivert}:
3091 @deffn Builtin undivert (@ovar{number}@dots{})
3092 Undiverts the diversions given by the arguments, in the order
3093 given.  If no arguments are supplied, all diversions are undiverted, in
3094 numerical order.  As a @acronym{GNU} extension, if @var{number} is not numeric,
3095 treat it as a file name instead.
3097 The expansion of @code{undivert} is void.
3098 @end deffn
3100 @example
3101 divert(`1')
3102 This text is diverted.
3103 divert
3104 @result{}
3105 This text is not diverted.
3106 @result{}This text is not diverted.
3107 undivert(`1')
3108 @result{}
3109 @result{}This text is diverted.
3110 @result{}
3111 @end example
3113 Notice the last two blank lines.  One of them comes from the newline
3114 following @code{undivert}, the other from the newline that followed the
3115 @code{divert}!  A diversion often starts with a blank line like this.
3117 When diverted text is undiverted, it is @emph{not} reread by @code{m4},
3118 but rather copied directly to the current output, and it is therefore
3119 not an error to undivert into a diversion.  Undiverting the empty string
3120 is the same as specifying diversion 0; in either case nothing happens
3121 since the output has already been flushed.
3123 @example
3124 divert(`1')diverted text
3125 divert
3126 @result{}
3127 undivert()
3128 @result{}
3129 undivert(`0')
3130 @result{}
3131 undivert
3132 @result{}diverted text
3133 @result{}
3134 @end example
3136 When a diversion has been undiverted, the diverted text is discarded,
3137 and it is not possible to bring back diverted text more than once.
3139 @example
3140 divert(`1')
3141 This text is diverted first.
3142 divert(`0')undivert(`1')dnl
3143 @result{}
3144 @result{}This text is diverted first.
3145 undivert(`1')
3146 @result{}
3147 divert(`1')
3148 This text is also diverted but not appended.
3149 divert(`0')undivert(`1')dnl
3150 @result{}
3151 @result{}This text is also diverted but not appended.
3152 @end example
3154 Attempts to undivert the current diversion are silently ignored.  Thus,
3155 when the current diversion is not 0, the current diversion does not get
3156 rearranged among the other diversions.
3158 @example
3159 divert(`1')one
3160 divert(`2')two
3161 divert(`3')three
3162 divert(`2')undivert`'dnl
3163 divert`'undivert`'dnl
3164 @result{}two
3165 @result{}one
3166 @result{}three
3167 @end example
3169 @cindex @acronym{GNU} extensions
3170 @cindex file inclusion
3171 @cindex inclusion, of files
3172 @acronym{GNU} @code{m4} allows named files to be undiverted.  Given a
3173 non-numeric
3174 argument, the contents of the file named will be copied, uninterpreted, to
3175 the current output.  This complements the builtin @code{include}
3176 (@pxref{Include}).  To illustrate the difference, the file
3177 @file{examples/@/foo} contains the word @samp{bar}:
3179 @example
3180 define(`bar', `BAR')
3181 @result{}
3182 undivert(`foo')
3183 @result{}bar
3184 @result{}
3185 include(`foo')
3186 @result{}BAR
3187 @result{}
3188 @end example
3190 If the file is not found (or cannot be read), an error message is
3191 issued, and the expansion is void.
3193 @node Divnum
3194 @section Diversion numbers
3196 @cindex diversion numbers
3197 The current diversion is tracked by the builtin @code{divnum}:
3199 @deffn Builtin divnum
3200 Expands to the number of the current diversion.
3201 @end deffn
3203 @example
3204 Initial divnum
3205 @result{}Initial 0
3206 divert(`1')
3207 Diversion one: divnum
3208 divert(`2')
3209 Diversion two: divnum
3211 @result{}
3212 @result{}Diversion one: 1
3213 @result{}
3214 @result{}Diversion two: 2
3215 @end example
3217 @node Cleardiv
3218 @section Discarding diverted text
3220 @cindex discarding diverted text
3221 @cindex diverted text, discarding
3222 Often it is not known, when output is diverted, whether the diverted
3223 text is actually needed.  Since all non-empty diversion are brought back
3224 on the main output stream when the end of input is seen, a method of
3225 discarding a diversion is needed.  If all diversions should be
3226 discarded, the easiest is to end the input to @code{m4} with
3227 @samp{divert(`-1')} followed by an explicit @samp{undivert}:
3229 @example
3230 divert(`1')
3231 Diversion one: divnum
3232 divert(`2')
3233 Diversion two: divnum
3234 divert(`-1')
3235 undivert
3237 @end example
3239 @noindent
3240 No output is produced at all.
3242 Clearing selected diversions can be done with the following macro:
3244 @deffn Composite cleardivert (@ovar{diversion}@dots{})
3245 Discard the contents of each listed diversion.
3246 @end deffn
3248 @example
3249 define(`cleardivert',
3250 `pushdef(`_n', divnum)divert(`-1')undivert($@@)divert(_n)popdef(`_n')')
3251 @result{}
3252 @end example
3254 It is called just like @code{undivert}, but the effect is to clear the
3255 diversions, given by the arguments.  (This macro has a nasty bug!  You
3256 should try to see if you can find it and correct it.  @pxref{Answers})
3258 @node Text handling
3259 @chapter Macros for text handling
3261 There are a number of builtins in @code{m4} for manipulating text in
3262 various ways, extracting substrings, searching, substituting, and so on.
3264 @menu
3265 * Len::                         Calculating length of strings
3266 * Index macro::                 Searching for substrings
3267 * Regexp::                      Searching for regular expressions
3268 * Substr::                      Extracting substrings
3269 * Translit::                    Translating characters
3270 * Patsubst::                    Substituting text by regular expression
3271 * Format::                      Formatting strings (printf-like)
3272 @end menu
3274 @node Len
3275 @section Calculating length of strings
3277 @cindex length of strings
3278 @cindex strings, length of
3279 The length of a string can be calculated by @code{len}:
3281 @deffn Builtin len (@var{string})
3282 Expands to the length of @var{string}, as a decimal number.
3284 The macro @code{len} is recognized only with parameters.
3285 @end deffn
3287 @example
3288 len()
3289 @result{}0
3290 len(`abcdef')
3291 @result{}6
3292 @end example
3294 @node Index macro
3295 @section Searching for substrings
3297 Searching for substrings is done with @code{index}:
3299 @deffn Builtin index (@var{string}, @var{substring})
3300 Expands to the index of the first occurrence of @var{substring} in
3301 @var{string}.  The first character in @var{string} has index 0.  If
3302 @var{substring} does not occur in @var{string}, @code{index} expands to
3303 @samp{-1}.
3305 The macro @code{index} is recognized only with parameters.
3306 @end deffn
3308 @example
3309 index(`gnus, gnats, and armadillos', `nat')
3310 @result{}7
3311 index(`gnus, gnats, and armadillos', `dag')
3312 @result{}-1
3313 @end example
3315 @node Regexp
3316 @section Searching for regular expressions
3318 @cindex regular expressions
3319 @cindex @acronym{GNU} extensions
3320 Searching for regular expressions is done with the builtin
3321 @code{regexp}:
3323 @deffn Builtin regexp (@var{string}, @var{regexp}, @ovar{replacement})
3324 Searches for @var{regexp} in @var{string}.  The syntax for regular
3325 expressions is the same as in @acronym{GNU} Emacs.
3326 @ifnothtml
3327 @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
3328 Manual}.
3329 @end ifnothtml
3330 @ifhtml
3332 @uref{http://www.gnu.org/@/software/@/emacs/@/manual/@/emacs.html#Regexps,
3333 Syntax of Regular Expressions} in the @acronym{GNU} Emacs Manual.
3334 @end ifhtml
3336 If @var{replacement} is omitted, @code{regexp} expands to the index of
3337 the first match of @var{regexp} in @var{string}.  If @var{regexp} does
3338 not match anywhere in @var{string}, it expands to -1.
3340 If @var{replacement} is supplied, and there was a match, @code{regexp}
3341 changes the expansion to this argument, with @samp{\@var{n}} substituted
3342 by the text matched by the @var{n}th parenthesized sub-expression of
3343 @var{regexp}, up to nine sub-expressions.  The escape @samp{\&} is
3344 replaced by the text of the entire regular expression matched.  For
3345 all other characters, @samp{\} treats the next character literally.  A
3346 warning is issued if there were fewer sub-expressions than the
3347 @samp{\@var{n}} requested, or if there is a trailing @samp{\}.  If there
3348 was no match, @code{regexp} expands to the empty string.
3350 The macro @code{regexp} is recognized only with parameters.
3351 @end deffn
3353 @example
3354 regexp(`GNUs not Unix', `\<[a-z]\w+')
3355 @result{}5
3356 regexp(`GNUs not Unix', `\<Q\w*')
3357 @result{}-1
3358 regexp(`GNUs not Unix', `\w\(\w+\)$', `*** \& *** \1 ***')
3359 @result{}*** Unix *** nix ***
3360 regexp(`GNUs not Unix', `\<Q\w*', `*** \& *** \1 ***')
3361 @result{}
3362 @end example
3364 Here are some more examples on the handling of backslash:
3366 @example
3367 regexp(`abc', `\(b\)', `\\\10\a')
3368 @result{}\b0a
3369 regexp(`abc', `b', `\1\')
3370 @error{}m4:stdin:2: Warning: sub-expression 1 not present
3371 @error{}m4:stdin:2: Warning: trailing \ ignored in replacement
3372 @result{}
3373 @end example
3375 @node Substr
3376 @section Extracting substrings
3378 @cindex extracting substrings
3379 @cindex substrings, extracting
3380 Substrings are extracted with @code{substr}:
3382 @deffn Builtin substr (@var{string}, @var{from}, @ovar{length})
3383 Expands to the substring of @var{string}, which starts at index
3384 @var{from}, and extends for @var{length} characters, or to the end of
3385 @var{string}, if @var{length} is omitted.  The starting index of a string
3386 is always 0.  The expansion is empty if there is an error parsing
3387 @var{from} or @var{length}, if @var{from} is beyond the end of
3388 @var{string}, or if @var{length} is negative.
3390 The macro @code{substr} is recognized only with parameters.
3391 @end deffn
3393 @example
3394 substr(`gnus, gnats, and armadillos', `6')
3395 @result{}gnats, and armadillos
3396 substr(`gnus, gnats, and armadillos', `6', `5')
3397 @result{}gnats
3398 @end example
3400 @node Translit
3401 @section Translating characters
3403 @cindex translating characters
3404 @cindex characters, translating
3405 Character translation is done with @code{translit}:
3407 @deffn Builtin translit (@var{string}, @var{chars}, @ovar{replacement})
3408 Expands to @var{string}, with each character that occurs in
3409 @var{chars} translated into the character from @var{replacement} with
3410 the same index.
3412 If @var{replacement} is shorter than @var{chars}, the excess characters
3413 are deleted from the expansion.  If @var{replacement} is omitted, all
3414 characters in @var{string} that are present in @var{chars} are deleted
3415 from the expansion.
3417 As a @acronym{GNU} extension, both @var{chars} and @var{replacement} can
3418 contain character-ranges,
3419 e.g., @samp{a-z} (meaning all lowercase letters) or @samp{0-9} (meaning
3420 all digits).  To include a dash @samp{-} in @var{chars} or
3421 @var{replacement}, place it first or last.
3423 It is not an error for the last character in the range to be `larger'
3424 than the first.  In that case, the range runs backwards, i.e.,
3425 @samp{9-0} means the string @samp{9876543210}.
3427 The macro @code{translit} is recognized only with parameters.
3428 @end deffn
3430 @example
3431 translit(`GNUs not Unix', `A-Z')
3432 @result{}s not nix
3433 translit(`GNUs not Unix', `a-z', `A-Z')
3434 @result{}GNUS NOT UNIX
3435 translit(`GNUs not Unix', `A-Z', `z-a')
3436 @result{}tmfs not fnix
3437 @end example
3439 The first example deletes all uppercase letters, the second converts
3440 lowercase to uppercase, and the third `mirrors' all uppercase letters,
3441 while converting them to lowercase.  The two first cases are by far the
3442 most common.
3444 @node Patsubst
3445 @section Substituting text by regular expression
3447 @cindex regular expressions
3448 @cindex pattern substitution
3449 @cindex substitution by regular expression
3450 @cindex @acronym{GNU} extensions
3451 Global substitution in a string is done by @code{patsubst}:
3453 @deffn Builtin patsubst (@var{string}, @var{regexp}, @ovar{replacement})
3454 Searches @var{string} for matches of @var{regexp}, and substitutes
3455 @var{replacement} for each match.  The syntax for regular expressions
3456 is the same as in @acronym{GNU} Emacs (@pxref{Regexp}).
3458 The parts of @var{string} that are not covered by any match of
3459 @var{regexp} are copied to the expansion.  Whenever a match is found, the
3460 search proceeds from the end of the match, so a character from
3461 @var{string} will never be substituted twice.  If @var{regexp} matches a
3462 string of zero length, the start position for the search is incremented,
3463 to avoid infinite loops.
3465 When a replacement is to be made, @var{replacement} is inserted into
3466 the expansion, with @samp{\@var{n}} substituted by the text matched by
3467 the @var{n}th parenthesized sub-expression of @var{patsubst}, for up to
3468 nine sub-expressions.  The escape @samp{\&} is replaced by the text of
3469 the entire regular expression matched.  For all other characters,
3470 @samp{\} treats the next character literally.  A warning is issued if
3471 there were fewer sub-expressions than the @samp{\@var{n}} requested, or
3472 if there is a trailing @samp{\}.
3474 The @var{replacement} argument can be omitted, in which case the text
3475 matched by @var{regexp} is deleted.
3477 The macro @code{patsubst} is recognized only with parameters.
3478 @end deffn
3480 @example
3481 patsubst(`GNUs not Unix', `^', `OBS: ')
3482 @result{}OBS: GNUs not Unix
3483 patsubst(`GNUs not Unix', `\<', `OBS: ')
3484 @result{}OBS: GNUs OBS: not OBS: Unix
3485 patsubst(`GNUs not Unix', `\w*', `(\&)')
3486 @result{}(GNUs)() (not)() (Unix)
3487 patsubst(`GNUs not Unix', `\w+', `(\&)')
3488 @result{}(GNUs) (not) (Unix)
3489 patsubst(`GNUs not Unix', `[A-Z][a-z]+')
3490 @result{}GN not @comment
3491 patsubst(`GNUs not Unix', `not', `NOT\')
3492 @error{}m4:stdin:6: Warning: trailing \ ignored in replacement
3493 @result{}GNUs NOT Unix
3494 @end example
3496 Here is a slightly more realistic example, which capitalizes individual
3497 word or whole sentences, by substituting calls of the macros
3498 @code{upcase} and @code{downcase} into the strings.
3500 @deffn Composite upcase (@var{text})
3501 @deffnx Composite downcase (@var{text})
3502 @deffnx Composite capitalize (@var{text})
3503 Expand to @var{text}, but with capitalization changed: @code{upcase}
3504 changes all letters to upper case, @code{downcase} changes all letters
3505 to lower case, and @code{capitalize} changes the first character of each
3506 word to upper case and the remaining characters to lower case.
3507 @end deffn
3509 @example
3510 define(`upcase', `translit(`$*', `a-z', `A-Z')')dnl
3511 define(`downcase', `translit(`$*', `A-Z', `a-z')')dnl
3512 define(`capitalize1',
3513        `regexp(`$1', `^\(\w\)\(\w*\)',
3514                `upcase(`\1')`'downcase(`\2')')')dnl
3515 define(`capitalize',
3516        `patsubst(`$1', `\w+', `capitalize1(`\&')')')dnl
3517 capitalize(`GNUs not Unix')
3518 @result{}Gnus Not Unix
3519 @end example
3521 While @code{regexp} replaces the whole input with the replacement as
3522 soon as there is a match, @code{patsubst} replaces each
3523 @emph{occurrence} of a match and preserves non-matching pieces:
3525 @example
3526 define(`patreg',
3527 `patsubst($@@)
3528 regexp($@@)')dnl
3529 patreg(`bar foo baz Foo', `foo\|Foo', `FOO')
3530 @result{}bar FOO baz FOO
3531 @result{}FOO
3532 patreg(`aba abb 121', `\(.\)\(.\)\1', `\2\1\2')
3533 @result{}bab abb 212
3534 @result{}bab
3535 @end example
3537 @node Format
3538 @section Formatted output
3540 @cindex formatted output
3541 @cindex output, formatted
3542 @cindex @acronym{GNU} extensions
3543 Formatted output can be made with @code{format}:
3545 @deffn Builtin format (@var{format-string}, @dots{})
3546 Works much like the C function @code{printf}.  The first argument
3547 @var{format-string} can contain @samp{%} specifications which are
3548 satisfied by additional arguments, and the expansion of @code{format} is
3549 the formatted string.
3551 The macro @code{format} is recognized only with parameters.
3552 @end deffn
3554 Its use is best described by a few examples:
3556 @example
3557 define(`foo', `The brown fox jumped over the lazy dog')
3558 @result{}
3559 format(`The string "%s" uses %d characters', foo, len(foo))
3560 @result{}The string "The brown fox jumped over the lazy dog" uses 38 characters
3561 format(`%.0f', `56789.9876')
3562 @result{}56790
3563 len(format(`%-*X', `300', `1'))
3564 @result{}300
3565 @end example
3567 Using the @code{forloop} macro defined in @xref{Loops}, this
3568 example shows how @code{format} can be used to produce tabular output.
3570 @example
3571 include(`forloop.m4')
3572 @result{}
3573 forloop(`i', `1', `10', `format(`%6d squared is %10d
3574 ', i, eval(i**2))')
3575 @result{}     1 squared is          1
3576 @result{}     2 squared is          4
3577 @result{}     3 squared is          9
3578 @result{}     4 squared is         16
3579 @result{}     5 squared is         25
3580 @result{}     6 squared is         36
3581 @result{}     7 squared is         49
3582 @result{}     8 squared is         64
3583 @result{}     9 squared is         81
3584 @result{}    10 squared is        100
3585 @result{}
3586 @end example
3588 The builtin @code{format} is modeled after the ANSI C @samp{printf}
3589 function, and supports these @samp{%} specifiers: @samp{c},
3590 @samp{s}, @samp{d}, @samp{o}, @samp{x}, @samp{X}, @samp{u}, @samp{e},
3591 @samp{E}, @samp{f}, @samp{F}, @samp{g}, @samp{G}, and @samp{%}; it
3592 supports field widths and precisions, and the
3593 modifiers @samp{+}, @samp{-}, @samp{@w{ }}, @samp{0}, @samp{#}, @samp{h} and
3594 @samp{l}.  For more details on the functioning of @code{printf}, see the
3595 C Library Manual.
3597 For now, unrecognized specifiers are silently ignored, but it is
3598 anticipated that a future release of @acronym{GNU} @code{m4} will support more
3599 specifiers, and give warnings when problems are encountered.  Likewise,
3600 escape sequences are not yet recognized.
3602 @node Arithmetic
3603 @chapter Macros for doing arithmetic
3605 @cindex arithmetic
3606 @cindex integer arithmetic
3607 Integer arithmetic is included in @code{m4}, with a C-like syntax.  As
3608 convenient shorthands, there are builtins for simple increment and
3609 decrement operations.
3611 @menu
3612 * Incr::                        Decrement and increment operators
3613 * Eval::                        Evaluating integer expressions
3614 @end menu
3616 @node Incr
3617 @section Decrement and increment operators
3619 @cindex decrement operator
3620 @cindex increment operator
3621 Increment and decrement of integers are supported using the builtins
3622 @code{incr} and @code{decr}:
3624 @deffn Builtin incr (@var{number})
3625 @deffnx Builtin decr (@var{number})
3626 Expand to the numerical value of @var{number}, incremented
3627 or decremented, respectively, by one.  Except for the empty string, the
3628 expansion is empty if @var{number} could not be parsed.
3630 The macros @code{incr} and @code{decr} are recognized only with
3631 parameters.
3632 @end deffn
3634 @example
3635 incr(`4')
3636 @result{}5
3637 decr(`7')
3638 @result{}6
3639 incr()
3640 @error{}m4:stdin:3: empty string treated as 0 in builtin `incr'
3641 @result{}1
3642 decr()
3643 @error{}m4:stdin:4: empty string treated as 0 in builtin `decr'
3644 @result{}-1
3645 @end example
3647 @node Eval
3648 @section Evaluating integer expressions
3650 @cindex integer expression evaluation
3651 @cindex evaluation, of integer expressions
3652 @cindex expressions, evaluation of integer
3653 Integer expressions are evaluated with @code{eval}:
3655 @deffn Builtin eval (@var{expression}, @dvar{radix, 10}, @ovar{width})
3656 Expands to the value of @var{expression}.  The expansion is empty
3657 if an error is encountered while parsing the arguments.  If specified,
3658 @var{radix} and @var{width} control the format of the output.
3660 The macro @code{eval} is recognized only with parameters.
3661 @end deffn
3663 Expressions can contain the following operators, listed in order of
3664 decreasing precedence.
3666 @table @code
3667 @item + -
3668 Unary plus and minus
3669 @item **
3670 Exponentiation
3671 @item *  /  %
3672 Multiplication, division and modulo
3673 @item +  -
3674 Addition and subtraction
3675 @item <<  >>
3676 Shift left or right
3677 @item ==  !=  >  >=  <  <=
3678 Relational operators
3679 @item !
3680 Logical negation
3681 @item ~
3682 Bitwise negation
3683 @item &
3684 Bitwise and
3685 @item ^
3686 Bitwise exclusive-or
3687 @item |
3688 Bitwise or
3689 @item &&
3690 Logical and
3691 @item ||
3692 Logical or
3693 @end table
3695 All operators, except exponentiation, are left associative.
3697 Note that some older @code{m4} implementations use @samp{^} as an
3698 alternate operator for exponentiation, although @acronym{POSIX} requires
3699 the C behavior of bitwise exclusive-or.  On the other hand, the
3700 precedence of @samp{~} and @samp{!} are different in @acronym{GNU}
3701 @code{m4} than
3702 they are in C, matching the precedence in traditional @code{m4}
3703 implementations.  This behavior is likely to change in a future
3704 version to match @acronym{POSIX}, so use parentheses to force the
3705 desired precedence.
3707 Within @var{expression}, (but not @var{radix} or @var{width}),
3708 numbers without a special prefix are decimal.  A simple @samp{0}
3709 prefix introduces an octal number.  @samp{0x} introduces a hexadecimal
3710 number.  @samp{0b} introduces a binary number.  @samp{0r} introduces a
3711 number expressed in any radix between 1 and 36: the prefix should be
3712 immediately followed by the decimal expression of the radix, a colon,
3713 then the digits making the number.  For radix 1, leading zeros are
3714 ignored and all remaining digits must be @samp{1}; for all other
3715 radices, the digits are
3716 @samp{0}, @samp{1}, @samp{2}, @dots{}.  Beyond @samp{9}, the digits are
3717 @samp{a}, @samp{b} @dots{} up to @samp{z}.  Lower and upper case letters
3718 can be used interchangeably in numbers prefixes and as number digits.
3720 Parentheses may be used to group subexpressions whenever needed.  For the
3721 relational operators, a true relation returns @code{1}, and a false
3722 relation return @code{0}.
3724 Here are a few examples of use of @code{eval}.
3726 @example
3727 eval(`-3 * 5')
3728 @result{}-15
3729 eval(index(`Hello world', `llo') >= 0)
3730 @result{}1
3731 eval(`0r1:0111 + 0b100 + 0r3:12')
3732 @result{}12
3733 define(`square', `eval(`('$1`)**2')')
3734 @result{}
3735 square(`9')
3736 @result{}81
3737 square(square(`5')`+1')
3738 @result{}676
3739 define(`foo', `666')
3740 @result{}
3741 eval(`foo/6')
3742 @error{}m4:stdin:8: bad expression in eval: foo/6
3743 @result{}
3744 eval(foo/6)
3745 @result{}111
3746 @end example
3748 As the last two lines show, @code{eval} does not handle macro
3749 names, even if they expand to a valid expression (or part of a valid
3750 expression).  Therefore all macros must be expanded before they are
3751 passed to @code{eval}.
3753 All evaluation is done with 32-bit signed integers, assuming
3754 2's-complement with wrap-around.  The shift operators are defined in
3755 @acronym{GNU}
3756 @code{m4} by doing an implicit bit-wise and of the right-hand operand
3757 with 0x1f, and sign-extension with right shift.
3759 @example
3760 eval(0x80000000 / -1)
3761 @result{}-2147483648
3762 eval(0x80000000 % -1)
3763 @result{}0
3764 eval(0x7fffffff)
3765 @result{}2147483647
3766 incr(eval(0x7fffffff))
3767 @result{}-2147483648
3768 eval(-4 >> 33)
3769 @result{}-2
3770 @end example
3772 If @var{radix} is specified, it specifies the radix to be used in the
3773 expansion.  The default radix is 10; this is also the case if
3774 @var{radix} is the empty string.  It is an error if the radix is outside
3775 the range of 1 through 36, inclusive.  The result of @code{eval} is
3776 always taken to be signed.  No radix prefix is output, and for radices
3777 greater than 10, the digits are lower case.  The @var{width} argument
3778 specifies the minimum output width, excluding any negative sign.  The
3779 result is zero-padded to extend the expansion to the requested width.
3780 It is an error if the width is negative.  On error, the expansion of
3781 @code{eval} is empty.
3783 @example
3784 eval(`666', `10')
3785 @result{}666
3786 eval(`666', `11')
3787 @result{}556
3788 eval(`666', `6')
3789 @result{}3030
3790 eval(`666', `6', `10')
3791 @result{}0000003030
3792 eval(`-666', `6', `10')
3793 @result{}-0000003030
3794 eval(`10', `', `0')
3795 @result{}10
3796 `0r1:'eval(`10', `1', `11')
3797 @result{}0r1:01111111111
3798 eval(`10', `16')
3799 @result{}a
3800 @end example
3802 @node Shell commands
3803 @chapter Running shell commands
3805 @cindex executing UNIX commands
3806 @cindex running UNIX commands
3807 @cindex UNIX commands, running
3808 @cindex commands, running UNIX
3809 @cindex executing shell commands
3810 @cindex running shell commands
3811 @cindex shell commands, running
3812 @cindex commands, running shell
3813 There are a few builtin macros in @code{m4} that allow you to run shell
3814 commands from within @code{m4}.
3816 Note that the definition of a valid shell command is system dependent.
3817 On UNIX systems, this is the typical @code{/bin/sh}.  But on other
3818 systems, such as native Windows, the shell has a different syntax of
3819 commands that it understands.  Some examples in this chapter assume
3820 @code{/bin/sh}, and also demonstrate how to quit early with a known
3821 exit value if this is not the case.
3823 @menu
3824 * Platform macros::             Determining the platform
3825 * Syscmd::                      Executing simple commands
3826 * Esyscmd::                     Reading the output of commands
3827 * Sysval::                      Exit status
3828 * Maketemp::                    Making names for temporary files
3829 @end menu
3831 @node Platform macros
3832 @section Determining the platform
3834 @cindex platform macros
3835 Sometimes it is desirable for an input file to know which
3836 platform @code{m4} is running on.  @acronym{GNU} @code{m4} provides several
3837 macros that are predefined to expand to the empty string; checking for
3838 their existence will confirm platform details.
3840 @deffn {Optional builtin} __gnu__
3841 @deffnx {Optional builtin} __os2__
3842 @deffnx {Optional builtin} os2
3843 @deffnx {Optional builtin} __unix__
3844 @deffnx {Optional builtin} unix
3845 @deffnx {Optional builtin} __windows__
3846 @deffnx {Optional builtin} windows
3847 Each of these macros is conditionally defined as needed to describe the
3848 environment of @code{m4}.  If defined, each macro expands to the empty
3849 string.
3850 @end deffn
3852 When @acronym{GNU} extensions are in effect (that is, when you did not use the
3853 @option{-G} option, @pxref{Invoking m4}), @acronym{GNU} @code{m4} will
3854 define the macro @code{__gnu__} to expand to the empty string.
3856 @example
3857 __gnu__
3858 @result{}
3859 ifdef(`__gnu__', `Extensions are active')
3860 @result{}Extensions are active
3861 @end example
3863 @cindex platform macro
3864 On UNIX systems, @acronym{GNU} @code{m4} will define @code{__unix__} by
3865 default, or @code{unix} when the @option{-G} option is specified.
3867 On native Windows systems, @acronym{GNU} @code{m4} will define
3868 @code{__windows__} by default, or @code{windows} when the @option{-G}
3869 option is specified.
3871 On OS/2 systems, @acronym{GNU} @code{m4} will define @code{__os2__} by
3872 default, or @code{os2} when the @option{-G} option is specified.
3874 If @acronym{GNU} @code{m4} does not provide a platform macro for your system,
3875 please report that as a bug.
3877 @example
3878 define(`provided', `0')
3879 @result{}
3880 ifdef(`__unix__', `define(`provided', incr(provided))')
3881 @result{}
3882 ifdef(`__windows__', `define(`provided', incr(provided))')
3883 @result{}
3884 ifdef(`__os2__', `define(`provided', incr(provided))')
3885 @result{}
3886 provided
3887 @result{}1
3888 @end example
3890 @node Syscmd
3891 @section Executing simple commands
3893 Any shell command can be executed, using @code{syscmd}:
3895 @deffn Builtin syscmd (@var{shell-command})
3896 Executes @var{shell-command} as a shell command.
3898 The expansion of @code{syscmd} is void, @emph{not} the output from
3899 @var{shell-command}!  Output or error messages from @var{shell-command}
3900 are not read by @code{m4}.  @xref{Esyscmd}, if you need to process the
3901 command output.
3903 Prior to executing the command, @code{m4} flushes its output buffers.
3904 The default standard input, output and error of @var{shell-command} are
3905 the same as those of @code{m4}.
3907 The macro @code{syscmd} is recognized only with parameters.
3908 @end deffn
3910 @example
3911 define(`foo', `FOO')
3912 @result{}
3913 syscmd(`echo foo')
3914 @result{}foo
3915 @result{}
3916 @end example
3918 Note how the expansion of @code{syscmd} keeps the trailing newline of
3919 the command, as well as using the newline that appeared after the macro.
3921 @node Esyscmd
3922 @section Reading the output of commands
3924 @cindex @acronym{GNU} extensions
3925 If you want @code{m4} to read the output of a shell command, use
3926 @code{esyscmd}:
3928 @deffn Builtin esyscmd (@var{shell-command})
3929 Expands to the standard output of the shell command
3930 @var{shell-command}.
3932 Prior to executing the command, @code{m4} flushes its output buffers.
3933 The default standard input and error output of @var{shell-command} are
3934 the same as those of @code{m4}.  The error output of @var{shell-command}
3935 is not a part of the expansion: it will appear along with the error
3936 output of @code{m4}.
3938 The macro @code{esyscmd} is recognized only with parameters.
3939 @end deffn
3941 @example
3942 define(`foo', `FOO')
3943 @result{}
3944 esyscmd(`echo foo')
3945 @result{}FOO
3946 @result{}
3947 @end example
3949 Note how the expansion of @code{esyscmd} keeps the trailing newline of
3950 the command, as well as using the newline that appeared after the macro.
3952 @node Sysval
3953 @section Exit status
3955 @cindex UNIX commands, exit status from
3956 @cindex exit status from shell commands
3957 @cindex shell commands, exit status from
3958 @cindex commands, exit status from shell
3959 @cindex status of shell commands
3960 To see whether a shell command succeeded, use @code{sysval}:
3962 @deffn Builtin sysval
3963 Expands to the exit status of the last shell command run with
3964 @code{syscmd} or @code{esyscmd}.  Expands to 0 if no command has been
3965 run yet.
3966 @end deffn
3968 @example
3969 syscmd(`false')
3970 @result{}
3971 ifelse(sysval, `0', `zero', `non-zero')
3972 @result{}non-zero
3973 syscmd(`exit 2')
3974 @result{}
3975 sysval
3976 @result{}2
3977 syscmd(`true')
3978 @result{}
3979 sysval
3980 @result{}0
3981 esyscmd(`false')
3982 @result{}
3983 ifelse(sysval, `0', `zero', `non-zero')
3984 @result{}non-zero
3985 esyscmd(`exit 2')
3986 @result{}
3987 sysval
3988 @result{}2
3989 esyscmd(`true')
3990 @result{}
3991 sysval
3992 @result{}0
3993 @end example
3995 @command{sysval} results in 127 if there was a problem executing the
3996 command, for example, if the system-imposed argument length is exceeded,
3997 or if there were not enough resources to fork.  It is not possible to
3998 distinguish between failed execution and successful execution that had
3999 an exit status of 127.
4001 On UNIX platforms, where it is possible to detect when command execution
4002 is terminated by a signal, rather than a normal exit, the result is the
4003 signal number shifted left by eight bits.
4005 @comment This test has difficulties being portable, even on platforms
4006 @comment where syscmd invokes /bin/sh.  Kill is not portable with signal
4007 @comment names.  According to autoconf, the only portable signal numbers
4008 @comment are 1 (HUP), 2 (INT), 9 (KILL), 13 (PIPE) and 15 (TERM).  But
4009 @comment all shells handle SIGINT, and ksh handles HUP (as in, the shell
4010 @comment exits normally rather than letting the signal terminate it).
4011 @comment Also, TERM is flaky, as it can also kill the running m4 on
4012 @comment systems where /bin/sh does not create its own process group.
4013 @comment That leaves KILL and PIPE as the two signals tested.
4014 @example
4015 dnl This test assumes kill is a shell builtin, and that signals are
4016 dnl recognizable.
4017 ifdef(`__unix__', , `m4exit(`77')')dnl
4018 syscmd(`kill -13 $$')
4019 @result{}
4020 sysval
4021 @result{}3328
4022 esyscmd(`kill -9 $$')
4023 @result{}
4024 sysval
4025 @result{}2304
4026 @end example
4028 @node Maketemp
4029 @section Making names for temporary files
4031 @cindex temporary file names
4032 @cindex files, names of temporary
4033 Commands specified to @code{syscmd} or @code{esyscmd} might need a
4034 temporary file, for output or for some other purpose.
4035 There is a builtin macro, @code{maketemp}, for making temporary file
4036 names:
4038 @deffn Builtin maketemp (@var{template})
4039 Expands to a name of a new, empty file, made from the string
4040 @var{template}, which should end with the string @samp{XXXXXX}.  The six
4041 @code{X} characters are then replaced with random data, in order to make
4042 the file name unique.
4044 The macro @code{maketemp} is recognized only with parameters.
4045 @end deffn
4047 @comment ignore
4048 @example
4049 maketemp(`/tmp/fooXXXXXX')
4050 @result{}/tmp/fooa07346
4051 @end example
4053 @ignore
4054 @c FIXME: POSIX requires maketemp to replace the trailing XXX with the
4055 @c process id, without creating the file; meaning you only get one
4056 @c string no matter how many times you use maketemp.  Instead, we treat
4057 @c it like mkstemp(), and create a unique file every invocation.
4059 @c This test makes sure maketemp gets testsuite coverage, but is
4060 @c somewhat complex for use in the manual.
4061 @example
4062 dnl This test assumes /tmp is a valid directory name, which is not true
4063 dnl for native Windows.
4064 ifdef(`__unix__', , `m4exit(`77')')dnl
4065 define(`file1', maketemp(`/tmp/fooXXXXXX'))dnl
4066 define(`file2', maketemp(`/tmp/fooXXXXXX'))dnl
4067 ifelse(file1, file2, `same', `different')
4068 @result{}different
4069 syscmd(`rm 'file1 file2)
4070 @result{}
4071 sysval
4072 @result{}0
4073 @end example
4074 @end ignore
4076 @node Miscellaneous
4077 @chapter Miscellaneous builtin macros
4079 This chapter describes various builtins, that do not really belong in
4080 any of the previous chapters.
4082 @menu
4083 * Errprint::                    Printing error messages
4084 * Location::                    Printing current location
4085 * M4exit::                      Exiting from m4
4086 @end menu
4088 @node Errprint
4089 @section Printing error messages
4091 @cindex printing error messages
4092 @cindex error messages, printing
4093 @cindex messages, printing error
4094 You can print error messages using @code{errprint}:
4096 @deffn Builtin errprint (@var{message}, @dots{})
4097 Prints @var{message} and the rest of the arguments on the
4098 standard error output, separated by spaces.
4100 The expansion of @code{errprint} is void.
4101 The macro @code{errprint} is recognized only with parameters.
4102 @end deffn
4104 @example
4105 errprint(`Invalid arguments to forloop
4107 @error{}Invalid arguments to forloop
4108 @result{}
4109 @end example
4111 A trailing newline is @emph{not} printed automatically, so it must be
4112 supplied as part of the argument, as in the example.  BSD
4113 implementations of @code{m4} do append a trailing newline on each
4114 @code{errprint} call, while some other implementations only print the
4115 first argument.
4117 @node Location
4118 @section Printing current location
4120 To make it possible to specify the location of an error, three
4121 utility builtins exist:
4123 @deffn Builtin __file__
4124 @deffnx Builtin __line__
4125 @deffnx Builtin __program__
4126 Expand to the quoted name of the current input file, the
4127 current input line number in that file, and the quoted name of the
4128 current invocation of @code{m4}.
4129 @end deffn
4131 @example
4132 errprint(__program__:__file__:__line__: `input error
4134 @error{}m4:stdin:1: input error
4135 @result{}
4136 @end example
4138 Line numbers start at 1 for each file.  If the file was found due to the
4139 @option{-I} option or @env{M4PATH} environment variable, that is
4140 reflected in the file name.  The syncline option (@option{-s},
4141 @pxref{Invoking m4}), and the
4142 @samp{f} and @samp{l} flags of @code{debugmode} (@pxref{Debug Levels}),
4143 also use this notion of current file and line.  Redefining the three
4144 location macros has no effect on syncline, debug, or warning message
4145 output.  Assume this example is run in the
4146 @file{checks} directory of the @acronym{GNU} M4 package, using
4147 @samp{--include=../examples} in the command line to find the file
4148 @file{incl.m4} mentioned earlier:
4150 @example
4151 define(`foo', ``$0' called at __file__:__line__')
4152 @result{}
4154 @result{}foo called at stdin:2
4155 include(`incl.m4')
4156 @result{}Include file start
4157 @result{}foo called at ../examples/incl.m4:2
4158 @result{}Include file end
4159 @result{}
4160 @end example
4162 Currently, all text wrapped with @code{m4wrap} (@pxref{M4wrap}) behaves
4163 as though it came from line 0 of the file ``''.  It is hoped that a
4164 future release of @code{m4} can overcome this limitation and remember
4165 which file invoked the call to @code{m4wrap}.
4167 The @code{__program__} macro behaves like @samp{$0} in shell
4168 terminology.  If you invoke @code{m4} through an absolute path or a link
4169 with a different spelling, rather than by relying on a @env{PATH} search
4170 for plain @samp{m4}, it will affect how @code{__program__} expands.  The
4171 intent is that you can use it to produce error messages with the same
4172 formatting that @code{m4} produces internally.  It can also be used
4173 within @code{syscmd} (@pxref{Syscmd}) to pick the same version of
4174 @code{m4} that is currently running, rather than whatever version of
4175 @code{m4} happens to be first in @env{PATH}.
4177 @node M4exit
4178 @section Exiting from @code{m4}
4180 @cindex exiting from @code{m4}
4181 @cindex status, setting @code{m4} exit
4182 If you need to exit from @code{m4} before the entire input has been
4183 read, you can use @code{m4exit}:
4185 @deffn Builtin m4exit (@dvar{code, 0})
4186 Causes @code{m4} to exit, with exit status @var{code}.  If @var{code} is
4187 left out, the exit status is zero.  If @var{code} cannot be parsed, or
4188 is outside the range of 0 to 255, the exit status is one.  No further
4189 input is read, and all wrapped and diverted text is discarded.
4190 @end deffn
4192 A common use of this is to abort processing:
4194 @deffn Composite fatal_error (@var{message})
4195 Abort processing with an error message and non-zero status.  Prefix
4196 @var{message} with details about where the error occurred, and print the
4197 resulting string to standard error.
4198 @end deffn
4200 @example
4201 define(`fatal_error',
4202        `errprint(__program__:__file__:__line__`: fatal error: $*
4203 ')m4exit(`1')')
4204 @result{}
4205 fatal_error(`this is a BAD one, buster')
4206 @error{}m4:stdin:4: fatal error: this is a BAD one, buster
4207 @end example
4209 After this macro call, @code{m4} will exit with exit status 1.  This macro
4210 is only intended for error exits, since the normal exit procedures are
4211 not followed, e.g., diverted text is not undiverted, and saved text
4212 (@pxref{M4wrap}) is not reread.  (This macro has a subtle bug, when
4213 invoked from wrapped text.  You should try to see if you can find it and
4214 correct it.  @pxref{Answers})
4216 @example
4217 m4wrap(`This text is lost to `m4exit'.')
4218 @result{}
4219 divert(`1') And so is this.
4220 divert
4221 @result{}
4222 m4exit
4223 @end example
4225 Note that it is still possible for the exit status to be different than
4226 what was requested by @code{m4exit}.  If @code{m4} detects some other
4227 error, such as a write error on standard out, the exit status will be
4228 non-zero even if @code{m4exit} requested zero.
4230 @node Frozen files
4231 @chapter Fast loading of frozen state
4233 Some bigger @code{m4} applications may be built over a common base
4234 containing hundreds of definitions and other costly initializations.
4235 Usually, the common base is kept in one or more declarative files,
4236 which files are listed on each @code{m4} invocation prior to the
4237 user's input file, or else each input file uses @code{include}.
4239 Reading the common base of a big application, over and over again, may
4240 be time consuming.  @acronym{GNU} @code{m4} offers some machinery to speed up
4241 the start of an application using lengthy common bases.
4243 @menu
4244 * Using frozen files::          Using frozen files
4245 * Frozen file format::          Frozen file format
4246 @end menu
4248 @node Using frozen files
4249 @section Using frozen files
4250 @cindex fast loading of frozen files
4251 @cindex frozen files for fast loading
4252 @cindex initialization, frozen states
4253 @cindex dumping into frozen file
4254 @cindex reloading a frozen file
4255 @cindex @acronym{GNU} extensions
4256 Suppose a user has a library of @code{m4} initializations in
4257 @file{base.m4}, which is then used with multiple input files:
4259 @comment ignore
4260 @example
4261 m4 base.m4 input1.m4
4262 m4 base.m4 input2.m4
4263 m4 base.m4 input3.m4
4264 @end example
4266 Rather than spending time parsing the fixed contents of @file{base.m4}
4267 every time, the user might rather execute:
4269 @comment ignore
4270 @example
4271 m4 -F base.m4f base.m4
4272 @end example
4274 @noindent
4275 once, and further execute, as often as needed:
4277 @comment ignore
4278 @example
4279 m4 -R base.m4f input1.m4
4280 m4 -R base.m4f input2.m4
4281 m4 -R base.m4f input3.m4
4282 @end example
4284 @noindent
4285 with the varying input.  The first call, containing the @option{-F}
4286 option, only reads and executes file @file{base.m4}, defining
4287 various application macros and computing other initializations.
4288 Once the input file @file{base.m4} has been completely processed, @acronym{GNU}
4289 @code{m4} produces on @file{base.m4f} a @dfn{frozen} file, that is, a
4290 file which contains a kind of snapshot of the @code{m4} internal state.
4292 Later calls, containing the @option{-R} option, are able to reload
4293 the internal state of @code{m4}, from @file{base.m4f},
4294 @emph{prior} to reading any other input files.  This means
4295 instead of starting with a virgin copy of @code{m4}, input will be
4296 read after having effectively recovered the effect of a prior run.
4297 In our example, the effect is the same as if file @file{base.m4} has
4298 been read anew.  However, this effect is achieved a lot faster.
4300 Only one frozen file may be created or read in any one @code{m4}
4301 invocation.  It is not possible to recover two frozen files at once.
4302 However, frozen files may be updated incrementally, through using
4303 @option{-R} and @option{-F} options simultaneously.  For example, if
4304 some care is taken, the command:
4306 @comment ignore
4307 @example
4308 m4 file1.m4 file2.m4 file3.m4 file4.m4
4309 @end example
4311 @noindent
4312 could be broken down in the following sequence, accumulating the same
4313 output:
4315 @comment ignore
4316 @example
4317 m4 -F file1.m4f file1.m4
4318 m4 -R file1.m4f -F file2.m4f file2.m4
4319 m4 -R file2.m4f -F file3.m4f file3.m4
4320 m4 -R file3.m4f file4.m4
4321 @end example
4323 Some care is necessary because not every effort has been made for
4324 this to work in all cases.  In particular, the trace attribute of
4325 macros is not handled, nor the current setting of @code{changeword}.
4326 Currently, @code{m4wrap} and @code{sysval} also have problems.
4327 Also, interactions for some options of @code{m4}, being used in one call
4328 and not in the next, have not been fully analyzed yet.  On the other
4329 end, you may be confident that stacks of @code{pushdef} definitions
4330 are handled correctly, as well as undefined or renamed builtins, and
4331 changed strings for quotes or comments.  And future releases of
4332 @acronym{GNU} M4 will improve on the utility of frozen files.
4334 When an @code{m4} run is to be frozen, the automatic undiversion
4335 which takes place at end of execution is inhibited.  Instead, all
4336 positively numbered diversions are saved into the frozen file.
4337 The active diversion number is also transmitted.
4339 A frozen file to be reloaded need not reside in the current directory.
4340 It is looked up the same way as an @code{include} file (@pxref{Search
4341 Path}).
4343 If the frozen file was generated with a newer version of @code{m4}, and
4344 contains directives that an older @code{m4} cannot parse, attempting to
4345 load the frozen file with option @option{-R} will cause @code{m4} to
4346 exit with status 63 to indicate version mismatch.
4348 @node Frozen file format
4349 @section Frozen file format
4350 @cindex frozen file format
4351 @cindex file format, frozen file
4352 Frozen files are sharable across architectures.  It is safe to write
4353 a frozen file on one machine and read it on another, given that the
4354 second machine uses the same or newer version of @acronym{GNU} @code{m4}.
4355 It is conventional, but not required, to give a frozen file the suffix
4356 of @code{.m4f}.
4358 These are simple (editable) text files, made up of directives,
4359 each starting with a capital letter and ending with a newline
4360 (@key{NL}).  Wherever a directive is expected, the character
4361 @kbd{#} introduces a comment line; empty lines are also ignored if they
4362 are not part of an embedded string.
4363 In the following descriptions, each @var{len} refers to the length of
4364 the corresponding strings @var{str} in the next line of input.  Numbers
4365 are always expressed in decimal.  There are no escape characters.  The
4366 directives are:
4368 @table @code
4369 @item C @var{len1} , @var{len2} @key{NL} @var{str1} @var{str2} @key{NL}
4370 Uses @var{str1} and @var{str2} as the begin-comment and
4371 end-comment strings.  If omitted, then @samp{#} and @key{NL} are the
4372 comment delimiters.
4374 @item D @var{number}, @var{len} @key{NL} @var{str} @key{NL}
4375 Selects diversion @var{number}, making it current, then copy
4376 @var{str} in the current diversion.  @var{number} may be a negative
4377 number for a non-existing diversion.  To merely specify an active
4378 selection, use this command with an empty @var{str}.  With 0 as the
4379 diversion @var{number}, @var{str} will be issued on standard output
4380 at reload time.  @acronym{GNU} @code{m4} will not produce the @samp{D}
4381 directive with non-zero length for diversion 0, but this can be done
4382 with manual edits.  This directive may
4383 appear more than once for the same diversion, in which case the
4384 diversion is the concatenation of the various uses.  If omitted, then
4385 diversion 0 is current.
4387 @item F @var{len1} , @var{len2} @key{NL} @var{str1} @var{str2} @key{NL}
4388 Defines, through @code{pushdef}, a definition for @var{str1}
4389 expanding to the function whose builtin name is @var{str2}.  If the
4390 builtin does not exist (for example, if the frozen file was produced by
4391 a copy of @code{m4} compiled with changeword support, but the version
4392 of @code{m4} reloading was compiled without it), the reload is silent,
4393 but any subsequent use of the definition of @var{str1} will result in
4394 a warning.  This directive may appear more than once for the same name,
4395 and its order, along with @samp{T}, is important.  If omitted, you will
4396 have no access to any builtins.
4398 @item Q @var{len1} , @var{len2} @key{NL} @var{str1} @var{str2} @key{NL}
4399 Uses @var{str1} and @var{str2} as the begin-quote and end-quote
4400 strings.  If omitted, then @samp{`} and @samp{'} are the quote
4401 delimiters.
4403 @item T @var{len1} , @var{len2} @key{NL} @var{str1} @var{str2} @key{NL}
4404 Defines, though @code{pushdef}, a definition for @var{str1}
4405 expanding to the text given by @var{str2}.  This directive may appear
4406 more than once for the same name, and its order, along with @samp{F}, is
4407 important.
4409 @item V @var{number} @key{NL}
4410 Confirms the format of the file.  @code{m4} @value{VERSION} only creates
4411 and understands frozen files where @var{number} is 1.  This directive
4412 must be the first non-comment in the file, and may not appear more than
4413 once.
4414 @end table
4416 @node Compatibility
4417 @chapter Compatibility with other versions of @code{m4}
4419 @cindex compatibility
4420 This chapter describes the differences between this implementation of
4421 @code{m4}, and the implementation found under UNIX, notably System V,
4422 Release 3.
4424 There are also differences in BSD flavors of @code{m4}.  No attempt
4425 is made to summarize these here.
4427 @menu
4428 * Extensions::                  Extensions in @acronym{GNU} M4
4429 * Incompatibilities::           Facilities in System V m4 not in GNU M4
4430 * Other Incompatibilities::     Other incompatibilities
4431 @end menu
4433 @node Extensions
4434 @section Extensions in @acronym{GNU} @code{m4}
4436 @cindex @acronym{GNU} extensions
4437 This version of @code{m4} contains a few facilities that do not exist
4438 in System V @code{m4}.  These extra facilities are all suppressed by
4439 using the @option{-G} command line option (@pxref{Invoking m4}), unless
4440 overridden by other command line options.
4442 @itemize @bullet
4443 @item
4444 In the @code{$}@var{n} notation for macro arguments, @var{n} can contain
4445 several digits, while the System V @code{m4} only accepts one digit.
4446 This allows macros in @acronym{GNU} @code{m4} to take any number of
4447 arguments, and not only nine (@pxref{Arguments}).
4449 This means that @code{define(`foo', `$11')} is ambiguous between
4450 implementations.  To portably choose between grabbing the first
4451 parameter and appending 1 to the expansion, or grabbing the eleventh
4452 parameter, you can do the following:
4454 @example
4455 define(`a1', `A1')
4456 @result{}
4457 dnl First argument, concatenated with 1
4458 define(`_1', `$1')define(`first1', `_1($@@)1')
4459 @result{}
4460 dnl Eleventh argument, portable
4461 define(`_9', `$9')define(`eleventh', `_9(shift(shift($@@)))')
4462 @result{}
4463 dnl Eleventh argument, GNU style
4464 define(`Eleventh', `$11')
4465 @result{}
4466 first1(`a', `b', `c', `d', `e', `f', `g', `h', `i', `j', `k')
4467 @result{}A1
4468 eleventh(`a', `b', `c', `d', `e', `f', `g', `h', `i', `j', `k')
4469 @result{}k
4470 Eleventh(`a', `b', `c', `d', `e', `f', `g', `h', `i', `j', `k')
4471 @result{}k
4472 @end example
4474 @item
4475 The @code{divert} (@pxref{Divert}) macro can manage more than 9
4476 diversions.  @acronym{GNU} @code{m4} treats all positive numbers as valid
4477 diversions, rather than discarding diversions greater than 9.
4479 @item
4480 Files included with @code{include} and @code{sinclude} are sought in a
4481 user specified search path, if they are not found in the working
4482 directory.  The search path is specified by the @option{-I} option and the
4483 @env{M4PATH} environment variable (@pxref{Search Path}).
4485 @item
4486 Arguments to @code{undivert} can be non-numeric, in which case the named
4487 file will be included uninterpreted in the output (@pxref{Undivert}).
4489 @item
4490 Formatted output is supported through the @code{format} builtin, which
4491 is modeled after the C library function @code{printf} (@pxref{Format}).
4493 @item
4494 Searches and text substitution through regular expressions are
4495 supported by the @code{regexp} (@pxref{Regexp}) and @code{patsubst}
4496 (@pxref{Patsubst}) builtins.
4498 @item
4499 The output of shell commands can be read into @code{m4} with
4500 @code{esyscmd} (@pxref{Esyscmd}).
4502 @item
4503 There is indirect access to any builtin macro with @code{builtin}
4504 (@pxref{Builtin}).
4506 @item
4507 Macros can be called indirectly through @code{indir} (@pxref{Indir}).
4509 @item
4510 The name of the program, the current input file, and the current input
4511 line number are accessible through the builtins @code{__program__},
4512 @code{__file__}, and @code{__line__} (@pxref{Location}).
4514 @item
4515 The format of the output from @code{dumpdef} and macro tracing can be
4516 controlled with @code{debugmode} (@pxref{Debug Levels}).
4518 @item
4519 The destination of trace and debug output can be controlled with
4520 @code{debugfile} (@pxref{Debug Output}).
4521 @end itemize
4523 In addition to the above extensions, @acronym{GNU} @code{m4} implements the
4524 following command line options: @option{-F}, @option{-G}, @option{-I},
4525 @option{-L}, @option{-R}, @option{-V}, @option{-W}, @option{-d},
4526 @option{-l}, @option{-o} and @option{-t}.  @xref{Invoking m4}, for a
4527 description of these options.
4529 Also, the debugging and tracing facilities in @acronym{GNU} @code{m4} are much
4530 more extensive than in most other versions of @code{m4}.
4532 @node Incompatibilities
4533 @section Facilities in System V @code{m4} not in @acronym{GNU} @code{m4}
4535 The version of @code{m4} from System V contains a few facilities that
4536 have not been implemented in @acronym{GNU} @code{m4} yet.  Additionally,
4537 @acronym{POSIX} requires some behaviors that @acronym{GNU} @code{m4} has not
4538 implemented yet.  Relying on these behaviors is non-portable, as a
4539 future release of @acronym{GNU} @code{m4} may change.
4541 @itemize @bullet
4542 @item
4543 System V @code{m4} supports multiple arguments to @code{defn}, and
4544 @acronym{POSIX} requires it.  This is not yet implemented in @acronym{GNU}
4545 @code{m4}.  Unfortunately, this means it is not possible to mix builtins
4546 and other text into a single macro; a helper macro is required.
4548 @item
4549 @acronym{POSIX} requires an application to exit with non-zero status if
4550 it wrote an error message to stderr.  This has not yet been consistently
4551 implemented for the various builtins that are required to issue an error
4552 (such as @code{include} (@pxref{Include}) when a file is unreadable,
4553 @code{eval} (@pxref{Eval}) when an argument cannot be parsed, or using
4554 @code{m4exit} (@pxref{M4exit}) with a non-numeric argument).
4556 @item
4557 @acronym{POSIX} requires @code{m4wrap} (@pxref{M4wrap}) to act in FIFO
4558 (first-in, first-out) order, but @acronym{GNU} @code{m4} currently uses
4559 LIFO order.  Furthermore, @acronym{POSIX} states that only the first
4560 argument to @code{m4wrap} is saved for later evaluation, bug
4561 @acronym{GNU} @code{m4} saves and processes all arguments, with output
4562 separated by spaces.
4564 However, it is possible to emulate @acronym{POSIX} behavior by
4565 including the file @file{examples/@/wrapfifo.m4} from the distribution:
4567 @example
4568 undivert(`wrapfifo.m4')dnl
4569 @result{}dnl Redefine m4wrap to have FIFO semantics.
4570 @result{}define(`_m4wrap_level', `0')dnl
4571 @result{}define(`m4wrap',
4572 @result{}`ifdef(`m4wrap'_m4wrap_level,
4573 @result{}       `define(`m4wrap'_m4wrap_level,
4574 @result{}               defn(`m4wrap'_m4wrap_level)`$1')',
4575 @result{}       `builtin(`m4wrap', `define(`_m4wrap_level',
4576 @result{}                                  incr(_m4wrap_level))dnl
4577 @result{}m4wrap'_m4wrap_level)dnl
4578 @result{}define(`m4wrap'_m4wrap_level, `$1')')')dnl
4579 include(`wrapfifo.m4')
4580 @result{}
4581 m4wrap(`a`'m4wrap(`c
4582 ', `d')')m4wrap(`b')
4583 @result{}
4585 @result{}abc
4586 @end example
4588 @item
4589 @acronym{POSIX} requires that all builtins that require arguments, but
4590 are called without arguments, behave as though empty strings had been
4591 passed.  For example, @code{a`'define`'b} would expand to @code{ab}.
4592 But @acronym{GNU} @code{m4} ignores certain builtins if they have missing
4593 arguments, giving @code{adefineb} for the above example.
4595 @item
4596 Traditional implementations handle @code{define(`f',`1')} (@pxref{Define})
4597 by undefining the entire stack of previous definitions, and if doing
4598 @code{undefine(`f')} first.  @acronym{GNU} @code{m4} replaces just the top
4599 definition on the stack, as if doing @code{popdef(`f')} followed by
4600 @code{pushdef(`f',`1')}.
4602 @item
4603 @acronym{POSIX} requires @code{syscmd} (@pxref{Syscmd}) to evaluate
4604 command output for macro expansion, but this appears to be a mistake
4605 in @acronym{POSIX} since traditional implementations did not do this.
4606 @acronym{GNU} @code{m4} follows traditional behavior in @code{syscmd}, and
4607 provides the extension @code{esyscmd} that provides the @acronym{POSIX}
4608 semantics.
4610 @item
4611 @acronym{POSIX} requires @code{maketemp} (@pxref{Maketemp}) to replace
4612 the trailing @samp{X} characters with the @code{m4} process id, giving
4613 the same result on identical input, without creating any files, which
4614 leaves the door open for a data race in which other processes can create
4615 a file by the same name.  @acronym{GNU} @code{m4} actually creates a temporary
4616 file for each invocation of @code{maketemp}, which means that the output
4617 of the macro is different even if the input is identical.
4619 @item
4620 @acronym{POSIX} requires @code{changequote(@var{arg})}
4621 (@pxref{Changequote}) to use newline as the close quote, but @acronym{GNU}
4622 @code{m4} uses @samp{'} as the close quote.  Meanwhile, some
4623 traditional implementations use @var{arg} as the close quote, making it
4624 impossible to nest quotes.  For predictable results, never call
4625 changequote with just one argument.
4627 @item
4628 Some implementations of @code{m4} give macros a higher precedence than
4629 comments when parsing, meaning that if the start delimiter given to
4630 @code{changecom} (@pxref{Changecom}) starts with a macro name, comments
4631 are effectively disabled.  @acronym{POSIX} does not specify what the
4632 precedence is, so the @acronym{GNU} @code{m4} parser recognizes comments, then
4633 macros, then quoted strings.
4635 @item
4636 Traditional implementations allow argument collection, but not string
4637 and comment processing, to span file boundaries.  Thus, if @file{a.m4}
4638 contains @samp{len(}, and @file{b.m4} contains @samp{abc)},
4639 @kbd{m4 a.m4 b.m4} outputs @samp{3} with traditional @code{m4}, but
4640 gives an error message that the end of file was encountered inside a
4641 macro with @acronym{GNU} @code{m4}.  On the other hand, traditional
4642 implementations do end of file processing for files included with
4643 @code{include} or @code{sinclude} (@pxref{Include}), while @acronym{GNU}
4644 @code{m4} seamlessly integrates the content of those files.  Thus
4645 @code{include(`a.m4')include(`b.m4')} will output @samp{3} instead of
4646 giving an error.
4648 @item
4649 Traditional @code{m4} treats @code{traceon} (@pxref{Trace}) without
4650 arguments as a global variable, independent of named macro tracing.
4651 Also, once a macro is undefined, named tracing of that macro is lost.
4652 On the other hand, when @acronym{GNU} @code{m4} encounters
4653 @code{traceon} without
4654 arguments, it turns tracing on for all existing definitions at the time,
4655 but does not trace future definitions; @code{traceoff} without arguments
4656 turns tracing off for all definitions regardless of whether they were
4657 also traced by name; and tracing by name, such as with @option{-tfoo} at
4658 the command line or @code{traceon(`foo')} in the input, is an attribute
4659 that is preserved even if the macro is currently undefined.
4661 @item
4662 @acronym{POSIX} requires @code{eval} (@pxref{Eval}) to treat all
4663 operators with the same precedence as C.  However, @acronym{GNU} @code{m4}
4664 currently follows the traditional precedence of other @code{m4}
4665 implementations, where bitwise and logical negation (@samp{~} and
4666 @samp{!}) have lower precedence than equality operators, rather than
4667 equal precedence with other unary operators.  Use explicit parentheses
4668 to ensure proper precedence.  As extensions to @acronym{POSIX}, @acronym{GNU}
4669 @code{m4} treats the shift operators @samp{<<} and @samp{>>} as
4670 well-defined on signed integers (even though they are not in C), and
4671 adds the exponentiation operator @samp{**}.
4673 @item
4674 @acronym{POSIX} requires @code{translit} (@pxref{Translit}) to treat
4675 each character of the second and third arguments literally, but @acronym{GNU}
4676 @code{m4} treats @samp{-} as a range operator.
4678 @item
4679 @acronym{POSIX} requires @code{m4} to honor the locale environment
4680 variables of @env{LANG}, @env{LC_ALL}, @env{LC_CTYPE},
4681 @env{LC_MESSAGES}, and @env{NLSPATH}, but this has not yet been
4682 implemented in @acronym{GNU} @code{m4}.
4683 @end itemize
4685 @node Other Incompatibilities
4686 @section Other incompatibilities
4688 There are a few other incompatibilities between this implementation of
4689 @code{m4}, and the System V version.
4691 @itemize @bullet
4692 @item
4693 @acronym{GNU} @code{m4} implements sync lines differently from System V
4694 @code{m4}, when text is being diverted.  @acronym{GNU} @code{m4} outputs
4695 the sync lines when the text is being diverted, and System V @code{m4}
4696 when the diverted text is being brought back.
4698 The problem is which lines and file names should be attached to text that
4699 is being, or has been, diverted.  System V @code{m4} regards all the
4700 diverted text as being generated by the source line containing the
4701 @code{undivert} call, whereas @acronym{GNU} @code{m4} regards the
4702 diverted text as being generated at the time it is diverted.
4704 The sync line option is used mostly when using @code{m4} as
4705 a front end to a compiler.  If a diverted line causes a compiler error,
4706 the error messages should most probably refer to the place where the
4707 diversion were made, and not where it was inserted again.
4709 @item
4710 @acronym{GNU} @code{m4} makes no attempt at prohibiting self-referential
4711 definitions like:
4713 @comment ignore
4714 @example
4715 define(`x', `x')
4716 @result{}
4717 define(`x', `x ')
4718 @result{}
4719 @end example
4721 There is nothing inherently wrong with defining @samp{x} to
4722 return @samp{x}.  The wrong thing is to expand @samp{x} unquoted.
4723 In @code{m4}, one might use macros to hold strings, as we do for
4724 variables in other programming languages, further checking them with:
4726 @comment ignore
4727 @example
4728 ifelse(defn(`@var{holder}'), `@var{value}', @dots{})
4729 @end example
4731 @noindent
4732 In cases like this one, an interdiction for a macro to hold its own
4733 name would be a useless limitation.  Of course, this leaves more rope
4734 for the @acronym{GNU} @code{m4} user to hang himself!  Rescanning hangs may be
4735 avoided through careful programming, a little like for endless loops
4736 in traditional programming languages.
4737 @end itemize
4739 @node Answers
4740 @chapter Correct version of some examples
4742 Some of the examples in this manuals are buggy, for demonstration
4743 purposes.  Correctly working macros are presented here.
4745 The @code{exch} macro (@pxref{Arguments}) as presented requires clients
4746 to double quote their arguments.  A nicer definition, which lets
4747 clients follow the rule of thumb of one level of quoting per level of
4748 parentheses, involves adding quotes in the definition of @code{exch}, as
4749 follows:
4751 @example
4752 define(`exch', ``$2', `$1'')
4753 @result{}
4754 define(exch(`expansion text', `macro'))
4755 @result{}
4756 macro
4757 @result{}expansion text
4758 @end example
4760 The @code{cleardivert} macro (@pxref{Cleardiv}) cannot, as it stands, be
4761 called without arguments to clear all pending diversions.  That is
4762 because using undivert with an empty string for an argument is different
4763 than using it with no arguments at all.  Compare the earlier definition
4764 with one that takes the number of arguments into account:
4766 @example
4767 define(`cleardivert',
4768   `pushdef(`_n', divnum)divert(`-1')undivert($@@)divert(_n)popdef(`_n')')
4769 @result{}
4770 divert(`1')one
4771 divert
4772 @result{}
4773 cleardivert
4774 @result{}
4775 undivert
4776 @result{}one
4777 @result{}
4778 define(`cleardivert',
4779   `pushdef(`_num', divnum)divert(`-1')ifelse(`$#', `0',
4780     `undivert`'', `undivert($@@)')divert(_num)popdef(`_num')')
4781 @result{}
4782 divert(`2')two
4783 divert
4784 @result{}
4785 cleardivert
4786 @result{}
4787 undivert
4788 @result{}
4789 @end example
4791 The @code{fatal_error} macro (@pxref{M4exit}) does not quite match the
4792 format of internal error messages when invoked inside wrapped text, due
4793 to the current limitations of @code{__file__} (@pxref{Location}) when
4794 invoked inside @code{m4wrap}.  Since @code{m4} omits the file and line
4795 number from its warning messages when there is no current file (or
4796 equivalently, when the current line is 0, since all files start at line
4797 1), a better implementation would be:
4799 @example
4800 define(`fatal_error',
4801   `errprint(__program__:ifelse(__line__, `0', `',
4802     `__file__:__line__:')` fatal error: $*
4803 ')m4exit(`1')')
4804 @result{}
4805 m4wrap(`divnum(`demo of internal message')
4806 fatal_error(`inside wrapped text')')
4807 @result{}
4809 @error{}m4: Warning: excess arguments to builtin `divnum' ignored
4810 @result{}0
4811 @error{}m4: fatal error: inside wrapped text
4812 @end example
4814 @c ========================================================== Appendices
4816 @node Copying This Manual
4817 @appendix Copying This Manual
4818 @cindex License
4820 @menu
4821 * GNU Free Documentation License::  License for copying this manual
4822 @end menu
4824 @include fdl.texi
4826 @node Indices
4827 @appendix Indices
4829 @menu
4830 * Concept index::               Index for many concepts
4831 * Macro index::                 Index for all m4 macros
4832 @end menu
4834 @node Concept index
4835 @appendixsec Concept index
4837 @printindex cp
4839 @node Macro index
4840 @appendixsec Macro index
4842 References are exclusively to the places where a builtin is introduced
4843 the first time.
4845 @iftex
4846 @sp 1
4847 @end iftex
4849 @printindex fn
4851 @bye
4853 @c Local Variables:
4854 @c coding: ISO-8859-1
4855 @c fill-column: 72
4856 @c ispell-local-dictionary: "american"
4857 @c indent-tabs-mode: nil
4858 @c whitespace-check-buffer-indent: nil
4859 @c End: