forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
executable file
·331 lines (294 loc) · 5.51 KB
/
Copy patheval.c
File metadata and controls
executable file
·331 lines (294 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* eval.c
* Facility: m4 macro processor
* by: oz
*/
#include "mdef.h"
#include "extr.h"
/*
* 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
* built-in.
* argv[2] = parameters to user-defined
* . macro or built-in.
* .
*
* Note that the minimum value for argc is 3. A call in the form
* of macro-or-builtin() will result in:
* argv[0] = nullstr
* argv[1] = macro-or-builtin
* argv[2] = nullstr
*
*/
void eval (argv, argc, td)
register char *argv[];
register int argc;
register int td;
{
register int c, n;
static int sysval;
#ifdef DEBUG
printf("argc = %d\n", argc);
for (n = 0; n < argc; n++)
printf("argv[%d] = %s\n", n, argv[n]);
#endif
/*
* 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]))
argc--;
switch (td & ~STATIC) {
case DEFITYPE:
if (argc > 2)
dodefine(argv[2], (argc > 3) ? argv[3] : null);
break;
case PUSDTYPE:
if (argc > 2)
dopushdef(argv[2], (argc > 3) ? argv[3] : null);
break;
case DUMPTYPE:
dodump(argv, argc);
break;
case EXPRTYPE:
/*
* doexpr - evaluate arithmetic expression
*
*/
if (argc > 2)
pbnum(expr(argv[2]));
break;
case IFELTYPE:
if (argc > 4)
doifelse(argv, argc);
break;
case IFDFTYPE:
/*
* doifdef - select one of two alternatives based
* on the existence of another definition
*/
if (argc > 3) {
if (lookup(argv[2]) != nil)
pbstr(argv[3]);
else if (argc > 4)
pbstr(argv[4]);
}
break;
case LENGTYPE:
/*
* dolen - find the length of the argument
*
*/
if (argc > 2)
pbnum((argc > 2) ? strlen(argv[2]) : 0);
break;
case INCRTYPE:
/*
* doincr - increment the value of the argument
*
*/
if (argc > 2)
pbnum(atoi(argv[2]) + 1);
break;
case DECRTYPE:
/*
* dodecr - decrement the value of the argument
*
*/
if (argc > 2)
pbnum(atoi(argv[2]) - 1);
break;
#if unix || vms
case SYSCTYPE:
/*
* dosys - execute system command
*
*/
if (argc > 2)
sysval = system(argv[2]);
break;
case SYSVTYPE:
/*
* dosysval - return value of the last system call.
*
*/
pbnum(sysval);
break;
#endif
case INCLTYPE:
if (argc > 2)
if (!doincl(argv[2])) {
fprintf(stderr,"m4: %s: ",argv[2]);
error("cannot open for read.");
}
break;
case SINCTYPE:
if (argc > 2)
(void) doincl(argv[2]);
break;
#ifdef EXTENDED
case PASTTYPE:
if (argc > 2)
if (!dopaste(argv[2])) {
fprintf(stderr,"m4: %s: ",argv[2]);
error("cannot open for read.");
}
break;
case SPASTYPE:
if (argc > 2)
(void) dopaste(argv[2]);
break;
#endif
case CHNQTYPE:
dochq(argv, argc);
break;
case CHNCTYPE:
dochc(argv, argc);
break;
case SUBSTYPE:
/*
* dosub - select substring
*
*/
if (argc > 3)
dosub(argv,argc);
break;
case SHIFTYPE:
/*
* doshift - push back all arguments except the
* first one (i.e. skip argv[2])
*/
if (argc > 3) {
for (n = argc-1; n > 3; n--) {
putback(rquote);
pbstr(argv[n]);
putback(lquote);
putback(',');
}
putback(rquote);
pbstr(argv[3]);
putback(lquote);
}
break;
case DIVRTYPE:
if (argc > 2 && (n = atoi(argv[2])) != 0)
dodiv(n);
else {
active = stdout;
oindex = 0;
}
break;
case UNDVTYPE:
doundiv(argv, argc);
break;
case DIVNTYPE:
/*
* dodivnum - return the number of current
* output diversion
*
*/
pbnum(oindex);
break;
case UNDFTYPE:
/*
* doundefine - undefine a previously defined
* macro(s) or m4 keyword(s).
*/
if (argc > 2)
for (n = 2; n < argc; n++)
remhash(argv[n], ALL);
break;
case POPDTYPE:
/*
* dopopdef - remove the topmost definitions of
* macro(s) or m4 keyword(s).
*/
if (argc > 2)
for (n = 2; n < argc; n++)
remhash(argv[n], TOP);
break;
case MKTMTYPE:
/*
* dotemp - create a temporary file
*
*/
if (argc > 2)
pbstr(mktemp(argv[2]));
break;
case TRNLTYPE:
/*
* dotranslit - replace all characters in the
* source string that appears in
* the "from" string with the corresponding
* characters in the "to" string.
*
*/
if (argc > 3) {
char temp[MAXTOK];
if (argc > 4)
map(temp, argv[2], argv[3], argv[4]);
else
map(temp, argv[2], argv[3], null);
pbstr(temp);
}
else
if (argc > 2)
pbstr(argv[2]);
break;
case INDXTYPE:
/*
* doindex - find the index of the second argument
* string in the first argument string.
* -1 if not present.
*/
pbnum((argc > 3) ? indx(argv[2], argv[3]) : -1);
break;
case ERRPTYPE:
/*
* doerrp - print the arguments to stderr file
*
*/
if (argc > 2) {
for (n = 2; n < argc; n++)
fprintf(stderr,"%s ", argv[n]);
fprintf(stderr, "\n");
}
break;
case DNLNTYPE:
/*
* dodnl - eat-up-to and including newline
*
*/
while ((c = gpbc()) != '\n' && c != EOF)
;
break;
case M4WRTYPE:
/*
* dom4wrap - set up for wrap-up/wind-down activity
*
*/
m4wraps = (argc > 2) ? strsave(argv[2]) : null;
break;
case EXITTYPE:
/*
* doexit - immediate exit from m4.
*
*/
exit((argc > 2) ? atoi(argv[2]) : 0);
break;
case DEFNTYPE:
if (argc > 2)
for (n = 2; n < argc; n++)
dodefn(argv[n]);
break;
default:
error("m4: major botch in eval.");
break;
}
}