xref: /dragonfly/bin/sh/nodes.c.pat (revision dadd6466)
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. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)nodes.c.pat	8.2 (Berkeley) 5/4/95
37 * $FreeBSD: head/bin/sh/nodes.c.pat 249235 2013-04-07 16:28:36Z jilles $
38 */
39
40#include <sys/param.h>
41#include <stdlib.h>
42#include <stddef.h>
43/*
44 * Routine for dealing with parsed shell commands.
45 */
46
47#include "shell.h"
48#include "nodes.h"
49#include "memalloc.h"
50#include "mystring.h"
51
52
53static int     funcblocksize;	/* size of structures in function */
54static int     funcstringsize;	/* size of strings in node */
55static pointer funcblock;	/* block to allocate function from */
56static char   *funcstring;	/* block to allocate strings from */
57
58%SIZES
59
60
61static void calcsize(union node *);
62static void sizenodelist(struct nodelist *);
63static union node *copynode(union node *);
64static struct nodelist *copynodelist(struct nodelist *);
65static char *nodesavestr(const char *);
66
67
68struct funcdef {
69	unsigned int refcount;
70	union node n;
71};
72
73/*
74 * Make a copy of a parse tree.
75 */
76
77struct funcdef *
78copyfunc(union node *n)
79{
80	struct funcdef *fn;
81
82	if (n == NULL)
83		return NULL;
84	funcblocksize = offsetof(struct funcdef, n);
85	funcstringsize = 0;
86	calcsize(n);
87	fn = ckmalloc(funcblocksize + funcstringsize);
88	fn->refcount = 1;
89	funcblock = (char *)fn + offsetof(struct funcdef, n);
90	funcstring = (char *)fn + funcblocksize;
91	copynode(n);
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)
105{
106	%CALCSIZE
107}
108
109
110
111static void
112sizenodelist(struct nodelist *lp)
113{
114	while (lp) {
115		funcblocksize += ALIGN(sizeof(struct nodelist));
116		calcsize(lp->n);
117		lp = lp->next;
118	}
119}
120
121
122
123static union node *
124copynode(union node *n)
125{
126	union node *new;
127
128	%COPY
129	return new;
130}
131
132
133static struct nodelist *
134copynodelist(struct nodelist *lp)
135{
136	struct nodelist *start;
137	struct nodelist **lpp;
138
139	lpp = &start;
140	while (lp) {
141		*lpp = funcblock;
142		funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist));
143		(*lpp)->n = copynode(lp->n);
144		lp = lp->next;
145		lpp = &(*lpp)->next;
146	}
147	*lpp = NULL;
148	return start;
149}
150
151
152
153static char *
154nodesavestr(const char *s)
155{
156	const char *p = s;
157	char *q = funcstring;
158	char   *rtn = funcstring;
159
160	while ((*q++ = *p++) != '\0')
161		continue;
162	funcstring = q;
163	return rtn;
164}
165
166
167void
168reffunc(struct funcdef *fn)
169{
170	if (fn)
171		fn->refcount++;
172}
173
174
175/*
176 * Decrement the reference count of a function definition, freeing it
177 * if it falls to 0.
178 */
179
180void
181unreffunc(struct funcdef *fn)
182{
183	if (fn) {
184		fn->refcount--;
185		if (fn->refcount > 0)
186			return;
187		ckfree(fn);
188	}
189}
190