forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl.c
More file actions
executable file
·333 lines (307 loc) · 6.24 KB
/
Copy pathl.c
File metadata and controls
executable file
·333 lines (307 loc) · 6.24 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
332
333
/*
* a small awk clone
*
* (C) 1989 Saeko Hirabauashi & Kouichi Hirabayashi
*
* Absolutely no warranty. Use this software with your own risk.
*
* Permission to use, copy, modify and distribute this software for any
* purpose and without fee is hereby granted, provided that the above
* copyright and disclaimer notice.
*
* This program was written to fit into 64K+64K memory of the Minix 1.2.
*/
#include <stdio.h>
#include <ctype.h>
#include "awk.h"
extern char *srcprg; /* inline program */
extern FILE *pfp; /* program file */
int sym; /* lexical token */
int sym1; /* auxiliary lexical token */
int regexflg; /* set by parser (y.c) to indicate parsing REGEXPR */
int funflg; /* set by parser (y.c) to indicate parsing FUNCTION */
int printflg; /* set by parser (y.c) to indicate parsing PRINT */
int getlineflg; /* set by parser (y.c) to indicate parsing GETLINE */
char text[BUFSIZ]; /* lexical word */
char line[BUFSIZ]; /* program line for error message (ring buffer) */
char *linep = line; /* line pointer */
char funnam[128]; /* function name for error message */
int lineno = 1;
lex()
{
int c, d;
char *s;
if (regexflg)
return sym = scanreg();
next:
while ((c = Getc()) == ' ' || c == '\t')
;
while (c == '#')
for (c = Getc(); c != '\n'; c = Getc())
;
switch (c) {
case '\\':
if ((c = Getc()) == '\n') {
lineno++;
goto next;
}
break;
case '\n':
lineno++;
break;
}
switch (c) {
case EOF: return sym = 0;
case '+': return sym = follow2('=', '+', ADDEQ, INC, ADD);
case '-': return sym = follow2('=', '-', SUBEQ, DEC, SUB);
case '*': return sym = follow('=', MULTEQ, MULT);
case '/': return sym = follow('=', DIVEQ, DIV);
case '%': return sym = follow('=', MODEQ, MOD);
case '^': return sym = follow('=', POWEQ, POWER);
case '=': return sym = follow('=', EQ, ASSIGN);
case '!': return sym = follow2('=', '~', NE, NOMATCH, NOT);
case '&': return sym = follow('&', AND, BINAND);
case '|': sym = follow('|', OR, BINOR);
if (printflg && sym == BINOR)
sym = R_POUT;
return sym;
case '<': sym = follow2('=', '<', LE, SHIFTL, LT);
if (getlineflg && sym == LT)
sym = R_IN;
return sym;
case '>': sym = follow2('=', '>', GE, SHIFTR, GT);
if (printflg) {
switch (sym) {
case GT: sym = R_OUT; break;
case SHIFTR: sym = R_APD; break;
}
}
return sym;
case '~': return sym = MATCH; break;
case ';': case '\n': return sym = EOL;
}
if (isalpha(c) || c == '_') {
for (s = text; isalnum(c) || c == '_'; ) {
*s++ = c; c = Getc();
}
Ungetc(c);
*s = '\0';
if ((d = iskeywd(text)) == 0 &&
(d = isbuiltin(text, &sym1)) == 0) {
if (c == '(')
return sym = CALL;
else if (funflg) {
if ((sym1 = isarg(text)) != -1)
return sym = ARG;
}
}
return sym = d ? d : IDENT;
}
else if (c == '.' || (isdigit(c))) {
Ungetc(c);
return sym = scannum(text); /* NUMBER */
}
else if (c == '"')
return sym = scanstr(text); /* STRING */
return sym = c;
}
static
follow(c1, r1, r2)
{
register int c;
if ((c = Getc()) == c1)
return r1;
else {
Ungetc(c);
return r2;
}
}
static
follow2(c1, c2, r1, r2, r3)
{
register int c;
if ((c = Getc()) == c1)
return r1;
else if (c == c2)
return r2;
else {
Ungetc(c);
return r3;
}
}
static
iskeywd(s) char *s;
{
static struct { char *kw; int token; } tab[] = {
"BEGIN", BEGIN,
"END", END,
"break", BREAK,
"continue", CONTIN,
"delete", DELETE,
"do", DO,
"else", ELSE,
"exit", EXIT,
"for", FOR,
"func", FUNC,
"function", FUNC,
"getline", GETLINE,
"if", IF,
"in", IN,
"next", NEXT,
"print", PRINT,
"printf", PRINTF,
"return", RETURN,
"sprint", SPRINT,
"sprintf", SPRINTF,
"while", WHILE,
"", 0, 0
};
register int i;
for (i = 0; tab[i].token; i++)
if (strcmp(tab[i].kw, s) == 0)
break;
return tab[i].token;
}
static
isbuiltin(s, p) char *s; int *p;
{
static struct { char *kw; int type; int token; } tab[] = {
"atan2", MATHFUN, ATAN2,
"close", STRFUN, CLOSE,
"cos", MATHFUN, COS,
"exp", MATHFUN, EXP,
"gsub", SUBST, RGSUB,
"index", STRFUN, INDEX,
"int", MATHFUN, INT,
"length", STRFUN, LENGTH,
"log", MATHFUN, LOG,
"match", STRFUN, RMATCH,
"sin", MATHFUN, SIN,
"sqrt", MATHFUN, SQRT,
"rand", MATHFUN, RAND,
"srand", MATHFUN, SRAND,
"split", STRFUN, SPLIT,
"sub", SUBST, RSUB,
"substr", STRFUN, SUBSTR,
"system", STRFUN, SYSTEM,
"", 0, 0
};
register int i;
for (i = 0; tab[i].token; i++)
if (strcmp(tab[i].kw, s) == 0)
break;
*p = tab[i].token;
return tab[i].type;
}
static
scannum(s) char *s;
{
register int c;
char *strchr();
if ((c = Getc()) && strchr("+-", c) != NULL) {
*s++ = c; c = Getc();
}
while (isdigit(c)) {
*s++ = c; c = Getc();
}
if (c == '.') {
*s++ = c; c = Getc();
while (isdigit(c)) {
*s++ = c; c = Getc();
}
}
if (c && strchr("eE", c) != NULL) {
*s++ = c; c = Getc();
if (c && strchr("+-", c) != NULL) {
*s++ = c; c = Getc();
}
while (isdigit(c)) {
*s++ = c; c = Getc();
}
}
*s = '\0';
Ungetc(c);
return NUMBER;
}
static
scanstr(s) char *s;
{
register int c, i, j;
for (c = Getc(); c != EOF & c != '"'; ) {
if (c == '\\') {
switch (c = Getc()) {
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
default:
if (isdigit(c)) {
for (i = j = 0; i < 3 && isdigit(c); c = Getc(), i++)
j = j * 8 + c - '0';
Ungetc(c);
c = j;
}
break;
}
}
*s++ = c;
if (isKanji(c))
*s++ = Getc();
c = Getc();
}
*s = '\0';
return STRING;
}
static
scanreg()
{
register int c;
register char *s;
for (s = text; (c = Getc()) != '/'; )
if (c == '\n')
error("newline in regular expression");
else {
if (isKanji(c) || c == '\\') {
*s++ = c; c = Getc();
}
*s++ = c;
}
*s = '\0';
return REGEXP;
}
static int c0;
Ungetc(c)
{
c0 = c;
if (linep > line) {
if (--linep < line)
linep == line + BUFSIZ - 1;
}
}
Getc()
{
register int c;
char *s, *t;
if (c0) {
c = c0; c0 = 0;
}
else if (srcprg)
c = *srcprg ? *srcprg++ : EOF;
else
c = fgetc(pfp);
#if 0
if (linep - line == BUFSIZ) {
printf("!!!\n");
for (s = line; *s != '\n' && ((s - line) <BUFSIZ); s++)
;
printf("***(%d)***\n", *s);
for (t = line; s < linep; )
*t++ = *++s;
}
#endif
*linep++ = c;
if ((linep - line) == BUFSIZ)
linep = line;
return c;
}