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