home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / DOC / m4.doc < prev    next >
Text File  |  2009-11-06  |  20KB  |  1,386 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.                    The M4 Macro Processor
  10.  
  11.  
  12.  
  13.  
  14.                      Brian W. Kernighan
  15.  
  16.  
  17.  
  18.                      Dennis M. Ritchie
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.                           _A_B_S_T_R_A_C_T
  26.  
  27.  
  28.  
  29.           M4 is a macro processor  available  on  UNIX|-
  30.  
  31.      and GCOS.  Its primary use has been as a front end
  32.  
  33.      for Ratfor for  those  cases  where  parameterless
  34.  
  35.      macros  are  not adequately powerful.  It has also
  36.  
  37.      been used for languages  as  disparate  as  C  and
  38.  
  39.      Cobol.   M4  is particularly suited for functional
  40.  
  41.      languages like Fortran, PL/I and  C  since  macros
  42.  
  43.      are specified in a functional notation.
  44.  
  45.  
  46.           M4 provides features  seldom  found  even  in
  47.  
  48.      much larger macro processors, including
  49.  
  50.  
  51.        o+  arguments
  52.  
  53.  
  54.        o+  condition testing
  55.  
  56.  
  57.        o+  arithmetic capabilities
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.                            - ii -
  70.  
  71.  
  72.        o+  string and substring functions
  73.  
  74.  
  75.        o+  file manipulation
  76.  
  77.  
  78.  
  79.           This paper is a user's manual for M4.
  80.  
  81.  
  82.  
  83.  
  84. July 1, 1977
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. -------------------------
  121.  
  122. |- UNIX is a trademark of Bell Laboratories.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.                    The M4 Macro Processor
  142.  
  143.  
  144.  
  145.  
  146.                      Brian W. Kernighan
  147.  
  148.  
  149.  
  150.                      Dennis M. Ritchie
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. _I_n_t_r_o_d_u_c_t_i_o_n
  158.  
  159.  
  160.      A macro processor is a useful way to enhance a program-
  161.  
  162. ming  language,  to make it more palatable or more readable,
  163.  
  164. or to tailor it to a particular  application.   The  #define
  165.  
  166. statement  in C and the analogous define in Ratfor are exam-
  167.  
  168. ples of the basic facility provided by any macro processor -
  169.  
  170. replacement of text by other text.
  171.  
  172.  
  173.      The M4 macro processor is an extension of a macro  pro-
  174.  
  175. cessor  called M3 which was written by D. M. Ritchie for the
  176.  
  177. AP-3 minicomputer; M3 was in turn based on a macro processor
  178.  
  179. implemented  for  [1].   Readers  unfamiliar  with the basic
  180.  
  181. ideas of macro processing may wish to read some of the  dis-
  182.  
  183. cussion there.
  184.  
  185.  
  186.      M4 is a suitable front end for Ratfor and  C,  and  has
  187.  
  188. also   been  used  successfully  with  Cobol.   Besides  the
  189.  
  190. straightforward  replacement  of  one  string  of  text   by
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.                            - 2 -
  202.  
  203.  
  204. another,  it  provides  macros  with  arguments, conditional
  205.  
  206. macro expansion, arithmetic,  file  manipulation,  and  some
  207.  
  208. specialized string processing functions.
  209.  
  210.  
  211.      The basic operation of M4 is to copy its input  to  its
  212.  
  213. output.   As  the  input is read, however, each alphanumeric
  214.  
  215. ``token''  (that  is,  string  of  letters  and  digits)  is
  216.  
  217. checked.  If it is the name of a macro, then the name of the
  218.  
  219. macro is replaced by its defining text,  and  the  resulting
  220.  
  221. string  is pushed back onto the input to be rescanned.  Mac-
  222.  
  223. ros may be called with arguments, in which  case  the  argu-
  224.  
  225. ments are collected and substituted into the right places in
  226.  
  227. the defining text before it is rescanned.
  228.  
  229.  
  230.      M4 provides a collection of about twenty built-in  mac-
  231.  
  232. ros  which  perform  various useful operations; in addition,
  233.  
  234. the user can define new macros.  Built-ins and  user-defined
  235.  
  236. macros  work  exactly  the same way, except that some of the
  237.  
  238. built-in macros have side effects on the state of  the  pro-
  239.  
  240. cess.
  241.  
  242.  
  243. _U_s_a_g_e
  244.  
  245.  
  246.      On UNIX, use
  247.  
  248.  
  249.    m4 [files]
  250.  
  251.  
  252. Each argument file is processed in order; if  there  are  no
  253.  
  254. arguments,  or  if an argument is `-', the standard input is
  255.  
  256. read at that point.  The processed text is  written  on  the
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.                            - 3 -
  268.  
  269.  
  270. standard  output,  which may be captured for subsequent pro-
  271.  
  272. cessing with
  273.  
  274.  
  275.    m4 [files] >outputfile
  276.  
  277.  
  278. On GCOS, usage is identical, but the program is called ./m4.
  279.  
  280.  
  281. _D_e_f_i_n_i_n_g _M_a_c_r_o_s
  282.  
  283.  
  284.      The primary built-in function of M4 is define, which is
  285.  
  286. used to define new macros.  The input
  287.  
  288.  
  289.    define(name, stuff)
  290.  
  291.  
  292. causes the string name to be defined as stuff.   All  subse-
  293.  
  294. quent  occurrences  of name will be replaced by stuff.  name
  295.  
  296. must be alphanumeric and  must  begin  with  a  letter  (the
  297.  
  298. underscore  _  counts  as a letter).  stuff is any text that
  299.  
  300. contains balanced parentheses; it may stretch over  multiple
  301.  
  302. lines.
  303.  
  304.  
  305.      Thus, as a typical example,
  306.  
  307.  
  308.    define(N, 100)
  309.  
  310.     ...
  311.  
  312.    if (i > N)
  313.  
  314.  
  315. defines N to be 100, and uses this ``symbolic constant''  in
  316.  
  317. a later if statement.
  318.  
  319.  
  320.      The left parenthesis must immediately follow  the  word
  321.  
  322. define,  to signal that define has arguments.  If a macro or
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.                            - 4 -
  334.  
  335.  
  336. built-in name is not followed  immediately  by  `(',  it  is
  337.  
  338. assumed  to  have no arguments.  This is the situation for N
  339.  
  340. above; it is actually a macro with no  arguments,  and  thus
  341.  
  342. when it is used there need be no (...) following it.
  343.  
  344.  
  345.      You should also notice that a macro name is only recog-
  346.  
  347. nized as such if it appears surrounded by non-alphanumerics.
  348.  
  349. For example, in
  350.  
  351.  
  352.    define(N, 100)
  353.  
  354.     ...
  355.  
  356.    if (NNN > 100)
  357.  
  358.  
  359. the variable NNN is  absolutely  unrelated  to  the  defined
  360.  
  361. macro N, even though it contains a lot of N'_s.
  362.  
  363.  
  364.      Things may be defined in terms of  other  things.   For
  365.  
  366. example,
  367.  
  368.  
  369.    define(N, 100)
  370.  
  371.    define(M, N)
  372.  
  373.  
  374. defines both M and N to be 100.
  375.  
  376.  
  377.      What happens if N is redefined?  Or, to say it  another
  378.  
  379. way, is M defined as N or as 100?  In M4, the latter is true
  380.  
  381. - M is 100, so even if N subsequently changes, M does not.
  382.  
  383.  
  384.      This behavior arises because  M4  expands  macro  names
  385.  
  386. into  their defining text as soon as it possibly can.  Here,
  387.  
  388. that means that when the string N is seen as  the  arguments
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.                            - 5 -
  400.  
  401.  
  402. of define are being collected, it is immediately replaced by
  403.  
  404. 100; it's just as if you had said
  405.  
  406.  
  407.    define(M, 100)
  408.  
  409.  
  410. in the first place.
  411.  
  412.  
  413.      If this isn't what you really want, there are two  ways
  414.  
  415. out  of it.  The first, which is specific to this situation,
  416.  
  417. is to interchange the order of the definitions:
  418.  
  419.  
  420.    define(M, N)
  421.  
  422.    define(N, 100)
  423.  
  424.  
  425. Now M is defined to be the string N, so when you ask  for  M
  426.  
  427. later,  you'll  always  get  the  value  of  N  at that time
  428.  
  429. (because the M will be replaced by N which will be  replaced
  430.  
  431. by 100).
  432.  
  433.  
  434. _Q_u_o_t_i_n_g
  435.  
  436.  
  437.      The more general solution is to delay the expansion  of
  438.  
  439. the  arguments  of  define  by  _q_u_o_t_i_n_g them.  Any text sur-
  440.  
  441. rounded by the single quotes ` and ' is not expanded immedi-
  442.  
  443. ately, but has the quotes stripped off.  If you say
  444.  
  445.  
  446.    define(N, 100)
  447.  
  448.    define(M, `N')
  449.  
  450.  
  451. the quotes around the N are stripped off as the argument  is
  452.  
  453. being  collected,  but they have served their purpose, and M
  454.  
  455. is defined as the string N, not 100.  The  general  rule  is
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.                            - 6 -
  466.  
  467.  
  468. that  M4  always strips off one level of single quotes when-
  469.  
  470. ever it evaluates something.  This is true even  outside  of
  471.  
  472. macros.   If  you want the word define to appear in the out-
  473.  
  474. put, you have to quote it in the input, as in
  475.  
  476.  
  477.         `define' = 1;
  478.  
  479.  
  480.  
  481.      As another instance of the same thing, which is  a  bit
  482.  
  483. more surprising, consider redefining N:
  484.  
  485.  
  486.    define(N, 100)
  487.  
  488.     ...
  489.  
  490.    define(N, 200)
  491.  
  492.  
  493. Perhaps regrettably, the  N  in  the  second  definition  is
  494.  
  495. evaluated  as  soon as it's seen; that is, it is replaced by
  496.  
  497. 100, so it's as if you had written
  498.  
  499.  
  500.    define(100, 200)
  501.  
  502.  
  503. This statement is ignored by M4, since you can  only  define
  504.  
  505. things  that  look like names, but it obviously doesn't have
  506.  
  507. the effect you wanted.  To really redefine N, you must delay
  508.  
  509. the evaluation by quoting:
  510.  
  511.  
  512.    define(N, 100)
  513.  
  514.     ...
  515.  
  516.    define(`N', 200)
  517.  
  518.  
  519. In M4, it is often wise to quote the  first  argument  of  a
  520.  
  521. macro.
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.                            - 7 -
  532.  
  533.  
  534.      If ` and ' are not  convenient  for  some  reason,  the
  535.  
  536. quote  characters  can  be changed with the built-in change-
  537.  
  538. quote:
  539.  
  540.  
  541.    changequote([, ])
  542.  
  543.  
  544. makes the new quote characters the left and right  brackets.
  545.  
  546. You can restore the original characters with just
  547.  
  548.  
  549.    changequote
  550.  
  551.  
  552.  
  553.      There are two additional built-ins related  to  define.
  554.  
  555. undefine removes the definition of some macro or built-in:
  556.  
  557.  
  558.    undefine(`N')
  559.  
  560.  
  561. removes the definition of N.  (Why are the quotes absolutely
  562.  
  563. necessary?) Built-ins can be removed with undefine, as in
  564.  
  565.  
  566.    undefine(`define')
  567.  
  568.  
  569. but once you remove one, you can never get it back.
  570.  
  571.  
  572.      The built-in ifdef provides a way  to  determine  if  a
  573.  
  574. macro  is  currently  defined.   In  particular, M4 has pre-
  575.  
  576. defined the names unix and gcos on  the  corresponding  sys-
  577.  
  578. tems, so you can tell which one you're using:
  579.  
  580.  
  581.    ifdef(`unix', `define(wordsize,16)' )
  582.  
  583.    ifdef(`gcos', `define(wordsize,36)' )
  584.  
  585.  
  586. makes a definition appropriate for the  particular  machine.
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.                            - 8 -
  598.  
  599.  
  600. Don't forget the quotes!
  601.  
  602.  
  603.      ifdef actually permits three arguments; if the name  is
  604.  
  605. undefined, the value of ifdef is then the third argument, as
  606.  
  607. in
  608.  
  609.  
  610.    ifdef(`unix', on UNIX, not on UNIX)
  611.  
  612.  
  613.  
  614. _A_r_g_u_m_e_n_t_s
  615.  
  616.  
  617.      So far we have discussed the  simplest  form  of  macro
  618.  
  619. processing - replacing one string by another (fixed) string.
  620.  
  621. User-defined macros may also have  arguments,  so  different
  622.  
  623. invocations can have different results.  Within the replace-
  624.  
  625. ment text for a macro (the second argument  of  its  define)
  626.  
  627. any  occurrence  of  $n will be replaced by the n_t_h argument
  628.  
  629. when the macro is actually  used.   Thus,  the  macro  bump,
  630.  
  631. defined as
  632.  
  633.  
  634.    define(bump, $1 = $1 + 1)
  635.  
  636.  
  637. generates code to increment its argument by 1:
  638.  
  639.  
  640.    bump(x)
  641.  
  642.  
  643. is
  644.  
  645.  
  646.    x = x + 1
  647.  
  648.  
  649.  
  650.      A macro can have as many arguments  as  you  want,  but
  651.  
  652. only  the first nine are accessible, through $1 to $9.  (The
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.                            - 9 -
  664.  
  665.  
  666. macro name itself is $0,  although  that  is  less  commonly
  667.  
  668. used.)  Arguments that are not supplied are replaced by null
  669.  
  670. strings, so we can define a macro cat which simply concaten-
  671.  
  672. ates its arguments, like this:
  673.  
  674.  
  675.    define(cat, $1$2$3$4$5$6$7$8$9)
  676.  
  677.  
  678. Thus
  679.  
  680.  
  681.    cat(x, y, z)
  682.  
  683.  
  684. is equivalent to
  685.  
  686.  
  687.    xyz
  688.  
  689.  
  690. $4 through $9 are null,  since  no  corresponding  arguments
  691.  
  692. were provided.
  693.  
  694.  
  695.  
  696.      Leading unquoted blanks, tabs, or newlines  that  occur
  697.  
  698. during  argument  collection are discarded.  All other white
  699.  
  700. space is retained.  Thus
  701.  
  702.  
  703.    define(a,   b   c)
  704.  
  705.  
  706. defines a to be b   c.
  707.  
  708.  
  709.      Arguments are separated by commas, but parentheses  are
  710.  
  711. counted  properly,  so  a comma ``protected'' by parentheses
  712.  
  713. does not terminate an argument.  That is, in
  714.  
  715.  
  716.    define(a, (b,c))
  717.  
  718.  
  719. there are only two arguments; the second is literally (b,c).
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.                            - 10 -
  730.  
  731.  
  732. And of course a bare comma or parenthesis can be inserted by
  733.  
  734. quoting it.
  735.  
  736.  
  737. _A_r_i_t_h_m_e_t_i_c _B_u_i_l_t-_i_n_s
  738.  
  739.  
  740.      M4 provides two built-in functions for doing arithmetic
  741.  
  742. on  integers (only).  The simplest is incr, which increments
  743.  
  744. its numeric argument by 1.  Thus to handle the  common  pro-
  745.  
  746. gramming  situation  where you want a variable to be defined
  747.  
  748. as ``one more than N'', write
  749.  
  750.  
  751.    define(N, 100)
  752.  
  753.    define(N1, `incr(N)')
  754.  
  755.  
  756. Then N1 is defined as one more than the current value of N.
  757.  
  758.  
  759.      The more general mechanism for arithmetic is a built-in
  760.  
  761. called  eval,  which  is  capable of arbitrary arithmetic on
  762.  
  763. integers.  It provides the operators (in decreasing order of
  764.  
  765. precedence)
  766.  
  767.  
  768.         unary + and -
  769.  
  770.         ** or ^ (exponentiation)
  771.  
  772.         *  /  % (modulus)
  773.  
  774.         +  -
  775.  
  776.         ==  !=  <  <=  >  >=
  777.  
  778.         !               (not)
  779.  
  780.         & or && (logical and)
  781.  
  782.         | or ||         (logical or)
  783.  
  784.  
  785. Parentheses may be used to group  operations  where  needed.
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.                            - 11 -
  796.  
  797.  
  798. All  the  operands of an expression given to eval must ulti-
  799.  
  800. mately be numeric.  The numeric value  of  a  true  relation
  801.  
  802. (like  1>0)  is 1, and false is 0.  The precision in eval is
  803.  
  804. 32 bits on UNIX and 36 bits on GCOS.
  805.  
  806.  
  807.      As a simple example, suppose we want M  to  be  2**N+1.
  808.  
  809. Then
  810.  
  811.  
  812.    define(N, 3)
  813.  
  814.    define(M, `eval(2**N+1)')
  815.  
  816.  
  817. As a matter of principle,  it  is  advisable  to  quote  the
  818.  
  819. defining  text  for  a macro unless it is very simple indeed
  820.  
  821. (say just a number); it usually gives the result  you  want,
  822.  
  823. and is a good habit to get into.
  824.  
  825.  
  826. _F_i_l_e _M_a_n_i_p_u_l_a_t_i_o_n
  827.  
  828.  
  829.      You can include a new file in the input at any time  by
  830.  
  831. the built-in function include:
  832.  
  833.  
  834.    include(filename)
  835.  
  836.  
  837. inserts the contents of filename in  place  of  the  include
  838.  
  839. command.  The contents of the file is often a set of defini-
  840.  
  841. tions.  The value of include (that is, its replacement text)
  842.  
  843. is the contents of the file; this can be captured in defini-
  844.  
  845. tions, etc.
  846.  
  847.  
  848.      It is a fatal error if the file named in include cannot
  849.  
  850. be  accessed.   To get some control over this situation, the
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.                            - 12 -
  862.  
  863.  
  864. alternate form sinclude  can  be  used;  sinclude  (``silent
  865.  
  866. include'') says nothing and continues if it can't access the
  867.  
  868. file.
  869.  
  870.  
  871.      It is also possible to divert the output of M4 to  tem-
  872.  
  873. porary  files  during  processing,  and output the collected
  874.  
  875. material upon command.  M4 maintains nine  of  these  diver-
  876.  
  877. sions, numbered 1 through 9.  If you say
  878.  
  879.  
  880.    divert(n)
  881.  
  882.  
  883. all subsequent output is put onto the  end  of  a  temporary
  884.  
  885. file referred to as n.  Diverting to this file is stopped by
  886.  
  887. another divert command; in particular, divert  or  divert(0)
  888.  
  889. resumes the normal output process.
  890.  
  891.  
  892.      Diverted text is normally output all at once at the end
  893.  
  894. of  processing, with the diversions output in numeric order.
  895.  
  896. It is possible, however, to bring  back  diversions  at  any
  897.  
  898. time, that is, to append them to the current diversion.
  899.  
  900.  
  901.    undivert
  902.  
  903.  
  904. brings back all diversions in numeric  order,  and  undivert
  905.  
  906. with  arguments  brings  back the selected diversions in the
  907.  
  908. order given.  The act of undiverting discards  the  diverted
  909.  
  910. stuff,  as  does  diverting into a diversion whose number is
  911.  
  912. not between 0 and 9 inclusive.
  913.  
  914.  
  915.      The value  of  undivert  is  _n_o_t  the  diverted  stuff.
  916.  
  917. Furthermore,  the  diverted  material  is  _n_o_t rescanned for
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.                            - 13 -
  928.  
  929.  
  930. macros.
  931.  
  932.  
  933.      The built-in divnum returns the number of the currently
  934.  
  935. active diversion.  This is zero during normal processing.
  936.  
  937.  
  938. _S_y_s_t_e_m _C_o_m_m_a_n_d
  939.  
  940.  
  941.      You can run any program in the local  operating  system
  942.  
  943. with the syscmd built-in.  For example,
  944.  
  945.  
  946.    syscmd(date)
  947.  
  948.  
  949. on UNIX runs the date command.   Normally  syscmd  would  be
  950.  
  951. used to create a file for a subsequent include.
  952.  
  953.  
  954.      To facilitate making unique file  names,  the  built-in
  955.  
  956. maketemp  is  provided, with specifications identical to the
  957.  
  958. system function _m_k_t_e_m_p: a string of XXXXX in the argument is
  959.  
  960. replaced by the process id of the current process.
  961.  
  962.  
  963. _C_o_n_d_i_t_i_o_n_a_l_s
  964.  
  965.  
  966.      There is a built-in called ifelse which enables you  to
  967.  
  968. perform  arbitrary  conditional  testing.   In  the simplest
  969.  
  970. form,
  971.  
  972.  
  973.    ifelse(a, b, c, d)
  974.  
  975.  
  976. compares the two strings a and b.  If these  are  identical,
  977.  
  978. ifelse  returns  the string c; otherwise it returns d.  Thus
  979.  
  980. we might define a macro called compare  which  compares  two
  981.  
  982. strings  and  returns ``yes'' or ``no'' if they are the same
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.                            - 14 -
  994.  
  995.  
  996. or different.
  997.  
  998.  
  999.    define(compare, `ifelse($1, $2, yes, no)')
  1000.  
  1001.  
  1002. Note the  quotes,  which  prevent  too-early  evaluation  of
  1003.  
  1004. ifelse.
  1005.  
  1006.  
  1007.      If the fourth argument is missing,  it  is  treated  as
  1008.  
  1009. empty.
  1010.  
  1011.  
  1012.      ifelse can actually have any number of  arguments,  and
  1013.  
  1014. thus  provides a limited form of multi-way decision capabil-
  1015.  
  1016. ity.  In the input
  1017.  
  1018.  
  1019.    ifelse(a, b, c, d, e, f, g)
  1020.  
  1021.  
  1022. if the string a matches the string b, the result is c.  Oth-
  1023.  
  1024. erwise,  if  d is the same as e, the result is f.  Otherwise
  1025.  
  1026. the result is g.  If the  final  argument  is  omitted,  the
  1027.  
  1028. result is null, so
  1029.  
  1030.  
  1031.    ifelse(a, b, c)
  1032.  
  1033.  
  1034. is c if a matches b, and null otherwise.
  1035.  
  1036.  
  1037. _S_t_r_i_n_g _M_a_n_i_p_u_l_a_t_i_o_n
  1038.  
  1039.  
  1040.      The built-in len returns the length of the string  that
  1041.  
  1042. makes up its argument.  Thus
  1043.  
  1044.  
  1045.    len(abcdef)
  1046.  
  1047.  
  1048. is 6, and len((a,b)) is 5.
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.                            - 15 -
  1060.  
  1061.  
  1062.      The built-in substr can be used to  produce  substrings
  1063.  
  1064. of strings.  substr(s, i, n) returns the substring of s that
  1065.  
  1066. starts at the i_t_h position (origin zero), and is  n  charac-
  1067.  
  1068. ters  long.   If  n  is  omitted,  the rest of the string is
  1069.  
  1070. returned, so
  1071.  
  1072.  
  1073.    substr(`now is the time', 1)
  1074.  
  1075.  
  1076. is
  1077.  
  1078.  
  1079.    ow is the time
  1080.  
  1081.  
  1082. If i or n are out of range, various sensible things happen.
  1083.  
  1084.  
  1085.      index(s1, s2) returns the index (position) in s1  where
  1086.  
  1087. the  string  s2  occurs, or -1 if it doesn't occur.  As with
  1088.  
  1089. substr, the origin for strings is 0.
  1090.  
  1091.  
  1092.      The built-in translit performs  character  translitera-
  1093.  
  1094. tion.
  1095.  
  1096.  
  1097.    translit(s, f, t)
  1098.  
  1099.  
  1100. modifies s by replacing any character  found  in  f  by  the
  1101.  
  1102. corresponding character of t.  That is,
  1103.  
  1104.  
  1105.    translit(s, aeiou, 12345)
  1106.  
  1107.  
  1108. replaces the vowels by the corresponding digits.   If  t  is
  1109.  
  1110. shorter  than  f,  characters which don't have an entry in t
  1111.  
  1112. are deleted; as a limiting case, if t is not present at all,
  1113.  
  1114. characters from f are deleted from s.  So
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.                            - 16 -
  1126.  
  1127.  
  1128.  
  1129.    translit(s, aeiou)
  1130.  
  1131.  
  1132. deletes vowels from s.
  1133.  
  1134.  
  1135.      There is also a built-in called dnl which  deletes  all
  1136.  
  1137. characters  that follow it up to and including the next new-
  1138.  
  1139. line; it is useful mainly for throwing away empty lines that
  1140.  
  1141. otherwise tend to clutter up M4 output.  For example, if you
  1142.  
  1143. say
  1144.  
  1145.  
  1146.    define(N, 100)
  1147.  
  1148.    define(M, 200)
  1149.  
  1150.    define(L, 300)
  1151.  
  1152.  
  1153. the newline at the end of each  line  is  not  part  of  the
  1154.  
  1155. definition,  so  it  is copied into the output, where it may
  1156.  
  1157. not be wanted.  If you add dnl to each of these  lines,  the
  1158.  
  1159. newlines will disappear.
  1160.  
  1161.  
  1162.      Another way to achieve this, due to J. E. Weythman, is
  1163.  
  1164.  
  1165.    divert(-1)
  1166.  
  1167.         define(...)
  1168.  
  1169.         ...
  1170.  
  1171.    divert
  1172.  
  1173.  
  1174.  
  1175. _P_r_i_n_t_i_n_g
  1176.  
  1177.  
  1178.      The built-in errprint writes its arguments out  on  the
  1179.  
  1180. standard error file.  Thus you can say
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.                            - 17 -
  1192.  
  1193.  
  1194.  
  1195.    errprint(`fatal error')
  1196.  
  1197.  
  1198.  
  1199.      dumpdef is a debugging  aid  which  dumps  the  current
  1200.  
  1201. definitions  of  defined  terms.  If there are no arguments,
  1202.  
  1203. you get everything; otherwise you get the ones you  name  as
  1204.  
  1205. arguments.  Don't forget to quote the names!
  1206.  
  1207.  
  1208. _S_u_m_m_a_r_y _o_f _B_u_i_l_t-_i_n_s
  1209.  
  1210.  
  1211.      Each entry is preceded by the page number where  it  is
  1212.  
  1213. described.
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.                            - 18 -
  1258.  
  1259.  
  1260.  
  1261.         3 changequote(L, R)
  1262.  
  1263.         1 define(name, replacement)
  1264.  
  1265.         4 divert(number)
  1266.  
  1267.         4 divnum
  1268.  
  1269.         5 dnl
  1270.  
  1271.         5 dumpdef(`name', `name', ...)
  1272.  
  1273.         5 errprint(s, s, ...)
  1274.  
  1275.         4 eval(numeric expression)
  1276.  
  1277.         3 ifdef(`name', this if true, this if false)
  1278.  
  1279.         5 ifelse(a, b, c, d)
  1280.  
  1281.         4 include(file)
  1282.  
  1283.         3 incr(number)
  1284.  
  1285.         5 index(s1, s2)
  1286.  
  1287.         5 len(string)
  1288.  
  1289.         4 maketemp(...XXXXX...)
  1290.  
  1291.         4 sinclude(file)
  1292.  
  1293.         5 substr(string, position, number)
  1294.  
  1295.         4 syscmd(s)
  1296.  
  1297.         5 translit(str, from, to)
  1298.  
  1299.         3 undefine(`name')
  1300.  
  1301.         4 undivert(number,number,...)
  1302.  
  1303.  
  1304.  
  1305. _A_c_k_n_o_w_l_e_d_g_e_m_e_n_t_s
  1306.  
  1307.  
  1308.      We are indebted to Rick  Becker,  John  Chambers,  Doug
  1309.  
  1310. McIlroy,  and  especially Jim Weythman, whose pioneering use
  1311.  
  1312. of M4 has led to several valuable improvements.  We are also
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.                            - 19 -
  1324.  
  1325.  
  1326. deeply  grateful to Weythman for several substantial contri-
  1327.  
  1328. butions to the code.
  1329.  
  1330.  
  1331. _R_e_f_e_r_e_n_c_e_s
  1332.  
  1333.  
  1334.  
  1335. [1]  B. W. Kernighan and  P.  J.  Plauger,  _S_o_f_t_w_a_r_e  _T_o_o_l_s,
  1336.  
  1337.      Addison-Wesley, Inc., 1976.
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.