2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)operator.c 8.1 (Berkeley) 6/6/93";
39 #include <sys/cdefs.h>
40 #include <sys/types.h>
49 static PLAN *yanknode(PLAN **);
50 static PLAN *yankexpr(PLAN **);
54 * destructively removes the top from the plan
57 yanknode(PLAN **planp)
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.
75 yankexpr(PLAN **planp)
77 PLAN *next; /* temp node holding subexpression results */
78 PLAN *node; /* pointer to returned node or expression */
79 PLAN *tail; /* pointer to tail of subplan */
80 PLAN *subplan; /* pointer to head of ( ) expression */
82 /* first pull the top node from the plan */
83 if ((node = yanknode(planp)) == NULL)
87 * If the node is an '(' then we recursively slurp up expressions
88 * until we find its associated ')'. If it's a closing paren we
89 * just return it and unwind our recursion; all other nodes are
90 * complete expressions, so just return them.
92 if (node->execute == f_openparen)
93 for (tail = subplan = NULL;;) {
94 if ((next = yankexpr(planp)) == NULL)
95 errx(1, "(: missing closing ')'");
97 * If we find a closing ')' we store the collected
98 * subplan in our '(' node and convert the node to
99 * a f_expr. The ')' we found is ignored. Otherwise,
100 * we just continue to add whatever we get to our
103 if (next->execute == f_closeparen) {
105 errx(1, "(): empty inner expression");
106 node->p_data[0] = subplan;
107 node->execute = f_expr;
111 tail = subplan = next;
124 * replaces "parenthesized" plans in our search plan with "expr" nodes.
127 paren_squish(PLAN *plan)
129 PLAN *expr; /* pointer to next expression */
130 PLAN *tail; /* pointer to tail of result plan */
131 PLAN *result; /* pointer to head of result plan */
133 result = tail = NULL;
136 * the basic idea is to have yankexpr do all our work and just
137 * collect its results together.
139 while ((expr = yankexpr(&plan)) != NULL) {
141 * if we find an unclaimed ')' it means there is a missing
144 if (expr->execute == f_closeparen)
145 errx(1, "): no beginning '('");
147 /* add the expression to our result plan */
149 tail = result = expr;
161 * compresses "!" expressions in our search plan.
164 not_squish(PLAN *plan)
166 PLAN *next; /* next node being processed */
167 PLAN *node; /* temporary node used in f_not processing */
168 PLAN *tail; /* pointer to tail of result plan */
169 PLAN *result; /* pointer to head of result plan */
171 tail = result = NULL;
173 while ((next = yanknode(&plan))) {
175 * if we encounter a ( expression ) then look for nots in
178 if (next->execute == f_expr)
179 next->p_data[0] = not_squish(next->p_data[0]);
182 * if we encounter a not, then snag the next node and place
183 * it in the not's subplan. As an optimization we compress
184 * several not's to zero or one not.
186 if (next->execute == f_not) {
189 node = yanknode(&plan);
190 while (node != NULL && node->execute == f_not) {
192 node = yanknode(&plan);
195 errx(1, "!: no following expression");
196 if (node->execute == f_or)
197 errx(1, "!: nothing between ! and -o");
199 * If we encounter ! ( expr ) then look for nots in
202 if (node->execute == f_expr)
203 node->p_data[0] = not_squish(node->p_data[0]);
204 if (notlevel % 2 != 1)
207 next->p_data[0] = node;
210 /* add the node to our result plan */
212 tail = result = next;
224 * compresses -o expressions in our search plan.
227 or_squish(PLAN *plan)
229 PLAN *next; /* next node being processed */
230 PLAN *tail; /* pointer to tail of result plan */
231 PLAN *result; /* pointer to head of result plan */
233 tail = result = next = NULL;
235 while ((next = yanknode(&plan)) != NULL) {
237 * if we encounter a ( expression ) then look for or's in
240 if (next->execute == f_expr)
241 next->p_data[0] = or_squish(next->p_data[0]);
243 /* if we encounter a not then look for or's in the subplan */
244 if (next->execute == f_not)
245 next->p_data[0] = or_squish(next->p_data[0]);
248 * if we encounter an or, then place our collected plan in the
249 * or's first subplan and then recursively collect the
250 * remaining stuff into the second subplan and return the or.
252 if (next->execute == f_or) {
254 errx(1, "-o: no expression before -o");
255 next->p_data[0] = result;
256 next->p_data[1] = or_squish(plan);
257 if (next->p_data[1] == NULL)
258 errx(1, "-o: no expression after -o");
262 /* add the node to our result plan */
264 tail = result = next;