nodes.c.pat revision 3044
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3420425Ssteve * SUCH DAMAGE.
351556Srgrimes *
361556Srgrimes *	@(#)nodes.c.pat	8.1 (Berkeley) 5/31/93
371556Srgrimes *	$Id$
381556Srgrimes */
391556Srgrimes
4036150Scharnier/*
4136150Scharnier * Routine for dealing with parsed shell commands.
4236150Scharnier */
431556Srgrimes
4499110Sobrien#include "shell.h"
4599110Sobrien#include "nodes.h"
461556Srgrimes#include "memalloc.h"
4717987Speter#include "machdep.h"
481556Srgrimes#include "mystring.h"
4917987Speter
5017987Speter
511556Srgrimesint funcblocksize;		/* size of structures in function */
5217525Sacheint funcstringsize;		/* size of strings in node */
5359214Simp#ifdef __STDC__
5417525Sachepointer funcblock;		/* block to allocate function from */
551556Srgrimes#else
561556Srgrimeschar *funcblock;		/* block to allocate function from */
571556Srgrimes#endif
581556Srgrimeschar *funcstring;		/* block to allocate strings from */
591556Srgrimes
601556Srgrimes%SIZES
611556Srgrimes
6217987Speter
631556Srgrimes#ifdef __STDC__
641556SrgrimesSTATIC void calcsize(union node *);
651556SrgrimesSTATIC void sizenodelist(struct nodelist *);
661556SrgrimesSTATIC union node *copynode(union node *);
671556SrgrimesSTATIC struct nodelist *copynodelist(struct nodelist *);
6817987SpeterSTATIC char *nodesavestr(char *);
691556Srgrimes#else
701556SrgrimesSTATIC void calcsize();
711556SrgrimesSTATIC void sizenodelist();
721556SrgrimesSTATIC union node *copynode();
7317987SpeterSTATIC struct nodelist *copynodelist();
7420425SsteveSTATIC char *nodesavestr();
751556Srgrimes#endif
761556Srgrimes
771556Srgrimes
78200998Sjilles
79221669Sjilles/*
801556Srgrimes * Make a copy of a parse tree.
81213811Sobrien */
82213811Sobrien
831556Srgrimesunion node *
841556Srgrimescopyfunc(n)
851556Srgrimes      union node *n;
861556Srgrimes      {
871556Srgrimes      if (n == NULL)
881556Srgrimes	    return NULL;
891556Srgrimes      funcblocksize = 0;
901556Srgrimes      funcstringsize = 0;
911556Srgrimes      calcsize(n);
9217987Speter      funcblock = ckmalloc(funcblocksize + funcstringsize);
9390111Simp      funcstring = funcblock + funcblocksize;
9417987Speter      return copynode(n);
951556Srgrimes}
961556Srgrimes
971556Srgrimes
981556Srgrimes
9917525SacheSTATIC void
100221669Sjillescalcsize(n)
1011556Srgrimes      union node *n;
102200998Sjilles      {
10320425Ssteve      %CALCSIZE
10420425Ssteve}
10520425Ssteve
10620425Ssteve
10720425Ssteve
10820425SsteveSTATIC void
10920425Sstevesizenodelist(lp)
11020425Ssteve      struct nodelist *lp;
11120425Ssteve      {
11220425Ssteve      while (lp) {
11320425Ssteve	    funcblocksize += ALIGN(sizeof (struct nodelist));
11420425Ssteve	    calcsize(lp->n);
11520425Ssteve	    lp = lp->next;
116220978Sjilles      }
117220978Sjilles}
118218306Sjilles
1191556Srgrimes
120215567Sjilles
121215567SjillesSTATIC union node *
1221556Srgrimescopynode(n)
1231556Srgrimes      union node *n;
1241556Srgrimes      {
1251556Srgrimes      union node *new;
1261556Srgrimes
1271556Srgrimes      %COPY
1281556Srgrimes      return new;
1291556Srgrimes}
1301556Srgrimes
1311556Srgrimes
1321556SrgrimesSTATIC struct nodelist *
133200998Sjillescopynodelist(lp)
1341556Srgrimes      struct nodelist *lp;
1351556Srgrimes      {
1361556Srgrimes      struct nodelist *start;
1371556Srgrimes      struct nodelist **lpp;
1381556Srgrimes
1391556Srgrimes      lpp = &start;
1401556Srgrimes      while (lp) {
1411556Srgrimes	    *lpp = funcblock;
1421556Srgrimes	    funcblock += ALIGN(sizeof (struct nodelist));
143206759Sjilles	    (*lpp)->n = copynode(lp->n);
144203576Sjilles	    lp = lp->next;
145203576Sjilles	    lpp = &(*lpp)->next;
1461556Srgrimes      }
1471556Srgrimes      *lpp = NULL;
1481556Srgrimes      return start;
1491556Srgrimes}
1501556Srgrimes
15119240Ssteve
15219240Ssteve
15319240SsteveSTATIC char *
15419240Sstevenodesavestr(s)
1558855Srgrimes      char *s;
1561556Srgrimes      {
1571556Srgrimes      register char *p = s;
15825471Ssteve      register char *q = funcstring;
15917987Speter      char *rtn = funcstring;
16017987Speter
16117987Speter      while (*q++ = *p++);
16217987Speter      funcstring = q;
1631556Srgrimes      return rtn;
1641556Srgrimes}
1651556Srgrimes
1661556Srgrimes
167194128Sjilles
1681556Srgrimes/*
1691556Srgrimes * Free a parse tree.
1701556Srgrimes */
1711556Srgrimes
1721556Srgrimesvoid
1731556Srgrimesfreefunc(n)
17417987Speter      union node *n;
17517987Speter      {
1761556Srgrimes      if (n)
1771556Srgrimes	    ckfree(n);
1781556Srgrimes}
1791556Srgrimes