date and time created 91/03/07 20:27:27 by bostic
[unix-history] / usr / src / bin / sh / nodes.c.pat
CommitLineData
e83ac6ec
KB
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 * %sccs.include.redist.c%
9 *
10 * @(#)nodes.c.pat 5.1 (Berkeley) %G%
11 */
12
13/*
14 * Routine for dealing with parsed shell commands.
15 */
16
17#include "shell.h"
18#include "nodes.h"
19#include "memalloc.h"
20#include "machdep.h"
21#include "mystring.h"
22
23
24int funcblocksize; /* size of structures in function */
25int funcstringsize; /* size of strings in node */
26pointer funcblock; /* block to allocate function from */
27char *funcstring; /* block to allocate strings from */
28
29%SIZES
30
31
32#ifdef __STDC__
33STATIC void calcsize(union node *);
34STATIC void sizenodelist(struct nodelist *);
35STATIC union node *copynode(union node *);
36STATIC struct nodelist *copynodelist(struct nodelist *);
37STATIC char *nodesavestr(char *);
38#else
39STATIC void calcsize();
40STATIC void sizenodelist();
41STATIC union node *copynode();
42STATIC struct nodelist *copynodelist();
43STATIC char *nodesavestr();
44#endif
45
46
47
48/*
49 * Make a copy of a parse tree.
50 */
51
52union node *
53copyfunc(n)
54 union node *n;
55 {
56 if (n == NULL)
57 return NULL;
58 funcblocksize = 0;
59 funcstringsize = 0;
60 calcsize(n);
61 funcblock = ckmalloc(funcblocksize + funcstringsize);
62 funcstring = funcblock + funcblocksize;
63 return copynode(n);
64}
65
66
67
68STATIC void
69calcsize(n)
70 union node *n;
71 {
72 %CALCSIZE
73}
74
75
76
77STATIC void
78sizenodelist(lp)
79 struct nodelist *lp;
80 {
81 while (lp) {
82 funcblocksize += ALIGN(sizeof (struct nodelist));
83 calcsize(lp->n);
84 lp = lp->next;
85 }
86}
87
88
89
90STATIC union node *
91copynode(n)
92 union node *n;
93 {
94 union node *new;
95
96 %COPY
97 return new;
98}
99
100
101STATIC struct nodelist *
102copynodelist(lp)
103 struct nodelist *lp;
104 {
105 struct nodelist *start;
106 struct nodelist **lpp;
107
108 lpp = &start;
109 while (lp) {
110 *lpp = funcblock;
111 funcblock += ALIGN(sizeof (struct nodelist));
112 (*lpp)->n = copynode(lp->n);
113 lp = lp->next;
114 lpp = &(*lpp)->next;
115 }
116 *lpp = NULL;
117 return start;
118}
119
120
121
122STATIC char *
123nodesavestr(s)
124 char *s;
125 {
126 register char *p = s;
127 register char *q = funcstring;
128 char *rtn = funcstring;
129
130 while (*q++ = *p++);
131 funcstring = q;
132 return rtn;
133}
134
135
136
137/*
138 * Free a parse tree.
139 */
140
141void
142freefunc(n)
143 union node *n;
144 {
145 if (n)
146 ckfree(n);
147}