operator.c revision 1.6 1 /* $NetBSD: operator.c,v 1.6 1998/02/21 22:47:21 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 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 * Cimarron D. Taylor of the University of California, Berkeley.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and 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
24 * without specific prior written permission.
25 *
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
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "from: @(#)operator.c 8.1 (Berkeley) 6/6/93";
43 #else
44 __RCSID("$NetBSD: operator.c,v 1.6 1998/02/21 22:47:21 christos Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <sys/types.h>
49
50 #include <err.h>
51 #include <fts.h>
52 #include <stdio.h>
53
54 #include "find.h"
55
56 static PLAN *yanknode __P((PLAN **));
57 static PLAN *yankexpr __P((PLAN **));
58
59 /*
60 * yanknode --
61 * destructively removes the top from the plan
62 */
63 static PLAN *
64 yanknode(planp)
65 PLAN **planp; /* pointer to top of plan (modified) */
66 {
67 PLAN *node; /* top node removed from the plan */
68
69 if ((node = (*planp)) == NULL)
70 return (NULL);
71 (*planp) = (*planp)->next;
72 node->next = NULL;
73 return (node);
74 }
75
76 /*
77 * yankexpr --
78 * Removes one expression from the plan. This is used mainly by
79 * paren_squish. In comments below, an expression is either a
80 * simple node or a N_EXPR node containing a list of simple nodes.
81 */
82 static PLAN *
83 yankexpr(planp)
84 PLAN **planp; /* pointer to top of plan (modified) */
85 {
86 PLAN *next; /* temp node holding subexpression results */
87 PLAN *node; /* pointer to returned node or expression */
88 PLAN *tail; /* pointer to tail of subplan */
89 PLAN *subplan; /* pointer to head of ( ) expression */
90
91 /* first pull the top node from the plan */
92 if ((node = yanknode(planp)) == NULL)
93 return (NULL);
94
95 /*
96 * If the node is an '(' then we recursively slurp up expressions
97 * until we find its associated ')'. If it's a closing paren we
98 * just return it and unwind our recursion; all other nodes are
99 * complete expressions, so just return them.
100 */
101 if (node->type == N_OPENPAREN)
102 for (tail = subplan = NULL;;) {
103 if ((next = yankexpr(planp)) == NULL)
104 err(1, "(: missing closing ')'");
105 /*
106 * If we find a closing ')' we store the collected
107 * subplan in our '(' node and convert the node to
108 * a N_EXPR. The ')' we found is ignored. Otherwise,
109 * we just continue to add whatever we get to our
110 * subplan.
111 */
112 if (next->type == N_CLOSEPAREN) {
113 if (subplan == NULL)
114 errx(1, "(): empty inner expression");
115 node->p_data[0] = subplan;
116 node->type = N_EXPR;
117 node->eval = f_expr;
118 break;
119 } else {
120 if (subplan == NULL)
121 tail = subplan = next;
122 else {
123 tail->next = next;
124 tail = next;
125 }
126 tail->next = NULL;
127 }
128 }
129 return (node);
130 }
131
132 /*
133 * paren_squish --
134 * replaces "parentheisized" plans in our search plan with "expr" nodes.
135 */
136 PLAN *
137 paren_squish(plan)
138 PLAN *plan; /* plan with ( ) nodes */
139 {
140 PLAN *expr; /* pointer to next expression */
141 PLAN *tail; /* pointer to tail of result plan */
142 PLAN *result; /* pointer to head of result plan */
143
144 result = tail = NULL;
145
146 /*
147 * the basic idea is to have yankexpr do all our work and just
148 * collect it's results together.
149 */
150 while ((expr = yankexpr(&plan)) != NULL) {
151 /*
152 * if we find an unclaimed ')' it means there is a missing
153 * '(' someplace.
154 */
155 if (expr->type == N_CLOSEPAREN)
156 errx(1, "): no beginning '('");
157
158 /* add the expression to our result plan */
159 if (result == NULL)
160 tail = result = expr;
161 else {
162 tail->next = expr;
163 tail = expr;
164 }
165 tail->next = NULL;
166 }
167 return (result);
168 }
169
170 /*
171 * not_squish --
172 * compresses "!" expressions in our search plan.
173 */
174 PLAN *
175 not_squish(plan)
176 PLAN *plan; /* plan to process */
177 {
178 PLAN *next; /* next node being processed */
179 PLAN *node; /* temporary node used in N_NOT processing */
180 PLAN *tail; /* pointer to tail of result plan */
181 PLAN *result; /* pointer to head of result plan */
182
183 tail = result = next = NULL;
184
185 while ((next = yanknode(&plan)) != NULL) {
186 /*
187 * if we encounter a ( expression ) then look for nots in
188 * the expr subplan.
189 */
190 if (next->type == N_EXPR)
191 next->p_data[0] = not_squish(next->p_data[0]);
192
193 /*
194 * if we encounter a not, then snag the next node and place
195 * it in the not's subplan. As an optimization we compress
196 * several not's to zero or one not.
197 */
198 if (next->type == N_NOT) {
199 int notlevel = 1;
200
201 node = yanknode(&plan);
202 while (node->type == N_NOT) {
203 ++notlevel;
204 node = yanknode(&plan);
205 }
206 if (node == NULL)
207 errx(1, "!: no following expression");
208 if (node->type == N_OR)
209 errx(1, "!: nothing between ! and -o");
210 if (notlevel % 2 != 1)
211 next = node;
212 else
213 next->p_data[0] = node;
214 }
215
216 /* add the node to our result plan */
217 if (result == NULL)
218 tail = result = next;
219 else {
220 tail->next = next;
221 tail = next;
222 }
223 tail->next = NULL;
224 }
225 return (result);
226 }
227
228 /*
229 * or_squish --
230 * compresses -o expressions in our search plan.
231 */
232 PLAN *
233 or_squish(plan)
234 PLAN *plan; /* plan with ors to be squished */
235 {
236 PLAN *next; /* next node being processed */
237 PLAN *tail; /* pointer to tail of result plan */
238 PLAN *result; /* pointer to head of result plan */
239
240 tail = result = next = NULL;
241
242 while ((next = yanknode(&plan)) != NULL) {
243 /*
244 * if we encounter a ( expression ) then look for or's in
245 * the expr subplan.
246 */
247 if (next->type == N_EXPR)
248 next->p_data[0] = or_squish(next->p_data[0]);
249
250 /* if we encounter a not then look for not's in the subplan */
251 if (next->type == N_NOT)
252 next->p_data[0] = or_squish(next->p_data[0]);
253
254 /*
255 * if we encounter an or, then place our collected plan in the
256 * or's first subplan and then recursively collect the
257 * remaining stuff into the second subplan and return the or.
258 */
259 if (next->type == N_OR) {
260 if (result == NULL)
261 errx(1, "-o: no expression before -o");
262 next->p_data[0] = result;
263 next->p_data[1] = or_squish(plan);
264 if (next->p_data[1] == NULL)
265 errx(1, "-o: no expression after -o");
266 return (next);
267 }
268
269 /* add the node to our result plan */
270 if (result == NULL)
271 tail = result = next;
272 else {
273 tail->next = next;
274 tail = next;
275 }
276 tail->next = NULL;
277 }
278 return (result);
279 }
280