source: trunk/essentials/sys-devel/m4/doc/m4.info

Last change on this file was 3090, checked in by bird, 18 years ago

m4 1.4.8

File size: 226.6 KB
Line 
1This is m4.info, produced by makeinfo version 4.8 from m4.texinfo.
2
3 This manual is for GNU M4 (version 1.4.8, 20 November 2006), a
4package containing an implementation of the m4 macro language.
5
6 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006
7Free Software Foundation, Inc.
8
9 Permission is granted to copy, distribute and/or modify this
10 document under the terms of the GNU Free Documentation License,
11 Version 1.2 or any later version published by the Free Software
12 Foundation; with no Invariant Sections, no Front-Cover Texts, and
13 no Back-Cover Texts. A copy of the license is included in the
14 section entitled "GNU Free Documentation License."
15
16INFO-DIR-SECTION GNU programming tools
17START-INFO-DIR-ENTRY
18* M4: (m4). A powerful macro processor.
19END-INFO-DIR-ENTRY
20
21
22File: m4.info, Node: Top, Next: Preliminaries, Up: (dir)
23
24GNU M4
25******
26
27This manual is for GNU M4 (version 1.4.8, 20 November 2006), a package
28containing an implementation of the m4 macro language.
29
30 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006
31Free Software Foundation, Inc.
32
33 Permission is granted to copy, distribute and/or modify this
34 document under the terms of the GNU Free Documentation License,
35 Version 1.2 or any later version published by the Free Software
36 Foundation; with no Invariant Sections, no Front-Cover Texts, and
37 no Back-Cover Texts. A copy of the license is included in the
38 section entitled "GNU Free Documentation License."
39
40 GNU `m4' is an implementation of the traditional UNIX macro
41processor. It is mostly SVR4 compatible, although it has some
42extensions (for example, handling more than 9 positional parameters to
43macros). `m4' also has builtin functions for including files, running
44shell commands, doing arithmetic, etc. Autoconf needs GNU `m4' for
45generating `configure' scripts, but not for running them.
46
47 GNU `m4' was originally written by Rene' Seindal, with subsequent
48changes by Franc,ois Pinard and other volunteers on the Internet. All
49names and email addresses can be found in the files `m4-1.4.8/AUTHORS'
50and `m4-1.4.8/THANKS' from the GNU M4 distribution.
51
52 This is release 1.4.8. It is now considered stable: future
53releases in the 1.4.x series are only meant to fix bugs, increase speed,
54or improve documentation. However...
55
56 An experimental feature, which would improve `m4' usefulness, allows
57for changing the syntax for what is a "word" in `m4'. You should use:
58 ./configure --enable-changeword
59 if you want this feature compiled in. The current implementation
60slows down `m4' considerably and is hardly acceptable. In the future,
61`m4' 2.0 will come with a different set of new features that provide
62similar capabilities, but without the inefficiencies, so changeword
63will go away and _you should not count on it_.
64
65* Menu:
66
67* Preliminaries:: Introduction and preliminaries
68* Invoking m4:: Invoking `m4'
69* Syntax:: Lexical and syntactic conventions
70
71* Macros:: How to invoke macros
72* Definitions:: How to define new macros
73* Conditionals:: Conditionals, loops, and recursion
74
75* Debugging:: How to debug macros and input
76
77* Input Control:: Input control
78* File Inclusion:: File inclusion
79* Diversions:: Diverting and undiverting output
80
81* Text handling:: Macros for text handling
82* Arithmetic:: Macros for doing arithmetic
83* Shell commands:: Macros for running shell commands
84* Miscellaneous:: Miscellaneous builtin macros
85* Frozen files:: Fast loading of frozen state
86
87* Compatibility:: Compatibility with other versions of `m4'
88* Answers:: Correct version of some examples
89* Copying This Manual:: How to make copies of this manual
90* Indices:: Indices of concepts and macros
91
92 --- The Detailed Node Listing ---
93
94Introduction and preliminaries
95
96* Intro:: Introduction to `m4'
97* History:: Historical references
98* Bugs:: Problems and bugs
99* Manual:: Using this manual
100
101Invoking `m4'
102
103* Operation modes:: Command line options for operation modes
104* Preprocessor features:: Command line options for preprocessor features
105* Limits control:: Command line options for limits control
106* Frozen state:: Command line options for frozen state
107* Debugging options:: Command line options for debugging
108* Command line files:: Specifying input files on the command line
109
110Lexical and syntactic conventions
111
112* Names:: Macro names
113* Quoted strings:: Quoting input to `m4'
114* Comments:: Comments in `m4' input
115* Other tokens:: Other kinds of input tokens
116* Input processing:: How `m4' copies input to output
117
118How to invoke macros
119
120* Invocation:: Macro invocation
121* Inhibiting Invocation:: Preventing macro invocation
122* Macro Arguments:: Macro arguments
123* Quoting Arguments:: On Quoting Arguments to macros
124* Macro expansion:: Expanding macros
125
126How to define new macros
127
128* Define:: Defining a new macro
129* Arguments:: Arguments to macros
130* Pseudo Arguments:: Special arguments to macros
131* Undefine:: Deleting a macro
132* Defn:: Renaming macros
133* Pushdef:: Temporarily redefining macros
134
135* Indir:: Indirect call of macros
136* Builtin:: Indirect call of builtins
137
138Conditionals, loops, and recursion
139
140* Ifdef:: Testing if a macro is defined
141* Ifelse:: If-else construct, or multibranch
142* Shift:: Recursion in `m4'
143* Forloop:: Iteration by counting
144* Foreach:: Iteration by list contents
145
146How to debug macros and input
147
148* Dumpdef:: Displaying macro definitions
149* Trace:: Tracing macro calls
150* Debug Levels:: Controlling debugging output
151* Debug Output:: Saving debugging output
152
153Input control
154
155* Dnl:: Deleting whitespace in input
156* Changequote:: Changing the quote characters
157* Changecom:: Changing the comment delimiters
158* Changeword:: Changing the lexical structure of words
159* M4wrap:: Saving text until end of input
160
161File inclusion
162
163* Include:: Including named files
164* Search Path:: Searching for include files
165
166Diverting and undiverting output
167
168* Divert:: Diverting output
169* Undivert:: Undiverting output
170* Divnum:: Diversion numbers
171* Cleardivert:: Discarding diverted text
172
173Macros for text handling
174
175* Len:: Calculating length of strings
176* Index macro:: Searching for substrings
177* Regexp:: Searching for regular expressions
178* Substr:: Extracting substrings
179* Translit:: Translating characters
180* Patsubst:: Substituting text by regular expression
181* Format:: Formatting strings (printf-like)
182
183Macros for doing arithmetic
184
185* Incr:: Decrement and increment operators
186* Eval:: Evaluating integer expressions
187
188Macros for running shell commands
189
190* Platform macros:: Determining the platform
191* Syscmd:: Executing simple commands
192* Esyscmd:: Reading the output of commands
193* Sysval:: Exit status
194* Mkstemp:: Making temporary files
195
196Miscellaneous builtin macros
197
198* Errprint:: Printing error messages
199* Location:: Printing current location
200* M4exit:: Exiting from `m4'
201
202Fast loading of frozen state
203
204* Using frozen files:: Using frozen files
205* Frozen file format:: Frozen file format
206
207Compatibility with other versions of `m4'
208
209* Extensions:: Extensions in GNU M4
210* Incompatibilities:: Facilities in System V m4 not in GNU M4
211* Other Incompatibilities:: Other incompatibilities
212
213Correct version of some examples
214
215* Improved exch:: Solution for `exch'
216* Improved forloop:: Solution for `forloop'
217* Improved foreach:: Solution for `foreach'
218* Improved cleardivert:: Solution for `cleardivert'
219* Improved fatal_error:: Solution for `fatal_error'
220
221How to make copies of this manual
222
223* GNU Free Documentation License:: License for copying this manual
224
225Indices of concepts and macros
226
227* Concept index:: Index for many concepts
228* Macro index:: Index for all `m4' macros
229
230
231File: m4.info, Node: Preliminaries, Next: Invoking m4, Prev: Top, Up: Top
232
2331 Introduction and preliminaries
234********************************
235
236This first chapter explains what GNU `m4' is, where `m4' comes from,
237how to read and use this documentation, how to call the `m4' program,
238and how to report bugs about it. It concludes by giving tips for
239reading the remainder of the manual.
240
241 The following chapters then detail all the features of the `m4'
242language.
243
244* Menu:
245
246* Intro:: Introduction to `m4'
247* History:: Historical references
248* Bugs:: Problems and bugs
249* Manual:: Using this manual
250
251
252File: m4.info, Node: Intro, Next: History, Up: Preliminaries
253
2541.1 Introduction to `m4'
255========================
256
257`m4' is a macro processor, in the sense that it copies its input to the
258output, expanding macros as it goes. Macros are either builtin or
259user-defined, and can take any number of arguments. Besides just doing
260macro expansion, `m4' has builtin functions for including named files,
261running shell commands, doing integer arithmetic, manipulating text in
262various ways, performing recursion, etc.... `m4' can be used either as
263a front-end to a compiler, or as a macro processor in its own right.
264
265 The `m4' macro processor is widely available on all UNIXes, and has
266been standardized by POSIX. Usually, only a small percentage of users
267are aware of its existence. However, those who find it often become
268committed users. The popularity of GNU Autoconf, which requires GNU
269`m4' for _generating_ `configure' scripts, is an incentive for many to
270install it, while these people will not themselves program in `m4'.
271GNU `m4' is mostly compatible with the System V, Release 3 version,
272except for some minor differences. *Note Compatibility::, for more
273details.
274
275 Some people find `m4' to be fairly addictive. They first use `m4'
276for simple problems, then take bigger and bigger challenges, learning
277how to write complex sets of `m4' macros along the way. Once really
278addicted, users pursue writing of sophisticated `m4' applications even
279to solve simple problems, devoting more time debugging their `m4'
280scripts than doing real work. Beware that `m4' may be dangerous for
281the health of compulsive programmers.
282
283
284File: m4.info, Node: History, Next: Bugs, Prev: Intro, Up: Preliminaries
285
2861.2 Historical references
287=========================
288
289`GPM' was an important ancestor of `m4'. See C. Stratchey: "A General
290Purpose Macro generator", Computer Journal 8,3 (1965), pp. 225 ff.
291`GPM' is also succinctly described into David Gries classic "Compiler
292Construction for Digital Computers".
293
294 The classic B. Kernighan and P.J. Plauger: "Software Tools",
295Addison-Wesley, Inc. (1976) describes and implements a Unix
296macro-processor language, which inspired Dennis Ritchie to write `m3',
297a macro processor for the AP-3 minicomputer.
298
299 Kernighan and Ritchie then joined forces to develop the original
300`m4', as described in "The M4 Macro Processor", Bell Laboratories
301(1977). It had only 21 builtin macros.
302
303 While `GPM' was more _pure_, `m4' is meant to deal with the true
304intricacies of real life: macros can be recognized without being
305pre-announced, skipping whitespace or end-of-lines is easier, more
306constructs are builtin instead of derived, etc.
307
308 Originally, the Kernighan and Plauger macro-processor, and then
309`m3', formed the engine for the Rational FORTRAN preprocessor, that is,
310the `Ratfor' equivalent of `cpp'. Later, `m4' was used as a frontend
311for `Ratfor', `C' and `Cobol'.
312
313 Rene' Seindal released his implementation of `m4', GNU `m4', in
3141990, with the aim of removing the artificial limitations in many of
315the traditional `m4' implementations, such as maximum line length,
316macro size, or number of macros.
317
318 The late Professor A. Dain Samples described and implemented a
319further evolution in the form of `M5': "User's Guide to the M5 Macro
320Language: 2nd edition", Electronic Announcement on comp.compilers
321newsgroup (1992).
322
323 Franc,ois Pinard took over maintenance of GNU `m4' in 1992, until
3241994 when he released GNU `m4' 1.4, which was the stable release for 10
325years. It was at this time that GNU Autoconf decided to require GNU
326`m4' as its underlying engine, since all other implementations of `m4'
327had too many limitations.
328
329 More recently, in 2004, Paul Eggert released 1.4.1 and 1.4.2 which
330addressed some long standing bugs in the venerable 1.4 release. Then
331in 2005 Gary V. Vaughan collected together the many patches to GNU `m4'
3321.4 that were floating around the net and released 1.4.3 and 1.4.4.
333And in 2006, Eric Blake joined the team and prepared patches for the
334release of 1.4.5, 1.4.6, 1.4.7, and 1.4.8.
335
336 Meanwhile, development has continued on new features for `m4', such
337as dynamic module loading and additional builtins. When complete, GNU
338`m4' 2.0 will start a new series of releases.
339
340
341File: m4.info, Node: Bugs, Next: Manual, Prev: History, Up: Preliminaries
342
3431.3 Problems and bugs
344=====================
345
346If you have problems with GNU M4 or think you've found a bug, please
347report it. Before reporting a bug, make sure you've actually found a
348real bug. Carefully reread the documentation and see if it really says
349you can do what you're trying to do. If it's not clear whether you
350should be able to do something or not, report that too; it's a bug in
351the documentation!
352
353 Before reporting a bug or trying to fix it yourself, try to isolate
354it to the smallest possible input file that reproduces the problem.
355Then send us the input file and the exact results `m4' gave you. Also
356say what you expected to occur; this will help us decide whether the
357problem was really in the documentation.
358
359 Once you've got a precise problem, send e-mail to (Internet)
360<bug-m4@gnu.org>. Please include the version number of `m4' you are
361using. You can get this information with the command `m4 --version'.
362Also provide details about the platform you are executing on.
363
364 Non-bug suggestions are always welcome as well. If you have
365questions about things that are unclear in the documentation or are
366just obscure features, please report them too.
367
368
369File: m4.info, Node: Manual, Prev: Bugs, Up: Preliminaries
370
3711.4 Using this manual
372=====================
373
374This manual contains a number of examples of `m4' input and output, and
375a simple notation is used to distinguish input, output and error
376messages from `m4'. Examples are set out from the normal text, and
377shown in a fixed width font, like this
378
379 This is an example of an example!
380
381 To distinguish input from output, all output from `m4' is prefixed
382by the string `=>', and all error messages by the string `error-->'.
383Thus
384
385 Example of input line
386 =>Output line from m4
387 error-->and an error message
388
389 The sequence `^D' in an example indicates the end of the input file.
390The majority of these examples are self-contained, and you can run them
391with similar results by invoking `m4 -d'. In fact, the testsuite that
392is bundled in the GNU M4 package consists of the examples in this
393document!
394
395 As each of the predefined macros in `m4' is described, a prototype
396call of the macro will be shown, giving descriptive names to the
397arguments, e.g.,
398
399 -- Composite: example (STRING, [COUNT = `1'], [ARGUMENT]...)
400 This is a sample prototype. There is not really a macro named
401 `example', but this documents that if there were, it would be a
402 Composite macro, rather than a Builtin. It requires at least one
403 argument, STRING. Remember that in `m4', there must not be a
404 space between the macro name and the opening parenthesis, unless
405 it was intended to call the macro without any arguments. The
406 brackets around COUNT and ARGUMENT show that these arguments are
407 optional. If COUNT is omitted, the macro behaves as if count were
408 `1', whereas if ARGUMENT is omitted, the macro behaves as if it
409 were the empty string. A blank argument is not the same as an
410 omitted argument. For example, `example(`a')', `example(`a',`1')',
411 and `example(`a',`1',)' would behave identically with COUNT set to
412 `1'; while `example(`a',)' and `example(`a',`')' would explicitly
413 pass the empty string for COUNT. The ellipses (`...') show that
414 the macro processes additional arguments after ARGUMENT, rather
415 than ignoring them.
416
417 All macro arguments in `m4' are strings, but some are given special
418interpretation, e.g., as numbers, file names, regular expressions, etc.
419The documentation for each macro will state how the parameters are
420interpreted, and what happens if the argument cannot be parsed
421according to the desired interpretation. Unless specified otherwise, a
422parameter specified to be a number is parsed as a decimal, even if the
423argument has leading zeros; and parsing the empty string as a number
424results in 0 rather than an error, although a warning will be issued.
425
426 This document consistently writes and uses "builtin", without a
427hyphen, as if it were an English word. This is how the `builtin'
428primitive is spelled within `m4'.
429
430
431File: m4.info, Node: Invoking m4, Next: Syntax, Prev: Preliminaries, Up: Top
432
4332 Invoking `m4'
434***************
435
436The format of the `m4' command is:
437
438 `m4' [OPTION...] [FILE...]
439
440 All options begin with `-', or if long option names are used, with
441`--'. A long option name need not be written completely, any
442unambiguous prefix is sufficient. POSIX requires `m4' to recognize
443arguments intermixed with files, even when `POSIXLY_CORRECT' is set in
444the environment. Most options take effect at startup regardless of
445their position, but some are documented below as taking effect after
446any files that occurred earlier in the command line. The argument `--'
447is a marker to denote the end of options.
448
449 With short options, options that do not take arguments may be
450combined into a single command line argument with subsequent options,
451options with mandatory arguments may be provided either as a single
452command line argument or as two arguments, and options with optional
453arguments must be provided as a single argument. In other words, `m4
454-QPDfoo -d a -d+f' is equivalent to `m4 -Q -P -D foo -d -d+f -- ./a',
455although the latter form is considered canonical.
456
457 With long options, options with mandatory arguments may be provided
458with an equal sign (`=') in a single argument, or as two arguments, and
459options with optional arguments must be provided as a single argument.
460In other words, `m4 --def foo --debug a' is equivalent to `m4
461--define=foo --debug= -- ./a', although the latter form is considered
462canonical (not to mention more robust, in case a future version of `m4'
463introduces an option named `--default').
464
465 `m4' understands the following options, grouped by functionality.
466
467* Menu:
468
469* Operation modes:: Command line options for operation modes
470* Preprocessor features:: Command line options for preprocessor features
471* Limits control:: Command line options for limits control
472* Frozen state:: Command line options for frozen state
473* Debugging options:: Command line options for debugging
474* Command line files:: Specifying input files on the command line
475
476
477File: m4.info, Node: Operation modes, Next: Preprocessor features, Up: Invoking m4
478
4792.1 Command line options for operation modes
480============================================
481
482Several options control the overall operation of `m4':
483
484`--help'
485 Print a help summary on standard output, then immediately exit
486 `m4' without reading any input files or performing any other
487 actions.
488
489`--version'
490 Print the version number of the program on standard output, then
491 immediately exit `m4' without reading any input files or
492 performing any other actions.
493
494`-E'
495`--fatal-warnings'
496 Stop execution and exit `m4' once the first warning has been
497 issued, considering all of them to be fatal.
498
499`-i'
500`--interactive'
501`-e'
502 Makes this invocation of `m4' interactive. This means that all
503 output will be unbuffered, and interrupts will be ignored. The
504 spelling `-e' exists for compatibility with other `m4'
505 implementations, and issues a warning because it may be withdrawn
506 in a future version of GNU M4.
507
508`-P'
509`--prefix-builtins'
510 Internally modify _all_ builtin macro names so they all start with
511 the prefix `m4_'. For example, using this option, one should write
512 `m4_define' instead of `define', and `m4___file__' instead of
513 `__file__'. This option has no effect if `-R' is also specified.
514
515`-Q'
516`--quiet'
517`--silent'
518 Suppress warnings, such as missing or superfluous arguments in
519 macro calls, or treating the empty string as zero.
520
521`-W REGEXP'
522`--word-regexp=REGEXP'
523 Use REGEXP as an alternative syntax for macro names. This
524 experimental option will not be present in all GNU `m4'
525 implementations (*note Changeword::).
526
527
528File: m4.info, Node: Preprocessor features, Next: Limits control, Prev: Operation modes, Up: Invoking m4
529
5302.2 Command line options for preprocessor features
531==================================================
532
533Several options allow `m4' to behave more like a preprocessor. Macro
534definitions and deletions can be made on the command line, the search
535path can be altered, and the output file can track where the input came
536from. These features occur with the following options:
537
538`-D NAME[=VALUE]'
539`--define=NAME[=VALUE]'
540 This enters NAME into the symbol table, before any input files are
541 read. If `=VALUE' is missing, the value is taken to be the empty
542 string. The VALUE can be any string, and the macro can be defined
543 to take arguments, just as if it was defined from within the
544 input. This option may be given more than once; order with
545 respect to file names is significant, and redefining the same NAME
546 loses the previous value.
547
548`-I DIRECTORY'
549`--include=DIRECTORY'
550 Make `m4' search DIRECTORY for included files that are not found
551 in the current working directory. *Note Search Path::, for more
552 details. This option may be given more than once.
553
554`-s'
555`--synclines'
556 Generate synchronization lines, for use by the C preprocessor or
557 other similar tools. Order is significant with respect to file
558 names. This option is useful, for example, when `m4' is used as a
559 front end to a compiler. Source file name and line number
560 information is conveyed by directives of the form `#line LINENUM
561 "FILE"', which are inserted as needed into the middle of the
562 output. Such directives mean that the following line originated
563 or was expanded from the contents of input file FILE at line
564 LINENUM. The `"FILE"' part is often omitted when the file name
565 did not change from the previous directive.
566
567 Synchronization directives are always given on complete lines by
568 themselves. When a synchronization discrepancy occurs in the
569 middle of an output line, the associated synchronization directive
570 is delayed until the beginning of the next generated line.
571
572`-U NAME'
573`--undefine=NAME'
574 This deletes any predefined meaning NAME might have. Obviously,
575 only predefined macros can be deleted in this way. This option
576 may be given more than once; undefining a NAME that does not have a
577 definition is silently ignored. Order is significant with respect
578 to file names.
579
580
581File: m4.info, Node: Limits control, Next: Frozen state, Prev: Preprocessor features, Up: Invoking m4
582
5832.3 Command line options for limits control
584===========================================
585
586There are some limits within `m4' that can be tuned. For
587compatibility, `m4' also accepts some options that control limits in
588other implementations, but which are automatically unbounded (limited
589only by your hardware and operating system constraints) in GNU `m4'.
590
591`-G'
592`--traditional'
593 Suppress all the extensions made in this implementation, compared
594 to the System V version. *Note Compatibility::, for a list of
595 these.
596
597`-H NUM'
598`--hashsize=NUM'
599 Make the internal hash table for symbol lookup be NUM entries big.
600 For better performance, the number should be prime, but this is not
601 checked. The default is 509 entries. It should not be necessary
602 to increase this value, unless you define an excessive number of
603 macros.
604
605`-L NUM'
606`--nesting-limit=NUM'
607 Artificially limit the nesting of macro calls to NUM levels,
608 stopping program execution if this limit is ever exceeded. When
609 not specified, nesting is limited to 1024 levels. A value of zero
610 means unlimited; but then heavily nested code could potentially
611 cause a stack overflow.
612
613 The precise effect of this option might be more correctly
614 associated with textual nesting than dynamic recursion. It has
615 been useful when some complex `m4' input was generated by
616 mechanical means. Most users would never need this option. If
617 shown to be obtrusive, this option (which is still experimental)
618 might well disappear.
619
620 This option does _not_ have the ability to break endless
621 rescanning loops, since these do not necessarily consume much
622 memory or stack space. Through clever usage of rescanning loops,
623 one can request complex, time-consuming computations from `m4'
624 with useful results. Putting limitations in this area would break
625 `m4' power. There are many pathological cases:
626 `define(`a', `a')a' is only the simplest example (but *note
627 Compatibility::). Expecting GNU `m4' to detect these would be a
628 little like expecting a compiler system to detect and diagnose
629 endless loops: it is a quite _hard_ problem in general, if not
630 undecidable!
631
632`-B NUM'
633`-S NUM'
634`-T NUM'
635 These options are present for compatibility with System V `m4', but
636 do nothing in this implementation. They may disappear in future
637 releases, and issue a warning to that effect.
638
639`-N NUM'
640`--diversions=NUM'
641 These options are present only for compatibility with previous
642 versions of GNU `m4', and were controlling the number of possible
643 diversions which could be used at the same time. They do nothing,
644 because there is no fixed limit anymore. They may disappear in
645 future releases, and issue a warning to that effect.
646
647
648File: m4.info, Node: Frozen state, Next: Debugging options, Prev: Limits control, Up: Invoking m4
649
6502.4 Command line options for frozen state
651=========================================
652
653GNU `m4' comes with a feature of freezing internal state (*note Frozen
654files::). This can be used to speed up `m4' execution when reusing a
655common initialization script.
656
657`-F FILE'
658`--freeze-state=FILE'
659 Once execution is finished, write out the frozen state on the
660 specified FILE. It is conventional, but not required, for FILE to
661 end in `.m4f'.
662
663`-R FILE'
664`--reload-state=FILE'
665 Before execution starts, recover the internal state from the
666 specified frozen FILE. The options `-D', `-U', and `-t' take
667 effect after state is reloaded, but before the input files are
668 read.
669
670
671File: m4.info, Node: Debugging options, Next: Command line files, Prev: Frozen state, Up: Invoking m4
672
6732.5 Command line options for debugging
674======================================
675
676Finally, there are several options for aiding in debugging `m4' scripts.
677
678`-d[FLAGS]'
679`--debug[=FLAGS]'
680 Set the debug-level according to the flags FLAGS. The debug-level
681 controls the format and amount of information presented by the
682 debugging functions. *Note Debug Levels::, for more details on
683 the format and meaning of FLAGS. If omitted, FLAGS defaults to
684 `aeq'.
685
686`--debugfile=FILE'
687`-o FILE'
688`--error-output=FILE'
689 Redirect `dumpdef' output, debug messages, and trace output to the
690 named FILE. Warnings, error messages, and `errprint' output are
691 still printed to standard error. If unspecified, debug output goes
692 to standard error; if empty, debug output is discarded. *Note
693 Debug Output::, for more details. The spellings `-o' and
694 `--error-output' are misleading and inconsistent with other GNU
695 tools; for now they are silently accepted as synonyms of
696 `--debugfile', but in a future version of M4, using them will
697 cause a warning to be issued.
698
699`-l NUM'
700`--arglength=NUM'
701 Restrict the size of the output generated by macro tracing to NUM
702 characters per trace line. If unspecified or zero, output is
703 unlimited. *Note Debug Levels::, for more details.
704
705`-t NAME'
706`--trace=NAME'
707 This enables tracing for the macro NAME, at any point where it is
708 defined. NAME need not be defined when this option is given.
709 This option may be given more than once, and order is significant
710 with respect to file names. *Note Trace::, for more details.
711
712
713File: m4.info, Node: Command line files, Prev: Debugging options, Up: Invoking m4
714
7152.6 Specifying input files on the command line
716==============================================
717
718The remaining arguments on the command line are taken to be input file
719names. If no names are present, standard input is read. A file name
720of `-' is taken to mean standard input. It is conventional, but not
721required, for input files to end in `.m4'.
722
723 The input files are read in the sequence given. Standard input can
724be read more than once, so the file name `-' may appear multiple times
725on the command line; this makes a difference when input is from a
726terminal or other special file type. It is an error if an input file
727ends in the middle of argument collection, a comment, or a quoted
728string.
729
730 The options `--define' (`-D'), `--undefine' (`-U'), `--synclines'
731(`-s'), and `--trace' (`-t') only take effect after processing input
732from any file names that occur earlier on the command line.
733
734 If none of the input files invoked `m4exit' (*note M4exit::), the
735exit status of `m4' will be 0 for success, 1 for general failure (such
736as problems with reading an input file), and 63 for version mismatch
737(*note Using frozen files::).
738
739 If you need to read a file whose name starts with a `-', you can
740specify it as `./-file', or use `--' to mark the end of options.
741
742
743File: m4.info, Node: Syntax, Next: Macros, Prev: Invoking m4, Up: Top
744
7453 Lexical and syntactic conventions
746***********************************
747
748As `m4' reads its input, it separates it into "tokens". A token is
749either a name, a quoted string, or any single character, that is not a
750part of either a name or a string. Input to `m4' can also contain
751comments. GNU `m4' does not yet understand locales; all operations are
752byte-oriented rather than character-oriented. However, `m4' is
753eight-bit clean, so you can use non-ASCII characters in quoted strings
754(*note Changequote::), comments (*note Changecom::), and macro names
755(*note Indir::), with the exception of the NUL character (the zero byte
756`'\0'').
757
758* Menu:
759
760* Names:: Macro names
761* Quoted strings:: Quoting input to `m4'
762* Comments:: Comments in `m4' input
763* Other tokens:: Other kinds of input tokens
764* Input processing:: How `m4' copies input to output
765
766
767File: m4.info, Node: Names, Next: Quoted strings, Up: Syntax
768
7693.1 Macro names
770===============
771
772A name is any sequence of letters, digits, and the character `_'
773(underscore), where the first character is not a digit. `m4' will use
774the longest such sequence found in the input. If a name has a macro
775definition, it will be subject to macro expansion (*note Macros::).
776Names are case-sensitive.
777
778 Examples of legal names are: `foo', `_tmp', and `name01'.
779
780
781File: m4.info, Node: Quoted strings, Next: Comments, Prev: Names, Up: Syntax
782
7833.2 Quoting input to `m4'
784=========================
785
786A quoted string is a sequence of characters surrounded by quote
787strings, defaulting to ``' and `'', where the nested begin and end
788quotes within the string are balanced. The value of a string token is
789the text, with one level of quotes stripped off. Thus
790
791 `'
792 =>
793
794is the empty string, and double-quoting turns into single-quoting.
795
796 ``quoted''
797 =>`quoted'
798
799 The quote characters can be changed at any time, using the builtin
800macro `changequote'. *Note Changequote::, for more information.
801
802
803File: m4.info, Node: Comments, Next: Other tokens, Prev: Quoted strings, Up: Syntax
804
8053.3 Comments in `m4' input
806==========================
807
808Comments in `m4' are normally delimited by the characters `#' and
809newline. All characters between the comment delimiters are ignored,
810but the entire comment (including the delimiters) is passed through to
811the output--comments are _not_ discarded by `m4'.
812
813 Comments cannot be nested, so the first newline after a `#' ends the
814comment. The commenting effect of the begin-comment string can be
815inhibited by quoting it.
816
817 `quoted text' # `commented text'
818 =>quoted text # `commented text'
819 `quoting inhibits' `#' `comments'
820 =>quoting inhibits # comments
821
822 The comment delimiters can be changed to any string at any time,
823using the builtin macro `changecom'. *Note Changecom::, for more
824information.
825
826
827File: m4.info, Node: Other tokens, Next: Input processing, Prev: Comments, Up: Syntax
828
8293.4 Other kinds of input tokens
830===============================
831
832Any character, that is neither a part of a name, nor of a quoted string,
833nor a comment, is a token by itself. When not in the context of macro
834expansion, all of these tokens are just copied to output. However,
835during macro expansion, whitespace characters (space, tab, newline,
836formfeed, carriage return, vertical tab), parentheses (`(' and `)'),
837comma (`,'), and dollar (`$') have additional roles, explained later.
838
839
840File: m4.info, Node: Input processing, Prev: Other tokens, Up: Syntax
841
8423.5 How `m4' copies input to output
843===================================
844
845As `m4' reads the input token by token, it will copy each token
846directly to the output immediately.
847
848 The exception is when it finds a word with a macro definition. In
849that case `m4' will calculate the macro's expansion, possibly reading
850more input to get the arguments. It then inserts the expansion in front
851of the remaining input. In other words, the resulting text from a macro
852call will be read and parsed into tokens again.
853
854 `m4' expands a macro as soon as possible. If it finds a macro call
855when collecting the arguments to another, it will expand the second
856call first. For a running example, examine how `m4' handles this input:
857
858 format(`Result is %d', eval(`2**15'))
859
860First, `m4' sees that the token `format' is a macro name, so it
861collects the tokens `(', ``Result is %d'', `,', and ` ', before
862encountering another potential macro. Sure enough, `eval' is a macro
863name, so the nested argument collection picks up `(', ``2**15'', and
864`)', invoking the eval macro with the lone argument of `2**15'. The
865expansion of `eval(2**15)' is `32768', which is then rescanned as the
866five tokens `3', `2', `7', `6', and `8'; and combined with the next
867`)', the format macro now has all its arguments, as if the user had
868typed:
869
870 format(`Result is %d', 32768)
871
872The format macro expands to `Result is 32768', and we have another
873round of scanning for the tokens `Result', ` ', `is', ` ', `3', `2',
874`7', `6', and `8'. None of these are macros, so the final output is
875
876 =>Result is 32768
877
878 The order in which `m4' expands the macros can be explored using the
879trace facilities of GNU `m4' (*note Trace::).
880
881 This process continues until there are no more macro calls to expand
882and all the input has been consumed.
883
884
885File: m4.info, Node: Macros, Next: Definitions, Prev: Syntax, Up: Top
886
8874 How to invoke macros
888**********************
889
890This chapter covers macro invocation, macro arguments and how macro
891expansion is treated.
892
893* Menu:
894
895* Invocation:: Macro invocation
896* Inhibiting Invocation:: Preventing macro invocation
897* Macro Arguments:: Macro arguments
898* Quoting Arguments:: On Quoting Arguments to macros
899* Macro expansion:: Expanding macros
900
901
902File: m4.info, Node: Invocation, Next: Inhibiting Invocation, Up: Macros
903
9044.1 Macro invocation
905====================
906
907Macro invocations has one of the forms
908
909 name
910
911which is a macro invocation without any arguments, or
912
913 name(arg1, arg2, ..., argN)
914
915which is a macro invocation with N arguments. Macros can have any
916number of arguments. All arguments are strings, but different macros
917might interpret the arguments in different ways.
918
919 The opening parenthesis _must_ follow the NAME directly, with no
920spaces in between. If it does not, the macro is called with no
921arguments at all.
922
923 For a macro call to have no arguments, the parentheses _must_ be
924left out. The macro call
925
926 name()
927
928is a macro call with one argument, which is the empty string, not a call
929with no arguments.
930
931
932File: m4.info, Node: Inhibiting Invocation, Next: Macro Arguments, Prev: Invocation, Up: Macros
933
9344.2 Preventing macro invocation
935===============================
936
937An innovation of the `m4' language, compared to some of its
938predecessors (like Stratchey's `GPM', for example), is the ability to
939recognize macro calls without resorting to any special, prefixed
940invocation character. While generally useful, this feature might
941sometimes be the source of spurious, unwanted macro calls. So, GNU
942`m4' offers several mechanisms or techniques for inhibiting the
943recognition of names as macro calls.
944
945 First of all, many builtin macros cannot meaningfully be called
946without arguments. For any of these macros, whenever an opening
947parenthesis does not immediately follow their name, the builtin macro
948call is not triggered. This solves the most usual cases, like for
949`include' or `eval'. Later in this document, the sentence "This macro
950is recognized only with parameters" refers to this specific provision.
951
952 There is also a command line option (`--prefix-builtins', or `-P',
953*note Invoking m4: Operation modes.) that renames all builtin macros
954with a prefix of `m4_' at startup. The option has no effect whatsoever
955on user defined macros. For example, with this option, one has to
956write `m4_dnl' and even `m4_m4exit'. It also has no effect on whether
957a macro requires parameters.
958
959 Another alternative is to redefine problematic macros to a name less
960likely to cause conflicts, *Note Definitions::.
961
962 If your version of GNU `m4' has the `changeword' feature compiled
963in, it offers far more flexibility in specifying the syntax of macro
964names, both builtin or user-defined. *Note Changeword::, for more
965information on this experimental feature.
966
967 Of course, the simplest way to prevent a name from being interpreted
968as a call to an existing macro is to quote it. The remainder of this
969section studies a little more deeply how quoting affects macro
970invocation, and how quoting can be used to inhibit macro invocation.
971
972 Even if quoting is usually done over the whole macro name, it can
973also be done over only a few characters of this name (provided, of
974course, that the unquoted portions are not also a macro). It is also
975possible to quote the empty string, but this works only _inside_ the
976name. For example:
977
978 `divert'
979 =>divert
980 `d'ivert
981 =>divert
982 di`ver't
983 =>divert
984 div`'ert
985 =>divert
986
987all yield the string `divert'. While in both:
988
989 `'divert
990 =>
991 divert`'
992 =>
993
994the `divert' builtin macro will be called, which expands to the empty
995string.
996
997 The output of macro evaluations is always rescanned. The following
998example would yield the string `de', exactly as if `m4' has been given
999`substr(`abcde', `3', `2')' as input:
1000
1001 define(`x', `substr(ab')
1002 =>
1003 define(`y', `cde, `3', `2')')
1004 =>
1005 x`'y
1006 =>de
1007
1008 Unquoted strings on either side of a quoted string are subject to
1009being recognized as macro names. In the following example, quoting the
1010empty string allows for the second `macro' to be recognized as such:
1011
1012 define(`macro', `m')
1013 =>
1014 macro(`m')macro
1015 =>mmacro
1016 macro(`m')`'macro
1017 =>mm
1018
1019 Quoting may prevent recognizing as a macro name the concatenation of
1020a macro expansion with the surrounding characters. In this example:
1021
1022 define(`macro', `di$1')
1023 =>
1024 macro(`v')`ert'
1025 =>divert
1026 macro(`v')ert
1027 =>
1028
1029the input will produce the string `divert'. When the quotes were
1030removed, the `divert' builtin was called instead.
1031
1032
1033File: m4.info, Node: Macro Arguments, Next: Quoting Arguments, Prev: Inhibiting Invocation, Up: Macros
1034
10354.3 Macro arguments
1036===================
1037
1038When a name is seen, and it has a macro definition, it will be expanded
1039as a macro.
1040
1041 If the name is followed by an opening parenthesis, the arguments
1042will be collected before the macro is called. If too few arguments are
1043supplied, the missing arguments are taken to be the empty string.
1044However, some builtins are documented to behave differently for a
1045missing optional argument than for an explicit empty string. If there
1046are too many arguments, the excess arguments are ignored. Unquoted
1047leading whitespace is stripped off all arguments, but whitespace
1048generated by a macro expansion or occuring after a macro that expanded
1049to an empty string remains intact. Whitespace includes space, tab,
1050newline, carriage return, vertical tab, and formfeed.
1051
1052 define(`macro', `$1')
1053 =>
1054 macro( unquoted leading space lost)
1055 =>unquoted leading space lost
1056 macro(` quoted leading space kept')
1057 => quoted leading space kept
1058 macro(
1059 divert `unquoted space kept after expansion')
1060 => unquoted space kept after expansion
1061 macro(macro(`
1062 ')`whitespace from expansion kept')
1063 =>
1064 =>whitespace from expansion kept
1065 macro(`unquoted trailing whitespace kept'
1066 )
1067 =>unquoted trailing whitespace kept
1068 =>
1069
1070 Normally `m4' will issue warnings if a builtin macro is called with
1071an inappropriate number of arguments, but it can be suppressed with the
1072`--quiet' command line option (or `--silent', or `-Q', *note Invoking
1073m4: Operation modes.). For user defined macros, there is no check of
1074the number of arguments given.
1075
1076 Macros are expanded normally during argument collection, and whatever
1077commas, quotes and parentheses that might show up in the resulting
1078expanded text will serve to define the arguments as well. Thus, if FOO
1079expands to `, b, c', the macro call
1080
1081 bar(a foo, d)
1082
1083is a macro call with four arguments, which are `a ', `b', `c' and `d'.
1084To understand why the first argument contains whitespace, remember that
1085unquoted leading whitespace is never part of an argument, but trailing
1086whitespace always is.
1087
1088 It is possible for a macro's definition to change during argument
1089collection, in which case the expansion uses the definition that was in
1090effect at the time the opening `(' was seen.
1091
1092 define(`f', `1')
1093 =>
1094 f(define(`f', `2'))
1095 =>1
1096 f
1097 =>2
1098
1099 It is an error if the end of file occurs while collecting arguments.
1100
1101 hello world
1102 =>hello world
1103 define(
1104 ^D
1105 error-->m4:stdin:2: ERROR: end of file in argument list
1106
1107
1108File: m4.info, Node: Quoting Arguments, Next: Macro expansion, Prev: Macro Arguments, Up: Macros
1109
11104.4 On Quoting Arguments to macros
1111==================================
1112
1113Each argument has unquoted leading whitespace removed. Within each
1114argument, all unquoted parentheses must match. For example, if FOO is
1115a macro,
1116
1117 foo(() (`(') `(')
1118
1119is a macro call, with one argument, whose value is `() (() ('. Commas
1120separate arguments, except when they occur inside quotes, comments, or
1121unquoted parentheses. *Note Pseudo Arguments::, for examples.
1122
1123 It is common practice to quote all arguments to macros, unless you
1124are sure you want the arguments expanded. Thus, in the above example
1125with the parentheses, the `right' way to do it is like this:
1126
1127 foo(`() (() (')
1128
1129 It is, however, in certain cases necessary or convenient to leave out
1130quotes for some arguments, and there is nothing wrong in doing it. It
1131just makes life a bit harder, if you are not careful. For consistency,
1132this manual follows the rule of thumb that each layer of parentheses
1133introduces another layer of single quoting, except when showing the
1134consequences of quoting rules. This is done even when the quoted string
1135cannot be a macro, such as with integers when you have not changed the
1136syntax via `changeword' (*note Changeword::).
1137
1138
1139File: m4.info, Node: Macro expansion, Prev: Quoting Arguments, Up: Macros
1140
11414.5 Macro expansion
1142===================
1143
1144When the arguments, if any, to a macro call have been collected, the
1145macro is expanded, and the expansion text is pushed back onto the input
1146(unquoted), and reread. The expansion text from one macro call might
1147therefore result in more macros being called, if the calls are included,
1148completely or partially, in the first macro calls' expansion.
1149
1150 Taking a very simple example, if FOO expands to `bar', and BAR
1151expands to `Hello world', the input
1152
1153 foo
1154
1155will expand first to `bar', and when this is reread and expanded, into
1156`Hello world'.
1157
1158
1159File: m4.info, Node: Definitions, Next: Conditionals, Prev: Macros, Up: Top
1160
11615 How to define new macros
1162**************************
1163
1164Macros can be defined, redefined and deleted in several different ways.
1165Also, it is possible to redefine a macro without losing a previous
1166value, and bring back the original value at a later time.
1167
1168* Menu:
1169
1170* Define:: Defining a new macro
1171* Arguments:: Arguments to macros
1172* Pseudo Arguments:: Special arguments to macros
1173* Undefine:: Deleting a macro
1174* Defn:: Renaming macros
1175* Pushdef:: Temporarily redefining macros
1176
1177* Indir:: Indirect call of macros
1178* Builtin:: Indirect call of builtins
1179
1180
1181File: m4.info, Node: Define, Next: Arguments, Up: Definitions
1182
11835.1 Defining a macro
1184====================
1185
1186The normal way to define or redefine macros is to use the builtin
1187`define':
1188
1189 -- Builtin: define (NAME, [EXPANSION])
1190 Defines NAME to expand to EXPANSION. If EXPANSION is not given,
1191 it is taken to be empty.
1192
1193 The expansion of `define' is void. The macro `define' is
1194 recognized only with parameters.
1195
1196 The following example defines the macro FOO to expand to the text
1197`Hello World.'.
1198
1199 define(`foo', `Hello world.')
1200 =>
1201 foo
1202 =>Hello world.
1203
1204 The empty line in the output is there because the newline is not a
1205part of the macro definition, and it is consequently copied to the
1206output. This can be avoided by use of the macro `dnl'. *Note Dnl::,
1207for details.
1208
1209 The first argument to `define' should be quoted; otherwise, if the
1210macro is already defined, you will be defining a different macro. This
1211example shows the problems with underquoting, since we did not want to
1212redefine `one':
1213
1214 define(foo, one)
1215 =>
1216 define(foo, two)
1217 =>
1218 one
1219 =>two
1220
1221 GNU `m4' normally replaces only the _topmost_ definition of a macro
1222if it has several definitions from `pushdef' (*note Pushdef::). Some
1223other implementations of `m4' replace all definitions of a macro with
1224`define'. *Note Incompatibilities::, for more details.
1225
1226 As a GNU extension, the first argument to `define' does not have to
1227be a simple word. It can be any text string, even the empty string. A
1228macro with a non-standard name cannot be invoked in the normal way, as
1229the name is not recognized. It can only be referenced by the builtins
1230`Indir' (*note Indir::) and `Defn' (*note Defn::).
1231
1232 Arrays and associative arrays can be simulated by using this trick.
1233
1234 define(`array', `defn(format(``array[%d]'', `$1'))')
1235 =>
1236 define(`array_set', `define(format(``array[%d]'', `$1'), `$2')')
1237 =>
1238 array_set(`4', `array element no. 4')
1239 =>
1240 array_set(`17', `array element no. 17')
1241 =>
1242 array(`4')
1243 =>array element no. 4
1244 array(eval(`10 + 7'))
1245 =>array element no. 17
1246
1247 Change the `%d' to `%s' and it is an associative array.
1248
1249
1250File: m4.info, Node: Arguments, Next: Pseudo Arguments, Prev: Define, Up: Definitions
1251
12525.2 Arguments to macros
1253=======================
1254
1255Macros can have arguments. The Nth argument is denoted by `$n' in the
1256expansion text, and is replaced by the Nth actual argument, when the
1257macro is expanded. Replacement of arguments happens before rescanning,
1258regardless of how many nesting levels of quoting appear in the
1259expansion. Here is an example of a macro with two arguments. It
1260simply exchanges the order of the two arguments.
1261
1262 define(`exch', `$2, $1')
1263 =>
1264 exch(`arg1', `arg2')
1265 =>arg2, arg1
1266
1267 This can be used, for example, if you like the arguments to `define'
1268to be reversed.
1269
1270 define(`exch', `$2, $1')
1271 =>
1272 define(exch(``expansion text'', ``macro''))
1273 =>
1274 macro
1275 =>expansion text
1276
1277 *Note Quoting Arguments::, for an explanation of the double quotes.
1278(You should try and improve this example so that clients of `exch' do
1279not have to double quote; or *note Answers: Improved exch.).
1280
1281 GNU `m4' allows the number following the `$' to consist of one or
1282more digits, allowing macros to have any number of arguments. This is
1283not so in UNIX implementations of `m4', which only recognize one digit.
1284
1285 As a special case, the zeroth argument, `$0', is always the name of
1286the macro being expanded.
1287
1288 define(`test', ``Macro name: $0'')
1289 =>
1290 test
1291 =>Macro name: test
1292
1293 If you want quoted text to appear as part of the expansion text,
1294remember that quotes can be nested in quoted strings. Thus, in
1295
1296 define(`foo', `This is macro `foo'.')
1297 =>
1298 foo
1299 =>This is macro foo.
1300
1301The `foo' in the expansion text is _not_ expanded, since it is a quoted
1302string, and not a name.
1303
1304
1305File: m4.info, Node: Pseudo Arguments, Next: Undefine, Prev: Arguments, Up: Definitions
1306
13075.3 Special arguments to macros
1308===============================
1309
1310There is a special notation for the number of actual arguments supplied,
1311and for all the actual arguments.
1312
1313 The number of actual arguments in a macro call is denoted by `$#' in
1314the expansion text. Thus, a macro to display the number of arguments
1315given can be
1316
1317 define(`nargs', `$#')
1318 =>
1319 nargs
1320 =>0
1321 nargs()
1322 =>1
1323 nargs(`arg1', `arg2', `arg3')
1324 =>3
1325 nargs(`commas can be quoted, like this')
1326 =>1
1327 nargs(arg1#inside comments, commas do not separate arguments
1328 still arg1)
1329 =>1
1330 nargs((unquoted parentheses, like this, group arguments))
1331 =>1
1332
1333 The notation `$*' can be used in the expansion text to denote all
1334the actual arguments, unquoted, with commas in between. For example
1335
1336 define(`echo', `$*')
1337 =>
1338 echo(arg1, arg2, arg3 , arg4)
1339 =>arg1,arg2,arg3 ,arg4
1340
1341 Often each argument should be quoted, and the notation `$@' handles
1342that. It is just like `$*', except that it quotes each argument. A
1343simple example of that is:
1344
1345 define(`echo', `$@')
1346 =>
1347 echo(arg1, arg2, arg3 , arg4)
1348 =>arg1,arg2,arg3 ,arg4
1349
1350 Where did the quotes go? Of course, they were eaten, when the
1351expanded text were reread by `m4'. To show the difference, try
1352
1353 define(`echo1', `$*')
1354 =>
1355 define(`echo2', `$@')
1356 =>
1357 define(`foo', `This is macro `foo'.')
1358 =>
1359 echo1(foo)
1360 =>This is macro This is macro foo..
1361 echo1(`foo')
1362 =>This is macro foo.
1363 echo2(foo)
1364 =>This is macro foo.
1365 echo2(`foo')
1366 =>foo
1367
1368*Note Trace::, if you do not understand this. As another example of the
1369difference, remember that comments encountered in arguments are passed
1370untouched to the macro, and that quoting disables comments.
1371
1372 define(`echo1', `$*')
1373 =>
1374 define(`echo2', `$@')
1375 =>
1376 define(`foo', `bar')
1377 =>
1378 echo1(#foo'foo
1379 foo)
1380 =>#foo'foo
1381 =>bar
1382 echo2(#foo'foo
1383 foo)
1384 =>#foobar
1385 =>bar'
1386
1387 A `$' sign in the expansion text, that is not followed by anything
1388`m4' understands, is simply copied to the macro expansion, as any other
1389text is.
1390
1391 define(`foo', `$$$ hello $$$')
1392 =>
1393 foo
1394 =>$$$ hello $$$
1395
1396 If you want a macro to expand to something like `$12', the judicious
1397use of nested quoting can put a safe character between the `$' and the
1398next character, relying on the rescanning to remove the nested quote.
1399This will prevent `m4' from interpreting the `$' sign as a reference to
1400an argument.
1401
1402 define(`foo', `no nested quote: $1')
1403 =>
1404 foo(`arg')
1405 =>no nested quote: arg
1406 define(`foo', `nested quote around $: `$'1')
1407 =>
1408 foo(`arg')
1409 =>nested quote around $: $1
1410 define(`foo', `nested empty quote after $: $`'1')
1411 =>
1412 foo(`arg')
1413 =>nested empty quote after $: $1
1414 define(`foo', `nested quote around next character: $`1'')
1415 =>
1416 foo(`arg')
1417 =>nested quote around next character: $1
1418 define(`foo', `nested quote around both: `$1'')
1419 =>
1420 foo(`arg')
1421 =>nested quote around both: arg
1422
1423
1424File: m4.info, Node: Undefine, Next: Defn, Prev: Pseudo Arguments, Up: Definitions
1425
14265.4 Deleting a macro
1427====================
1428
1429A macro definition can be removed with `undefine':
1430
1431 -- Builtin: undefine (NAME...)
1432 For each argument, remove the macro NAME. The macro names must
1433 necessarily be quoted, since they will be expanded otherwise.
1434
1435 The expansion of `undefine' is void. The macro `undefine' is
1436 recognized only with parameters.
1437
1438 foo bar blah
1439 =>foo bar blah
1440 define(`foo', `some')define(`bar', `other')define(`blah', `text')
1441 =>
1442 foo bar blah
1443 =>some other text
1444 undefine(`foo')
1445 =>
1446 foo bar blah
1447 =>foo other text
1448 undefine(`bar', `blah')
1449 =>
1450 foo bar blah
1451 =>foo bar blah
1452
1453 Undefining a macro inside that macro's expansion is safe; the macro
1454still expands to the definition that was in effect at the `('.
1455
1456 define(`f', ``$0':$1')
1457 =>
1458 f(f(f(undefine(`f')`hello world')))
1459 =>f:f:f:hello world
1460 f(`bye')
1461 =>f(bye)
1462
1463 It is not an error for NAME to have no macro definition. In that
1464case, `undefine' does nothing.
1465
1466
1467File: m4.info, Node: Defn, Next: Pushdef, Prev: Undefine, Up: Definitions
1468
14695.5 Renaming macros
1470===================
1471
1472It is possible to rename an already defined macro. To do this, you need
1473the builtin `defn':
1474
1475 -- Builtin: defn (NAME)
1476 Expands to the _quoted definition_ of NAME. If the argument is
1477 not a defined macro, the expansion is void.
1478
1479 If NAME is a user-defined macro, the quoted definition is simply
1480 the quoted expansion text. If, instead, NAME is a builtin, the
1481 expansion is a special token, which points to the builtin's
1482 internal definition. This token is only meaningful as the second
1483 argument to `define' (and `pushdef'), and is silently converted to
1484 an empty string in most other contexts.
1485
1486 The macro `defn' is recognized only with parameters.
1487
1488 Its normal use is best understood through an example, which shows
1489how to rename `undefine' to `zap':
1490
1491 define(`zap', defn(`undefine'))
1492 =>
1493 zap(`undefine')
1494 =>
1495 undefine(`zap')
1496 =>undefine(zap)
1497
1498 In this way, `defn' can be used to copy macro definitions, and also
1499definitions of builtin macros. Even if the original macro is removed,
1500the other name can still be used to access the definition.
1501
1502 The fact that macro definitions can be transferred also explains why
1503you should use `$0', rather than retyping a macro's name in its
1504definition:
1505
1506 define(`foo', `This is `$0'')
1507 =>
1508 define(`bar', defn(`foo'))
1509 =>
1510 bar
1511 =>This is bar
1512
1513 Macros used as string variables should be referred through `defn',
1514to avoid unwanted expansion of the text:
1515
1516 define(`string', `The macro dnl is very useful
1517 ')
1518 =>
1519 string
1520 =>The macro
1521 defn(`string')
1522 =>The macro dnl is very useful
1523 =>
1524
1525 However, it is important to remember that `m4' rescanning is purely
1526textual. If an unbalanced end-quote string occurs in a macro
1527definition, the rescan will see that embedded quote as the termination
1528of the quoted string, and the remainder of the macro's definition will
1529be rescanned unquoted. Thus it is a good idea to avoid unbalanced
1530end-quotes in macro definitions or arguments to macros.
1531
1532 define(`foo', a'a)
1533 =>
1534 define(`a', `A')
1535 =>
1536 define(`echo', `$@')
1537 =>
1538 foo
1539 =>A'A
1540 defn(`foo')
1541 =>aA'
1542 echo(foo)
1543 =>AA'
1544
1545 Using `defn' to generate special tokens for builtin macros outside
1546of expected contexts can sometimes trigger warnings. But most of the
1547time, such tokens are silently converted to the empty string.
1548
1549 defn(`defn')
1550 =>
1551 define(defn(`divnum'), `cannot redefine a builtin token')
1552 error-->m4:stdin:2: Warning: define: invalid macro name ignored
1553 =>
1554 divnum
1555 =>0
1556
1557
1558File: m4.info, Node: Pushdef, Next: Indir, Prev: Defn, Up: Definitions
1559
15605.6 Temporarily redefining macros
1561=================================
1562
1563It is possible to redefine a macro temporarily, reverting to the
1564previous definition at a later time. This is done with the builtins
1565`pushdef' and `popdef':
1566
1567 -- Builtin: pushdef (NAME, [EXPANSION])
1568 -- Builtin: popdef (NAME...)
1569 Analogous to `define' and `undefine'.
1570
1571 These macros work in a stack-like fashion. A macro is temporarily
1572 redefined with `pushdef', which replaces an existing definition of
1573 NAME, while saving the previous definition, before the new one is
1574 installed. If there is no previous definition, `pushdef' behaves
1575 exactly like `define'.
1576
1577 If a macro has several definitions (of which only one is
1578 accessible), the topmost definition can be removed with `popdef'.
1579 If there is no previous definition, `popdef' behaves like
1580 `undefine'.
1581
1582 The expansion of both `pushdef' and `popdef' is void. The macros
1583 `pushdef' and `popdef' are recognized only with parameters.
1584
1585 define(`foo', `Expansion one.')
1586 =>
1587 foo
1588 =>Expansion one.
1589 pushdef(`foo', `Expansion two.')
1590 =>
1591 foo
1592 =>Expansion two.
1593 pushdef(`foo', `Expansion three.')
1594 =>
1595 pushdef(`foo', `Expansion four.')
1596 =>
1597 popdef(`foo')
1598 =>
1599 foo
1600 =>Expansion three.
1601 popdef(`foo', `foo')
1602 =>
1603 foo
1604 =>Expansion one.
1605 popdef(`foo')
1606 =>
1607 foo
1608 =>foo
1609
1610 If a macro with several definitions is redefined with `define', the
1611topmost definition is _replaced_ with the new definition. If it is
1612removed with `undefine', _all_ the definitions are removed, and not
1613only the topmost one.
1614
1615 define(`foo', `Expansion one.')
1616 =>
1617 foo
1618 =>Expansion one.
1619 pushdef(`foo', `Expansion two.')
1620 =>
1621 foo
1622 =>Expansion two.
1623 define(`foo', `Second expansion two.')
1624 =>
1625 foo
1626 =>Second expansion two.
1627 undefine(`foo')
1628 =>
1629 foo
1630 =>foo
1631
1632 Local variables within macros are made with `pushdef' and `popdef'.
1633At the start of the macro a new definition is pushed, within the macro
1634it is manipulated and at the end it is popped, revealing the former
1635definition.
1636
1637 It is possible to temporarily redefine a builtin with `pushdef' and
1638`defn'.
1639
1640
1641File: m4.info, Node: Indir, Next: Builtin, Prev: Pushdef, Up: Definitions
1642
16435.7 Indirect call of macros
1644===========================
1645
1646Any macro can be called indirectly with `indir':
1647
1648 -- Builtin: indir (NAME, [ARGS...])
1649 Results in a call to the macro NAME, which is passed the rest of
1650 the arguments ARGS. If NAME is not defined, an error message is
1651 printed, and the expansion is void.
1652
1653 The macro `indir' is recognized only with parameters.
1654
1655 This can be used to call macros with computed or "invalid" names
1656(`define' allows such names to be defined):
1657
1658 define(`$$internal$macro', `Internal macro (name `$0')')
1659 =>
1660 $$internal$macro
1661 =>$$internal$macro
1662 indir(`$$internal$macro')
1663 =>Internal macro (name $$internal$macro)
1664
1665 The point is, here, that larger macro packages can have private
1666macros defined, that will not be called by accident. They can _only_ be
1667called through the builtin `indir'.
1668
1669 One other point to observe is that argument collection occurs before
1670`indir' invokes NAME, so if argument collection changes the value of
1671NAME, that will be reflected in the final expansion. This is different
1672than the behavior when invoking macros directly, where the definition
1673that was in effect before argument collection is used.
1674
1675 define(`f', `1')
1676 =>
1677 f(define(`f', `2'))
1678 =>1
1679 indir(`f', define(`f', `3'))
1680 =>3
1681 indir(`f', undefine(`f'))
1682 error-->m4:stdin:4: undefined macro `f'
1683 =>
1684
1685 When handed the result of `defn' (*note Defn::) as one of its
1686arguments, `indir' defers to the invoked NAME for whether a token
1687representing a builtin is recognized or flattened to the empty string.
1688
1689 indir(defn(`defn'), `divnum')
1690 error-->m4:stdin:1: Warning: indir: invalid macro name ignored
1691 =>
1692 indir(`define', defn(`defn'), `divnum')
1693 error-->m4:stdin:2: Warning: define: invalid macro name ignored
1694 =>
1695 indir(`define', `foo', defn(`divnum'))
1696 =>
1697 foo
1698 =>0
1699 indir(`divert', defn(`foo'))
1700 error-->m4:stdin:5: empty string treated as 0 in builtin `divert'
1701 =>
1702
1703
1704File: m4.info, Node: Builtin, Prev: Indir, Up: Definitions
1705
17065.8 Indirect call of builtins
1707=============================
1708
1709Builtin macros can be called indirectly with `builtin':
1710
1711 -- Builtin: builtin (NAME, [ARGS...])
1712 Results in a call to the builtin NAME, which is passed the rest of
1713 the arguments ARGS. If NAME does not name a builtin, an error
1714 message is printed, and the expansion is void.
1715
1716 The macro `builtin' is recognized only with parameters.
1717
1718 This can be used even if NAME has been given another definition that
1719has covered the original, or been undefined so that no macro maps to
1720the builtin.
1721
1722 pushdef(`define', `hidden')
1723 =>
1724 undefine(`undefine')
1725 =>
1726 define(`foo', `bar')
1727 =>hidden
1728 foo
1729 =>foo
1730 builtin(`define', `foo', defn(`divnum'))
1731 =>
1732 foo
1733 =>0
1734 builtin(`define', `foo', `BAR')
1735 =>
1736 foo
1737 =>BAR
1738 undefine(`foo')
1739 =>undefine(foo)
1740 foo
1741 =>BAR
1742 builtin(`undefine', `foo')
1743 =>
1744 foo
1745 =>foo
1746
1747 The NAME argument only matches the original name of the builtin,
1748even when the `--prefix-builtins' option (or `-P', *note Invoking m4:
1749Operation modes.) is in effect. This is different from `indir', which
1750only tracks current macro names.
1751
1752 Note that `indir' and `builtin' can be used to invoke builtins
1753without arguments, even when they normally require parameters to be
1754recognized; but it will provoke a warning, and result in a void
1755expansion.
1756
1757 builtin
1758 =>builtin
1759 builtin()
1760 error-->m4:stdin:2: undefined builtin `'
1761 =>
1762 builtin(`builtin')
1763 error-->m4:stdin:3: Warning: too few arguments to builtin `builtin'
1764 =>
1765 builtin(`builtin',)
1766 error-->m4:stdin:4: undefined builtin `'
1767 =>
1768
1769
1770File: m4.info, Node: Conditionals, Next: Debugging, Prev: Definitions, Up: Top
1771
17726 Conditionals, loops, and recursion
1773************************************
1774
1775Macros, expanding to plain text, perhaps with arguments, are not quite
1776enough. We would like to have macros expand to different things, based
1777on decisions taken at run-time. For that, we need some kind of
1778conditionals. Also, we would like to have some kind of loop construct,
1779so we could do something a number of times, or while some condition is
1780true.
1781
1782* Menu:
1783
1784* Ifdef:: Testing if a macro is defined
1785* Ifelse:: If-else construct, or multibranch
1786* Shift:: Recursion in `m4'
1787* Forloop:: Iteration by counting
1788* Foreach:: Iteration by list contents
1789
1790
1791File: m4.info, Node: Ifdef, Next: Ifelse, Up: Conditionals
1792
17936.1 Testing if a macro is defined
1794=================================
1795
1796There are two different builtin conditionals in `m4'. The first is
1797`ifdef':
1798
1799 -- Builtin: ifdef (NAME, STRING-1, [STRING-2])
1800 If NAME is defined as a macro, `ifdef' expands to STRING-1,
1801 otherwise to STRING-2. If STRING-2 is omitted, it is taken to be
1802 the empty string (according to the normal rules).
1803
1804 The macro `ifdef' is recognized only with parameters.
1805
1806 ifdef(`foo', ``foo' is defined', ``foo' is not defined')
1807 =>foo is not defined
1808 define(`foo', `')
1809 =>
1810 ifdef(`foo', ``foo' is defined', ``foo' is not defined')
1811 =>foo is defined
1812 ifdef(`no_such_macro', `yes', `no', `extra argument')
1813 error-->m4:stdin:4: Warning: excess arguments to builtin `ifdef' ignored
1814 =>no
1815
1816
1817File: m4.info, Node: Ifelse, Next: Shift, Prev: Ifdef, Up: Conditionals
1818
18196.2 If-else construct, or multibranch
1820=====================================
1821
1822The other conditional, `ifelse', is much more powerful. It can be used
1823as a way to introduce a long comment, as an if-else construct, or as a
1824multibranch, depending on the number of arguments supplied:
1825
1826 -- Builtin: ifelse (COMMENT)
1827 -- Builtin: ifelse (STRING-1, STRING-2, EQUAL, [NOT-EQUAL])
1828 -- Builtin: ifelse (STRING-1, STRING-2, EQUAL-1, STRING-3, STRING-4,
1829 EQUAL-2, ...)
1830 Used with only one argument, the `ifelse' simply discards it and
1831 produces no output.
1832
1833 If called with three or four arguments, `ifelse' expands into
1834 EQUAL, if STRING-1 and STRING-2 are equal (character for
1835 character), otherwise it expands to NOT-EQUAL. A final fifth
1836 argument is ignored, after triggering a warning.
1837
1838 If called with six or more arguments, and STRING-1 and STRING-2
1839 are equal, `ifelse' expands into EQUAL-1, otherwise the first
1840 three arguments are discarded and the processing starts again.
1841
1842 The macro `ifelse' is recognized only with parameters.
1843
1844 Using only one argument is a common `m4' idiom for introducing a
1845block comment, as an alternative to repeatedly using `dnl'. This
1846special usage is recognized by GNU `m4', so that in this case, the
1847warning about missing arguments is never triggered.
1848
1849 ifelse(`some comments')
1850 =>
1851 ifelse(`foo', `bar')
1852 error-->m4:stdin:2: Warning: too few arguments to builtin `ifelse'
1853 =>
1854
1855 Using three or four arguments provides decision points.
1856
1857 ifelse(`foo', `bar', `true')
1858 =>
1859 ifelse(`foo', `foo', `true')
1860 =>true
1861 define(`foo', `bar')
1862 =>
1863 ifelse(foo, `bar', `true', `false')
1864 =>true
1865 ifelse(foo, `foo', `true', `false')
1866 =>false
1867
1868 Notice how the first argument was used unquoted; it is common to
1869compare the expansion of a macro with a string. With this macro, you
1870can now reproduce the behavior of many of the builtins, where the macro
1871is recognized only with arguments.
1872
1873 define(`foo', `ifelse(`$#', `0', ``$0'', `arguments:$#')')
1874 =>
1875 foo
1876 =>foo
1877 foo()
1878 =>arguments:1
1879 foo(`a', `b', `c')
1880 =>arguments:3
1881
1882 However, `ifelse' can take more than four arguments. If given more
1883than four arguments, `ifelse' works like a `case' or `switch' statement
1884in traditional programming languages. If STRING-1 and STRING-2 are
1885equal, `ifelse' expands into EQUAL-1, otherwise the procedure is
1886repeated with the first three arguments discarded. This calls for an
1887example:
1888
1889 ifelse(`foo', `bar', `third', `gnu', `gnats')
1890 error-->m4:stdin:1: Warning: excess arguments to builtin `ifelse' ignored
1891 =>gnu
1892 ifelse(`foo', `bar', `third', `gnu', `gnats', `sixth')
1893 =>
1894 ifelse(`foo', `bar', `third', `gnu', `gnats', `sixth', `seventh')
1895 =>seventh
1896 ifelse(`foo', `bar', `3', `gnu', `gnats', `6', `7', `8')
1897 error-->m4:stdin:4: Warning: excess arguments to builtin `ifelse' ignored
1898 =>7
1899
1900 Naturally, the normal case will be slightly more advanced than these
1901examples. A common use of `ifelse' is in macros implementing loops of
1902various kinds.
1903
1904
1905File: m4.info, Node: Shift, Next: Forloop, Prev: Ifelse, Up: Conditionals
1906
19076.3 Recursion in `m4'
1908=====================
1909
1910There is no direct support for loops in `m4', but macros can be
1911recursive. There is no limit on the number of recursion levels, other
1912than those enforced by your hardware and operating system.
1913
1914 Loops can be programmed using recursion and the conditionals
1915described previously.
1916
1917 There is a builtin macro, `shift', which can, among other things, be
1918used for iterating through the actual arguments to a macro:
1919
1920 -- Builtin: shift (ARG1, ...)
1921 Takes any number of arguments, and expands to all its arguments
1922 except ARG1, separated by commas, with each argument quoted.
1923
1924 The macro `shift' is recognized only with parameters.
1925
1926 shift
1927 =>shift
1928 shift(`bar')
1929 =>
1930 shift(`foo', `bar', `baz')
1931 =>bar,baz
1932
1933 An example of the use of `shift' is this macro:
1934
1935 -- Composite: reverse (...)
1936 Takes any number of arguments, and reverses their order.
1937
1938 It is implemented as:
1939
1940 define(`reverse', `ifelse(`$#', `0', , `$#', `1', ``$1'',
1941 `reverse(shift($@)), `$1'')')
1942 =>
1943 reverse
1944 =>
1945 reverse(`foo')
1946 =>foo
1947 reverse(`foo', `bar', `gnats', `and gnus')
1948 =>and gnus, gnats, bar, foo
1949
1950 While not a very interesting macro, it does show how simple loops
1951can be made with `shift', `ifelse' and recursion. It also shows that
1952`shift' is usually used with `$@'. Sometimes, a recursive algorithm
1953requires adding quotes to each element:
1954
1955 -- Composite: quote (...)
1956 -- Composite: dquote (...)
1957 -- Composite: dquote_elt (...)
1958 Takes any number of arguments, and adds quoting. With `quote',
1959 only one level of quoting is added, effectively removing whitespace
1960 after commas and turning multiple arguments into a single string.
1961 With `dquote', two levels of quoting are added, one around each
1962 element, and one around the list. And with `dquote_elt', two
1963 levels of quoting are added around each element.
1964
1965 An actual implementation of these three macros is distributed as
1966`m4-1.4.8/examples/quote.m4' in this package. First, let's examine
1967their usage:
1968
1969 include(`quote.m4')
1970 =>
1971 -quote-dquote-dquote_elt-
1972 =>----
1973 -quote()-dquote()-dquote_elt()-
1974 =>--`'-`'-
1975 -quote(`1')-dquote(`1')-dquote_elt(`1')-
1976 =>-1-`1'-`1'-
1977 -quote(`1', `2')-dquote(`1', `2')-dquote_elt(`1', `2')-
1978 =>-1,2-`1',`2'-`1',`2'-
1979 define(`n', `$#')dnl
1980 -n(quote(`1', `2'))-n(dquote(`1', `2'))-n(dquote_elt(`1', `2'))-
1981 =>-1-1-2-
1982 dquote(dquote_elt(`1', `2'))
1983 =>``1'',``2''
1984 dquote_elt(dquote(`1', `2'))
1985 =>``1',`2''
1986
1987 The last two lines show that when given two arguments, `dquote'
1988results in one string, while `dquote_elt' results in two. Now, examine
1989the implementation. Note that `quote' and `dquote_elt' make decisions
1990based on their number of arguments, so that when called without
1991arguments, they result in nothing instead of a quoted empty string;
1992this is so that it is possible to distinquish between no arguments and
1993an empty first argument. `dquote', on the other hand, results in a
1994string no matter what, since it is still possible to tell whether it
1995was invoked without arguments based on the resulting string.
1996
1997 undivert(`quote.m4')dnl
1998 =>divert(`-1')
1999 =># quote(args) - convert args to single-quoted string
2000 =>define(`quote', `ifelse(`$#', `0', `', ``$*'')')
2001 =># dquote(args) - convert args to quoted list of quoted strings
2002 =>define(`dquote', ``$@'')
2003 =># dquote_elt(args) - convert args to list of double-quoted strings
2004 =>define(`dquote_elt', `ifelse(`$#', `0', `', `$#', `1', ```$1''',
2005 => ```$1'',$0(shift($@))')')
2006 =>divert`'dnl
2007
2008
2009File: m4.info, Node: Forloop, Next: Foreach, Prev: Shift, Up: Conditionals
2010
20116.4 Iteration by counting
2012=========================
2013
2014Here is an example of a loop macro that implements a simple for loop.
2015
2016 -- Composite: forloop (ITERATOR, START, END, TEXT)
2017 Takes the name in ITERATOR, which must be a valid macro name, and
2018 successively assign it each integer value from START to END,
2019 inclusive. For each assignment to ITERATOR, append TEXT to the
2020 expansion of the `forloop'. TEXT may refer to ITERATOR. Any
2021 definition of ITERATOR prior to this invocation is restored.
2022
2023 It can, for example, be used for simple counting:
2024
2025 include(`forloop.m4')
2026 =>
2027 forloop(`i', `1', `8', `i ')
2028 =>1 2 3 4 5 6 7 8
2029
2030 For-loops can be nested, like:
2031
2032 include(`forloop.m4')
2033 =>
2034 forloop(`i', `1', `4', `forloop(`j', `1', `8', ` (i, j)')
2035 ')
2036 => (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8)
2037 => (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8)
2038 => (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8)
2039 => (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8)
2040 =>
2041
2042 The implementation of the `forloop' macro is fairly straightforward.
2043The `forloop' macro itself is simply a wrapper, which saves the
2044previous definition of the first argument, calls the internal macro
2045`_forloop', and re-establishes the saved definition of the first
2046argument.
2047
2048 The macro `_forloop' expands the fourth argument once, and tests to
2049see if the iterator has reached the final value. If it has not
2050finished, it increments the iterator (using the predefined macro
2051`incr', *note Incr::), and recurses.
2052
2053 Here is an actual implementation of `forloop', distributed as
2054`m4-1.4.8/examples/forloop.m4' in this package:
2055
2056 undivert(`forloop.m4')dnl
2057 =>divert(`-1')
2058 =># forloop(var, from, to, stmt) - simple version
2059 =>define(`forloop', `pushdef(`$1', `$2')_forloop($@)popdef(`$1')')
2060 =>define(`_forloop',
2061 => `$4`'ifelse($1, `$3', `', `define(`$1', incr($1))$0($@)')')
2062 =>divert`'dnl
2063
2064 Notice the careful use of quotes. Certain macro arguments are left
2065unquoted, each for its own reason. Try to find out _why_ these
2066arguments are left unquoted, and see what happens if they are quoted.
2067(As presented, these two macros are useful but not very robust for
2068general use. They lack even basic error handling for cases like START
2069less than END, END not numeric, or ITERATOR not being a macro name.
2070See if you can improve these macros; or *note Answers: Improved
2071forloop.).
2072
2073
2074File: m4.info, Node: Foreach, Prev: Forloop, Up: Conditionals
2075
20766.5 Iteration by list contents
2077==============================
2078
2079Here is an example of a loop macro that implements list iteration.
2080
2081 -- Composite: foreach (ITERATOR, PAREN-LIST, TEXT)
2082 -- Composite: foreachq (ITERATOR, QUOTE-LIST, TEXT)
2083 Takes the name in ITERATOR, which must be a valid macro name, and
2084 successively assign it each value from PAREN-LIST or QUOTE-LIST.
2085 In `foreach', PAREN-LIST is a comma-separated list of elements
2086 contained in parentheses. In `foreachq', QUOTE-LIST is a
2087 comma-separated list of elements contained in a quoted string.
2088 For each assignment to ITERATOR, append TEXT to the overall
2089 expansion. TEXT may refer to ITERATOR. Any definition of
2090 ITERATOR prior to this invocation is restored.
2091
2092 As an example, this displays each word in a list inside of a
2093sentence, using an implementation of `foreach' distributed as
2094`m4-1.4.8/examples/foreach.m4', and `foreachq' in
2095`m4-1.4.8/examples/foreachq.m4'.
2096
2097 include(`foreach.m4')
2098 =>
2099 foreach(`x', (foo, bar, foobar), `Word was: x
2100 ')dnl
2101 =>Word was: foo
2102 =>Word was: bar
2103 =>Word was: foobar
2104 include(`foreachq.m4')
2105 =>
2106 foreachq(`x', `foo, bar, foobar', `Word was: x
2107 ')dnl
2108 =>Word was: foo
2109 =>Word was: bar
2110 =>Word was: foobar
2111
2112 It is possible to be more complex; each element of the PAREN-LIST or
2113QUOTE-LIST can itself be a list, to pass as further arguments to a
2114helper macro. This example generates a shell case statement:
2115
2116 include(`foreach.m4')
2117 =>
2118 define(`_case', ` $1)
2119 $2=" $1";;
2120 ')dnl
2121 define(`_cat', `$1$2')dnl
2122 case $`'1 in
2123 =>case $1 in
2124 foreach(`x', `(`(`a', `vara')', `(`b', `varb')', `(`c', `varc')')',
2125 `_cat(`_case', x)')dnl
2126 => a)
2127 => vara=" a";;
2128 => b)
2129 => varb=" b";;
2130 => c)
2131 => varc=" c";;
2132 esac
2133 =>esac
2134
2135 The implementation of the `foreach' macro is a bit more involved; it
2136is a wrapper around two helper macros. First, `_arg1' is needed to
2137grab the first element of a list. Second, `_foreach' implements the
2138recursion, successively walking through the original list. Here is a
2139simple implementation of `foreach':
2140
2141 undivert(`foreach.m4')dnl
2142 =>divert(`-1')
2143 =># foreach(x, (item_1, item_2, ..., item_n), stmt)
2144 =># parenthesized list, simple version
2145 =>define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')')
2146 =>define(`_arg1', `$1')
2147 =>define(`_foreach', `ifelse(`$2', `()', `',
2148 => `define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')')
2149 =>divert`'dnl
2150
2151 Unfortunately, that implementation is not robust to macro names as
2152list elements. Each iteration of `_foreach' is stripping another layer
2153of quotes, leading to erratic results if list elements are not already
2154fully expanded. The first cut at implementing `foreachq' takes this
2155into account. Also, when using quoted elements in a PAREN-LIST, the
2156overall list must be quoted. A QUOTE-LIST has the nice property of
2157requiring fewer characters to create a list containing the same quoted
2158elements. To see the difference between the two macros, we attempt to
2159pass double-quoted macro names in a list, expecting the macro name on
2160output after one layer of quotes is removed during list iteration and
2161the final layer removed during the final rescan:
2162
2163 define(`a', `1')define(`b', `2')define(`c', `3')
2164 =>
2165 include(`foreach.m4')
2166 =>
2167 include(`foreachq.m4')
2168 =>
2169 foreach(`x', `(``a'', ``(b'', ``c)'')', `x
2170 ')
2171 =>1
2172 =>(2)1
2173 =>
2174 =>, x
2175 =>)
2176 foreachq(`x', ```a'', ``(b'', ``c)''', `x
2177 ')dnl
2178 =>a
2179 =>(b
2180 =>c)
2181
2182 Obviously, `foreachq' did a better job; here is its implementation:
2183
2184 undivert(`foreachq.m4')dnl
2185 =>include(`quote.m4')dnl
2186 =>divert(`-1')
2187 =># foreachq(x, `item_1, item_2, ..., item_n', stmt)
2188 =># quoted list, simple version
2189 =>define(`foreachq', `pushdef(`$1')_foreachq($@)popdef(`$1')')
2190 =>define(`_arg1', `$1')
2191 =>define(`_foreachq', `ifelse(quote($2), `', `',
2192 => `define(`$1', `_arg1($2)')$3`'$0(`$1', `shift($2)', `$3')')')
2193 =>divert`'dnl
2194
2195 Notice that `_foreachq' had to use the helper macro `quote' defined
2196earlier (*note Shift::), to ensure that the embedded `ifelse' call does
2197not go haywire if a list element contains a comma. Unfortunately, this
2198implementation of `foreachq' has its own severe flaw. Whereas the
2199`foreach' implementation was linear, this macro is quadratic in the
2200number of list elements, and is much more likely to trip up the limit
2201set by the command line option `--nesting-limit' (or `-L', *note
2202Invoking m4: Limits control.). (It is possible to have robust
2203iteration with linear behavior for either list style. See if you can
2204learn from the best elements of both of these implementations to create
2205robust macros; or *note Answers: Improved foreach.).
2206
2207
2208File: m4.info, Node: Debugging, Next: Input Control, Prev: Conditionals, Up: Top
2209
22107 How to debug macros and input
2211*******************************
2212
2213When writing macros for `m4', they often do not work as intended on the
2214first try (as is the case with most programming languages).
2215Fortunately, there is support for macro debugging in `m4'.
2216
2217* Menu:
2218
2219* Dumpdef:: Displaying macro definitions
2220* Trace:: Tracing macro calls
2221* Debug Levels:: Controlling debugging output
2222* Debug Output:: Saving debugging output
2223
2224
2225File: m4.info, Node: Dumpdef, Next: Trace, Up: Debugging
2226
22277.1 Displaying macro definitions
2228================================
2229
2230If you want to see what a name expands into, you can use the builtin
2231`dumpdef':
2232
2233 -- Builtin: dumpdef ([NAMES...])
2234 Accepts any number of arguments. If called without any arguments,
2235 it displays the definitions of all known names, otherwise it
2236 displays the definitions of the NAMES given. The output is
2237 printed to the current debug file (usually standard error), and is
2238 sorted by name. If an unknown name is encountered, a warning is
2239 printed.
2240
2241 The expansion of `dumpdef' is void.
2242
2243 define(`foo', `Hello world.')
2244 =>
2245 dumpdef(`foo')
2246 error-->foo: `Hello world.'
2247 =>
2248 dumpdef(`define')
2249 error-->define: <define>
2250 =>
2251
2252 The last example shows how builtin macros definitions are displayed.
2253The definition that is dumped corresponds to what would occur if the
2254macro were to be called at that point, even if other definitions are
2255still live due to redefining a macro during argument collection.
2256
2257 pushdef(`f', ``$0'1')pushdef(`f', ``$0'2')
2258 =>
2259 f(popdef(`f')dumpdef(`f'))
2260 error-->f: ``$0'1'
2261 =>f2
2262 f(popdef(`f')dumpdef(`f'))
2263 error-->m4:stdin:3: undefined macro `f'
2264 =>f1
2265
2266 *Note Debug Levels::, for information on controlling the details of
2267the display.
2268
2269
2270File: m4.info, Node: Trace, Next: Debug Levels, Prev: Dumpdef, Up: Debugging
2271
22727.2 Tracing macro calls
2273=======================
2274
2275It is possible to trace macro calls and expansions through the builtins
2276`traceon' and `traceoff':
2277
2278 -- Builtin: traceon ([NAMES...])
2279 -- Builtin: traceoff ([NAMES...])
2280 When called without any arguments, `traceon' and `traceoff' will
2281 turn tracing on and off, respectively, for all defined macros.
2282
2283 When called with arguments, only the macros listed in NAMES are
2284 affected, whether or not they are currently defined.
2285
2286 The expansion of `traceon' and `traceoff' is void.
2287
2288 Whenever a traced macro is called and the arguments have been
2289collected, the call is displayed. If the expansion of the macro call
2290is not void, the expansion can be displayed after the call. The output
2291is printed to the current debug file (defaulting to standard error,
2292*note Debug Output::).
2293
2294 define(`foo', `Hello World.')
2295 =>
2296 define(`echo', `$@')
2297 =>
2298 traceon(`foo', `echo')
2299 =>
2300 foo
2301 error-->m4trace: -1- foo -> `Hello World.'
2302 =>Hello World.
2303 echo(`gnus', `and gnats')
2304 error-->m4trace: -1- echo(`gnus', `and gnats') -> ``gnus',`and gnats''
2305 =>gnus,and gnats
2306
2307 The number between dashes is the depth of the expansion. It is one
2308most of the time, signifying an expansion at the outermost level, but it
2309increases when macro arguments contain unquoted macro calls. The
2310maximum number that will appear between dashes is controlled by the
2311option `--nesting-limit' (*note Invoking m4: Limits control.).
2312
2313 Tracing by name is an attribute that is preserved whether the macro
2314is defined or not. This allows the `-t' option to select macros to
2315trace before those macros are defined.
2316
2317 traceoff(`foo')
2318 =>
2319 traceon(`foo')
2320 =>
2321 foo
2322 =>foo
2323 define(`foo', `bar')
2324 =>
2325 foo
2326 error-->m4trace: -1- foo -> `bar'
2327 =>bar
2328 undefine(`foo')
2329 =>
2330 ifdef(`foo', `yes', `no')
2331 =>no
2332 indir(`foo')
2333 error-->m4:stdin:8: undefined macro `foo'
2334 =>
2335 define(`foo', `blah')
2336 =>
2337 foo
2338 error-->m4trace: -1- foo -> `blah'
2339 =>blah
2340 traceoff
2341 =>
2342 foo
2343 =>blah
2344
2345 Tracing even works on builtins. However, `defn' (*note Defn::) does
2346not transfer tracing status.
2347
2348 traceon(`eval', `m4_divnum')
2349 =>
2350 define(`m4_eval', defn(`eval'))
2351 =>
2352 define(`m4_divnum', defn(`divnum'))
2353 =>
2354 eval(divnum)
2355 error-->m4trace: -1- eval(`0') -> `0'
2356 =>0
2357 m4_eval(m4_divnum)
2358 error-->m4trace: -2- m4_divnum -> `0'
2359 =>0
2360
2361 *Note Debug Levels::, for information on controlling the details of
2362the display.
2363
2364
2365File: m4.info, Node: Debug Levels, Next: Debug Output, Prev: Trace, Up: Debugging
2366
23677.3 Controlling debugging output
2368================================
2369
2370The `-d' option to `m4' (*note Invoking m4: Debugging options.)
2371controls the amount of details presented, when using the macros
2372described in the preceding sections.
2373
2374 The FLAGS following the option can be one or more of the following:
2375
2376`a'
2377 Show the actual arguments in each macro call. This applies to all
2378 macro calls if the `t' flag is used, otherwise only the macros
2379 covered by calls of `traceon'.
2380
2381`c'
2382 Show several trace lines for each macro call. A line is shown
2383 when the macro is seen, but before the arguments are collected; a
2384 second line when the arguments have been collected and a third
2385 line after the call has completed.
2386
2387`e'
2388 Show the expansion of each macro call, if it is not void. This
2389 applies to all macro calls if the `t' flag is used, otherwise only
2390 the macros covered by calls of `traceon'.
2391
2392`f'
2393 Show the name of the current input file in each trace output line.
2394
2395`i'
2396 Print a message each time the current input file is changed,
2397 giving file name and input line number.
2398
2399`l'
2400 Show the current input line number in each trace output line.
2401
2402`p'
2403 Print a message when a named file is found through the path search
2404 mechanism (*note Search Path::), giving the actual file name used.
2405
2406`q'
2407 Quote actual arguments and macro expansions in the display with the
2408 current quotes.
2409
2410`t'
2411 Trace all macro calls made in this invocation of `m4'.
2412
2413`x'
2414 Add a unique `macro call id' to each line of the trace output.
2415 This is useful in connection with the `c' flag above.
2416
2417`V'
2418 A shorthand for all of the above flags.
2419
2420 If no flags are specified with the `-d' option, the default is
2421`aeq'. The examples throughout this manual assume the default flags.
2422
2423 There is a builtin macro `debugmode', which allows on-the-fly
2424control of the debugging output format:
2425
2426 -- Builtin: debugmode ([FLAGS])
2427 The argument FLAGS should be a subset of the letters listed above.
2428 As special cases, if the argument starts with a `+', the flags are
2429 added to the current debug flags, and if it starts with a `-', they
2430 are removed. If no argument is present, all debugging flags are
2431 cleared (as if no `-d' was given), and with an empty argument the
2432 flags are reset to the default of `aeq'.
2433
2434 The expansion of `debugmode' is void.
2435
2436 define(`foo', `FOO')
2437 =>
2438 traceon(`foo')
2439 =>
2440 debugmode()
2441 =>
2442 foo
2443 error-->m4trace: -1- foo -> `FOO'
2444 =>FOO
2445 debugmode
2446 =>
2447 foo
2448 error-->m4trace: -1- foo
2449 =>FOO
2450 debugmode(`+l')
2451 =>
2452 foo
2453 error-->m4trace:8: -1- foo
2454 =>FOO
2455
2456
2457File: m4.info, Node: Debug Output, Prev: Debug Levels, Up: Debugging
2458
24597.4 Saving debugging output
2460===========================
2461
2462Debug and tracing output can be redirected to files using either the
2463`--debugfile' option to `m4' (*note Invoking m4: Debugging options.),
2464or with the builtin macro `debugfile':
2465
2466 -- Builtin: debugfile ([FILE])
2467 Sends all further debug and trace output to FILE, opened in append
2468 mode. If FILE is the empty string, debug and trace output are
2469 discarded. If `debugfile' is called without any arguments, debug
2470 and trace output are sent to standard error. This does not affect
2471 warnings, error messages, or `errprint' output, which are always
2472 sent to standard error. If FILE cannot be opened, the current
2473 debug file is unchanged, and an error is issued.
2474
2475 The expansion of `debugfile' is void.
2476
2477 traceon(`divnum')
2478 =>
2479 divnum(`extra')
2480 error-->m4:stdin:2: Warning: excess arguments to builtin `divnum' ignored
2481 error-->m4trace: -1- divnum(`extra') -> `0'
2482 =>0
2483 debugfile()
2484 =>
2485 divnum(`extra')
2486 error-->m4:stdin:4: Warning: excess arguments to builtin `divnum' ignored
2487 =>0
2488 debugfile
2489 =>
2490 divnum
2491 error-->m4trace: -1- divnum -> `0'
2492 =>0
2493
2494
2495File: m4.info, Node: Input Control, Next: File Inclusion, Prev: Debugging, Up: Top
2496
24978 Input control
2498***************
2499
2500This chapter describes various builtin macros for controlling the input
2501to `m4'.
2502
2503* Menu:
2504
2505* Dnl:: Deleting whitespace in input
2506* Changequote:: Changing the quote characters
2507* Changecom:: Changing the comment delimiters
2508* Changeword:: Changing the lexical structure of words
2509* M4wrap:: Saving text until end of input
2510
2511
2512File: m4.info, Node: Dnl, Next: Changequote, Up: Input Control
2513
25148.1 Deleting whitespace in input
2515================================
2516
2517The builtin `dnl' stands for "Discard to Next Line":
2518
2519 -- Builtin: dnl
2520 All characters, up to and including the next newline, are discarded
2521 without performing any macro expansion. A warning is issued if
2522 the end of the file is encountered without a newline.
2523
2524 The expansion of `dnl' is void.
2525
2526 It is often used in connection with `define', to remove the newline
2527that follows the call to `define'. Thus
2528
2529 define(`foo', `Macro `foo'.')dnl A very simple macro, indeed.
2530 foo
2531 =>Macro foo.
2532
2533 The input up to and including the next newline is discarded, as
2534opposed to the way comments are treated (*note Comments::).
2535
2536 Usually, `dnl' is immediately followed by an end of line or some
2537other whitespace. GNU `m4' will produce a warning diagnostic if `dnl'
2538is followed by an open parenthesis. In this case, `dnl' will collect
2539and process all arguments, looking for a matching close parenthesis.
2540All predictable side effects resulting from this collection will take
2541place. `dnl' will return no output. The input following the matching
2542close parenthesis up to and including the next newline, on whatever
2543line containing it, will still be discarded.
2544
2545 dnl(`args are ignored, but side effects occur',
2546 define(`foo', `like this')) while this text is ignored: undefine(`foo')
2547 error-->m4:stdin:1: Warning: excess arguments to builtin `dnl' ignored
2548 See how `foo' was defined, foo?
2549 =>See how foo was defined, like this?
2550
2551 If the end of file is encountered without a newline character, a
2552warning is issued and dnl stops consuming input.
2553
2554 m4wrap(`m4wrap(`2 hi
2555 ')0 hi dnl 1 hi')
2556 =>
2557 define(`hi', `HI')
2558 =>
2559 ^D
2560 error-->m4:stdin:1: Warning: end of file treated as newline
2561 =>0 HI 2 HI
2562
2563
2564File: m4.info, Node: Changequote, Next: Changecom, Prev: Dnl, Up: Input Control
2565
25668.2 Changing the quote characters
2567=================================
2568
2569The default quote delimiters can be changed with the builtin
2570`changequote':
2571
2572 -- Builtin: changequote ([START = ``'], [END = `''])
2573 This sets START as the new begin-quote delimiter and END as the
2574 new end-quote delimiter. If both arguments are missing, the
2575 default quotes (``' and `'') are used. If START is void, then
2576 quoting is disabled. Otherwise, if END is missing or void, the
2577 default end-quote delimiter (`'') is used. The quote delimiters
2578 can be of any length.
2579
2580 The expansion of `changequote' is void.
2581
2582 changequote(`[', `]')
2583 =>
2584 define([foo], [Macro [foo].])
2585 =>
2586 foo
2587 =>Macro foo.
2588
2589 The quotation strings can safely contain eight-bit characters. If
2590no single character is appropriate, START and END can be of any length.
2591Other implementations cap the delimiter length to five characters, but
2592GNU has no inherent limit.
2593
2594 changequote(`[[[', `]]]')
2595 =>
2596 define([[[foo]]], [[[Macro [[[[[foo]]]]].]]])
2597 =>
2598 foo
2599 =>Macro [[foo]].
2600
2601 Calling `changequote' with START as the empty string will
2602effectively disable the quoting mechanism, leaving no way to quote text.
2603However, using an empty string is not portable, as some other
2604implementations of `m4' revert to the default quoting, while others
2605preserve the prior non-empty delimiter. If START is not empty, then an
2606empty END will use the default end-quote delimiter of `'', as
2607otherwise, it would be impossible to end a quoted string. Again, this
2608is not portable, as some other `m4' implementations reuse START as the
2609end-quote delimiter, while others preserve the previous non-empty
2610value. Omitting both arguments restores the default begin-quote and
2611end-quote delimiters; fortunately this behavior is portable to all
2612implementations of `m4'.
2613
2614 define(`foo', `Macro `FOO'.')
2615 =>
2616 changequote(`', `')
2617 =>
2618 foo
2619 =>Macro `FOO'.
2620 `foo'
2621 =>`Macro `FOO'.'
2622 changequote(`,)
2623 =>
2624 foo
2625 =>Macro FOO.
2626
2627 There is no way in `m4' to quote a string containing an unmatched
2628begin-quote, except using `changequote' to change the current quotes.
2629
2630 If the quotes should be changed from, say, `[' to `[[', temporary
2631quote characters have to be defined. To achieve this, two calls of
2632`changequote' must be made, one for the temporary quotes and one for
2633the new quotes.
2634
2635 Macros are recognized in preference to the begin-quote string, so if
2636a prefix of START can be recognized as part of a potential macro name,
2637the quoting mechanism is effectively disabled. Unless you use
2638`changeword' (*note Changeword::), this means that START should not
2639begin with a letter, digit, or `_' (underscore). However, even though
2640quoted strings are not recognized, the quote characters can still be
2641discerned in macro expansion and in trace output.
2642
2643 define(`echo', `$@')
2644 =>
2645 define(`hi', `HI')
2646 =>
2647 changequote(`q', `Q')
2648 =>
2649 q hi Q hi
2650 =>q HI Q HI
2651 echo(hi)
2652 =>qHIQ
2653 changequote
2654 =>
2655 changequote(`-', `EOF')
2656 =>
2657 - hi EOF hi
2658 => hi HI
2659 changequote
2660 =>
2661 changequote(`1', `2')
2662 =>
2663 hi1hi2
2664 =>hi1hi2
2665 hi 1hi2
2666 =>HI hi
2667
2668 Quotes are recognized in preference to argument collection. In
2669particular, if START is a single `(', then argument collection is
2670effectively disabled. For portability with other implementations, it
2671is a good idea to avoid `(', `,', and `)' as the first character in
2672START.
2673
2674 define(`echo', `$#:$@:')
2675 =>
2676 define(`hi', `HI')
2677 =>
2678 changequote(`(',`)')
2679 =>
2680 echo(hi)
2681 =>0::hi
2682 changequote
2683 =>
2684 changequote(`((', `))')
2685 =>
2686 echo(hi)
2687 =>1:HI:
2688 echo((hi))
2689 =>0::hi
2690 changequote
2691 =>
2692 changequote(`,', `)')
2693 =>
2694 echo(hi,hi)bye)
2695 =>1:HIhibye:
2696
2697 If END is a prefix of START, the end-quote will be recognized in
2698preference to a nested begin-quote. In particular, changing the quotes
2699to have the same string for START and END disables nesting of quotes.
2700When quote nesting is disabled, it is impossible to double-quote
2701strings across macro expansions, so using the same string is not done
2702very often.
2703
2704 define(`hi', `HI')
2705 =>
2706 changequote(`""', `"')
2707 =>
2708 ""hi"""hi"
2709 =>hihi
2710 ""hi" ""hi"
2711 =>hi hi
2712 ""hi"" "hi"
2713 =>hi" "HI"
2714 changequote
2715 =>
2716 `hi`hi'hi'
2717 =>hi`hi'hi
2718 changequote(`"', `"')
2719 =>
2720 "hi"hi"hi"
2721 =>hiHIhi
2722
2723 It is an error if the end of file occurs within a quoted string.
2724
2725 `hello world'
2726 =>hello world
2727 `dangling quote
2728 ^D
2729 error-->m4:stdin:2: ERROR: end of file in string
2730
2731
2732File: m4.info, Node: Changecom, Next: Changeword, Prev: Changequote, Up: Input Control
2733
27348.3 Changing the comment delimiters
2735===================================
2736
2737The default comment delimiters can be changed with the builtin macro
2738`changecom':
2739
2740 -- Builtin: changecom ([START], [END = `<NL>'])
2741 This sets START as the new begin-comment delimiter and END as the
2742 new end-comment delimiter. If both arguments are missing, or
2743 START is void, then comments are disabled. Otherwise, if END is
2744 missing or void, the default end-comment delimiter of newline is
2745 used. The comment delimiters can be of any length.
2746
2747 The expansion of `changecom' is void.
2748
2749 define(`comment', `COMMENT')
2750 =>
2751 # A normal comment
2752 =># A normal comment
2753 changecom(`/*', `*/')
2754 =>
2755 # Not a comment anymore
2756 =># Not a COMMENT anymore
2757 But: /* this is a comment now */ while this is not a comment
2758 =>But: /* this is a comment now */ while this is not a COMMENT
2759
2760 Note how comments are copied to the output, much as if they were
2761quoted strings. If you want the text inside a comment expanded, quote
2762the begin-comment delimiter.
2763
2764 Calling `changecom' without any arguments, or with START as the
2765empty string, will effectively disable the commenting mechanism. To
2766restore the original comment start of `#', you must explicitly ask for
2767it. If START is not empty, then an empty END will use the default
2768end-comment delimiter of newline, as otherwise, it would be impossible
2769to end a comment. However, this is not portable, as some other `m4'
2770implementations preserve the previous non-empty delimiters instead.
2771
2772 define(`comment', `COMMENT')
2773 =>
2774 changecom
2775 =>
2776 # Not a comment anymore
2777 =># Not a COMMENT anymore
2778 changecom(`#', `')
2779 =>
2780 # comment again
2781 =># comment again
2782
2783 The comment strings can safely contain eight-bit characters. If no
2784single character is appropriate, START and END can be of any length.
2785Other implementations cap the delimiter length to five characters, but
2786GNU has no inherent limit.
2787
2788 Comments are recognized in preference to macros. However, this is
2789not compatible with other implementations, where macros and even quoting
2790takes precedence over comments, so it may change in a future release.
2791For portability, this means that START should not begin with a letter,
2792digit, or `_' (underscore), and that neither the start-quote nor the
2793start-comment string should be a prefix of the other.
2794
2795 define(`hi', `HI')
2796 =>
2797 define(`hi1hi2', `hello')
2798 =>
2799 changecom(`q', `Q')
2800 =>
2801 q hi Q hi
2802 =>q hi Q HI
2803 changecom(`1', `2')
2804 =>
2805 hi1hi2
2806 =>hello
2807 hi 1hi2
2808 =>HI 1hi2
2809
2810 Comments are recognized in preference to argument collection. In
2811particular, if START is a single `(', then argument collection is
2812effectively disabled. For portability with other implementations, it
2813is a good idea to avoid `(', `,', and `)' as the first character in
2814START.
2815
2816 define(`echo', `$#:$@:')
2817 =>
2818 define(`hi', `HI')
2819 =>
2820 changecom(`(',`)')
2821 =>
2822 echo(hi)
2823 =>0::(hi)
2824 changecom
2825 =>
2826 changecom(`((', `))')
2827 =>
2828 echo(hi)
2829 =>1:HI:
2830 echo((hi))
2831 =>0::((hi))
2832 changecom(`,', `)')
2833 =>
2834 echo(hi,hi)bye)
2835 =>1:HI,hi)bye:
2836
2837 It is an error if the end of file occurs within a comment.
2838
2839 changecom(`/*', `*/')
2840 =>
2841 /*dangling comment
2842 ^D
2843 error-->m4:stdin:2: ERROR: end of file in comment
2844
2845
2846File: m4.info, Node: Changeword, Next: M4wrap, Prev: Changecom, Up: Input Control
2847
28488.4 Changing the lexical structure of words
2849===========================================
2850
2851 The macro `changeword' and all associated functionality is
2852 experimental. It is only available if the `--enable-changeword'
2853 option was given to `configure', at GNU `m4' installation time.
2854 The functionality will go away in the future, to be replaced by
2855 other new features that are more efficient at providing the same
2856 capabilities. _Do not rely on it_. Please direct your comments
2857 about it the same way you would do for bugs.
2858
2859 A file being processed by `m4' is split into quoted strings, words
2860(potential macro names) and simple tokens (any other single character).
2861Initially a word is defined by the following regular expression:
2862
2863 [_a-zA-Z][_a-zA-Z0-9]*
2864
2865 Using `changeword', you can change this regular expression:
2866
2867 -- Optional builtin: changeword (REGEX)
2868 Changes the regular expression for recognizing macro names to be
2869 REGEX. If REGEX is empty, use `[_a-zA-Z][_a-zA-Z0-9]*'. REGEX
2870 must obey the constraint that every prefix of the desired final
2871 pattern is also accepted by the regular expression. If REGEX
2872 contains grouping parentheses, the macro invoked is the portion
2873 that matched the first group, rather than the entire matching
2874 string.
2875
2876 The expansion of `changeword' is void. The macro `changeword' is
2877 recognized only with parameters.
2878
2879 Relaxing the lexical rules of `m4' might be useful (for example) if
2880you wanted to apply translations to a file of numbers:
2881
2882 ifdef(`changeword', `', `errprint(` skipping: no changeword support
2883 ')m4exit(`77')')dnl
2884 changeword(`[_a-zA-Z0-9]+')
2885 =>
2886 define(`1', `0')1
2887 =>0
2888
2889 Tightening the lexical rules is less useful, because it will
2890generally make some of the builtins unavailable. You could use it to
2891prevent accidental call of builtins, for example:
2892
2893 ifdef(`changeword', `', `errprint(` skipping: no changeword support
2894 ')m4exit(`77')')dnl
2895 define(`_indir', defn(`indir'))
2896 =>
2897 changeword(`_[_a-zA-Z0-9]*')
2898 =>
2899 esyscmd(`foo')
2900 =>esyscmd(foo)
2901 _indir(`esyscmd', `echo hi')
2902 =>hi
2903 =>
2904
2905 Because `m4' constructs its words a character at a time, there is a
2906restriction on the regular expressions that may be passed to
2907`changeword'. This is that if your regular expression accepts `foo',
2908it must also accept `f' and `fo'.
2909
2910 ifdef(`changeword', `', `errprint(` skipping: no changeword support
2911 ')m4exit(`77')')dnl
2912 define(`foo
2913 ', `bar
2914 ')
2915 =>
2916 dnl This example wants to recognize changeword, dnl, and `foo\n'.
2917 dnl First, we check that our regexp will match.
2918 regexp(`changeword', `[cd][a-z]*\|foo[
2919 ]')
2920 =>0
2921 regexp(`foo
2922 ', `[cd][a-z]*\|foo[
2923 ]')
2924 =>0
2925 regexp(`f', `[cd][a-z]*\|foo[
2926 ]')
2927 =>-1
2928 foo
2929 =>foo
2930 changeword(`[cd][a-z]*\|foo[
2931 ]')
2932 =>
2933 dnl Even though `foo\n' matches, we forgot to allow `f'.
2934 foo
2935 =>foo
2936 changeword(`[cd][a-z]*\|fo*[
2937 ]?')
2938 =>
2939 dnl Now we can call `foo\n'.
2940 foo
2941 =>bar
2942
2943 `changeword' has another function. If the regular expression
2944supplied contains any grouped subexpressions, then text outside the
2945first of these is discarded before symbol lookup. So:
2946
2947 ifdef(`changeword', `', `errprint(` skipping: no changeword support
2948 ')m4exit(`77')')dnl
2949 ifdef(`__unix__', ,
2950 `errprint(` skipping: syscmd does not have unix semantics
2951 ')m4exit(`77')')dnl
2952 changecom(`/*', `*/')dnl
2953 define(`foo', `bar')dnl
2954 changeword(`#\([_a-zA-Z0-9]*\)')
2955 =>
2956 #esyscmd(`echo foo \#foo')
2957 =>foo bar
2958 =>
2959
2960 `m4' now requires a `#' mark at the beginning of every macro
2961invocation, so one can use `m4' to preprocess plain text without losing
2962various words like `divert'.
2963
2964 In `m4', macro substitution is based on text, while in TeX, it is
2965based on tokens. `changeword' can throw this difference into relief.
2966For example, here is the same idea represented in TeX and `m4'. First,
2967the TeX version:
2968
2969 \def\a{\message{Hello}}
2970 \catcode`\@=0
2971 \catcode`\\=12
2972 @a
2973 @bye
2974 =>Hello
2975
2976Then, the `m4' version:
2977
2978 ifdef(`changeword', `', `errprint(` skipping: no changeword support
2979 ')m4exit(`77')')dnl
2980 define(`a', `errprint(`Hello')')dnl
2981 changeword(`@\([_a-zA-Z0-9]*\)')
2982 =>
2983 @a
2984 =>errprint(Hello)
2985
2986 In the TeX example, the first line defines a macro `a' to print the
2987message `Hello'. The second line defines <@> to be usable instead of
2988<\> as an escape character. The third line defines <\> to be a normal
2989printing character, not an escape. The fourth line invokes the macro
2990`a'. So, when TeX is run on this file, it displays the message `Hello'.
2991
2992 When the `m4' example is passed through `m4', it outputs
2993`errprint(Hello)'. The reason for this is that TeX does lexical
2994analysis of macro definition when the macro is _defined_. `m4' just
2995stores the text, postponing the lexical analysis until the macro is
2996_used_.
2997
2998 You should note that using `changeword' will slow `m4' down by a
2999factor of about seven, once it is changed to something other than the
3000default regular expression. You can invoke `changeword' with the empty
3001string to restore the default word definition, and regain the parsing
3002speed.
3003
3004
3005File: m4.info, Node: M4wrap, Prev: Changeword, Up: Input Control
3006
30078.5 Saving text until end of input
3008==================================
3009
3010It is possible to `save' some text until the end of the normal input has
3011been seen. Text can be saved, to be read again by `m4' when the normal
3012input has been exhausted. This feature is normally used to initiate
3013cleanup actions before normal exit, e.g., deleting temporary files.
3014
3015 To save input text, use the builtin `m4wrap':
3016
3017 -- Builtin: m4wrap (STRING, ...)
3018 Stores STRING in a safe place, to be reread when end of input is
3019 reached. As a GNU extension, additional arguments are
3020 concatenated with a space to the STRING.
3021
3022 The expansion of `m4wrap' is void. The macro `m4wrap' is
3023 recognized only with parameters.
3024
3025 define(`cleanup', `This is the `cleanup' action.
3026 ')
3027 =>
3028 m4wrap(`cleanup')
3029 =>
3030 This is the first and last normal input line.
3031 =>This is the first and last normal input line.
3032 ^D
3033 =>This is the cleanup action.
3034
3035 The saved input is only reread when the end of normal input is seen,
3036and not if `m4exit' is used to exit `m4'.
3037
3038 It is safe to call `m4wrap' from saved text, but then the order in
3039which the saved text is reread is undefined. If `m4wrap' is not used
3040recursively, the saved pieces of text are reread in the opposite order
3041in which they were saved (LIFO--last in, first out). However, this
3042behavior is likely to change in a future release, to match POSIX, so
3043you should not depend on this order.
3044
3045 Here is an example of implementing a factorial function using
3046`m4wrap':
3047
3048 define(`f', `ifelse(`$1', `0', `Answer: 0!=1
3049 ', eval(`$1>1'), `0', `Answer: $2$1=eval(`$2$1')
3050 ', `m4wrap(`f(decr(`$1'), `$2$1*')')')')
3051 =>
3052 f(`10')
3053 =>
3054 ^D
3055 =>Answer: 10*9*8*7*6*5*4*3*2*1=3628800
3056
3057 Invocations of `m4wrap' at the same recursion level are concatenated
3058and rescanned as usual:
3059
3060 define(`aa', `AA
3061 ')
3062 =>
3063 m4wrap(`a')m4wrap(`a')
3064 =>
3065 ^D
3066 =>AA
3067
3068however, the transition between recursion levels behaves like an end of
3069file condition between two input files.
3070
3071 m4wrap(`m4wrap(`)')len(abc')
3072 =>
3073 ^D
3074 error-->m4:stdin:1: ERROR: end of file in argument list
3075
3076
3077File: m4.info, Node: File Inclusion, Next: Diversions, Prev: Input Control, Up: Top
3078
30799 File inclusion
3080****************
3081
3082`m4' allows you to include named files at any point in the input.
3083
3084* Menu:
3085
3086* Include:: Including named files
3087* Search Path:: Searching for include files
3088
3089
3090File: m4.info, Node: Include, Next: Search Path, Up: File Inclusion
3091
30929.1 Including named files
3093=========================
3094
3095There are two builtin macros in `m4' for including files:
3096
3097 -- Builtin: include (FILE)
3098 -- Builtin: sinclude (FILE)
3099 Both macros cause the file named FILE to be read by `m4'. When
3100 the end of the file is reached, input is resumed from the previous
3101 input file.
3102
3103 The expansion of `include' and `sinclude' is therefore the
3104 contents of FILE.
3105
3106 If FILE does not exist (or cannot be read), the expansion is void,
3107 and `include' will fail with an error while `sinclude' is silent.
3108 The empty string counts as a file that does not exist.
3109
3110 The macros `include' and `sinclude' are recognized only with
3111 parameters.
3112
3113 include(`none')
3114 error-->m4:stdin:1: cannot open `none': No such file or directory
3115 =>
3116 include()
3117 error-->m4:stdin:2: cannot open `': No such file or directory
3118 =>
3119 sinclude(`none')
3120 =>
3121 sinclude()
3122 =>
3123
3124 The rest of this section assumes that `m4' is invoked with the `-I'
3125option (*note Invoking m4: Preprocessor features.) pointing to the
3126`m4-1.4.8/examples' directory shipped as part of the GNU `m4' package.
3127The file `m4-1.4.8/examples/incl.m4' in the distribution contains the
3128lines:
3129
3130 Include file start
3131 foo
3132 Include file end
3133
3134 Normally file inclusion is used to insert the contents of a file
3135into the input stream. The contents of the file will be read by `m4'
3136and macro calls in the file will be expanded:
3137
3138 define(`foo', `FOO')
3139 =>
3140 include(`incl.m4')
3141 =>Include file start
3142 =>FOO
3143 =>Include file end
3144 =>
3145
3146 The fact that `include' and `sinclude' expand to the contents of the
3147file can be used to define macros that operate on entire files. Here
3148is an example, which defines `bar' to expand to the contents of
3149`incl.m4':
3150
3151 define(`bar', include(`incl.m4'))
3152 =>
3153 This is `bar': >>bar<<
3154 =>This is bar: >>Include file start
3155 =>foo
3156 =>Include file end
3157 =><<
3158
3159 This use of `include' is not trivial, though, as files can contain
3160quotes, commas, and parentheses, which can interfere with the way the
3161`m4' parser works. GNU `m4' seamlessly concatenates the file contents
3162with the next character, even if the included file ended in the middle
3163of a comment, string, or macro call. These conditions are only treated
3164as end of file errors if specified as input files on the command line.
3165
3166 In GNU `m4', an alternative method of reading files is using
3167`undivert' (*note Undivert::) on a named file.
3168
3169
3170File: m4.info, Node: Search Path, Prev: Include, Up: File Inclusion
3171
31729.2 Searching for include files
3173===============================
3174
3175GNU `m4' allows included files to be found in other directories than
3176the current working directory.
3177
3178 If the `--prepend-include' or `-B' command-line option was provided
3179(*note Invoking m4: Preprocessor features.), those directories are
3180searched first, in reverse order that those options were listed on the
3181command line. Then `m4' looks in the current working directory. Next
3182comes the directories specified with the `--include' or `-I' option, in
3183the order found on the command line. Finally, if the `M4PATH'
3184environment variable is set, it is expected to contain a
3185colon-separated list of directories, which will be searched in order.
3186
3187 If the automatic search for include-files causes trouble, the `p'
3188debug flag (*note Debug Levels::) can help isolate the problem.
3189
3190
3191File: m4.info, Node: Diversions, Next: Text handling, Prev: File Inclusion, Up: Top
3192
319310 Diverting and undiverting output
3194***********************************
3195
3196Diversions are a way of temporarily saving output. The output of `m4'
3197can at any time be diverted to a temporary file, and be reinserted into
3198the output stream, "undiverted", again at a later time.
3199
3200 Numbered diversions are counted from 0 upwards, diversion number 0
3201being the normal output stream. The number of simultaneous diversions
3202is limited mainly by the memory used to describe them, because GNU `m4'
3203tries to keep diversions in memory. However, there is a limit to the
3204overall memory usable by all diversions taken altogether (512K,
3205currently). When this maximum is about to be exceeded, a temporary
3206file is opened to receive the contents of the biggest diversion still
3207in memory, freeing this memory for other diversions. When creating the
3208temporary file, `m4' honors the value of the environment variable
3209`TMPDIR', and falls back to `/tmp'. So, it is theoretically possible
3210that the number and aggregate size of diversions is limited only by
3211available disk space.
3212
3213 Diversions make it possible to generate output in a different order
3214than the input was read. It is possible to implement topological
3215sorting dependencies. For example, GNU Autoconf makes use of
3216diversions under the hood to ensure that the expansion of a prerequisite
3217macro appears in the output prior to the expansion of a dependent macro,
3218regardless of which order the two macros were invoked in the user's
3219input file.
3220
3221* Menu:
3222
3223* Divert:: Diverting output
3224* Undivert:: Undiverting output
3225* Divnum:: Diversion numbers
3226* Cleardivert:: Discarding diverted text
3227
3228
3229File: m4.info, Node: Divert, Next: Undivert, Up: Diversions
3230
323110.1 Diverting output
3232=====================
3233
3234Output is diverted using `divert':
3235
3236 -- Builtin: divert ([NUMBER = `0'])
3237 The current diversion is changed to NUMBER. If NUMBER is left out
3238 or empty, it is assumed to be zero. If NUMBER cannot be parsed,
3239 the diversion is unchanged.
3240
3241 The expansion of `divert' is void.
3242
3243 When all the `m4' input will have been processed, all existing
3244diversions are automatically undiverted, in numerical order.
3245
3246 divert(`1')
3247 This text is diverted.
3248 divert
3249 =>
3250 This text is not diverted.
3251 =>This text is not diverted.
3252 ^D
3253 =>
3254 =>This text is diverted.
3255
3256 Several calls of `divert' with the same argument do not overwrite
3257the previous diverted text, but append to it. Diversions are printed
3258after any wrapped text is expanded.
3259
3260 define(`text', `TEXT')
3261 =>
3262 divert(`1')`diverted text.'
3263 divert
3264 =>
3265 m4wrap(`Wrapped text preceeds ')
3266 =>
3267 ^D
3268 =>Wrapped TEXT preceeds diverted text.
3269
3270 If output is diverted to a negative diversion, it is simply
3271discarded. This can be used to suppress unwanted output. A common
3272example of unwanted output is the trailing newlines after macro
3273definitions. Here is a common programming idiom in `m4' for avoiding
3274them.
3275
3276 divert(`-1')
3277 define(`foo', `Macro `foo'.')
3278 define(`bar', `Macro `bar'.')
3279 divert
3280 =>
3281
3282 Traditional implementations only supported ten diversions. But as a
3283GNU extension, diversion numbers can be as large as positive integers
3284will allow, rather than treating a multi-digit diversion number as a
3285request to discard text.
3286
3287 divert(eval(`1<<28'))world
3288 divert(`2')hello
3289 ^D
3290 =>hello
3291 =>world
3292
3293 Note that `divert' is an English word, but also an active macro
3294without arguments. When processing plain text, the word might appear in
3295normal text and be unintentionally swallowed as a macro invocation. One
3296way to avoid this is to use the `-P' option to rename all builtins
3297(*note Invoking m4: Operation modes.). Another is to write a wrapper
3298that requires a parameter to be recognized.
3299
3300 We decided to divert the stream for irrigation.
3301 =>We decided to the stream for irrigation.
3302 define(`divert', `ifelse(`$#', `0', ``$0'', `builtin(`$0', $@)')')
3303 =>
3304 divert(`-1')
3305 Ignored text.
3306 divert(`0')
3307 =>
3308 We decided to divert the stream for irrigation.
3309 =>We decided to divert the stream for irrigation.
3310
3311
3312File: m4.info, Node: Undivert, Next: Divnum, Prev: Divert, Up: Diversions
3313
331410.2 Undiverting output
3315=======================
3316
3317Diverted text can be undiverted explicitly using the builtin `undivert':
3318
3319 -- Builtin: undivert ([DIVERSIONS...])
3320 Undiverts the numeric DIVERSIONS given by the arguments, in the
3321 order given. If no arguments are supplied, all diversions are
3322 undiverted, in numerical order.
3323
3324 As a GNU extension, DIVERSIONS may contain non-numeric strings,
3325 which are treated as the names of files to copy into the output
3326 without expansion. A warning is issued if a file could not be
3327 opened.
3328
3329 The expansion of `undivert' is void.
3330
3331 divert(`1')
3332 This text is diverted.
3333 divert
3334 =>
3335 This text is not diverted.
3336 =>This text is not diverted.
3337 undivert(`1')
3338 =>
3339 =>This text is diverted.
3340 =>
3341
3342 Notice the last two blank lines. One of them comes from the newline
3343following `undivert', the other from the newline that followed the
3344`divert'! A diversion often starts with a blank line like this.
3345
3346 When diverted text is undiverted, it is _not_ reread by `m4', but
3347rather copied directly to the current output, and it is therefore not
3348an error to undivert into a diversion. Undiverting the empty string is
3349the same as specifying diversion 0; in either case nothing happens
3350since the output has already been flushed.
3351
3352 divert(`1')diverted text
3353 divert
3354 =>
3355 undivert()
3356 =>
3357 undivert(`0')
3358 =>
3359 undivert
3360 =>diverted text
3361 =>
3362
3363 When a diversion has been undiverted, the diverted text is discarded,
3364and it is not possible to bring back diverted text more than once.
3365
3366 divert(`1')
3367 This text is diverted first.
3368 divert(`0')undivert(`1')dnl
3369 =>
3370 =>This text is diverted first.
3371 undivert(`1')
3372 =>
3373 divert(`1')
3374 This text is also diverted but not appended.
3375 divert(`0')undivert(`1')dnl
3376 =>
3377 =>This text is also diverted but not appended.
3378
3379 Attempts to undivert the current diversion are silently ignored.
3380Thus, when the current diversion is not 0, the current diversion does
3381not get rearranged among the other diversions.
3382
3383 divert(`1')one
3384 divert(`2')two
3385 divert(`3')three
3386 divert(`2')undivert`'dnl
3387 divert`'undivert`'dnl
3388 =>two
3389 =>one
3390 =>three
3391
3392 GNU `m4' allows named files to be undiverted. Given a non-numeric
3393argument, the contents of the file named will be copied, uninterpreted,
3394to the current output. This complements the builtin `include' (*note
3395Include::). To illustrate the difference, the file
3396`m4-1.4.8/examples/foo' contains the word `bar':
3397
3398 define(`bar', `BAR')
3399 =>
3400 undivert(`foo')
3401 =>bar
3402 =>
3403 include(`foo')
3404 =>BAR
3405 =>
3406
3407 If the file is not found (or cannot be read), an error message is
3408issued, and the expansion is void.
3409
3410
3411File: m4.info, Node: Divnum, Next: Cleardivert, Prev: Undivert, Up: Diversions
3412
341310.3 Diversion numbers
3414======================
3415
3416The current diversion is tracked by the builtin `divnum':
3417
3418 -- Builtin: divnum
3419 Expands to the number of the current diversion.
3420
3421 Initial divnum
3422 =>Initial 0
3423 divert(`1')
3424 Diversion one: divnum
3425 divert(`2')
3426 Diversion two: divnum
3427 ^D
3428 =>
3429 =>Diversion one: 1
3430 =>
3431 =>Diversion two: 2
3432
3433
3434File: m4.info, Node: Cleardivert, Prev: Divnum, Up: Diversions
3435
343610.4 Discarding diverted text
3437=============================
3438
3439Often it is not known, when output is diverted, whether the diverted
3440text is actually needed. Since all non-empty diversion are brought back
3441on the main output stream when the end of input is seen, a method of
3442discarding a diversion is needed. If all diversions should be
3443discarded, the easiest is to end the input to `m4' with `divert(`-1')'
3444followed by an explicit `undivert':
3445
3446 divert(`1')
3447 Diversion one: divnum
3448 divert(`2')
3449 Diversion two: divnum
3450 divert(`-1')
3451 undivert
3452 ^D
3453
3454No output is produced at all.
3455
3456 Clearing selected diversions can be done with the following macro:
3457
3458 -- Composite: cleardivert ([DIVERSIONS...])
3459 Discard the contents of each of the listed numeric DIVERSIONS.
3460
3461 define(`cleardivert',
3462 `pushdef(`_n', divnum)divert(`-1')undivert($@)divert(_n)popdef(`_n')')
3463 =>
3464
3465 It is called just like `undivert', but the effect is to clear the
3466diversions, given by the arguments. (This macro has a nasty bug! You
3467should try to see if you can find it and correct it; or *note Answers:
3468Improved cleardivert.).
3469
3470
3471File: m4.info, Node: Text handling, Next: Arithmetic, Prev: Diversions, Up: Top
3472
347311 Macros for text handling
3474***************************
3475
3476There are a number of builtins in `m4' for manipulating text in various
3477ways, extracting substrings, searching, substituting, and so on.
3478
3479* Menu:
3480
3481* Len:: Calculating length of strings
3482* Index macro:: Searching for substrings
3483* Regexp:: Searching for regular expressions
3484* Substr:: Extracting substrings
3485* Translit:: Translating characters
3486* Patsubst:: Substituting text by regular expression
3487* Format:: Formatting strings (printf-like)
3488
3489
3490File: m4.info, Node: Len, Next: Index macro, Up: Text handling
3491
349211.1 Calculating length of strings
3493==================================
3494
3495The length of a string can be calculated by `len':
3496
3497 -- Builtin: len (STRING)
3498 Expands to the length of STRING, as a decimal number.
3499
3500 The macro `len' is recognized only with parameters.
3501
3502 len()
3503 =>0
3504 len(`abcdef')
3505 =>6
3506
3507
3508File: m4.info, Node: Index macro, Next: Regexp, Prev: Len, Up: Text handling
3509
351011.2 Searching for substrings
3511=============================
3512
3513Searching for substrings is done with `index':
3514
3515 -- Builtin: index (STRING, SUBSTRING)
3516 Expands to the index of the first occurrence of SUBSTRING in
3517 STRING. The first character in STRING has index 0. If SUBSTRING
3518 does not occur in STRING, `index' expands to `-1'.
3519
3520 The macro `index' is recognized only with parameters.
3521
3522 index(`gnus, gnats, and armadillos', `nat')
3523 =>7
3524 index(`gnus, gnats, and armadillos', `dag')
3525 =>-1
3526
3527 Omitting SUBSTRING evokes a warning, but still produces output.
3528
3529 index(`abc')
3530 error-->m4:stdin:1: Warning: too few arguments to builtin `index'
3531 =>0
3532
3533
3534File: m4.info, Node: Regexp, Next: Substr, Prev: Index macro, Up: Text handling
3535
353611.3 Searching for regular expressions
3537======================================
3538
3539Searching for regular expressions is done with the builtin `regexp':
3540
3541 -- Builtin: regexp (STRING, REGEXP, [REPLACEMENT])
3542 Searches for REGEXP in STRING. The syntax for regular expressions
3543 is the same as in GNU Emacs. *Note Syntax of Regular Expressions:
3544 (emacs)Regexps.
3545
3546 If REPLACEMENT is omitted, `regexp' expands to the index of the
3547 first match of REGEXP in STRING. If REGEXP does not match
3548 anywhere in STRING, it expands to -1.
3549
3550 If REPLACEMENT is supplied, and there was a match, `regexp'
3551 changes the expansion to this argument, with `\N' substituted by
3552 the text matched by the Nth parenthesized sub-expression of
3553 REGEXP, up to nine sub-expressions. The escape `\&' is replaced
3554 by the text of the entire regular expression matched. For all
3555 other characters, `\' treats the next character literally. A
3556 warning is issued if there were fewer sub-expressions than the
3557 `\N' requested, or if there is a trailing `\'. If there was no
3558 match, `regexp' expands to the empty string.
3559
3560 The macro `regexp' is recognized only with parameters.
3561
3562 regexp(`GNUs not Unix', `\<[a-z]\w+')
3563 =>5
3564 regexp(`GNUs not Unix', `\<Q\w*')
3565 =>-1
3566 regexp(`GNUs not Unix', `\w\(\w+\)$', `*** \& *** \1 ***')
3567 =>*** Unix *** nix ***
3568 regexp(`GNUs not Unix', `\<Q\w*', `*** \& *** \1 ***')
3569 =>
3570
3571 Here are some more examples on the handling of backslash:
3572
3573 regexp(`abc', `\(b\)', `\\\10\a')
3574 =>\b0a
3575 regexp(`abc', `b', `\1\')
3576 error-->m4:stdin:2: Warning: sub-expression 1 not present
3577 error-->m4:stdin:2: Warning: trailing \ ignored in replacement
3578 =>
3579 regexp(`abc', `\(\(d\)?\)\(c\)', `\1\2\3\4\5\6')
3580 error-->m4:stdin:3: Warning: sub-expression 4 not present
3581 error-->m4:stdin:3: Warning: sub-expression 5 not present
3582 error-->m4:stdin:3: Warning: sub-expression 6 not present
3583 =>c
3584
3585 Omitting REGEXP evokes a warning, but still produces output.
3586
3587 regexp(`abc')
3588 error-->m4:stdin:1: Warning: too few arguments to builtin `regexp'
3589 =>0
3590
3591
3592File: m4.info, Node: Substr, Next: Translit, Prev: Regexp, Up: Text handling
3593
359411.4 Extracting substrings
3595==========================
3596
3597Substrings are extracted with `substr':
3598
3599 -- Builtin: substr (STRING, FROM, [LENGTH])
3600 Expands to the substring of STRING, which starts at index FROM,
3601 and extends for LENGTH characters, or to the end of STRING, if
3602 LENGTH is omitted. The starting index of a string is always 0.
3603 The expansion is empty if there is an error parsing FROM or
3604 LENGTH, if FROM is beyond the end of STRING, or if LENGTH is
3605 negative.
3606
3607 The macro `substr' is recognized only with parameters.
3608
3609 substr(`gnus, gnats, and armadillos', `6')
3610 =>gnats, and armadillos
3611 substr(`gnus, gnats, and armadillos', `6', `5')
3612 =>gnats
3613
3614 Omitting FROM evokes a warning, but still produces output.
3615
3616 substr(`abc')
3617 error-->m4:stdin:1: Warning: too few arguments to builtin `substr'
3618 =>abc
3619 substr(`abc',)
3620 error-->m4:stdin:2: empty string treated as 0 in builtin `substr'
3621 =>abc
3622
3623
3624File: m4.info, Node: Translit, Next: Patsubst, Prev: Substr, Up: Text handling
3625
362611.5 Translating characters
3627===========================
3628
3629Character translation is done with `translit':
3630
3631 -- Builtin: translit (STRING, CHARS, [REPLACEMENT])
3632 Expands to STRING, with each character that occurs in CHARS
3633 translated into the character from REPLACEMENT with the same index.
3634
3635 If REPLACEMENT is shorter than CHARS, the excess characters of
3636 CHARS are deleted from the expansion; if CHARS is shorter, the
3637 excess characters in REPLACEMENT are silently ignored. If
3638 REPLACEMENT is omitted, all characters in STRING that are present
3639 in CHARS are deleted from the expansion. If a character appears
3640 more than once in CHARS, only the first instance is used in making
3641 the translation. Only a single translation pass is made, even if
3642 characters in REPLACEMENT also appear in CHARS.
3643
3644 As a GNU extension, both CHARS and REPLACEMENT can contain
3645 character-ranges, e.g., `a-z' (meaning all lowercase letters) or
3646 `0-9' (meaning all digits). To include a dash `-' in CHARS or
3647 REPLACEMENT, place it first or last in the entire string, or as
3648 the last character of a range. Back-to-back ranges can share a
3649 common endpoint. It is not an error for the last character in the
3650 range to be `larger' than the first. In that case, the range runs
3651 backwards, i.e., `9-0' means the string `9876543210'. The
3652 expansion of a range is dependent on the underlying encoding of
3653 characters, so using ranges is not always portable between
3654 machines.
3655
3656 The macro `translit' is recognized only with parameters.
3657
3658 translit(`GNUs not Unix', `A-Z')
3659 =>s not nix
3660 translit(`GNUs not Unix', `a-z', `A-Z')
3661 =>GNUS NOT UNIX
3662 translit(`GNUs not Unix', `A-Z', `z-a')
3663 =>tmfs not fnix
3664 translit(`+,-12345', `+--1-5', `<;>a-c-a')
3665 =><;>abcba
3666 translit(`abcdef', `aabdef', `bcged')
3667 =>bgced
3668
3669 In the ASCII encoding, the first example deletes all uppercase
3670letters, the second converts lowercase to uppercase, and the third
3671`mirrors' all uppercase letters, while converting them to lowercase.
3672The two first cases are by far the most common, even though they are not
3673portable to EBCDIC or other encodings. The fourth example shows a
3674range ending in `-', as well as back-to-back ranges. The final example
3675shows that `a' is mapped to `b', not `c'; the resulting `b' is not
3676further remapped to `g'; the `d' and `e' are swapped, and the `f' is
3677discarded.
3678
3679 Omitting CHARS evokes a warning, but still produces output.
3680
3681 translit(`abc')
3682 error-->m4:stdin:1: Warning: too few arguments to builtin `translit'
3683 =>abc
3684
3685
3686File: m4.info, Node: Patsubst, Next: Format, Prev: Translit, Up: Text handling
3687
368811.6 Substituting text by regular expression
3689============================================
3690
3691Global substitution in a string is done by `patsubst':
3692
3693 -- Builtin: patsubst (STRING, REGEXP, [REPLACEMENT])
3694 Searches STRING for matches of REGEXP, and substitutes REPLACEMENT
3695 for each match. The syntax for regular expressions is the same as
3696 in GNU Emacs (*note Regexp::).
3697
3698 The parts of STRING that are not covered by any match of REGEXP
3699 are copied to the expansion. Whenever a match is found, the
3700 search proceeds from the end of the match, so a character from
3701 STRING will never be substituted twice. If REGEXP matches a
3702 string of zero length, the start position for the search is
3703 incremented, to avoid infinite loops.
3704
3705 When a replacement is to be made, REPLACEMENT is inserted into the
3706 expansion, with `\N' substituted by the text matched by the Nth
3707 parenthesized sub-expression of PATSUBST, for up to nine
3708 sub-expressions. The escape `\&' is replaced by the text of the
3709 entire regular expression matched. For all other characters, `\'
3710 treats the next character literally. A warning is issued if there
3711 were fewer sub-expressions than the `\N' requested, or if there is
3712 a trailing `\'.
3713
3714 The REPLACEMENT argument can be omitted, in which case the text
3715 matched by REGEXP is deleted.
3716
3717 The macro `patsubst' is recognized only with parameters.
3718
3719 patsubst(`GNUs not Unix', `^', `OBS: ')
3720 =>OBS: GNUs not Unix
3721 patsubst(`GNUs not Unix', `\<', `OBS: ')
3722 =>OBS: GNUs OBS: not OBS: Unix
3723 patsubst(`GNUs not Unix', `\w*', `(\&)')
3724 =>(GNUs)() (not)() (Unix)()
3725 patsubst(`GNUs not Unix', `\w+', `(\&)')
3726 =>(GNUs) (not) (Unix)
3727 patsubst(`GNUs not Unix', `[A-Z][a-z]+')
3728 =>GN not
3729 patsubst(`GNUs not Unix', `not', `NOT\')
3730 error-->m4:stdin:6: Warning: trailing \ ignored in replacement
3731 =>GNUs NOT Unix
3732
3733 Here is a slightly more realistic example, which capitalizes
3734individual word or whole sentences, by substituting calls of the macros
3735`upcase' and `downcase' into the strings.
3736
3737 -- Composite: upcase (TEXT)
3738 -- Composite: downcase (TEXT)
3739 -- Composite: capitalize (TEXT)
3740 Expand to TEXT, but with capitalization changed: `upcase' changes
3741 all letters to upper case, `downcase' changes all letters to lower
3742 case, and `capitalize' changes the first character of each word to
3743 upper case and the remaining characters to lower case.
3744
3745 define(`upcase', `translit(`$*', `a-z', `A-Z')')dnl
3746 define(`downcase', `translit(`$*', `A-Z', `a-z')')dnl
3747 define(`capitalize1',
3748 `regexp(`$1', `^\(\w\)\(\w*\)',
3749 `upcase(`\1')`'downcase(`\2')')')dnl
3750 define(`capitalize',
3751 `patsubst(`$1', `\w+', `capitalize1(`\&')')')dnl
3752 capitalize(`GNUs not Unix')
3753 =>Gnus Not Unix
3754
3755 While `regexp' replaces the whole input with the replacement as soon
3756as there is a match, `patsubst' replaces each _occurrence_ of a match
3757and preserves non-matching pieces:
3758
3759 define(`patreg',
3760 `patsubst($@)
3761 regexp($@)')dnl
3762 patreg(`bar foo baz Foo', `foo\|Foo', `FOO')
3763 =>bar FOO baz FOO
3764 =>FOO
3765 patreg(`aba abb 121', `\(.\)\(.\)\1', `\2\1\2')
3766 =>bab abb 212
3767 =>bab
3768
3769 Omitting REGEXP evokes a warning, but still produces output.
3770
3771 patsubst(`abc')
3772 error-->m4:stdin:1: Warning: too few arguments to builtin `patsubst'
3773 =>abc
3774
3775
3776File: m4.info, Node: Format, Prev: Patsubst, Up: Text handling
3777
377811.7 Formatted output
3779=====================
3780
3781Formatted output can be made with `format':
3782
3783 -- Builtin: format (FORMAT-STRING, ...)
3784 Works much like the C function `printf'. The first argument
3785 FORMAT-STRING can contain `%' specifications which are satisfied
3786 by additional arguments, and the expansion of `format' is the
3787 formatted string.
3788
3789 The macro `format' is recognized only with parameters.
3790
3791 Its use is best described by a few examples:
3792
3793 define(`foo', `The brown fox jumped over the lazy dog')
3794 =>
3795 format(`The string "%s" uses %d characters', foo, len(foo))
3796 =>The string "The brown fox jumped over the lazy dog" uses 38 characters
3797 format(`%.0f', `56789.9876')
3798 =>56790
3799 len(format(`%-*X', `300', `1'))
3800 =>300
3801
3802 Using the `forloop' macro defined earlier (*note Forloop::), this
3803example shows how `format' can be used to produce tabular output.
3804
3805 include(`forloop.m4')
3806 =>
3807 forloop(`i', `1', `10', `format(`%6d squared is %10d
3808 ', i, eval(i**2))')
3809 => 1 squared is 1
3810 => 2 squared is 4
3811 => 3 squared is 9
3812 => 4 squared is 16
3813 => 5 squared is 25
3814 => 6 squared is 36
3815 => 7 squared is 49
3816 => 8 squared is 64
3817 => 9 squared is 81
3818 => 10 squared is 100
3819 =>
3820
3821 The builtin `format' is modeled after the ANSI C `printf' function,
3822and supports these `%' specifiers: `c', `s', `d', `o', `x', `X', `u',
3823`e', `E', `f', `F', `g', `G', and `%'; it supports field widths and
3824precisions, and the modifiers `+', `-', ` ', `0', `#', `h' and `l'.
3825For more details on the functioning of `printf', see the C Library
3826Manual.
3827
3828 For now, unrecognized specifiers are silently ignored, but it is
3829anticipated that a future release of GNU `m4' will support more
3830specifiers, and give warnings when problems are encountered. Likewise,
3831escape sequences are not yet recognized.
3832
3833
3834File: m4.info, Node: Arithmetic, Next: Shell commands, Prev: Text handling, Up: Top
3835
383612 Macros for doing arithmetic
3837******************************
3838
3839Integer arithmetic is included in `m4', with a C-like syntax. As
3840convenient shorthands, there are builtins for simple increment and
3841decrement operations.
3842
3843* Menu:
3844
3845* Incr:: Decrement and increment operators
3846* Eval:: Evaluating integer expressions
3847
3848
3849File: m4.info, Node: Incr, Next: Eval, Up: Arithmetic
3850
385112.1 Decrement and increment operators
3852======================================
3853
3854Increment and decrement of integers are supported using the builtins
3855`incr' and `decr':
3856
3857 -- Builtin: incr (NUMBER)
3858 -- Builtin: decr (NUMBER)
3859 Expand to the numerical value of NUMBER, incremented or
3860 decremented, respectively, by one. Except for the empty string,
3861 the expansion is empty if NUMBER could not be parsed.
3862
3863 The macros `incr' and `decr' are recognized only with parameters.
3864
3865 incr(`4')
3866 =>5
3867 decr(`7')
3868 =>6
3869 incr()
3870 error-->m4:stdin:3: empty string treated as 0 in builtin `incr'
3871 =>1
3872 decr()
3873 error-->m4:stdin:4: empty string treated as 0 in builtin `decr'
3874 =>-1
3875
3876
3877File: m4.info, Node: Eval, Prev: Incr, Up: Arithmetic
3878
387912.2 Evaluating integer expressions
3880===================================
3881
3882Integer expressions are evaluated with `eval':
3883
3884 -- Builtin: eval (EXPRESSION, [RADIX = `10'], [WIDTH])
3885 Expands to the value of EXPRESSION. The expansion is empty if an
3886 error is encountered while parsing the arguments. If specified,
3887 RADIX and WIDTH control the format of the output.
3888
3889 The macro `eval' is recognized only with parameters.
3890
3891 Expressions can contain the following operators, listed in order of
3892decreasing precedence.
3893
3894`+ -'
3895 Unary plus and minus
3896
3897`**'
3898 Exponentiation
3899
3900`* / %'
3901 Multiplication, division and modulo
3902
3903`+ -'
3904 Addition and subtraction
3905
3906`<< >>'
3907 Shift left or right
3908
3909`== != > >= < <='
3910 Relational operators
3911
3912`!'
3913 Logical negation
3914
3915`~'
3916 Bitwise negation
3917
3918`&'
3919 Bitwise and
3920
3921`^'
3922 Bitwise exclusive-or
3923
3924`|'
3925 Bitwise or
3926
3927`&&'
3928 Logical and
3929
3930`||'
3931 Logical or
3932
3933 All operators, except exponentiation, are left associative.
3934
3935 Note that some older `m4' implementations use `^' as an alternate
3936operator for exponentiation, although POSIX requires the C behavior of
3937bitwise exclusive-or. On the other hand, the precedence of `~' and `!'
3938are different in GNU `m4' than they are in C, matching the precedence
3939in traditional `m4' implementations. This behavior is likely to change
3940in a future version to match POSIX, so use parentheses to force the
3941desired precedence.
3942
3943 Within EXPRESSION, (but not RADIX or WIDTH), numbers without a
3944special prefix are decimal. A simple `0' prefix introduces an octal
3945number. `0x' introduces a hexadecimal number. `0b' introduces a
3946binary number. `0r' introduces a number expressed in any radix between
39471 and 36: the prefix should be immediately followed by the decimal
3948expression of the radix, a colon, then the digits making the number.
3949For radix 1, leading zeros are ignored and all remaining digits must be
3950`1'; for all other radices, the digits are `0', `1', `2', .... Beyond
3951`9', the digits are `a', `b' ... up to `z'. Lower and upper case
3952letters can be used interchangeably in numbers prefixes and as number
3953digits.
3954
3955 Parentheses may be used to group subexpressions whenever needed.
3956For the relational operators, a true relation returns `1', and a false
3957relation return `0'.
3958
3959 Here are a few examples of use of `eval'.
3960
3961 eval(`-3 * 5')
3962 =>-15
3963 eval(index(`Hello world', `llo') >= 0)
3964 =>1
3965 eval(`0r1:0111 + 0b100 + 0r3:12')
3966 =>12
3967 define(`square', `eval(`('$1`)**2')')
3968 =>
3969 square(`9')
3970 =>81
3971 square(square(`5')`+1')
3972 =>676
3973 define(`foo', `666')
3974 =>
3975 eval(`foo/6')
3976 error-->m4:stdin:8: bad expression in eval: foo/6
3977 =>
3978 eval(foo/6)
3979 =>111
3980
3981 As the last two lines show, `eval' does not handle macro names, even
3982if they expand to a valid expression (or part of a valid expression).
3983Therefore all macros must be expanded before they are passed to `eval'.
3984
3985 All evaluation is done with 32-bit signed integers, assuming
39862's-complement with wrap-around. The shift operators are defined in GNU
3987`m4' by doing an implicit bit-wise and of the right-hand operand with
39880x1f, and sign-extension with right shift.
3989
3990 eval(0x80000000 / -1)
3991 =>-2147483648
3992 eval(0x80000000 % -1)
3993 =>0
3994 eval(0x7fffffff)
3995 =>2147483647
3996 incr(eval(0x7fffffff))
3997 =>-2147483648
3998 eval(-4 >> 33)
3999 =>-2
4000
4001 If RADIX is specified, it specifies the radix to be used in the
4002expansion. The default radix is 10; this is also the case if RADIX is
4003the empty string. It is an error if the radix is outside the range of
40041 through 36, inclusive. The result of `eval' is always taken to be
4005signed. No radix prefix is output, and for radices greater than 10,
4006the digits are lower case. The WIDTH argument specifies the minimum
4007output width, excluding any negative sign. The result is zero-padded
4008to extend the expansion to the requested width. It is an error if the
4009width is negative. On error, the expansion of `eval' is empty.
4010
4011 eval(`666', `10')
4012 =>666
4013 eval(`666', `11')
4014 =>556
4015 eval(`666', `6')
4016 =>3030
4017 eval(`666', `6', `10')
4018 =>0000003030
4019 eval(`-666', `6', `10')
4020 =>-0000003030
4021 eval(`10', `', `0')
4022 =>10
4023 `0r1:'eval(`10', `1', `11')
4024 =>0r1:01111111111
4025 eval(`10', `16')
4026 =>a
4027
4028
4029File: m4.info, Node: Shell commands, Next: Miscellaneous, Prev: Arithmetic, Up: Top
4030
403113 Macros for running shell commands
4032************************************
4033
4034There are a few builtin macros in `m4' that allow you to run shell
4035commands from within `m4'.
4036
4037 Note that the definition of a valid shell command is system
4038dependent. On UNIX systems, this is the typical `/bin/sh'. But on
4039other systems, such as native Windows, the shell has a different syntax
4040of commands that it understands. Some examples in this chapter assume
4041`/bin/sh', and also demonstrate how to quit early with a known exit
4042value if this is not the case.
4043
4044* Menu:
4045
4046* Platform macros:: Determining the platform
4047* Syscmd:: Executing simple commands
4048* Esyscmd:: Reading the output of commands
4049* Sysval:: Exit status
4050* Mkstemp:: Making temporary files
4051
4052
4053File: m4.info, Node: Platform macros, Next: Syscmd, Up: Shell commands
4054
405513.1 Determining the platform
4056=============================
4057
4058Sometimes it is desirable for an input file to know which platform `m4'
4059is running on. GNU `m4' provides several macros that are predefined to
4060expand to the empty string; checking for their existence will confirm
4061platform details.
4062
4063 -- Optional builtin: __gnu__
4064 -- Optional builtin: __os2__
4065 -- Optional builtin: os2
4066 -- Optional builtin: __unix__
4067 -- Optional builtin: unix
4068 -- Optional builtin: __windows__
4069 -- Optional builtin: windows
4070 Each of these macros is conditionally defined as needed to
4071 describe the environment of `m4'. If defined, each macro expands
4072 to the empty string.
4073
4074 When GNU extensions are in effect (that is, when you did not use the
4075`-G' option, *note Invoking m4: Limits control.), GNU `m4' will define
4076the macro `__gnu__' to expand to the empty string.
4077
4078 __gnu__
4079 =>
4080 ifdef(`__gnu__', `Extensions are active')
4081 =>Extensions are active
4082
4083 On UNIX systems, GNU `m4' will define `__unix__' by default, or
4084`unix' when the `-G' option is specified.
4085
4086 On native Windows systems, GNU `m4' will define `__windows__' by
4087default, or `windows' when the `-G' option is specified.
4088
4089 On OS/2 systems, GNU `m4' will define `__os2__' by default, or `os2'
4090when the `-G' option is specified.
4091
4092 If GNU `m4' does not provide a platform macro for your system,
4093please report that as a bug.
4094
4095 define(`provided', `0')
4096 =>
4097 ifdef(`__unix__', `define(`provided', incr(provided))')
4098 =>
4099 ifdef(`__windows__', `define(`provided', incr(provided))')
4100 =>
4101 ifdef(`__os2__', `define(`provided', incr(provided))')
4102 =>
4103 provided
4104 =>1
4105
4106
4107File: m4.info, Node: Syscmd, Next: Esyscmd, Prev: Platform macros, Up: Shell commands
4108
410913.2 Executing simple commands
4110==============================
4111
4112Any shell command can be executed, using `syscmd':
4113
4114 -- Builtin: syscmd (SHELL-COMMAND)
4115 Executes SHELL-COMMAND as a shell command.
4116
4117 The expansion of `syscmd' is void, _not_ the output from
4118 SHELL-COMMAND! Output or error messages from SHELL-COMMAND are
4119 not read by `m4'. *Note Esyscmd::, if you need to process the
4120 command output.
4121
4122 Prior to executing the command, `m4' flushes its buffers. The
4123 default standard input, output and error of SHELL-COMMAND are the
4124 same as those of `m4'.
4125
4126 The macro `syscmd' is recognized only with parameters.
4127
4128 define(`foo', `FOO')
4129 =>
4130 syscmd(`echo foo')
4131 =>foo
4132 =>
4133
4134 Note how the expansion of `syscmd' keeps the trailing newline of the
4135command, as well as using the newline that appeared after the macro.
4136
4137 As an example of SHELL-COMMAND using the same standard input as
4138`m4', the command line `echo "m4wrap(\`syscmd(\`cat')')" | m4' will
4139tell `m4' to read all of its input before executing the wrapped text,
4140then hand a valid (albeit emptied) pipe as standard input for the `cat'
4141subcommand. Therefore, you should be careful when using standard input
4142(either by specifying no files, or by passing `-' as a file name on the
4143command line, *note Invoking m4: Command line files.), and also
4144invoking subcommands via `syscmd' or `esyscmd' that consume data from
4145standard input. When standard input is a seekable file, the subprocess
4146will pick up with the next character not yet processed by `m4'; when it
4147is a pipe or other non-seekable file, there is no guarantee how much
4148data will already be buffered by `m4' and thus unavailable to the child.
4149
4150
4151File: m4.info, Node: Esyscmd, Next: Sysval, Prev: Syscmd, Up: Shell commands
4152
415313.3 Reading the output of commands
4154===================================
4155
4156If you want `m4' to read the output of a shell command, use `esyscmd':
4157
4158 -- Builtin: esyscmd (SHELL-COMMAND)
4159 Expands to the standard output of the shell command SHELL-COMMAND.
4160
4161 Prior to executing the command, `m4' flushes its buffers. The
4162 default standard input and standard error of SHELL-COMMAND are the
4163 same as those of `m4'. The error output of SHELL-COMMAND is not a
4164 part of the expansion: it will appear along with the error output
4165 of `m4'.
4166
4167 The macro `esyscmd' is recognized only with parameters.
4168
4169 define(`foo', `FOO')
4170 =>
4171 esyscmd(`echo foo')
4172 =>FOO
4173 =>
4174
4175 Note how the expansion of `esyscmd' keeps the trailing newline of
4176the command, as well as using the newline that appeared after the macro.
4177
4178 Just as with `syscmd', care must be exercised when sharing standard
4179input between `m4' and the child process of `esyscmd'.
4180
4181
4182File: m4.info, Node: Sysval, Next: Mkstemp, Prev: Esyscmd, Up: Shell commands
4183
418413.4 Exit status
4185================
4186
4187To see whether a shell command succeeded, use `sysval':
4188
4189 -- Builtin: sysval
4190 Expands to the exit status of the last shell command run with
4191 `syscmd' or `esyscmd'. Expands to 0 if no command has been run
4192 yet.
4193
4194 syscmd(`false')
4195 =>
4196 ifelse(sysval, `0', `zero', `non-zero')
4197 =>non-zero
4198 syscmd(`exit 2')
4199 =>
4200 sysval
4201 =>2
4202 syscmd(`true')
4203 =>
4204 sysval
4205 =>0
4206 esyscmd(`false')
4207 =>
4208 ifelse(sysval, `0', `zero', `non-zero')
4209 =>non-zero
4210 esyscmd(`exit 2')
4211 =>
4212 sysval
4213 =>2
4214 esyscmd(`true')
4215 =>
4216 sysval
4217 =>0
4218
4219 `sysval' results in 127 if there was a problem executing the
4220command, for example, if the system-imposed argument length is exceeded,
4221or if there were not enough resources to fork. It is not possible to
4222distinguish between failed execution and successful execution that had
4223an exit status of 127.
4224
4225 On UNIX platforms, where it is possible to detect when command
4226execution is terminated by a signal, rather than a normal exit, the
4227result is the signal number shifted left by eight bits.
4228
4229 dnl This test assumes kill is a shell builtin, and that signals are
4230 dnl recognizable.
4231 ifdef(`__unix__', ,
4232 `errprint(` skipping: syscmd does not have unix semantics
4233 ')m4exit(`77')')dnl
4234 syscmd(`kill -13 $$')
4235 =>
4236 sysval
4237 =>3328
4238 esyscmd(`kill -9 $$')
4239 =>
4240 sysval
4241 =>2304
4242
4243
4244File: m4.info, Node: Mkstemp, Prev: Sysval, Up: Shell commands
4245
424613.5 Making temporary files
4247===========================
4248
4249Commands specified to `syscmd' or `esyscmd' might need a temporary
4250file, for output or for some other purpose. There is a builtin macro,
4251`mkstemp', for making a temporary file:
4252
4253 -- Builtin: mkstemp (TEMPLATE)
4254 -- Builtin: maketemp (TEMPLATE)
4255 Expands to a name of a new, empty file, made from the string
4256 TEMPLATE, which should end with the string `XXXXXX'. The six `X'
4257 characters are then replaced with random characters matching the
4258 regular expression `[a-zA-Z0-9._-]', in order to make the file
4259 name unique. If fewer than six `X' characters are found at the end
4260 of `template', the result will be longer than the template. The
4261 created file will have access permissions as if by `chmod =rw,go=',
4262 meaning that the current umask of the `m4' process is taken into
4263 account, and at most only the current user can read and write the
4264 file.
4265
4266 The traditional behavior, standardized by POSIX, is that
4267 `maketemp' merely replaces the trailing `X' with the process id,
4268 without creating a file, and without ensuring that the resulting
4269 string is a unique file name. In part, this means that using the
4270 same TEMPLATE twice in the same input file will result in the same
4271 expansion. This behavior is a security hole, as it is very easy
4272 for another process to guess the name that will be generated, and
4273 thus interfere with a subsequent use of `syscmd' trying to
4274 manipulate that file name. Hence, POSIX has recommended that all
4275 new implementations of `m4' provide the secure `mkstemp' builtin,
4276 and that users of `m4' check for its existence.
4277
4278 The macros `mkstemp' and `maketemp' are recognized only with
4279 parameters.
4280
4281 If you try this next example, you will most likely get different
4282output for the two file names, since the replacement characters are
4283randomly chosen:
4284
4285 maketemp(`/tmp/fooXXXXXX')
4286 =>/tmp/fooa07346
4287 ifdef(`mkstemp', `define(`maketemp', defn(`mkstemp'))',
4288 `define(`mkstemp', defn(`maketemp'))dnl
4289 errprint(`warning: potentially insecure maketemp implementation
4290 ')')
4291 =>
4292 mkstemp(`doc')
4293 =>docQv83Uw
4294
4295 Unless you use the `--traditional' command line option (or `-G',
4296*note Invoking m4: Limits control.), the GNU version of `maketemp' is
4297secure. This means that using the same template to multiple calls will
4298generate multiple files. However, we recommend that you use the new
4299`mkstemp' macro, introduced in GNU M4 1.4.8, which is secure even in
4300traditional mode.
4301
4302 syscmd(`echo foo??????')dnl
4303 =>foo??????
4304 define(`file1', maketemp(`fooXXXXXX'))dnl
4305 ifelse(esyscmd(`echo foo??????'), `foo??????', `no file', `created')
4306 =>created
4307 define(`file2', maketemp(`fooXX'))dnl
4308 define(`file3', mkstemp(`fooXXXXXX'))dnl
4309 ifelse(len(file1), len(file2), `same length', `different')
4310 =>same length
4311 ifelse(file1, file2, `same', `different file')
4312 =>different file
4313 ifelse(file2, file3, `same', `different file')
4314 =>different file
4315 ifelse(file1, file3, `same', `different file')
4316 =>different file
4317 syscmd(`rm 'file1 file2 file3)
4318 =>
4319 sysval
4320 =>0
4321
4322
4323File: m4.info, Node: Miscellaneous, Next: Frozen files, Prev: Shell commands, Up: Top
4324
432514 Miscellaneous builtin macros
4326*******************************
4327
4328This chapter describes various builtins, that do not really belong in
4329any of the previous chapters.
4330
4331* Menu:
4332
4333* Errprint:: Printing error messages
4334* Location:: Printing current location
4335* M4exit:: Exiting from `m4'
4336
4337
4338File: m4.info, Node: Errprint, Next: Location, Up: Miscellaneous
4339
434014.1 Printing error messages
4341============================
4342
4343You can print error messages using `errprint':
4344
4345 -- Builtin: errprint (MESSAGE, ...)
4346 Prints MESSAGE and the rest of the arguments to standard error,
4347 separated by spaces. Standard error is used, regardless of the
4348 `--debugfile' option (*note Invoking m4: Debugging options.).
4349
4350 The expansion of `errprint' is void. The macro `errprint' is
4351 recognized only with parameters.
4352
4353 errprint(`Invalid arguments to forloop
4354 ')
4355 error-->Invalid arguments to forloop
4356 =>
4357 errprint(`1')errprint(`2',`3
4358 ')
4359 error-->12 3
4360 =>
4361
4362 A trailing newline is _not_ printed automatically, so it should be
4363supplied as part of the argument, as in the example. Unfortunately, the
4364exact output of `errprint' is not very portable to other `m4'
4365implementations: POSIX requires that all arguments be printed, but some
4366implementations of `m4' only print the first. Furthermore, some BSD
4367implementations always append a newline for each `errprint' call,
4368regardless of whether the last argument already had one, and POSIX is
4369silent on whether this is acceptable.
4370
4371
4372File: m4.info, Node: Location, Next: M4exit, Prev: Errprint, Up: Miscellaneous
4373
437414.2 Printing current location
4375==============================
4376
4377To make it possible to specify the location of an error, three utility
4378builtins exist:
4379
4380 -- Builtin: __file__
4381 -- Builtin: __line__
4382 -- Builtin: __program__
4383 Expand to the quoted name of the current input file, the current
4384 input line number in that file, and the quoted name of the current
4385 invocation of `m4'.
4386
4387 errprint(__program__:__file__:__line__: `input error
4388 ')
4389 error-->m4:stdin:1: input error
4390 =>
4391
4392 Line numbers start at 1 for each file. If the file was found due to
4393the `-I' option or `M4PATH' environment variable, that is reflected in
4394the file name. The syncline option (`-s', *note Invoking m4:
4395Preprocessor features.), and the `f' and `l' flags of `debugmode'
4396(*note Debug Levels::), also use this notion of current file and line.
4397Redefining the three location macros has no effect on syncline, debug,
4398or warning message output. Assume this example is run in the
4399`m4-1.4.8/checks' directory of the GNU M4 package, using
4400`--include=../examples' in the command line to find the file `incl.m4'
4401mentioned earlier:
4402
4403 define(`foo', ``$0' called at __file__:__line__')
4404 =>
4405 foo
4406 =>foo called at stdin:2
4407 include(`incl.m4')
4408 =>Include file start
4409 =>foo called at ../examples/incl.m4:2
4410 =>Include file end
4411 =>
4412
4413 The location of macros invoked during the rescanning of macro
4414expansion text corresponds to the location in the file where the
4415expansion was triggered, regardless of how many newline characters the
4416expansion text contains. As of GNU M4 1.4.8, the location of text
4417wrapped with `m4wrap' (*note M4wrap::) is the point at which the
4418`m4wrap' was invoked. Previous versions, however, behaved as though
4419wrapped text came from line 0 of the file "".
4420
4421 define(`echo', `$@')
4422 =>
4423 define(`foo', `echo(__line__
4424 __line__)')
4425 =>
4426 echo(__line__
4427 __line__)
4428 =>4
4429 =>5
4430 m4wrap(`foo
4431 ')
4432 =>
4433 foo(errprint(__line__
4434 __line__
4435 ))
4436 error-->8
4437 error-->9
4438 =>8
4439 =>8
4440 __line__
4441 =>11
4442 ^D
4443 =>6
4444 =>6
4445
4446 The `__program__' macro behaves like `$0' in shell terminology. If
4447you invoke `m4' through an absolute path or a link with a different
4448spelling, rather than by relying on a `PATH' search for plain `m4', it
4449will affect how `__program__' expands. The intent is that you can use
4450it to produce error messages with the same formatting that `m4'
4451produces internally. It can also be used within `syscmd' (*note
4452Syscmd::) to pick the same version of `m4' that is currently running,
4453rather than whatever version of `m4' happens to be first in `PATH'. It
4454was first introduced in GNU M4 1.4.6.
4455
4456
4457File: m4.info, Node: M4exit, Prev: Location, Up: Miscellaneous
4458
445914.3 Exiting from `m4'
4460======================
4461
4462If you need to exit from `m4' before the entire input has been read,
4463you can use `m4exit':
4464
4465 -- Builtin: m4exit ([CODE = `0'])
4466 Causes `m4' to exit, with exit status CODE. If CODE is left out,
4467 the exit status is zero. If CODE cannot be parsed, or is outside
4468 the range of 0 to 255, the exit status is one. No further input
4469 is read, and all wrapped and diverted text is discarded.
4470
4471 m4wrap(`This text is lost due to `m4exit'.')
4472 =>
4473 divert(`1') So is this.
4474 divert
4475 =>
4476 m4exit And this is never read.
4477
4478 A common use of this is to abort processing:
4479
4480 -- Composite: fatal_error (MESSAGE)
4481 Abort processing with an error message and non-zero status. Prefix
4482 MESSAGE with details about where the error occurred, and print the
4483 resulting string to standard error.
4484
4485 define(`fatal_error',
4486 `errprint(__program__:__file__:__line__`: fatal error: $*
4487 ')m4exit(`1')')
4488 =>
4489 fatal_error(`this is a BAD one, buster')
4490 error-->m4:stdin:4: fatal error: this is a BAD one, buster
4491
4492 After this macro call, `m4' will exit with exit status 1. This macro
4493is only intended for error exits, since the normal exit procedures are
4494not followed, e.g., diverted text is not undiverted, and saved text
4495(*note M4wrap::) is not reread. (This macro could be made more robust
4496to earlier versions of `m4'. You should try to see if you can find
4497weaknesses and correct them; or *note Answers: Improved fatal_error.).
4498
4499 Note that it is still possible for the exit status to be different
4500than what was requested by `m4exit'. If `m4' detects some other error,
4501such as a write error on standard output, the exit status will be
4502non-zero even if `m4exit' requested zero.
4503
4504 If standard input is seekable, then the file will be positioned at
4505the next unread character. If it is a pipe or other non-seekable file,
4506then there are no guarantees how much data `m4' might have read into
4507buffers, and thus discarded.
4508
4509
4510File: m4.info, Node: Frozen files, Next: Compatibility, Prev: Miscellaneous, Up: Top
4511
451215 Fast loading of frozen state
4513*******************************
4514
4515Some bigger `m4' applications may be built over a common base
4516containing hundreds of definitions and other costly initializations.
4517Usually, the common base is kept in one or more declarative files,
4518which files are listed on each `m4' invocation prior to the user's
4519input file, or else each input file uses `include'.
4520
4521 Reading the common base of a big application, over and over again,
4522may be time consuming. GNU `m4' offers some machinery to speed up the
4523start of an application using lengthy common bases.
4524
4525* Menu:
4526
4527* Using frozen files:: Using frozen files
4528* Frozen file format:: Frozen file format
4529
4530
4531File: m4.info, Node: Using frozen files, Next: Frozen file format, Up: Frozen files
4532
453315.1 Using frozen files
4534=======================
4535
4536Suppose a user has a library of `m4' initializations in `base.m4',
4537which is then used with multiple input files:
4538
4539 m4 base.m4 input1.m4
4540 m4 base.m4 input2.m4
4541 m4 base.m4 input3.m4
4542
4543 Rather than spending time parsing the fixed contents of `base.m4'
4544every time, the user might rather execute:
4545
4546 m4 -F base.m4f base.m4
4547
4548once, and further execute, as often as needed:
4549
4550 m4 -R base.m4f input1.m4
4551 m4 -R base.m4f input2.m4
4552 m4 -R base.m4f input3.m4
4553
4554with the varying input. The first call, containing the `-F' option,
4555only reads and executes file `base.m4', defining various application
4556macros and computing other initializations. Once the input file
4557`base.m4' has been completely processed, GNU `m4' produces on
4558`base.m4f' a "frozen" file, that is, a file which contains a kind of
4559snapshot of the `m4' internal state.
4560
4561 Later calls, containing the `-R' option, are able to reload the
4562internal state of `m4', from `base.m4f', _prior_ to reading any other
4563input files. This means instead of starting with a virgin copy of
4564`m4', input will be read after having effectively recovered the effect
4565of a prior run. In our example, the effect is the same as if file
4566`base.m4' has been read anew. However, this effect is achieved a lot
4567faster.
4568
4569 Only one frozen file may be created or read in any one `m4'
4570invocation. It is not possible to recover two frozen files at once.
4571However, frozen files may be updated incrementally, through using `-R'
4572and `-F' options simultaneously. For example, if some care is taken,
4573the command:
4574
4575 m4 file1.m4 file2.m4 file3.m4 file4.m4
4576
4577could be broken down in the following sequence, accumulating the same
4578output:
4579
4580 m4 -F file1.m4f file1.m4
4581 m4 -R file1.m4f -F file2.m4f file2.m4
4582 m4 -R file2.m4f -F file3.m4f file3.m4
4583 m4 -R file3.m4f file4.m4
4584
4585 Some care is necessary because not every effort has been made for
4586this to work in all cases. In particular, the trace attribute of
4587macros is not handled, nor the current setting of `changeword'.
4588Currently, `m4wrap' and `sysval' also have problems. Also,
4589interactions for some options of `m4', being used in one call and not
4590in the next, have not been fully analyzed yet. On the other end, you
4591may be confident that stacks of `pushdef' definitions are handled
4592correctly, as well as undefined or renamed builtins, and changed
4593strings for quotes or comments. And future releases of GNU M4 will
4594improve on the utility of frozen files.
4595
4596 When an `m4' run is to be frozen, the automatic undiversion which
4597takes place at end of execution is inhibited. Instead, all positively
4598numbered diversions are saved into the frozen file. The active
4599diversion number is also transmitted.
4600
4601 A frozen file to be reloaded need not reside in the current
4602directory. It is looked up the same way as an `include' file (*note
4603Search Path::).
4604
4605 If the frozen file was generated with a newer version of `m4', and
4606contains directives that an older `m4' cannot parse, attempting to load
4607the frozen file with option `-R' will cause `m4' to exit with status 63
4608to indicate version mismatch.
4609
4610
4611File: m4.info, Node: Frozen file format, Prev: Using frozen files, Up: Frozen files
4612
461315.2 Frozen file format
4614=======================
4615
4616Frozen files are sharable across architectures. It is safe to write a
4617frozen file on one machine and read it on another, given that the
4618second machine uses the same or newer version of GNU `m4'. It is
4619conventional, but not required, to give a frozen file the suffix of
4620`.m4f'.
4621
4622 These are simple (editable) text files, made up of directives, each
4623starting with a capital letter and ending with a newline (<NL>).
4624Wherever a directive is expected, the character `#' introduces a
4625comment line; empty lines are also ignored if they are not part of an
4626embedded string. In the following descriptions, each LEN refers to the
4627length of the corresponding strings STR in the next line of input.
4628Numbers are always expressed in decimal. There are no escape
4629characters. The directives are:
4630
4631`C LEN1 , LEN2 <NL> STR1 STR2 <NL>'
4632 Uses STR1 and STR2 as the begin-comment and end-comment strings.
4633 If omitted, then `#' and <NL> are the comment delimiters.
4634
4635`D NUMBER, LEN <NL> STR <NL>'
4636 Selects diversion NUMBER, making it current, then copy STR in the
4637 current diversion. NUMBER may be a negative number for a
4638 non-existing diversion. To merely specify an active selection,
4639 use this command with an empty STR. With 0 as the diversion
4640 NUMBER, STR will be issued on standard output at reload time. GNU
4641 `m4' will not produce the `D' directive with non-zero length for
4642 diversion 0, but this can be done with manual edits. This
4643 directive may appear more than once for the same diversion, in
4644 which case the diversion is the concatenation of the various uses.
4645 If omitted, then diversion 0 is current.
4646
4647`F LEN1 , LEN2 <NL> STR1 STR2 <NL>'
4648 Defines, through `pushdef', a definition for STR1 expanding to the
4649 function whose builtin name is STR2. If the builtin does not
4650 exist (for example, if the frozen file was produced by a copy of
4651 `m4' compiled with changeword support, but the version of `m4'
4652 reloading was compiled without it), the reload is silent, but any
4653 subsequent use of the definition of STR1 will result in a warning.
4654 This directive may appear more than once for the same name, and
4655 its order, along with `T', is important. If omitted, you will
4656 have no access to any builtins.
4657
4658`Q LEN1 , LEN2 <NL> STR1 STR2 <NL>'
4659 Uses STR1 and STR2 as the begin-quote and end-quote strings. If
4660 omitted, then ``' and `'' are the quote delimiters.
4661
4662`T LEN1 , LEN2 <NL> STR1 STR2 <NL>'
4663 Defines, though `pushdef', a definition for STR1 expanding to the
4664 text given by STR2. This directive may appear more than once for
4665 the same name, and its order, along with `F', is important.
4666
4667`V NUMBER <NL>'
4668 Confirms the format of the file. `m4' 1.4.8 only creates and
4669 understands frozen files where NUMBER is 1. This directive must
4670 be the first non-comment in the file, and may not appear more than
4671 once.
4672
4673
4674File: m4.info, Node: Compatibility, Next: Answers, Prev: Frozen files, Up: Top
4675
467616 Compatibility with other versions of `m4'
4677********************************************
4678
4679This chapter describes the differences between this implementation of
4680`m4', and the implementation found under UNIX, notably System V,
4681Release 3.
4682
4683 There are also differences in BSD flavors of `m4'. No attempt is
4684made to summarize these here.
4685
4686* Menu:
4687
4688* Extensions:: Extensions in GNU M4
4689* Incompatibilities:: Facilities in System V m4 not in GNU M4
4690* Other Incompatibilities:: Other incompatibilities
4691
4692
4693File: m4.info, Node: Extensions, Next: Incompatibilities, Up: Compatibility
4694
469516.1 Extensions in GNU `m4'
4696===========================
4697
4698This version of `m4' contains a few facilities that do not exist in
4699System V `m4'. These extra facilities are all suppressed by using the
4700`-G' command line option (*note Invoking m4: Limits control.), unless
4701overridden by other command line options.
4702
4703 * In the `$'N notation for macro arguments, N can contain several
4704 digits, while the System V `m4' only accepts one digit. This
4705 allows macros in GNU `m4' to take any number of arguments, and not
4706 only nine (*note Arguments::).
4707
4708 This means that `define(`foo', `$11')' is ambiguous between
4709 implementations. To portably choose between grabbing the first
4710 parameter and appending 1 to the expansion, or grabbing the
4711 eleventh parameter, you can do the following:
4712
4713 define(`a1', `A1')
4714 =>
4715 dnl First argument, concatenated with 1
4716 define(`_1', `$1')define(`first1', `_1($@)1')
4717 =>
4718 dnl Eleventh argument, portable
4719 define(`_9', `$9')define(`eleventh', `_9(shift(shift($@)))')
4720 =>
4721 dnl Eleventh argument, GNU style
4722 define(`Eleventh', `$11')
4723 =>
4724 first1(`a', `b', `c', `d', `e', `f', `g', `h', `i', `j', `k')
4725 =>A1
4726 eleventh(`a', `b', `c', `d', `e', `f', `g', `h', `i', `j', `k')
4727 =>k
4728 Eleventh(`a', `b', `c', `d', `e', `f', `g', `h', `i', `j', `k')
4729 =>k
4730
4731 * The `divert' (*note Divert::) macro can manage more than 9
4732 diversions. GNU `m4' treats all positive numbers as valid
4733 diversions, rather than discarding diversions greater than 9.
4734
4735 * Files included with `include' and `sinclude' are sought in a user
4736 specified search path, if they are not found in the working
4737 directory. The search path is specified by the `-I' option and the
4738 `M4PATH' environment variable (*note Search Path::).
4739
4740 * Arguments to `undivert' can be non-numeric, in which case the named
4741 file will be included uninterpreted in the output (*note
4742 Undivert::).
4743
4744 * Formatted output is supported through the `format' builtin, which
4745 is modeled after the C library function `printf' (*note Format::).
4746
4747 * Searches and text substitution through regular expressions are
4748 supported by the `regexp' (*note Regexp::) and `patsubst' (*note
4749 Patsubst::) builtins.
4750
4751 * The output of shell commands can be read into `m4' with `esyscmd'
4752 (*note Esyscmd::).
4753
4754 * There is indirect access to any builtin macro with `builtin'
4755 (*note Builtin::).
4756
4757 * Macros can be called indirectly through `indir' (*note Indir::).
4758
4759 * The name of the program, the current input file, and the current
4760 input line number are accessible through the builtins
4761 `__program__', `__file__', and `__line__' (*note Location::).
4762
4763 * The format of the output from `dumpdef' and macro tracing can be
4764 controlled with `debugmode' (*note Debug Levels::).
4765
4766 * The destination of trace and debug output can be controlled with
4767 `debugfile' (*note Debug Output::).
4768
4769 * The `maketemp' (*note Mkstemp::) macro behaves like `mkstemp',
4770 creating a new file with a unique name on every invocation, rather
4771 than following the insecure behavior of replacing the trailing `X'
4772 characters with the `m4' process id.
4773
4774 In addition to the above extensions, GNU `m4' implements the
4775following command line options: `-F', `-G', `-I', `-L', `-R', `-V',
4776`-W', `-d', `-i', `-l', `--debugfile' and `-t'. *Note Invoking m4::,
4777for a description of these options.
4778
4779 Also, the debugging and tracing facilities in GNU `m4' are much more
4780extensive than in most other versions of `m4'.
4781
4782
4783File: m4.info, Node: Incompatibilities, Next: Other Incompatibilities, Prev: Extensions, Up: Compatibility
4784
478516.2 Facilities in System V `m4' not in GNU `m4'
4786================================================
4787
4788The version of `m4' from System V contains a few facilities that have
4789not been implemented in GNU `m4' yet. Additionally, POSIX requires
4790some behaviors that GNU `m4' has not implemented yet. Relying on these
4791behaviors is non-portable, as a future release of GNU `m4' may change.
4792
4793 * System V `m4' supports multiple arguments to `defn', and POSIX
4794 requires it. This is not yet implemented in GNU `m4'.
4795 Unfortunately, this means it is not possible to mix builtins and
4796 other text into a single macro; a helper macro is required.
4797
4798 * POSIX requires an application to exit with non-zero status if it
4799 wrote an error message to stderr. This has not yet been
4800 consistently implemented for the various builtins that are
4801 required to issue an error (such as `include' (*note Include::)
4802 when a file is unreadable, `eval' (*note Eval::) when an argument
4803 cannot be parsed, or using `m4exit' (*note M4exit::) with a
4804 non-numeric argument).
4805
4806 * Some traditional implementations only allow reading standard input
4807 once, but GNU `m4' correctly handles multiple instances of `-' on
4808 the command line.
4809
4810 * POSIX requires `m4wrap' (*note M4wrap::) to act in FIFO (first-in,
4811 first-out) order, but GNU `m4' currently uses LIFO order.
4812 Furthermore, POSIX states that only the first argument to `m4wrap'
4813 is saved for later evaluation, bug GNU `m4' saves and processes
4814 all arguments, with output separated by spaces.
4815
4816 However, it is possible to emulate POSIX behavior by including the
4817 file `m4-1.4.8/examples/wrapfifo.m4' from the distribution:
4818
4819 undivert(`wrapfifo.m4')dnl
4820 =>dnl Redefine m4wrap to have FIFO semantics.
4821 =>define(`_m4wrap_level', `0')dnl
4822 =>define(`m4wrap',
4823 =>`ifdef(`m4wrap'_m4wrap_level,
4824 => `define(`m4wrap'_m4wrap_level,
4825 => defn(`m4wrap'_m4wrap_level)`$1')',
4826 => `builtin(`m4wrap', `define(`_m4wrap_level',
4827 => incr(_m4wrap_level))dnl
4828 =>m4wrap'_m4wrap_level)dnl
4829 =>define(`m4wrap'_m4wrap_level, `$1')')')dnl
4830 include(`wrapfifo.m4')
4831 =>
4832 m4wrap(`a`'m4wrap(`c
4833 ', `d')')m4wrap(`b')
4834 =>
4835 ^D
4836 =>abc
4837
4838 * POSIX requires that all builtins that require arguments, but are
4839 called without arguments, behave as though empty strings had been
4840 passed. For example, `a`'define`'b' would expand to `ab'. But
4841 GNU `m4' ignores certain builtins if they have missing arguments,
4842 giving `adefineb' for the above example.
4843
4844 * Traditional implementations handle `define(`f',`1')' (*note
4845 Define::) by undefining the entire stack of previous definitions,
4846 and if doing `undefine(`f')' first. GNU `m4' replaces just the top
4847 definition on the stack, as if doing `popdef(`f')' followed by
4848 `pushdef(`f',`1')'.
4849
4850 * POSIX requires `syscmd' (*note Syscmd::) to evaluate command
4851 output for macro expansion, but this appears to be a mistake in
4852 POSIX since traditional implementations did not do this. GNU `m4'
4853 follows traditional behavior in `syscmd', and provides the
4854 extension `esyscmd' that provides the POSIX semantics.
4855
4856 * At one point, POSIX required `changequote(ARG)' (*note
4857 Changequote::) to use newline as the close quote, but this was a
4858 bug, and the next version of POSIX is anticipated to state that
4859 using empty strings or just one argument is unspecified.
4860 Meanwhile, the GNU `m4' behavior of treating an empty end-quote
4861 delimiter as `'' is not portable, as Solaris treats it as
4862 repeating the start-quote delimiter, and BSD treats it as leaving
4863 the previous end-quote delimiter unchanged. For predictable
4864 results, never call changequote with just one argument, or with
4865 empty strings for arguments.
4866
4867 * At one point, POSIX required `changecom(ARG,)' (*note Changecom::)
4868 to make it impossible to end a comment, but this is a bug, and the
4869 next version of POSIX is anticipated to state that using empty
4870 strings is unspecified. Meanwhile, the GNU `m4' behavior of
4871 treating an empty end-comment delimiter as newline is not
4872 portable, as BSD treats it as leaving the previous end-comment
4873 delimiter unchanged. It is also impossible in BSD implementations
4874 to disable comments, even though that is required by POSIX. For
4875 predictable results, never call changecom with empty strings for
4876 arguments.
4877
4878 * Most implementations of `m4' give macros a higher precedence than
4879 comments when parsing, meaning that if the start delimiter given to
4880 `changecom' (*note Changecom::) starts with a macro name, comments
4881 are effectively disabled. POSIX does not specify what the
4882 precedence is, so the GNU `m4' parser recognizes comments, then
4883 macros, then quoted strings.
4884
4885 * Traditional implementations allow argument collection, but not
4886 string and comment processing, to span file boundaries. Thus, if
4887 `a.m4' contains `len(', and `b.m4' contains `abc)', `m4 a.m4 b.m4'
4888 outputs `3' with traditional `m4', but gives an error message that
4889 the end of file was encountered inside a macro with GNU `m4'. On
4890 the other hand, traditional implementations do end of file
4891 processing for files included with `include' or `sinclude' (*note
4892 Include::), while GNU `m4' seamlessly integrates the content of
4893 those files. Thus `include(`a.m4')include(`b.m4')' will output
4894 `3' instead of giving an error.
4895
4896 * Traditional `m4' treats `traceon' (*note Trace::) without
4897 arguments as a global variable, independent of named macro tracing.
4898 Also, once a macro is undefined, named tracing of that macro is
4899 lost. On the other hand, when GNU `m4' encounters `traceon'
4900 without arguments, it turns tracing on for all existing
4901 definitions at the time, but does not trace future definitions;
4902 `traceoff' without arguments turns tracing off for all definitions
4903 regardless of whether they were also traced by name; and tracing
4904 by name, such as with `-tfoo' at the command line or
4905 `traceon(`foo')' in the input, is an attribute that is preserved
4906 even if the macro is currently undefined.
4907
4908 * POSIX requires `eval' (*note Eval::) to treat all operators with
4909 the same precedence as C. However, GNU `m4' currently follows the
4910 traditional precedence of other `m4' implementations, where
4911 bitwise and logical negation (`~' and `!') have lower precedence
4912 than equality operators, rather than equal precedence with other
4913 unary operators. Use explicit parentheses to ensure proper
4914 precedence. As extensions to POSIX, GNU `m4' treats the shift
4915 operators `<<' and `>>' as well-defined on signed integers (even
4916 though they are not in C), and adds the exponentiation operator
4917 `**'.
4918
4919 * POSIX requires `translit' (*note Translit::) to treat each
4920 character of the second and third arguments literally, but GNU
4921 `m4' treats `-' as a range operator.
4922
4923 * POSIX requires `m4' to honor the locale environment variables of
4924 `LANG', `LC_ALL', `LC_CTYPE', `LC_MESSAGES', and `NLSPATH', but
4925 this has not yet been implemented in GNU `m4'.
4926
4927 * POSIX states that only unquoted leading newlines and blanks (that
4928 is, space and tab) are ignored when collecting macro arguments.
4929 However, this appears to be a bug in POSIX, since most traditional
4930 implementations also ignore all whitespace (formfeed, carriage
4931 return, and vertical tab). GNU `m4' follows tradition and ignores
4932 all leading unquoted whitespace.
4933
4934
4935File: m4.info, Node: Other Incompatibilities, Prev: Incompatibilities, Up: Compatibility
4936
493716.3 Other incompatibilities
4938============================
4939
4940There are a few other incompatibilities between this implementation of
4941`m4', and the System V version.
4942
4943 * GNU `m4' implements sync lines differently from System V `m4',
4944 when text is being diverted. GNU `m4' outputs the sync lines when
4945 the text is being diverted, and System V `m4' when the diverted
4946 text is being brought back.
4947
4948 The problem is which lines and file names should be attached to
4949 text that is being, or has been, diverted. System V `m4' regards
4950 all the diverted text as being generated by the source line
4951 containing the `undivert' call, whereas GNU `m4' regards the
4952 diverted text as being generated at the time it is diverted.
4953
4954 The sync line option is used mostly when using `m4' as a front end
4955 to a compiler. If a diverted line causes a compiler error, the
4956 error messages should most probably refer to the place where the
4957 diversion were made, and not where it was inserted again.
4958
4959 * GNU `m4' makes no attempt at prohibiting self-referential
4960 definitions like:
4961
4962 define(`x', `x')
4963 =>
4964 define(`x', `x ')
4965 =>
4966
4967 There is nothing inherently wrong with defining `x' to return `x'.
4968 The wrong thing is to expand `x' unquoted. In `m4', one might
4969 use macros to hold strings, as we do for variables in other
4970 programming languages, further checking them with:
4971
4972 ifelse(defn(`HOLDER'), `VALUE', ...)
4973
4974 In cases like this one, an interdiction for a macro to hold its own
4975 name would be a useless limitation. Of course, this leaves more
4976 rope for the GNU `m4' user to hang himself! Rescanning hangs may
4977 be avoided through careful programming, a little like for endless
4978 loops in traditional programming languages.
4979
4980
4981File: m4.info, Node: Answers, Next: Copying This Manual, Prev: Compatibility, Up: Top
4982
498317 Correct version of some examples
4984***********************************
4985
4986Some of the examples in this manuals are buggy or not very robust, for
4987demonstration purposes. Improved versions of these composite macros are
4988presented here.
4989
4990* Menu:
4991
4992* Improved exch:: Solution for `exch'
4993* Improved forloop:: Solution for `forloop'
4994* Improved foreach:: Solution for `foreach'
4995* Improved cleardivert:: Solution for `cleardivert'
4996* Improved fatal_error:: Solution for `fatal_error'
4997
4998
4999File: m4.info, Node: Improved exch, Next: Improved forloop, Up: Answers
5000
500117.1 Solution for `exch'
5002========================
5003
5004The `exch' macro (*note Arguments::) as presented requires clients to
5005double quote their arguments. A nicer definition, which lets clients
5006follow the rule of thumb of one level of quoting per level of
5007parentheses, involves adding quotes in the definition of `exch', as
5008follows:
5009
5010 define(`exch', ``$2', `$1'')
5011 =>
5012 define(exch(`expansion text', `macro'))
5013 =>
5014 macro
5015 =>expansion text
5016
5017
5018File: m4.info, Node: Improved forloop, Next: Improved foreach, Prev: Improved exch, Up: Answers
5019
502017.2 Solution for `forloop'
5021===========================
5022
5023The `forloop' macro (*note Forloop::) as presented earlier can go into
5024an infinite loop if given an iterator that is not parsed as a macro
5025name. It does not do any sanity checking on its numeric bounds, and
5026only permits decimal numbers for bounds. Here is an improved version,
5027shipped as `m4-1.4.8/examples/forloop2.m4'; this version also optimizes
5028based on the fact that the starting bound does not need to be passed to
5029the helper `_forloop'.
5030
5031 undivert(`forloop2.m4')dnl
5032 =>divert(`-1')
5033 =># forloop(var, from, to, stmt) - improved version:
5034 =># works even if VAR is not a strict macro name
5035 =># performs sanity check that FROM is larger than TO
5036 =># allows complex numerical expressions in TO and FROM
5037 =>define(`forloop', `ifelse(eval(`($3) >= ($2)'), `1',
5038 => `pushdef(`$1', eval(`$2'))_forloop(`$1',
5039 => eval(`$3'), `$4')popdef(`$1')')')
5040 =>define(`_forloop',
5041 => `$3`'ifelse(indir(`$1'), `$2', `',
5042 => `define(`$1', incr(indir(`$1')))$0($@)')')
5043 =>divert`'dnl
5044 include(`forloop2.m4')
5045 =>
5046 forloop(`i', `2', `1', `no iteration occurs')
5047 =>
5048 forloop(`', `1', `2', ` odd iterator name')
5049 => odd iterator name odd iterator name
5050 forloop(`i', `5 + 5', `0xc', ` 0x`'eval(i, `16')')
5051 => 0xa 0xb 0xc
5052 forloop(`i', `a', `b', `non-numeric bounds')
5053 error-->m4:stdin:6: bad expression in eval (bad input): (b) >= (a)
5054 =>
5055
5056 Of course, it is possible to make even more improvements, such as
5057adding an optional step argument, or allowing iteration through
5058descending sequences. GNU Autoconf provides some of these additional
5059bells and whistles in its `m4_for' macro.
5060
5061
5062File: m4.info, Node: Improved foreach, Next: Improved cleardivert, Prev: Improved forloop, Up: Answers
5063
506417.3 Solution for `foreach'
5065===========================
5066
5067The `foreach' and `foreachq' macros (*note Foreach::) as presented
5068earlier each have flaws. First, we will examine and fix the quadratic
5069behavior of `foreachq':
5070
5071 include(`foreachq.m4')
5072 =>
5073 traceon(`shift')debugmode(`aq')
5074 =>
5075 foreachq(`x', ``1', `2', `3', `4'', `x
5076 ')dnl
5077 =>1
5078 error-->m4trace: -3- shift(`1', `2', `3', `4')
5079 error-->m4trace: -2- shift(`1', `2', `3', `4')
5080 =>2
5081 error-->m4trace: -4- shift(`1', `2', `3', `4')
5082 error-->m4trace: -3- shift(`2', `3', `4')
5083 error-->m4trace: -3- shift(`1', `2', `3', `4')
5084 error-->m4trace: -2- shift(`2', `3', `4')
5085 =>3
5086 error-->m4trace: -5- shift(`1', `2', `3', `4')
5087 error-->m4trace: -4- shift(`2', `3', `4')
5088 error-->m4trace: -3- shift(`3', `4')
5089 error-->m4trace: -4- shift(`1', `2', `3', `4')
5090 error-->m4trace: -3- shift(`2', `3', `4')
5091 error-->m4trace: -2- shift(`3', `4')
5092 =>4
5093 error-->m4trace: -6- shift(`1', `2', `3', `4')
5094 error-->m4trace: -5- shift(`2', `3', `4')
5095 error-->m4trace: -4- shift(`3', `4')
5096 error-->m4trace: -3- shift(`4')
5097
5098 Each successive iteration was adding more quoted `shift'
5099invocations, and the entire list contents were passing through every
5100iteration. In general, when recursing, it is a good idea to make the
5101recursion use fewer arguments, rather than adding additional quoted
5102uses of `shift'. By doing so, `m4' uses less memory, invokes fewer
5103macros, is less likely to run into machine limits, and most
5104importantly, performs faster. The fixed version of `foreachq' can be
5105found in `m4-1.4.8/examples/foreachq2.m4':
5106
5107 include(`foreachq2.m4')
5108 =>
5109 undivert(`foreachq2.m4')dnl
5110 =>include(`quote.m4')dnl
5111 =>divert(`-1')
5112 =># foreachq(x, `item_1, item_2, ..., item_n', stmt)
5113 =># quoted list, improved version
5114 =>define(`foreachq', `pushdef(`$1')_foreachq($@)popdef(`$1')')
5115 =>define(`_arg1q', ``$1'')
5116 =>define(`_rest', `ifelse(`$#', `1', `', `dquote(shift($@))')')
5117 =>define(`_foreachq', `ifelse(`$2', `', `',
5118 => `define(`$1', _arg1q($2))$3`'$0(`$1', _rest($2), `$3')')')
5119 =>divert`'dnl
5120 traceon(`shift')debugmode(`aq')
5121 =>
5122 foreachq(`x', ``1', `2', `3', `4'', `x
5123 ')dnl
5124 =>1
5125 error-->m4trace: -3- shift(`1', `2', `3', `4')
5126 =>2
5127 error-->m4trace: -3- shift(`2', `3', `4')
5128 =>3
5129 error-->m4trace: -3- shift(`3', `4')
5130 =>4
5131
5132 Note that the fixed version calls unquoted helper macros in
5133`_foreachq' to trim elements immediately; those helper macros in turn
5134must re-supply the layer of quotes lost in the macro invocation.
5135Contrast the use of `_arg1q', which quotes the first list element, with
5136`_arg1' of the earlier implementation that returned the first list
5137element directly.
5138
5139 For a different approach, the improved version of `foreach',
5140available in `m4-1.4.8/examples/foreach2.m4', simply overquotes the
5141arguments to `_foreach' to begin with, using `dquote_elt'. Then
5142`_foreach' can just use `_arg1' to remove the extra layer of quoting
5143that was added up front:
5144
5145 include(`foreach2.m4')
5146 =>
5147 undivert(`foreach2.m4')dnl
5148 =>include(`quote.m4')dnl
5149 =>divert(`-1')
5150 =># foreach(x, (item_1, item_2, ..., item_n), stmt)
5151 =># parenthesized list, improved version
5152 =>define(`foreach', `pushdef(`$1')_foreach(`$1',
5153 => (dquote(dquote_elt$2)), `$3')popdef(`$1')')
5154 =>define(`_arg1', `$1')
5155 =>define(`_foreach', `ifelse(`$2', `(`')', `',
5156 => `define(`$1', _arg1$2)$3`'$0(`$1', (dquote(shift$2)), `$3')')')
5157 =>divert`'dnl
5158 traceon(`shift')debugmode(`aq')
5159 =>
5160 foreach(`x', `(`1', `2', `3', `4')', `x
5161 ')dnl
5162 error-->m4trace: -4- shift(`1', `2', `3', `4')
5163 error-->m4trace: -4- shift(`2', `3', `4')
5164 error-->m4trace: -4- shift(`3', `4')
5165 =>1
5166 error-->m4trace: -3- shift(``1'', ``2'', ``3'', ``4'')
5167 =>2
5168 error-->m4trace: -3- shift(``2'', ``3'', ``4'')
5169 =>3
5170 error-->m4trace: -3- shift(``3'', ``4'')
5171 =>4
5172 error-->m4trace: -3- shift(``4'')
5173
5174 In summary, recursion over list elements is trickier than it
5175appeared at first glance, but provides a powerful idiom within `m4'
5176processing. As a final demonstration, both list styles are now able to
5177handle several scenarios that would wreak havoc on the original
5178implementations. This points out one other difference between the two
5179list styles. `foreach' evaluates unquoted list elements only once, in
5180preparation for calling `_foreach'. But `foreachq' evaluates unquoted
5181list elements twice while visiting the first list element, once in
5182`_arg1q' and once in `_rest'. When deciding which list style to use,
5183one must take into account whether repeating the side effects of
5184unquoted list elements will have any detrimental effects.
5185
5186 include(`foreach2.m4')
5187 =>
5188 include(`foreachq2.m4')
5189 =>
5190 dnl 0-element list:
5191 foreach(`x', `', `<x>') / foreachq(`x', `', `<x>')
5192 => /
5193 dnl 1-element list of empty element
5194 foreach(`x', `()', `<x>') / foreachq(`x', ``'', `<x>')
5195 =><> / <>
5196 dnl 2-element list of empty elements
5197 foreach(`x', `(`',`')', `<x>') / foreachq(`x', ``',`'', `<x>')
5198 =><><> / <><>
5199 dnl 1-element list of a comma
5200 foreach(`x', `(`,')', `<x>') / foreachq(`x', ``,'', `<x>')
5201 =><,> / <,>
5202 dnl 2-element list of unbalanced parentheses
5203 foreach(`x', `(`(', `)')', `<x>') / foreachq(`x', ``(', `)'', `<x>')
5204 =><(><)> / <(><)>
5205 define(`active', `ACT, IVE')
5206 =>
5207 traceon(`active')
5208 =>
5209 dnl list of unquoted macros; expansion occurs before recursion
5210 foreach(`x', `(active, active)', `<x>
5211 ')dnl
5212 error-->m4trace: -4- active -> `ACT, IVE'
5213 error-->m4trace: -4- active -> `ACT, IVE'
5214 =><ACT>
5215 =><IVE>
5216 =><ACT>
5217 =><IVE>
5218 foreachq(`x', `active, active', `<x>
5219 ')dnl
5220 error-->m4trace: -3- active -> `ACT, IVE'
5221 error-->m4trace: -3- active -> `ACT, IVE'
5222 =><ACT>
5223 error-->m4trace: -3- active -> `ACT, IVE'
5224 error-->m4trace: -3- active -> `ACT, IVE'
5225 =><IVE>
5226 =><ACT>
5227 =><IVE>
5228 dnl list of quoted macros; expansion occurs during recursion
5229 foreach(`x', `(`active', `active')', `<x>
5230 ')dnl
5231 error-->m4trace: -1- active -> `ACT, IVE'
5232 =><ACT, IVE>
5233 error-->m4trace: -1- active -> `ACT, IVE'
5234 =><ACT, IVE>
5235 foreachq(`x', ``active', `active'', `<x>
5236 ')dnl
5237 error-->m4trace: -1- active -> `ACT, IVE'
5238 =><ACT, IVE>
5239 error-->m4trace: -1- active -> `ACT, IVE'
5240 =><ACT, IVE>
5241 dnl list of double-quoted macro names; no expansion
5242 foreach(`x', `(``active'', ``active'')', `<x>
5243 ')dnl
5244 =><active>
5245 =><active>
5246 foreachq(`x', ```active'', ``active''', `<x>
5247 ')dnl
5248 =><active>
5249 =><active>
5250
5251
5252File: m4.info, Node: Improved cleardivert, Next: Improved fatal_error, Prev: Improved foreach, Up: Answers
5253
525417.4 Solution for `cleardivert'
5255===============================
5256
5257The `cleardivert' macro (*note Cleardivert::) cannot, as it stands, be
5258called without arguments to clear all pending diversions. That is
5259because using undivert with an empty string for an argument is different
5260than using it with no arguments at all. Compare the earlier definition
5261with one that takes the number of arguments into account:
5262
5263 define(`cleardivert',
5264 `pushdef(`_n', divnum)divert(`-1')undivert($@)divert(_n)popdef(`_n')')
5265 =>
5266 divert(`1')one
5267 divert
5268 =>
5269 cleardivert
5270 =>
5271 undivert
5272 =>one
5273 =>
5274 define(`cleardivert',
5275 `pushdef(`_num', divnum)divert(`-1')ifelse(`$#', `0',
5276 `undivert`'', `undivert($@)')divert(_num)popdef(`_num')')
5277 =>
5278 divert(`2')two
5279 divert
5280 =>
5281 cleardivert
5282 =>
5283 undivert
5284 =>
5285
5286
5287File: m4.info, Node: Improved fatal_error, Prev: Improved cleardivert, Up: Answers
5288
528917.5 Solution for `fatal_error'
5290===============================
5291
5292The `fatal_error' macro (*note M4exit::) is not robust to versions of
5293GNU M4 earlier than 1.4.8, where invoking `__file__' (*note Location::)
5294inside `m4wrap' would result in an empty string, and `__line__'
5295resulted in `0' even though all files start at line 1. Furthermore,
5296versions earlier than 1.4.6 did not support the `__program__' macro.
5297If you want `fatal_error' to work across the entire 1.4.x release
5298series, a better implementation would be:
5299
5300 define(`fatal_error',
5301 `errprint(ifdef(`__program__', `__program__', ``m4'')'dnl
5302 `:ifelse(__line__, `0', `',
5303 `__file__:__line__:')` fatal error: $*
5304 ')m4exit(`1')')
5305 =>
5306 m4wrap(`divnum(`demo of internal message')
5307 fatal_error(`inside wrapped text')')
5308 =>
5309 ^D
5310 error-->m4:stdin:6: Warning: excess arguments to builtin `divnum' ignored
5311 =>0
5312 error-->m4:stdin:6: fatal error: inside wrapped text
5313
5314
5315File: m4.info, Node: Copying This Manual, Next: Indices, Prev: Answers, Up: Top
5316
5317Appendix A How to make copies of this manual
5318********************************************
5319
5320* Menu:
5321
5322* GNU Free Documentation License:: License for copying this manual
5323
5324
5325File: m4.info, Node: GNU Free Documentation License, Up: Copying This Manual
5326
5327A.1 GNU Free Documentation License
5328==================================
5329
5330 Version 1.2, November 2002
5331
5332 Copyright (C) 2000,2001,2002 Free Software Foundation, Inc.
5333 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
5334
5335 Everyone is permitted to copy and distribute verbatim copies
5336 of this license document, but changing it is not allowed.
5337
5338 0. PREAMBLE
5339
5340 The purpose of this License is to make a manual, textbook, or other
5341 functional and useful document "free" in the sense of freedom: to
5342 assure everyone the effective freedom to copy and redistribute it,
5343 with or without modifying it, either commercially or
5344 noncommercially. Secondarily, this License preserves for the
5345 author and publisher a way to get credit for their work, while not
5346 being considered responsible for modifications made by others.
5347
5348 This License is a kind of "copyleft", which means that derivative
5349 works of the document must themselves be free in the same sense.
5350 It complements the GNU General Public License, which is a copyleft
5351 license designed for free software.
5352
5353 We have designed this License in order to use it for manuals for
5354 free software, because free software needs free documentation: a
5355 free program should come with manuals providing the same freedoms
5356 that the software does. But this License is not limited to
5357 software manuals; it can be used for any textual work, regardless
5358 of subject matter or whether it is published as a printed book.
5359 We recommend this License principally for works whose purpose is
5360 instruction or reference.
5361
5362 1. APPLICABILITY AND DEFINITIONS
5363
5364 This License applies to any manual or other work, in any medium,
5365 that contains a notice placed by the copyright holder saying it
5366 can be distributed under the terms of this License. Such a notice
5367 grants a world-wide, royalty-free license, unlimited in duration,
5368 to use that work under the conditions stated herein. The
5369 "Document", below, refers to any such manual or work. Any member
5370 of the public is a licensee, and is addressed as "you". You
5371 accept the license if you copy, modify or distribute the work in a
5372 way requiring permission under copyright law.
5373
5374 A "Modified Version" of the Document means any work containing the
5375 Document or a portion of it, either copied verbatim, or with
5376 modifications and/or translated into another language.
5377
5378 A "Secondary Section" is a named appendix or a front-matter section
5379 of the Document that deals exclusively with the relationship of the
5380 publishers or authors of the Document to the Document's overall
5381 subject (or to related matters) and contains nothing that could
5382 fall directly within that overall subject. (Thus, if the Document
5383 is in part a textbook of mathematics, a Secondary Section may not
5384 explain any mathematics.) The relationship could be a matter of
5385 historical connection with the subject or with related matters, or
5386 of legal, commercial, philosophical, ethical or political position
5387 regarding them.
5388
5389 The "Invariant Sections" are certain Secondary Sections whose
5390 titles are designated, as being those of Invariant Sections, in
5391 the notice that says that the Document is released under this
5392 License. If a section does not fit the above definition of
5393 Secondary then it is not allowed to be designated as Invariant.
5394 The Document may contain zero Invariant Sections. If the Document
5395 does not identify any Invariant Sections then there are none.
5396
5397 The "Cover Texts" are certain short passages of text that are
5398 listed, as Front-Cover Texts or Back-Cover Texts, in the notice
5399 that says that the Document is released under this License. A
5400 Front-Cover Text may be at most 5 words, and a Back-Cover Text may
5401 be at most 25 words.
5402
5403 A "Transparent" copy of the Document means a machine-readable copy,
5404 represented in a format whose specification is available to the
5405 general public, that is suitable for revising the document
5406 straightforwardly with generic text editors or (for images
5407 composed of pixels) generic paint programs or (for drawings) some
5408 widely available drawing editor, and that is suitable for input to
5409 text formatters or for automatic translation to a variety of
5410 formats suitable for input to text formatters. A copy made in an
5411 otherwise Transparent file format whose markup, or absence of
5412 markup, has been arranged to thwart or discourage subsequent
5413 modification by readers is not Transparent. An image format is
5414 not Transparent if used for any substantial amount of text. A
5415 copy that is not "Transparent" is called "Opaque".
5416
5417 Examples of suitable formats for Transparent copies include plain
5418 ASCII without markup, Texinfo input format, LaTeX input format,
5419 SGML or XML using a publicly available DTD, and
5420 standard-conforming simple HTML, PostScript or PDF designed for
5421 human modification. Examples of transparent image formats include
5422 PNG, XCF and JPG. Opaque formats include proprietary formats that
5423 can be read and edited only by proprietary word processors, SGML or
5424 XML for which the DTD and/or processing tools are not generally
5425 available, and the machine-generated HTML, PostScript or PDF
5426 produced by some word processors for output purposes only.
5427
5428 The "Title Page" means, for a printed book, the title page itself,
5429 plus such following pages as are needed to hold, legibly, the
5430 material this License requires to appear in the title page. For
5431 works in formats which do not have any title page as such, "Title
5432 Page" means the text near the most prominent appearance of the
5433 work's title, preceding the beginning of the body of the text.
5434
5435 A section "Entitled XYZ" means a named subunit of the Document
5436 whose title either is precisely XYZ or contains XYZ in parentheses
5437 following text that translates XYZ in another language. (Here XYZ
5438 stands for a specific section name mentioned below, such as
5439 "Acknowledgements", "Dedications", "Endorsements", or "History".)
5440 To "Preserve the Title" of such a section when you modify the
5441 Document means that it remains a section "Entitled XYZ" according
5442 to this definition.
5443
5444 The Document may include Warranty Disclaimers next to the notice
5445 which states that this License applies to the Document. These
5446 Warranty Disclaimers are considered to be included by reference in
5447 this License, but only as regards disclaiming warranties: any other
5448 implication that these Warranty Disclaimers may have is void and
5449 has no effect on the meaning of this License.
5450
5451 2. VERBATIM COPYING
5452
5453 You may copy and distribute the Document in any medium, either
5454 commercially or noncommercially, provided that this License, the
5455 copyright notices, and the license notice saying this License
5456 applies to the Document are reproduced in all copies, and that you
5457 add no other conditions whatsoever to those of this License. You
5458 may not use technical measures to obstruct or control the reading
5459 or further copying of the copies you make or distribute. However,
5460 you may accept compensation in exchange for copies. If you
5461 distribute a large enough number of copies you must also follow
5462 the conditions in section 3.
5463
5464 You may also lend copies, under the same conditions stated above,
5465 and you may publicly display copies.
5466
5467 3. COPYING IN QUANTITY
5468
5469 If you publish printed copies (or copies in media that commonly
5470 have printed covers) of the Document, numbering more than 100, and
5471 the Document's license notice requires Cover Texts, you must
5472 enclose the copies in covers that carry, clearly and legibly, all
5473 these Cover Texts: Front-Cover Texts on the front cover, and
5474 Back-Cover Texts on the back cover. Both covers must also clearly
5475 and legibly identify you as the publisher of these copies. The
5476 front cover must present the full title with all words of the
5477 title equally prominent and visible. You may add other material
5478 on the covers in addition. Copying with changes limited to the
5479 covers, as long as they preserve the title of the Document and
5480 satisfy these conditions, can be treated as verbatim copying in
5481 other respects.
5482
5483 If the required texts for either cover are too voluminous to fit
5484 legibly, you should put the first ones listed (as many as fit
5485 reasonably) on the actual cover, and continue the rest onto
5486 adjacent pages.
5487
5488 If you publish or distribute Opaque copies of the Document
5489 numbering more than 100, you must either include a
5490 machine-readable Transparent copy along with each Opaque copy, or
5491 state in or with each Opaque copy a computer-network location from
5492 which the general network-using public has access to download
5493 using public-standard network protocols a complete Transparent
5494 copy of the Document, free of added material. If you use the
5495 latter option, you must take reasonably prudent steps, when you
5496 begin distribution of Opaque copies in quantity, to ensure that
5497 this Transparent copy will remain thus accessible at the stated
5498 location until at least one year after the last time you
5499 distribute an Opaque copy (directly or through your agents or
5500 retailers) of that edition to the public.
5501
5502 It is requested, but not required, that you contact the authors of
5503 the Document well before redistributing any large number of
5504 copies, to give them a chance to provide you with an updated
5505 version of the Document.
5506
5507 4. MODIFICATIONS
5508
5509 You may copy and distribute a Modified Version of the Document
5510 under the conditions of sections 2 and 3 above, provided that you
5511 release the Modified Version under precisely this License, with
5512 the Modified Version filling the role of the Document, thus
5513 licensing distribution and modification of the Modified Version to
5514 whoever possesses a copy of it. In addition, you must do these
5515 things in the Modified Version:
5516
5517 A. Use in the Title Page (and on the covers, if any) a title
5518 distinct from that of the Document, and from those of
5519 previous versions (which should, if there were any, be listed
5520 in the History section of the Document). You may use the
5521 same title as a previous version if the original publisher of
5522 that version gives permission.
5523
5524 B. List on the Title Page, as authors, one or more persons or
5525 entities responsible for authorship of the modifications in
5526 the Modified Version, together with at least five of the
5527 principal authors of the Document (all of its principal
5528 authors, if it has fewer than five), unless they release you
5529 from this requirement.
5530
5531 C. State on the Title page the name of the publisher of the
5532 Modified Version, as the publisher.
5533
5534 D. Preserve all the copyright notices of the Document.
5535
5536 E. Add an appropriate copyright notice for your modifications
5537 adjacent to the other copyright notices.
5538
5539 F. Include, immediately after the copyright notices, a license
5540 notice giving the public permission to use the Modified
5541 Version under the terms of this License, in the form shown in
5542 the Addendum below.
5543
5544 G. Preserve in that license notice the full lists of Invariant
5545 Sections and required Cover Texts given in the Document's
5546 license notice.
5547
5548 H. Include an unaltered copy of this License.
5549
5550 I. Preserve the section Entitled "History", Preserve its Title,
5551 and add to it an item stating at least the title, year, new
5552 authors, and publisher of the Modified Version as given on
5553 the Title Page. If there is no section Entitled "History" in
5554 the Document, create one stating the title, year, authors,
5555 and publisher of the Document as given on its Title Page,
5556 then add an item describing the Modified Version as stated in
5557 the previous sentence.
5558
5559 J. Preserve the network location, if any, given in the Document
5560 for public access to a Transparent copy of the Document, and
5561 likewise the network locations given in the Document for
5562 previous versions it was based on. These may be placed in
5563 the "History" section. You may omit a network location for a
5564 work that was published at least four years before the
5565 Document itself, or if the original publisher of the version
5566 it refers to gives permission.
5567
5568 K. For any section Entitled "Acknowledgements" or "Dedications",
5569 Preserve the Title of the section, and preserve in the
5570 section all the substance and tone of each of the contributor
5571 acknowledgements and/or dedications given therein.
5572
5573 L. Preserve all the Invariant Sections of the Document,
5574 unaltered in their text and in their titles. Section numbers
5575 or the equivalent are not considered part of the section
5576 titles.
5577
5578 M. Delete any section Entitled "Endorsements". Such a section
5579 may not be included in the Modified Version.
5580
5581 N. Do not retitle any existing section to be Entitled
5582 "Endorsements" or to conflict in title with any Invariant
5583 Section.
5584
5585 O. Preserve any Warranty Disclaimers.
5586
5587 If the Modified Version includes new front-matter sections or
5588 appendices that qualify as Secondary Sections and contain no
5589 material copied from the Document, you may at your option
5590 designate some or all of these sections as invariant. To do this,
5591 add their titles to the list of Invariant Sections in the Modified
5592 Version's license notice. These titles must be distinct from any
5593 other section titles.
5594
5595 You may add a section Entitled "Endorsements", provided it contains
5596 nothing but endorsements of your Modified Version by various
5597 parties--for example, statements of peer review or that the text
5598 has been approved by an organization as the authoritative
5599 definition of a standard.
5600
5601 You may add a passage of up to five words as a Front-Cover Text,
5602 and a passage of up to 25 words as a Back-Cover Text, to the end
5603 of the list of Cover Texts in the Modified Version. Only one
5604 passage of Front-Cover Text and one of Back-Cover Text may be
5605 added by (or through arrangements made by) any one entity. If the
5606 Document already includes a cover text for the same cover,
5607 previously added by you or by arrangement made by the same entity
5608 you are acting on behalf of, you may not add another; but you may
5609 replace the old one, on explicit permission from the previous
5610 publisher that added the old one.
5611
5612 The author(s) and publisher(s) of the Document do not by this
5613 License give permission to use their names for publicity for or to
5614 assert or imply endorsement of any Modified Version.
5615
5616 5. COMBINING DOCUMENTS
5617
5618 You may combine the Document with other documents released under
5619 this License, under the terms defined in section 4 above for
5620 modified versions, provided that you include in the combination
5621 all of the Invariant Sections of all of the original documents,
5622 unmodified, and list them all as Invariant Sections of your
5623 combined work in its license notice, and that you preserve all
5624 their Warranty Disclaimers.
5625
5626 The combined work need only contain one copy of this License, and
5627 multiple identical Invariant Sections may be replaced with a single
5628 copy. If there are multiple Invariant Sections with the same name
5629 but different contents, make the title of each such section unique
5630 by adding at the end of it, in parentheses, the name of the
5631 original author or publisher of that section if known, or else a
5632 unique number. Make the same adjustment to the section titles in
5633 the list of Invariant Sections in the license notice of the
5634 combined work.
5635
5636 In the combination, you must combine any sections Entitled
5637 "History" in the various original documents, forming one section
5638 Entitled "History"; likewise combine any sections Entitled
5639 "Acknowledgements", and any sections Entitled "Dedications". You
5640 must delete all sections Entitled "Endorsements."
5641
5642 6. COLLECTIONS OF DOCUMENTS
5643
5644 You may make a collection consisting of the Document and other
5645 documents released under this License, and replace the individual
5646 copies of this License in the various documents with a single copy
5647 that is included in the collection, provided that you follow the
5648 rules of this License for verbatim copying of each of the
5649 documents in all other respects.
5650
5651 You may extract a single document from such a collection, and
5652 distribute it individually under this License, provided you insert
5653 a copy of this License into the extracted document, and follow
5654 this License in all other respects regarding verbatim copying of
5655 that document.
5656
5657 7. AGGREGATION WITH INDEPENDENT WORKS
5658
5659 A compilation of the Document or its derivatives with other
5660 separate and independent documents or works, in or on a volume of
5661 a storage or distribution medium, is called an "aggregate" if the
5662 copyright resulting from the compilation is not used to limit the
5663 legal rights of the compilation's users beyond what the individual
5664 works permit. When the Document is included in an aggregate, this
5665 License does not apply to the other works in the aggregate which
5666 are not themselves derivative works of the Document.
5667
5668 If the Cover Text requirement of section 3 is applicable to these
5669 copies of the Document, then if the Document is less than one half
5670 of the entire aggregate, the Document's Cover Texts may be placed
5671 on covers that bracket the Document within the aggregate, or the
5672 electronic equivalent of covers if the Document is in electronic
5673 form. Otherwise they must appear on printed covers that bracket
5674 the whole aggregate.
5675
5676 8. TRANSLATION
5677
5678 Translation is considered a kind of modification, so you may
5679 distribute translations of the Document under the terms of section
5680 4. Replacing Invariant Sections with translations requires special
5681 permission from their copyright holders, but you may include
5682 translations of some or all Invariant Sections in addition to the
5683 original versions of these Invariant Sections. You may include a
5684 translation of this License, and all the license notices in the
5685 Document, and any Warranty Disclaimers, provided that you also
5686 include the original English version of this License and the
5687 original versions of those notices and disclaimers. In case of a
5688 disagreement between the translation and the original version of
5689 this License or a notice or disclaimer, the original version will
5690 prevail.
5691
5692 If a section in the Document is Entitled "Acknowledgements",
5693 "Dedications", or "History", the requirement (section 4) to
5694 Preserve its Title (section 1) will typically require changing the
5695 actual title.
5696
5697 9. TERMINATION
5698
5699 You may not copy, modify, sublicense, or distribute the Document
5700 except as expressly provided for under this License. Any other
5701 attempt to copy, modify, sublicense or distribute the Document is
5702 void, and will automatically terminate your rights under this
5703 License. However, parties who have received copies, or rights,
5704 from you under this License will not have their licenses
5705 terminated so long as such parties remain in full compliance.
5706
5707 10. FUTURE REVISIONS OF THIS LICENSE
5708
5709 The Free Software Foundation may publish new, revised versions of
5710 the GNU Free Documentation License from time to time. Such new
5711 versions will be similar in spirit to the present version, but may
5712 differ in detail to address new problems or concerns. See
5713 `http://www.gnu.org/copyleft/'.
5714
5715 Each version of the License is given a distinguishing version
5716 number. If the Document specifies that a particular numbered
5717 version of this License "or any later version" applies to it, you
5718 have the option of following the terms and conditions either of
5719 that specified version or of any later version that has been
5720 published (not as a draft) by the Free Software Foundation. If
5721 the Document does not specify a version number of this License,
5722 you may choose any version ever published (not as a draft) by the
5723 Free Software Foundation.
5724
5725ADDENDUM: How to use this License for your documents
5726====================================================
5727
5728To use this License in a document you have written, include a copy of
5729the License in the document and put the following copyright and license
5730notices just after the title page:
5731
5732 Copyright (C) YEAR YOUR NAME.
5733 Permission is granted to copy, distribute and/or modify this document
5734 under the terms of the GNU Free Documentation License, Version 1.2
5735 or any later version published by the Free Software Foundation;
5736 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
5737 Texts. A copy of the license is included in the section entitled ``GNU
5738 Free Documentation License''.
5739
5740 If you have Invariant Sections, Front-Cover Texts and Back-Cover
5741Texts, replace the "with...Texts." line with this:
5742
5743 with the Invariant Sections being LIST THEIR TITLES, with
5744 the Front-Cover Texts being LIST, and with the Back-Cover Texts
5745 being LIST.
5746
5747 If you have Invariant Sections without Cover Texts, or some other
5748combination of the three, merge those two alternatives to suit the
5749situation.
5750
5751 If your document contains nontrivial examples of program code, we
5752recommend releasing these examples in parallel under your choice of
5753free software license, such as the GNU General Public License, to
5754permit their use in free software.
5755
5756
5757File: m4.info, Node: Indices, Prev: Copying This Manual, Up: Top
5758
5759Appendix B Indices of concepts and macros
5760*****************************************
5761
5762* Menu:
5763
5764* Concept index:: Index for many concepts
5765* Macro index:: Index for all `m4' macros
5766
5767
5768File: m4.info, Node: Concept index, Next: Macro index, Up: Indices
5769
5770B.1 Index for many concepts
5771===========================
5772
5773[index]
5774* Menu:
5775
5776* arguments to macros: Macro Arguments. (line 6)
5777* Arguments to macros: Arguments. (line 6)
5778* arguments to macros, special: Pseudo Arguments. (line 6)
5779* arguments, quoted macro: Quoting Arguments. (line 6)
5780* arithmetic: Arithmetic. (line 6)
5781* arrays: Define. (line 52)
5782* builtins, indirect call of: Builtin. (line 6)
5783* call of builtins, indirect: Builtin. (line 6)
5784* call of macros, indirect: Indir. (line 6)
5785* changing comment delimiters: Changecom. (line 6)
5786* changing the quote delimiters: Changequote. (line 6)
5787* characters, translating: Translit. (line 6)
5788* command line, file names on the: Command line files. (line 6)
5789* command line, macro definitions on the: Preprocessor features.
5790 (line 6)
5791* command line, options: Invoking m4. (line 10)
5792* commands, exit status from shell: Sysval. (line 6)
5793* commands, running shell: Shell commands. (line 6)
5794* commands, running UNIX: Shell commands. (line 6)
5795* comment delimiters, changing: Changecom. (line 6)
5796* comments: Comments. (line 6)
5797* comments, copied to output: Changecom. (line 29)
5798* comparing strings: Ifelse. (line 6)
5799* compatibility: Compatibility. (line 6)
5800* conditionals: Ifdef. (line 6)
5801* controlling debugging output: Debug Levels. (line 6)
5802* counting loops: Forloop. (line 6)
5803* debugging output, controlling: Debug Levels. (line 6)
5804* debugging output, saving: Debug Output. (line 6)
5805* decrement operator: Incr. (line 6)
5806* defining new macros: Definitions. (line 6)
5807* definitions, displaying macro: Dumpdef. (line 6)
5808* deleting macros: Undefine. (line 6)
5809* deleting whitespace in input: Dnl. (line 6)
5810* discarding diverted text: Cleardivert. (line 6)
5811* displaying macro definitions: Dumpdef. (line 6)
5812* diversion numbers: Divnum. (line 6)
5813* diverted text, discarding: Cleardivert. (line 6)
5814* diverting output to files: Divert. (line 6)
5815* dumping into frozen file: Using frozen files. (line 6)
5816* error messages, printing: Errprint. (line 6)
5817* evaluation, of integer expressions: Eval. (line 6)
5818* executing shell commands: Shell commands. (line 6)
5819* executing UNIX commands: Shell commands. (line 6)
5820* exit status from shell commands: Sysval. (line 6)
5821* exiting from m4: M4exit. (line 6)
5822* expansion of macros: Macro expansion. (line 6)
5823* expansion, tracing macro: Trace. (line 6)
5824* expressions, evaluation of integer: Eval. (line 6)
5825* extracting substrings: Substr. (line 6)
5826* fast loading of frozen files: Using frozen files. (line 6)
5827* FDL, GNU Free Documentation License: GNU Free Documentation License.
5828 (line 6)
5829* file format, frozen file: Frozen file format. (line 6)
5830* file inclusion <1>: Undivert. (line 81)
5831* file inclusion: File Inclusion. (line 6)
5832* file names, on the command line: Command line files. (line 6)
5833* files, diverting output to: Divert. (line 6)
5834* files, names of temporary: Mkstemp. (line 6)
5835* for each loops: Foreach. (line 6)
5836* for loops: Forloop. (line 6)
5837* formatted output: Format. (line 6)
5838* frozen file format: Frozen file format. (line 6)
5839* frozen files for fast loading: Using frozen files. (line 6)
5840* GNU extensions <1>: Indir. (line 6)
5841* GNU extensions <2>: Patsubst. (line 6)
5842* GNU extensions <3>: Extensions. (line 6)
5843* GNU extensions <4>: Regexp. (line 6)
5844* GNU extensions <5>: Using frozen files. (line 6)
5845* GNU extensions <6>: Format. (line 6)
5846* GNU extensions <7>: Undivert. (line 81)
5847* GNU extensions <8>: Divert. (line 54)
5848* GNU extensions <9>: Search Path. (line 6)
5849* GNU extensions <10>: Define. (line 41)
5850* GNU extensions <11>: Arguments. (line 32)
5851* GNU extensions <12>: Esyscmd. (line 6)
5852* GNU extensions <13>: Debug Output. (line 6)
5853* GNU extensions <14>: Mkstemp. (line 52)
5854* GNU extensions <15>: Builtin. (line 6)
5855* GNU extensions: Debug Levels. (line 59)
5856* included files, search path for: Search Path. (line 6)
5857* inclusion, of files <1>: Undivert. (line 81)
5858* inclusion, of files: File Inclusion. (line 6)
5859* increment operator: Incr. (line 6)
5860* indirect call of builtins: Builtin. (line 6)
5861* indirect call of macros: Indir. (line 6)
5862* initialization, frozen states: Using frozen files. (line 6)
5863* input tokens: Syntax. (line 6)
5864* input, saving: M4wrap. (line 6)
5865* integer arithmetic: Arithmetic. (line 6)
5866* integer expression evaluation: Eval. (line 6)
5867* iterating over lists: Foreach. (line 6)
5868* length of strings: Len. (line 6)
5869* lexical structure of words: Changeword. (line 6)
5870* License: Copying This Manual. (line 6)
5871* local variables: Pushdef. (line 75)
5872* loops: Shift. (line 10)
5873* loops, counting: Forloop. (line 6)
5874* loops, list iteration: Foreach. (line 6)
5875* macro definitions, on the command line: Preprocessor features.
5876 (line 6)
5877* macro expansion, tracing: Trace. (line 6)
5878* macro invocation: Invocation. (line 6)
5879* macros, arguments to <1>: Macro Arguments. (line 6)
5880* macros, arguments to: Arguments. (line 6)
5881* macros, displaying definitions: Dumpdef. (line 6)
5882* macros, expansion of: Macro expansion. (line 6)
5883* macros, how to define new: Definitions. (line 6)
5884* macros, how to delete: Undefine. (line 6)
5885* macros, how to rename: Defn. (line 6)
5886* macros, indirect call of: Indir. (line 6)
5887* macros, quoted arguments to: Quoting Arguments. (line 6)
5888* macros, recursive: Shift. (line 6)
5889* macros, special arguments to: Pseudo Arguments. (line 6)
5890* macros, temporary redefinition of: Pushdef. (line 6)
5891* messages, printing error: Errprint. (line 6)
5892* multibranches: Ifelse. (line 66)
5893* names: Names. (line 6)
5894* options, command line: Invoking m4. (line 10)
5895* output, diverting to files: Divert. (line 6)
5896* output, formatted: Format. (line 6)
5897* output, saving debugging: Debug Output. (line 6)
5898* pattern substitution: Patsubst. (line 6)
5899* platform macro: Platform macros. (line 31)
5900* platform macros: Platform macros. (line 6)
5901* POSIXLY_CORRECT: Invoking m4. (line 10)
5902* printing error messages: Errprint. (line 6)
5903* quote delimiters, changing the: Changequote. (line 6)
5904* quoted macro arguments: Quoting Arguments. (line 6)
5905* quoted string: Quoted strings. (line 6)
5906* recursive macros: Shift. (line 6)
5907* redefinition of macros, temporary: Pushdef. (line 6)
5908* regular expressions <1>: Patsubst. (line 6)
5909* regular expressions: Regexp. (line 6)
5910* reloading a frozen file: Using frozen files. (line 6)
5911* renaming macros: Defn. (line 6)
5912* running shell commands: Shell commands. (line 6)
5913* running UNIX commands: Shell commands. (line 6)
5914* saving debugging output: Debug Output. (line 6)
5915* saving input: M4wrap. (line 6)
5916* search path for included files: Search Path. (line 6)
5917* shell commands, exit status from: Sysval. (line 6)
5918* shell commands, running: Shell commands. (line 6)
5919* special arguments to macros: Pseudo Arguments. (line 6)
5920* status of shell commands: Sysval. (line 6)
5921* status, setting m4 exit: M4exit. (line 6)
5922* strings, length of: Len. (line 6)
5923* substitution by regular expression: Patsubst. (line 6)
5924* substrings, extracting: Substr. (line 6)
5925* temporary file names: Mkstemp. (line 6)
5926* temporary redefinition of macros: Pushdef. (line 6)
5927* tokens: Syntax. (line 6)
5928* tracing macro expansion: Trace. (line 6)
5929* translating characters: Translit. (line 6)
5930* undefining macros: Undefine. (line 6)
5931* UNIX commands, exit status from: Sysval. (line 6)
5932* UNIX commands, running: Shell commands. (line 6)
5933* variables, local: Pushdef. (line 75)
5934* words, lexical structure of: Changeword. (line 6)
5935
5936
5937File: m4.info, Node: Macro index, Prev: Concept index, Up: Indices
5938
5939B.2 Index for all `m4' macros
5940=============================
5941
5942References are exclusively to the places where a builtin is introduced
5943the first time.
5944
5945[index]
5946* Menu:
5947
5948* __file__: Location. (line 10)
5949* __gnu__: Platform macros. (line 12)
5950* __line__: Location. (line 11)
5951* __os2__: Platform macros. (line 13)
5952* __program__: Location. (line 12)
5953* __unix__: Platform macros. (line 15)
5954* __windows__: Platform macros. (line 17)
5955* builtin: Builtin. (line 9)
5956* capitalize: Patsubst. (line 55)
5957* changecom: Changecom. (line 10)
5958* changequote: Changequote. (line 10)
5959* changeword: Changeword. (line 23)
5960* cleardivert: Cleardivert. (line 26)
5961* debugfile: Debug Output. (line 11)
5962* debugmode: Debug Levels. (line 63)
5963* decr: Incr. (line 11)
5964* define: Define. (line 10)
5965* defn: Defn. (line 10)
5966* divert: Divert. (line 9)
5967* divnum: Divnum. (line 9)
5968* dnl: Dnl. (line 9)
5969* downcase: Patsubst. (line 54)
5970* dquote: Shift. (line 53)
5971* dquote_elt: Shift. (line 54)
5972* dumpdef: Dumpdef. (line 10)
5973* errprint: Errprint. (line 9)
5974* esyscmd: Esyscmd. (line 9)
5975* eval: Eval. (line 9)
5976* example: Manual. (line 32)
5977* fatal_error: M4exit. (line 25)
5978* foreach: Foreach. (line 9)
5979* foreachq: Foreach. (line 10)
5980* forloop: Forloop. (line 9)
5981* format: Format. (line 9)
5982* ifdef: Ifdef. (line 10)
5983* ifelse: Ifelse. (line 12)
5984* include: Include. (line 9)
5985* incr: Incr. (line 10)
5986* index: Index macro. (line 9)
5987* indir: Indir. (line 9)
5988* len: Len. (line 9)
5989* m4exit: M4exit. (line 10)
5990* m4wrap: M4wrap. (line 14)
5991* maketemp: Mkstemp. (line 12)
5992* mkstemp: Mkstemp. (line 11)
5993* os2: Platform macros. (line 14)
5994* patsubst: Patsubst. (line 9)
5995* popdef: Pushdef. (line 12)
5996* pushdef: Pushdef. (line 11)
5997* quote: Shift. (line 52)
5998* regexp: Regexp. (line 9)
5999* reverse: Shift. (line 32)
6000* shift: Shift. (line 17)
6001* sinclude: Include. (line 10)
6002* substr: Substr. (line 9)
6003* syscmd: Syscmd. (line 9)
6004* sysval: Sysval. (line 9)
6005* traceoff: Trace. (line 11)
6006* traceon: Trace. (line 10)
6007* translit: Translit. (line 9)
6008* undefine: Undefine. (line 9)
6009* undivert: Undivert. (line 9)
6010* unix: Platform macros. (line 16)
6011* upcase: Patsubst. (line 53)
6012* windows: Platform macros. (line 18)
6013
6014
6015
6016Tag Table:
6017Node: Top840
6018Node: Preliminaries9196
6019Node: Intro9882
6020Node: History11514
6021Node: Bugs14146
6022Node: Manual15408
6023Node: Invoking m418340
6024Node: Operation modes20488
6025Node: Preprocessor features22199
6026Node: Limits control24701
6027Node: Frozen state27637
6028Node: Debugging options28436
6029Node: Command line files30175
6030Node: Syntax31538
6031Node: Names32539
6032Node: Quoted strings33001
6033Node: Comments33650
6034Node: Other tokens34517
6035Node: Input processing35095
6036Node: Macros36982
6037Node: Invocation37476
6038Node: Inhibiting Invocation38277
6039Node: Macro Arguments41848
6040Node: Quoting Arguments44539
6041Node: Macro expansion45861
6042Node: Definitions46529
6043Node: Define47314
6044Node: Arguments49520
6045Node: Pseudo Arguments51268
6046Node: Undefine54483
6047Node: Defn55613
6048Node: Pushdef58346
6049Node: Indir60680
6050Node: Builtin62782
6051Node: Conditionals64544
6052Node: Ifdef65363
6053Node: Ifelse66226
6054Node: Shift69445
6055Node: Forloop73244
6056Node: Foreach75813
6057Node: Debugging80792
6058Node: Dumpdef81377
6059Node: Trace82761
6060Node: Debug Levels85449
6061Node: Debug Output88255
6062Node: Input Control89526
6063Node: Dnl90063
6064Node: Changequote91964
6065Node: Changecom96760
6066Node: Changeword100276
6067Node: M4wrap105717
6068Node: File Inclusion107978
6069Node: Include108295
6070Node: Search Path110895
6071Node: Diversions111812
6072Node: Divert113603
6073Node: Undivert116129
6074Node: Divnum119011
6075Node: Cleardivert119475
6076Node: Text handling120680
6077Node: Len121403
6078Node: Index macro121788
6079Node: Regexp122559
6080Node: Substr124808
6081Node: Translit125859
6082Node: Patsubst128581
6083Node: Format132121
6084Node: Arithmetic134188
6085Node: Incr134637
6086Node: Eval135412
6087Node: Shell commands139821
6088Node: Platform macros140743
6089Node: Syscmd142487
6090Node: Esyscmd144292
6091Node: Sysval145340
6092Node: Mkstemp146895
6093Node: Miscellaneous150197
6094Node: Errprint150630
6095Node: Location151849
6096Node: M4exit154651
6097Node: Frozen files156738
6098Node: Using frozen files157519
6099Node: Frozen file format160759
6100Node: Compatibility163822
6101Node: Extensions164437
6102Node: Incompatibilities168204
6103Node: Other Incompatibilities176116
6104Node: Answers178044
6105Node: Improved exch178661
6106Node: Improved forloop179199
6107Node: Improved foreach181035
6108Node: Improved cleardivert187974
6109Node: Improved fatal_error188957
6110Node: Copying This Manual190019
6111Node: GNU Free Documentation License190275
6112Node: Indices212669
6113Node: Concept index212949
6114Node: Macro index224692
6115
6116End Tag Table
Note: See TracBrowser for help on using the repository browser.