nodes.c.pat revision 50471
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 50471 1999-08-27 23:15:48Z peter $
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
6017987SpeterSTATIC void calcsize __P((union node *));
6117987SpeterSTATIC void sizenodelist __P((struct nodelist *));
6217987SpeterSTATIC union node *copynode __P((union node *));
6317987SpeterSTATIC struct nodelist *copynodelist __P((struct nodelist *));
6417987SpeterSTATIC char *nodesavestr __P((char *));
651556Srgrimes
661556Srgrimes
671556Srgrimes
681556Srgrimes/*
691556Srgrimes * Make a copy of a parse tree.
701556Srgrimes */
711556Srgrimes
721556Srgrimesunion node *
731556Srgrimescopyfunc(n)
7417987Speter	union node *n;
7517987Speter{
7617987Speter	if (n == NULL)
7717987Speter		return NULL;
7817987Speter	funcblocksize = 0;
7917987Speter	funcstringsize = 0;
8017987Speter	calcsize(n);
8117987Speter	funcblock = ckmalloc(funcblocksize + funcstringsize);
8225226Ssteve	funcstring = (char *)funcblock + funcblocksize;
8317987Speter	return copynode(n);
841556Srgrimes}
851556Srgrimes
861556Srgrimes
871556Srgrimes
881556SrgrimesSTATIC void
891556Srgrimescalcsize(n)
9017987Speter	union node *n;
9117987Speter{
9217987Speter	%CALCSIZE
931556Srgrimes}
941556Srgrimes
951556Srgrimes
961556Srgrimes
971556SrgrimesSTATIC void
981556Srgrimessizenodelist(lp)
9917987Speter	struct nodelist *lp;
10017987Speter{
10117987Speter	while (lp) {
10217987Speter		funcblocksize += ALIGN(sizeof(struct nodelist));
10317987Speter		calcsize(lp->n);
10417987Speter		lp = lp->next;
10517987Speter	}
1061556Srgrimes}
1071556Srgrimes
1081556Srgrimes
1091556Srgrimes
1101556SrgrimesSTATIC union node *
1111556Srgrimescopynode(n)
11217987Speter	union node *n;
11317987Speter{
11417987Speter	union node *new;
1151556Srgrimes
11617987Speter	%COPY
11717987Speter	return new;
1181556Srgrimes}
1191556Srgrimes
1201556Srgrimes
1211556SrgrimesSTATIC struct nodelist *
1221556Srgrimescopynodelist(lp)
12317987Speter	struct nodelist *lp;
12417987Speter{
12517987Speter	struct nodelist *start;
12617987Speter	struct nodelist **lpp;
1271556Srgrimes
12817987Speter	lpp = &start;
12917987Speter	while (lp) {
13017987Speter		*lpp = funcblock;
13125226Ssteve		funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist));
13217987Speter		(*lpp)->n = copynode(lp->n);
13317987Speter		lp = lp->next;
13417987Speter		lpp = &(*lpp)->next;
13517987Speter	}
13617987Speter	*lpp = NULL;
13717987Speter	return start;
1381556Srgrimes}
1391556Srgrimes
1401556Srgrimes
1411556Srgrimes
1421556SrgrimesSTATIC char *
1431556Srgrimesnodesavestr(s)
14417987Speter	char   *s;
14517987Speter{
14625226Ssteve	char *p = s;
14725226Ssteve	char *q = funcstring;
14817987Speter	char   *rtn = funcstring;
1491556Srgrimes
15017987Speter	while ((*q++ = *p++) != '\0')
15117987Speter		continue;
15217987Speter	funcstring = q;
15317987Speter	return rtn;
1541556Srgrimes}
1551556Srgrimes
1561556Srgrimes
1571556Srgrimes
1581556Srgrimes/*
1591556Srgrimes * Free a parse tree.
1601556Srgrimes */
1611556Srgrimes
1621556Srgrimesvoid
1631556Srgrimesfreefunc(n)
16417987Speter	union node *n;
16517987Speter{
16617987Speter	if (n)
16717987Speter		ckfree(n);
1681556Srgrimes}
169