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