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