xref: /freebsd/bin/sh/nodes.c.pat (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <stdlib.h>
35#include <stddef.h>
36/*
37 * Routine for dealing with parsed shell commands.
38 */
39
40#include "shell.h"
41#include "nodes.h"
42#include "memalloc.h"
43#include "mystring.h"
44
45
46struct nodesize {
47	int     blocksize;	/* size of structures in function */
48	int     stringsize;	/* size of strings in node */
49};
50
51struct nodecopystate {
52	pointer block;		/* block to allocate function from */
53	char   *string;		/* block to allocate strings from */
54};
55
56%SIZES
57
58
59static void calcsize(union node *, struct nodesize *);
60static void sizenodelist(struct nodelist *, struct nodesize *);
61static union node *copynode(union node *, struct nodecopystate *);
62static struct nodelist *copynodelist(struct nodelist *, struct nodecopystate *);
63static char *nodesavestr(const char *, struct nodecopystate *);
64
65
66struct funcdef {
67	unsigned int refcount;
68	union node n;
69};
70
71/*
72 * Make a copy of a parse tree.
73 */
74
75struct funcdef *
76copyfunc(union node *n)
77{
78	struct nodesize sz;
79	struct nodecopystate st;
80	struct funcdef *fn;
81
82	if (n == NULL)
83		return NULL;
84	sz.blocksize = offsetof(struct funcdef, n);
85	sz.stringsize = 0;
86	calcsize(n, &sz);
87	fn = ckmalloc(sz.blocksize + sz.stringsize);
88	fn->refcount = 1;
89	st.block = (char *)fn + offsetof(struct funcdef, n);
90	st.string = (char *)fn + sz.blocksize;
91	copynode(n, &st);
92	return fn;
93}
94
95
96union node *
97getfuncnode(struct funcdef *fn)
98{
99	return fn == NULL ? NULL : &fn->n;
100}
101
102
103static void
104calcsize(union node *n, struct nodesize *result)
105{
106	%CALCSIZE
107}
108
109
110
111static void
112sizenodelist(struct nodelist *lp, struct nodesize *result)
113{
114	while (lp) {
115		result->blocksize += ALIGN(sizeof(struct nodelist));
116		calcsize(lp->n, result);
117		lp = lp->next;
118	}
119}
120
121
122
123static union node *
124copynode(union node *n, struct nodecopystate *state)
125{
126	union node *new;
127
128	%COPY
129	return new;
130}
131
132
133static struct nodelist *
134copynodelist(struct nodelist *lp, struct nodecopystate *state)
135{
136	struct nodelist *start;
137	struct nodelist **lpp;
138
139	lpp = &start;
140	while (lp) {
141		*lpp = state->block;
142		state->block = (char *)state->block +
143		    ALIGN(sizeof(struct nodelist));
144		(*lpp)->n = copynode(lp->n, state);
145		lp = lp->next;
146		lpp = &(*lpp)->next;
147	}
148	*lpp = NULL;
149	return start;
150}
151
152
153
154static char *
155nodesavestr(const char *s, struct nodecopystate *state)
156{
157	const char *p = s;
158	char *q = state->string;
159	char   *rtn = state->string;
160
161	while ((*q++ = *p++) != '\0')
162		continue;
163	state->string = q;
164	return rtn;
165}
166
167
168void
169reffunc(struct funcdef *fn)
170{
171	if (fn)
172		fn->refcount++;
173}
174
175
176/*
177 * Decrement the reference count of a function definition, freeing it
178 * if it falls to 0.
179 */
180
181void
182unreffunc(struct funcdef *fn)
183{
184	if (fn) {
185		fn->refcount--;
186		if (fn->refcount > 0)
187			return;
188		ckfree(fn);
189	}
190}
191