Home | History | Annotate | Line # | Download | only in csh
parse.c revision 1.14
      1  1.14       agc /* $NetBSD: parse.c,v 1.14 2003/08/07 09:05:06 agc Exp $ */
      2   1.6       cgd 
      3   1.1       cgd /*-
      4   1.5   mycroft  * Copyright (c) 1980, 1991, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.14       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.8  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.6       cgd #if 0
     35   1.6       cgd static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 5/31/93";
     36   1.6       cgd #else
     37  1.14       agc __RCSID("$NetBSD: parse.c,v 1.14 2003/08/07 09:05:06 agc Exp $");
     38   1.6       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/types.h>
     42  1.11       wiz 
     43  1.13       wiz #include <stdarg.h>
     44   1.1       cgd #include <stdlib.h>
     45   1.1       cgd #include <string.h>
     46   1.1       cgd 
     47   1.1       cgd #include "csh.h"
     48   1.1       cgd #include "extern.h"
     49   1.1       cgd 
     50  1.11       wiz static void asyntax(struct wordent *, struct wordent *);
     51  1.11       wiz static void asyn0(struct wordent *, struct wordent *);
     52  1.11       wiz static void asyn3(struct wordent *, struct wordent *);
     53  1.11       wiz static struct wordent *freenod(struct wordent *, struct wordent *);
     54  1.11       wiz static struct command *syn0(struct wordent *, struct wordent *, int);
     55  1.11       wiz static struct command *syn1(struct wordent *, struct wordent *, int);
     56  1.11       wiz static struct command *syn1a(struct wordent *, struct wordent *, int);
     57  1.11       wiz static struct command *syn1b(struct wordent *, struct wordent *, int);
     58  1.11       wiz static struct command *syn2(struct wordent *, struct wordent *, int);
     59  1.11       wiz static struct command *syn3(struct wordent *, struct wordent *, int);
     60   1.1       cgd 
     61  1.11       wiz #define ALEFT 21		/* max of 20 alias expansions	 */
     62  1.11       wiz #define HLEFT 11		/* max of 10 history expansions	 */
     63   1.1       cgd /*
     64   1.1       cgd  * Perform aliasing on the word list lex
     65   1.1       cgd  * Do a (very rudimentary) parse to separate into commands.
     66   1.1       cgd  * If word 0 of a command has an alias, do it.
     67   1.1       cgd  * Repeat a maximum of 20 times.
     68   1.1       cgd  */
     69   1.1       cgd static int aleft;
     70   1.1       cgd extern int hleft;
     71  1.11       wiz 
     72   1.1       cgd void
     73  1.12     lukem alias(struct wordent *lexp)
     74   1.1       cgd {
     75   1.1       cgd     jmp_buf osetexit;
     76   1.1       cgd 
     77   1.1       cgd     aleft = ALEFT;
     78   1.1       cgd     hleft = HLEFT;
     79   1.1       cgd     getexit(osetexit);
     80  1.11       wiz     (void)setexit();
     81   1.1       cgd     if (haderr) {
     82   1.1       cgd 	resexit(osetexit);
     83   1.1       cgd 	reset();
     84   1.1       cgd     }
     85  1.10   mycroft     if (--aleft == 0)
     86   1.1       cgd 	stderror(ERR_ALIASLOOP);
     87  1.12     lukem     asyntax(lexp->next, lexp);
     88   1.1       cgd     resexit(osetexit);
     89   1.1       cgd }
     90   1.1       cgd 
     91   1.1       cgd static void
     92  1.11       wiz asyntax(struct wordent *p1, struct wordent *p2)
     93   1.1       cgd {
     94   1.1       cgd     while (p1 != p2)
     95   1.1       cgd 	if (any(";&\n", p1->word[0]))
     96   1.1       cgd 	    p1 = p1->next;
     97   1.1       cgd 	else {
     98   1.1       cgd 	    asyn0(p1, p2);
     99   1.1       cgd 	    return;
    100   1.1       cgd 	}
    101   1.1       cgd }
    102   1.1       cgd 
    103   1.1       cgd static void
    104  1.11       wiz asyn0(struct wordent *p1, struct wordent *p2)
    105   1.1       cgd {
    106   1.7       tls     struct wordent *p;
    107  1.11       wiz     int l;
    108   1.1       cgd 
    109  1.11       wiz     l = 0;
    110   1.1       cgd     for (p = p1; p != p2; p = p->next)
    111   1.1       cgd 	switch (p->word[0]) {
    112   1.1       cgd 	case '(':
    113   1.1       cgd 	    l++;
    114   1.1       cgd 	    continue;
    115   1.1       cgd 	case ')':
    116   1.1       cgd 	    l--;
    117  1.10   mycroft 	    if (l < 0)
    118   1.1       cgd 		stderror(ERR_TOOMANYRP);
    119   1.1       cgd 	    continue;
    120   1.1       cgd 	case '>':
    121   1.1       cgd 	    if (p->next != p2 && eq(p->next->word, STRand))
    122   1.1       cgd 		p = p->next;
    123   1.1       cgd 	    continue;
    124   1.1       cgd 	case '&':
    125   1.1       cgd 	case '|':
    126   1.1       cgd 	case ';':
    127   1.1       cgd 	case '\n':
    128   1.1       cgd 	    if (l != 0)
    129   1.1       cgd 		continue;
    130   1.1       cgd 	    asyn3(p1, p);
    131   1.1       cgd 	    asyntax(p->next, p2);
    132   1.1       cgd 	    return;
    133   1.1       cgd 	}
    134   1.1       cgd     if (l == 0)
    135   1.1       cgd 	asyn3(p1, p2);
    136   1.1       cgd }
    137   1.1       cgd 
    138   1.1       cgd static void
    139  1.11       wiz asyn3(struct wordent *p1, struct wordent *p2)
    140   1.1       cgd {
    141   1.7       tls     struct varent *ap;
    142   1.1       cgd     struct wordent alout;
    143   1.7       tls     bool redid;
    144   1.1       cgd 
    145   1.1       cgd     if (p1 == p2)
    146   1.1       cgd 	return;
    147   1.1       cgd     if (p1->word[0] == '(') {
    148   1.1       cgd 	for (p2 = p2->prev; p2->word[0] != ')'; p2 = p2->prev)
    149   1.1       cgd 	    if (p2 == p1)
    150   1.1       cgd 		return;
    151   1.1       cgd 	if (p2 == p1->next)
    152   1.1       cgd 	    return;
    153   1.1       cgd 	asyn0(p1->next, p2);
    154   1.1       cgd 	return;
    155   1.1       cgd     }
    156   1.1       cgd     ap = adrof1(p1->word, &aliases);
    157   1.1       cgd     if (ap == 0)
    158   1.1       cgd 	return;
    159   1.1       cgd     alhistp = p1->prev;
    160   1.1       cgd     alhistt = p2;
    161   1.1       cgd     alvec = ap->vec;
    162   1.1       cgd     redid = lex(&alout);
    163   1.1       cgd     alhistp = alhistt = 0;
    164   1.1       cgd     alvec = 0;
    165   1.1       cgd     if (seterr) {
    166   1.1       cgd 	freelex(&alout);
    167   1.1       cgd 	stderror(ERR_OLD);
    168   1.1       cgd     }
    169   1.1       cgd     if (p1->word[0] && eq(p1->word, alout.next->word)) {
    170  1.11       wiz 	Char *cp;
    171   1.1       cgd 
    172  1.11       wiz 	cp = alout.next->word;
    173   1.1       cgd 	alout.next->word = Strspl(STRQNULL, cp);
    174   1.1       cgd 	xfree((ptr_t) cp);
    175   1.1       cgd     }
    176   1.1       cgd     p1 = freenod(p1, redid ? p2 : p1->next);
    177   1.1       cgd     if (alout.next != &alout) {
    178   1.1       cgd 	p1->next->prev = alout.prev->prev;
    179   1.1       cgd 	alout.prev->prev->next = p1->next;
    180   1.1       cgd 	alout.next->prev = p1;
    181   1.1       cgd 	p1->next = alout.next;
    182  1.11       wiz 	xfree((ptr_t)alout.prev->word);
    183  1.11       wiz 	xfree((ptr_t)(alout.prev));
    184   1.1       cgd     }
    185   1.1       cgd     reset();			/* throw! */
    186   1.1       cgd }
    187   1.1       cgd 
    188   1.1       cgd static struct wordent *
    189  1.11       wiz freenod(struct wordent *p1, struct wordent *p2)
    190   1.1       cgd {
    191  1.11       wiz     struct wordent *retp;
    192   1.1       cgd 
    193  1.11       wiz     retp = p1->prev;
    194   1.1       cgd     while (p1 != p2) {
    195  1.11       wiz 	xfree((ptr_t)p1->word);
    196   1.1       cgd 	p1 = p1->next;
    197   1.1       cgd 	xfree((ptr_t) (p1->prev));
    198   1.1       cgd     }
    199   1.1       cgd     retp->next = p2;
    200   1.1       cgd     p2->prev = retp;
    201   1.1       cgd     return (retp);
    202   1.1       cgd }
    203   1.1       cgd 
    204   1.1       cgd #define	PHERE	1
    205   1.1       cgd #define	PIN	2
    206   1.1       cgd #define	POUT	4
    207   1.5   mycroft #define	PERR	8
    208   1.1       cgd 
    209   1.1       cgd /*
    210   1.1       cgd  * syntax
    211   1.1       cgd  *	empty
    212   1.1       cgd  *	syn0
    213   1.1       cgd  */
    214   1.1       cgd struct command *
    215  1.11       wiz syntax(struct wordent *p1, struct wordent *p2, int flags)
    216   1.1       cgd {
    217   1.1       cgd     while (p1 != p2)
    218   1.1       cgd 	if (any(";&\n", p1->word[0]))
    219   1.1       cgd 	    p1 = p1->next;
    220   1.1       cgd 	else
    221   1.1       cgd 	    return (syn0(p1, p2, flags));
    222   1.1       cgd     return (0);
    223   1.1       cgd }
    224   1.1       cgd 
    225   1.1       cgd /*
    226   1.1       cgd  * syn0
    227   1.1       cgd  *	syn1
    228   1.1       cgd  *	syn1 & syntax
    229   1.1       cgd  */
    230   1.1       cgd static struct command *
    231  1.11       wiz syn0(struct wordent *p1, struct wordent *p2, int flags)
    232   1.1       cgd {
    233   1.7       tls     struct wordent *p;
    234   1.7       tls     struct command *t, *t1;
    235  1.11       wiz     int l;
    236   1.1       cgd 
    237   1.1       cgd     l = 0;
    238   1.1       cgd     for (p = p1; p != p2; p = p->next)
    239   1.1       cgd 	switch (p->word[0]) {
    240   1.1       cgd 	case '(':
    241   1.1       cgd 	    l++;
    242   1.1       cgd 	    continue;
    243   1.1       cgd 	case ')':
    244   1.1       cgd 	    l--;
    245   1.1       cgd 	    if (l < 0)
    246   1.1       cgd 		seterror(ERR_TOOMANYRP);
    247   1.1       cgd 	    continue;
    248   1.1       cgd 	case '|':
    249   1.1       cgd 	    if (p->word[1] == '|')
    250   1.1       cgd 		continue;
    251   1.9   mycroft 	    /* FALLTHROUGH */
    252   1.1       cgd 	case '>':
    253   1.1       cgd 	    if (p->next != p2 && eq(p->next->word, STRand))
    254   1.1       cgd 		p = p->next;
    255   1.1       cgd 	    continue;
    256   1.1       cgd 	case '&':
    257   1.1       cgd 	    if (l != 0)
    258   1.1       cgd 		break;
    259   1.1       cgd 	    if (p->word[1] == '&')
    260   1.1       cgd 		continue;
    261   1.1       cgd 	    t1 = syn1(p1, p, flags);
    262   1.1       cgd 	    if (t1->t_dtyp == NODE_LIST ||
    263   1.1       cgd 		t1->t_dtyp == NODE_AND ||
    264   1.1       cgd 		t1->t_dtyp == NODE_OR) {
    265  1.11       wiz 		t = (struct command *)xcalloc(1, sizeof(*t));
    266   1.1       cgd 		t->t_dtyp = NODE_PAREN;
    267   1.1       cgd 		t->t_dflg = F_AMPERSAND | F_NOINTERRUPT;
    268   1.1       cgd 		t->t_dspr = t1;
    269   1.1       cgd 		t1 = t;
    270   1.1       cgd 	    }
    271   1.1       cgd 	    else
    272   1.1       cgd 		t1->t_dflg |= F_AMPERSAND | F_NOINTERRUPT;
    273  1.11       wiz 	    t = (struct command *)xcalloc(1, sizeof(*t));
    274   1.1       cgd 	    t->t_dtyp = NODE_LIST;
    275   1.1       cgd 	    t->t_dflg = 0;
    276   1.1       cgd 	    t->t_dcar = t1;
    277   1.1       cgd 	    t->t_dcdr = syntax(p, p2, flags);
    278   1.1       cgd 	    return (t);
    279   1.1       cgd 	}
    280   1.1       cgd     if (l == 0)
    281   1.1       cgd 	return (syn1(p1, p2, flags));
    282   1.1       cgd     seterror(ERR_TOOMANYLP);
    283   1.1       cgd     return (0);
    284   1.1       cgd }
    285   1.1       cgd 
    286   1.1       cgd /*
    287   1.1       cgd  * syn1
    288   1.1       cgd  *	syn1a
    289   1.1       cgd  *	syn1a ; syntax
    290   1.1       cgd  */
    291   1.1       cgd static struct command *
    292  1.11       wiz syn1(struct wordent *p1, struct wordent *p2, int flags)
    293   1.1       cgd {
    294   1.7       tls     struct wordent *p;
    295   1.7       tls     struct command *t;
    296  1.11       wiz     int l;
    297   1.1       cgd 
    298   1.1       cgd     l = 0;
    299   1.1       cgd     for (p = p1; p != p2; p = p->next)
    300   1.1       cgd 	switch (p->word[0]) {
    301   1.1       cgd 	case '(':
    302   1.1       cgd 	    l++;
    303   1.1       cgd 	    continue;
    304   1.1       cgd 	case ')':
    305   1.1       cgd 	    l--;
    306   1.1       cgd 	    continue;
    307   1.1       cgd 	case ';':
    308   1.1       cgd 	case '\n':
    309   1.1       cgd 	    if (l != 0)
    310   1.1       cgd 		break;
    311   1.1       cgd 	    t = (struct command *) xcalloc(1, sizeof(*t));
    312   1.1       cgd 	    t->t_dtyp = NODE_LIST;
    313   1.1       cgd 	    t->t_dcar = syn1a(p1, p, flags);
    314   1.1       cgd 	    t->t_dcdr = syntax(p->next, p2, flags);
    315   1.1       cgd 	    if (t->t_dcdr == 0)
    316   1.1       cgd 		t->t_dcdr = t->t_dcar, t->t_dcar = 0;
    317   1.1       cgd 	    return (t);
    318   1.1       cgd 	}
    319   1.1       cgd     return (syn1a(p1, p2, flags));
    320   1.1       cgd }
    321   1.1       cgd 
    322   1.1       cgd /*
    323   1.1       cgd  * syn1a
    324   1.1       cgd  *	syn1b
    325   1.1       cgd  *	syn1b || syn1a
    326   1.1       cgd  */
    327   1.1       cgd static struct command *
    328  1.11       wiz syn1a(struct wordent *p1, struct wordent *p2, int flags)
    329   1.1       cgd {
    330   1.7       tls     struct wordent *p;
    331   1.7       tls     struct command *t;
    332  1.11       wiz     int l;
    333   1.1       cgd 
    334  1.11       wiz     l = 0;
    335   1.1       cgd     for (p = p1; p != p2; p = p->next)
    336   1.1       cgd 	switch (p->word[0]) {
    337   1.1       cgd 	case '(':
    338   1.1       cgd 	    l++;
    339   1.1       cgd 	    continue;
    340   1.1       cgd 	case ')':
    341   1.1       cgd 	    l--;
    342   1.1       cgd 	    continue;
    343   1.1       cgd 	case '|':
    344   1.1       cgd 	    if (p->word[1] != '|')
    345   1.1       cgd 		continue;
    346   1.1       cgd 	    if (l == 0) {
    347  1.11       wiz 		t = (struct command *)xcalloc(1, sizeof(*t));
    348   1.1       cgd 		t->t_dtyp = NODE_OR;
    349   1.1       cgd 		t->t_dcar = syn1b(p1, p, flags);
    350   1.1       cgd 		t->t_dcdr = syn1a(p->next, p2, flags);
    351   1.1       cgd 		t->t_dflg = 0;
    352   1.1       cgd 		return (t);
    353   1.1       cgd 	    }
    354   1.1       cgd 	    continue;
    355   1.1       cgd 	}
    356   1.1       cgd     return (syn1b(p1, p2, flags));
    357   1.1       cgd }
    358   1.1       cgd 
    359   1.1       cgd /*
    360   1.1       cgd  * syn1b
    361   1.1       cgd  *	syn2
    362   1.1       cgd  *	syn2 && syn1b
    363   1.1       cgd  */
    364   1.1       cgd static struct command *
    365  1.11       wiz syn1b(struct wordent *p1, struct wordent *p2, int flags)
    366   1.1       cgd {
    367   1.7       tls     struct wordent *p;
    368   1.7       tls     struct command *t;
    369  1.11       wiz     int l;
    370   1.1       cgd 
    371  1.11       wiz     l = 0;
    372   1.1       cgd     for (p = p1; p != p2; p = p->next)
    373   1.1       cgd 	switch (p->word[0]) {
    374   1.1       cgd 	case '(':
    375   1.1       cgd 	    l++;
    376   1.1       cgd 	    continue;
    377   1.1       cgd 	case ')':
    378   1.1       cgd 	    l--;
    379   1.1       cgd 	    continue;
    380   1.1       cgd 	case '&':
    381   1.1       cgd 	    if (p->word[1] == '&' && l == 0) {
    382  1.11       wiz 		t = (struct command *)xcalloc(1, sizeof(*t));
    383   1.1       cgd 		t->t_dtyp = NODE_AND;
    384   1.1       cgd 		t->t_dcar = syn2(p1, p, flags);
    385   1.1       cgd 		t->t_dcdr = syn1b(p->next, p2, flags);
    386   1.1       cgd 		t->t_dflg = 0;
    387   1.1       cgd 		return (t);
    388   1.1       cgd 	    }
    389   1.1       cgd 	    continue;
    390   1.1       cgd 	}
    391   1.1       cgd     return (syn2(p1, p2, flags));
    392   1.1       cgd }
    393   1.1       cgd 
    394   1.1       cgd /*
    395   1.1       cgd  * syn2
    396   1.1       cgd  *	syn3
    397   1.1       cgd  *	syn3 | syn2
    398   1.1       cgd  *	syn3 |& syn2
    399   1.1       cgd  */
    400   1.1       cgd static struct command *
    401  1.11       wiz syn2(struct wordent *p1, struct wordent *p2, int flags)
    402   1.1       cgd {
    403   1.7       tls     struct wordent *p, *pn;
    404   1.7       tls     struct command *t;
    405  1.11       wiz     int f, l;
    406   1.1       cgd 
    407  1.11       wiz     l = 0;
    408   1.1       cgd     for (p = p1; p != p2; p = p->next)
    409   1.1       cgd 	switch (p->word[0]) {
    410   1.1       cgd 	case '(':
    411   1.1       cgd 	    l++;
    412   1.1       cgd 	    continue;
    413   1.1       cgd 	case ')':
    414   1.1       cgd 	    l--;
    415   1.1       cgd 	    continue;
    416   1.1       cgd 	case '|':
    417   1.1       cgd 	    if (l != 0)
    418   1.1       cgd 		continue;
    419  1.11       wiz 	    t = (struct command *)xcalloc(1, sizeof(*t));
    420   1.1       cgd 	    f = flags | POUT;
    421   1.1       cgd 	    pn = p->next;
    422   1.1       cgd 	    if (pn != p2 && pn->word[0] == '&') {
    423   1.5   mycroft 		f |= PERR;
    424   1.1       cgd 		t->t_dflg |= F_STDERR;
    425   1.1       cgd 	    }
    426   1.1       cgd 	    t->t_dtyp = NODE_PIPE;
    427   1.1       cgd 	    t->t_dcar = syn3(p1, p, f);
    428   1.1       cgd 	    if (pn != p2 && pn->word[0] == '&')
    429   1.1       cgd 		p = pn;
    430   1.1       cgd 	    t->t_dcdr = syn2(p->next, p2, flags | PIN);
    431   1.1       cgd 	    return (t);
    432   1.1       cgd 	}
    433   1.1       cgd     return (syn3(p1, p2, flags));
    434   1.1       cgd }
    435   1.1       cgd 
    436   1.1       cgd static char RELPAR[] = {'<', '>', '(', ')', '\0'};
    437   1.1       cgd 
    438   1.1       cgd /*
    439   1.1       cgd  * syn3
    440   1.1       cgd  *	( syn0 ) [ < in  ] [ > out ]
    441   1.1       cgd  *	word word* [ < in ] [ > out ]
    442   1.1       cgd  *	KEYWORD ( word* ) word* [ < in ] [ > out ]
    443   1.1       cgd  *
    444   1.1       cgd  *	KEYWORD = (@ exit foreach if set switch test while)
    445   1.1       cgd  */
    446   1.1       cgd static struct command *
    447  1.11       wiz syn3(struct wordent *p1, struct wordent *p2, int flags)
    448   1.1       cgd {
    449  1.11       wiz     struct wordent *lp, *p, *rp;
    450   1.7       tls     struct command *t;
    451  1.11       wiz     Char **av;
    452  1.11       wiz     int c, l, n;
    453  1.11       wiz     bool specp;
    454   1.1       cgd 
    455  1.11       wiz     specp = 0;
    456   1.1       cgd     if (p1 != p2) {
    457   1.1       cgd 	p = p1;
    458   1.1       cgd again:
    459   1.1       cgd 	switch (srchx(p->word)) {
    460   1.1       cgd 	case T_ELSE:
    461   1.1       cgd 	    p = p->next;
    462   1.1       cgd 	    if (p != p2)
    463   1.1       cgd 		goto again;
    464   1.1       cgd 	    break;
    465   1.1       cgd 	case T_EXIT:
    466   1.1       cgd 	case T_FOREACH:
    467   1.1       cgd 	case T_IF:
    468   1.1       cgd 	case T_LET:
    469   1.1       cgd 	case T_SET:
    470   1.1       cgd 	case T_SWITCH:
    471   1.1       cgd 	case T_WHILE:
    472   1.1       cgd 	    specp = 1;
    473   1.1       cgd 	    break;
    474   1.1       cgd 	}
    475   1.1       cgd     }
    476   1.1       cgd     n = 0;
    477   1.1       cgd     l = 0;
    478   1.1       cgd     for (p = p1; p != p2; p = p->next)
    479   1.1       cgd 	switch (p->word[0]) {
    480   1.1       cgd 	case '(':
    481   1.1       cgd 	    if (specp)
    482   1.1       cgd 		n++;
    483   1.1       cgd 	    l++;
    484   1.1       cgd 	    continue;
    485   1.1       cgd 	case ')':
    486   1.1       cgd 	    if (specp)
    487   1.1       cgd 		n++;
    488   1.1       cgd 	    l--;
    489   1.1       cgd 	    continue;
    490   1.1       cgd 	case '>':
    491   1.1       cgd 	case '<':
    492   1.1       cgd 	    if (l != 0) {
    493   1.1       cgd 		if (specp)
    494   1.1       cgd 		    n++;
    495   1.1       cgd 		continue;
    496   1.1       cgd 	    }
    497   1.1       cgd 	    if (p->next == p2)
    498   1.1       cgd 		continue;
    499   1.1       cgd 	    if (any(RELPAR, p->next->word[0]))
    500   1.1       cgd 		continue;
    501   1.1       cgd 	    n--;
    502   1.1       cgd 	    continue;
    503   1.1       cgd 	default:
    504   1.1       cgd 	    if (!specp && l != 0)
    505   1.1       cgd 		continue;
    506   1.1       cgd 	    n++;
    507   1.1       cgd 	    continue;
    508   1.1       cgd 	}
    509   1.1       cgd     if (n < 0)
    510   1.1       cgd 	n = 0;
    511  1.11       wiz     t = (struct command *)xcalloc(1, sizeof(*t));
    512  1.11       wiz     av = (Char **)xcalloc((size_t)(n + 1), sizeof(Char **));
    513   1.1       cgd     t->t_dcom = av;
    514   1.1       cgd     n = 0;
    515   1.1       cgd     if (p2->word[0] == ')')
    516   1.1       cgd 	t->t_dflg = F_NOFORK;
    517   1.1       cgd     lp = 0;
    518   1.1       cgd     rp = 0;
    519   1.1       cgd     l = 0;
    520   1.1       cgd     for (p = p1; p != p2; p = p->next) {
    521   1.1       cgd 	c = p->word[0];
    522   1.1       cgd 	switch (c) {
    523   1.1       cgd 	case '(':
    524   1.1       cgd 	    if (l == 0) {
    525   1.1       cgd 		if (lp != 0 && !specp)
    526   1.1       cgd 		    seterror(ERR_BADPLP);
    527   1.1       cgd 		lp = p->next;
    528   1.1       cgd 	    }
    529   1.1       cgd 	    l++;
    530   1.1       cgd 	    goto savep;
    531   1.1       cgd 	case ')':
    532   1.1       cgd 	    l--;
    533   1.1       cgd 	    if (l == 0)
    534   1.1       cgd 		rp = p;
    535   1.1       cgd 	    goto savep;
    536   1.1       cgd 	case '>':
    537   1.1       cgd 	    if (l != 0)
    538   1.1       cgd 		goto savep;
    539   1.1       cgd 	    if (p->word[1] == '>')
    540   1.1       cgd 		t->t_dflg |= F_APPEND;
    541   1.1       cgd 	    if (p->next != p2 && eq(p->next->word, STRand)) {
    542   1.1       cgd 		t->t_dflg |= F_STDERR, p = p->next;
    543   1.5   mycroft 		if (flags & (POUT | PERR)) {
    544   1.1       cgd 		    seterror(ERR_OUTRED);
    545   1.1       cgd 		    continue;
    546   1.1       cgd 		}
    547   1.1       cgd 	    }
    548   1.1       cgd 	    if (p->next != p2 && eq(p->next->word, STRbang))
    549   1.1       cgd 		t->t_dflg |= F_OVERWRITE, p = p->next;
    550   1.1       cgd 	    if (p->next == p2) {
    551   1.1       cgd 		seterror(ERR_MISRED);
    552   1.1       cgd 		continue;
    553   1.1       cgd 	    }
    554   1.1       cgd 	    p = p->next;
    555   1.1       cgd 	    if (any(RELPAR, p->word[0])) {
    556   1.1       cgd 		seterror(ERR_MISRED);
    557   1.1       cgd 		continue;
    558   1.1       cgd 	    }
    559   1.5   mycroft 	    if ((flags & POUT) && ((flags & PERR) == 0 || t->t_drit))
    560   1.1       cgd 		seterror(ERR_OUTRED);
    561   1.1       cgd 	    else
    562   1.1       cgd 		t->t_drit = Strsave(p->word);
    563   1.1       cgd 	    continue;
    564   1.1       cgd 	case '<':
    565   1.1       cgd 	    if (l != 0)
    566   1.1       cgd 		goto savep;
    567   1.1       cgd 	    if (p->word[1] == '<')
    568   1.1       cgd 		t->t_dflg |= F_READ;
    569   1.1       cgd 	    if (p->next == p2) {
    570   1.1       cgd 		seterror(ERR_MISRED);
    571   1.1       cgd 		continue;
    572   1.1       cgd 	    }
    573   1.1       cgd 	    p = p->next;
    574   1.1       cgd 	    if (any(RELPAR, p->word[0])) {
    575   1.1       cgd 		seterror(ERR_MISRED);
    576   1.1       cgd 		continue;
    577   1.1       cgd 	    }
    578   1.1       cgd 	    if ((flags & PHERE) && (t->t_dflg & F_READ))
    579   1.1       cgd 		seterror(ERR_REDPAR);
    580   1.1       cgd 	    else if ((flags & PIN) || t->t_dlef)
    581   1.1       cgd 		seterror(ERR_INRED);
    582   1.1       cgd 	    else
    583   1.1       cgd 		t->t_dlef = Strsave(p->word);
    584   1.1       cgd 	    continue;
    585   1.9   mycroft 	savep:
    586   1.1       cgd 	    if (!specp)
    587   1.1       cgd 		continue;
    588   1.9   mycroft 	    /* FALLTHROUGH */
    589   1.1       cgd 	default:
    590   1.1       cgd 	    if (l != 0 && !specp)
    591   1.1       cgd 		continue;
    592   1.1       cgd 	    if (seterr == 0)
    593   1.1       cgd 		av[n] = Strsave(p->word);
    594   1.1       cgd 	    n++;
    595   1.1       cgd 	    continue;
    596   1.1       cgd 	}
    597   1.1       cgd     }
    598   1.1       cgd     if (lp != 0 && !specp) {
    599   1.1       cgd 	if (n != 0)
    600   1.1       cgd 	    seterror(ERR_BADPLPS);
    601   1.1       cgd 	t->t_dtyp = NODE_PAREN;
    602   1.1       cgd 	t->t_dspr = syn0(lp, rp, PHERE);
    603   1.1       cgd     }
    604   1.1       cgd     else {
    605   1.1       cgd 	if (n == 0)
    606   1.1       cgd 	    seterror(ERR_NULLCOM);
    607   1.1       cgd 	t->t_dtyp = NODE_COMMAND;
    608   1.1       cgd     }
    609   1.1       cgd     return (t);
    610   1.1       cgd }
    611   1.1       cgd 
    612   1.1       cgd void
    613  1.11       wiz freesyn(struct command *t)
    614   1.1       cgd {
    615   1.7       tls     Char **v;
    616   1.1       cgd 
    617   1.1       cgd     if (t == 0)
    618   1.1       cgd 	return;
    619   1.1       cgd     switch (t->t_dtyp) {
    620   1.1       cgd     case NODE_COMMAND:
    621   1.1       cgd 	for (v = t->t_dcom; *v; v++)
    622   1.1       cgd 	    xfree((ptr_t) * v);
    623  1.11       wiz 	xfree((ptr_t)(t->t_dcom));
    624  1.11       wiz 	xfree((ptr_t)t->t_dlef);
    625  1.11       wiz 	xfree((ptr_t)t->t_drit);
    626   1.1       cgd 	break;
    627   1.1       cgd     case NODE_PAREN:
    628   1.1       cgd 	freesyn(t->t_dspr);
    629  1.11       wiz 	xfree((ptr_t)t->t_dlef);
    630  1.11       wiz 	xfree((ptr_t)t->t_drit);
    631   1.1       cgd 	break;
    632   1.1       cgd     case NODE_AND:
    633   1.1       cgd     case NODE_OR:
    634   1.1       cgd     case NODE_PIPE:
    635   1.1       cgd     case NODE_LIST:
    636   1.1       cgd 	freesyn(t->t_dcar), freesyn(t->t_dcdr);
    637   1.1       cgd 	break;
    638   1.1       cgd     }
    639  1.11       wiz     xfree((ptr_t)t);
    640   1.1       cgd }
    641