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