This is Info file m4.info, produced by Makeinfo-1.55 from the input file m4.texinfo. START-INFO-DIR-ENTRY * m4: (m4). A powerful macro processor. END-INFO-DIR-ENTRY This file documents the GNU `m4' utility. Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation. File: m4.info, Node: Top, Next: Preliminaries, Prev: (dir), Up: (dir) GNU `m4' ******** GNU `m4' is an implementation of the traditional UNIX macro processor. It is mostly SVR4 compatible, although it has some extensions (for example, handling more than 9 positional parameters to macros). `m4' also has builtin functions for including files, running shell commands, doing arithmetic, etc. Autoconf needs GNU `m4' for generating `configure' scripts, but not for running them. GNU `m4' was originally written by Ren'e Seindal, with subsequent changes by Franc,ois Pinard and other volunteers on the Internet. All names and email addresses can be found in the file `THANKS' from the GNU `m4' distribution. This is release 1.4. It is now to be considered stable, future releases are only meant to fix bugs, increase speed, or improve documentation. However... An experimental feature, which would improve `m4' usefulness, allows for changing the syntax for what is a "word" in `m4'. You should use: ./configure --enable-changeword if you want this feature compiled in. The current implementation slows down `m4' considerably and is hardly acceptable. So, it might go away, do not count on it yet. * Menu: * Preliminaries:: Introduction and preliminaries * Syntax:: Lexical and syntactic conventions * Macros:: How to invoke macros * Definitions:: How to define new macros * Conditionals:: Conditionals and loops * Debugging:: How to debug macros and input * Input Control:: Input control * File Inclusion:: File inclusion * Diversions:: Diverting and undiverting output * Text handling:: Macros for text handling * Arithmetic:: Macros for doing arithmetic * UNIX commands:: Macros for running UNIX commands * Miscellaneous:: Miscellaneous builtin macros * Frozen files:: Fast loading of frozen states * Compatibility:: Compatibility with other versions of m4 * Concept index:: Index for many concepts * Macro index:: Index for all m4 macros -- The Detailed Node Listing -- Introduction and preliminaries * Intro:: Introduction to `m4' * History:: Historical references * Invoking m4:: Invoking `m4' * Bugs:: Problems and bugs * Manual:: Using this manual Lexical and syntactic conventions * Names:: Macro names * Quoted strings:: Quoting input to m4 * Other tokens:: Other kinds of input tokens * Comments:: Comments in m4 input How to invoke macros * Invocation:: Macro invocation * Inhibiting Invocation:: Preventing macro invocation * Macro Arguments:: Macro arguments * Quoting Arguments:: On Quoting Arguments to macros * Macro expansion:: Expanding macros How to define new macros * Define:: Defining a new macro * Arguments:: Arguments to macros * Pseudo Arguments:: Pseudo arguments to macros * Undefine:: Deleting a macro * Defn:: Renaming macros * Pushdef:: Temporarily redefining macros * Indir:: Indirect call of macros * Builtin:: Indirect call of builtins Conditionals, loops and recursion * Ifdef:: Testing if a macro is defined * Ifelse:: If-else construct, or multibranch * Loops:: Loops and recursion in m4 How to debug macros and input * Dumpdef:: Displaying macro definitions * Trace:: Tracing macro calls * Debug Levels:: Controlling debugging output * Debug Output:: Saving debugging output Input control * Dnl:: Deleting whitespace in input * Changequote:: Changing the quote characters * Changecom:: Changing the comment delimiters * Changeword:: Changing the lexical structure of words * M4wrap:: Saving input until end of input File inclusion * Include:: Including named files * Search Path:: Searching for include files Diverting and undiverting output * Divert:: Diverting output * Undivert:: Undiverting output * Divnum:: Diversion numbers * Cleardiv:: Discarding diverted text Macros for text handling * Len:: Calculating length of strings * Index:: Searching for substrings * Regexp:: Searching for regular expressions * Substr:: Extracting substrings * Translit:: Translating characters * Patsubst:: Substituting text by regular expression * Format:: Formatting strings (printf-like) Macros for doing arithmetic * Incr:: Decrement and increment operators * Eval:: Evaluating integer expressions Running UNIX commands * Syscmd:: Executing simple commands * Esyscmd:: Reading the output of commands * Sysval:: Exit codes * Maketemp:: Making names for temporary files Miscellaneous builtin macros * Errprint:: Printing error messages * M4exit:: Exiting from m4 Compatibility with other versions of `m4' * Extensions:: Extensions in GNU m4 * Incompatibilities:: Facilities in System V m4 not in GNU m4 * Other Incompat:: Other incompatibilities File: m4.info, Node: Preliminaries, Next: Syntax, Prev: Top, Up: Top Introduction and preliminaries ****************************** This first chapter explains what is GNU `m4', where `m4' comes from, how to read and use this documentation, how to call the `m4' program and how to report bugs about it. It concludes by giving tips for reading the remainder of the manual. The following chapters then detail all the features of the `m4' language. * Menu: * Intro:: Introduction to `m4' * History:: Historical references * Invoking m4:: Invoking `m4' * Bugs:: Problems and bugs * Manual:: Using this manual File: m4.info, Node: Intro, Next: History, Prev: Preliminaries, Up: Preliminaries Introduction to `m4' ==================== `m4' is a macro processor, in the sense that it copies its input to the output, expanding macros as it goes. Macros are either builtin or user-defined, and can take any number of arguments. Besides just doing macro expansion, `m4' has builtin functions for including named files, running UNIX commands, doing integer arithmetic, manipulating text in various ways, recursion, etc... `m4' can be used either as a front-end to a compiler, or as a macro processor in its own right. The `m4' macro processor is widely available on all UNIXes. Usually, only a small percentage of users are aware of its existence. However, those who do often become commited users. The growing popularity of GNU Autoconf, which prerequires GNU `m4' for *generating* the `configure' scripts, is an incentive for many to install it, while these people will not themselves program in `m4'. GNU `m4' is mostly compatible with the System V, Release 3 version, except for some minor differences. *Note Compatibility:: for more details. Some people found `m4' to be fairly addictive. They first use `m4' for simple problems, then take bigger and bigger challenges, learning how to write complex `m4' sets of macros along the way. Once really addicted, users pursue writing of sophisticated `m4' applications even to solve simple problems, devoting more time debugging their `m4' scripts than doing real work. Beware that `m4' may be dangerous for the health of compulsive programmers. File: m4.info, Node: History, Next: Invoking m4, Prev: Intro, Up: Preliminaries Historical references ===================== The historical notes included here are fairly incomplete, and not authoritative at all. Please knowledgeable users help us to more properly write this section. `GPM' has been an important ancestor of `m4'. See C. Stratchey: "A General Purpose Macro generator", Computer Journal 8,3 (1965), pp. 225 ff. `GPM' is also succintly described into David Gries classic "Compiler Construction for Digital Computers". While `GPM' was *pure*, `m4' was meant to deal more with the true intricacies of real life: macros could be recognized with being pre-announced, skipping whitespace or end-of-lines was made easier, more constructs were builtin instead of derived, etc. Originally, `m4' was the engine for Rational FORTRAN preprocessor, that is, the `ratfor' equivalent of `cpp'. File: m4.info, Node: Invoking m4, Next: Bugs, Prev: History, Up: Preliminaries Invoking `m4' ============= The format of the `m4' command is: `m4' [OPTION...] [MACRO-DEFINITIONS...] [INPUT-FILE...] All options begin with `-', or if long option names are used, with a `--'. A long option name need not be written completely, and unambigous prefix is sufficient. `m4' understands the following options: `--version' Print the version number of the program on standard output, then immediately exit `m4' without reading any INPUT-FILES. `--help' Print an help summary on standard output, then immediately exit `m4' without reading any INPUT-FILES. `--traditional' Suppress all the extensions made in this implementation, compared to the System V version. *Note Compatibility::, for a list of these. `--fatal-warnings' Stop execution and exit `m4' once the first warning has been issued, considering all of them to be fatal. `-dFLAGS' `--debug=FLAGS' Set the debug-level according to the flags FLAGS. The debug-level controls the format and amount of information presented by the debugging functions. *Note Debug Levels:: for more details on the format and meaning of FLAGS. `-lNUM' `--arglength=NUM' Restrict the size of the output generated by macro tracing. *Note Debug Levels:: for more details. `-oFILE' `--error-output=FILE' Redirect debug and trace output to the named file. Error messages are still printed on the standard error output. *Note Debug Output:: for more details. `-IDIR' `--include=DIR' Make `m4' search DIR for included files that are not found in the current working directory. *Note Search Path:: for more details. `--interactive' Makes this invocation of `m4' interactive. This means that all output will be unbuffered, and interrupts will be ignored. `--synclines' Generate synchronisation lines, for use by the C preprocessor or other similar tools. This is useful, for example, when `m4' is used as a front end to a compiler. Source file name and line number information is conveyed by directives of the form `#line LINENUM "FILENAME"', which are inserted as needed into the middle of the input. Such directives mean that the following line originated or was expanded from the contents of input file FILENAME at line LINENUM. The `"FILENAME"' part is often omitted when the file name did not change from the previous directive. Synchronisation directives are always given on complete lines per themselves. When a synchronisation discrepancy occurs in the middle of an output line, the associated synchronisation directive is delayed until the beginning of the next generated line. `--prefix-builtins' Internally modify *all* builtin macro names so they all start with the prefix `m4_'. For example, using this option, one should write `m4_define' instead of `define', and `m4___file__' instead of `__file__'. `-WREGEXP' `--word-regexp=REGEXP' Use an alternative syntax for macro names. This experimental option might not be present on all GNU `m4' implementations. (*note Changeword::.). `-HN' `--hashsize=N' Make the internal hash table for symbol lookup be N entries big. The number should be prime. The default is 509 entries. It should not be necessary to increase this value, unless you define an excessive number of macros. `-LN' `--nesting-limit=N' Artificially limit the nesting of macro calls to N levels, stopping program execution if this limit is ever exceeded. When not specified, nesting is limited to 250 levels. The precise effect of this option might be more correctly associated with textual nesting than dynamic recursion. It has been useful when some complex `m4' input was generated by mechanical means. Most users would never need this option. If shown to be obtrusive, this option (which is still experimental) might well disappear. This option does *not* have the ability to break endless rescanning loops, while these do not necessarily consume much memory or stack space. Through clever usage of rescanning loops, one can request complex, time-consuming computations to `m4' with useful results. Putting limitations in this area would break `m4' power. There are many pathological cases: `define(`a', `a')a' is only the simplest example (but *note Compatibility::.). Expecting GNU `m4' to detect these would be a little like expecting a compiler system to detect and diagnose endless loops: it is a quite *hard* problem in general, if not undecidable! `--quiet' `--silent' Suppress warnings about missing or superflous arguments in macro calls. These options are present for compatibility with System V `m4', but do nothing in this implementation. `-NN' `--diversions=N' These options are present only for compatibility with previous versions of GNU `m4', and were controlling the number of possible diversions which could be used at the same time. They do nothing, because there is no fixed limit anymore. Macro definitions and deletions can be made on the command line, by using the `-D' and `-U' options. They have the following format: `-DNAME' `-DNAME=VALUE' `--define=NAME' `--define=NAME=VALUE' This enters NAME into the symbol table, before any input files are read. If `=VALUE' is missing, the value is taken to be the empty string. The VALUE can be any string, and the macro can be defined to take arguments, just as if it was defined from within the input. `-UNAME' `--undefine=NAME' This deletes any predefined meaning NAME might have. Obviously, only predefined macros can be deleted in this way. `-tNAME' `--trace=NAME' This enters NAME into the symbol table, as undefined but traced. The macro will consequently be traced from the point it is defined. `-FFILE' `--freeze-state FILE' Once execution is finished, write out the frozen state on the specified FILE (*note Frozen files::.). `-RFILE' `--reload-state FILE' Before execution starts, recover the internal state from the specified frozen FILE (*note Frozen files::.). The remaining arguments on the command line are taken to be input file names. If no names are present, the standard input is read. A file name of `-' is taken to mean the standard input. The input files are read in the sequence given. The standard input can only be read once, so the filename `-' should only appear once on the command line. File: m4.info, Node: Bugs, Next: Manual, Prev: Invoking m4, Up: Preliminaries Problems and bugs ================= If you have problems with GNU `m4' or think you've found a bug, please report it. Before reporting a bug, make sure you've actually found a real bug. Carefully reread the documentation and see if it really says you can do what you're trying to do. If it's not clear whether you should be able to do something or not, report that too; it's a bug in the documentation! Before reporting a bug or trying to fix it yourself, try to isolate it to the smallest possible input file that reproduces the problem. Then send us the input file and the exact results `m4' gave you. Also say what you expected to occur; this will help us decide whether the problem was really in the documentation. Once you've got a precise problem, send e-mail to (Internet) `bug-gnu-utils@prep.ai.mit.edu' or (UUCP) `mit-eddie!prep.ai.mit.edu!bug-gnu-utils'. Please include the version number of `m4' you are using. You can get this information with the command `m4 --version'. Non-bug suggestions are always welcome as well. If you have questions about things that are unclear in the documentation or are just obscure features, please report them too. File: m4.info, Node: Manual, Prev: Bugs, Up: Preliminaries Using this manual ================= This manual contains a number of examples of `m4' input and output, and a simple notation is used to distinguish input, output and error messages from `m4'. Examples are set out from the normal text, and shown in a fixed width font, like this This is an example of an example! To distinguish input from output, all output from `m4' is prefixed by the string `=>', and all error messages by the string `error-->'. Example of input line =>Output line from m4 error-->and an error message As each of the predefined macros in `m4' is described, a prototype call of the macro will be shown, giving descriptive names to the arguments, e.g., regexp(STRING, REGEXP, opt REPLACEMENT) All macro arguments in `m4' are strings, but some are given special interpretation, e.g., as numbers, filenames, regular expressions, etc. The `opt' before the third argument shows that this argument is optional--if it is left out, it is taken to be the empty string. An ellipsis (`...') last in the argument list indicates that any number of arguments may follow. This document consistently writes and uses "builtin", without an hyphen, as if it were an English word. This is how the `builtin' primitive is spelled within `m4'. File: m4.info, Node: Syntax, Next: Macros, Prev: Preliminaries, Up: Top Lexical and syntactic conventions ********************************* As `m4' reads its input, it separates it into "tokens". A token is either a name, a quoted string, or any single character, that is not a part of either a name or a string. Input to `m4' can also contain comments. * Menu: * Names:: Macro names * Quoted strings:: Quoting input to m4 * Other tokens:: Other kinds of input tokens * Comments:: Comments in m4 input File: m4.info, Node: Names, Next: Quoted strings, Prev: Syntax, Up: Syntax Names ===== A name is any sequence of letters, digits, and the character `_' (underscore), where the first character is not a digit. If a name has a macro definition, it will be subject to macro expansion (*note Macros::.). Examples of legal names are: `foo', `_tmp', and `name01'. File: m4.info, Node: Quoted strings, Next: Other tokens, Prev: Names, Up: Syntax Quoted strings ============== A quoted string is a sequence of characters surrounded by the quotes ``' and `'', where the number of start and end quotes within the string balances. The value of a string token is the text, with one level of quotes stripped off. Thus `' is the empty string, and ``quoted'' is the string `quoted' The quote characters can be changed at any time, using the builtin macro `changequote'. *Note Changequote:: for more information. File: m4.info, Node: Other tokens, Next: Comments, Prev: Quoted strings, Up: Syntax Other tokens ============ Any character, that is neither a part of a name, nor of a quoted string, is a token by itself. File: m4.info, Node: Comments, Prev: Other tokens, Up: Syntax Comments ======== Comments in `m4' are normally delimited by the characters `#' and newline. All characters between the comment delimiters are ignored, but the entire comment (including the delimiters) is passed through to the output--comments are *not* discarded by `m4'. Comments cannot be nested, so the first newline after a `#' ends the comment. The commenting effect of the begin comment character can be inhibited by quoting it. The comment delimiters can be changed to any string at any time, using the builtin macro `changecom'. *Note Changecom:: for more information. File: m4.info, Node: Macros, Next: Definitions, Prev: Syntax, Up: Top How to invoke macros ******************** This chapter covers macro invocation, macro arguments and how macro expansion is treated. * Menu: * Invocation:: Macro invocation * Inhibiting Invocation:: Preventing macro invocation * Macro Arguments:: Macro arguments * Quoting Arguments:: On Quoting Arguments to macros * Macro expansion:: Expanding macros File: m4.info, Node: Invocation, Next: Inhibiting Invocation, Prev: Macros, Up: Macros Macro invocation ================ Macro invocations has one of the forms name which is a macro invocation without any arguments, or name(arg1, arg2, ..., argN) which is a macro invocation with N arguments. Macros can have any number of arguments. All arguments are strings, but different macros might interpret the arguments in different ways. The opening parenthesis *must* follow the NAME directly, with no spaces in between. If it does not, the macro is called with no arguments at all. For a macro call to have no arguments, the parentheses *must* be left out. The macro call name() is a macro call with one argument, which is the empty string, not a call with no arguments. File: m4.info, Node: Inhibiting Invocation, Next: Macro Arguments, Prev: Invocation, Up: Macros Preventing macro invocation =========================== An innovation of the `m4' language, compared to some of its predecessors (like Stratchey's `GPM', for example), is the ability to recognize macro calls without resorting to any special, prefixed invocation character. While generally useful, this feature might sometimes be the source of spurious, unwanted macro calls. So, GNU `m4' offers several mechanisms or techniques for inhibiting the recognition of names as macro calls. First of all, many builtin macros cannot meaningfully be called without arguments. For any of these macros, whenever an opening parenthesis does not immediately follow their name, the builtin macro call is not triggered. This solves the most usual cases, like for `include' or `eval'. Later in this document, the sentence "This macro is recognized only when given arguments" refers to this specific provision. There is also a command call option (`--prefix-builtins', or `-P') which requires all builtin macro names to be prefixed by `m4_' for them to be recognized. The option has no effect whatsoever on user defined macros. For example, with this option, one has to write `m4_dnl' and even `m4_m4exit'. If your version of GNU `m4' has the `changeword' feature compiled in, there it offers far more flexibility in specifying the syntax of macro names, both builtin or user-defined. *Note Changeword:: for more information on this experimental feature. Of course, the simplest way to prevent a name to be interpreted as a call to an existing macro is to quote it. The remainder of this section studies a little more deeply how quoting affects macro invocation, and how quoting can be used to inhibit macro invocation. Even if quoting is usually done over the whole macro name, it can also be done over only a few characters of this name. It is also possible to quote the empty string, but this works only *inside* the name. For example: `divert' `d'ivert di`ver't div`'ert all yield the string `divert'. While in both: `'divert divert`' the `divert' builtin macro will be called. The output of macro evaluations is always rescanned. The following example would yield the string `de', exactly as if `m4' has been given `substr(abcde, 3, 2)' as input: define(`x', `substr(ab') define(`y', `cde, 3, 2)') x`'y Unquoted strings on either side of a quoted string are subject to being recognized as macro names. In the following example, quoting the empty string allows for the `dnl' macro to be recognized as such: define(`macro', `di$1') macro(v)`'dnl Without the quotes, this would rather yield the string `divdnl' followed by an end of line. Quoting may prevent recognizing as a macro name the concatenation of a macro expansion with the surrounding characters. In this example: define(`macro', `di$1') macro(v)`ert' the input will produce the string `divert'. If the quote was removed, the `divert' builtin would be called instead. File: m4.info, Node: Macro Arguments, Next: Quoting Arguments, Prev: Inhibiting Invocation, Up: Macros Macro arguments =============== When a name is seen, and it has a macro definition, it will be expanded as a macro. If the name is followed by an opening parenthesis, the arguments will be collected before the macro is called. If too few arguments are supplied, the missing arguments are taken to be the empty string. If there are too many arguments, the excess arguments are ignored. Normally `m4' will issue warnings if a builtin macro is called with an inappropriate number of arguments, but it can be suppressed with the `-Q' command line option. For user defined macros, there is no check of the number of arguments given. Macros are expanded normally during argument collection, and whatever commas, quotes and parentheses that might show up in the resulting expanded text will serve to define the arguments as well. Thus, if FOO expands to `, b, c', the macro call bar(a foo, d) is a macro call with four arguments, which are `a ', `b', `c' and `d'. To understand why the first argument contains whitespace, remember that leading unquoted whitespace is never part of an argument, but trailing whitespace always is. File: m4.info, Node: Quoting Arguments, Next: Macro expansion, Prev: Macro Arguments, Up: Macros Quoting macro arguments ======================= Each argument has leading unquoted whitespace removed. Within each argument, all unquoted parentheses must match. For example, if FOO is a macro, foo(() (`(') `(') is a macro call, with one argument, whose value is `() (() ('. It is common practice to quote all arguments to macros, unless you are sure you want the arguments expanded. Thus, in the above example with the parentheses, the `right' way to do it is like this: foo(`() (() (') It is, however, in certain cases necessary to leave out quotes for some arguments, and there is nothing wrong in doing it. It just makes life a bit harder, if you are not careful. File: m4.info, Node: Macro expansion, Prev: Quoting Arguments, Up: Macros Macro expansion =============== When the arguments, if any, to a macro call have been collected, the macro is expanded, and the expansion text is pushed back onto the input (unquoted), and reread. The expansion text from one macro call might therefore result in more macros being called, if the calls are included, completely or partially, in the first macro calls' expansion. Taking a very simple example, if FOO expands to `bar', and BAR expands to `Hello world', the input foo will expand first to `bar', and when this is reread and expanded, into `Hello world'. File: m4.info, Node: Definitions, Next: Conditionals, Prev: Macros, Up: Top How to define new macros ************************ Macros can be defined, redefined and deleted in several different ways. Also, it is possible to redefine a macro, without losing a previous value, which can be brought back at a later time. * Menu: * Define:: Defining a new macro * Arguments:: Arguments to macros * Pseudo Arguments:: Pseudo arguments to macros * Undefine:: Deleting a macro * Defn:: Renaming macros * Pushdef:: Temporarily redefining macros * Indir:: Indirect call of macros * Builtin:: Indirect call of builtins File: m4.info, Node: Define, Next: Arguments, Prev: Definitions, Up: Definitions Defining a macro ================ The normal way to define or redefine macros is to use the builtin `define': define(NAME [, EXPANSION]) which defines NAME to expand to EXPANSION. If EXPANSION is not given, it is taken to be empty. The expansion of `define' is void. The following example defines the macro FOO to expand to the text `Hello World.'. define(`foo', `Hello world.') => foo =>Hello world. The empty line in the output is there because the newline is not a part of the macro definition, and it is consequently copied to the output. This can be avoided by use of the macro `dnl'. *Note Dnl::, for details. The macro `define' is recognized only with parameters. File: m4.info, Node: Arguments, Next: Pseudo Arguments, Prev: Define, Up: Definitions Arguments to macros =================== Macros can have arguments. The Nth argument is denoted by `$n' in the expansion text, and is replaced by the Nth actual argument, when the macro is expanded. Here is a example of a macro with two arguments. It simply exchanges the order of the two arguments. define(`exch', `$2, $1') => exch(arg1, arg2) =>arg2, arg1 This can be used, for example, if you like the arguments to `define' to be reversed. define(`exch', `$2, $1') => define(exch(``expansion text'', ``macro'')) => macro =>expansion text *Note Quoting Arguments::, for an explanation of the double quotes. GNU `m4' allows the number following the `$' to consist of one or more digits, allowing macros to have any number of arguments. This is not so in UNIX implementations of `m4', which only recognize one digit. As a special case, the zero'th argument, `$0', is always the name of the macro being expanded. define(`test', ``Macro name: $0'') => test =>Macro name: test If you want quoted text to appear as part of the expansion text, remember that quotes can be nested in quoted strings. Thus, in define(`foo', `This is macro `foo'.') => foo =>This is macro foo. The `foo' in the expansion text is *not* expanded, since it is a quoted string, and not a name. File: m4.info, Node: Pseudo Arguments, Next: Undefine, Prev: Arguments, Up: Definitions Special arguments to macros =========================== There is a special notation for the number of actual arguments supplied, and for all the actual arguments. The number of actual arguments in a macro call is denoted by `$#' in the expansion text. Thus, a macro to display the number of arguments given can be define(`nargs', `$#') => nargs =>0 nargs() =>1 nargs(arg1, arg2, arg3) =>3 The notation `$*' can be used in the expansion text to denote all the actual arguments, unquoted, with commas in between. For example define(`echo', `$*') => echo(arg1, arg2, arg3 , arg4) =>arg1,arg2,arg3 ,arg4 Often each argument should be quoted, and the notation `$@' handles that. It is just like `$*', except that it quotes each argument. A simple example of that is: define(`echo', `$@') => echo(arg1, arg2, arg3 , arg4) =>arg1,arg2,arg3 ,arg4 Where did the quotes go? Of course, they were eaten, when the expanded text were reread by `m4'. To show the difference, try define(`echo1', `$*') => define(`echo2', `$@') => define(`foo', `This is macro `foo'.') => echo1(foo) =>This is macro This is macro foo.. echo2(foo) =>This is macro foo. *Note Trace::, if you do not understand this. A `$' sign in the expansion text, that is not followed by anything `m4' understands, is simply copied to the macro expansion, as any other text is. define(`foo', `$$$ hello $$$') => foo =>$$$ hello $$$ If you want a macro to expand to something like `$12', put a pair of quotes after the `$'. This will prevent `m4' from interpreting the `$' sign as a reference to an argument. File: m4.info, Node: Undefine, Next: Defn, Prev: Pseudo Arguments, Up: Definitions Deleting a macro ================ A macro definition can be removed with `undefine': undefine(NAME) which removes the macro NAME. The macro name must necessarily be quoted, since it will be expanded otherwise. The expansion of `undefine' is void. foo =>foo define(`foo', `expansion text') => foo =>expansion text undefine(`foo') => foo =>foo It is not an error for NAME to have no macro definition. In that case, `undefine' does nothing. The macro `undefine' is recognized only with parameters. File: m4.info, Node: Defn, Next: Pushdef, Prev: Undefine, Up: Definitions Renaming macros =============== It is possible to rename an already defined macro. To do this, you need the builtin `defn': defn(NAME) which expands to the *quoted definition* of NAME. If the argument is not a defined macro, the expansion is void. If NAME is a user-defined macro, the quoted definition is simply the quoted expansion text. If, instead, NAME is a builtin, the expansion is a special token, which points to the builtin's internal definition. This token is only meaningful as the second argument to `define' (and `pushdef'), and is ignored in any other context. Its normal use is best understood through an example, which shows how to rename `undefine' to `zap': define(`zap', defn(`undefine')) => zap(`undefine') => undefine(`zap') =>undefine(zap) In this way, `defn' can be used to copy macro definitions, and also definitions of builtin macros. Even if the original macro is removed, the other name can still be used to access the definition. The macro `defn' is recognized only with parameters. File: m4.info, Node: Pushdef, Next: Indir, Prev: Defn, Up: Definitions Temporarily redefining macros ============================= It is possible to redefine a macro temporarily, reverting to the previous definition at a later time. This is done with the builtins `pushdef' and `popdef': pushdef(NAME [, EXPANSION]) popdef(NAME) which are quite analogous to `define' and `undefine'. These macros work in a stack-like fashion. A macro is temporarily redefined with `pushdef', which replaces an existing definition of NAME, while saving the previous definition, before the new one is installed. If there is no previous definition, `pushdef' behaves exactly like `define'. If a macro has several definitions (of which only one is accessible), the topmost definition can be removed with `popdef'. If there is no previous definition, `popdef' behaves like `undefine'. define(`foo', `Expansion one.') => foo =>Expansion one. pushdef(`foo', `Expansion two.') => foo =>Expansion two. popdef(`foo') => foo =>Expansion one. popdef(`foo') => foo =>foo If a macro with several definitions is redefined with `define', the topmost definition is *replaced* with the new definition. If it is removed with `undefine', *all* the definitions are removed, and not only the topmost one. define(`foo', `Expansion one.') => foo =>Expansion one. pushdef(`foo', `Expansion two.') => foo =>Expansion two. define(`foo', `Second expansion two.') => foo =>Second expansion two. undefine(`foo') => foo =>foo It is possible to temporarily redefine a builtin with `pushdef' and `defn'. The macros `pushdef' and `popdef' are recognized only with parameters. File: m4.info, Node: Indir, Next: Builtin, Prev: Pushdef, Up: Definitions Indirect call of macros ======================= Any macro can be called indirectly with `indir': indir(NAME, ...) which results in a call to the macro NAME, which is passed the rest of the arguments. This can be used to call macros with "illegal" names (`define' allows such names to be defined): define(`$$internal$macro', `Internal macro (name `$0')') => $$internal$macro =>$$internal$macro indir(`$$internal$macro') =>Internal macro (name $$internal$macro) The point is, here, that larger macro packages can have private macros defined, that will not be called by accident. They can *only* be called through the builtin `indir'. File: m4.info, Node: Builtin, Prev: Indir, Up: Definitions Indirect call of builtins ========================= Builtin macros can be called indirectly with `builtin': builtin(NAME, ...) which results in a call to the builtin NAME, which is passed the rest of the arguments. This can be used, if NAME has been given another definition that has covered the original. The macro `builtin' is recognized only with parameters. File: m4.info, Node: Conditionals, Next: Debugging, Prev: Definitions, Up: Top Conditionals, loops and recursion ********************************* Macros, expanding to plain text, perhaps with arguments, are not quite enough. We would like to have macros expand to different things, based on decisions taken at run-time. E.g., we need some kind of conditionals. Also, we would like to have some kind of loop construct, so we could do something a number of times, or while some condition is true. * Menu: * Ifdef:: Testing if a macro is defined * Ifelse:: If-else construct, or multibranch * Loops:: Loops and recursion in m4 File: m4.info, Node: Ifdef, Next: Ifelse, Prev: Conditionals, Up: Conditionals Testing macro definitions ========================= There are two different builtin conditionals in `m4'. The first is `ifdef': ifdef(NAME, STRING-1, opt STRING-2) which makes it possible to test whether a macro is defined or not. If NAME is defined as a macro, `ifdef' expands to STRING-1, otherwise to STRING-2. If STRING-2 is omitted, it is taken to be the empty string (according to the normal rules). ifdef(`foo', ``foo' is defined', ``foo' is not defined') =>foo is not defined define(`foo', `') => ifdef(`foo', ``foo' is defined', ``foo' is not defined') =>foo is defined The macro `ifdef' is recognized only with parameters. File: m4.info, Node: Ifelse, Next: Loops, Prev: Ifdef, Up: Conditionals Comparing strings ================= The other conditional, `ifelse', is much more powerful. It can be used as a way to introduce a long comment, as an if-else construct, or as a multibranch, depending on the number of arguments supplied: ifelse(COMMENT) ifelse(STRING-1, STRING-2, EQUAL, opt NOT-EQUAL) ifelse(STRING-1, STRING-2, EQUAL, ...) Used with only one argument, the `ifelse' simply discards it and produces no output. This is a common `m4' idiom for introducing a block comment, as an alternative to repeatedly using `dnl'. This special usage is recognized by GNU `m4', so that in this case, the warning about missing arguments is never triggered. If called with three or four arguments, `ifelse' expands into EQUAL, if STRING-1 and STRING-2 are equal (character for character), otherwise it expands to NOT-EQUAL. ifelse(foo, bar, `true') => ifelse(foo, foo, `true') =>true ifelse(foo, bar, `true', `false') =>false ifelse(foo, foo, `true', `false') =>true However, `ifelse' can take more than four arguments. If given more than four arguments, `ifelse' works like a `case' or `switch' statement in traditional programming languages. If STRING-1 and STRING-2 are equal, `ifelse' expands into EQUAL, otherwise the procedure is repeated with the first three arguments discarded. This calls for an example: ifelse(foo, bar, `third', gnu, gnats, `sixth', `seventh') =>seventh Naturally, the normal case will be slightly more advanced than these examples. A common use of `ifelse' is in macros implementing loops of various kinds. The macro `ifelse' is recognized only with parameters. File: m4.info, Node: Loops, Prev: Ifelse, Up: Conditionals Loops and recursion =================== There is no direct support for loops in `m4', but macros can be recursive. There is no limit on the number of recursion levels, other than those enforced by your hardware and operating system. Loops can be programmed using recursion and the conditionals described previously. There is a builtin macro, `shift', which can, among other things, be used for iterating through the actual arguments to a macro: shift(...) It takes any number of arguments, and expands to all but the first argument, separated by commas, with each argument quoted. shift(bar) => shift(foo, bar, baz) =>bar,baz An example of the use of `shift' is this macro, which reverses the order of its arguments: define(`reverse', `ifelse($#, 0, , $#, 1, ``$1'', `reverse(shift($@)), `$1'')') => reverse => reverse(foo) =>foo reverse(foo, bar, gnats, and gnus) =>and gnus, gnats, bar, foo While not a very interesting macro, it does show how simple loops can be made with `shift', `ifelse' and recursion. Here is an example of a loop macro that implements a simple forloop. It can, for example, be used for simple counting: forloop(`i', 1, 8, `i ') =>1 2 3 4 5 6 7 8 The arguments are a name for the iteration variable, the starting value, the final value, and the text to be expanded for each iteration. With this macro, the macro `i' is defined only within the loop. After the loop, it retains whatever value it might have had before. For-loops can be nested, like forloop(`i', 1, 4, `forloop(`j', 1, 8, `(i, j) ') ') =>(1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) =>(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8) =>(3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8) =>(4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8) => The implementation of the `forloop' macro is fairly straightforward. The `forloop' macro itself is simply a wrapper, which saves the previous definition of the first argument, calls the internal macro `_forloop', and re-establishes the saved definition of the first argument. The macro `_forloop' expands the fourth argument once, and tests to see if it is finished. If it has not finished, it increments the iteration variable (using the predefined macro `incr', *note Incr::.), and recurses. Here is the actual implementation of `forloop': define(`forloop', `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')') define(`_forloop', `$4`'ifelse($1, `$3', , `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')') Notice the careful use of quotes. Only three macro arguments are unquoted, each for its own reason. Try to find out *why* these three arguments are left unquoted, and see what happens if they are quoted. Now, even though these two macros are useful, they are still not robust enough for general use. They lack even basic error handling of cases like start value less than final value, and the first argument not being a name. Correcting these errors are left as an exercise to the reader. File: m4.info, Node: Debugging, Next: Input Control, Prev: Conditionals, Up: Top How to debug macros and input ***************************** When writing macros for `m4', most of the time they woould not work as intended (as is the case with most programming languages). There is a little support for macro debugging in `m4'. * Menu: * Dumpdef:: Displaying macro definitions * Trace:: Tracing macro calls * Debug Levels:: Controlling debugging output * Debug Output:: Saving debugging output File: m4.info, Node: Dumpdef, Next: Trace, Prev: Debugging, Up: Debugging Displaying macro definitions ============================ If you want to see what a name expands into, you can use the builtin `dumpdef': dumpdef(...) which accepts any number of arguments. If called without any arguments, it displays the definitions of all known names, otherwise it displays the definitions of the names given. The output is printed directly on the standard error output. The expansion of `dumpdef' is void. define(`foo', `Hello world.') => dumpdef(`foo') error-->foo: `Hello world.' => dumpdef(`define') error-->define: => The last example shows how builtin macros definitions are displayed. *Note Debug Levels:: for information on controlling the details of the display. File: m4.info, Node: Trace, Next: Debug Levels, Prev: Dumpdef, Up: Debugging Tracing macro calls =================== It is possible to trace macro calls and expansions through the builtins `traceon' and `traceoff': traceon(...) traceoff(...) When called without any arguments, `traceon' and `traceoff' will turn tracing on and off, respectively, for all defined macros. When called with arguments, only the named macros are affected. The expansion of `traceon' and `traceoff' is void. Whenever a traced macro is called and the arguments have been collected, the call is displayed. If the expansion of the macro call is not void, the expansion can be displayed after the call. The output is printed directly on the standard error output. define(`foo', `Hello World.') => define(`echo', `$@') => traceon(`foo', `echo') => foo error-->m4trace: -1- foo -> `Hello World.' =>Hello World. echo(gnus, and gnats) error-->m4trace: -1- echo(`gnus', `and gnats') -> ``gnus',`and gnats'' =>gnus,and gnats The number between dashes is the depth of the expansion. It is one most of the time, signifying an expansion at the outermost level, but it increases when macro arguments contain unquoted macro calls. *Note Debug Levels:: for information on controlling the details of the display. File: m4.info, Node: Debug Levels, Next: Debug Output, Prev: Trace, Up: Debugging Controlling debugging output ============================ The `-d' option to `m4' controls the amount of details presented, when using the macros described in the preceding sections. The FLAGS following the option can be one or more of the following: Trace all macro calls made in this invocation of `m4'. Show the actual arguments in each macro call. This applies to all macro calls if the `t' flag is used, otherwise only the macros covered by calls of `traceon'. Show the expansion of each macro call, if it is not void. This applies to all macro calls if the `t' flag is used, otherwise only the macros covered by calls of `traceon'. Quote actual arguments and macro expansions in the display with the current quotes. Show several trace lines for each macro call. A line is shown when the macro is seen, but before the arguments are collected; a second line when the arguments have been collected and a third line after the call has completed. Add a unique `macro call id' to each line of the trace output. This is useful in connection with the `c' flag above. Show the name of the current input file in each trace output line. Show the the current input line number in each trace output line. Print a message when a named file is found through the path search mecanism (*note Search Path::.), giving the actual filename used. Print a message each time the current input file is changed, giving file name and input line number. A shorthand for all of the above flags. If no flags are specified with the `-d' option, the default is `aeq'. The examples in the previous two sections assumed the default flags. There is a builtin macro `debugmode', which allows on-the-fly control of the debugging output format: debugmode(opt FLAGS) The argument FLAGS should be a subset of the letters listed above. As special cases, if the argument starts with a `+', the flags are added to the current debug flags, and if it starts with a `-', they are removed. If no argument is present, the debugging flags are set to zero (as if no `-d' was given), and with an empty argument the flags are reset to the default.