home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GCC258_3.LHA / gcc / info / m4.info-1 < prev    next >
Encoding:
GNU Info File  |  1993-12-07  |  48.5 KB  |  1,546 lines

  1. This is Info file m4.info, produced by Makeinfo-1.47 from the input
  2. file ./m4.texinfo.
  3.  
  4.    This file documents the GNU m4 utility.
  5.  
  6.    Copyright (C) 1989-1992 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the entire resulting derived work is distributed under the terms
  15. of a permission notice identical to this one.
  16.  
  17.    Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions.
  20.  
  21. 
  22. File: m4.info,  Node: Top,  Next: Intro,  Prev: (dir),  Up: (dir)
  23.  
  24.    This file documents GNU `m4', which is a simple macroprocessor.  It
  25. has been originally written by Rene' Seindal, from Denmark.
  26.  
  27. * Menu:
  28.  
  29. * Intro::                       Introduction to m4
  30. * Manual::                      How to use this manual
  31. * Bugs::                        Reporting bugs in m4
  32.  
  33. * Invoking m4::                 How to run the program
  34. * Syntax::                      Lexical and syntactic conventions
  35.  
  36. * Macros::                      How to invoke macros
  37. * Definitions::                 How to define new macros
  38. * Conditionals::                Conditionals and loops
  39.  
  40. * Debugging::                   How to debug macros and input
  41. * Input Control::               Input control
  42.  
  43. * File Inclusion::              File inclusion
  44. * Diversions::                  Diverting and undiverting output
  45.  
  46. * Text handling::               Macros for text handling
  47. * Arithmetic::                  Macros for doing arithmetic
  48. * Unix commands::               Macros for running Unix commands
  49. * Miscellaneous::               Miscellaneous built-in macros
  50.  
  51. * Compatibility::               Compatibility with other versions of m4
  52.  
  53. * Concept Index::        Index for many concepts
  54. * Macro Index::            Index for all m4 macros
  55.  
  56.  -- The Detailed Node Listing --
  57.  
  58. Lexical and syntactic conventions
  59.  
  60. * Names::                       Macro names.
  61. * Quoted strings::              Quoting input to m4.
  62. * Other tokens::                Other kinds of input tokens.
  63. * Comments::                    Comments in m4 input.
  64.  
  65. How to invoke macros
  66.  
  67. * Invocation::                  Macro invocation
  68. * Macro Arguments::             Macro arguments.
  69. * Quoting Arguments::           On Quoting Arguments to macros.
  70. * Macro expansion::             Expanding macros.
  71.  
  72. How to define new macros
  73.  
  74. * Define::                      Defining a new macro
  75. * Arguments::                   Arguments to macros
  76. * Pseudo Arguments::            Pseudo arguments to macros
  77. * Undefine::                    Deleting a macro
  78. * Defn::                        Renaming macros
  79. * Pushdef::                     Temporarily redefining macros
  80.  
  81. * Indir::                       Indirect call of macros
  82. * Builtin::                     Indirect call of built-ins
  83.  
  84. Conditionals, loops and recursion
  85.  
  86. * Ifdef::                       Testing if a macro is defined
  87. * Ifelse::                      If-else construct, or multibranch
  88. * Loops::                       Loops and recursion in m4
  89.  
  90. How to debug macros and input
  91.  
  92. * Dumpdef::                     Displaying macro definitions
  93. * Trace::                       Tracing macro calls
  94. * Debug Levels::                Controlling debugging output
  95. * Debug Output::                Saving debugging output
  96.  
  97. Input control
  98.  
  99. * Dnl::                         Deleting whitespace in input
  100. * Changequote::                 Changing the quote characters
  101. * Changecom::                   Changing the comment delimiters
  102. * M4wrap::                      Saving input until end of input
  103.  
  104. File inclusion
  105.  
  106. * Include::                     Including named files
  107. * Search Path::                 Searching for include files
  108.  
  109. Diverting and undiverting output
  110.  
  111. * Divert::                      Diverting output
  112. * Undivert::                    Undiverting output
  113. * Divnum::                      Diversion numbers
  114. * Cleardiv::                    Discarding diverted text
  115.  
  116. Macros for text handling
  117.  
  118. * Len::                         Calculating length of strings
  119. * Index::                       Searching for substrings
  120. * Regexp::                      Searching for regular expressions
  121. * Substr::                      Extracting substrings
  122. * Translit::                    Translating characters
  123. * Patsubst::                    Substituting text by regular expression
  124. * Format::                      Formatting strings (printf-like)
  125.  
  126. Macros for doing arithmetic
  127.  
  128. * Incr::                        Decrement and increment operators
  129. * Eval::                        Evaluating integer expressions
  130.  
  131. Running Unix commands
  132.  
  133. * Syscmd::                      Executing simple commands
  134. * Esyscmd::                     Reading the output of commands
  135. * Sysval::                      Exit codes
  136. * Maketemp::                    Making names for temporary files
  137.  
  138. Miscellaneous built-in macros
  139.  
  140. * Errprint::                    Printing error messages
  141. * M4exit::                      Exiting from m4
  142.  
  143. Compatibility with other versions of `m4'
  144.  
  145. * Extensions::                  Extensions in GNU m4
  146. * Incompatibilities::           Facilities in System V m4 not in GNU m4
  147. * Other Incompat::              Other incompatibilities
  148.  
  149. 
  150. File: m4.info,  Node: Intro,  Next: Manual,  Prev: Top,  Up: Top
  151.  
  152. Introduction to `m4'
  153. ********************
  154.  
  155.    `m4' is a macro processor, in the sense that in copies its input to
  156. the output, expanding macros as it goes.  Macros are either built-in or
  157. user-defined, and can take any number of arguments.  Besides just doing
  158. macro expansion, `m4' has built-in functions for including named files,
  159. running Unix commands, doing integer arithmetic, manipulating text in
  160. various ways, recursion, etc...
  161.  
  162.    `m4' can be used either as a front-end to a compiler, or as a macro
  163. processor in its own right.
  164.  
  165.    GNU `m4' is mostly compatible with the System V, Release 3 version,
  166. except for some minor differences.  *Note Compatibility:: for more
  167. details.
  168.  
  169. 
  170. File: m4.info,  Node: Manual,  Next: Bugs,  Prev: Intro,  Up: Top
  171.  
  172. Using this manual
  173. *****************
  174.  
  175.    This manual contains a number of examples of `m4' input and output,
  176. and a simple notation is used to distinguish input, output and error
  177. messages from `m4'.  Examples are set out from the normal text, and
  178. shown in a fixed width font, like this
  179.  
  180.      This is an example of an example!
  181.  
  182.    To distinguish input from output, all output from `m4' is prefixed
  183. by the string `=>', and all error messages by the string `error-->'. 
  184. Thus
  185.  
  186.      Example of input line
  187.      =>Output line from m4
  188.      error-->and an error message
  189.  
  190.    As each of the predefined macros in `m4' is described, a prototype
  191. call of the macro will be shown, giving descriptive names to the
  192. arguments, e.g.,
  193.  
  194.      regexp(STRING, REGEXP, opt REPLACEMENT)
  195.  
  196.    All macro arguments in `m4' are strings, but some are given special
  197. interpretation, e.g., as numbers, filenames, regular expressions, etc.
  198.  
  199.    The `opt' before the third argument shows that this argument is
  200. optional--if it is left out, it is taken to be the empty string.  An
  201. ellipsis (`...') last in the argument list indicates that any number of
  202. arguments may follow.
  203.  
  204. 
  205. File: m4.info,  Node: Bugs,  Next: Invoking m4,  Prev: Manual,  Up: Top
  206.  
  207. Problems and bugs
  208. *****************
  209.  
  210.    If you have problems with GNU `m4' or think you've found a bug,
  211. please report it.  Before reporting a bug, make sure you've actually
  212. found a real bug.  Carefully reread the documentation and see if it
  213. really says you can do what you're trying to do.  If it's not clear
  214. whether you should be able to do something or not, report that too; it's
  215. a bug in the documentation!
  216.  
  217.    Before reporting a bug or trying to fix it yourself, try to isolate
  218. it to the smallest possible input file that reproduces the problem. 
  219. Then send us the input file and the exact results `m4' gave you.  Also
  220. say what you expected to occur; this will help us decide whether the
  221. problem was really in the documentation.
  222.  
  223.    Once you've got a precise problem, send e-mail to (Internet)
  224. `bug-gnu-utils@prep.ai.mit.edu' or (UUCP)
  225. `mit-eddie!prep.ai.mit.edu!bug-gnu-utils'.  Please include the version
  226. number of `m4' you are using.  You can get this information with the
  227. command `m4 -V /dev/null'.
  228.  
  229.    Non-bug suggestions are always welcome as well.  If you have
  230. questions about things that are unclear in the documentation or are
  231. just obscure features, please report them too.
  232.  
  233. 
  234. File: m4.info,  Node: Invoking m4,  Next: Syntax,  Prev: Bugs,  Up: Top
  235.  
  236. Invoking `m4'
  237. *************
  238.  
  239.    The format of the `m4' command is:
  240.  
  241.      `m4' [OPTIONS] [MACRO-DEFINITIONS] [INPUT-FILES]
  242.  
  243.    All options begin with `-', or if long option names are used, with a
  244. `--'.  A long option name need not be written completely, and
  245. unambigous prefix is sufficient.  `m4' understands the following
  246. options:
  247.  
  248. `-V'
  249. `--version'
  250.      Print the version number of the program.  To see only the version
  251.      number, use the command `m4 -V /dev/null'.
  252.  
  253. `-G'
  254. `--no-gnu-extensions'
  255.      Suppress all the extensions made in this implementation, compared
  256.      to the System V version.  For a list of these, *note
  257.      Compatibility::..
  258.  
  259. `-dFLAGS'
  260. `--debug FLAGS'
  261.      Set the debug-level according to the flags FLAGS.  The debug-level
  262.      controls the format and amount of information presented by the
  263.      debugging functions.  *Note Debug Levels:: for more details on the
  264.      format and meaning of FLAGS.
  265.  
  266. `-lNUM'
  267. `--arglength NUM'
  268.      Restrict the size of the output generated by macro tracing.  *Note
  269.      Debug Levels:: for more details.
  270.  
  271. `-oFILE'
  272. `--erroroutput FILE'
  273.      Redirect debug and trace output to the named file.  Error messages
  274.      are still printed on the standard error output.  *Note Debug
  275.      Output:: for more details.
  276.  
  277. `-IDIR'
  278. `--include DIR'
  279.      Make `m4' search DIR for included files that are not found in the
  280.      current working directory.  *Note Search Path:: for more details.
  281.  
  282. `-e'
  283. `--interactive'
  284.      Makes this invocation of `m4' interactive.  This means that all
  285.      output will be unbuffered, and interrupts will be ignored.
  286.  
  287. `-s'
  288. `--synclines'
  289.      Generate synchronisation lines, for use by the C preprocessor or
  290.      other similar tools.  This is useful, for example, when `m4' is
  291.      used as a front end to a compiler.  Source file name and line
  292.      number information is conveyed by lines of the form `#line LINENUM
  293.      "FILENAME"', which are inserted as needed into the middle of the
  294.      input (but always on complete lines per themselves).  Such lines
  295.      mean that the following line originated or was expanded from the
  296.      contents of input file FILENAME at line LINENUM.  The `"FILENAME"'
  297.      part is often omitted when the file name did not change from the
  298.      previous synchronisation line.
  299.  
  300. `-HN'
  301. `--hashsize N'
  302.      Make the internal hash table for symbol lookup be N entries big.
  303.      The number should be prime.  The default is 509 entries.  It
  304.      should not be necessary to increase this value, unless you define
  305.      an excessive number of macros.
  306.  
  307. `-NN'
  308. `--diversions N'
  309.      Allow for up to N diversions to be used at the same time.  The
  310.      default is 10 diversions.
  311.  
  312. `-Q'
  313. `--quiet'
  314. `--silent'
  315.      Suppress warnings about missing or superflous arguments in macro
  316.      calls.
  317.  
  318. `-B'
  319. `-S'
  320. `-T'
  321.      These options are present for compatibility with System V `m4', but
  322.      do nothing in this implementation.
  323.  
  324.    Macro definitions and deletions can be made on the command line, by
  325. using the `-D' and `-U' options.  They have the following format:
  326.  
  327. `-DNAME'
  328. `-DNAME=VALUE'
  329. `--define NAME'
  330. `--define NAME=VALUE'
  331.      This enters NAME into the symbol table, before any input files are
  332.      read.  If `=VALUE' is missing, the value is taken to be the empty
  333.      string.  The VALUE can be any string, and the macro can be defined
  334.      to take arguments, just as if it was defined from within the input.
  335.  
  336. `-UNAME'
  337. `--undefine NAME'
  338.      This deletes any predefined meaning NAME might have.  Obviously,
  339.      only predefined macros can be deleted in this way.
  340.  
  341. `-tNAME'
  342. `--trace NAME'
  343.      This enters NAME into the symbol table, as undefined but traced.
  344.      The macro will consequently be traced from the point it is defined.
  345.  
  346.    The remaining arguments on the command line are taken to be input
  347. file names.  If no names are present, the standard input is read.  A
  348. file name of `-' is taken to mean the standard input.
  349.  
  350.    The input files are read in the sequence given.  The standard input
  351. can only be read once, so the filename `-' should only appear once on
  352. the command line.
  353.  
  354. 
  355. File: m4.info,  Node: Syntax,  Next: Macros,  Prev: Invoking m4,  Up: Top
  356.  
  357. Lexical and syntactic conventions
  358. *********************************
  359.  
  360.    As `m4' reads its input, it separates it into "tokens".  A token is
  361. either a name, a quoted string, or any single character, that is not a
  362. part of either a name or a string.  Input to `m4' can also contain
  363. comments.
  364.  
  365. * Menu:
  366.  
  367. * Names::                       Macro names.
  368. * Quoted strings::              Quoting input to m4.
  369. * Other tokens::                Other kinds of input tokens.
  370. * Comments::                    Comments in m4 input.
  371.  
  372. 
  373. File: m4.info,  Node: Names,  Next: Quoted strings,  Prev: Syntax,  Up: Syntax
  374.  
  375. Names
  376. =====
  377.  
  378.    A name is any sequence of letters, digits, and the character `_'
  379. (underscore), where the first character is not a digit.  If a name has a
  380. macro definition, it will be subject to macro expansion (*note
  381. Macros::. for more details).
  382.  
  383.    Examples of legal names are: `foo', `_tmp', and `name01'.
  384.  
  385. 
  386. File: m4.info,  Node: Quoted strings,  Next: Other tokens,  Prev: Names,  Up: Syntax
  387.  
  388. Quoted strings
  389. ==============
  390.  
  391.    A quoted string is a sequence of characters surrounded by the quotes
  392. ``' and `'', where the number of start and end quotes within the string
  393. balances.  The value of a string token is the text, with one level of
  394. quotes stripped off.  Thus
  395.  
  396.      `'
  397.  
  398.    is the empty string, and
  399.  
  400.      ``quoted''
  401.  
  402.    is the string
  403.  
  404.      `quoted'
  405.  
  406.    The quote characters can be changed at any time, using the built-in
  407. macro `changequote'.  *Note Changequote:: for more information.
  408.  
  409. 
  410. File: m4.info,  Node: Other tokens,  Next: Comments,  Prev: Quoted strings,  Up: Syntax
  411.  
  412. Other tokens
  413. ============
  414.  
  415.    Any character, that is neither a part of a name, nor of a quoted
  416. string, is a token by itself.
  417.  
  418. 
  419. File: m4.info,  Node: Comments,  Prev: Other tokens,  Up: Syntax
  420.  
  421. Comments
  422. ========
  423.  
  424.    Comments in `m4' are normally delimited by the characters `#' and
  425. newline.  All characters between the comment delimiters are ignored,
  426. but the entire comment (including the delimiters) is passed through to
  427. the output--comments are *not* discarded by `m4'.
  428.  
  429.    Comments cannot be nested, so the first newline after a `#' ends the
  430. comment.  The begin comment character can be included in the input by
  431. quoting it.
  432.  
  433.    The comment delimiters can be changed to any string at any time,
  434. using the built-in macro `changecom'.  *Note Changecom:: for more
  435. information.
  436.  
  437. 
  438. File: m4.info,  Node: Macros,  Next: Definitions,  Prev: Syntax,  Up: Top
  439.  
  440. How to invoke macros
  441. ********************
  442.  
  443.    This chapter covers macro invocation, macro arguments and how macro
  444. expansion is treated.
  445.  
  446. * Menu:
  447.  
  448. * Invocation::                  Macro invocation
  449. * Macro Arguments::             Macro arguments.
  450. * Quoting Arguments::           On Quoting Arguments to macros.
  451. * Macro expansion::             Expanding macros.
  452.  
  453. 
  454. File: m4.info,  Node: Invocation,  Next: Macro Arguments,  Prev: Macros,  Up: Macros
  455.  
  456. Macro invocation
  457. ================
  458.  
  459.    Macro invocations has one of the forms
  460.  
  461.      name
  462.  
  463. which is a macro invocation without any arguments, or
  464.  
  465.      name(arg1, arg2, ..., argN)
  466.  
  467. which is a macro invocation with N arguments.  Macros can have any
  468. number of arguments.  All arguments are strings, but different macros
  469. might interpret the arguments in different ways.
  470.  
  471.    The opening parenthesis *must* follow the NAME directly, with no
  472. spaces in between.  If it does not, the macro is called with no
  473. arguments at all.
  474.  
  475.    For a macro call to have no arguments, the parentheses *must* be
  476. left out.  The macro call
  477.  
  478.      name()
  479.  
  480. is a macro call with one argument, which is the empty string, not a call
  481. with no arguments.
  482.  
  483. 
  484. File: m4.info,  Node: Macro Arguments,  Next: Quoting Arguments,  Prev: Invocation,  Up: Macros
  485.  
  486. Macro arguments
  487. ===============
  488.  
  489.    When a name is seen, and it has a macro definition, it will be
  490. expanded as a macro.
  491.  
  492.    If the name is followed by an opening parenthesis, the arguments
  493. will be collected before the macro is called.  If too few arguments are
  494. supplied, the missing arguments are taken to be the empty string.  If
  495. there are too many arguments, the excess arguments are ignored.
  496.  
  497.    Normally `m4' will issue warnings if a built-in macro is called with
  498. an inappropriate number of arguments, but it can be suppressed with the
  499. `-Q' command line option.  For user defined macros, there is no check
  500. of the number of arguments given.
  501.  
  502.    Macros are expanded normally during argument collection, and whatever
  503. commas, quotes and parentheses that might show up in the resulting
  504. expanded text will serve to define the arguments as well.  Thus, if FOO
  505. expands to `,b,c', the macro call
  506.  
  507.      bar(a foo,d)
  508.  
  509.    is a macro call with four arguments, which are `a ', `b', `c' and
  510. `d'.
  511.  
  512. 
  513. File: m4.info,  Node: Quoting Arguments,  Next: Macro expansion,  Prev: Macro Arguments,  Up: Macros
  514.  
  515. Quoting macro arguments
  516. =======================
  517.  
  518.    Each argument has leading unquoted whitespace removed.  Within each
  519. argument, all unquoted parentheses must match.  For example, if FOO is
  520. a macro,
  521.  
  522.      foo(() (`(') `(')
  523.  
  524.    is a macro call, with one argument, whose value is `() (() ('.
  525.  
  526.    It is common practice to quote all arguments to macros, unless you
  527. are sure you want the arguments expanded.  Thus, in the above example
  528. with the parentheses, the `right' way to do it is like this:
  529.  
  530.      foo(`() (() (')
  531.  
  532.    It is, however, in certain cases necessary to leave out quotes for
  533. some arguments, and there is nothing wrong in doing it.  It just makes
  534. life a bit harder, if you are not careful.
  535.  
  536. 
  537. File: m4.info,  Node: Macro expansion,  Prev: Quoting Arguments,  Up: Macros
  538.  
  539. Macro expansion
  540. ===============
  541.  
  542.    When the arguments, if any, to a macro call have been collected, the
  543. macro is expanded, and the expansion text is pushed back onto the input
  544. (unquoted), and reread.  The expansion text from one macro call might
  545. therefore result in more macros being called, if the calls are included,
  546. completely or partially, in the first macro calls' expansion.
  547.  
  548.    Taking a very simple example, if FOO expands to `bar', and BAR
  549. expands to `Hello world', the input
  550.  
  551.      foo
  552.  
  553. will expand first to `bar', and when this is reread and expanded, into
  554. `Hello world'.
  555.  
  556. 
  557. File: m4.info,  Node: Definitions,  Next: Conditionals,  Prev: Macros,  Up: Top
  558.  
  559. How to define new macros
  560. ************************
  561.  
  562.    Macros can be defined, redefined and deleted in several different
  563. ways. Also, it is possible to redefine a macro, without losing a
  564. previous value, which can be brought back at a later time.
  565.  
  566. * Menu:
  567.  
  568. * Define::                      Defining a new macro
  569. * Arguments::                   Arguments to macros
  570. * Pseudo Arguments::            Pseudo arguments to macros
  571. * Undefine::                    Deleting a macro
  572. * Defn::                        Renaming macros
  573. * Pushdef::                     Temporarily redefining macros
  574.  
  575. * Indir::                       Indirect call of macros
  576. * Builtin::                     Indirect call of built-ins
  577.  
  578. 
  579. File: m4.info,  Node: Define,  Next: Arguments,  Prev: Definitions,  Up: Definitions
  580.  
  581. Defining a macro
  582. ================
  583.  
  584.    The normal way to define or redefine macros is to use the built-in
  585. `define':
  586.  
  587.      define(NAME, EXPANSION)
  588.  
  589. which defines NAME to expand to EXPANSION.
  590.  
  591.    The expansion of `define' is void.
  592.  
  593.    The following example defines the macro FOO to expand to the text
  594. `Hello World.'.
  595.  
  596.      define(`foo', `Hello world.')
  597.      =>
  598.      foo
  599.      =>Hello world.
  600.  
  601.    The empty line in the output is there because the newline is not a
  602. part of the macro definition, and it is consequently copied to the
  603. output. This can be avoided by use of the macro `dnl' (*note Dnl::. for
  604. details).
  605.  
  606. 
  607. File: m4.info,  Node: Arguments,  Next: Pseudo Arguments,  Prev: Define,  Up: Definitions
  608.  
  609. Arguments to macros
  610. ===================
  611.  
  612.    Macros can have arguments.  The Nth argument is denoted by `$n' in
  613. the expansion text, and is replaced by the Nth actual argument, when
  614. the macro is expanded.  Here is a example of a macro with two
  615. arguments.  It simply exchanges the order of the two arguments.
  616.  
  617.      define(`exch', `$2, $1')
  618.      =>
  619.      exch(arg1, arg2)
  620.      =>arg2, arg1
  621.  
  622.    This can be used, for example, if you like the arguments to `define'
  623. to be reversed.
  624.  
  625.      define(`exch', `$2, $1')
  626.      =>
  627.      define(exch(``expansion text'', ``macro''))
  628.      =>
  629.      macro
  630.      =>expansion text
  631.  
  632.    For an explanation of the double quotes, *note Quoting Arguments::..
  633.  
  634.    GNU `m4' allows the number following the `$' to consist of one or
  635. more digits, allowing macros to have any number of arguments.  This is
  636. not so in Unix implementations of `m4', which only recognize one digit.
  637.  
  638.    As a special case, the zero'th argument, `$0', is always the name of
  639. the macro being expanded.
  640.  
  641.      define(`test', ``Macro name: $0'')
  642.      =>
  643.      test
  644.      =>Macro name: test
  645.  
  646.    If you want quoted text to appear as part of the expansion text,
  647. remember that quotes can be nested in quoted strings.  Thus, in
  648.  
  649.      define(`foo', `This is macro `foo'.')
  650.      =>
  651.      foo
  652.      =>This is macro foo.
  653.  
  654. The `foo' in the expansion text is *not* expanded, since it is a quoted
  655. string, and not a name.
  656.  
  657. 
  658. File: m4.info,  Node: Pseudo Arguments,  Next: Undefine,  Prev: Arguments,  Up: Definitions
  659.  
  660. Special arguments to macros
  661. ===========================
  662.  
  663.    There is a special notation for the number of actual arguments
  664. supplied, and for all the actual arguments.
  665.  
  666.    The number of actual arguments in a macro call is denoted by `$#' in
  667. the expansion text.  Thus, a macro to display the number of arguments
  668. given can be
  669.  
  670.      define(`nargs', `$#')
  671.      =>
  672.      nargs
  673.      =>0
  674.      nargs()
  675.      =>1
  676.      nargs(arg1, arg2, arg3)
  677.      =>3
  678.  
  679.    The notation `$*' can be used in the expansion text to denote all
  680. the actual arguments, unquoted, with commas in between.  For example
  681.  
  682.      define(`echo', `$*')
  683.      =>
  684.      echo(arg1,    arg2, arg3 , arg4)
  685.      =>arg1,arg2,arg3 ,arg4
  686.  
  687.    Often each argument should be quoted, and the notation `$@' handles
  688. that.  It is just like `$*', except that it quotes each argument. A
  689. simple example of that is:
  690.  
  691.      define(`echo', `$@')
  692.      =>
  693.      echo(arg1,    arg2, arg3 , arg4)
  694.      =>arg1,arg2,arg3 ,arg4
  695.  
  696.    Where did the quotes go?  Of course, they were eaten, when the
  697. expanded text were reread by `m4'.  To show the difference, try
  698.  
  699.      define(`echo1', `$*')
  700.      =>
  701.      define(`echo2', `$@')
  702.      =>
  703.      define(`foo', `This is macro `foo'.')
  704.      =>
  705.      echo1(foo)
  706.      =>This is macro This is macro foo..
  707.      echo2(foo)
  708.      =>This is macro foo.
  709.  
  710.    If you don't understand this, *note Trace::..
  711.  
  712.    A `$' sign in the expansion text, that is not followed by anything
  713. `m4' understands, is simply copied to the macro expansion, as any other
  714. text is.
  715.  
  716.      define(`foo', `$$$ hello $$$')
  717.      =>
  718.      foo
  719.      =>$$$ hello $$$
  720.  
  721.    If you want a macro to expand to something like `$12', put a pair of
  722. quotes after the `$'.  This will prevent `m4' from interpreting the `$'
  723. sign as a reference to an argument.
  724.  
  725. 
  726. File: m4.info,  Node: Undefine,  Next: Defn,  Prev: Pseudo Arguments,  Up: Definitions
  727.  
  728. Deleting a macro
  729. ================
  730.  
  731.    A macro definition can be removed with `undefine':
  732.  
  733.      undefine(NAME)
  734.  
  735. which removes the macro NAME.  The macro name must necessarily be
  736. quoted, since it will be expanded otherwise.
  737.  
  738.    The expansion of `undefine' is void.
  739.  
  740.      foo
  741.      =>foo
  742.      define(`foo', `expansion text')
  743.      =>
  744.      foo
  745.      =>expansion text
  746.      undefine(`foo')
  747.      =>
  748.      foo
  749.      =>foo
  750.  
  751.    It is not an error for NAME to have no macro definition.  In that
  752. case, `undefine' does nothing.
  753.  
  754. 
  755. File: m4.info,  Node: Defn,  Next: Pushdef,  Prev: Undefine,  Up: Definitions
  756.  
  757. Renaming macros
  758. ===============
  759.  
  760.    It is possible to rename an already defined macro.  To do this, you
  761. need the built-in `defn':
  762.  
  763.      defn(NAME)
  764.  
  765. which expands to the *quoted definition* of NAME.  If the argument is
  766. not a defined macro, the expansion is void.
  767.  
  768.    If NAME is a user-defined macro, the quoted definition is simply the
  769. quoted expansion text.  If, instead, NAME is a built-in, the expansion
  770. is a special token, which points to the built-in's internal definition.
  771.  This token is only meaningful as the second argument to `define' (and
  772. `pushdef'), and is ignored in any other context.
  773.  
  774.    Its normal use is best understood through an example, which shows
  775. how to rename `undefine' to `zap':
  776.  
  777.      define(`zap', defn(`undefine'))
  778.      =>
  779.      zap(`undefine')
  780.      =>
  781.      undefine(`zap')
  782.      =>undefine(zap)
  783.  
  784.    In this way, `defn' can be used to copy macro definitions, and also
  785. definitions of built-in macros.  Even if the original macro is removed,
  786. the other name can still be used to access the definition.
  787.  
  788. 
  789. File: m4.info,  Node: Pushdef,  Next: Indir,  Prev: Defn,  Up: Definitions
  790.  
  791. Temporarily redefining macros
  792. =============================
  793.  
  794.    It is possible to redefine a macro temporarily, reverting to the
  795. previous definition at a later time. This is done with the built-ins
  796. `pushdef' and `popdef':
  797.  
  798.      pushdef(NAME, EXPANSION)
  799.      popdef(NAME)
  800.  
  801. which are quite analogous to `define' and `undefine'.
  802.  
  803.    These macros work in a stack-like fashion.  A macro is temporarily
  804. redefined with `pushdef', which replaces an existing definition of
  805. NAME, while saving the previous definition, before the new one is
  806. installed.  If there is no previous definition, `pushdef' behaves
  807. exactly like `define'.
  808.  
  809.    If a macro has several definitions (of which only one is accessible),
  810. the topmost definition can be removed with `popdef'. If there is no
  811. previous definition, `popdef' does nothing.
  812.  
  813.      define(`foo', `Expansion one.')
  814.      =>
  815.      foo
  816.      =>Expansion one.
  817.      pushdef(`foo', `Expansion two.')
  818.      =>
  819.      foo
  820.      =>Expansion two.
  821.      popdef(`foo')
  822.      =>
  823.      foo
  824.      =>Expansion one.
  825.      popdef(`foo')
  826.      =>
  827.      foo
  828.      =>foo
  829.  
  830.    If a macro with several definitions is redefined with `define', the
  831. topmost definition is *replaced* with the new definition.  If it is
  832. removed with `undefine', *all* the definitions are removed, and not
  833. only the topmost one.
  834.  
  835.      define(`foo', `Expansion one.')
  836.      =>
  837.      foo
  838.      =>Expansion one.
  839.      pushdef(`foo', `Expansion two.')
  840.      =>
  841.      foo
  842.      =>Expansion two.
  843.      define(`foo', `Second expansion two.')
  844.      =>
  845.      foo
  846.      =>Second expansion two.
  847.      undefine(`foo')
  848.      =>
  849.      foo
  850.      =>foo
  851.  
  852.    It is possible to temporarily redefine a built-in with `pushdef' and
  853. `defn'.
  854.  
  855. 
  856. File: m4.info,  Node: Indir,  Next: Builtin,  Prev: Pushdef,  Up: Definitions
  857.  
  858. Indirect call of macros
  859. =======================
  860.  
  861.    Any macro can be called indirectly with `indir':
  862.  
  863.      indir(NAME, ...)
  864.  
  865. which results in a call to the macro NAME, which is passed the rest of
  866. the arguments.  This can be used to call macros with "illegal" names
  867. (`define' allows such names to be defined):
  868.  
  869.      define(`$$internal$macro', `Internal macro (name `$0')')
  870.      =>
  871.      $$internal$macro
  872.      =>$$internal$macro
  873.      indir(`$$internal$macro')
  874.      =>Internal macro (name $$internal$macro)
  875.  
  876.    The point is, here, that larger macro packages can have private
  877. macros defined, that will not be called by accident.  They can *only* be
  878. called through the built-in `indir'.
  879.  
  880. 
  881. File: m4.info,  Node: Builtin,  Prev: Indir,  Up: Definitions
  882.  
  883. Indirect call of built-ins
  884. ==========================
  885.  
  886.    Built-in macros can be called indirectly with `built-in':
  887.  
  888.      builtin(NAME, ...)
  889.  
  890. which results in a call to the built-in NAME, which is passed the rest
  891. of the arguments.  This can be used, if NAME has been given another
  892. definition that has covered the original.
  893.  
  894. 
  895. File: m4.info,  Node: Conditionals,  Next: Debugging,  Prev: Definitions,  Up: Top
  896.  
  897. Conditionals, loops and recursion
  898. *********************************
  899.  
  900.    Macros, expanding to plain text, perhaps with arguments, are not
  901. quite enough.  We would like to have macros expand to different things,
  902. based on decisions taken at run-time.  E.g., we need some kind of
  903. conditionals. Also, we would like to have some kind of loop construct,
  904. so we could do something a number of times, or while some condition is
  905. true.
  906.  
  907. * Menu:
  908.  
  909. * Ifdef::                       Testing if a macro is defined
  910. * Ifelse::                      If-else construct, or multibranch
  911. * Loops::                       Loops and recursion in m4
  912.  
  913. 
  914. File: m4.info,  Node: Ifdef,  Next: Ifelse,  Prev: Conditionals,  Up: Conditionals
  915.  
  916. Testing macro definitions
  917. =========================
  918.  
  919.    There are two different built-in conditionals in `m4'.  The first is
  920. `ifdef':
  921.  
  922.      ifdef(NAME, STRING-1, opt STRING-2)
  923.  
  924. which makes it possible to test whether a macro is defined or not.  If
  925. NAME is defined as a macro, `ifdef' expands to STRING-1, otherwise to
  926. STRING-2.  If STRING-2 is omitted, it is taken to be the empty string
  927. (according to the normal rules).
  928.  
  929.      ifdef(`foo', ``foo' is defined', ``foo' is not defined')
  930.      =>foo is not defined
  931.      define(`foo', `')
  932.      =>
  933.      ifdef(`foo', ``foo' is defined', ``foo' is not defined')
  934.      =>foo is defined
  935.  
  936. 
  937. File: m4.info,  Node: Ifelse,  Next: Loops,  Prev: Ifdef,  Up: Conditionals
  938.  
  939. Comparing strings
  940. =================
  941.  
  942.    The other conditional, `ifelse', is much more powerful.  It can be
  943. used as a way to introduce a long comment, as an if-else construct, or
  944. as a multibranch, depending on the number of arguments supplied:
  945.  
  946.      ifelse(COMMENT)
  947.      ifelse(STRING-1, STRING-2, EQUAL, opt NOT-EQUAL)
  948.      ifelse(STRING-1, STRING-2, EQUAL, ...)
  949.  
  950. Used with only one argument, the `ifelse' simply discards it and
  951. produces no output.  This is a common `m4' idiom for introducing a
  952. block comment, as an alternative to repeatedly using `dnl'.  This
  953. special usage is recognized by GNU `m4', so that in this case, the
  954. warning about missing arguments is never triggered.
  955.  
  956.    If called with three or four arguments, `ifelse' expands into EQUAL,
  957. if STRING-1 and STRING-2 are equal (character for character), otherwise
  958. it expands to NOT-EQUAL.
  959.  
  960.      ifelse(foo, bar, `true')
  961.      =>
  962.      ifelse(foo, foo, `true')
  963.      =>true
  964.      ifelse(foo, bar, `true', `false')
  965.      =>false
  966.      ifelse(foo, foo, `true', `false')
  967.      =>true
  968.  
  969.    However, `ifelse' can take more than four arguments.  If given more
  970. than four arguments, `ifelse' works like a `case' or `switch' statement
  971. in traditional programming languages.  If STRING-1 and STRING-2 are
  972. equal, `ifelse' expands into EQUAL, otherwise the procedure is repeated
  973. with the first three arguments discarded.  This calls for an example:
  974.  
  975.      ifelse(foo, bar, `third', gnu, gnats, `sixth', `seventh')
  976.      =>seventh
  977.  
  978.    Naturally, the normal case will be slightly more advanced than these
  979. examples.  A common use of `ifelse' is in macros implementing loops of
  980. various kinds.
  981.  
  982. 
  983. File: m4.info,  Node: Loops,  Prev: Ifelse,  Up: Conditionals
  984.  
  985. Loops and recursion
  986. ===================
  987.  
  988.    There is no direct support for loops in `m4', but macros can be
  989. recursive.  There is no limit on the number of recursion levels, other
  990. than those enforced by your hardware and operating system.
  991.  
  992.    Loops can be programmed using recursion and the conditionals
  993. described previously.
  994.  
  995.    There is a built-in macro, `shift', which can, among other things,
  996. be used for iterating through the actual arguments to a macro:
  997.  
  998.      shift(...)
  999.  
  1000. It takes any number of arguments, and expands to all but the first
  1001. argument, separated by commas, with each argument quoted.
  1002.  
  1003.      shift(bar)
  1004.      =>
  1005.      shift(foo, bar, baz)
  1006.      =>bar,baz
  1007.  
  1008.    An example of the use of `shift' is this macro, which reverses the
  1009. order of its arguments:
  1010.  
  1011.      define(`reverse', `ifelse($#, 0, , $#, 1, ``$1'',
  1012.                    `reverse(shift($@)), `$1'')')
  1013.      =>
  1014.      reverse
  1015.      =>
  1016.      reverse(foo)
  1017.      =>foo
  1018.      reverse(foo, bar, gnats,and gnus)
  1019.      =>and gnus, gnats, bar, foo
  1020.  
  1021.    While not a very interesting macro, it does show how simple loops
  1022. can be made with `shift', `ifelse' and recursion.
  1023.  
  1024.    Here is an example of a loop macro that implements a simple forloop.
  1025.  It can, for example, be used for simple counting:
  1026.  
  1027.      forloop(`i', 1, 8, `i ')
  1028.      =>1 2 3 4 5 6 7 8
  1029.  
  1030.    The arguments are a name for the iteration variable, the starting
  1031. value, the final value, and the text to be expanded for each iteration.
  1032.  With this macro, the macro `i' is defined only within the loop.  After
  1033. the loop, it retains whatever value it might have had before.
  1034.  
  1035.    For-loops can be nested, like
  1036.  
  1037.      forloop(`i', 1, 4, `forloop(`j', 1, 8, `(i, j) ')
  1038.      ')
  1039.      =>(1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8)
  1040.      =>(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8)
  1041.      =>(3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8)
  1042.      =>(4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8)
  1043.      =>
  1044.  
  1045.    The implementation of the `forloop' macro is fairly straightforward.
  1046.  The `forloop' macro itself is simply a wrapper, which saves the
  1047. previous definition of the first argument, calls the internal macro
  1048. `_forloop', and re-establishes the saved definition of the first
  1049. argument.
  1050.  
  1051.    The macro `_forloop' expands the fourth argument once, and tests to
  1052. see if it is finished.  If it has not finished, it increments the
  1053. iteration variable (using the predefined macro `incr' (*note Incr::.
  1054. for details)), and recurses.
  1055.  
  1056.    Here is the actual implementation of `forloop':
  1057.  
  1058.      define(`forloop',
  1059.             `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
  1060.      define(`_forloop',
  1061.             `$4`'ifelse($1, `$3', ,
  1062.                 `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
  1063.  
  1064.    Notice the careful use of quotes.  Only three macro arguments are
  1065. unquoted, each for its own reason.  Try to find out *why* these three
  1066. arguments are left unquoted, and see what happens if they are quoted.
  1067.  
  1068.    Now, even though these two macros are useful, they are still not
  1069. robust enough for general use. They lack even basic error handling of
  1070. cases like start value less than final value, and the first argument
  1071. not being a name.  Correcting these errors are left as an exercise to
  1072. the reader.
  1073.  
  1074. 
  1075. File: m4.info,  Node: Debugging,  Next: Input Control,  Prev: Conditionals,  Up: Top
  1076.  
  1077. How to debug macros and input
  1078. *****************************
  1079.  
  1080.    When writing macros for `m4', most of the time they won't work as
  1081. intended (as is the case with most programming languages).  There is a
  1082. little support for macro debugging in `m4'.
  1083.  
  1084. * Menu:
  1085.  
  1086. * Dumpdef::                     Displaying macro definitions
  1087. * Trace::                       Tracing macro calls
  1088. * Debug Levels::                Controlling debugging output
  1089. * Debug Output::                Saving debugging output
  1090.  
  1091. 
  1092. File: m4.info,  Node: Dumpdef,  Next: Trace,  Prev: Debugging,  Up: Debugging
  1093.  
  1094. Displaying macro definitions
  1095. ============================
  1096.  
  1097.    If you want to see what a name expands into, you can use the built-in
  1098. `dumpdef':
  1099.  
  1100.      dumpdef(...)
  1101.  
  1102. which accepts any number of arguments.  If called without any arguments,
  1103. it displays the definitions of all known names, otherwise it displays
  1104. the definitions of the names given.  The output is printed directly on
  1105. the standard error output.
  1106.  
  1107.    The expansion of `dumpdef' is void.
  1108.  
  1109.      define(`foo', `Hello world.')
  1110.      =>
  1111.      dumpdef(`foo')
  1112.      error-->foo:    `Hello world.'
  1113.      =>
  1114.      dumpdef(`define')
  1115.      error-->define:    <define>
  1116.      =>
  1117.  
  1118.    The last example shows how built-in macros definitions are displayed.
  1119.  
  1120.    *Note Debug Levels:: for information on controlling the details of
  1121. the display.
  1122.  
  1123. 
  1124. File: m4.info,  Node: Trace,  Next: Debug Levels,  Prev: Dumpdef,  Up: Debugging
  1125.  
  1126. Tracing macro calls
  1127. ===================
  1128.  
  1129.    It is possible to trace macro calls and expansions through the
  1130. built-ins `traceon' and `traceoff':
  1131.  
  1132.      traceon(...)
  1133.      traceoff(...)
  1134.  
  1135. When called without any arguments, `traceon' and `traceoff' will turn
  1136. tracing on and off, respectively,  for all defined macros. When called
  1137. with arguments, only the named macros are affected.
  1138.  
  1139.    The expansion of `traceon' and `traceoff' is void.
  1140.  
  1141.    Whenever a traced macro is called and the arguments have been
  1142. collected, the call is displayed.  If the expansion of the macro call
  1143. is not void, the expansion can be displayed after the call.  The output
  1144. is printed directly on the standard error output.
  1145.  
  1146.      define(`foo', `Hello World.')
  1147.      =>
  1148.      define(`echo', `$@')
  1149.      =>
  1150.      traceon(`foo', `echo')
  1151.      =>
  1152.      foo
  1153.      error-->m4trace: -1- foo -> `Hello World.'
  1154.      =>Hello World.
  1155.      echo(gnus, and gnats)
  1156.      error-->m4trace: -1- echo(`gnus', `and gnats') -> ``gnus',`and gnats''
  1157.      =>gnus,and gnats
  1158.  
  1159.    The number between dashes is the depth of the expansion.  It is one
  1160. most of the time, signifying an expansion at the outermost level, but it
  1161. increases when macro arguments contain unquoted macro calls.
  1162.  
  1163.    *Note Debug Levels:: for information on controlling the details of
  1164. the display.
  1165.  
  1166. 
  1167. File: m4.info,  Node: Debug Levels,  Next: Debug Output,  Prev: Trace,  Up: Debugging
  1168.  
  1169. Controlling debugging output
  1170. ============================
  1171.  
  1172.    The `-d' option to `m4' controls the amount of details presented,
  1173. when using the macros described in the preceding sections.
  1174.  
  1175.    The FLAGS following the option can be one or more of the following:
  1176.  
  1177. `t'
  1178.      Trace all macro calls made in this invocation of `m4'.
  1179.  
  1180. `a'
  1181.      Show the actual arguments in each macro call.  This applies to all
  1182.      macro calls if the `t' flag is used, otherwise only the macros
  1183.      covered by calls of `traceon'.
  1184.  
  1185. `e'
  1186.      Show the expansion of each macro call, if it is not void.  This
  1187.      applies to all macro calls if the `t' flag is used, otherwise only
  1188.      the macros covered by calls of `traceon'.
  1189.  
  1190. `q'
  1191.      Quote actual arguments and macro expansions in the display with the
  1192.      current quotes.
  1193.  
  1194. `c'
  1195.      Show several trace lines for each macro call.  A line is shown
  1196.      when the macro is seen, but before the arguments are collected; a
  1197.      second line when the arguments have been collected and a third
  1198.      line after the call has completed.
  1199.  
  1200. `x'
  1201.      Add a unique `macro call id' to each line of the trace output. 
  1202.      This is useful in connection with the `c' flag above.
  1203.  
  1204. `f'
  1205.      Show the name of the current input file in each trace output line.
  1206.  
  1207. `l'
  1208.      Show the the current input line number in each trace output line.
  1209.  
  1210. `p'
  1211.      Print a message when a named file is found through the path search
  1212.      mecanism (*note Search Path::.), giving the actual filename used.
  1213.  
  1214. `i'
  1215.      Print a message each time the current input file is changed,
  1216.      giving file name and input line number.
  1217.  
  1218. `V'
  1219.      A shorthand for all of the above flags.
  1220.  
  1221.    If no flags are specified with the `-d' option, the default is
  1222. `aeq'. The examples in the previous two sections assumed the default
  1223. flags.
  1224.  
  1225.    There is a built-in macro `debugmode', which allows on-the-fly
  1226. control of the debugging output format:
  1227.  
  1228.      debugmode(opt FLAGS)
  1229.  
  1230. The argument FLAGS should be a subset of the letters listed above. As
  1231. special cases, if the argument starts with a `+', the flags are added
  1232. to the current debug flags, and if it starts with a `-', they are
  1233. removed.  If no argument is present, the debugging flags are set to
  1234. zero (as if no `-d' was given), and with an empty argument the flags
  1235. are reset to the default.
  1236.  
  1237. 
  1238. File: m4.info,  Node: Debug Output,  Prev: Debug Levels,  Up: Debugging
  1239.  
  1240. Saving debugging output
  1241. =======================
  1242.  
  1243.    Debug and tracing output can be redirected to files using either the
  1244. `-o' option to `m4', or with the built-in macro `debugfile':
  1245.  
  1246.      debugfile(opt FILENAME)
  1247.  
  1248. will send all further debug and trace output to FILENAME.  If FILENAME
  1249. is empty, debug and trace output are discarded and if `debugfile' is
  1250. called without any arguments, debug and trace output are sent to the
  1251. standard error output.
  1252.  
  1253. 
  1254. File: m4.info,  Node: Input Control,  Next: File Inclusion,  Prev: Debugging,  Up: Top
  1255.  
  1256. Input control
  1257. *************
  1258.  
  1259.    This chapter describes various built-in macros for controlling the
  1260. input to `m4'.
  1261.  
  1262. * Menu:
  1263.  
  1264. * Dnl::                         Deleting whitespace in input
  1265. * Changequote::                 Changing the quote characters
  1266. * Changecom::                   Changing the comment delimiters
  1267. * M4wrap::                      Saving input until end of input
  1268.  
  1269. 
  1270. File: m4.info,  Node: Dnl,  Next: Changequote,  Prev: Input Control,  Up: Input Control
  1271.  
  1272. Deleting whitespace in input
  1273. ============================
  1274.  
  1275.    The built-in `dnl' reads and discards all characters, up to and
  1276. including the first newline:
  1277.  
  1278.      dnl
  1279.  
  1280. and it is often used in connection with `define', to remove the newline
  1281. that follow the call to `define'.  Thus
  1282.  
  1283.      define(`foo', `Macro `foo'.')dnl A very simple macro, indeed.
  1284.      foo
  1285.      =>Macro foo.
  1286.  
  1287.    The input up to and including the next newline is discarded, as
  1288. opposed to the way comments are treated (*note Comments::.).
  1289.  
  1290.    Usually, `dnl' is immediately followed by an end of line or some
  1291. other whitespace.  GNU `m4' will produce a warning diagnostic if `dnl'
  1292. is followed by an open parenthesis.  In this case, `dnl' will collect
  1293. and process all arguments, looking for a matching close parenthesis. 
  1294. All predictable side effects resulting from this collection will take
  1295. place.  `dnl' will return no output.  The input following the matching
  1296. close parenthesis up to and including the next newline, on whatever
  1297. line containing it, will still be discarded.
  1298.  
  1299. 
  1300. File: m4.info,  Node: Changequote,  Next: Changecom,  Prev: Dnl,  Up: Input Control
  1301.  
  1302. Changing the quote characters
  1303. =============================
  1304.  
  1305.    The default quote delimiters can be changed with the built-in
  1306. `changequote':
  1307.  
  1308.      changequote(opt START, opt END)
  1309.  
  1310. where START is the new start-quote delimiter and END is the new
  1311. end-quote delimiter.  If any of the arguments are missing, the default
  1312. quotes (``' and `'') are used instead of the void arguments.
  1313.  
  1314.    The expansion of `changequote' is void.
  1315.  
  1316.      changequote([,])
  1317.      =>
  1318.      define([foo], [Macro [foo].])
  1319.      =>
  1320.      foo
  1321.      =>Macro foo.
  1322.  
  1323.    If no single character is appropriate, START and END can be of any
  1324. length.
  1325.  
  1326.      changequote([[,]])
  1327.      =>
  1328.      define([[foo]], [[Macro [[[foo]]].]])
  1329.      =>
  1330.      foo
  1331.      =>Macro [foo].
  1332.  
  1333.    Changing the quotes to the empty strings will effectively disable the
  1334. quoting mechanism, leaving no way to quote text.
  1335.  
  1336.      define(`foo', `Macro `FOO'.')
  1337.      =>
  1338.      changequote(,)
  1339.      =>
  1340.      foo
  1341.      =>Macro `FOO'.
  1342.      `foo'
  1343.      =>`Macro `FOO'.'
  1344.  
  1345.    There is no way in `m4' to quote a string containing an unmatched
  1346. left quote, except using `changequote' to change the current quotes.
  1347.  
  1348.    Neither quote string should start with a letter or `_' (underscore),
  1349. as they will be confused with names in the input.  Doing so disables
  1350. the quoting mechanism.
  1351.  
  1352. 
  1353. File: m4.info,  Node: Changecom,  Next: M4wrap,  Prev: Changequote,  Up: Input Control
  1354.  
  1355. Changing comment delimiters
  1356. ===========================
  1357.  
  1358.    The default comment delimiters can be changed with the built-in
  1359. macro `changecom':
  1360.  
  1361.      changecom(opt START, opt END)
  1362.  
  1363. where START is the new start-comment delimiter and END is the new
  1364. end-comment delimiter.  If any of the arguments are void, the default
  1365. comment delimiters (`#' and newline) are used instead of the void
  1366. arguments.  The comment delimiters can be of any length.
  1367.  
  1368.    The expansion of `changecom' is void.
  1369.  
  1370.      define(`comment', `COMMENT')
  1371.      =>
  1372.      # A normal comment
  1373.      =># A normal comment
  1374.      changecom(`/*', `*/')
  1375.      =>
  1376.      # Not a comment anymore
  1377.      =># Not a COMMENT anymore
  1378.      But: /* this is a comment now */ while this is not a comment
  1379.      =>But: /* this is a comment now */ while this is not a COMMENT
  1380.  
  1381.    Note how comments are copied to the output, much as it they were
  1382. quoted strings.  If you want the text inside a comment expanded, quote
  1383. the start comment delimiter.
  1384.  
  1385.    Calling `changecom' without any arguments disables the commenting
  1386. mechanism completely.
  1387.  
  1388.      define(`comment', `COMMENT')
  1389.      =>
  1390.      changecom
  1391.      =>
  1392.      # Not a comment anymore
  1393.      =># Not a COMMENT anymore
  1394.  
  1395. 
  1396. File: m4.info,  Node: M4wrap,  Prev: Changecom,  Up: Input Control
  1397.  
  1398. Saving input
  1399. ============
  1400.  
  1401.    It is possible to `save' some text until the end of the normal input
  1402. has been seen.  Text can be saved, to be read again by `m4' when the
  1403. normal input has been exhausted.  This feature is normally used to
  1404. initiate cleanup actions before normal exit, e.g., deleting temporary
  1405. files.
  1406.  
  1407.    To save input text, use the built-in `m4wrap':
  1408.  
  1409.      m4wrap(STRING, ...)
  1410.  
  1411. which stores STRING and the rest of the arguments in a safe place, to
  1412. be reread when end of input is reached.
  1413.  
  1414.      define(`cleanup', `This is the `cleanup' actions.
  1415.      ')
  1416.      =>
  1417.      m4wrap(`cleanup')
  1418.      =>
  1419.      This is the first and last normal input line.
  1420.      =>This is the first and last normal input line.
  1421.      ^D
  1422.      =>This is the cleanup actions.
  1423.  
  1424.    The saved input is only reread when the end of normal input is seen,
  1425. and not if `m4exit' is used to exit `m4'.
  1426.  
  1427.    It is safe to call `m4wrap' from saved text, but then the order in
  1428. which the saved text is reread is undefined.  If `m4wrap' is not used
  1429. recursively, the saved pieces of text are reread in the opposite order
  1430. in which they were saved (LIFO--last in, first out).
  1431.  
  1432. 
  1433. File: m4.info,  Node: File Inclusion,  Next: Diversions,  Prev: Input Control,  Up: Top
  1434.  
  1435. File inclusion
  1436. **************
  1437.  
  1438.    `m4' allows you to include named files at any point in the input.
  1439.  
  1440. * Menu:
  1441.  
  1442. * Include::                     Including named files
  1443. * Search Path::                 Searching for include files
  1444.  
  1445. 
  1446. File: m4.info,  Node: Include,  Next: Search Path,  Prev: File Inclusion,  Up: File Inclusion
  1447.  
  1448. Including named files
  1449. =====================
  1450.  
  1451.    There are two built-in macros in `m4' for including files:
  1452.  
  1453.      include(FILENAME)
  1454.      sinclude(FILENAME)
  1455.  
  1456. both of which cause the file named FILENAME to be read by `m4'.  When
  1457. the end of the file is reached, input is resumed from the previous
  1458. input file.
  1459.  
  1460.    The expansion of `include' and `sinclude' is therefore the contents
  1461. of FILENAME.
  1462.  
  1463.    It is an error for an `include'd file not to exist.  If you don't
  1464. want error messages about non-existent files, `sinclude' can be used to
  1465. include a file, if it exists, expanding to nothing if it does not.
  1466.  
  1467.      include(`no-such-file')
  1468.      =>
  1469.      error-->m4:30.include:2: can't open no-such-file: No such file or directory
  1470.      sinclude(`no-such-file')
  1471.      =>
  1472.  
  1473.    Assume in the following that the file `incl.m4' contains the lines:
  1474.      Include file start
  1475.      foo
  1476.      Include file end
  1477.  
  1478. Normally file inclusion is used to insert the contents of a file into
  1479. the input stream.  The contents of the file will be read by `m4' and
  1480. macro calls in the file will be expanded:
  1481.  
  1482.      define(`foo', `FOO')
  1483.      =>
  1484.      include(`incl.m4')
  1485.      =>Include file start
  1486.      =>FOO
  1487.      =>Include file end
  1488.      =>
  1489.  
  1490.    The fact that `include' and `sinclude' expand to the contents of the
  1491. file can be used to define macros that operate on entire files. Here is
  1492. an example, which defines `bar' to expand to the contents of `incl.m4':
  1493.  
  1494.      define(`bar', include(`incl.m4'))
  1495.      =>
  1496.      This is `bar':  >>>bar<<<
  1497.      =>This is bar:  >>>Include file start
  1498.      =>foo
  1499.      =>Include file end
  1500.      =><<<
  1501.  
  1502.    This use of `include' is not trivial, though, as files can contain
  1503. quotes, commas and parentheses, which can interfere with the way the
  1504. `m4' parser works.
  1505.  
  1506. 
  1507. File: m4.info,  Node: Search Path,  Prev: Include,  Up: File Inclusion
  1508.  
  1509. Searching for include files
  1510. ===========================
  1511.  
  1512.    GNU `m4' allows included files to be found in other directories than
  1513. the current working directory.
  1514.  
  1515.    If a file is not found in the current working directory, and the file
  1516. name is not absolute, the file will be looked for in a specified search
  1517. path.  First, the directories specified with the `-I' option will be
  1518. searched, in the order found on the command line.  Second, if the
  1519. `M4PATH' environment variable is set, it is expected to contain a
  1520. colon-separated list of directories, which will be searched in order.
  1521.  
  1522.    If the automatic search for include-files causes trouble, the `p'
  1523. debug flag (*note Debug Levels::.) can help isolate the problem.
  1524.  
  1525. 
  1526. File: m4.info,  Node: Diversions,  Next: Text handling,  Prev: File Inclusion,  Up: Top
  1527.  
  1528. Diverting and undiverting output
  1529. ********************************
  1530.  
  1531.    Diversions are a way of temporarily saving output.  The output of
  1532. `m4' can at any time be diverted to a temporary file, and be reinserted
  1533. into the output stream, "undiverted", again at a later time.
  1534.  
  1535.    Up to ten numbered diversions (numbered from 0 to 9) are supported in
  1536. `m4', of which diversion number 0 is the normal output stream.  The
  1537. number of available diversions can be increased with the `-N' option.
  1538.  
  1539. * Menu:
  1540.  
  1541. * Divert::                      Diverting output
  1542. * Undivert::                    Undiverting output
  1543. * Divnum::                      Diversion numbers
  1544. * Cleardiv::                    Discarding diverted text
  1545.  
  1546.