xref: /freebsd/usr.bin/find/operator.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)operator.c	8.1 (Berkeley) 6/6/93";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/types.h>
45 
46 #include <err.h>
47 #include <fts.h>
48 #include <stdio.h>
49 #include <time.h>
50 
51 #include "find.h"
52 
53 static PLAN *yanknode(PLAN **);
54 static PLAN *yankexpr(PLAN **);
55 
56 /*
57  * yanknode --
58  *	destructively removes the top from the plan
59  */
60 static PLAN *
61 yanknode(PLAN **planp)
62 {
63 	PLAN *node;		/* top node removed from the plan */
64 
65 	if ((node = (*planp)) == NULL)
66 		return (NULL);
67 	(*planp) = (*planp)->next;
68 	node->next = NULL;
69 	return (node);
70 }
71 
72 /*
73  * yankexpr --
74  *	Removes one expression from the plan.  This is used mainly by
75  *	paren_squish.  In comments below, an expression is either a
76  *	simple node or a f_expr node containing a list of simple nodes.
77  */
78 static PLAN *
79 yankexpr(PLAN **planp)
80 {
81 	PLAN *next;		/* temp node holding subexpression results */
82 	PLAN *node;		/* pointer to returned node or expression */
83 	PLAN *tail;		/* pointer to tail of subplan */
84 	PLAN *subplan;		/* pointer to head of ( ) expression */
85 
86 	/* first pull the top node from the plan */
87 	if ((node = yanknode(planp)) == NULL)
88 		return (NULL);
89 
90 	/*
91 	 * If the node is an '(' then we recursively slurp up expressions
92 	 * until we find its associated ')'.  If it's a closing paren we
93 	 * just return it and unwind our recursion; all other nodes are
94 	 * complete expressions, so just return them.
95 	 */
96 	if (node->execute == f_openparen)
97 		for (tail = subplan = NULL;;) {
98 			if ((next = yankexpr(planp)) == NULL)
99 				errx(1, "(: missing closing ')'");
100 			/*
101 			 * If we find a closing ')' we store the collected
102 			 * subplan in our '(' node and convert the node to
103 			 * a f_expr.  The ')' we found is ignored.  Otherwise,
104 			 * we just continue to add whatever we get to our
105 			 * subplan.
106 			 */
107 			if (next->execute == f_closeparen) {
108 				if (subplan == NULL)
109 					errx(1, "(): empty inner expression");
110 				node->p_data[0] = subplan;
111 				node->execute = f_expr;
112 				break;
113 			} else {
114 				if (subplan == NULL)
115 					tail = subplan = next;
116 				else {
117 					tail->next = next;
118 					tail = next;
119 				}
120 				tail->next = NULL;
121 			}
122 		}
123 	return (node);
124 }
125 
126 /*
127  * paren_squish --
128  *	replaces "parenthesized" plans in our search plan with "expr" nodes.
129  */
130 PLAN *
131 paren_squish(PLAN *plan)
132 {
133 	PLAN *expr;		/* pointer to next expression */
134 	PLAN *tail;		/* pointer to tail of result plan */
135 	PLAN *result;		/* pointer to head of result plan */
136 
137 	result = tail = NULL;
138 
139 	/*
140 	 * the basic idea is to have yankexpr do all our work and just
141 	 * collect its results together.
142 	 */
143 	while ((expr = yankexpr(&plan)) != NULL) {
144 		/*
145 		 * if we find an unclaimed ')' it means there is a missing
146 		 * '(' someplace.
147 		 */
148 		if (expr->execute == f_closeparen)
149 			errx(1, "): no beginning '('");
150 
151 		/* add the expression to our result plan */
152 		if (result == NULL)
153 			tail = result = expr;
154 		else {
155 			tail->next = expr;
156 			tail = expr;
157 		}
158 		tail->next = NULL;
159 	}
160 	return (result);
161 }
162 
163 /*
164  * not_squish --
165  *	compresses "!" expressions in our search plan.
166  */
167 PLAN *
168 not_squish(PLAN *plan)
169 {
170 	PLAN *next;		/* next node being processed */
171 	PLAN *node;		/* temporary node used in f_not processing */
172 	PLAN *tail;		/* pointer to tail of result plan */
173 	PLAN *result;		/* pointer to head of result plan */
174 
175 	tail = result = NULL;
176 
177 	while ((next = yanknode(&plan))) {
178 		/*
179 		 * if we encounter a ( expression ) then look for nots in
180 		 * the expr subplan.
181 		 */
182 		if (next->execute == f_expr)
183 			next->p_data[0] = not_squish(next->p_data[0]);
184 
185 		/*
186 		 * if we encounter a not, then snag the next node and place
187 		 * it in the not's subplan.  As an optimization we compress
188 		 * several not's to zero or one not.
189 		 */
190 		if (next->execute == f_not) {
191 			int notlevel = 1;
192 
193 			node = yanknode(&plan);
194 			while (node != NULL && node->execute == f_not) {
195 				++notlevel;
196 				node = yanknode(&plan);
197 			}
198 			if (node == NULL)
199 				errx(1, "!: no following expression");
200 			if (node->execute == f_or)
201 				errx(1, "!: nothing between ! and -o");
202 			/*
203 			 * If we encounter ! ( expr ) then look for nots in
204 			 * the expr subplan.
205 			 */
206 			if (node->execute == f_expr)
207 				node->p_data[0] = not_squish(node->p_data[0]);
208 			if (notlevel % 2 != 1)
209 				next = node;
210 			else
211 				next->p_data[0] = node;
212 		}
213 
214 		/* add the node to our result plan */
215 		if (result == NULL)
216 			tail = result = next;
217 		else {
218 			tail->next = next;
219 			tail = next;
220 		}
221 		tail->next = NULL;
222 	}
223 	return (result);
224 }
225 
226 /*
227  * or_squish --
228  *	compresses -o expressions in our search plan.
229  */
230 PLAN *
231 or_squish(PLAN *plan)
232 {
233 	PLAN *next;		/* next node being processed */
234 	PLAN *tail;		/* pointer to tail of result plan */
235 	PLAN *result;		/* pointer to head of result plan */
236 
237 	tail = result = next = NULL;
238 
239 	while ((next = yanknode(&plan)) != NULL) {
240 		/*
241 		 * if we encounter a ( expression ) then look for or's in
242 		 * the expr subplan.
243 		 */
244 		if (next->execute == f_expr)
245 			next->p_data[0] = or_squish(next->p_data[0]);
246 
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]);
250 
251 		/*
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.
255 		 */
256 		if (next->execute == f_or) {
257 			if (result == NULL)
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");
263 			return (next);
264 		}
265 
266 		/* add the node to our result plan */
267 		if (result == NULL)
268 			tail = result = next;
269 		else {
270 			tail->next = next;
271 			tail = next;
272 		}
273 		tail->next = NULL;
274 	}
275 	return (result);
276 }
277