home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GCC258_3.LHA / gcc / info / m4.info-2 < prev    next >
Encoding:
GNU Info File  |  1993-12-07  |  35.0 KB  |  1,070 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: Divert,  Next: Undivert,  Prev: Diversions,  Up: Diversions
  23.  
  24. Diverting output
  25. ================
  26.  
  27.    Output is diverted using `divert':
  28.  
  29.      divert(opt NUMBER)
  30.  
  31. where NUMBER is the diversion to be used.  If NUMBER is left out, it is
  32. assumed to be zero.
  33.  
  34.    The expansion of `divert' is void.
  35.  
  36.    Diverted output, that hasn't been explicitly undiverted, will be
  37. undiverted when all the input has been processed.
  38.  
  39.      divert(1)
  40.      This text is diverted.
  41.      divert
  42.      =>
  43.      This text is not diverted.
  44.      =>This text is not diverted.
  45.      ^D
  46.      =>
  47.      =>This text is diverted.
  48.  
  49.    Several calls of `divert' with the same argument do not overwrite
  50. the previous diverted text, but append to it.
  51.  
  52.    If output is diverted to an non-existent diversion, it is simply
  53. discarded.  This can be used to suppress unwanted output.  A common
  54. example of unwanted output is the trailing newlines after macro
  55. definitions.  Here is how to avoid them.
  56.  
  57.      divert(-1)
  58.      define(`foo', `Macro `foo'.')
  59.      define(`bar', `Macro `bar'.')
  60.      divert
  61.      =>
  62.  
  63.    This is a common programming idiom in `m4'.
  64.  
  65. 
  66. File: m4.info,  Node: Undivert,  Next: Divnum,  Prev: Divert,  Up: Diversions
  67.  
  68. Undiverting output
  69. ==================
  70.  
  71.    Diverted text can be undiverted explicitly using the built-in
  72. `undivert':
  73.  
  74.      undivert(opt NUMBER, ...)
  75.  
  76. which undiverts the diversions given by the arguments, in the order
  77. given.  If no arguments are supplied, all diversions are undiverted, in
  78. numerical order.
  79.  
  80.    The expansion of `undivert' is void.
  81.  
  82.      divert(1)
  83.      This text is diverted.
  84.      divert
  85.      =>
  86.      This text is not diverted.
  87.      =>This text is not diverted.
  88.      undivert(1)
  89.      =>
  90.      =>This text is diverted.
  91.      =>
  92.  
  93.    Notice the last two blank lines.  One of them comes from the newline
  94. following `undivert', the other from the newline that followed the
  95. `divert'!  A diversion often starts with a blank line like this.
  96.  
  97.    When diverted text is undiverted, it is *not* reread by `m4', but
  98. rather copied directly to the current output, and it is therefore not
  99. an error to undivert into a diversion.
  100.  
  101.    When a diversion has been undiverted, the diverted text is discarded,
  102. and it is not possible to bring back diverted text more than once.
  103.  
  104.      divert(1)
  105.      This text is diverted first.
  106.      divert(0)undivert(1)dnl
  107.      =>
  108.      =>This text is diverted first.
  109.      undivert(1)
  110.      =>
  111.      divert(1)
  112.      This text is also diverted but not appended.
  113.      divert(0)undivert(1)dnl
  114.      =>
  115.      =>This text is also diverted but not appended.
  116.  
  117.    Attempts to undivert the current diversion are silently ignored.
  118.  
  119.    GNU `m4' allows named files to be undiverted.  Given a non-numeric
  120. argument, the contents of the file named will be copied, uninterpreted,
  121. to the current output.  This complements the built-in `include' (*note
  122. Include::.).  To illustrate the difference, assume the file `foo'
  123. contains the word `bar':
  124.  
  125.      define(`bar', `BAR')
  126.      =>
  127.      undivert(`foo')
  128.      =>bar
  129.      =>
  130.      include(`foo')
  131.      =>BAR
  132.      =>
  133.  
  134. 
  135. File: m4.info,  Node: Divnum,  Next: Cleardiv,  Prev: Undivert,  Up: Diversions
  136.  
  137. Diversion numbers
  138. =================
  139.  
  140.    The built-in `divnum':
  141.  
  142.      divnum
  143.  
  144. expands to the number of the current diversion.
  145.  
  146.      Initial divnum
  147.      =>Initial 0
  148.      divert(1)
  149.      Diversion one: divnum
  150.      divert(2)
  151.      Diversion two: divnum
  152.      divert
  153.      =>
  154.      ^D
  155.      =>
  156.      =>Diversion one: 1
  157.      =>
  158.      =>Diversion two: 2
  159.  
  160.    The last call of `divert' without argument is necessary, since the
  161. undiverted text would otherwise be diverted itself.
  162.  
  163. 
  164. File: m4.info,  Node: Cleardiv,  Prev: Divnum,  Up: Diversions
  165.  
  166. Discarding diverted text
  167. ========================
  168.  
  169.    Often it is not known, when output is diverted, whether the diverted
  170. text is actually needed.  Since all non-empty diversion are brought back
  171. when the end of input is seen, a method of discarding a diversion is
  172. needed.  If all diversions should be discarded, the easiest is to end
  173. the input to `m4' with `divert(-1)':
  174.  
  175.      divert(1)
  176.      Diversion one: divnum
  177.      divert(2)
  178.      Diversion two: divnum
  179.      divert(-1)
  180.      ^D
  181.  
  182. No output is produced at all.
  183.  
  184.    Clearing selected diversions can be done with the following macro:
  185.  
  186.      define(`cleardivert',
  187.      `pushdef(`_num', divnum)divert(-1)undivert($@)divert(_num)popdef(`_num')')
  188.      =>
  189.  
  190.    It is called just like `undivert', but the effect is to clear the
  191. diversions, given by the arguments.  (This macro has a nasty bug!  You
  192. should try to see if you can find it and correct it.)
  193.  
  194. 
  195. File: m4.info,  Node: Text handling,  Next: Arithmetic,  Prev: Diversions,  Up: Top
  196.  
  197. Macros for text handling
  198. ************************
  199.  
  200.    There are a number of built-ins in `m4' for manipulating text in
  201. various ways, extracting substrings, searching, substituting, and so on.
  202.  
  203. * Menu:
  204.  
  205. * Len::                         Calculating length of strings
  206. * Index::                       Searching for substrings
  207. * Regexp::                      Searching for regular expressions
  208. * Substr::                      Extracting substrings
  209. * Translit::                    Translating characters
  210. * Patsubst::                    Substituting text by regular expression
  211. * Format::                      Formatting strings (printf-like)
  212.  
  213. 
  214. File: m4.info,  Node: Len,  Next: Index,  Prev: Text handling,  Up: Text handling
  215.  
  216. Calculating length of strings
  217. =============================
  218.  
  219.    The length of a string can be calculated by `len':
  220.  
  221.      len(STRING)
  222.  
  223. which expands to the length of STRING, as a decimal number.
  224.  
  225.      len()
  226.      =>0
  227.      len(`abcdef')
  228.      =>6
  229.  
  230. 
  231. File: m4.info,  Node: Index,  Next: Regexp,  Prev: Len,  Up: Text handling
  232.  
  233. Searching for substrings
  234. ========================
  235.  
  236.    Searching for substrings is done with `index':
  237.  
  238.      index(STRING, SUBSTRING)
  239.  
  240. which expands to the index of the first occurrence of SUBSTRING in
  241. STRING.  The first character in STRING has index 0.  If SUBSTRING does
  242. not occur in STRING, `index' expands to `-1'.
  243.  
  244.      index(`gnus, gnats, and armadillos', `nat')
  245.      =>7
  246.      index(`gnus, gnats, and armadillos', `dag')
  247.      =>-1
  248.  
  249. 
  250. File: m4.info,  Node: Regexp,  Next: Substr,  Prev: Index,  Up: Text handling
  251.  
  252. Searching for regular expressions
  253. =================================
  254.  
  255.    Searching for regular expressions is done with the built-in `regexp':
  256.  
  257.      regexp(STRING, REGEXP, opt REPLACEMENT)
  258.  
  259. which searches for REGEXP in STRING.  The syntax for regular
  260. expressions is the same as in GNU Emacs.  *Note Syntax of Regular
  261. Expressions: (emacs)Regexps.
  262.  
  263.    If REPLACEMENT is omitted, `regexp' expands to the index of the
  264. first match of REGEXP in STRING.  If REGEXP does not match anywhere in
  265. STRING, it expands to -1.
  266.  
  267.      regexp(`GNUs not Unix', `\<[a-z]\w+')
  268.      =>5
  269.      regexp(`GNUs not Unix', `\<Q\w*')
  270.      =>-1
  271.  
  272.    If REPLACEMENT is supplied, `regexp' changes the expansion to this
  273. argument, with `\&' substituted by STRING, and `\N' substituted by the
  274. text matched by the Nth parenthesized sub-expression of REGEXP, `\0'
  275. being the text the entire regular expression matched.
  276.  
  277.      regexp(`GNUs not Unix', `\w\(\w+\)$', `*** \0 *** \1 ***')
  278.      =>*** Unix *** nix ***
  279.  
  280. 
  281. File: m4.info,  Node: Substr,  Next: Translit,  Prev: Regexp,  Up: Text handling
  282.  
  283. Extracting substrings
  284. =====================
  285.  
  286.    Substrings are extracted with `substr':
  287.  
  288.      substr(STRING, FROM, opt LENGTH)
  289.  
  290. which expands to the substring of STRING, which starts at index FROM,
  291. and extends for LENGTH characters, or to the end of STRING, if LENGTH
  292. is omitted.  The starting index of a string is always 0.
  293.  
  294.      substr(`gnus, gnats, and armadillos', 6)
  295.      =>gnats, and armadillos
  296.      substr(`gnus, gnats, and armadillos', 6, 5)
  297.      =>gnats
  298.  
  299. 
  300. File: m4.info,  Node: Translit,  Next: Patsubst,  Prev: Substr,  Up: Text handling
  301.  
  302. Translating characters
  303. ======================
  304.  
  305.    Character translation is done with `translit':
  306.  
  307.      translit(STRING, CHARS, REPLACEMENT)
  308.  
  309. which expands to STRING, with each character that occurs in CHARS
  310. translated into the character from REPLACEMENT with the same index.
  311.  
  312.    If REPLACEMENT is shorter than CHARS, the excess characters are
  313. deleted from the expansion.  If REPLACEMENT is omitted, all characters
  314. in STRING, that are present in CHARS are deleted from the expansion.
  315.  
  316.    Both CHARS and REPLACEMENT can contain character-ranges, e.g., `a-z'
  317. (meaning all lowercase letters) or `0-9' (meaning all digits).  To
  318. include a dash `-' in CHARS or REPLACEMENT, place it first or last.
  319.  
  320.    It is not an error for the last character in the range to be `larger'
  321. than the first.  In that case, the range runs backwards, i.e., `9-0'
  322. means the string `9876543210'.
  323.  
  324.      translit(`GNUs not Unix', `A-Z')
  325.      =>s not nix
  326.      translit(`GNUs not Unix', `a-z', `A-Z')
  327.      =>GNUS NOT UNIX
  328.      translit(`GNUs not Unix', `A-Z', `z-a')
  329.      =>tmfs not fnix
  330.  
  331.    The first example deletes all uppercase letters, the second converts
  332. lowercase to uppercase, and the third `mirrors' all uppercase letters,
  333. while converting them to lowercase.  The two first cases are by far the
  334. most common.
  335.  
  336. 
  337. File: m4.info,  Node: Patsubst,  Next: Format,  Prev: Translit,  Up: Text handling
  338.  
  339. Substituting text by regular expression
  340. =======================================
  341.  
  342.    Global substitution in a string is done by `patsubst':
  343.  
  344.      patsubst(STRING, REGEXP, opt REPLACEMENT)
  345.  
  346. which searches STRING for matches of REGEXP, and substitutes
  347. REPLACEMENT for each match.  The syntax for regular expressions is the
  348. same as in GNU Emacs.
  349.  
  350.    The parts of STRING that are not covered by any match of REGEXP are
  351. copied to the expansion.  Whenever a match is found, the search
  352. proceeds from the end of the match, so a character from STRING will
  353. never be substituted twice.  If REGEXP matches a string of zero length,
  354. the start position for the search is incremented, to avoid infinite
  355. loops.
  356.  
  357.    When a replacement is to be made, REPLACEMENT is inserted into the
  358. expansion, with `\&' substituted by STRING, and `\N' substituted by the
  359. text matched by the Nth parenthesized sub-expression of REGEXP, `\0'
  360. being the text the entire regular expression matched.
  361.  
  362.    The REPLACEMENT argument can be omitted, in which case the text
  363. matched by REGEXP is deleted.
  364.  
  365.      patsubst(`GNUs not Unix', `^', `OBS: ')
  366.      =>OBS: GNUs not Unix
  367.      patsubst(`GNUs not Unix', `\<', `OBS: ')
  368.      =>OBS: GNUs OBS: not OBS: Unix
  369.      patsubst(`GNUs not Unix', `\w*', `(\0)')
  370.      =>(GNUs)() (not)() (Unix)
  371.      patsubst(`GNUs not Unix', `\w+', `(\0)')
  372.      =>(GNUs) (not) (Unix)
  373.      patsubst(`GNUs not Unix', `[A-Z][a-z]+')
  374.      =>GN not
  375.  
  376.    Here is a slightly more realistic example, which capitalizes
  377. individual word or whole sentences, by substituting calls of the macros
  378. `upcase' and `downcase' into the strings.
  379.  
  380.      define(`upcase', `translit(`$*', `a-z', `A-Z')')dnl
  381.      define(`downcase', `translit(`$*', `A-Z', `a-z')')dnl
  382.      define(`capitalize1',
  383.           `regexp(`$1', `^\(\w\)\(\w*\)', `upcase(`\1')`'downcase(`\2')')')dnl
  384.      define(`capitalize',
  385.           `patsubst(`$1', `\w+', `capitalize1(`\0')')')dnl
  386.      capitalize(`GNUs not Unix')
  387.      =>Gnus Not Unix
  388.  
  389. 
  390. File: m4.info,  Node: Format,  Prev: Patsubst,  Up: Text handling
  391.  
  392. Formatted output
  393. ================
  394.  
  395.    Formatted output can be made with `format':
  396.  
  397.      format(FORMAT-STRING, ...)
  398.  
  399. which works much like the C function `printf'.  The first argument is a
  400. format string, which can contain `%' specifications, and the expansion
  401. of `format' is the formatted string.
  402.  
  403.    Its use is best described by a few examples:
  404.  
  405.      define(`foo', `The brown fox jumped over the lazy dog')
  406.      =>
  407.      format(`The string "%s" is %d characters long', foo, len(foo))
  408.      =>The string "The brown fox jumped over the lazy dog" is 38 characters long
  409.  
  410.    Using the `forloop' macro defined in *Note Loops::, this example
  411. shows how `format' can be used to produce tabular output.
  412.  
  413.      forloop(`i', 1, 10, `format(`%6d squared is %10d
  414.      ', i, eval(i^2))')
  415.      =>     1 squared is        1
  416.      =>     2 squared is        4
  417.      =>     3 squared is        9
  418.      =>     4 squared is       16
  419.      =>     5 squared is       25
  420.      =>     6 squared is       36
  421.      =>     7 squared is       49
  422.      =>     8 squared is       64
  423.      =>     9 squared is       81
  424.      =>    10 squared is      100
  425.  
  426.    The built-in `format' is modeled after the ANSI C `printf' function,
  427. and supports the normal `%' specifiers: `c', `s', `d', `o', `x', `X',
  428. `u', `e', `E' and `f'; it supports field widths and precisions, and the
  429. modifiers `+', `-', ` ', `0', `#', `h' and `l'.  For more details on
  430. the functioning of `printf', see the C Library Manual.
  431.  
  432. 
  433. File: m4.info,  Node: Arithmetic,  Next: Unix commands,  Prev: Text handling,  Up: Top
  434.  
  435. Macros for doing arithmetic
  436. ***************************
  437.  
  438.    Integer arithmetic is included in `m4', with a C-like syntax.  As
  439. convenient shorthands, there are built-ins for simple increment and
  440. decrement operations.
  441.  
  442. * Menu:
  443.  
  444. * Incr::                        Decrement and increment operators
  445. * Eval::                        Evaluating integer expressions
  446.  
  447. 
  448. File: m4.info,  Node: Incr,  Next: Eval,  Prev: Arithmetic,  Up: Arithmetic
  449.  
  450. Decrement and increment operators
  451. =================================
  452.  
  453.    Increment and decrement of integers are supported using the built-ins
  454. `incr' and `decr':
  455.  
  456.      incr(NUMBER)
  457.      decr(NUMBER)
  458.  
  459. which expand to the numerical value of NUMBER, incremented, or
  460. decremented, respectively, by one.
  461.  
  462.      incr(4)
  463.      =>5
  464.      decr(7)
  465.      =>6
  466.  
  467. 
  468. File: m4.info,  Node: Eval,  Prev: Incr,  Up: Arithmetic
  469.  
  470. Evaluating integer expressions
  471. ==============================
  472.  
  473.    Integer expressions are evaluated with `eval':
  474.  
  475.      eval(EXPRESSION, opt RADIX, opt WIDTH)
  476.  
  477. which expands to the value of EXPRESSION.
  478.  
  479.    Expressions can contain the following operators, listed in order of
  480. decreasing precedence.
  481.  
  482. `-'
  483.      Unany minus
  484.  
  485. `**  ^'
  486.      Exponentiation
  487.  
  488. `*  /  %'
  489.      Multiplication, division and modulo
  490.  
  491. `+  -'
  492.      Addition and subtraction
  493.  
  494. `==  !=  >  >=  <  <='
  495.      Relational operators
  496.  
  497. `!'
  498.      Logical negation
  499.  
  500. `&'
  501.      Bitwise and
  502.  
  503. `|'
  504.      Bitwise or
  505.  
  506. `&&'
  507.      Logical and
  508.  
  509. `||'
  510.      Logical or
  511.  
  512.    All operators, except exponentiation, are left associative.
  513.  
  514.    Numbers can be given in decimal, octal (starting with `0'), or
  515. hexadecimal (starting with `0x').
  516.  
  517.    Parentheses may be used to group subexpressions whenever needed. 
  518. For the relational operators, a true relation returns `1', and a false
  519. relation return `0'.
  520.  
  521.    Here are a few examples of use of `eval'.
  522.  
  523.      eval(-3 * 5)
  524.      =>-15
  525.      eval(index(`Hello world', `llo') >= 0)
  526.      =>1
  527.      define(`square', `eval(($1)^2)')
  528.      =>
  529.      square(9)
  530.      =>81
  531.      square(square(5)+1)
  532.      =>676
  533.      define(`foo', `666')
  534.      =>
  535.      eval(`foo'/6)
  536.      error-->m4:51.eval:14: bad expression in eval: foo/6
  537.      =>
  538.      eval(foo/6)
  539.      =>111
  540.  
  541.    As the second to last example shows, `eval' does not handle macro
  542. names, even if they expand to a valid expression (or part of a valid
  543. expression).  Therefore all macros must be expanded before they are
  544. passed to `eval'.
  545.  
  546.    If RADIX is specified, it specifies the radix to be used in the
  547. expansion.  The default radix is 10.  The result of `eval' is always
  548. taken to be signed.  The WIDTH argument specifies a minimum output
  549. width.  The result is zero-padded to extend the expansion to the
  550. requested width.
  551.  
  552.      eval(666, 10)
  553.      =>666
  554.      eval(666, 11)
  555.      =>556
  556.      eval(666, 6)
  557.      =>3030
  558.      eval(666, 6, 10)
  559.      =>0000003030
  560.      eval(-666, 6, 10)
  561.      =>-000003030
  562.  
  563.    Please take note that RADIX cannot be larger than 36 in the current
  564. implemention.  Which characters can be used as digits, if the radix is
  565. larger than 36?  Currently any radix larger than 36 are rejected.
  566.  
  567. 
  568. File: m4.info,  Node: Unix commands,  Next: Miscellaneous,  Prev: Arithmetic,  Up: Top
  569.  
  570. Running Unix commands
  571. *********************
  572.  
  573.    There are a few built-in macros in `m4' that allow you to run Unix
  574. commands from within `m4'.
  575.  
  576. * Menu:
  577.  
  578. * Syscmd::                      Executing simple commands
  579. * Esyscmd::                     Reading the output of commands
  580. * Sysval::                      Exit codes
  581. * Maketemp::                    Making names for temporary files
  582.  
  583. 
  584. File: m4.info,  Node: Syscmd,  Next: Esyscmd,  Prev: Unix commands,  Up: Unix commands
  585.  
  586. Executing simple commands
  587. =========================
  588.  
  589.    Any shell command can be executed, using `syscmd':
  590.  
  591.      syscmd(SHELL-COMMAND)
  592.  
  593. which executes SHELL-COMMAND as a shell command.
  594.  
  595.    The expansion of `syscmd' is void.
  596.  
  597.    The expansion is *not* the output from the command!  Instead the
  598. standard input, output and error of the command are the same as those of
  599. `m4'.  This means that output or error messages from the commands are
  600. not read by `m4', and might get mixed up with the normal output from
  601. `m4'.  This can produce unexpected results.  It is therefore a good
  602. habit to always redirect the input and output of shell commands used
  603. with `syscmd'.
  604.  
  605. 
  606. File: m4.info,  Node: Esyscmd,  Next: Sysval,  Prev: Syscmd,  Up: Unix commands
  607.  
  608. Reading the output of commands
  609. ==============================
  610.  
  611.    If you want `m4' to read the output of a Unix command, use `esyscmd':
  612.  
  613.      esyscmd(SHELL-COMMAND)
  614.  
  615. which expands to the standard output of the shell command SHELL-COMMAND.
  616.  
  617.    The error output of SHELL-COMMAND is not a part of the expansion. It
  618. will appear along with the error output of `m4'.  Assume you are
  619. positioned into the `checks' directory of GNU `m4' distribution, then:
  620.  
  621.      define(`vice', `esyscmd(grep Vice COPYING)')
  622.      =>
  623.      vice
  624.      =>  Ty Coon, President of Vice
  625.      =>
  626.  
  627.    Note how the expansion of `esyscmd' has a trailing newline.
  628.  
  629. 
  630. File: m4.info,  Node: Sysval,  Next: Maketemp,  Prev: Esyscmd,  Up: Unix commands
  631.  
  632. Exit codes
  633. ==========
  634.  
  635.    To see whether a shell command succeeded, use `sysval':
  636.  
  637.      sysval
  638.  
  639. which expands to the exit status of the last shell command run with
  640. `syscmd' or `esyscmd'.
  641.  
  642.      syscmd(`false')
  643.      =>
  644.      ifelse(sysval, 0, zero, non-zero)
  645.      =>non-zero
  646.      syscmd(`true')
  647.      =>
  648.      sysval
  649.      =>0
  650.  
  651. 
  652. File: m4.info,  Node: Maketemp,  Prev: Sysval,  Up: Unix commands
  653.  
  654. Making names for temporary files
  655. ================================
  656.  
  657.    Commands specified to `syscmd' or `esyscmd' might need a temporary
  658. file, for output or for some other purpose. There is a built-in macro,
  659. `maketemp', for making temporary file names:
  660.  
  661.      maketemp(TEMPLATE)
  662.  
  663. which expands to a name of a non-existent file, made from the string
  664. TEMPLATE, which should end with the string `XXXXXX'.  The six `X''s are
  665. then replaced, usually with something that includes the process id of
  666. the `m4' process, in order to make the filename unique.
  667.  
  668.      maketemp(`/tmp/fooXXXXXX')
  669.      =>/tmp/fooa07346
  670.      maketemp(`/tmp/fooXXXXXX')
  671.      =>/tmp/fooa07346
  672.  
  673.    As seen in the example, several calls of `maketemp' might expand to
  674. the same string, since the selection criteria is whether the file exists
  675. or not.  If a file has not been created before the next call, the two
  676. macro calls might expand to the same name.
  677.  
  678. 
  679. File: m4.info,  Node: Miscellaneous,  Next: Compatibility,  Prev: Unix commands,  Up: Top
  680.  
  681. Miscellaneous built-in macros
  682. *****************************
  683.  
  684.    This chapter describes various built-ins, that don't really belong in
  685. any of the previous chapters.
  686.  
  687. * Menu:
  688.  
  689. * Errprint::                    Printing error messages
  690. * M4exit::                      Exiting from m4
  691.  
  692. 
  693. File: m4.info,  Node: Errprint,  Next: M4exit,  Prev: Miscellaneous,  Up: Miscellaneous
  694.  
  695. Printing error messages
  696. =======================
  697.  
  698.    You can print error messages using `errprint':
  699.  
  700.      errprint(MESSAGE, ...)
  701.  
  702. which simply prints MESSAGE and the rest of the arguments on the
  703. standard error output.
  704.  
  705.    The expansion of `errprint' is void.
  706.  
  707.      errprint(`Illegal arguments to forloop
  708.      ')
  709.      error-->Illegal arguments to forloop
  710.      =>
  711.  
  712.    A trailing newline is *not* printed automatically, so it must be
  713. supplied as part of the argument, as in the example.
  714.  
  715.    To make it possible to specify the location of the error, two
  716. utility built-ins exist:
  717.  
  718.      __file__
  719.      __line__
  720.  
  721. which expands to the quoted name of the current input file, and the
  722. current input line number in that file.
  723.  
  724.      errprint(`m4:'__file__:__line__: `Input error
  725.      ')
  726.      error-->m4:56.errprint:2: Input error
  727.      =>
  728.  
  729. 
  730. File: m4.info,  Node: M4exit,  Prev: Errprint,  Up: Miscellaneous
  731.  
  732. Exiting from `m4'
  733. =================
  734.  
  735.    If you need to exit from `m4' before the entire input has been read,
  736. you can use `m4exit':
  737.  
  738.      m4exit(opt CODE)
  739.  
  740. which causes `m4' to exit, with exit code CODE.  If CODE is left out,
  741. the exit code is zero.
  742.  
  743.      define(`fatal_error', `errprint(`m4: '__file__: __line__`: fatal error: $*
  744.      ')m4exit(1)')
  745.      =>
  746.      fatal_error(`This is a BAD one, buster')
  747.      error-->m4: 57.m4exit: 5: fatal error: This is a BAD one, buster
  748.  
  749.    After this macro call, `m4' will exit with exit code 1.  This macro
  750. is only intended for error exits, since the normal exit procedures are
  751. not followed, e.g., diverted text is not undiverted, and saved text
  752. (*note M4wrap::.) is not reread.
  753.  
  754. 
  755. File: m4.info,  Node: Compatibility,  Next: Concept Index,  Prev: Miscellaneous,  Up: Top
  756.  
  757. Compatibility with other versions of `m4'
  758. *****************************************
  759.  
  760.    This chapter describes the differences between this implementation of
  761. `m4', and the implementation found under Unix, notably System V,
  762. Release 3.
  763.  
  764. * Menu:
  765.  
  766. * Extensions::                  Extensions in GNU m4
  767. * Incompatibilities::           Facilities in System V m4 not in GNU m4
  768. * Other Incompat::              Other incompatibilities
  769.  
  770. 
  771. File: m4.info,  Node: Extensions,  Next: Incompatibilities,  Prev: Compatibility,  Up: Compatibility
  772.  
  773. Extensions in GNU `m4'
  774. ======================
  775.  
  776.    This version of `m4' contains a few facilities, that do not exist in
  777. System V `m4'.  These extra facilities are all suppressed by using the
  778. `-G' command line option, unless overridden by other command line
  779. options.
  780.  
  781.    * In the `$'N notation for macro arguments, N can contain several
  782.      digits, while the System V `m4' only accepts one digit. This
  783.      allows macros in GNU `m4' to take any number of arguments, and not
  784.      only nine (*note Arguments::.).
  785.  
  786.    * Files included with `include' and `sinclude' are sought in a user
  787.      specified search path, if they are not found in the working
  788.      directory.  The search path is specified by the `-I' option and the
  789.      `M4PATH' environment variable (*note Search Path::.).
  790.  
  791.    * Arguments to `undivert' can be non-numeric, in which case the named
  792.      file will be included uninterpreted in the output (*note
  793.      Undivert::.).
  794.  
  795.    * Formatted output is supported through the `format' built-in, which
  796.      is modeled after the C library function `printf' (*note Format::.).
  797.  
  798.    * Searches and text substitution through regular expressions are
  799.      supported by the `regexp' and `patsubst' built-ins (*note
  800.      Regexp::. and *Note Patsubst::).
  801.  
  802.    * The output of shell commands can be read into `m4' with `esyscmd'
  803.      (*note Esyscmd::.).
  804.  
  805.    * There is indirect access to any built-in macro with `builtin'
  806.      (*note Builtin::.).
  807.  
  808.    * Macros can be called indirectly through `indir' (*note Indir::.).
  809.  
  810.    * The name of the current input file and the current input line
  811.      number are accessible through the built-ins `__file__' and
  812.      `__line__' (*note Errprint::.).
  813.  
  814.    * The format of the output from `dumpdef' and macro tracing can be
  815.      controlled with `debugmode' (*note Debug Levels::.).
  816.  
  817.    * The destination of trace and debug output can be controlled with
  818.      `debugfile' (*note Debug Output::.).
  819.  
  820.    In addition to the above extensions, GNU `m4' implements the
  821. following command line options: `-V', `-d', `-l', `-o', `-N', `-I' and
  822. `-t'.  For a description of these options, *note Invoking m4::.
  823.  
  824.    Also, the debugging and tracing facilities in GNU `m4' are much more
  825. extensive than in most other versions of `m4'.
  826.  
  827. 
  828. File: m4.info,  Node: Incompatibilities,  Next: Other Incompat,  Prev: Extensions,  Up: Compatibility
  829.  
  830. Facilities in System V `m4' not in GNU `m4'
  831. ===========================================
  832.  
  833.    The version of `m4' from System V contains a few facilities that
  834. have not been implemented in GNU `m4' yet.
  835.  
  836.    * System V `m4' supports multiple arguments to `defn'.  This is not
  837.      implemented in GNU `m4'.  Its usefulness is unclear to me.
  838.  
  839. 
  840. File: m4.info,  Node: Other Incompat,  Prev: Incompatibilities,  Up: Compatibility
  841.  
  842. Other incompatibilities
  843. =======================
  844.  
  845.    There are a few other incompatibilities between this implementation
  846. of `m4', and the System V version.
  847.  
  848.    * GNU `m4' implements sync lines differently from System V `m4',
  849.      when text is being diverted.  GNU `m4' outputs the sync lines when
  850.      the text is being diverted, and System V `m4' when the diverted
  851.      text is being brought back.
  852.  
  853.      The problem is which lines and filenames should be attached to
  854.      text that is being, or has been, diverted.  System V `m4' regards
  855.      all the diverted text as being generated by the source line
  856.      containing the `undivert' call, whereas GNU `m4' regards the
  857.      diverted text as being generated at the time it is diverted.
  858.  
  859.      Which is right?  I expect the sync line option to be used mostly
  860.      when using `m4' as a front end to a compiler.  If a diverted line
  861.      causes a compiler error, I believe that the error messages should
  862.      refer to the place where the diversion were made, and not where it
  863.      was inserted again.  Comments anyone?
  864.  
  865.      Anyway, GNU `m4''s approach causes a serious bug, if calls to
  866.      undivert aren't alone on the input line.  See the file
  867.      `examples/divert.m4' for a demonstration of the bug.  I don't feel
  868.      it is acceptable to insert newlines in the output the user hasn't
  869.      asked for.
  870.  
  871.    * GNU `m4' without `-G' option will define the macro `__gnu__' to
  872.      expand to the empty string.
  873.  
  874.      On Unix systems, GNU `m4' without the `-G' option will define the
  875.      macro `__unix__', otherwise the macro `unix'.  Both will expand to
  876.      the empty string.
  877.  
  878. 
  879. File: m4.info,  Node: Concept Index,  Next: Macro Index,  Prev: Compatibility,  Up: Top
  880.  
  881. Concept index
  882. *************
  883.  
  884. * Menu:
  885.  
  886. * Arguments to macros:                  Macro Arguments.
  887. * Arguments to macros:                  Arguments.
  888. * Arguments to macros, special:         Pseudo Arguments.
  889. * Arguments, quoted macro:              Quoting Arguments.
  890. * Arithmetic:                           Arithmetic.
  891. * Builtins, indirect call of:           Builtin.
  892. * Call of built-ins, indirect:          Builtin.
  893. * Call of macros, indirect:             Indir.
  894. * Changing comment delimiters:          Changecom.
  895. * Changing the quote delimiters:        Changequote.
  896. * Characters, translating:              Translit.
  897. * Command line, filenames on the:       Invoking m4.
  898. * Command line, macro definitions on the: Invoking m4.
  899. * Command line, options:                Invoking m4.
  900. * Commands, exit code from Unix:        Sysval.
  901. * Commands, running Unix:               Unix commands.
  902. * Comment delimiters, changing:         Changecom.
  903. * Comments:                             Comments.
  904. * Comments, copied to output:           Changecom.
  905. * Comparing strings:                    Ifelse.
  906. * Compatibility:                        Compatibility.
  907. * Conditionals:                         Ifdef.
  908. * Controlling debugging output:         Debug Levels.
  909. * Counting loops:                       Loops.
  910. * Debugging output, controlling:        Debug Levels.
  911. * Debugging output, saving:             Debug Output.
  912. * Decrement operator:                   Incr.
  913. * Defining new macros:                  Definitions.
  914. * Definitions, displaying macro:        Dumpdef.
  915. * Deleting macros:                      Undefine.
  916. * Deleting whitespace in input:         Dnl.
  917. * Discarding diverted text:             Cleardiv.
  918. * Displaying macro definitions:         Dumpdef.
  919. * Diversion numbers:                    Divnum.
  920. * Diverted text, discarding:            Cleardiv.
  921. * Diverting output to files:            Divert.
  922. * Error messages, printing:             Errprint.
  923. * Evaluation, of integer expressions:   Eval.
  924. * Executing Unix commands:              Unix commands.
  925. * Exit code from Unix commands:         Sysval.
  926. * Exiting from m4:                      M4exit.
  927. * Expansion of macros:                  Macro expansion.
  928. * Expansion, tracing macro:             Trace.
  929. * Expressions, evaluation of integer:   Eval.
  930. * Extracting substrings:                Substr.
  931. * File inclusion:                       File Inclusion.
  932. * File inclusion:                       Undivert.
  933. * Filenames, on the command line:       Invoking m4.
  934. * Files, diverting output to:           Divert.
  935. * Files, names of temporary:            Maketemp.
  936. * Forloops:                             Loops.
  937. * Formatted output:                     Format.
  938. * GNU extensions:                       Arguments.
  939. * GNU extensions:                       Search Path.
  940. * GNU extensions:                       Format.
  941. * GNU extensions:                       Builtin.
  942. * GNU extensions:                       Regexp.
  943. * GNU extensions:                       Esyscmd.
  944. * GNU extensions:                       Undivert.
  945. * GNU extensions:                       Debug Output.
  946. * GNU extensions:                       Extensions.
  947. * GNU extensions:                       Indir.
  948. * GNU extensions:                       Patsubst.
  949. * GNU extensions:                       Debug Levels.
  950. * Included files, search path for:      Search Path.
  951. * Inclusion, of files:                  File Inclusion.
  952. * Inclusion, of files:                  Undivert.
  953. * Increment operator:                   Incr.
  954. * Indirect call of built-ins:           Builtin.
  955. * Indirect call of macros:              Indir.
  956. * Input tokens:                         Syntax.
  957. * Input, saving:                        M4wrap.
  958. * Integer arithmetic:                   Arithmetic.
  959. * Integer expression evaluation:        Eval.
  960. * Length of strings:                    Len.
  961. * Loops:                                Loops.
  962. * Loops, counting:                      Loops.
  963. * Macro definitions, on the command line: Invoking m4.
  964. * Macro expansion, tracing:             Trace.
  965. * Macro invocation:                     Invocation.
  966. * Macros, arguments to:                 Macro Arguments.
  967. * Macros, arguments to:                 Arguments.
  968. * Macros, displaying definitions:       Dumpdef.
  969. * Macros, expansion of:                 Macro expansion.
  970. * Macros, how to define new:            Definitions.
  971. * Macros, how to delete:                Undefine.
  972. * Macros, how to rename:                Defn.
  973. * Macros, indirect call of:             Indir.
  974. * Macros, quoted arguments to:          Quoting Arguments.
  975. * Macros, recursive:                    Loops.
  976. * Macros, special arguments to:         Pseudo Arguments.
  977. * Macros, temporary redefinition of:    Pushdef.
  978. * Messages, printing error:             Errprint.
  979. * Multibranches:                        Ifelse.
  980. * Names:                                Names.
  981. * Options, command line:                Invoking m4.
  982. * Output, diverting to files:           Divert.
  983. * Output, formatted:                    Format.
  984. * Output, saving debugging:             Debug Output.
  985. * Pattern substitution:                 Patsubst.
  986. * Printing error messages:              Errprint.
  987. * Quote delimiters, changing the:       Changequote.
  988. * Quoted macro arguments:               Quoting Arguments.
  989. * Quoted string:                        Quoted strings.
  990. * Recursive macros:                     Loops.
  991. * Redefinition of macros, temporary:    Pushdef.
  992. * Regular expressions:                  Patsubst.
  993. * Regular expressions:                  Regexp.
  994. * Renaming macros:                      Defn.
  995. * Running Unix commands:                Unix commands.
  996. * Saving debugging output:              Debug Output.
  997. * Saving input:                         M4wrap.
  998. * Search path for included files:       Search Path.
  999. * Special arguments to macros:          Pseudo Arguments.
  1000. * Strings, length of:                   Len.
  1001. * Substitution by regular expression:   Patsubst.
  1002. * Substrings, extracting:               Substr.
  1003. * Temporary filenames:                  Maketemp.
  1004. * Temporary redefinition of macros:     Pushdef.
  1005. * Tokens:                               Syntax.
  1006. * Tracing macro expansion:              Trace.
  1007. * Translating characters:               Translit.
  1008. * Undefining macros:                    Undefine.
  1009. * Unix commands, exit code from:        Sysval.
  1010. * Unix commands, running:               Unix commands.
  1011.  
  1012. 
  1013. File: m4.info,  Node: Macro Index,  Prev: Concept Index,  Up: Top
  1014.  
  1015. Macro index
  1016. ***********
  1017.  
  1018.    References are exclusively to the places where a built-in is
  1019. introduced the first time.  Names starting and ending with `__' have
  1020. these characters removed in the index.
  1021.  
  1022. * Menu:
  1023.  
  1024. * builtin:                              Builtin.
  1025. * changecom:                            Changecom.
  1026. * changequote:                          Changequote.
  1027. * debugfile:                            Debug Output.
  1028. * debugmode:                            Debug Levels.
  1029. * decr:                                 Incr.
  1030. * define:                               Define.
  1031. * defn:                                 Defn.
  1032. * divert:                               Divert.
  1033. * divnum:                               Divnum.
  1034. * dnl:                                  Dnl.
  1035. * dumpdef:                              Dumpdef.
  1036. * errprint:                             Errprint.
  1037. * esyscmd:                              Esyscmd.
  1038. * eval:                                 Eval.
  1039. * file:                                 Errprint.
  1040. * format:                               Format.
  1041. * gnu:                                  Other Incompat.
  1042. * ifdef:                                Ifdef.
  1043. * ifelse:                               Ifelse.
  1044. * include:                              Include.
  1045. * incr:                                 Incr.
  1046. * index:                                Index.
  1047. * indir:                                Indir.
  1048. * len:                                  Len.
  1049. * line:                                 Errprint.
  1050. * m4exit:                               M4exit.
  1051. * m4wrap:                               M4wrap.
  1052. * maketemp:                             Maketemp.
  1053. * patsubst:                             Patsubst.
  1054. * popdef:                               Pushdef.
  1055. * pushdef:                              Pushdef.
  1056. * regexp:                               Regexp.
  1057. * shift:                                Loops.
  1058. * sinclude:                             Include.
  1059. * substr:                               Substr.
  1060. * syscmd:                               Syscmd.
  1061. * sysval:                               Sysval.
  1062. * traceoff:                             Trace.
  1063. * traceon:                              Trace.
  1064. * translit:                             Translit.
  1065. * undefine:                             Undefine.
  1066. * undivert:                             Undivert.
  1067. * unix:                                 Other Incompat.
  1068.  
  1069.  
  1070.