nodes.c.pat revision 50471
160812Sps/*-
260786Sps * Copyright (c) 1991, 1993
3221715Sdelphij *	The Regents of the University of California.  All rights reserved.
460786Sps *
560786Sps * This code is derived from software contributed to Berkeley by
660786Sps * Kenneth Almquist.
760786Sps *
860786Sps * Redistribution and use in source and binary forms, with or without
960786Sps * modification, are permitted provided that the following conditions
1060786Sps * are met:
1160786Sps * 1. Redistributions of source code must retain the above copyright
1260786Sps *    notice, this list of conditions and the following disclaimer.
1360786Sps * 2. Redistributions in binary form must reproduce the above copyright
1460786Sps *    notice, this list of conditions and the following disclaimer in the
1560786Sps *    documentation and/or other materials provided with the distribution.
1660786Sps * 3. All advertising materials mentioning features or use of this software
1760786Sps *    must display the following acknowledgement:
1889022Sps *	This product includes software developed by the University of
1989022Sps *	California, Berkeley and its contributors.
2089022Sps * 4. Neither the name of the University nor the names of its contributors
2160786Sps *    may be used to endorse or promote products derived from this software
2260786Sps *    without specific prior written permission.
2360786Sps *
2460786Sps * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2560786Sps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2660786Sps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2760786Sps * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2860786Sps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2960786Sps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3060786Sps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3160786Sps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3260786Sps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3360786Sps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3460786Sps * SUCH DAMAGE.
3560786Sps *
3660786Sps *	@(#)nodes.c.pat	8.2 (Berkeley) 5/4/95
3760786Sps * $FreeBSD: head/bin/sh/nodes.c.pat 50471 1999-08-27 23:15:48Z peter $
3860786Sps */
3960786Sps
4060786Sps#include <stdlib.h>
4160786Sps/*
4260786Sps * Routine for dealing with parsed shell commands.
4360786Sps */
4460786Sps
4560786Sps#include "shell.h"
4660786Sps#include "nodes.h"
4760786Sps#include "memalloc.h"
4860786Sps#include "machdep.h"
4989022Sps#include "mystring.h"
5060786Sps
5160786Sps
5260786Spsint     funcblocksize;		/* size of structures in function */
5360786Spsint     funcstringsize;		/* size of strings in node */
5489022Spspointer funcblock;		/* block to allocate function from */
5589022Spschar   *funcstring;		/* block to allocate strings from */
5689022Sps
5789022Sps%SIZES
58221715Sdelphij
5960786Sps
6060786SpsSTATIC void calcsize __P((union node *));
61170259SdelphijSTATIC void sizenodelist __P((struct nodelist *));
62171009SdelphijSTATIC union node *copynode __P((union node *));
63170259SdelphijSTATIC struct nodelist *copynodelist __P((struct nodelist *));
6460786SpsSTATIC char *nodesavestr __P((char *));
6560786Sps
6660786Sps
6760786Sps
6860786Sps/*
6960786Sps * Make a copy of a parse tree.
7060786Sps */
7160786Sps
7260786Spsunion node *
7360786Spscopyfunc(n)
7460786Sps	union node *n;
7560786Sps{
7660812Sps	if (n == NULL)
7760786Sps		return NULL;
7860786Sps	funcblocksize = 0;
7960786Sps	funcstringsize = 0;
8060786Sps	calcsize(n);
8160786Sps	funcblock = ckmalloc(funcblocksize + funcstringsize);
8260786Sps	funcstring = (char *)funcblock + funcblocksize;
8360786Sps	return copynode(n);
8460786Sps}
8560786Sps
8660786Sps
8760786Sps
8860786SpsSTATIC void
8960786Spscalcsize(n)
9060786Sps	union node *n;
9160786Sps{
9260786Sps	%CALCSIZE
9360786Sps}
9460786Sps
9560786Sps
9660786Sps
9760786SpsSTATIC void
9860786Spssizenodelist(lp)
9960786Sps	struct nodelist *lp;
10060786Sps{
10160786Sps	while (lp) {
10260786Sps		funcblocksize += ALIGN(sizeof(struct nodelist));
10360786Sps		calcsize(lp->n);
10460786Sps		lp = lp->next;
10560786Sps	}
10660786Sps}
10760786Sps
10860786Sps
10960786Sps
11089022SpsSTATIC union node *
11160786Spscopynode(n)
11260786Sps	union node *n;
11360786Sps{
11460786Sps	union node *new;
11560786Sps
11660786Sps	%COPY
11760786Sps	return new;
11860786Sps}
11960786Sps
12060786Sps
12160786SpsSTATIC struct nodelist *
122161478Sdelphijcopynodelist(lp)
12360786Sps	struct nodelist *lp;
124195941Sdelphij{
125170259Sdelphij	struct nodelist *start;
126170259Sdelphij	struct nodelist **lpp;
127170259Sdelphij
128170259Sdelphij	lpp = &start;
129170259Sdelphij	while (lp) {
130170259Sdelphij		*lpp = funcblock;
131170259Sdelphij		funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist));
132170259Sdelphij		(*lpp)->n = copynode(lp->n);
133170259Sdelphij		lp = lp->next;
13460812Sps		lpp = &(*lpp)->next;
135170259Sdelphij	}
136170259Sdelphij	*lpp = NULL;
137170259Sdelphij	return start;
138170259Sdelphij}
139170259Sdelphij
140170812Sdelphij
141170812Sdelphij
142170812SdelphijSTATIC char *
143170259Sdelphijnodesavestr(s)
14460786Sps	char   *s;
14560786Sps{
14660786Sps	char *p = s;
147170963Sdelphij	char *q = funcstring;
148170963Sdelphij	char   *rtn = funcstring;
14960786Sps
15060786Sps	while ((*q++ = *p++) != '\0')
15160786Sps		continue;
15260786Sps	funcstring = q;
15360786Sps	return rtn;
15460786Sps}
15560786Sps
15660786Sps
15760786Sps
15860786Sps/*
15960786Sps * Free a parse tree.
16060786Sps */
16160786Sps
16260786Spsvoid
16360786Spsfreefunc(n)
16460786Sps	union node *n;
16560786Sps{
16660786Sps	if (n)
16760786Sps		ckfree(n);
16860786Sps}
169171817Sdelphij