VirtualBox

source: kBuild/trunk/src/kash/nodes.c.pat@ 3505

Last change on this file since 3505 was 3458, checked in by bird, 4 years ago

kash: Use reference counting of parser output in threaded-mode.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette