home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 138.lha / M4 / m4.man < prev    next >
Text File  |  1986-11-20  |  14KB  |  271 lines

  1. M4(local)           UNIX Programmer's Manual            M4(local)
  2. NAME
  3.      pd m4 - macro processor
  4. ORIGIN
  5.      MetaSystems
  6. SYNOPSIS
  7.      m4[ options ]
  8. DESCRIPTION
  9.      Pd M4 is a un*x M4 look-alike macro processor intended as a
  10.      front end for Ratfor, Pascal, and other languages that do
  11.      not have a built-in macro processing capability.  Pd M4
  12.      reads standard input, the processed text is written on the
  13.      standard output.
  14.      The options and their effects are as follows:
  15.      -Dname[=val]
  16.           Defines name to val or to null in val's absence.
  17.      -Uname
  18.           undefines name.
  19.      Macro calls have the form:
  20.           name(arg1,arg2, ..., argn)
  21.      The ( must immediately follow the name of the macro.  If the
  22.      name of a defined macro is not followed by a (, it is taken
  23.      to be a call of that macro with no arguments, i.e. name().
  24.      Potential macro names consist of alphabetic letters and
  25.      digits.
  26.      Leading unquoted blanks, tabs and newlines are ignored while
  27.      collecting arguments.  Left and right single quotes are used
  28.      to quote strings.  The value of a quoted string is the
  29.      string stripped of the quotes.
  30.      When a macro name is recognized, its arguments are collected
  31.      by searching for a matching ).  If fewer arguments are sup-
  32.      plied than are in the macro definition, the trailing argu-
  33.      ments are taken to be null.  Macro evaluation proceeds nor-
  34.      mally during the collection of the arguments, and any commas
  35.      or right parentheses which happen to turn up within the
  36.      value of a nested call are as effective as those in the ori-
  37.      ginal input text. (This is typically referred as inside-out
  38.      macro expansion.) After argument collection, the value of
  39.      the macro is pushed back onto the input stream and res-
  40.      canned.
  41. Printed 5/16/88            30 Aug 1987                          1
  42. M4(local)           UNIX Programmer's Manual            M4(local)
  43.      Pd M4 makes available the following built-in macros.  They
  44.      may be redefined, but once this is done the original meaning
  45.      is lost.  Their values are null unless otherwise stated.
  46.      define        usage: define(name [, val])
  47.                    the second argument is installed as the value
  48.                    of the macro whose name is the first argument.
  49.                    If there is no second argument, the value is
  50.                    null.  Each occurrence of $n in the replace-
  51.                    ment text, where n is a digit, is replaced by
  52.                    the n-th argument.  Argument 0 is the name of
  53.                    the macro; missing arguments are replaced by
  54.                    the null string.
  55.      defn          usage: defn(name [, name ...])
  56.                    returns the quoted definition of its
  57.                    argument(s). Useful in renaming macros.
  58.      undefine      usage: undefine(name [, name ...])
  59.                    removes the definition of the macro(s) named.
  60.                    If there is more than one definition for the
  61.                    named macro, (due to previous use of pushdef)
  62.                    all definitions are removed.
  63.      pushdef       usage: pushdef(name [, val])
  64.                    like define, but saves any previous definition
  65.                    by stacking the current definition.
  66.      popdef        usage: popdef(name [, name ...])
  67.                    removes current definition of its argument(s),
  68.                    exposing the previous one if any.
  69.      ifdef         usage: ifdef(name, if-def [, ifnot-def])
  70.                    if the first argument is defined, the value is
  71.                    the second argument, otherwise the third.  If
  72.                    there is no third argument, the value is null.
  73.                    A word indicating the current operating system
  74.                    is predefined.  (e.g.  unix or vms)
  75.      shift         usage: shift(arg, arg, arg, ...)
  76.                    returns all but its first argument.  The other
  77.                    arguments are quoted and pushed back with com-
  78.                    mas in between.  The quoting nullifies the
  79.                    effect of the extra scan that will subse-
  80.                    quently be performed.
  81.      changequote   usage: changequote(lqchar, rqchar)
  82.                    change quote symbols to the first and second
  83.                    arguments.  With no arguments, the quotes are
  84.                    reset back to the default characters. (i.e.,
  85.                    ).
  86. Printed 5/16/88            30 Aug 1987                          2
  87. M4(local)           UNIX Programmer's Manual            M4(local)
  88.      changecom     usage: changecom(lcchar, rcchar)
  89.                    change left and right comment markers from the
  90.                    default # and newline.  With no arguments, the
  91.                    comment mechanism is reset back to the default
  92.                    characters.  With one argument, the left
  93.                    marker becomes the argument and the right
  94.                    marker becomes newline.  With two arguments,
  95.                    both markers are affected.
  96.      divert        usage: divert(divnum)
  97.                    m4 maintains 10 output streams, numbered 0-9.
  98.                    initially stream 0 is the current stream. The
  99.                    divert macro changes the current output stream
  100.                    to its (digit-string) argument.  Output
  101.                    diverted to a stream other than 0 through 9
  102.                    disappears into bitbucket.
  103.      undivert      usage: undivert([divnum [, divnum ...]])
  104.                    causes immediate output of text from diver-
  105.                    sions named as argument(s), or all diversions
  106.                    if no argument.  Text may be undiverted into
  107.                    another diversion.  Undiverting discards the
  108.                    diverted text. At the end of input processing,
  109.                    M4 forces an automatic undivert, unless m4wrap
  110.                    is defined.
  111.      divnum        usage: divnum()
  112.                    returns the value of the current output
  113.                    stream.
  114.      dnl           usage: dnl()
  115.                    reads and discards characters up to and
  116.                    including the next newline.
  117.      ifelse        usage: ifelse(arg, arg, if-same [, ifnot-same
  118.                    | arg, arg ...])
  119.                    has three or more arguments.  If the first
  120.                    argument is the same string as the second,
  121.                    then the value is the third argument.  If not,
  122.                    and if there are more than four arguments, the
  123.                    process  is repeated with arguments 4, 5, 6 and
  124.                    7.  Otherwise, the value is either the fourth
  125.                    string, or, if it is not present, null.
  126.      incr          usage: incr(num)
  127.                    returns the value of its argument incremented
  128.                    by 1.  The value of the argument is calculated
  129.                    by interpreting an initial digit-string as a
  130.                    decimal number.
  131.      decr          usage: decr(num)
  132.                    returns the value of its argument decremented
  133. Printed 5/16/88            30 Aug 1987                          3
  134. M4(local)           UNIX Programmer's Manual            M4(local)
  135.                    by 1.
  136.      eval          usage: eval(expression)
  137.                    evaluates its argument as a constant expres-
  138.                    sion, using integer arithmetic.  The evalua-
  139.                    tion mechanism is very similar to that of cpp
  140.                    (#if expression). The expression can involve
  141.                    only integer constants and character con-
  142.                    stants, possibly connected by the binary
  143.                    operators
  144.                    *    /    %    +    -    >>   <<   <    >
  145.                    <=   >=   ==   !=   &    ^    |    &&   ||
  146.                    or the unary operators - ~ !  or by the ter-
  147.                    nary operator  ? : .  Parentheses may be used
  148.                    for grouping. Octal numbers may be specified
  149.                    as in C.
  150.      len           usage: len(string)
  151.                    returns the number of characters in its argu-
  152.                    ment.
  153.      index         usage: index(search-string, string)
  154.                    returns the position in its first argument
  155.                    where the second argument begins (zero ori-
  156.                    gin), or -1 if the second argument does not
  157.                    occur.
  158.      substr        usage: substr(string, index [, length])
  159.                    returns a substring of its first argument.
  160.                    The second argument is a zero origin number
  161.                    selecting the first character (internally
  162.                    treated as an expression); the third argument
  163.                    indicates the length of the substring.  A
  164.                    missing third argument is taken to be large
  165.                    enough to extend to the end of the first
  166.                    string.
  167.      translit      usage: translit(source, from [, to])
  168.                    transliterates the characters in its first
  169.                    argument from the set given by the second
  170.                    argument to the set given by the third.  If
  171.                    the third argument is shorter than the second,
  172.                    all extra characters in the second argument
  173.                    are deleted from the first argument. If the
  174.                    third argument is missing altogether, all
  175.                    characters in the second argument are deleted
  176.                    from the first argument.
  177.      include       usage: include(filename)
  178.                    returns the contents of the file named in the
  179. Printed 5/16/88            30 Aug 1987                          4
  180. M4(local)           UNIX Programmer's Manual            M4(local)
  181.                    argument.
  182.      sinclude      usage: sinclude(filename)
  183.                    is identical to include, except that it says
  184.                    nothing if the file is inaccessible.
  185.      paste         usage: paste(filename)
  186.                    returns the contents of the file named in the
  187.                    argument without any processing, unlike
  188.                    include.
  189.      spaste        usage: spaste(filename)
  190.                    is identical to paste, except that it says
  191.                    nothing if the file is inaccessible.
  192.      syscmd        usage: syscmd(command)
  193.                    executes the UNIX command given in the first
  194.                    argument.  No value is returned.
  195.      sysval        usage: sysval()
  196.                    is the return code from the last call to
  197.                    syscmd.
  198.      maketemp      usage: maketemp(string)
  199.                    fills in a string of XXXXXX in its argument
  200.                    with the current process ID.
  201.      m4exit        usage: m4exit([exitcode])
  202.                    causes immediate exit from m4.  Argument 1, if
  203.                    given, is the exit code; the default is 0.
  204.      m4wrap        usage: m4wrap(m4-macro-or-built-in)
  205.                    argument 1 will be pushed back at final EOF;
  206.                    example: m4wrap(`dumptable()').
  207.      errprint      usage: errprint(str [, str, str, ...])
  208.                    prints its argument(s) on stderr. If there is
  209.                    more than one argument, each argument is
  210.                    separated by a space during the output.
  211.      dumpdef       usage: dumpdef([name, name, ...])
  212.                    prints current names and definitions, for the
  213.                    named items, or for all if no arguments are
  214.                    given.
  215. AUTHOR
  216.      Ozan S. Yigit (oz)
  217. BUGS
  218.      Pd M4 is distributed at the source level, and does not
  219.      require an expensive license agreement.
  220. Printed 5/16/88            30 Aug 1987                          5
  221. M4(local)           UNIX Programmer's Manual            M4(local)
  222.      A sufficiently complex M4 macro set is about as readable as
  223.      APL.
  224.      All complex uses of M4 require the ability to program in
  225.      deep recursion.  Previous lisp experience is recommended.
  226.      Pd M4 is slower than V7 M4.
  227. EXAMPLES
  228.      The following macro program illustrates the type of things
  229.      that can be done with M4.
  230.           changequote(<,>) define(HASHVAL,99) dnl
  231.           define(hash,<expr(str(substr($1,1),0)%HASHVAL)>) dnl
  232.           define(str,
  233.                <ifelse($1,",$2,
  234.                     <str(substr(<$1>,1),<expr($2+'substr($1,0,1)')>)>)
  235.                >) dnl
  236.           define(KEYWORD,<$1,hash($1),>) dnl
  237.           define(TSTART,
  238.           <struct prehash {
  239.                char *keyword;
  240.                int   hashval;
  241.           } keytab[] = {>) dnl
  242.           define(TEND,<  "",0
  243.           };>) dnl
  244.      Thus a keyword table containing the keyword string and its
  245.      pre-calculated hash value may be generated thus:
  246.           TSTART
  247.                KEYWORD("foo")
  248.                KEYWORD("bar")
  249.                KEYWORD("baz")
  250.           TEND
  251.      which will expand into:
  252.           struct prehash {
  253.                char *keyword;
  254.                int   hashval;
  255.           } keytab[] = {
  256.                "foo",27,
  257.                "bar",12,
  258.                "baz",20,
  259.                "",0
  260.           };
  261.      Presumably, such a table would speed up the installation of
  262.      the keywords into a dynamic hash table. (Note that the above
  263.      macro cannot be used with M4, since eval does not handle
  264.      character constants.)
  265. Printed 5/16/88            30 Aug 1987                          6
  266. M4(local)           UNIX Programmer's Manual            M4(local)
  267. SEE ALSO
  268.      cc(1), m4(1), cpp(1).  The M4 Macro Processor by B. W. Ker-
  269.      nighan and D. M. Ritchie.
  270. Printed 5/16/88            30 Aug 1987                          7
  271.