]>
git.saurik.com Git - apple/shell_cmds.git/blob - find/operator.c
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * $FreeBSD: src/usr.bin/find/operator.c,v 1.5.6.1 2001/05/06 09:53:22 phk Exp $
40 static char sccsid
[] = "@(#)operator.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/types.h>
53 * destructively removes the top from the plan
57 PLAN
**planp
; /* pointer to top of plan (modified) */
59 PLAN
*node
; /* top node removed from the plan */
61 if ((node
= (*planp
)) == NULL
)
63 (*planp
) = (*planp
)->next
;
70 * Removes one expression from the plan. This is used mainly by
71 * paren_squish. In comments below, an expression is either a
72 * simple node or a f_expr node containing a list of simple nodes.
76 PLAN
**planp
; /* pointer to top of plan (modified) */
78 register PLAN
*next
; /* temp node holding subexpression results */
79 PLAN
*node
; /* pointer to returned node or expression */
80 PLAN
*tail
; /* pointer to tail of subplan */
81 PLAN
*subplan
; /* pointer to head of ( ) expression */
83 /* first pull the top node from the plan */
84 if ((node
= yanknode(planp
)) == NULL
)
88 * If the node is an '(' then we recursively slurp up expressions
89 * until we find its associated ')'. If it's a closing paren we
90 * just return it and unwind our recursion; all other nodes are
91 * complete expressions, so just return them.
93 if (node
->execute
== f_openparen
)
94 for (tail
= subplan
= NULL
;;) {
95 if ((next
= yankexpr(planp
)) == NULL
)
96 err(1, "(: missing closing ')'");
98 * If we find a closing ')' we store the collected
99 * subplan in our '(' node and convert the node to
100 * a f_expr. The ')' we found is ignored. Otherwise,
101 * we just continue to add whatever we get to our
104 if (next
->execute
== f_closeparen
) {
106 errx(1, "(): empty inner expression");
107 node
->p_data
[0] = subplan
;
108 node
->execute
= f_expr
;
112 tail
= subplan
= next
;
125 * replaces "parentheisized" plans in our search plan with "expr" nodes.
129 PLAN
*plan
; /* plan with ( ) nodes */
131 register PLAN
*expr
; /* pointer to next expression */
132 register PLAN
*tail
; /* pointer to tail of result plan */
133 PLAN
*result
; /* pointer to head of result plan */
135 result
= tail
= NULL
;
138 * the basic idea is to have yankexpr do all our work and just
139 * collect its results together.
141 while ((expr
= yankexpr(&plan
)) != NULL
) {
143 * if we find an unclaimed ')' it means there is a missing
146 if (expr
->execute
== f_closeparen
)
147 errx(1, "): no beginning '('");
149 /* add the expression to our result plan */
151 tail
= result
= expr
;
163 * compresses "!" expressions in our search plan.
167 PLAN
*plan
; /* plan to process */
169 register PLAN
*next
; /* next node being processed */
170 register PLAN
*node
; /* temporary node used in f_not processing */
171 register PLAN
*tail
; /* pointer to tail of result plan */
172 PLAN
*result
; /* pointer to head of result plan */
174 tail
= result
= NULL
;
176 while (next
= yanknode(&plan
)) {
178 * if we encounter a ( expression ) then look for nots in
181 if (next
->execute
== f_expr
)
182 next
->p_data
[0] = not_squish(next
->p_data
[0]);
185 * if we encounter a not, then snag the next node and place
186 * it in the not's subplan. As an optimization we compress
187 * several not's to zero or one not.
189 if (next
->execute
== f_not
) {
192 node
= yanknode(&plan
);
193 while (node
!= NULL
&& node
->execute
== f_not
) {
195 node
= yanknode(&plan
);
198 errx(1, "!: no following expression");
199 if (node
->execute
== f_or
)
200 errx(1, "!: nothing between ! and -o");
202 * If we encounter ! ( expr ) then look for nots in
205 if (node
->execute
== f_expr
)
206 node
->p_data
[0] = not_squish(node
->p_data
[0]);
207 if (notlevel
% 2 != 1)
210 next
->p_data
[0] = node
;
213 /* add the node to our result plan */
215 tail
= result
= next
;
227 * compresses -o expressions in our search plan.
231 PLAN
*plan
; /* plan with ors to be squished */
233 register PLAN
*next
; /* next node being processed */
234 register PLAN
*tail
; /* pointer to tail of result plan */
235 PLAN
*result
; /* pointer to head of result plan */
237 tail
= result
= next
= NULL
;
239 while ((next
= yanknode(&plan
)) != NULL
) {
241 * if we encounter a ( expression ) then look for or's in
244 if (next
->execute
== f_expr
)
245 next
->p_data
[0] = or_squish(next
->p_data
[0]);
247 /* if we encounter a not then look for or's in the subplan */
248 if (next
->execute
== f_not
)
249 next
->p_data
[0] = or_squish(next
->p_data
[0]);
252 * if we encounter an or, then place our collected plan in the
253 * or's first subplan and then recursively collect the
254 * remaining stuff into the second subplan and return the or.
256 if (next
->execute
== f_or
) {
258 errx(1, "-o: no expression before -o");
259 next
->p_data
[0] = result
;
260 next
->p_data
[1] = or_squish(plan
);
261 if (next
->p_data
[1] == NULL
)
262 errx(1, "-o: no expression after -o");
266 /* add the node to our result plan */
268 tail
= result
= next
;