nodes.c.pat revision 90111
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
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes *
3617987Speter *	@(#)nodes.c.pat	8.2 (Berkeley) 5/4/95
3750471Speter * $FreeBSD: head/bin/sh/nodes.c.pat 90111 2002-02-02 06:50:57Z imp $
381556Srgrimes */
391556Srgrimes
4017987Speter#include <stdlib.h>
411556Srgrimes/*
421556Srgrimes * Routine for dealing with parsed shell commands.
431556Srgrimes */
441556Srgrimes
451556Srgrimes#include "shell.h"
461556Srgrimes#include "nodes.h"
471556Srgrimes#include "memalloc.h"
481556Srgrimes#include "machdep.h"
491556Srgrimes#include "mystring.h"
501556Srgrimes
511556Srgrimes
5217987Speterint     funcblocksize;		/* size of structures in function */
5317987Speterint     funcstringsize;		/* size of strings in node */
541556Srgrimespointer funcblock;		/* block to allocate function from */
5517987Speterchar   *funcstring;		/* block to allocate strings from */
561556Srgrimes
571556Srgrimes%SIZES
581556Srgrimes
591556Srgrimes
6090111SimpSTATIC void calcsize(union node *);
6190111SimpSTATIC void sizenodelist(struct nodelist *);
6290111SimpSTATIC union node *copynode(union node *);
6390111SimpSTATIC struct nodelist *copynodelist(struct nodelist *);
6490111SimpSTATIC char *nodesavestr(char *);
651556Srgrimes
661556Srgrimes
671556Srgrimes
681556Srgrimes/*
691556Srgrimes * Make a copy of a parse tree.
701556Srgrimes */
711556Srgrimes
721556Srgrimesunion node *
7390111Simpcopyfunc(union node *n)
7417987Speter{
7517987Speter	if (n == NULL)
7617987Speter		return NULL;
7717987Speter	funcblocksize = 0;
7817987Speter	funcstringsize = 0;
7917987Speter	calcsize(n);
8017987Speter	funcblock = ckmalloc(funcblocksize + funcstringsize);
8125226Ssteve	funcstring = (char *)funcblock + funcblocksize;
8217987Speter	return copynode(n);
831556Srgrimes}
841556Srgrimes
851556Srgrimes
861556Srgrimes
871556SrgrimesSTATIC void
8890111Simpcalcsize(union node *n)
8917987Speter{
9017987Speter	%CALCSIZE
911556Srgrimes}
921556Srgrimes
931556Srgrimes
941556Srgrimes
951556SrgrimesSTATIC void
9690111Simpsizenodelist(struct nodelist *lp)
9717987Speter{
9817987Speter	while (lp) {
9917987Speter		funcblocksize += ALIGN(sizeof(struct nodelist));
10017987Speter		calcsize(lp->n);
10117987Speter		lp = lp->next;
10217987Speter	}
1031556Srgrimes}
1041556Srgrimes
1051556Srgrimes
1061556Srgrimes
1071556SrgrimesSTATIC union node *
10890111Simpcopynode(union node *n)
10917987Speter{
11017987Speter	union node *new;
1111556Srgrimes
11217987Speter	%COPY
11317987Speter	return new;
1141556Srgrimes}
1151556Srgrimes
1161556Srgrimes
1171556SrgrimesSTATIC struct nodelist *
11890111Simpcopynodelist(struct nodelist *lp)
11917987Speter{
12017987Speter	struct nodelist *start;
12117987Speter	struct nodelist **lpp;
1221556Srgrimes
12317987Speter	lpp = &start;
12417987Speter	while (lp) {
12517987Speter		*lpp = funcblock;
12625226Ssteve		funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist));
12717987Speter		(*lpp)->n = copynode(lp->n);
12817987Speter		lp = lp->next;
12917987Speter		lpp = &(*lpp)->next;
13017987Speter	}
13117987Speter	*lpp = NULL;
13217987Speter	return start;
1331556Srgrimes}
1341556Srgrimes
1351556Srgrimes
1361556Srgrimes
1371556SrgrimesSTATIC char *
13890111Simpnodesavestr(char *s)
13917987Speter{
14025226Ssteve	char *p = s;
14125226Ssteve	char *q = funcstring;
14217987Speter	char   *rtn = funcstring;
1431556Srgrimes
14417987Speter	while ((*q++ = *p++) != '\0')
14517987Speter		continue;
14617987Speter	funcstring = q;
14717987Speter	return rtn;
1481556Srgrimes}
1491556Srgrimes
1501556Srgrimes
1511556Srgrimes
1521556Srgrimes/*
1531556Srgrimes * Free a parse tree.
1541556Srgrimes */
1551556Srgrimes
1561556Srgrimesvoid
15790111Simpfreefunc(union node *n)
15817987Speter{
15917987Speter	if (n)
16017987Speter		ckfree(n);
1611556Srgrimes}
162