Home | History | Annotate | Line # | Download | only in sh
nodes.c.pat revision 1.5
      1 /*-
      2  * Copyright (c) 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Kenneth Almquist.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	from: @(#)nodes.c.pat	8.1 (Berkeley) 5/31/93
     37  *	$Id: nodes.c.pat,v 1.5 1994/06/11 16:12:21 mycroft Exp $
     38  */
     39 
     40 /*
     41  * Routine for dealing with parsed shell commands.
     42  */
     43 
     44 #include "shell.h"
     45 #include "nodes.h"
     46 #include "memalloc.h"
     47 #include "machdep.h"
     48 #include "mystring.h"
     49 
     50 
     51 int funcblocksize;		/* size of structures in function */
     52 int funcstringsize;		/* size of strings in node */
     53 #ifdef __STDC__
     54 pointer funcblock;		/* block to allocate function from */
     55 #else
     56 char *funcblock;		/* block to allocate function from */
     57 #endif
     58 char *funcstring;		/* block to allocate strings from */
     59 
     60 %SIZES
     61 
     62 
     63 #ifdef __STDC__
     64 STATIC void calcsize(union node *);
     65 STATIC void sizenodelist(struct nodelist *);
     66 STATIC union node *copynode(union node *);
     67 STATIC struct nodelist *copynodelist(struct nodelist *);
     68 STATIC char *nodesavestr(char *);
     69 #else
     70 STATIC void calcsize();
     71 STATIC void sizenodelist();
     72 STATIC union node *copynode();
     73 STATIC struct nodelist *copynodelist();
     74 STATIC char *nodesavestr();
     75 #endif
     76 
     77 
     78 
     79 /*
     80  * Make a copy of a parse tree.
     81  */
     82 
     83 union node *
     84 copyfunc(n)
     85       union node *n;
     86       {
     87       if (n == NULL)
     88 	    return NULL;
     89       funcblocksize = 0;
     90       funcstringsize = 0;
     91       calcsize(n);
     92       funcblock = ckmalloc(funcblocksize + funcstringsize);
     93       funcstring = funcblock + funcblocksize;
     94       return copynode(n);
     95 }
     96 
     97 
     98 
     99 STATIC void
    100 calcsize(n)
    101       union node *n;
    102       {
    103       %CALCSIZE
    104 }
    105 
    106 
    107 
    108 STATIC void
    109 sizenodelist(lp)
    110       struct nodelist *lp;
    111       {
    112       while (lp) {
    113 	    funcblocksize += ALIGN(sizeof (struct nodelist));
    114 	    calcsize(lp->n);
    115 	    lp = lp->next;
    116       }
    117 }
    118 
    119 
    120 
    121 STATIC union node *
    122 copynode(n)
    123       union node *n;
    124       {
    125       union node *new;
    126 
    127       %COPY
    128       return new;
    129 }
    130 
    131 
    132 STATIC struct nodelist *
    133 copynodelist(lp)
    134       struct nodelist *lp;
    135       {
    136       struct nodelist *start;
    137       struct nodelist **lpp;
    138 
    139       lpp = &start;
    140       while (lp) {
    141 	    *lpp = funcblock;
    142 	    funcblock += ALIGN(sizeof (struct nodelist));
    143 	    (*lpp)->n = copynode(lp->n);
    144 	    lp = lp->next;
    145 	    lpp = &(*lpp)->next;
    146       }
    147       *lpp = NULL;
    148       return start;
    149 }
    150 
    151 
    152 
    153 STATIC char *
    154 nodesavestr(s)
    155       char *s;
    156       {
    157       register char *p = s;
    158       register char *q = funcstring;
    159       char *rtn = funcstring;
    160 
    161       while (*q++ = *p++);
    162       funcstring = q;
    163       return rtn;
    164 }
    165 
    166 
    167 
    168 /*
    169  * Free a parse tree.
    170  */
    171 
    172 void
    173 freefunc(n)
    174       union node *n;
    175       {
    176       if (n)
    177 	    ckfree(n);
    178 }
    179