9 /* -------- csyn.c -------- */
11 * shell: syntax (C version)
40 #define CONTIN 01 /* skip new lines to complete command */
43 #define SYNTAXERR zzerr()
47 static int iounit = IODEFAULT;
49 static YYSTYPE yylval;
51 _PROTOTYPE(static struct op *pipeline, (int cf ));
52 _PROTOTYPE(static struct op *andor, (void));
53 _PROTOTYPE(static struct op *c_list, (void));
54 _PROTOTYPE(static int synio, (int cf ));
55 _PROTOTYPE(static void musthave, (int c, int cf ));
56 _PROTOTYPE(static struct op *simple, (void));
57 _PROTOTYPE(static struct op *nested, (int type, int mark ));
58 _PROTOTYPE(static struct op *command, (int cf ));
59 _PROTOTYPE(static struct op *dogroup, (int onlydone ));
60 _PROTOTYPE(static struct op *thenpart, (void));
61 _PROTOTYPE(static struct op *elsepart, (void));
62 _PROTOTYPE(static struct op *caselist, (void));
63 _PROTOTYPE(static struct op *casepart, (void));
64 _PROTOTYPE(static char **pattern, (void));
65 _PROTOTYPE(static char **wordlist, (void));
66 _PROTOTYPE(static struct op *list, (struct op *t1, struct op *t2 ));
67 _PROTOTYPE(static struct op *block, (int type, struct op *t1, struct op *t2, char **wp ));
68 _PROTOTYPE(static struct op *newtp, (void));
69 _PROTOTYPE(static struct op *namelist, (struct op *t ));
70 _PROTOTYPE(static char **copyw, (void));
71 _PROTOTYPE(static void word, (char *cp ));
72 _PROTOTYPE(static struct ioword **copyio, (void));
73 _PROTOTYPE(static struct ioword *io, (int u, int f, char *cp ));
74 _PROTOTYPE(static void zzerr, (void));
75 _PROTOTYPE(void yyerror, (char *s ));
76 _PROTOTYPE(static int yylex, (int cf ));
77 _PROTOTYPE(int collect, (int c, int c1 ));
78 _PROTOTYPE(int dual, (int c ));
79 _PROTOTYPE(static void diag, (int ec ));
80 _PROTOTYPE(static char *tree, (unsigned size ));
81 _PROTOTYPE(void printf, (char *s ));
98 register struct op *t, *p;
103 while ((c = yylex(0)) == '|') {
104 if ((p = command(CONTIN)) == NULL)
106 if (t->type != TPAREN && t->type != TCOM) {
107 /* shell statement */
108 t = block(TPAREN, t, NOBLOCK, NOWORDS);
110 t = block(TPIPE, t, p, NOWORDS);
120 register struct op *t, *p;
125 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
126 if ((p = pipeline(CONTIN)) == NULL)
128 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
138 register struct op *t, *p;
143 if((peeksym = yylex(0)) == '&')
144 t = block(TASYNC, t, NOBLOCK, NOWORDS);
145 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
146 if ((p = andor()) == NULL)
148 if((peeksym = yylex(0)) == '&')
149 p = block(TASYNC, p, NOBLOCK, NOWORDS);
162 register struct ioword *iop;
166 if ((c = yylex(cf)) != '<' && c != '>') {
172 iop = io(iounit, i, yylval.cp);
175 markhere(yylval.cp, iop);
183 if ((peeksym = yylex(cf)) != c)
191 register struct op *t;
195 switch (peeksym = yylex(0)) {
220 register struct op *t;
226 return(block(type, t, NOBLOCK, NOWORDS));
233 register struct op *t;
234 struct wdblock *iosave;
243 switch (c = yylex(cf)) {
246 if ((t = simple()) == NULL) {
248 return((struct op *)NULL);
255 t = nested(TPAREN, ')');
259 t = nested(TBRACE, '}');
269 t->words = wordlist();
270 if ((c = yylex(0)) != '\n' && c != ';')
272 t->left = dogroup(0);
280 t->type = c == WHILE? TWHILE: TUNTIL;
282 t->right = dogroup(1);
294 musthave(IN, CONTIN);
296 t->left = caselist();
306 t->right = thenpart();
323 register struct op *list;
326 if (c == DONE && onlydone)
327 return((struct op *)NULL);
339 register struct op *t;
341 if ((c = yylex(0)) != THEN) {
343 return((struct op *)NULL);
350 t->right = elsepart();
358 register struct op *t;
360 switch (c = yylex(0)) {
362 if ((t = c_list()) == NULL)
370 t->right = thenpart();
375 return((struct op *)NULL);
382 register struct op *t;
385 while ((peeksym = yylex(CONTIN)) != ESAC)
386 t = list(t, casepart());
393 register struct op *t;
397 t->words = pattern();
400 if ((peeksym = yylex(CONTIN)) != ESAC)
401 musthave(BREAK, CONTIN);
415 } while ((c = yylex(0)) == '|');
426 if ((c = yylex(0)) != IN) {
428 return((char **)NULL);
431 while ((c = yylex(0)) == WORD)
439 * supporting functions
443 register struct op *t1, *t2;
449 return(block(TLIST, t1, t2, NOWORDS));
453 block(type, t1, t2, wp)
458 register struct op *t;
499 register struct res *rp;
501 for (rp = restab; rp->r_name; rp++)
502 if (strcmp(rp->r_name, n) == 0)
510 register struct op *t;
512 t = (struct op *)tree(sizeof(*t));
524 register struct op *t;
527 iolist = addword((char *)NULL, iolist);
531 if (t->type != TCOM) {
532 if (t->type != TPAREN && t->ioact != NULL) {
533 t = block(TPAREN, t, NOBLOCK, NOWORDS);
534 t->ioact = t->left->ioact;
535 t->left->ioact = NULL;
549 wd = getwords(wdlist);
558 wdlist = addword(cp, wdlist);
561 static struct ioword **
564 register struct ioword **iop;
566 iop = (struct ioword **) getwords(iolist);
571 static struct ioword *
577 register struct ioword *iop;
579 iop = (struct ioword *) tree(sizeof(*iop));
583 iolist = addword((char *)iop, iolist);
590 yyerror("syntax error");
598 if (talking && e.iop <= iostack) {
600 while (eofc() == 0 && yylex(0) != '\n')
614 if ((c = peeksym) > 0) {
627 while ((c = getc(0)) == ' ' || c == '\t')
631 if (any(c, "0123456789")) {
633 if (c1 == '<' || c1 == '>') {
643 while ((c = getc(0)) != 0 && c != '\n')
653 if ((c = getc(0)) == '{') {
654 if ((c = collect(c, '}')) != '\0')
663 if ((c = collect(c, c)) != '\0')
670 if ((c1 = dual(c)) != '\0') {
688 if (multiline || cf & CONTIN) {
689 if (talking && e.iop <= iostack)
705 while ((c = getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
706 if (e.linep >= elinep)
707 err("word too long");
714 if (atstart && (c = rlookup(line))!=0) {
718 yylval.cp = strsave(line, areanum);
729 while ((c = getc(c1)) != c1) {
734 prs("no closing "); yyerror(s);
737 if (talking && c == '\n' && e.iop <= iostack)
750 register char *cp = s;
755 if ((c = rlookup(s)) == 0)
767 if (c == '>' || c == '<') {
770 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
773 yylval.i = ec == '>'? IOWRITE: IOREAD;
774 if (c != '&' || yylval.i == IOHERE)
786 if ((t = getcell(size)) == NULL) {
787 prs("command line too complicated\n");
797 printf(s) /* yyparse calls it */