xref: /netbsd-src/usr.bin/find/operator.c (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Cimarron D. Taylor of the University of California, Berkeley.
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 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)operator.c	5.4 (Berkeley) 5/24/91";*/
39 static char rcsid[] = "$Id: operator.c,v 1.2 1993/08/01 18:16:10 mycroft Exp $";
40 #endif /* not lint */
41 
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include "find.h"
45 
46 /*
47  * yanknode --
48  *	destructively removes the top from the plan
49  */
50 static PLAN *
51 yanknode(planp)
52 	PLAN **planp;		/* pointer to top of plan (modified) */
53 {
54 	PLAN *node;		/* top node removed from the plan */
55 
56 	if ((node = (*planp)) == NULL)
57 		return(NULL);
58 	(*planp) = (*planp)->next;
59 	node->next = NULL;
60 	return(node);
61 }
62 
63 /*
64  * yankexpr --
65  *	Removes one expression from the plan.  This is used mainly by
66  *	paren_squish.  In comments below, an expression is either a
67  *	simple node or a N_EXPR node containing a list of simple nodes.
68  */
69 static PLAN *
70 yankexpr(planp)
71 	PLAN **planp;		/* pointer to top of plan (modified) */
72 {
73 	register PLAN *next;	/* temp node holding subexpression results */
74 	PLAN *node;		/* pointer to returned node or expression */
75 	PLAN *tail;		/* pointer to tail of subplan */
76 	PLAN *subplan;		/* pointer to head of ( ) expression */
77 	int f_expr();
78 
79 	/* first pull the top node from the plan */
80 	if ((node = yanknode(planp)) == NULL)
81 		return(NULL);
82 
83 	/*
84 	 * If the node is an '(' then we recursively slurp up expressions
85 	 * until we find its associated ')'.  If it's a closing paren we
86 	 * just return it and unwind our recursion; all other nodes are
87 	 * complete expressions, so just return them.
88 	 */
89 	if (node->type == N_OPENPAREN)
90 		for (tail = subplan = NULL;;) {
91 			if ((next = yankexpr(planp)) == NULL)
92 				err("%s: %s", "(", "missing closing ')'");
93 			/*
94 			 * If we find a closing ')' we store the collected
95 			 * subplan in our '(' node and convert the node to
96 			 * a N_EXPR.  The ')' we found is ignored.  Otherwise,
97 			 * we just continue to add whatever we get to our
98 			 * subplan.
99 			 */
100 			if (next->type == N_CLOSEPAREN) {
101 				if (subplan == NULL)
102 					err("%s: %s",
103 					    "()", "empty inner expression");
104 				node->p_data[0] = subplan;
105 				node->type = N_EXPR;
106 				node->eval = f_expr;
107 				break;
108 			} else {
109 				if (subplan == NULL)
110 					tail = subplan = next;
111 				else {
112 					tail->next = next;
113 					tail = next;
114 				}
115 				tail->next = NULL;
116 			}
117 		}
118 	return(node);
119 }
120 
121 /*
122  * paren_squish --
123  *	replaces "parentheisized" plans in our search plan with "expr" nodes.
124  */
125 PLAN *
126 paren_squish(plan)
127 	PLAN *plan;		/* plan with ( ) nodes */
128 {
129 	register PLAN *expr;	/* pointer to next expression */
130 	register PLAN *tail;	/* pointer to tail of result plan */
131 	PLAN *result;		/* pointer to head of result plan */
132 
133 	result = tail = NULL;
134 
135 	/*
136 	 * the basic idea is to have yankexpr do all our work and just
137 	 * collect it's results together.
138 	 */
139 	while ((expr = yankexpr(&plan)) != NULL) {
140 		/*
141 		 * if we find an unclaimed ')' it means there is a missing
142 		 * '(' someplace.
143 		 */
144 		if (expr->type == N_CLOSEPAREN)
145 			err("%s: %s", ")", "no beginning '('");
146 
147 		/* add the expression to our result plan */
148 		if (result == NULL)
149 			tail = result = expr;
150 		else {
151 			tail->next = expr;
152 			tail = expr;
153 		}
154 		tail->next = NULL;
155 	}
156 	return(result);
157 }
158 
159 /*
160  * not_squish --
161  *	compresses "!" expressions in our search plan.
162  */
163 PLAN *
164 not_squish(plan)
165 	PLAN *plan;		/* plan to process */
166 {
167 	register PLAN *next;	/* next node being processed */
168 	register PLAN *node;	/* temporary node used in N_NOT processing */
169 	register PLAN *tail;	/* pointer to tail of result plan */
170 	PLAN *result;		/* pointer to head of result plan */
171 
172 	tail = result = next = NULL;
173 
174 	while ((next = yanknode(&plan)) != NULL) {
175 		/*
176 		 * if we encounter a ( expression ) then look for nots in
177 		 * the expr subplan.
178 		 */
179 		if (next->type == N_EXPR)
180 			next->p_data[0] = not_squish(next->p_data[0]);
181 
182 		/*
183 		 * if we encounter a not, then snag the next node and place
184 		 * it in the not's subplan.  As an optimization we compress
185 		 * several not's to zero or one not.
186 		 */
187 		if (next->type == N_NOT) {
188 			int notlevel = 1;
189 
190 			node = yanknode(&plan);
191 			while (node->type == N_NOT) {
192 				++notlevel;
193 				node = yanknode(&plan);
194 			}
195 			if (node == NULL)
196 				err("%s: %s", "!", "no following expression");
197 			if (node->type == N_OR)
198 				err("%s: %s", "!", "nothing between ! and -o");
199 			if (notlevel % 2 != 1)
200 				next = node;
201 			else
202 				next->p_data[0] = node;
203 		}
204 
205 		/* add the node to our result plan */
206 		if (result == NULL)
207 			tail = result = next;
208 		else {
209 			tail->next = next;
210 			tail = next;
211 		}
212 		tail->next = NULL;
213 	}
214 	return(result);
215 }
216 
217 /*
218  * or_squish --
219  *	compresses -o expressions in our search plan.
220  */
221 PLAN *
222 or_squish(plan)
223 	PLAN *plan;		/* plan with ors to be squished */
224 {
225 	register PLAN *next;	/* next node being processed */
226 	register PLAN *tail;	/* pointer to tail of result plan */
227 	PLAN *result;		/* pointer to head of result plan */
228 
229 	tail = result = next = NULL;
230 
231 	while ((next = yanknode(&plan)) != NULL) {
232 		/*
233 		 * if we encounter a ( expression ) then look for or's in
234 		 * the expr subplan.
235 		 */
236 		if (next->type == N_EXPR)
237 			next->p_data[0] = or_squish(next->p_data[0]);
238 
239 		/* if we encounter a not then look for not's in the subplan */
240 		if (next->type == N_NOT)
241 			next->p_data[0] = or_squish(next->p_data[0]);
242 
243 		/*
244 		 * if we encounter an or, then place our collected plan in the
245 		 * or's first subplan and then recursively collect the
246 		 * remaining stuff into the second subplan and return the or.
247 		 */
248 		if (next->type == N_OR) {
249 			if (result == NULL)
250 				err("%s: %s", "-o", "no expression before -o");
251 			next->p_data[0] = result;
252 			next->p_data[1] = or_squish(plan);
253 			if (next->p_data[1] == NULL)
254 				err("%s: %s", "-o", "no expression after -o");
255 			return(next);
256 		}
257 
258 		/* add the node to our result plan */
259 		if (result == NULL)
260 			tail = result = next;
261 		else {
262 			tail->next = next;
263 			tail = next;
264 		}
265 		tail->next = NULL;
266 	}
267 	return(result);
268 }
269