-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.c
416 lines (391 loc) · 11.2 KB
/
lex.c
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* lex.c: rc's lexical analyzer */
#include "rc.h"
#include "input.h"
#include "parse.h"
/*
Special characters (i.e., "non-word") in rc:
\t \n # ; & | ^ $ = ~ ` ' { } @ ! ( ) < > \
The lexical analyzer is fairly straightforward. The only really
unclean part concerns backslash continuation and "double
backslashes". A backslash followed by a newline is treated as a
space, otherwise backslash is not a special character (i.e.,
it can be part of a word). This introduces a host of unwanted
special cases. In our case, \ cannot be a word character, since
we wish to read in all word characters in a tight loop.
Note: to save the trouble of declaring these arrays with TRUEs
and FALSEs, I am assuming that FALSE = 0, TRUE = 1. (and so is
it declared in rc.h)
*/
#define BUFSIZE ((size_t) 1000) /* malloc hates power of 2 buffers? */
#define BUFMAX (8 * BUFSIZE) /* How big the buffer can get before we re-allocate the
space at BUFSIZE again. Premature optimization? Maybe.
*/
typedef enum wordstates {
NW, RW, KW /* "nonword", "realword", "keyword" */
} wordstates;
static void getpair(int);
int lineno;
/* lookup table for non-word characters */
const char nw[] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/* lookup table for non-word characters in variable names */
const char dnw[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
/* lookup table for quotable characters: nw + glob metachars */
const char q[] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static size_t bufsize = BUFSIZE;
static char *realbuf = NULL;
static bool newline = FALSE;
static bool errset = FALSE;
static bool prerror = FALSE;
static wordstates w = NW;
static int fd_left, fd_right;
#define checkfreecaret {if (w != NW) { w = NW; ugchar(c); return '^'; }}
enum filedescriptors {
UNSET = -9, CLOSED = -1
};
/* does this string require quoting? */
extern bool quotep(char *s, bool dollar) {
unsigned char c;
const char *meta;
meta = dollar ? dnw : q;
while ((c = *s++))
if (meta[c])
return TRUE;
return FALSE;
}
extern int yylex() {
static bool dollar = FALSE;
bool saw_meta = FALSE;
int c;
size_t i; /* The purpose of all these local assignments is to */
const char *meta; /* allow optimizing compilers like gcc to load these */
char *buf = realbuf; /* values into registers. On a sparc this is a */
YYSTYPE *y = &yylval; /* win, in code size *and* execution time */
if (errset) {
errset = FALSE;
return '\n';
}
/* rc variable-names may contain only alnum, '*' and '_', so use dnw if we are scanning one. */
meta = (dollar ? dnw : nw);
if (newline) {
--lineno; /* slight space optimization; nextline() always increments lineno */
nextline();
newline = FALSE;
}
top: while ((c = gchar()) == ' ' || c == '\t')
w = NW;
if (c != '(') dollar = FALSE;
if (c == EOF)
return END;
if (!meta[(unsigned char) c]) { /* it's a word or keyword. */
checkfreecaret;
w = RW;
i = 0;
read: do {
buf[i++] = c;
if (c == '?' || c == '[' || c == '*')
saw_meta = TRUE;
if (i >= bufsize)
buf = realbuf = erealloc(buf, bufsize *= 2);
} while ((c = gchar()) != EOF && !meta[(unsigned char) c]);
while (c == '\\') {
if ((c = gchar()) == '\n') {
nextline();
c = ' '; /* Pretend a space was read */
break;
} else {
bs: if (meta != dnw) { /* all words but varnames may have a bslash */
buf[i++] = '\\';
if (i >= bufsize)
buf = realbuf = erealloc(buf, bufsize *= 2);
if (!meta[(unsigned char) c])
goto read;
} else {
ugchar(c);
c = '\\';
break;
}
}
}
ugchar(c);
buf[i] = '\0';
w = KW;
if (i == 2) {
if (*buf == 'i' && buf[1] == 'f') return IF;
if (*buf == 'f' && buf[1] == 'n') return FN;
if (*buf == 'i' && buf[1] == 'n') return IN;
}
if (streq(buf, "for")) return FOR;
if (streq(buf, "else")) return ELSE;
if (streq(buf, "switch")) return SWITCH;
if (streq(buf, "while")) return WHILE;
if (streq(buf, "case")) return CASE;
w = RW;
y->word.w = ncpy(buf);
if (saw_meta) {
char *r, *s;
y->word.m = nalloc(strlen(buf) + 1);
for (r = buf, s = y->word.m; *r != '\0'; r++, s++)
*s = (*r == '?' || *r == '[' || *r == '*');
} else {
y->word.m = NULL;
}
y->word.q = FALSE;
return WORD;
}
if (c == '`' || c == '!' || c == '@' || c == '~' || c == '$' || c == '\'') {
checkfreecaret;
if (c == '!' || c == '@' || c == '~')
w = KW;
}
switch (c) {
case '!':
return BANG;
case '@':
return SUBSHELL;
case '~':
return TWIDDLE;
case '`':
c = gchar();
if (c == '`')
return BACKBACK;
ugchar(c);
return '`';
case '$':
dollar = TRUE;
c = gchar();
if (c == '#')
return COUNT;
if (c == '^' || c == '"')
return FLAT;
ugchar(c);
return '$';
case '\'':
w = RW;
i = 0;
/* double ' to quote it, like this: 'how''s it going?' */
while ((c = gchar()) != '\'' || (c = gchar()) == '\'') {
buf[i++] = c;
if (c == '\n')
nextline();
if (c == EOF) {
w = NW;
scanerror("eof in quoted string");
return HUH;
}
if (i >= bufsize)
buf = realbuf = erealloc(buf, bufsize *= 2);
}
ugchar(c);
buf[i] = '\0';
y->word.w = ncpy(buf);
y->word.m = NULL;
y->word.q = TRUE;
return WORD;
case '\\':
if ((c = gchar()) == '\n') {
nextline();
goto top; /* Pretend it was just another space. */
}
ugchar(c);
c = '\\';
checkfreecaret;
c = gchar();
i = 0;
goto bs;
case '(':
if (w == RW) /* SUB's happen only after real words, not keywords, so if () and while () work */
c = SUB;
w = NW;
return c;
case '#':
while ((c = gchar()) != '\n') /* skip comment until newline */
if (c == EOF)
return END;
/* FALLTHROUGH */
case '\n':
lineno++;
newline = TRUE;
/* FALLTHROUGH */
case ';':
case '^':
case ')':
case '=':
case '{': case '}':
w = NW;
return c;
case '&':
w = NW;
c = gchar();
if (c == '&')
return ANDAND;
ugchar(c);
return '&';
case '|':
w = NW;
c = gchar();
if (c == '|')
return OROR;
getpair(c);
if (errset)
return HUH;
if ((y->pipe.left = fd_left) == UNSET)
y->pipe.left = 1; /* default to fd 1 */
if ((y->pipe.right = fd_right) == UNSET)
y->pipe.right = 0; /* default to fd 0 */
if (y->pipe.right == CLOSED) {
scanerror("expected digit after '='"); /* can't close a pipe */
return HUH;
}
return PIPE;
case '>':
c = gchar();
if (c == '>') {
c = gchar();
y->redir.type = rAppend;
} else
y->redir.type = rCreate;
y->redir.fd = 1;
goto common;
case '<':
c = gchar();
if (c == '<') {
c = gchar();
if (c == '<') {
c = gchar();
y->redir.type = rHerestring;
} else {
y->redir.type = rHeredoc;
}
} else
y->redir.type = rFrom;
y->redir.fd = 0;
common:
w = NW;
getpair(c);
if (errset)
return HUH;
if (fd_right == UNSET) { /* redirection, not dup */
if (fd_left != UNSET) {
y->redir.fd = fd_left;
return SREDIR;
}
return (y->redir.type == rFrom || y->redir.type == rCreate) ? REDIR : SREDIR;
} else { /* dup; recast yylval */
y->dup.type = y->redir.type;
y->dup.left = fd_left;
y->dup.right = fd_right;
return DUP;
}
default:
w = NW;
return c; /* don't know what it is, let yacc barf on it */
}
}
extern void yyerror(const char *s) {
char *tok;
if (prerror) { /* don't print "syntax error" if there's a more informative scanerror */
prerror = FALSE;
return;
}
if (!interactive) {
if (w != NW)
tok = realbuf;
else if (lastchar == EOF)
tok = "eof";
else if (lastchar == '\n')
tok = "end of line";
else
tok = nprint((lastchar < 32 || lastchar > 126) ? "(decimal %d)" : "'%c'", lastchar);
fprint(2, "rc: line %d: %s near %s\n", lineno - (lastchar == '\n'), s, tok);
} else
fprint(2, "rc: %s\n", s);
}
extern void scanerror(char *s) {
skiptonl(); /* flush upto newline */
yyerror(s);
errset = prerror = TRUE;
}
extern void inityy() {
newline = FALSE;
w = NW;
hq = NULL;
/* return memory to the system if the buffer got too large */
if (bufsize > BUFMAX && realbuf != NULL) {
efree(realbuf);
bufsize = BUFSIZE;
realbuf = ealloc(bufsize);
} else if (realbuf == NULL)
realbuf = ealloc(bufsize);
}
/*
Scan in a pair of integers for redirections like >[2=1]. CLOSED represents a closed file
descriptor (i.e., >[2=]) and UNSET represents an undesignated file descriptor (e.g.,
>[2] is represented as (2,UNSET).
This function makes use of unsigned compares to make range tests in one compare operation.
*/
static void getpair(int c) {
int n;
fd_left = fd_right = UNSET;
if (c != '[') {
ugchar(c);
return;
}
if ((unsigned int) (n = gchar() - '0') > 9) {
scanerror("expected digit after '['");
return;
}
while ((unsigned int) (c = gchar() - '0') <= 9)
n = n * 10 + c;
fd_left = n;
c += '0';
switch (c) {
default:
scanerror("expected '=' or ']' after digit");
return;
case ']':
return;
case '=':
if ((unsigned int) (n = gchar() - '0') > 9) {
if (n != ']' - '0') {
scanerror("expected digit or ']' after '='");
return;
}
fd_right = CLOSED;
} else {
while ((unsigned int) (c = gchar() - '0') <= 9)
n = n * 10 + c;
if (c != ']' - '0') {
scanerror("expected ']' after digit");
return;
}
fd_right = n;
}
}
}