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#include <sys/types.h>
36
37#include <err.h>
38#include <fts.h>
39#include <stdio.h>
40#include <time.h>
41
42#include "find.h"
43
44static PLAN *yanknode(PLAN **);
45static PLAN *yankexpr(PLAN **);
46
47/*
48 * yanknode --
49 *	destructively removes the top from the plan
50 */
51static PLAN *
52yanknode(PLAN **planp)
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 f_expr node containing a list of simple nodes.
68 */
69static PLAN *
70yankexpr(PLAN **planp)
71{
72	PLAN *next;		/* temp node holding subexpression results */
73	PLAN *node;		/* pointer to returned node or expression */
74	PLAN *tail;		/* pointer to tail of subplan */
75	PLAN *subplan;		/* pointer to head of ( ) expression */
76
77	/* first pull the top node from the plan */
78	if ((node = yanknode(planp)) == NULL)
79		return (NULL);
80
81	/*
82	 * If the node is an '(' then we recursively slurp up expressions
83	 * until we find its associated ')'.  If it's a closing paren we
84	 * just return it and unwind our recursion; all other nodes are
85	 * complete expressions, so just return them.
86	 */
87	if (node->execute == f_openparen)
88		for (tail = subplan = NULL;;) {
89			if ((next = yankexpr(planp)) == NULL)
90				errx(1, "(: missing closing ')'");
91			/*
92			 * If we find a closing ')' we store the collected
93			 * subplan in our '(' node and convert the node to
94			 * a f_expr.  The ')' we found is ignored.  Otherwise,
95			 * we just continue to add whatever we get to our
96			 * subplan.
97			 */
98			if (next->execute == f_closeparen) {
99				if (subplan == NULL)
100					errx(1, "(): empty inner expression");
101				node->p_data[0] = subplan;
102				node->execute = f_expr;
103				break;
104			} else {
105				if (subplan == NULL)
106					tail = subplan = next;
107				else {
108					tail->next = next;
109					tail = next;
110				}
111				tail->next = NULL;
112			}
113		}
114	return (node);
115}
116
117/*
118 * paren_squish --
119 *	replaces "parenthesized" plans in our search plan with "expr" nodes.
120 */
121PLAN *
122paren_squish(PLAN *plan)
123{
124	PLAN *expr;		/* pointer to next expression */
125	PLAN *tail;		/* pointer to tail of result plan */
126	PLAN *result;		/* pointer to head of result plan */
127
128	result = tail = NULL;
129
130	/*
131	 * the basic idea is to have yankexpr do all our work and just
132	 * collect its results together.
133	 */
134	while ((expr = yankexpr(&plan)) != NULL) {
135		/*
136		 * if we find an unclaimed ')' it means there is a missing
137		 * '(' someplace.
138		 */
139		if (expr->execute == f_closeparen)
140			errx(1, "): no beginning '('");
141
142		/* add the expression to our result plan */
143		if (result == NULL)
144			tail = result = expr;
145		else {
146			tail->next = expr;
147			tail = expr;
148		}
149		tail->next = NULL;
150	}
151	return (result);
152}
153
154/*
155 * not_squish --
156 *	compresses "!" expressions in our search plan.
157 */
158PLAN *
159not_squish(PLAN *plan)
160{
161	PLAN *next;		/* next node being processed */
162	PLAN *node;		/* temporary node used in f_not processing */
163	PLAN *tail;		/* pointer to tail of result plan */
164	PLAN *result;		/* pointer to head of result plan */
165
166	tail = result = NULL;
167
168	while ((next = yanknode(&plan))) {
169		/*
170		 * if we encounter a ( expression ) then look for nots in
171		 * the expr subplan.
172		 */
173		if (next->execute == f_expr)
174			next->p_data[0] = not_squish(next->p_data[0]);
175
176		/*
177		 * if we encounter a not, then snag the next node and place
178		 * it in the not's subplan.  As an optimization we compress
179		 * several not's to zero or one not.
180		 */
181		if (next->execute == f_not) {
182			int notlevel = 1;
183
184			node = yanknode(&plan);
185			while (node != NULL && node->execute == f_not) {
186				++notlevel;
187				node = yanknode(&plan);
188			}
189			if (node == NULL)
190				errx(1, "!: no following expression");
191			if (node->execute == f_or)
192				errx(1, "!: nothing between ! and -o");
193			/*
194			 * If we encounter ! ( expr ) then look for nots in
195			 * the expr subplan.
196			 */
197			if (node->execute == f_expr)
198				node->p_data[0] = not_squish(node->p_data[0]);
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 */
221PLAN *
222or_squish(PLAN *plan)
223{
224	PLAN *next;		/* next node being processed */
225	PLAN *tail;		/* pointer to tail of result plan */
226	PLAN *result;		/* pointer to head of result plan */
227
228	tail = result = next = NULL;
229
230	while ((next = yanknode(&plan)) != NULL) {
231		/*
232		 * if we encounter a ( expression ) then look for or's in
233		 * the expr subplan.
234		 */
235		if (next->execute == f_expr)
236			next->p_data[0] = or_squish(next->p_data[0]);
237
238		/* if we encounter a not then look for or's in the subplan */
239		if (next->execute == f_not)
240			next->p_data[0] = or_squish(next->p_data[0]);
241
242		/*
243		 * if we encounter an or, then place our collected plan in the
244		 * or's first subplan and then recursively collect the
245		 * remaining stuff into the second subplan and return the or.
246		 */
247		if (next->execute == f_or) {
248			if (result == NULL)
249				errx(1, "-o: no expression before -o");
250			next->p_data[0] = result;
251			next->p_data[1] = or_squish(plan);
252			if (next->p_data[1] == NULL)
253				errx(1, "-o: no expression after -o");
254			return (next);
255		}
256
257		/* add the node to our result plan */
258		if (result == NULL)
259			tail = result = next;
260		else {
261			tail->next = next;
262			tail = next;
263		}
264		tail->next = NULL;
265	}
266	return (result);
267}
268