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