-/* $NetBSD: operator.c,v 1.6 1998/02/21 22:47:21 christos Exp $ */
-
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
#ifndef lint
#if 0
-static char sccsid[] = "from: @(#)operator.c 8.1 (Berkeley) 6/6/93";
-#else
-__RCSID("$NetBSD: operator.c,v 1.6 1998/02/21 22:47:21 christos Exp $");
+static char sccsid[] = "@(#)operator.c 8.1 (Berkeley) 6/6/93";
#endif
#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: src/usr.bin/find/operator.c,v 1.17 2010/12/11 08:32:16 joel Exp $");
+
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include "find.h"
-
-static PLAN *yanknode __P((PLAN **));
-static PLAN *yankexpr __P((PLAN **));
+
+static PLAN *yanknode(PLAN **);
+static PLAN *yankexpr(PLAN **);
/*
* yanknode --
* destructively removes the top from the plan
*/
static PLAN *
-yanknode(planp)
- PLAN **planp; /* pointer to top of plan (modified) */
+yanknode(PLAN **planp)
{
PLAN *node; /* top node removed from the plan */
-
+
if ((node = (*planp)) == NULL)
return (NULL);
(*planp) = (*planp)->next;
node->next = NULL;
return (node);
}
-
+
/*
* yankexpr --
* Removes one expression from the plan. This is used mainly by
* paren_squish. In comments below, an expression is either a
- * simple node or a N_EXPR node containing a list of simple nodes.
+ * simple node or a f_expr node containing a list of simple nodes.
*/
static PLAN *
-yankexpr(planp)
- PLAN **planp; /* pointer to top of plan (modified) */
+yankexpr(PLAN **planp)
{
PLAN *next; /* temp node holding subexpression results */
PLAN *node; /* pointer to returned node or expression */
PLAN *tail; /* pointer to tail of subplan */
PLAN *subplan; /* pointer to head of ( ) expression */
-
+
/* first pull the top node from the plan */
if ((node = yanknode(planp)) == NULL)
return (NULL);
-
+
/*
* If the node is an '(' then we recursively slurp up expressions
* until we find its associated ')'. If it's a closing paren we
* just return it and unwind our recursion; all other nodes are
* complete expressions, so just return them.
*/
- if (node->type == N_OPENPAREN)
+ if (node->execute == f_openparen)
for (tail = subplan = NULL;;) {
if ((next = yankexpr(planp)) == NULL)
- err(1, "(: missing closing ')'");
+ errx(1, "(: missing closing ')'");
/*
* If we find a closing ')' we store the collected
* subplan in our '(' node and convert the node to
- * a N_EXPR. The ')' we found is ignored. Otherwise,
+ * a f_expr. The ')' we found is ignored. Otherwise,
* we just continue to add whatever we get to our
* subplan.
*/
- if (next->type == N_CLOSEPAREN) {
+ if (next->execute == f_closeparen) {
if (subplan == NULL)
errx(1, "(): empty inner expression");
node->p_data[0] = subplan;
- node->type = N_EXPR;
- node->eval = f_expr;
+ node->execute = f_expr;
break;
} else {
if (subplan == NULL)
}
return (node);
}
-
+
/*
* paren_squish --
- * replaces "parentheisized" plans in our search plan with "expr" nodes.
+ * replaces "parenthesized" plans in our search plan with "expr" nodes.
*/
PLAN *
-paren_squish(plan)
- PLAN *plan; /* plan with ( ) nodes */
+paren_squish(PLAN *plan)
{
PLAN *expr; /* pointer to next expression */
PLAN *tail; /* pointer to tail of result plan */
PLAN *result; /* pointer to head of result plan */
-
+
result = tail = NULL;
/*
* the basic idea is to have yankexpr do all our work and just
- * collect it's results together.
+ * collect its results together.
*/
while ((expr = yankexpr(&plan)) != NULL) {
/*
* if we find an unclaimed ')' it means there is a missing
* '(' someplace.
*/
- if (expr->type == N_CLOSEPAREN)
+ if (expr->execute == f_closeparen)
errx(1, "): no beginning '('");
/* add the expression to our result plan */
}
return (result);
}
-
+
/*
* not_squish --
* compresses "!" expressions in our search plan.
*/
PLAN *
-not_squish(plan)
- PLAN *plan; /* plan to process */
+not_squish(PLAN *plan)
{
PLAN *next; /* next node being processed */
- PLAN *node; /* temporary node used in N_NOT processing */
+ PLAN *node; /* temporary node used in f_not processing */
PLAN *tail; /* pointer to tail of result plan */
PLAN *result; /* pointer to head of result plan */
-
- tail = result = next = NULL;
-
- while ((next = yanknode(&plan)) != NULL) {
+
+ tail = result = NULL;
+
+ while ((next = yanknode(&plan))) {
/*
* if we encounter a ( expression ) then look for nots in
* the expr subplan.
*/
- if (next->type == N_EXPR)
+ if (next->execute == f_expr)
next->p_data[0] = not_squish(next->p_data[0]);
/*
* it in the not's subplan. As an optimization we compress
* several not's to zero or one not.
*/
- if (next->type == N_NOT) {
+ if (next->execute == f_not) {
int notlevel = 1;
node = yanknode(&plan);
- while (node->type == N_NOT) {
+ while (node != NULL && node->execute == f_not) {
++notlevel;
node = yanknode(&plan);
}
if (node == NULL)
errx(1, "!: no following expression");
- if (node->type == N_OR)
+ if (node->execute == f_or)
errx(1, "!: nothing between ! and -o");
+ /*
+ * If we encounter ! ( expr ) then look for nots in
+ * the expr subplan.
+ */
+ if (node->execute == f_expr)
+ node->p_data[0] = not_squish(node->p_data[0]);
if (notlevel % 2 != 1)
next = node;
else
}
return (result);
}
-
+
/*
* or_squish --
* compresses -o expressions in our search plan.
*/
PLAN *
-or_squish(plan)
- PLAN *plan; /* plan with ors to be squished */
+or_squish(PLAN *plan)
{
PLAN *next; /* next node being processed */
PLAN *tail; /* pointer to tail of result plan */
PLAN *result; /* pointer to head of result plan */
-
+
tail = result = next = NULL;
-
+
while ((next = yanknode(&plan)) != NULL) {
/*
* if we encounter a ( expression ) then look for or's in
* the expr subplan.
*/
- if (next->type == N_EXPR)
+ if (next->execute == f_expr)
next->p_data[0] = or_squish(next->p_data[0]);
- /* if we encounter a not then look for not's in the subplan */
- if (next->type == N_NOT)
+ /* if we encounter a not then look for or's in the subplan */
+ if (next->execute == f_not)
next->p_data[0] = or_squish(next->p_data[0]);
/*
* or's first subplan and then recursively collect the
* remaining stuff into the second subplan and return the or.
*/
- if (next->type == N_OR) {
+ if (next->execute == f_or) {
if (result == NULL)
errx(1, "-o: no expression before -o");
next->p_data[0] = result;