syn.c revision 1.1 1 1.1 jtc /*
2 1.1 jtc * shell parser (C version)
3 1.1 jtc */
4 1.1 jtc
5 1.1 jtc #include "sh.h"
6 1.1 jtc #include "c_test.h"
7 1.1 jtc
8 1.1 jtc struct multiline_state {
9 1.1 jtc int on; /* set in multiline commands (\n becomes ;) */
10 1.1 jtc int start_token; /* token multiline is for (eg, FOR, {, etc.) */
11 1.1 jtc int start_line; /* line multiline command started on */
12 1.1 jtc };
13 1.1 jtc
14 1.1 jtc static void yyparse ARGS((void));
15 1.1 jtc static struct op *pipeline ARGS((int cf));
16 1.1 jtc static struct op *andor ARGS((void));
17 1.1 jtc static struct op *c_list ARGS((void));
18 1.1 jtc static struct ioword *synio ARGS((int cf));
19 1.1 jtc static void musthave ARGS((int c, int cf));
20 1.1 jtc static struct op *nested ARGS((int type, int smark, int emark));
21 1.1 jtc static struct op *get_command ARGS((int cf));
22 1.1 jtc static struct op *dogroup ARGS((void));
23 1.1 jtc static struct op *thenpart ARGS((void));
24 1.1 jtc static struct op *elsepart ARGS((void));
25 1.1 jtc static struct op *caselist ARGS((void));
26 1.1 jtc static struct op *casepart ARGS((int endtok));
27 1.1 jtc static struct op *function_body ARGS((char *name, int ksh_func));
28 1.1 jtc static char ** wordlist ARGS((void));
29 1.1 jtc static struct op *block ARGS((int type, struct op *t1, struct op *t2,
30 1.1 jtc char **wp));
31 1.1 jtc static struct op *newtp ARGS((int type));
32 1.1 jtc static void syntaxerr ARGS((const char *what))
33 1.1 jtc GCC_FUNC_ATTR(noreturn);
34 1.1 jtc static void multiline_push ARGS((struct multiline_state *save, int tok));
35 1.1 jtc static void multiline_pop ARGS((struct multiline_state *saved));
36 1.1 jtc static int assign_command ARGS((char *s));
37 1.1 jtc #ifdef KSH
38 1.1 jtc static int dbtestp_isa ARGS((Test_env *te, Test_meta meta));
39 1.1 jtc static const char *dbtestp_getopnd ARGS((Test_env *te, Test_op op,
40 1.1 jtc int do_eval));
41 1.1 jtc static int dbtestp_eval ARGS((Test_env *te, Test_op op, const char *opnd1,
42 1.1 jtc const char *opnd2, int do_eval));
43 1.1 jtc static void dbtestp_error ARGS((Test_env *te, int offset, const char *msg));
44 1.1 jtc #endif /* KSH */
45 1.1 jtc
46 1.1 jtc static struct op *outtree; /* yyparse output */
47 1.1 jtc
48 1.1 jtc static struct multiline_state multiline; /* \n changed to ; */
49 1.1 jtc
50 1.1 jtc static int reject; /* token(cf) gets symbol again */
51 1.1 jtc static int symbol; /* yylex value */
52 1.1 jtc
53 1.1 jtc #define REJECT (reject = 1)
54 1.1 jtc #define ACCEPT (reject = 0)
55 1.1 jtc #define token(cf) \
56 1.1 jtc ((reject) ? (ACCEPT, symbol) : (symbol = yylex(cf)))
57 1.1 jtc #define tpeek(cf) \
58 1.1 jtc ((reject) ? (symbol) : (REJECT, symbol = yylex(cf)))
59 1.1 jtc
60 1.1 jtc static void
61 1.1 jtc yyparse()
62 1.1 jtc {
63 1.1 jtc int c;
64 1.1 jtc
65 1.1 jtc ACCEPT;
66 1.1 jtc yynerrs = 0;
67 1.1 jtc
68 1.1 jtc outtree = c_list();
69 1.1 jtc c = tpeek(0);
70 1.1 jtc if (c == 0 && !outtree)
71 1.1 jtc outtree = newtp(TEOF);
72 1.1 jtc else if (c != '\n' && c != 0)
73 1.1 jtc syntaxerr((char *) 0);
74 1.1 jtc }
75 1.1 jtc
76 1.1 jtc static struct op *
77 1.1 jtc pipeline(cf)
78 1.1 jtc int cf;
79 1.1 jtc {
80 1.1 jtc register struct op *t, *p, *tl = NULL;
81 1.1 jtc
82 1.1 jtc t = get_command(cf);
83 1.1 jtc if (t != NULL) {
84 1.1 jtc while (token(0) == '|') {
85 1.1 jtc if ((p = get_command(CONTIN)) == NULL)
86 1.1 jtc syntaxerr((char *) 0);
87 1.1 jtc if (tl == NULL)
88 1.1 jtc t = tl = block(TPIPE, t, p, NOWORDS);
89 1.1 jtc else
90 1.1 jtc tl = tl->right = block(TPIPE, tl->right, p, NOWORDS);
91 1.1 jtc }
92 1.1 jtc REJECT;
93 1.1 jtc }
94 1.1 jtc return (t);
95 1.1 jtc }
96 1.1 jtc
97 1.1 jtc static struct op *
98 1.1 jtc andor()
99 1.1 jtc {
100 1.1 jtc register struct op *t, *p;
101 1.1 jtc register int c;
102 1.1 jtc
103 1.1 jtc t = pipeline(0);
104 1.1 jtc if (t != NULL) {
105 1.1 jtc while ((c = token(0)) == LOGAND || c == LOGOR) {
106 1.1 jtc if ((p = pipeline(CONTIN)) == NULL)
107 1.1 jtc syntaxerr((char *) 0);
108 1.1 jtc t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
109 1.1 jtc }
110 1.1 jtc REJECT;
111 1.1 jtc }
112 1.1 jtc return (t);
113 1.1 jtc }
114 1.1 jtc
115 1.1 jtc static struct op *
116 1.1 jtc c_list()
117 1.1 jtc {
118 1.1 jtc register struct op *t, *p, *tl = NULL;
119 1.1 jtc register int c;
120 1.1 jtc
121 1.1 jtc t = andor();
122 1.1 jtc if (t != NULL) {
123 1.1 jtc while ((c = token(0)) == ';' || c == '&' || c == COPROC ||
124 1.1 jtc (c == '\n' && (multiline.on || source->type == SALIAS)))
125 1.1 jtc {
126 1.1 jtc if (c == '&' || c == COPROC) {
127 1.1 jtc int type = c == '&' ? TASYNC : TCOPROC;
128 1.1 jtc if (tl)
129 1.1 jtc tl->right = block(type, tl->right,
130 1.1 jtc NOBLOCK, NOWORDS);
131 1.1 jtc else
132 1.1 jtc t = block(type, t, NOBLOCK, NOWORDS);
133 1.1 jtc }
134 1.1 jtc if ((p = andor()) == NULL)
135 1.1 jtc return (t);
136 1.1 jtc if (tl == NULL)
137 1.1 jtc t = tl = block(TLIST, t, p, NOWORDS);
138 1.1 jtc else
139 1.1 jtc tl = tl->right = block(TLIST, tl->right, p, NOWORDS);
140 1.1 jtc }
141 1.1 jtc REJECT;
142 1.1 jtc }
143 1.1 jtc return (t);
144 1.1 jtc }
145 1.1 jtc
146 1.1 jtc static struct ioword *
147 1.1 jtc synio(cf)
148 1.1 jtc int cf;
149 1.1 jtc {
150 1.1 jtc register struct ioword *iop;
151 1.1 jtc int ishere;
152 1.1 jtc
153 1.1 jtc if (tpeek(cf) != REDIR)
154 1.1 jtc return NULL;
155 1.1 jtc ACCEPT;
156 1.1 jtc iop = yylval.iop;
157 1.1 jtc ishere = (iop->flag&IOTYPE) == IOHERE;
158 1.1 jtc musthave(LWORD, ishere ? HEREDELIM : 0);
159 1.1 jtc if (ishere) {
160 1.1 jtc iop->delim = yylval.cp;
161 1.1 jtc if (*ident != 0) /* unquoted */
162 1.1 jtc iop->flag |= IOEVAL;
163 1.1 jtc if (herep >= &heres[HERES])
164 1.1 jtc yyerror("too many <<'s\n");
165 1.1 jtc *herep++ = iop;
166 1.1 jtc } else
167 1.1 jtc iop->name = yylval.cp;
168 1.1 jtc return iop;
169 1.1 jtc }
170 1.1 jtc
171 1.1 jtc static void
172 1.1 jtc musthave(c, cf)
173 1.1 jtc int c, cf;
174 1.1 jtc {
175 1.1 jtc if ((token(cf)) != c)
176 1.1 jtc syntaxerr((char *) 0);
177 1.1 jtc }
178 1.1 jtc
179 1.1 jtc static struct op *
180 1.1 jtc nested(type, smark, emark)
181 1.1 jtc int type, smark, emark;
182 1.1 jtc {
183 1.1 jtc register struct op *t;
184 1.1 jtc struct multiline_state old_multiline;
185 1.1 jtc
186 1.1 jtc multiline_push(&old_multiline, smark);
187 1.1 jtc t = c_list();
188 1.1 jtc musthave(emark, KEYWORD|ALIAS);
189 1.1 jtc multiline_pop(&old_multiline);
190 1.1 jtc return (block(type, t, NOBLOCK, NOWORDS));
191 1.1 jtc }
192 1.1 jtc
193 1.1 jtc static struct op *
194 1.1 jtc get_command(cf)
195 1.1 jtc int cf;
196 1.1 jtc {
197 1.1 jtc register struct op *t;
198 1.1 jtc register int c, iopn = 0, syniocf;
199 1.1 jtc struct ioword *iop, **iops;
200 1.1 jtc XPtrV args, vars;
201 1.1 jtc struct multiline_state old_multiline;
202 1.1 jtc
203 1.1 jtc iops = (struct ioword **) alloc(sizeofN(struct ioword *, NUFILE+1),
204 1.1 jtc ATEMP);
205 1.1 jtc XPinit(args, 16);
206 1.1 jtc XPinit(vars, 16);
207 1.1 jtc
208 1.1 jtc if (multiline.on)
209 1.1 jtc cf = CONTIN;
210 1.1 jtc syniocf = KEYWORD|ALIAS;
211 1.1 jtc switch (c = token(cf|KEYWORD|ALIAS|VARASN)) {
212 1.1 jtc default:
213 1.1 jtc REJECT;
214 1.1 jtc afree((void*) iops, ATEMP);
215 1.1 jtc XPfree(args);
216 1.1 jtc XPfree(vars);
217 1.1 jtc return NULL; /* empty line */
218 1.1 jtc
219 1.1 jtc case LWORD:
220 1.1 jtc case REDIR:
221 1.1 jtc REJECT;
222 1.1 jtc syniocf &= ~(KEYWORD|ALIAS);
223 1.1 jtc t = newtp(TCOM);
224 1.1 jtc while (1) {
225 1.1 jtc cf = (t->u.evalflags ? ARRAYVAR : 0)
226 1.1 jtc | (XPsize(args) == 0 ? ALIAS|VARASN : CMDWORD);
227 1.1 jtc switch (tpeek(cf)) {
228 1.1 jtc case REDIR:
229 1.1 jtc if (iopn >= NUFILE)
230 1.1 jtc yyerror("too many redirections\n");
231 1.1 jtc iops[iopn++] = synio(cf);
232 1.1 jtc break;
233 1.1 jtc
234 1.1 jtc case LWORD:
235 1.1 jtc ACCEPT;
236 1.1 jtc /* the iopn == 0 and XPsize(vars) == 0 are
237 1.1 jtc * dubious but at&t ksh acts this way
238 1.1 jtc */
239 1.1 jtc if (iopn == 0 && XPsize(vars) == 0
240 1.1 jtc && XPsize(args) == 0
241 1.1 jtc && assign_command(ident))
242 1.1 jtc t->u.evalflags = DOVACHECK;
243 1.1 jtc if ((XPsize(args) == 0 || Flag(FKEYWORD))
244 1.1 jtc && is_wdvarassign(yylval.cp))
245 1.1 jtc XPput(vars, yylval.cp);
246 1.1 jtc else
247 1.1 jtc XPput(args, yylval.cp);
248 1.1 jtc break;
249 1.1 jtc
250 1.1 jtc case '(':
251 1.1 jtc /* Check for "> foo (echo hi)", which at&t ksh
252 1.1 jtc * allows (not POSIX, but not disallowed)
253 1.1 jtc */
254 1.1 jtc afree(t, ATEMP);
255 1.1 jtc if (XPsize(args) == 0 && XPsize(vars) == 0) {
256 1.1 jtc ACCEPT;
257 1.1 jtc goto Subshell;
258 1.1 jtc }
259 1.1 jtc /* Must be a function */
260 1.1 jtc if (iopn != 0 || XPsize(args) != 1
261 1.1 jtc || XPsize(vars) != 0)
262 1.1 jtc syntaxerr((char *) 0);
263 1.1 jtc ACCEPT;
264 1.1 jtc /*(*/
265 1.1 jtc musthave(')', 0);
266 1.1 jtc t = function_body(XPptrv(args)[0], FALSE);
267 1.1 jtc goto Leave;
268 1.1 jtc
269 1.1 jtc default:
270 1.1 jtc goto Leave;
271 1.1 jtc }
272 1.1 jtc }
273 1.1 jtc Leave:
274 1.1 jtc break;
275 1.1 jtc
276 1.1 jtc Subshell:
277 1.1 jtc case '(':
278 1.1 jtc t = nested(TPAREN, '(', ')');
279 1.1 jtc break;
280 1.1 jtc
281 1.1 jtc case '{': /*}*/
282 1.1 jtc t = nested(TBRACE, '{', '}');
283 1.1 jtc break;
284 1.1 jtc
285 1.1 jtc case MDPAREN:
286 1.1 jtc {
287 1.1 jtc static const char let_cmd[] = { CHAR, 'l', CHAR, 'e',
288 1.1 jtc CHAR, 't', EOS };
289 1.1 jtc syniocf &= ~(KEYWORD|ALIAS);
290 1.1 jtc t = newtp(TCOM);
291 1.1 jtc ACCEPT;
292 1.1 jtc XPput(args, wdcopy(let_cmd, ATEMP));
293 1.1 jtc musthave(LWORD,LETEXPR);
294 1.1 jtc XPput(args, yylval.cp);
295 1.1 jtc break;
296 1.1 jtc }
297 1.1 jtc
298 1.1 jtc #ifdef KSH
299 1.1 jtc case DBRACKET: /* [[ .. ]] */
300 1.1 jtc syniocf &= ~(KEYWORD|ALIAS);
301 1.1 jtc t = newtp(TDBRACKET);
302 1.1 jtc ACCEPT;
303 1.1 jtc {
304 1.1 jtc Test_env te;
305 1.1 jtc
306 1.1 jtc te.flags = TEF_DBRACKET;
307 1.1 jtc te.pos.av = &args;
308 1.1 jtc te.isa = dbtestp_isa;
309 1.1 jtc te.getopnd = dbtestp_getopnd;
310 1.1 jtc te.eval = dbtestp_eval;
311 1.1 jtc te.error = dbtestp_error;
312 1.1 jtc
313 1.1 jtc test_parse(&te);
314 1.1 jtc }
315 1.1 jtc break;
316 1.1 jtc #endif /* KSH */
317 1.1 jtc
318 1.1 jtc case FOR:
319 1.1 jtc case SELECT:
320 1.1 jtc t = newtp((c == FOR) ? TFOR : TSELECT);
321 1.1 jtc musthave(LWORD, ARRAYVAR);
322 1.1 jtc if (!is_wdvarname(yylval.cp, TRUE))
323 1.1 jtc yyerror("%s: bad identifier\n",
324 1.1 jtc c == FOR ? "for" : "select");
325 1.1 jtc t->str = str_save(ident, ATEMP);
326 1.1 jtc multiline_push(&old_multiline, c);
327 1.1 jtc t->vars = wordlist();
328 1.1 jtc t->left = dogroup();
329 1.1 jtc multiline_pop(&old_multiline);
330 1.1 jtc break;
331 1.1 jtc
332 1.1 jtc case WHILE:
333 1.1 jtc case UNTIL:
334 1.1 jtc multiline_push(&old_multiline, c);
335 1.1 jtc t = newtp((c == WHILE) ? TWHILE : TUNTIL);
336 1.1 jtc t->left = c_list();
337 1.1 jtc t->right = dogroup();
338 1.1 jtc multiline_pop(&old_multiline);
339 1.1 jtc break;
340 1.1 jtc
341 1.1 jtc case CASE:
342 1.1 jtc t = newtp(TCASE);
343 1.1 jtc musthave(LWORD, 0);
344 1.1 jtc t->str = yylval.cp;
345 1.1 jtc multiline_push(&old_multiline, c);
346 1.1 jtc t->left = caselist();
347 1.1 jtc multiline_pop(&old_multiline);
348 1.1 jtc break;
349 1.1 jtc
350 1.1 jtc case IF:
351 1.1 jtc multiline_push(&old_multiline, c);
352 1.1 jtc t = newtp(TIF);
353 1.1 jtc t->left = c_list();
354 1.1 jtc t->right = thenpart();
355 1.1 jtc musthave(FI, KEYWORD|ALIAS);
356 1.1 jtc multiline_pop(&old_multiline);
357 1.1 jtc break;
358 1.1 jtc
359 1.1 jtc case BANG:
360 1.1 jtc syniocf &= ~(KEYWORD|ALIAS);
361 1.1 jtc t = pipeline(0);
362 1.1 jtc if (t == (struct op *) 0)
363 1.1 jtc syntaxerr((char *) 0);
364 1.1 jtc t = block(TBANG, NOBLOCK, t, NOWORDS);
365 1.1 jtc break;
366 1.1 jtc
367 1.1 jtc case TIME:
368 1.1 jtc syniocf &= ~(KEYWORD|ALIAS);
369 1.1 jtc t = pipeline(0);
370 1.1 jtc t = block(TTIME, t, NOBLOCK, NOWORDS);
371 1.1 jtc break;
372 1.1 jtc
373 1.1 jtc case FUNCTION:
374 1.1 jtc musthave(LWORD, 0);
375 1.1 jtc t = function_body(yylval.cp, TRUE);
376 1.1 jtc break;
377 1.1 jtc }
378 1.1 jtc
379 1.1 jtc while ((iop = synio(syniocf)) != NULL) {
380 1.1 jtc if (iopn >= NUFILE)
381 1.1 jtc yyerror("too many redirections\n");
382 1.1 jtc iops[iopn++] = iop;
383 1.1 jtc }
384 1.1 jtc
385 1.1 jtc if (iopn == 0) {
386 1.1 jtc afree((void*) iops, ATEMP);
387 1.1 jtc t->ioact = NULL;
388 1.1 jtc } else {
389 1.1 jtc iops[iopn++] = NULL;
390 1.1 jtc iops = (struct ioword **) aresize((void*) iops,
391 1.1 jtc sizeofN(struct ioword *, iopn), ATEMP);
392 1.1 jtc t->ioact = iops;
393 1.1 jtc }
394 1.1 jtc
395 1.1 jtc if (t->type == TCOM || t->type == TDBRACKET) {
396 1.1 jtc XPput(args, NULL);
397 1.1 jtc t->args = (char **) XPclose(args);
398 1.1 jtc XPput(vars, NULL);
399 1.1 jtc t->vars = (char **) XPclose(vars);
400 1.1 jtc } else {
401 1.1 jtc XPfree(args);
402 1.1 jtc XPfree(vars);
403 1.1 jtc }
404 1.1 jtc
405 1.1 jtc return t;
406 1.1 jtc }
407 1.1 jtc
408 1.1 jtc static struct op *
409 1.1 jtc dogroup()
410 1.1 jtc {
411 1.1 jtc register int c;
412 1.1 jtc register struct op *list;
413 1.1 jtc
414 1.1 jtc c = token(CONTIN|KEYWORD|ALIAS);
415 1.1 jtc /* A {...} can be used instead of do...done for for/select loops
416 1.1 jtc * but not for while/until loops - we don't need to check if it
417 1.1 jtc * is a while loop because it would have been parsed as part of
418 1.1 jtc * the conditional command list...
419 1.1 jtc */
420 1.1 jtc if (c == DO)
421 1.1 jtc c = DONE;
422 1.1 jtc else if (c == '{')
423 1.1 jtc c = '}';
424 1.1 jtc else
425 1.1 jtc syntaxerr((char *) 0);
426 1.1 jtc list = c_list();
427 1.1 jtc musthave(c, KEYWORD|ALIAS);
428 1.1 jtc return list;
429 1.1 jtc }
430 1.1 jtc
431 1.1 jtc static struct op *
432 1.1 jtc thenpart()
433 1.1 jtc {
434 1.1 jtc register struct op *t;
435 1.1 jtc
436 1.1 jtc musthave(THEN, KEYWORD|ALIAS);
437 1.1 jtc t = newtp(0);
438 1.1 jtc t->left = c_list();
439 1.1 jtc if (t->left == NULL)
440 1.1 jtc syntaxerr((char *) 0);
441 1.1 jtc t->right = elsepart();
442 1.1 jtc return (t);
443 1.1 jtc }
444 1.1 jtc
445 1.1 jtc static struct op *
446 1.1 jtc elsepart()
447 1.1 jtc {
448 1.1 jtc register struct op *t;
449 1.1 jtc
450 1.1 jtc switch (token(KEYWORD|ALIAS|VARASN)) {
451 1.1 jtc case ELSE:
452 1.1 jtc if ((t = c_list()) == NULL)
453 1.1 jtc syntaxerr((char *) 0);
454 1.1 jtc return (t);
455 1.1 jtc
456 1.1 jtc case ELIF:
457 1.1 jtc t = newtp(TELIF);
458 1.1 jtc t->left = c_list();
459 1.1 jtc t->right = thenpart();
460 1.1 jtc return (t);
461 1.1 jtc
462 1.1 jtc default:
463 1.1 jtc REJECT;
464 1.1 jtc }
465 1.1 jtc return NULL;
466 1.1 jtc }
467 1.1 jtc
468 1.1 jtc static struct op *
469 1.1 jtc caselist()
470 1.1 jtc {
471 1.1 jtc register struct op *t, *tl;
472 1.1 jtc int c;
473 1.1 jtc
474 1.1 jtc c = token(CONTIN|KEYWORD|ALIAS);
475 1.1 jtc /* A {...} can be used instead of in...esac for case statements */
476 1.1 jtc if (c == IN)
477 1.1 jtc c = ESAC;
478 1.1 jtc else if (c == '{')
479 1.1 jtc c = '}';
480 1.1 jtc else
481 1.1 jtc syntaxerr((char *) 0);
482 1.1 jtc t = tl = NULL;
483 1.1 jtc while ((tpeek(CONTIN|KEYWORD|ESACONLY)) != c) { /* no ALIAS here */
484 1.1 jtc struct op *tc = casepart(c);
485 1.1 jtc if (tl == NULL)
486 1.1 jtc t = tl = tc, tl->right = NULL;
487 1.1 jtc else
488 1.1 jtc tl->right = tc, tl = tc;
489 1.1 jtc }
490 1.1 jtc musthave(c, KEYWORD|ALIAS);
491 1.1 jtc return (t);
492 1.1 jtc }
493 1.1 jtc
494 1.1 jtc static struct op *
495 1.1 jtc casepart(endtok)
496 1.1 jtc int endtok;
497 1.1 jtc {
498 1.1 jtc register struct op *t;
499 1.1 jtc register int c;
500 1.1 jtc XPtrV ptns;
501 1.1 jtc
502 1.1 jtc XPinit(ptns, 16);
503 1.1 jtc t = newtp(TPAT);
504 1.1 jtc c = token(CONTIN|KEYWORD); /* no ALIAS here */
505 1.1 jtc if (c != '(')
506 1.1 jtc REJECT;
507 1.1 jtc do {
508 1.1 jtc musthave(LWORD, 0);
509 1.1 jtc XPput(ptns, yylval.cp);
510 1.1 jtc } while ((c = token(0)) == '|');
511 1.1 jtc REJECT;
512 1.1 jtc XPput(ptns, NULL);
513 1.1 jtc t->vars = (char **) XPclose(ptns);
514 1.1 jtc musthave(')', 0);
515 1.1 jtc
516 1.1 jtc t->left = c_list();
517 1.1 jtc if ((tpeek(CONTIN|KEYWORD|ALIAS)) != endtok)
518 1.1 jtc musthave(BREAK, CONTIN|KEYWORD|ALIAS);
519 1.1 jtc return (t);
520 1.1 jtc }
521 1.1 jtc
522 1.1 jtc static struct op *
523 1.1 jtc function_body(name, ksh_func)
524 1.1 jtc char *name;
525 1.1 jtc int ksh_func; /* function foo { ... } vs foo() { .. } */
526 1.1 jtc {
527 1.1 jtc XString xs;
528 1.1 jtc char *xp, *p;
529 1.1 jtc struct op *t;
530 1.1 jtc int old_func_parse;
531 1.1 jtc
532 1.1 jtc Xinit(xs, xp, 16, ATEMP);
533 1.1 jtc for (p = name; ; ) {
534 1.1 jtc if ((*p == EOS && Xlength(xs, xp) == 0)
535 1.1 jtc || (*p != EOS && *p != CHAR && *p != QCHAR
536 1.1 jtc && *p != OQUOTE && *p != CQUOTE))
537 1.1 jtc {
538 1.1 jtc p = snptreef((char *) 0, 32, "%S", name);
539 1.1 jtc yyerror("%s: invalid function name\n", p);
540 1.1 jtc }
541 1.1 jtc Xcheck(xs, xp);
542 1.1 jtc if (*p == EOS) {
543 1.1 jtc Xput(xs, xp, '\0');
544 1.1 jtc break;
545 1.1 jtc } else if (*p == CHAR || *p == QCHAR) {
546 1.1 jtc Xput(xs, xp, p[1]);
547 1.1 jtc p += 2;
548 1.1 jtc } else
549 1.1 jtc p++; /* OQUOTE/CQUOTE */
550 1.1 jtc }
551 1.1 jtc t = newtp(TFUNCT);
552 1.1 jtc t->str = Xclose(xs, xp);
553 1.1 jtc t->u.ksh_func = ksh_func;
554 1.1 jtc
555 1.1 jtc /* Note that POSIX allows only compound statements after foo(), sh and
556 1.1 jtc * at&t ksh allow any command, go with the later since it shouldn't
557 1.1 jtc * break anything. However, for function foo, at&t ksh only accepts
558 1.1 jtc * an open-brace.
559 1.1 jtc */
560 1.1 jtc if (ksh_func) {
561 1.1 jtc musthave('{', CONTIN|KEYWORD|ALIAS); /* } */
562 1.1 jtc REJECT;
563 1.1 jtc }
564 1.1 jtc
565 1.1 jtc old_func_parse = e->flags & EF_FUNC_PARSE;
566 1.1 jtc e->flags |= EF_FUNC_PARSE;
567 1.1 jtc if ((t->left = get_command(CONTIN)) == (struct op *) 0) {
568 1.1 jtc /* create empty command so foo(): will work */
569 1.1 jtc t->left = newtp(TCOM);
570 1.1 jtc t->args = (char **) alloc(sizeof(char *), ATEMP);
571 1.1 jtc t->args[0] = (char *) 0;
572 1.1 jtc t->vars = (char **) alloc(sizeof(char *), ATEMP);
573 1.1 jtc t->vars[0] = (char *) 0;
574 1.1 jtc }
575 1.1 jtc if (!old_func_parse)
576 1.1 jtc e->flags &= ~EF_FUNC_PARSE;
577 1.1 jtc
578 1.1 jtc return t;
579 1.1 jtc }
580 1.1 jtc
581 1.1 jtc static char **
582 1.1 jtc wordlist()
583 1.1 jtc {
584 1.1 jtc register int c;
585 1.1 jtc XPtrV args;
586 1.1 jtc
587 1.1 jtc XPinit(args, 16);
588 1.1 jtc if ((c = token(CONTIN|KEYWORD|ALIAS)) != IN) {
589 1.1 jtc if (c != ';') /* non-POSIX, but at&t ksh accepts a ; here */
590 1.1 jtc REJECT;
591 1.1 jtc return NULL;
592 1.1 jtc }
593 1.1 jtc while ((c = token(0)) == LWORD)
594 1.1 jtc XPput(args, yylval.cp);
595 1.1 jtc if (c != '\n' && c != ';')
596 1.1 jtc syntaxerr((char *) 0);
597 1.1 jtc if (XPsize(args) == 0) {
598 1.1 jtc XPfree(args);
599 1.1 jtc return NULL;
600 1.1 jtc } else {
601 1.1 jtc XPput(args, NULL);
602 1.1 jtc return (char **) XPclose(args);
603 1.1 jtc }
604 1.1 jtc }
605 1.1 jtc
606 1.1 jtc /*
607 1.1 jtc * supporting functions
608 1.1 jtc */
609 1.1 jtc
610 1.1 jtc static struct op *
611 1.1 jtc block(type, t1, t2, wp)
612 1.1 jtc int type;
613 1.1 jtc struct op *t1, *t2;
614 1.1 jtc char **wp;
615 1.1 jtc {
616 1.1 jtc register struct op *t;
617 1.1 jtc
618 1.1 jtc t = newtp(type);
619 1.1 jtc t->left = t1;
620 1.1 jtc t->right = t2;
621 1.1 jtc t->vars = wp;
622 1.1 jtc return (t);
623 1.1 jtc }
624 1.1 jtc
625 1.1 jtc const struct tokeninfo {
626 1.1 jtc const char *name;
627 1.1 jtc short val;
628 1.1 jtc short reserved;
629 1.1 jtc } tokentab[] = {
630 1.1 jtc /* Reserved words */
631 1.1 jtc { "if", IF, TRUE },
632 1.1 jtc { "then", THEN, TRUE },
633 1.1 jtc { "else", ELSE, TRUE },
634 1.1 jtc { "elif", ELIF, TRUE },
635 1.1 jtc { "fi", FI, TRUE },
636 1.1 jtc { "case", CASE, TRUE },
637 1.1 jtc { "esac", ESAC, TRUE },
638 1.1 jtc { "for", FOR, TRUE },
639 1.1 jtc #ifdef KSH
640 1.1 jtc { "select", SELECT, TRUE },
641 1.1 jtc #endif /* KSH */
642 1.1 jtc { "while", WHILE, TRUE },
643 1.1 jtc { "until", UNTIL, TRUE },
644 1.1 jtc { "do", DO, TRUE },
645 1.1 jtc { "done", DONE, TRUE },
646 1.1 jtc { "in", IN, TRUE },
647 1.1 jtc { "function", FUNCTION, TRUE },
648 1.1 jtc { "time", TIME, TRUE },
649 1.1 jtc { "{", '{', TRUE },
650 1.1 jtc { "}", '}', TRUE },
651 1.1 jtc { "!", BANG, TRUE },
652 1.1 jtc #ifdef KSH
653 1.1 jtc { "[[", DBRACKET, TRUE },
654 1.1 jtc #endif /* KSH */
655 1.1 jtc /* Lexical tokens (0[EOF], LWORD and REDIR handled specially) */
656 1.1 jtc { "&&", LOGAND, FALSE },
657 1.1 jtc { "||", LOGOR, FALSE },
658 1.1 jtc { ";;", BREAK, FALSE },
659 1.1 jtc { "((", MDPAREN, FALSE },
660 1.1 jtc #ifdef KSH
661 1.1 jtc { "|&", COPROC, FALSE },
662 1.1 jtc #endif /* KSH */
663 1.1 jtc /* and some special cases... */
664 1.1 jtc { "newline", '\n', FALSE },
665 1.1 jtc { 0 }
666 1.1 jtc };
667 1.1 jtc
668 1.1 jtc void
669 1.1 jtc initkeywords()
670 1.1 jtc {
671 1.1 jtc register struct tokeninfo const *tt;
672 1.1 jtc register struct tbl *p;
673 1.1 jtc
674 1.1 jtc tinit(&keywords, APERM, 32); /* must be 2^n (currently 20 keywords) */
675 1.1 jtc for (tt = tokentab; tt->name; tt++) {
676 1.1 jtc if (tt->reserved) {
677 1.1 jtc p = tenter(&keywords, tt->name, hash(tt->name));
678 1.1 jtc p->flag |= DEFINED|ISSET;
679 1.1 jtc p->type = CKEYWD;
680 1.1 jtc p->val.i = tt->val;
681 1.1 jtc }
682 1.1 jtc }
683 1.1 jtc }
684 1.1 jtc
685 1.1 jtc static void
686 1.1 jtc syntaxerr(what)
687 1.1 jtc const char *what;
688 1.1 jtc {
689 1.1 jtc char redir[6]; /* 2<<- is the longest redirection, I think */
690 1.1 jtc const char *s;
691 1.1 jtc struct tokeninfo const *tt;
692 1.1 jtc int c;
693 1.1 jtc
694 1.1 jtc if (!what)
695 1.1 jtc what = "unexpected";
696 1.1 jtc REJECT;
697 1.1 jtc c = token(0);
698 1.1 jtc Again:
699 1.1 jtc switch (c) {
700 1.1 jtc case 0:
701 1.1 jtc if (multiline.on && multiline.start_token) {
702 1.1 jtc multiline.on = FALSE; /* avoid infinate loops */
703 1.1 jtc c = multiline.start_token;
704 1.1 jtc source->errline = multiline.start_line;
705 1.1 jtc what = "unmatched";
706 1.1 jtc goto Again;
707 1.1 jtc }
708 1.1 jtc /* don't quote the EOF */
709 1.1 jtc yyerror("syntax error: unexpected EOF\n");
710 1.1 jtc /*NOTREACHED*/
711 1.1 jtc
712 1.1 jtc case LWORD:
713 1.1 jtc s = snptreef((char *) 0, 32, "%S", yylval.cp);
714 1.1 jtc break;
715 1.1 jtc
716 1.1 jtc case REDIR:
717 1.1 jtc s = snptreef(redir, sizeof(redir), "%R", yylval.iop);
718 1.1 jtc break;
719 1.1 jtc
720 1.1 jtc default:
721 1.1 jtc for (tt = tokentab; tt->name; tt++)
722 1.1 jtc if (tt->val == c)
723 1.1 jtc break;
724 1.1 jtc if (tt->name)
725 1.1 jtc s = tt->name;
726 1.1 jtc else {
727 1.1 jtc if (c > 0 && c < 256) {
728 1.1 jtc redir[0] = c;
729 1.1 jtc redir[1] = '\0';
730 1.1 jtc } else
731 1.1 jtc shf_snprintf(redir, sizeof(redir),
732 1.1 jtc "?%d", c);
733 1.1 jtc s = redir;
734 1.1 jtc }
735 1.1 jtc }
736 1.1 jtc yyerror("syntax error: `%s' %s\n", s, what);
737 1.1 jtc }
738 1.1 jtc
739 1.1 jtc static void
740 1.1 jtc multiline_push(save, tok)
741 1.1 jtc struct multiline_state *save;
742 1.1 jtc int tok;
743 1.1 jtc {
744 1.1 jtc *save = multiline;
745 1.1 jtc multiline.on = TRUE;
746 1.1 jtc multiline.start_token = tok;
747 1.1 jtc multiline.start_line = source->line;
748 1.1 jtc }
749 1.1 jtc
750 1.1 jtc static void
751 1.1 jtc multiline_pop(saved)
752 1.1 jtc struct multiline_state *saved;
753 1.1 jtc {
754 1.1 jtc multiline = *saved;
755 1.1 jtc }
756 1.1 jtc
757 1.1 jtc static struct op *
758 1.1 jtc newtp(type)
759 1.1 jtc int type;
760 1.1 jtc {
761 1.1 jtc register struct op *t;
762 1.1 jtc
763 1.1 jtc t = (struct op *) alloc(sizeof(*t), ATEMP);
764 1.1 jtc t->type = type;
765 1.1 jtc t->u.evalflags = 0;
766 1.1 jtc t->args = t->vars = NULL;
767 1.1 jtc t->ioact = NULL;
768 1.1 jtc t->left = t->right = NULL;
769 1.1 jtc t->str = NULL;
770 1.1 jtc return (t);
771 1.1 jtc }
772 1.1 jtc
773 1.1 jtc struct op *
774 1.1 jtc compile(s)
775 1.1 jtc Source *s;
776 1.1 jtc {
777 1.1 jtc yynerrs = 0;
778 1.1 jtc multiline.on = s->type == SSTRING;
779 1.1 jtc multiline.start_token = 0;
780 1.1 jtc multiline.start_line = 0;
781 1.1 jtc herep = heres;
782 1.1 jtc source = s;
783 1.1 jtc yyparse();
784 1.1 jtc return outtree;
785 1.1 jtc }
786 1.1 jtc
787 1.1 jtc /* This kludge exists to take care of sh/at&t ksh oddity in which
788 1.1 jtc * the arguments of alias/export/readonly/typeset have no field
789 1.1 jtc * splitting, file globbing, or (normal) tilde expansion done.
790 1.1 jtc * at&t ksh seems to do something similar to this since
791 1.1 jtc * $ touch a=a; typeset a=[ab]; echo "$a"
792 1.1 jtc * a=[ab]
793 1.1 jtc * $ x=typeset; $x a=[ab]; echo "$a"
794 1.1 jtc * a=a
795 1.1 jtc * $
796 1.1 jtc */
797 1.1 jtc static int
798 1.1 jtc assign_command(s)
799 1.1 jtc char *s;
800 1.1 jtc {
801 1.1 jtc char c = *s;
802 1.1 jtc
803 1.1 jtc if (Flag(FPOSIX) || !*s)
804 1.1 jtc return 0;
805 1.1 jtc return (c == 'a' && strcmp(s, "alias") == 0)
806 1.1 jtc || (c == 'e' && strcmp(s, "export") == 0)
807 1.1 jtc || (c == 'r' && strcmp(s, "readonly") == 0)
808 1.1 jtc || (c == 't' && strcmp(s, "typeset") == 0);
809 1.1 jtc }
810 1.1 jtc
811 1.1 jtc
812 1.1 jtc #ifdef KSH
813 1.1 jtc /* Order important - indexed by Test_meta values
814 1.1 jtc * Note that ||, &&, ( and ) can't appear in as unquoted strings
815 1.1 jtc * in normal shell input, so these can be interpreted unambiguously
816 1.1 jtc * in the evaluation pass.
817 1.1 jtc */
818 1.1 jtc static const char dbtest_or[] = { CHAR, '|', CHAR, '|', EOS };
819 1.1 jtc static const char dbtest_and[] = { CHAR, '&', CHAR, '&', EOS };
820 1.1 jtc static const char dbtest_not[] = { CHAR, '!', EOS };
821 1.1 jtc static const char dbtest_oparen[] = { CHAR, '(', EOS };
822 1.1 jtc static const char dbtest_cparen[] = { CHAR, ')', EOS };
823 1.1 jtc const char *const dbtest_tokens[] = {
824 1.1 jtc dbtest_or, dbtest_and, dbtest_not,
825 1.1 jtc dbtest_oparen, dbtest_cparen
826 1.1 jtc };
827 1.1 jtc const char db_close[] = { CHAR, ']', CHAR, ']', EOS };
828 1.1 jtc const char db_lthan[] = { CHAR, '<', EOS };
829 1.1 jtc const char db_gthan[] = { CHAR, '>', EOS };
830 1.1 jtc
831 1.1 jtc /* Test if the current token is a whatever. Accepts the current token if
832 1.1 jtc * it is. Returns 0 if it is not, non-zero if it is (in the case of
833 1.1 jtc * TM_UNOP and TM_BINOP, the returned value is a Test_op).
834 1.1 jtc */
835 1.1 jtc static int
836 1.1 jtc dbtestp_isa(te, meta)
837 1.1 jtc Test_env *te;
838 1.1 jtc Test_meta meta;
839 1.1 jtc {
840 1.1 jtc int c = tpeek(ARRAYVAR | (meta == TM_BINOP ? 0 : CONTIN));
841 1.1 jtc int uqword = 0;
842 1.1 jtc char *save = (char *) 0;
843 1.1 jtc int ret = 0;
844 1.1 jtc
845 1.1 jtc /* unquoted word? */
846 1.1 jtc uqword = c == LWORD && *ident;
847 1.1 jtc
848 1.1 jtc if (meta == TM_OR)
849 1.1 jtc ret = c == LOGOR;
850 1.1 jtc else if (meta == TM_AND)
851 1.1 jtc ret = c == LOGAND;
852 1.1 jtc else if (meta == TM_NOT)
853 1.1 jtc ret = uqword && strcmp(yylval.cp, dbtest_tokens[(int) TM_NOT]) == 0;
854 1.1 jtc else if (meta == TM_OPAREN)
855 1.1 jtc ret = c == '(' /*)*/;
856 1.1 jtc else if (meta == TM_CPAREN)
857 1.1 jtc ret = c == /*(*/ ')';
858 1.1 jtc else if (meta == TM_UNOP || meta == TM_BINOP) {
859 1.1 jtc if (meta == TM_BINOP && c == REDIR
860 1.1 jtc && (yylval.iop->flag == IOREAD
861 1.1 jtc || yylval.iop->flag == IOWRITE))
862 1.1 jtc {
863 1.1 jtc ret = 1;
864 1.1 jtc save = wdcopy(yylval.iop->flag == IOREAD ?
865 1.1 jtc db_lthan : db_gthan, ATEMP);
866 1.1 jtc } else if (uqword && (ret = (int) test_isop(te, meta, ident)))
867 1.1 jtc save = yylval.cp;
868 1.1 jtc } else /* meta == TM_END */
869 1.1 jtc ret = uqword && strcmp(yylval.cp, db_close) == 0;
870 1.1 jtc if (ret) {
871 1.1 jtc ACCEPT;
872 1.1 jtc if (meta != TM_END) {
873 1.1 jtc if (!save)
874 1.1 jtc save = wdcopy(dbtest_tokens[(int) meta], ATEMP);
875 1.1 jtc XPput(*te->pos.av, save);
876 1.1 jtc }
877 1.1 jtc }
878 1.1 jtc return ret;
879 1.1 jtc }
880 1.1 jtc
881 1.1 jtc static const char *
882 1.1 jtc dbtestp_getopnd(te, op, do_eval)
883 1.1 jtc Test_env *te;
884 1.1 jtc Test_op op;
885 1.1 jtc int do_eval;
886 1.1 jtc {
887 1.1 jtc int c = tpeek(ARRAYVAR);
888 1.1 jtc
889 1.1 jtc if (c != LWORD)
890 1.1 jtc return (const char *) 0;
891 1.1 jtc
892 1.1 jtc ACCEPT;
893 1.1 jtc XPput(*te->pos.av, yylval.cp);
894 1.1 jtc
895 1.1 jtc return null;
896 1.1 jtc }
897 1.1 jtc
898 1.1 jtc static int
899 1.1 jtc dbtestp_eval(te, op, opnd1, opnd2, do_eval)
900 1.1 jtc Test_env *te;
901 1.1 jtc Test_op op;
902 1.1 jtc const char *opnd1;
903 1.1 jtc const char *opnd2;
904 1.1 jtc int do_eval;
905 1.1 jtc {
906 1.1 jtc return 1;
907 1.1 jtc }
908 1.1 jtc
909 1.1 jtc static void
910 1.1 jtc dbtestp_error(te, offset, msg)
911 1.1 jtc Test_env *te;
912 1.1 jtc int offset;
913 1.1 jtc const char *msg;
914 1.1 jtc {
915 1.1 jtc te->flags |= TEF_ERROR;
916 1.1 jtc
917 1.1 jtc if (offset < 0) {
918 1.1 jtc REJECT;
919 1.1 jtc /* Kludgy to say the least... */
920 1.1 jtc symbol = LWORD;
921 1.1 jtc yylval.cp = *(XPptrv(*te->pos.av) + XPsize(*te->pos.av)
922 1.1 jtc + offset);
923 1.1 jtc }
924 1.1 jtc syntaxerr(msg);
925 1.1 jtc }
926 1.1 jtc #endif /* KSH */
927