* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)eval.c 5.4 (Berkeley) %G%";
* 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.");