* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)eval.c 5.4 (Berkeley) 2/26/91";
* Facility: m4 macro processor
* eval - evaluate built-in macros.
* argc - number of elements in argv.
* argv - element vector :
* argv[0] = definition of a user
* macro or nil if built-in.
* argv[1] = name of the macro or
* argv[2] = parameters to user-defined
* Note that the minimum value for argc is 3. A call in the form
* of macro-or-builtin() will result in:
* argv[1] = macro-or-builtin
printf("argc = %d\n", argc
);
for (n
= 0; n
< argc
; n
++)
printf("argv[%d] = %s\n", n
, argv
[n
]);
* if argc == 3 and argv[2] is null,
* then we have macro-or-builtin() type call.
* We adjust argc to avoid further checking..
if (argc
== 3 && !*(argv
[2]))
dodefine(argv
[2], (argc
> 3) ? argv
[3] : null
);
dopushdef(argv
[2], (argc
> 3) ? argv
[3] : null
);
* doexpr - evaluate arithmetic expression
* doifdef - select one of two alternatives based
* on the existence of another definition
if (lookup(argv
[2]) != nil
)
* dolen - find the length of the argument
pbnum((argc
> 2) ? strlen(argv
[2]) : 0);
* doincr - increment the value of the argument
pbnum(atoi(argv
[2]) + 1);
* dodecr - decrement the value of the argument
pbnum(atoi(argv
[2]) - 1);
* dosys - execute system command
sysval
= system(argv
[2]);
* dosysval - return value of the last system call.
fprintf(stderr
,"m4: %s: ",argv
[2]);
error("cannot open for read.");
fprintf(stderr
,"m4: %s: ",argv
[2]);
error("cannot open for read.");
* dosub - select substring
* doshift - push back all arguments except the
* first one (i.e. skip argv[2])
for (n
= argc
-1; n
> 3; n
--) {
if (argc
> 2 && (n
= atoi(argv
[2])) != 0)
* dodivnum - return the number of current
* doundefine - undefine a previously defined
* macro(s) or m4 keyword(s).
for (n
= 2; n
< argc
; n
++)
* dopopdef - remove the topmost definitions of
* macro(s) or m4 keyword(s).
for (n
= 2; n
< argc
; n
++)
* dotemp - create a temporary file
* dotranslit - replace all characters in the
* source string that appears in
* the "from" string with the corresponding
* characters in the "to" string.
map(temp
, argv
[2], argv
[3], argv
[4]);
map(temp
, argv
[2], argv
[3], null
);
* doindex - find the index of the second argument
* string in the first argument string.
pbnum((argc
> 3) ? indx(argv
[2], argv
[3]) : -1);
* doerrp - print the arguments to stderr file
for (n
= 2; n
< argc
; n
++)
fprintf(stderr
,"%s ", argv
[n
]);
* dodnl - eat-up-to and including newline
while ((c
= gpbc()) != '\n' && c
!= EOF
)
* dom4wrap - set up for wrap-up/wind-down activity
m4wraps
= (argc
> 2) ? strdup(argv
[2]) : null
;
* doexit - immediate exit from m4.
exit((argc
> 2) ? atoi(argv
[2]) : 0);
for (n
= 2; n
< argc
; n
++)
error("m4: major botch in eval.");