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