xref: /netbsd-src/bin/sh/nodes.c.pat (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * 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	5.2 (Berkeley) 3/8/91
37 *
38 *	$Header: /cvsroot/src/bin/sh/nodes.c.pat,v 1.3 1993/03/23 00:28:55 cgd Exp $
39 */
40
41/*
42 * Routine for dealing with parsed shell commands.
43 */
44
45#include "shell.h"
46#include "nodes.h"
47#include "memalloc.h"
48#include "machdep.h"
49#include "mystring.h"
50
51
52int funcblocksize;		/* size of structures in function */
53int funcstringsize;		/* size of strings in node */
54#ifdef __STDC__
55pointer funcblock;		/* block to allocate function from */
56#else
57char *funcblock;		/* block to allocate function from */
58#endif
59char *funcstring;		/* block to allocate strings from */
60
61%SIZES
62
63
64#ifdef __STDC__
65STATIC void calcsize(union node *);
66STATIC void sizenodelist(struct nodelist *);
67STATIC union node *copynode(union node *);
68STATIC struct nodelist *copynodelist(struct nodelist *);
69STATIC char *nodesavestr(char *);
70#else
71STATIC void calcsize();
72STATIC void sizenodelist();
73STATIC union node *copynode();
74STATIC struct nodelist *copynodelist();
75STATIC char *nodesavestr();
76#endif
77
78
79
80/*
81 * Make a copy of a parse tree.
82 */
83
84union node *
85copyfunc(n)
86      union node *n;
87      {
88      if (n == NULL)
89	    return NULL;
90      funcblocksize = 0;
91      funcstringsize = 0;
92      calcsize(n);
93      funcblock = ckmalloc(funcblocksize + funcstringsize);
94      funcstring = funcblock + funcblocksize;
95      return copynode(n);
96}
97
98
99
100STATIC void
101calcsize(n)
102      union node *n;
103      {
104      %CALCSIZE
105}
106
107
108
109STATIC void
110sizenodelist(lp)
111      struct nodelist *lp;
112      {
113      while (lp) {
114	    funcblocksize += ALIGN(sizeof (struct nodelist));
115	    calcsize(lp->n);
116	    lp = lp->next;
117      }
118}
119
120
121
122STATIC union node *
123copynode(n)
124      union node *n;
125      {
126      union node *new;
127
128      %COPY
129      return new;
130}
131
132
133STATIC struct nodelist *
134copynodelist(lp)
135      struct nodelist *lp;
136      {
137      struct nodelist *start;
138      struct nodelist **lpp;
139
140      lpp = &start;
141      while (lp) {
142	    *lpp = funcblock;
143	    funcblock += ALIGN(sizeof (struct nodelist));
144	    (*lpp)->n = copynode(lp->n);
145	    lp = lp->next;
146	    lpp = &(*lpp)->next;
147      }
148      *lpp = NULL;
149      return start;
150}
151
152
153
154STATIC char *
155nodesavestr(s)
156      char *s;
157      {
158      register char *p = s;
159      register char *q = funcstring;
160      char *rtn = funcstring;
161
162      while (*q++ = *p++);
163      funcstring = q;
164      return rtn;
165}
166
167
168
169/*
170 * Free a parse tree.
171 */
172
173void
174freefunc(n)
175      union node *n;
176      {
177      if (n)
178	    ckfree(n);
179}
180