nodes.c.pat revision 196483
196462Sru/*-
296462Sru * Copyright (c) 1991, 1993
396462Sru *	The Regents of the University of California.  All rights reserved.
496462Sru *
596462Sru * This code is derived from software contributed to Berkeley by
696462Sru * Kenneth Almquist.
7156813Sru *
8155264Sru * Redistribution and use in source and binary forms, with or without
996462Sru * modification, are permitted provided that the following conditions
1096462Sru * are met:
1196668Sru * 1. Redistributions of source code must retain the above copyright
1296462Sru *    notice, this list of conditions and the following disclaimer.
1396668Sru * 2. Redistributions in binary form must reproduce the above copyright
1496462Sru *    notice, this list of conditions and the following disclaimer in the
1596462Sru *    documentation and/or other materials provided with the distribution.
1696462Sru * 4. Neither the name of the University nor the names of its contributors
1796668Sru *    may be used to endorse or promote products derived from this software
1896462Sru *    without specific prior written permission.
1996668Sru *
2096462Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2196462Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2296462Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2396462Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2496462Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2596462Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2696462Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2796462Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2896462Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2996462Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3096462Sru * SUCH DAMAGE.
3196462Sru *
3296462Sru *	@(#)nodes.c.pat	8.2 (Berkeley) 5/4/95
3396462Sru * $FreeBSD: head/bin/sh/nodes.c.pat 196483 2009-08-23 21:09:46Z jilles $
3496462Sru */
3596462Sru
3696462Sru#include <sys/param.h>
3796462Sru#include <stdlib.h>
3896462Sru#include <stddef.h>
3996462Sru/*
4096462Sru * Routine for dealing with parsed shell commands.
4196462Sru */
4296668Sru
4396462Sru#include "shell.h"
4496462Sru#include "nodes.h"
4596462Sru#include "memalloc.h"
4696462Sru#include "mystring.h"
4796462Sru
4896462Sru
4996462SruSTATIC int     funcblocksize;	/* size of structures in function */
5096462SruSTATIC int     funcstringsize;	/* size of strings in node */
5196462SruSTATIC pointer funcblock;	/* block to allocate function from */
5296462SruSTATIC char   *funcstring;	/* block to allocate strings from */
5396668Sru
5496462Sru%SIZES
5596462Sru
5696462Sru
5796462SruSTATIC void calcsize(union node *);
5896462SruSTATIC void sizenodelist(struct nodelist *);
5996462SruSTATIC union node *copynode(union node *);
6096462SruSTATIC struct nodelist *copynodelist(struct nodelist *);
6196462SruSTATIC char *nodesavestr(char *);
6296462Sru
6396462Sru
64144893Sharti
6596462Sru/*
6696462Sru * Make a copy of a parse tree.
6796462Sru */
6896668Sru
6996462Srustruct funcdef *
7096462Srucopyfunc(union node *n)
7196462Sru{
7296462Sru	struct funcdef *fn;
7396462Sru
7496462Sru	if (n == NULL)
7596462Sru		return NULL;
7696462Sru	funcblocksize = offsetof(struct funcdef, n);
7796462Sru	funcstringsize = 0;
7896462Sru	calcsize(n);
79144893Sharti	fn = ckmalloc(funcblocksize + funcstringsize);
8096668Sru	fn->refcount = 1;
8196668Sru	funcblock = (char *)fn + offsetof(struct funcdef, n);
8299343Sru	funcstring = (char *)fn + funcblocksize;
83155264Sru	copynode(n);
84156813Sru	return fn;
85}
86
87
88
89STATIC void
90calcsize(union node *n)
91{
92	%CALCSIZE
93}
94
95
96
97STATIC void
98sizenodelist(struct nodelist *lp)
99{
100	while (lp) {
101		funcblocksize += ALIGN(sizeof(struct nodelist));
102		calcsize(lp->n);
103		lp = lp->next;
104	}
105}
106
107
108
109STATIC union node *
110copynode(union node *n)
111{
112	union node *new;
113
114	%COPY
115	return new;
116}
117
118
119STATIC struct nodelist *
120copynodelist(struct nodelist *lp)
121{
122	struct nodelist *start;
123	struct nodelist **lpp;
124
125	lpp = &start;
126	while (lp) {
127		*lpp = funcblock;
128		funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist));
129		(*lpp)->n = copynode(lp->n);
130		lp = lp->next;
131		lpp = &(*lpp)->next;
132	}
133	*lpp = NULL;
134	return start;
135}
136
137
138
139STATIC char *
140nodesavestr(char *s)
141{
142	char *p = s;
143	char *q = funcstring;
144	char   *rtn = funcstring;
145
146	while ((*q++ = *p++) != '\0')
147		continue;
148	funcstring = q;
149	return rtn;
150}
151
152
153void
154reffunc(struct funcdef *fn)
155{
156	fn->refcount++;
157}
158
159
160/*
161 * Decrement the reference count of a function definition, freeing it
162 * if it falls to 0.
163 */
164
165void
166unreffunc(struct funcdef *fn)
167{
168	if (fn) {
169		fn->refcount--;
170		if (fn->refcount > 0)
171			return;
172		ckfree(fn);
173	}
174}
175