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