3 * Facility: m4 macro processor
11 * eval - evaluate built-in macros.
12 * argc - number of elements in argv.
13 * argv - element vector :
14 * argv[0] = definition of a user
15 * macro or nil if built-in.
16 * argv[1] = name of the macro or
18 * argv[2] = parameters to user-defined
19 * . macro or built-in.
22 * Note that the minimum value for argc is 3. A call in the form
23 * of macro-or-builtin() will result in:
25 * argv[1] = macro-or-builtin
30 void eval (argv
, argc
, td
)
31 register char *argv
[];
39 printf("argc = %d\n", argc
);
40 for (n
= 0; n
< argc
; n
++)
41 printf("argv[%d] = %s\n", n
, argv
[n
]);
44 * if argc == 3 and argv[2] is null,
45 * then we have macro-or-builtin() type call.
46 * We adjust argc to avoid further checking..
49 if (argc
== 3 && !*(argv
[2]))
52 switch (td
& ~STATIC
) {
56 dodefine(argv
[2], (argc
> 3) ? argv
[3] : null
);
61 dopushdef(argv
[2], (argc
> 3) ? argv
[3] : null
);
70 * doexpr - evaluate arithmetic expression
84 * doifdef - select one of two alternatives based
85 * on the existence of another definition
88 if (lookup(argv
[2]) != nil
)
97 * dolen - find the length of the argument
101 pbnum((argc
> 2) ? strlen(argv
[2]) : 0);
106 * doincr - increment the value of the argument
110 pbnum(atoi(argv
[2]) + 1);
115 * dodecr - decrement the value of the argument
119 pbnum(atoi(argv
[2]) - 1);
126 * dosys - execute system command
130 sysval
= system(argv
[2]);
135 * dosysval - return value of the last system call.
144 if (!doincl(argv
[2])) {
145 fprintf(stderr
,"m4: %s: ",argv
[2]);
146 error("cannot open for read.");
152 (void) doincl(argv
[2]);
157 if (!dopaste(argv
[2])) {
158 fprintf(stderr
,"m4: %s: ",argv
[2]);
159 error("cannot open for read.");
165 (void) dopaste(argv
[2]);
178 * dosub - select substring
187 * doshift - push back all arguments except the
188 * first one (i.e. skip argv[2])
191 for (n
= argc
-1; n
> 3; n
--) {
204 if (argc
> 2 && (n
= atoi(argv
[2])) != 0)
218 * dodivnum - return the number of current
227 * doundefine - undefine a previously defined
228 * macro(s) or m4 keyword(s).
231 for (n
= 2; n
< argc
; n
++)
232 remhash(argv
[n
], ALL
);
237 * dopopdef - remove the topmost definitions of
238 * macro(s) or m4 keyword(s).
241 for (n
= 2; n
< argc
; n
++)
242 remhash(argv
[n
], TOP
);
247 * dotemp - create a temporary file
251 pbstr(mktemp(argv
[2]));
256 * dotranslit - replace all characters in the
257 * source string that appears in
258 * the "from" string with the corresponding
259 * characters in the "to" string.
265 map(temp
, argv
[2], argv
[3], argv
[4]);
267 map(temp
, argv
[2], argv
[3], null
);
277 * doindex - find the index of the second argument
278 * string in the first argument string.
281 pbnum((argc
> 3) ? indx(argv
[2], argv
[3]) : -1);
286 * doerrp - print the arguments to stderr file
290 for (n
= 2; n
< argc
; n
++)
291 fprintf(stderr
,"%s ", argv
[n
]);
292 fprintf(stderr
, "\n");
298 * dodnl - eat-up-to and including newline
301 while ((c
= gpbc()) != '\n' && c
!= EOF
)
307 * dom4wrap - set up for wrap-up/wind-down activity
310 m4wraps
= (argc
> 2) ? strsave(argv
[2]) : null
;
315 * doexit - immediate exit from m4.
318 exit((argc
> 2) ? atoi(argv
[2]) : 0);
323 for (n
= 2; n
< argc
; n
++)
328 error("m4: major botch in eval.");