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
|
#include "rc.h"
#include "exec.h"
#include "io.h"
#include "getflags.h"
#include "fns.h"
int getnext(void);
int wordchr(int c)
{
return !strchr("\n \t#;&|^$=`'{}()<>", c) && c!=EOF;
}
int idchr(int c)
{
/*
* Formerly:
* return 'a'<=c && c<='z' || 'A'<=c && c<='Z' || '0'<=c && c<='9'
* || c=='_' || c=='*';
*/
return c>' ' && !strchr("!\"#$%&'()+,-./:;<=>?@[\\]^`{|}~", c);
}
int future=EOF;
int doprompt=1;
int inquote;
/*
* Look ahead in the input stream
*/
int nextc(void){
if(future==EOF) future=getnext();
return future;
}
/*
* Consume the lookahead character.
*/
int advance(void){
int c=nextc();
lastc=future;
future=EOF;
return c;
}
/*
* read a character from the input stream
*/
int getnext(void){
register int c;
static int peekc=EOF;
if(peekc!=EOF){
c=peekc;
peekc=EOF;
return c;
}
if(runq->eof) return EOF;
if(doprompt) pprompt();
c=rchr(runq->cmdfd);
if(!inquote && c=='\\'){
c=rchr(runq->cmdfd);
if(c=='\n'){
doprompt=1;
c=' ';
}
else{
peekc=c;
c='\\';
}
}
doprompt=doprompt || c=='\n' || c==EOF;
if(c==EOF) runq->eof++;
else if(flag['V'] || ndot>=2 && flag['v']) pchr(err, c);
return c;
}
void pprompt(void){
var *prompt;
if(runq->iflag){
pstr(err, promptstr);
flush(err);
prompt=vlook("prompt");
if(prompt->val && prompt->val->next)
promptstr=prompt->val->next->word;
else
promptstr="\t";
}
runq->lineno++;
doprompt=0;
}
void skipwhite(void){
int c;
for(;;){
c=nextc();
if(c=='#'){ /* Why did this used to be if(!inquote && c=='#') ?? */
for(;;){
c=nextc();
if(c=='\n' || c==EOF) break;
advance();
}
}
if(c==' ' || c=='\t') advance();
else return;
}
}
void skipnl(void){
register int c;
for(;;){
skipwhite();
c=nextc();
if(c!='\n') return;
advance();
}
}
int nextis(int c){
if(nextc()==c){
advance();
return 1;
}
return 0;
}
char *addtok(char *p, int val){
if(p==0) return 0;
if(p==&tok[NTOK]){
*p=0;
yyerror("token buffer too short");
return 0;
}
*p++=val;
return p;
}
char *addutf(char *p, int c){
p=addtok(p, c);
if(twobyte(c)) /* 2-byte escape */
return addtok(p, advance());
if(threebyte(c)){ /* 3-byte escape */
p=addtok(p, advance());
return addtok(p, advance());
}
return p;
}
int lastdol; /* was the last token read '$' or '$#' or '"'? */
int lastword; /* was the last token read a word or compound word terminator? */
int yylex(void){
register int c, d=nextc();
register char *w=tok;
register struct tree *t;
yylval.tree=0;
/*
* Embarassing sneakiness: if the last token read was a quoted or unquoted
* WORD then we alter the meaning of what follows. If the next character
* is `(', we return SUB (a subscript paren) and consume the `('. Otherwise,
* if the next character is the first character of a simple or compound word,
* we insert a `^' before it.
*/
if(lastword){
lastword=0;
if(d=='('){
advance();
strcpy(tok, "( [SUB]");
return SUB;
}
if(wordchr(d) || d=='\'' || d=='`' || d=='$' || d=='"'){
strcpy(tok, "^");
return '^';
}
}
inquote=0;
skipwhite();
switch(c=advance()){
case EOF:
lastdol=0;
strcpy(tok, "EOF");
return EOF;
case '$':
lastdol=1;
if(nextis('#')){
strcpy(tok, "$#");
return COUNT;
}
if(nextis('"')){
strcpy(tok, "$\"");
return '"';
}
strcpy(tok, "$");
return '$';
case '&':
lastdol=0;
if(nextis('&')){
skipnl();
strcpy(tok, "&&");
return ANDAND;
}
strcpy(tok, "&");
return '&';
case '|':
lastdol=0;
if(nextis(c)){
skipnl();
strcpy(tok, "||");
return OROR;
}
case '<':
case '>':
lastdol=0;
/*
* funny redirection tokens:
* redir: arrow | arrow '[' fd ']'
* arrow: '<' | '<<' | '>' | '>>' | '|'
* fd: digit | digit '=' | digit '=' digit
* digit: '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
* some possibilities are nonsensical and get a message.
*/
*w++=c;
t=newtree();
switch(c){
case '|':
t->type=PIPE;
t->fd0=1;
t->fd1=0;
break;
case '>':
t->type=REDIR;
if(nextis(c)){
t->rtype=APPEND;
*w++=c;
}
else t->rtype=WRITE;
t->fd0=1;
break;
case '<':
t->type=REDIR;
if(nextis(c)){
t->rtype=HERE;
*w++=c;
}
else t->rtype=READ;
t->fd0=0;
break;
}
if(nextis('[')){
*w++='[';
c=advance();
if(c<'0' || '9'<c){
RedirErr:
*w++ = c;
*w=0;
yyerror(t->type==PIPE?"pipe syntax"
:"redirection syntax");
return EOF;
}
t->fd0=0;
do{
t->fd0=t->fd0*10+c-'0';
*w++=c;
c=advance();
}while('0'<=c && c<='9');
if(c=='='){
*w++='=';
if(t->type==REDIR) t->type=DUP;
c=advance();
if('0'<=c && c<='9'){
t->rtype=DUPFD;
t->fd1=t->fd0;
t->fd0=0;
do{
t->fd0=t->fd0*10+c-'0';
*w++=c;
c=advance();
}while('0'<=c && c<='9');
}
else{
if(t->type==PIPE) goto RedirErr;
t->rtype=CLOSE;
}
}
*w=0;
if(c!=']'
|| t->type==DUP && (t->rtype==HERE || t->rtype==APPEND))
goto RedirErr;
*w++=']';
}
*w='\0';
yylval.tree=t;
if(t->type==PIPE) skipnl();
return t->type;
case '\'':
lastdol=0;
lastword=1;
inquote=1;
for(;;){
c=advance();
if(c==EOF) break;
if(c=='\''){
if(nextc()!='\'')
break;
advance();
}
w=addutf(w, c);
}
if(w!=0) *w='\0';
t=token(tok, WORD);
t->quoted=1;
yylval.tree=t;
return t->type;
}
if(!wordchr(c)){
lastdol=0;
tok[0]=c;
tok[1]='\0';
return c;
}
for(;;){
/* next line should have (char)c==GLOB, but ken's compiler is broken */
if(c=='*' || c=='[' || c=='?' || c==(unsigned char)GLOB)
w=addtok(w, GLOB);
w=addutf(w, c);
c=nextc();
if(lastdol?!idchr(c):!wordchr(c)) break;
advance();
}
lastword=1;
lastdol=0;
if(w!=0) *w='\0';
t=klook(tok);
if(t->type!=WORD) lastword=0;
t->quoted=0;
yylval.tree=t;
return t->type;
}
|