17c478bd9Sstevel@tonic-gate /*
26c02b4a4Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include "sh.h"
167c478bd9Sstevel@tonic-gate #include "sh.tconst.h"
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate * C shell
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
226c02b4a4Smuffin void asyntax(struct wordent *, struct wordent *);
236c02b4a4Smuffin void asyn0(struct wordent *, struct wordent *);
246c02b4a4Smuffin void asyn3(struct wordent *, struct wordent *);
256c02b4a4Smuffin void chr_blkfree(char **);
266c02b4a4Smuffin struct command *syn0(struct wordent *, struct wordent *, int);
276c02b4a4Smuffin struct command *syn1(struct wordent *, struct wordent *, int);
286c02b4a4Smuffin struct command *syn1a(struct wordent *, struct wordent *, int);
296c02b4a4Smuffin struct command *syn1b(struct wordent *, struct wordent *, int);
306c02b4a4Smuffin struct command *syn2(struct wordent *, struct wordent *, int);
316c02b4a4Smuffin struct command *syn3(struct wordent *, struct wordent *, int);
326c02b4a4Smuffin struct wordent *freenod(struct wordent *, struct wordent *);
336c02b4a4Smuffin
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate * Perform aliasing on the word list lex
367c478bd9Sstevel@tonic-gate * Do a (very rudimentary) parse to separate into commands.
377c478bd9Sstevel@tonic-gate * If word 0 of a command has an alias, do it.
387c478bd9Sstevel@tonic-gate * Repeat a maximum of 20 times.
397c478bd9Sstevel@tonic-gate */
406c02b4a4Smuffin void
alias(struct wordent * lex)416c02b4a4Smuffin alias(struct wordent *lex)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate int aleft = 21;
447c478bd9Sstevel@tonic-gate jmp_buf osetexit;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #ifdef TRACE
477c478bd9Sstevel@tonic-gate tprintf("TRACE- alias()\n");
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate getexit(osetexit);
507c478bd9Sstevel@tonic-gate setexit();
517c478bd9Sstevel@tonic-gate if (haderr) {
527c478bd9Sstevel@tonic-gate resexit(osetexit);
537c478bd9Sstevel@tonic-gate reset();
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate if (--aleft == 0)
567c478bd9Sstevel@tonic-gate error("Alias loop");
577c478bd9Sstevel@tonic-gate asyntax(lex->next, lex);
587c478bd9Sstevel@tonic-gate resexit(osetexit);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
616c02b4a4Smuffin void
asyntax(struct wordent * p1,struct wordent * p2)626c02b4a4Smuffin asyntax(struct wordent *p1, struct wordent *p2)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate #ifdef TRACE
657c478bd9Sstevel@tonic-gate tprintf("TRACE- asyntax()\n");
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate while (p1 != p2)
697c478bd9Sstevel@tonic-gate /* if (any(p1->word[0], ";&\n")) */ /* For char -> tchar */
707c478bd9Sstevel@tonic-gate if (p1->word[0] == ';' ||
717c478bd9Sstevel@tonic-gate p1->word[0] == '&' ||
727c478bd9Sstevel@tonic-gate p1->word[0] == '\n')
737c478bd9Sstevel@tonic-gate p1 = p1->next;
747c478bd9Sstevel@tonic-gate else {
757c478bd9Sstevel@tonic-gate asyn0(p1, p2);
767c478bd9Sstevel@tonic-gate return;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
806c02b4a4Smuffin void
asyn0(struct wordent * p1,struct wordent * p2)816c02b4a4Smuffin asyn0(struct wordent *p1, struct wordent *p2)
827c478bd9Sstevel@tonic-gate {
836c02b4a4Smuffin struct wordent *p;
846c02b4a4Smuffin int l = 0;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #ifdef TRACE
877c478bd9Sstevel@tonic-gate tprintf("TRACE- asyn0()\n");
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
907c478bd9Sstevel@tonic-gate switch (p->word[0]) {
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate case '(':
937c478bd9Sstevel@tonic-gate l++;
947c478bd9Sstevel@tonic-gate continue;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate case ')':
977c478bd9Sstevel@tonic-gate l--;
987c478bd9Sstevel@tonic-gate if (l < 0)
997c478bd9Sstevel@tonic-gate error("Too many )'s");
1007c478bd9Sstevel@tonic-gate continue;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate case '>':
1037c478bd9Sstevel@tonic-gate if (p->next != p2 && eq(p->next->word, S_AND /* "&"*/))
1047c478bd9Sstevel@tonic-gate p = p->next;
1057c478bd9Sstevel@tonic-gate continue;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate case '&':
1087c478bd9Sstevel@tonic-gate case '|':
1097c478bd9Sstevel@tonic-gate case ';':
1107c478bd9Sstevel@tonic-gate case '\n':
1117c478bd9Sstevel@tonic-gate if (l != 0)
1127c478bd9Sstevel@tonic-gate continue;
1137c478bd9Sstevel@tonic-gate asyn3(p1, p);
1147c478bd9Sstevel@tonic-gate asyntax(p->next, p2);
1157c478bd9Sstevel@tonic-gate return;
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate if (l == 0)
1187c478bd9Sstevel@tonic-gate asyn3(p1, p2);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1216c02b4a4Smuffin void
asyn3(struct wordent * p1,struct wordent * p2)1226c02b4a4Smuffin asyn3(struct wordent *p1, struct wordent *p2)
1237c478bd9Sstevel@tonic-gate {
1246c02b4a4Smuffin struct varent *ap;
1257c478bd9Sstevel@tonic-gate struct wordent alout;
1266c02b4a4Smuffin bool redid;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate #ifdef TRACE
1297c478bd9Sstevel@tonic-gate tprintf("TRACE- asyn3()\n");
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate if (p1 == p2)
1327c478bd9Sstevel@tonic-gate return;
1337c478bd9Sstevel@tonic-gate if (p1->word[0] == '(') {
1347c478bd9Sstevel@tonic-gate for (p2 = p2->prev; p2->word[0] != ')'; p2 = p2->prev)
1357c478bd9Sstevel@tonic-gate if (p2 == p1)
1367c478bd9Sstevel@tonic-gate return;
1377c478bd9Sstevel@tonic-gate if (p2 == p1->next)
1387c478bd9Sstevel@tonic-gate return;
1397c478bd9Sstevel@tonic-gate asyn0(p1->next, p2);
1407c478bd9Sstevel@tonic-gate return;
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate ap = adrof1(p1->word, &aliases);
1437c478bd9Sstevel@tonic-gate if (ap == 0)
1447c478bd9Sstevel@tonic-gate return;
1457c478bd9Sstevel@tonic-gate alhistp = p1->prev;
1467c478bd9Sstevel@tonic-gate alhistt = p2;
1477c478bd9Sstevel@tonic-gate alvec = ap->vec;
1487c478bd9Sstevel@tonic-gate redid = lex(&alout);
1497c478bd9Sstevel@tonic-gate alhistp = alhistt = 0;
1507c478bd9Sstevel@tonic-gate alvec = 0;
151*258f91c6SToomas Soome if (err_msg) {
1527c478bd9Sstevel@tonic-gate freelex(&alout);
153*258f91c6SToomas Soome error("%s", gettext(err_msg));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate if (p1->word[0] && eq(p1->word, alout.next->word)) {
1567c478bd9Sstevel@tonic-gate tchar *cp = alout.next->word;
1577c478bd9Sstevel@tonic-gate
15865b0c20eSnakanon alout.next->word = strspl(S_TOPBIT /* "\200" */, cp);
15965b0c20eSnakanon xfree(cp);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate p1 = freenod(p1, redid ? p2 : p1->next);
1627c478bd9Sstevel@tonic-gate if (alout.next != &alout) {
1637c478bd9Sstevel@tonic-gate p1->next->prev = alout.prev->prev;
1647c478bd9Sstevel@tonic-gate alout.prev->prev->next = p1->next;
1657c478bd9Sstevel@tonic-gate alout.next->prev = p1;
1667c478bd9Sstevel@tonic-gate p1->next = alout.next;
16765b0c20eSnakanon xfree(alout.prev->word);
16865b0c20eSnakanon xfree(alout.prev);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate reset(); /* throw! */
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate struct wordent *
freenod(struct wordent * p1,struct wordent * p2)1746c02b4a4Smuffin freenod(struct wordent *p1, struct wordent *p2)
1757c478bd9Sstevel@tonic-gate {
1766c02b4a4Smuffin struct wordent *retp = p1->prev;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate #ifdef TRACE
1797c478bd9Sstevel@tonic-gate tprintf("TRACE- freenod()\n");
1807c478bd9Sstevel@tonic-gate #endif
1817c478bd9Sstevel@tonic-gate while (p1 != p2) {
18265b0c20eSnakanon xfree(p1->word);
1837c478bd9Sstevel@tonic-gate p1 = p1->next;
18465b0c20eSnakanon xfree(p1->prev);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate retp->next = p2;
1877c478bd9Sstevel@tonic-gate p2->prev = retp;
1887c478bd9Sstevel@tonic-gate return (retp);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate #define PHERE 1
1927c478bd9Sstevel@tonic-gate #define PIN 2
1937c478bd9Sstevel@tonic-gate #define POUT 4
1947c478bd9Sstevel@tonic-gate #define PDIAG 8
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * syntax
1987c478bd9Sstevel@tonic-gate * empty
1997c478bd9Sstevel@tonic-gate * syn0
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate struct command *
syntax(struct wordent * p1,struct wordent * p2,int flags)2026c02b4a4Smuffin syntax(struct wordent *p1, struct wordent *p2, int flags)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate #ifdef TRACE
2057c478bd9Sstevel@tonic-gate tprintf("TRACE- syntax()\n");
2067c478bd9Sstevel@tonic-gate #endif
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate while (p1 != p2)
2097c478bd9Sstevel@tonic-gate /* if (any(p1->word[0], ";&\n")) */ /* for char -> tchar */
2107c478bd9Sstevel@tonic-gate if (p1->word[0] == ';' ||
2117c478bd9Sstevel@tonic-gate p1->word[0] == '&' ||
2127c478bd9Sstevel@tonic-gate p1->word[0] == '\n')
2137c478bd9Sstevel@tonic-gate p1 = p1->next;
2147c478bd9Sstevel@tonic-gate else
2157c478bd9Sstevel@tonic-gate return (syn0(p1, p2, flags));
2167c478bd9Sstevel@tonic-gate return (0);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate * syn0
2217c478bd9Sstevel@tonic-gate * syn1
2227c478bd9Sstevel@tonic-gate * syn1 & syntax
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate struct command *
syn0(struct wordent * p1,struct wordent * p2,int flags)2256c02b4a4Smuffin syn0(struct wordent *p1, struct wordent *p2, int flags)
2267c478bd9Sstevel@tonic-gate {
2276c02b4a4Smuffin struct wordent *p;
2286c02b4a4Smuffin struct command *t, *t1;
2297c478bd9Sstevel@tonic-gate int l;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate #ifdef TRACE
2327c478bd9Sstevel@tonic-gate tprintf("TRACE- syn0()\n");
2337c478bd9Sstevel@tonic-gate #endif
2347c478bd9Sstevel@tonic-gate l = 0;
2357c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
2367c478bd9Sstevel@tonic-gate switch (p->word[0]) {
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate case '(':
2397c478bd9Sstevel@tonic-gate l++;
2407c478bd9Sstevel@tonic-gate continue;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate case ')':
2437c478bd9Sstevel@tonic-gate l--;
2447c478bd9Sstevel@tonic-gate if (l < 0)
2457c478bd9Sstevel@tonic-gate seterr("Too many )'s");
2467c478bd9Sstevel@tonic-gate continue;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate case '|':
2497c478bd9Sstevel@tonic-gate if (p->word[1] == '|')
2507c478bd9Sstevel@tonic-gate continue;
2517c478bd9Sstevel@tonic-gate /* fall into ... */
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate case '>':
25465b0c20eSnakanon if (p->next != p2 && eq(p->next->word, S_AND /* "&" */))
2557c478bd9Sstevel@tonic-gate p = p->next;
2567c478bd9Sstevel@tonic-gate continue;
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate case '&':
2597c478bd9Sstevel@tonic-gate if (l != 0)
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate if (p->word[1] == '&')
2627c478bd9Sstevel@tonic-gate continue;
2637c478bd9Sstevel@tonic-gate t1 = syn1(p1, p, flags);
2647c478bd9Sstevel@tonic-gate if (t1->t_dtyp == TLST ||
26565b0c20eSnakanon t1->t_dtyp == TAND ||
26665b0c20eSnakanon t1->t_dtyp == TOR) {
26765b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
2687c478bd9Sstevel@tonic-gate t->t_dtyp = TPAR;
2697c478bd9Sstevel@tonic-gate t->t_dflg = FAND|FINT;
2707c478bd9Sstevel@tonic-gate t->t_dspr = t1;
2717c478bd9Sstevel@tonic-gate t1 = t;
2727c478bd9Sstevel@tonic-gate } else
2737c478bd9Sstevel@tonic-gate t1->t_dflg |= FAND|FINT;
27465b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
2757c478bd9Sstevel@tonic-gate t->t_dtyp = TLST;
2767c478bd9Sstevel@tonic-gate t->t_dflg = 0;
2777c478bd9Sstevel@tonic-gate t->t_dcar = t1;
2787c478bd9Sstevel@tonic-gate t->t_dcdr = syntax(p, p2, flags);
27965b0c20eSnakanon return (t);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate if (l == 0)
2827c478bd9Sstevel@tonic-gate return (syn1(p1, p2, flags));
2837c478bd9Sstevel@tonic-gate seterr("Too many ('s");
2847c478bd9Sstevel@tonic-gate return (0);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate * syn1
2897c478bd9Sstevel@tonic-gate * syn1a
2907c478bd9Sstevel@tonic-gate * syn1a ; syntax
2917c478bd9Sstevel@tonic-gate */
2927c478bd9Sstevel@tonic-gate struct command *
syn1(struct wordent * p1,struct wordent * p2,int flags)2936c02b4a4Smuffin syn1(struct wordent *p1, struct wordent *p2, int flags)
2947c478bd9Sstevel@tonic-gate {
2956c02b4a4Smuffin struct wordent *p;
2966c02b4a4Smuffin struct command *t;
2977c478bd9Sstevel@tonic-gate int l;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate #ifdef TRACE
3007c478bd9Sstevel@tonic-gate tprintf("TRACE- syn1()\n");
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate l = 0;
3037c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
3047c478bd9Sstevel@tonic-gate switch (p->word[0]) {
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate case '(':
3077c478bd9Sstevel@tonic-gate l++;
3087c478bd9Sstevel@tonic-gate continue;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate case ')':
3117c478bd9Sstevel@tonic-gate l--;
3127c478bd9Sstevel@tonic-gate continue;
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate case ';':
3157c478bd9Sstevel@tonic-gate case '\n':
3167c478bd9Sstevel@tonic-gate if (l != 0)
3177c478bd9Sstevel@tonic-gate break;
31865b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
3197c478bd9Sstevel@tonic-gate t->t_dtyp = TLST;
3207c478bd9Sstevel@tonic-gate t->t_dcar = syn1a(p1, p, flags);
3217c478bd9Sstevel@tonic-gate t->t_dcdr = syntax(p->next, p2, flags);
3227c478bd9Sstevel@tonic-gate if (t->t_dcdr == 0)
3237c478bd9Sstevel@tonic-gate t->t_dcdr = t->t_dcar, t->t_dcar = 0;
3247c478bd9Sstevel@tonic-gate return (t);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate return (syn1a(p1, p2, flags));
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate * syn1a
3317c478bd9Sstevel@tonic-gate * syn1b
3327c478bd9Sstevel@tonic-gate * syn1b || syn1a
3337c478bd9Sstevel@tonic-gate */
3347c478bd9Sstevel@tonic-gate struct command *
syn1a(struct wordent * p1,struct wordent * p2,int flags)3356c02b4a4Smuffin syn1a(struct wordent *p1, struct wordent *p2, int flags)
3367c478bd9Sstevel@tonic-gate {
3376c02b4a4Smuffin struct wordent *p;
3386c02b4a4Smuffin struct command *t;
3396c02b4a4Smuffin int l = 0;
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate #ifdef TRACE
3427c478bd9Sstevel@tonic-gate tprintf("TRACE- syn1a()\n");
3437c478bd9Sstevel@tonic-gate #endif
3447c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
3457c478bd9Sstevel@tonic-gate switch (p->word[0]) {
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate case '(':
3487c478bd9Sstevel@tonic-gate l++;
3497c478bd9Sstevel@tonic-gate continue;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate case ')':
3527c478bd9Sstevel@tonic-gate l--;
3537c478bd9Sstevel@tonic-gate continue;
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate case '|':
3567c478bd9Sstevel@tonic-gate if (p->word[1] != '|')
3577c478bd9Sstevel@tonic-gate continue;
3587c478bd9Sstevel@tonic-gate if (l == 0) {
35965b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
3607c478bd9Sstevel@tonic-gate t->t_dtyp = TOR;
3617c478bd9Sstevel@tonic-gate t->t_dcar = syn1b(p1, p, flags);
3627c478bd9Sstevel@tonic-gate t->t_dcdr = syn1a(p->next, p2, flags);
3637c478bd9Sstevel@tonic-gate t->t_dflg = 0;
3647c478bd9Sstevel@tonic-gate return (t);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate continue;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate return (syn1b(p1, p2, flags));
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate * syn1b
3737c478bd9Sstevel@tonic-gate * syn2
3747c478bd9Sstevel@tonic-gate * syn2 && syn1b
3757c478bd9Sstevel@tonic-gate */
3767c478bd9Sstevel@tonic-gate struct command *
syn1b(struct wordent * p1,struct wordent * p2,int flags)3776c02b4a4Smuffin syn1b(struct wordent *p1, struct wordent *p2, int flags)
3787c478bd9Sstevel@tonic-gate {
3796c02b4a4Smuffin struct wordent *p;
3806c02b4a4Smuffin struct command *t;
3816c02b4a4Smuffin int l = 0;
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate #ifdef TRACE
3847c478bd9Sstevel@tonic-gate tprintf("TRACE- syn1b()\n");
3857c478bd9Sstevel@tonic-gate #endif
3867c478bd9Sstevel@tonic-gate l = 0;
3877c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
3887c478bd9Sstevel@tonic-gate switch (p->word[0]) {
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate case '(':
3917c478bd9Sstevel@tonic-gate l++;
3927c478bd9Sstevel@tonic-gate continue;
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate case ')':
3957c478bd9Sstevel@tonic-gate l--;
3967c478bd9Sstevel@tonic-gate continue;
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate case '&':
3997c478bd9Sstevel@tonic-gate if (p->word[1] == '&' && l == 0) {
40065b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
4017c478bd9Sstevel@tonic-gate t->t_dtyp = TAND;
4027c478bd9Sstevel@tonic-gate t->t_dcar = syn2(p1, p, flags);
4037c478bd9Sstevel@tonic-gate t->t_dcdr = syn1b(p->next, p2, flags);
4047c478bd9Sstevel@tonic-gate t->t_dflg = 0;
4057c478bd9Sstevel@tonic-gate return (t);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate continue;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate return (syn2(p1, p2, flags));
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate * syn2
4147c478bd9Sstevel@tonic-gate * syn3
4157c478bd9Sstevel@tonic-gate * syn3 | syn2
4167c478bd9Sstevel@tonic-gate * syn3 |& syn2
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate struct command *
syn2(struct wordent * p1,struct wordent * p2,int flags)4196c02b4a4Smuffin syn2(struct wordent *p1, struct wordent *p2, int flags)
4207c478bd9Sstevel@tonic-gate {
4216c02b4a4Smuffin struct wordent *p, *pn;
4226c02b4a4Smuffin struct command *t;
4236c02b4a4Smuffin int l = 0;
4247c478bd9Sstevel@tonic-gate int f;
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate #ifdef TRACE
4277c478bd9Sstevel@tonic-gate tprintf("TRACE- syn2()\n");
4287c478bd9Sstevel@tonic-gate #endif
4297c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
4307c478bd9Sstevel@tonic-gate switch (p->word[0]) {
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate case '(':
4337c478bd9Sstevel@tonic-gate l++;
4347c478bd9Sstevel@tonic-gate continue;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate case ')':
4377c478bd9Sstevel@tonic-gate l--;
4387c478bd9Sstevel@tonic-gate continue;
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate case '|':
4417c478bd9Sstevel@tonic-gate if (l != 0)
4427c478bd9Sstevel@tonic-gate continue;
44365b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
4447c478bd9Sstevel@tonic-gate f = flags | POUT;
4457c478bd9Sstevel@tonic-gate pn = p->next;
4467c478bd9Sstevel@tonic-gate if (pn != p2 && pn->word[0] == '&') {
4477c478bd9Sstevel@tonic-gate f |= PDIAG;
4487c478bd9Sstevel@tonic-gate t->t_dflg |= FDIAG;
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate t->t_dtyp = TFIL;
4517c478bd9Sstevel@tonic-gate t->t_dcar = syn3(p1, p, f);
4527c478bd9Sstevel@tonic-gate if (pn != p2 && pn->word[0] == '&')
4537c478bd9Sstevel@tonic-gate p = pn;
4547c478bd9Sstevel@tonic-gate t->t_dcdr = syn2(p->next, p2, flags | PIN);
4557c478bd9Sstevel@tonic-gate return (t);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate return (syn3(p1, p2, flags));
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate tchar RELPAR[] = {'<', '>', '(', ')', 0}; /* "<>()" */
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate * syn3
4647c478bd9Sstevel@tonic-gate * ( syn0 ) [ < in ] [ > out ]
4657c478bd9Sstevel@tonic-gate * word word* [ < in ] [ > out ]
4667c478bd9Sstevel@tonic-gate * KEYWORD ( word* ) word* [ < in ] [ > out ]
4677c478bd9Sstevel@tonic-gate *
4687c478bd9Sstevel@tonic-gate * KEYWORD = (@ exit foreach if set switch test while)
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate struct command *
syn3(struct wordent * p1,struct wordent * p2,int flags)4716c02b4a4Smuffin syn3(struct wordent *p1, struct wordent *p2, int flags)
4727c478bd9Sstevel@tonic-gate {
4736c02b4a4Smuffin struct wordent *p;
4747c478bd9Sstevel@tonic-gate struct wordent *lp, *rp;
4756c02b4a4Smuffin struct command *t;
4766c02b4a4Smuffin int l;
4777c478bd9Sstevel@tonic-gate tchar **av;
4787c478bd9Sstevel@tonic-gate int n, c;
4797c478bd9Sstevel@tonic-gate bool specp = 0;
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate #ifdef TRACE
4827c478bd9Sstevel@tonic-gate tprintf("TRACE- syn3()\n");
4837c478bd9Sstevel@tonic-gate #endif
4847c478bd9Sstevel@tonic-gate if (p1 != p2) {
4857c478bd9Sstevel@tonic-gate p = p1;
4867c478bd9Sstevel@tonic-gate again:
4877c478bd9Sstevel@tonic-gate switch (srchx(p->word)) {
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate case ZELSE:
4907c478bd9Sstevel@tonic-gate p = p->next;
4917c478bd9Sstevel@tonic-gate if (p != p2)
4927c478bd9Sstevel@tonic-gate goto again;
4937c478bd9Sstevel@tonic-gate break;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate case ZEXIT:
4967c478bd9Sstevel@tonic-gate case ZFOREACH:
4977c478bd9Sstevel@tonic-gate case ZIF:
4987c478bd9Sstevel@tonic-gate case ZLET:
4997c478bd9Sstevel@tonic-gate case ZSET:
5007c478bd9Sstevel@tonic-gate case ZSWITCH:
5017c478bd9Sstevel@tonic-gate case ZWHILE:
5027c478bd9Sstevel@tonic-gate specp = 1;
5037c478bd9Sstevel@tonic-gate break;
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate n = 0;
5077c478bd9Sstevel@tonic-gate l = 0;
5087c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next)
5097c478bd9Sstevel@tonic-gate switch (p->word[0]) {
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate case '(':
5127c478bd9Sstevel@tonic-gate if (specp)
5137c478bd9Sstevel@tonic-gate n++;
5147c478bd9Sstevel@tonic-gate l++;
5157c478bd9Sstevel@tonic-gate continue;
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate case ')':
5187c478bd9Sstevel@tonic-gate if (specp)
5197c478bd9Sstevel@tonic-gate n++;
5207c478bd9Sstevel@tonic-gate l--;
5217c478bd9Sstevel@tonic-gate continue;
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate case '>':
5247c478bd9Sstevel@tonic-gate case '<':
5257c478bd9Sstevel@tonic-gate if (l != 0) {
5267c478bd9Sstevel@tonic-gate if (specp)
5277c478bd9Sstevel@tonic-gate n++;
5287c478bd9Sstevel@tonic-gate continue;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate if (p->next == p2)
5317c478bd9Sstevel@tonic-gate continue;
5327c478bd9Sstevel@tonic-gate if (any(p->next->word[0], RELPAR))
5337c478bd9Sstevel@tonic-gate continue;
5347c478bd9Sstevel@tonic-gate n--;
5357c478bd9Sstevel@tonic-gate continue;
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate default:
5387c478bd9Sstevel@tonic-gate if (!specp && l != 0)
5397c478bd9Sstevel@tonic-gate continue;
5407c478bd9Sstevel@tonic-gate n++;
5417c478bd9Sstevel@tonic-gate continue;
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate if (n < 0)
5447c478bd9Sstevel@tonic-gate n = 0;
54565b0c20eSnakanon t = (struct command *)xcalloc(1, sizeof (*t));
54665b0c20eSnakanon av = (tchar **)xcalloc((unsigned)(n + 1), sizeof (tchar **));
5477c478bd9Sstevel@tonic-gate t->t_dcom = av;
5487c478bd9Sstevel@tonic-gate n = 0;
5497c478bd9Sstevel@tonic-gate if (p2->word[0] == ')')
5507c478bd9Sstevel@tonic-gate t->t_dflg = FPAR;
5517c478bd9Sstevel@tonic-gate lp = 0;
5527c478bd9Sstevel@tonic-gate rp = 0;
5537c478bd9Sstevel@tonic-gate l = 0;
5547c478bd9Sstevel@tonic-gate for (p = p1; p != p2; p = p->next) {
5557c478bd9Sstevel@tonic-gate c = p->word[0];
5567c478bd9Sstevel@tonic-gate switch (c) {
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate case '(':
5597c478bd9Sstevel@tonic-gate if (l == 0) {
5607c478bd9Sstevel@tonic-gate if (lp != 0 && !specp)
5617c478bd9Sstevel@tonic-gate seterr("Badly placed (");
5627c478bd9Sstevel@tonic-gate lp = p->next;
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate l++;
5657c478bd9Sstevel@tonic-gate goto savep;
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate case ')':
5687c478bd9Sstevel@tonic-gate l--;
5697c478bd9Sstevel@tonic-gate if (l == 0)
5707c478bd9Sstevel@tonic-gate rp = p;
5717c478bd9Sstevel@tonic-gate goto savep;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate case '>':
5747c478bd9Sstevel@tonic-gate if (l != 0)
5757c478bd9Sstevel@tonic-gate goto savep;
5767c478bd9Sstevel@tonic-gate if (p->word[1] == '>')
5777c478bd9Sstevel@tonic-gate t->t_dflg |= FCAT;
57865b0c20eSnakanon if (p->next != p2 && eq(p->next->word, S_AND /* "&" */)) {
5797c478bd9Sstevel@tonic-gate t->t_dflg |= FDIAG, p = p->next;
5807c478bd9Sstevel@tonic-gate if (flags & (POUT|PDIAG))
5817c478bd9Sstevel@tonic-gate goto badout;
5827c478bd9Sstevel@tonic-gate }
58365b0c20eSnakanon if (p->next != p2 && eq(p->next->word, S_EXAS /* "!" */))
5847c478bd9Sstevel@tonic-gate t->t_dflg |= FANY, p = p->next;
5857c478bd9Sstevel@tonic-gate if (p->next == p2) {
5867c478bd9Sstevel@tonic-gate missfile:
5877c478bd9Sstevel@tonic-gate seterr("Missing name for redirect");
5887c478bd9Sstevel@tonic-gate continue;
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate p = p->next;
5917c478bd9Sstevel@tonic-gate if (any(p->word[0], RELPAR))
5927c478bd9Sstevel@tonic-gate goto missfile;
5937c478bd9Sstevel@tonic-gate if ((flags & POUT) && (flags & PDIAG) == 0 || t->t_drit)
5947c478bd9Sstevel@tonic-gate badout:
5957c478bd9Sstevel@tonic-gate seterr("Ambiguous output redirect");
5967c478bd9Sstevel@tonic-gate else
5977c478bd9Sstevel@tonic-gate t->t_drit = savestr(p->word);
5987c478bd9Sstevel@tonic-gate continue;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate case '<':
6017c478bd9Sstevel@tonic-gate if (l != 0)
6027c478bd9Sstevel@tonic-gate goto savep;
6037c478bd9Sstevel@tonic-gate if (p->word[1] == '<')
6047c478bd9Sstevel@tonic-gate t->t_dflg |= FHERE;
6057c478bd9Sstevel@tonic-gate if (p->next == p2)
6067c478bd9Sstevel@tonic-gate goto missfile;
6077c478bd9Sstevel@tonic-gate p = p->next;
6087c478bd9Sstevel@tonic-gate if (any(p->word[0], RELPAR))
6097c478bd9Sstevel@tonic-gate goto missfile;
6107c478bd9Sstevel@tonic-gate if ((flags & PHERE) && (t->t_dflg & FHERE))
6117c478bd9Sstevel@tonic-gate seterr("Can't << within ()'s");
6127c478bd9Sstevel@tonic-gate else if ((flags & PIN) || t->t_dlef)
6137c478bd9Sstevel@tonic-gate seterr("Ambiguous input redirect");
6147c478bd9Sstevel@tonic-gate else
6157c478bd9Sstevel@tonic-gate t->t_dlef = savestr(p->word);
6167c478bd9Sstevel@tonic-gate continue;
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate savep:
6197c478bd9Sstevel@tonic-gate if (!specp)
6207c478bd9Sstevel@tonic-gate continue;
6217c478bd9Sstevel@tonic-gate default:
6227c478bd9Sstevel@tonic-gate if (l != 0 && !specp)
6237c478bd9Sstevel@tonic-gate continue;
624*258f91c6SToomas Soome if (err_msg == NULL)
6257c478bd9Sstevel@tonic-gate av[n] = savestr(p->word);
6267c478bd9Sstevel@tonic-gate n++;
6277c478bd9Sstevel@tonic-gate continue;
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate if (lp != 0 && !specp) {
6317c478bd9Sstevel@tonic-gate if (n != 0)
6327c478bd9Sstevel@tonic-gate seterr("Badly placed ()'s");
6337c478bd9Sstevel@tonic-gate t->t_dtyp = TPAR;
6347c478bd9Sstevel@tonic-gate t->t_dspr = syn0(lp, rp, PHERE);
6357c478bd9Sstevel@tonic-gate } else {
6367c478bd9Sstevel@tonic-gate if (n == 0)
6377c478bd9Sstevel@tonic-gate seterr("Invalid null command");
6387c478bd9Sstevel@tonic-gate t->t_dtyp = TCOM;
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate return (t);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate
6436c02b4a4Smuffin void
freesyn(struct command * t)6446c02b4a4Smuffin freesyn(struct command *t)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate #ifdef TRACE
6477c478bd9Sstevel@tonic-gate tprintf("TRACE- freesyn()\n");
6487c478bd9Sstevel@tonic-gate #endif
6497c478bd9Sstevel@tonic-gate if (t == 0)
6507c478bd9Sstevel@tonic-gate return;
6517c478bd9Sstevel@tonic-gate switch (t->t_dtyp) {
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate case TCOM:
6547c478bd9Sstevel@tonic-gate blkfree(t->t_dcom);
6557c478bd9Sstevel@tonic-gate if (t->cfname)
6567c478bd9Sstevel@tonic-gate xfree(t->cfname);
6577c478bd9Sstevel@tonic-gate if (t->cargs)
6587c478bd9Sstevel@tonic-gate chr_blkfree(t->cargs);
6597c478bd9Sstevel@tonic-gate goto lr;
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate case TPAR:
6627c478bd9Sstevel@tonic-gate freesyn(t->t_dspr);
6637c478bd9Sstevel@tonic-gate /* fall into ... */
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate lr:
66665b0c20eSnakanon xfree(t->t_dlef);
66765b0c20eSnakanon xfree(t->t_drit);
6687c478bd9Sstevel@tonic-gate break;
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate case TAND:
6717c478bd9Sstevel@tonic-gate case TOR:
6727c478bd9Sstevel@tonic-gate case TFIL:
6737c478bd9Sstevel@tonic-gate case TLST:
6747c478bd9Sstevel@tonic-gate freesyn(t->t_dcar), freesyn(t->t_dcdr);
6757c478bd9Sstevel@tonic-gate break;
6767c478bd9Sstevel@tonic-gate }
67765b0c20eSnakanon xfree(t);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate
6816c02b4a4Smuffin void
chr_blkfree(char ** vec)6826c02b4a4Smuffin chr_blkfree(char **vec)
6837c478bd9Sstevel@tonic-gate {
6846c02b4a4Smuffin char **av;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate for (av = vec; *av; av++)
6877c478bd9Sstevel@tonic-gate xfree(*av);
6887c478bd9Sstevel@tonic-gate xfree(vec);
6897c478bd9Sstevel@tonic-gate }
690