nodes.c.pat revision 17987
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
3436150Scharnier * SUCH DAMAGE.
3536150Scharnier *
3636150Scharnier *	@(#)nodes.c.pat	8.2 (Berkeley) 5/4/95
371556Srgrimes *	$Id: nodes.c.pat,v 1.2 1994/09/24 02:58:02 davidg Exp $
3899110Sobrien */
3999110Sobrien
401556Srgrimes#include <stdlib.h>
41213775Sjhb/*
42213775Sjhb * Routine for dealing with parsed shell commands.
43213775Sjhb */
44213775Sjhb
45213775Sjhb#include "shell.h"
46213775Sjhb#include "nodes.h"
4717987Speter#include "memalloc.h"
48213775Sjhb#include "machdep.h"
4917987Speter#include "mystring.h"
50213925Sjilles
51213775Sjhb
5217987Speterint     funcblocksize;		/* size of structures in function */
5317987Speterint     funcstringsize;		/* size of strings in node */
541556Srgrimespointer funcblock;		/* block to allocate function from */
551556Srgrimeschar   *funcstring;		/* block to allocate strings from */
5617987Speter
571556Srgrimes%SIZES
581556Srgrimes
5917987Speter
6017987SpeterSTATIC void calcsize __P((union node *));
611556SrgrimesSTATIC void sizenodelist __P((struct nodelist *));
621556SrgrimesSTATIC union node *copynode __P((union node *));
631556SrgrimesSTATIC struct nodelist *copynodelist __P((struct nodelist *));
641556SrgrimesSTATIC char *nodesavestr __P((char *));
651556Srgrimes
661556Srgrimes
671556Srgrimes
681556Srgrimes/*
691556Srgrimes * Make a copy of a parse tree.
701556Srgrimes */
711556Srgrimes
721556Srgrimesunion node *
73223024Sjillescopyfunc(n)
741556Srgrimes	union node *n;
751556Srgrimes{
76213760Sobrien	if (n == NULL)
77213760Sobrien		return NULL;
7828346Ssteve	funcblocksize = 0;
79209600Sjilles	funcstringsize = 0;
801556Srgrimes	calcsize(n);
81213760Sobrien	funcblock = ckmalloc(funcblocksize + funcstringsize);
82213760Sobrien	funcstring = funcblock + funcblocksize;
831556Srgrimes	return copynode(n);
8438536Scracauer}
8538950Scracauer
8638536Scracauer
8799762Stjr
881556SrgrimesSTATIC void
8920425Sstevecalcsize(n)
90213811Sobrien	union node *n;
9120425Ssteve{
92213811Sobrien	%CALCSIZE
93213811Sobrien}
94213811Sobrien
95213811Sobrien
96213811Sobrien
97213811SobrienSTATIC void
98213811Sobriensizenodelist(lp)
9997659Stjr	struct nodelist *lp;
100213811Sobrien{
101213811Sobrien	while (lp) {
102213811Sobrien		funcblocksize += ALIGN(sizeof(struct nodelist));
10397659Stjr		calcsize(lp->n);
104216217Sjilles		lp = lp->next;
105216217Sjilles	}
1061556Srgrimes}
1071556Srgrimes
1081556Srgrimes
1091556Srgrimes
1101556SrgrimesSTATIC union node *
1111556Srgrimescopynode(n)
1121556Srgrimes	union node *n;
1131556Srgrimes{
11420425Ssteve	union node *new;
1151556Srgrimes
11690111Simp	%COPY
11717987Speter	return new;
11899762Stjr}
1191556Srgrimes
1201556Srgrimes
1211556SrgrimesSTATIC struct nodelist *
1221556Srgrimescopynodelist(lp)
12399762Stjr	struct nodelist *lp;
12499762Stjr{
12599762Stjr	struct nodelist *start;
12699762Stjr	struct nodelist **lpp;
12799762Stjr
12899762Stjr	lpp = &start;
129109927Stjr	while (lp) {
13099762Stjr		*lpp = funcblock;
13199762Stjr		funcblock += ALIGN(sizeof(struct nodelist));
132109927Stjr		(*lpp)->n = copynode(lp->n);
133109927Stjr		lp = lp->next;
134109927Stjr		lpp = &(*lpp)->next;
135109927Stjr	}
136109927Stjr	*lpp = NULL;
137109927Stjr	return start;
138109927Stjr}
139109927Stjr
140109927Stjr
141109927Stjr
142109927StjrSTATIC char *
143109927Stjrnodesavestr(s)
144109927Stjr	char   *s;
145103223Snectar{
14699762Stjr	register char *p = s;
14799762Stjr	register char *q = funcstring;
14899762Stjr	char   *rtn = funcstring;
14999762Stjr
1501556Srgrimes	while ((*q++ = *p++) != '\0')
15199762Stjr		continue;
15220425Ssteve	funcstring = q;
153199629Sjilles	return rtn;
1541556Srgrimes}
1551556Srgrimes
1561556Srgrimes
157216400Sjilles
158216400Sjilles/*
1591556Srgrimes * Free a parse tree.
1601556Srgrimes */
1611556Srgrimes
1621556Srgrimesvoid
1631556Srgrimesfreefunc(n)
1641556Srgrimes	union node *n;
16517987Speter{
16699762Stjr	if (n)
1671556Srgrimes		ckfree(n);
16817987Speter}
16999762Stjr