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