1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Cimarron D. Taylor of the University of California, Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2000-06-12 11:12:41 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)operator.c 8.1 (Berkeley) 6/6/93";
|
2000-06-12 11:12:41 +00:00
|
|
|
#else
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD$";
|
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fts.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "find.h"
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2002-03-20 10:32:05 +00:00
|
|
|
static PLAN *yanknode(PLAN **);
|
|
|
|
static PLAN *yankexpr(PLAN **);
|
2002-02-27 17:57:00 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* yanknode --
|
|
|
|
* destructively removes the top from the plan
|
|
|
|
*/
|
|
|
|
static PLAN *
|
1995-05-30 06:41:30 +00:00
|
|
|
yanknode(planp)
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN **planp; /* pointer to top of plan (modified) */
|
|
|
|
{
|
|
|
|
PLAN *node; /* top node removed from the plan */
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((node = (*planp)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
(*planp) = (*planp)->next;
|
|
|
|
node->next = NULL;
|
|
|
|
return (node);
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* yankexpr --
|
|
|
|
* Removes one expression from the plan. This is used mainly by
|
|
|
|
* paren_squish. In comments below, an expression is either a
|
2001-05-03 18:05:35 +00:00
|
|
|
* simple node or a f_expr node containing a list of simple nodes.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
static PLAN *
|
1995-05-30 06:41:30 +00:00
|
|
|
yankexpr(planp)
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN **planp; /* pointer to top of plan (modified) */
|
|
|
|
{
|
2002-02-27 17:57:00 +00:00
|
|
|
PLAN *next; /* temp node holding subexpression results */
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN *node; /* pointer to returned node or expression */
|
|
|
|
PLAN *tail; /* pointer to tail of subplan */
|
|
|
|
PLAN *subplan; /* pointer to head of ( ) expression */
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* first pull the top node from the plan */
|
|
|
|
if ((node = yanknode(planp)) == NULL)
|
|
|
|
return (NULL);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (node->execute == f_openparen)
|
1994-05-27 12:33:43 +00:00
|
|
|
for (tail = subplan = NULL;;) {
|
|
|
|
if ((next = yankexpr(planp)) == NULL)
|
|
|
|
err(1, "(: missing closing ')'");
|
|
|
|
/*
|
|
|
|
* If we find a closing ')' we store the collected
|
|
|
|
* subplan in our '(' node and convert the node to
|
2001-05-03 18:05:35 +00:00
|
|
|
* a f_expr. The ')' we found is ignored. Otherwise,
|
1994-05-27 12:33:43 +00:00
|
|
|
* we just continue to add whatever we get to our
|
|
|
|
* subplan.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (next->execute == f_closeparen) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (subplan == NULL)
|
|
|
|
errx(1, "(): empty inner expression");
|
|
|
|
node->p_data[0] = subplan;
|
2001-05-03 18:05:35 +00:00
|
|
|
node->execute = f_expr;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (subplan == NULL)
|
|
|
|
tail = subplan = next;
|
|
|
|
else {
|
|
|
|
tail->next = next;
|
|
|
|
tail = next;
|
|
|
|
}
|
|
|
|
tail->next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (node);
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* paren_squish --
|
2002-03-26 12:05:35 +00:00
|
|
|
* replaces "parenthesized" plans in our search plan with "expr" nodes.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
PLAN *
|
|
|
|
paren_squish(plan)
|
|
|
|
PLAN *plan; /* plan with ( ) nodes */
|
|
|
|
{
|
2002-02-27 17:57:00 +00:00
|
|
|
PLAN *expr; /* pointer to next expression */
|
|
|
|
PLAN *tail; /* pointer to tail of result plan */
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN *result; /* pointer to head of result plan */
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
result = tail = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the basic idea is to have yankexpr do all our work and just
|
2001-05-03 18:05:35 +00:00
|
|
|
* collect its results together.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
while ((expr = yankexpr(&plan)) != NULL) {
|
|
|
|
/*
|
|
|
|
* if we find an unclaimed ')' it means there is a missing
|
|
|
|
* '(' someplace.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (expr->execute == f_closeparen)
|
1994-05-27 12:33:43 +00:00
|
|
|
errx(1, "): no beginning '('");
|
|
|
|
|
|
|
|
/* add the expression to our result plan */
|
|
|
|
if (result == NULL)
|
|
|
|
tail = result = expr;
|
|
|
|
else {
|
|
|
|
tail->next = expr;
|
|
|
|
tail = expr;
|
|
|
|
}
|
|
|
|
tail->next = NULL;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* not_squish --
|
|
|
|
* compresses "!" expressions in our search plan.
|
|
|
|
*/
|
|
|
|
PLAN *
|
|
|
|
not_squish(plan)
|
|
|
|
PLAN *plan; /* plan to process */
|
|
|
|
{
|
2002-02-27 17:57:00 +00:00
|
|
|
PLAN *next; /* next node being processed */
|
|
|
|
PLAN *node; /* temporary node used in f_not processing */
|
|
|
|
PLAN *tail; /* pointer to tail of result plan */
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN *result; /* pointer to head of result plan */
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-05-03 18:05:35 +00:00
|
|
|
tail = result = NULL;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-07-23 23:27:28 +00:00
|
|
|
while ((next = yanknode(&plan))) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* if we encounter a ( expression ) then look for nots in
|
|
|
|
* the expr subplan.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (next->execute == f_expr)
|
1994-05-27 12:33:43 +00:00
|
|
|
next->p_data[0] = not_squish(next->p_data[0]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we encounter a not, then snag the next node and place
|
|
|
|
* it in the not's subplan. As an optimization we compress
|
|
|
|
* several not's to zero or one not.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (next->execute == f_not) {
|
1994-05-27 12:33:43 +00:00
|
|
|
int notlevel = 1;
|
|
|
|
|
|
|
|
node = yanknode(&plan);
|
2001-05-03 18:05:35 +00:00
|
|
|
while (node != NULL && node->execute == f_not) {
|
1994-05-27 12:33:43 +00:00
|
|
|
++notlevel;
|
|
|
|
node = yanknode(&plan);
|
|
|
|
}
|
|
|
|
if (node == NULL)
|
|
|
|
errx(1, "!: no following expression");
|
2001-05-03 18:05:35 +00:00
|
|
|
if (node->execute == f_or)
|
1994-05-27 12:33:43 +00:00
|
|
|
errx(1, "!: nothing between ! and -o");
|
1997-03-10 19:20:39 +00:00
|
|
|
/*
|
|
|
|
* If we encounter ! ( expr ) then look for nots in
|
|
|
|
* the expr subplan.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (node->execute == f_expr)
|
1997-03-10 19:20:39 +00:00
|
|
|
node->p_data[0] = not_squish(node->p_data[0]);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (notlevel % 2 != 1)
|
|
|
|
next = node;
|
|
|
|
else
|
|
|
|
next->p_data[0] = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the node to our result plan */
|
|
|
|
if (result == NULL)
|
|
|
|
tail = result = next;
|
|
|
|
else {
|
|
|
|
tail->next = next;
|
|
|
|
tail = next;
|
|
|
|
}
|
|
|
|
tail->next = NULL;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* or_squish --
|
|
|
|
* compresses -o expressions in our search plan.
|
|
|
|
*/
|
|
|
|
PLAN *
|
|
|
|
or_squish(plan)
|
|
|
|
PLAN *plan; /* plan with ors to be squished */
|
|
|
|
{
|
2002-02-27 17:57:00 +00:00
|
|
|
PLAN *next; /* next node being processed */
|
|
|
|
PLAN *tail; /* pointer to tail of result plan */
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN *result; /* pointer to head of result plan */
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
tail = result = next = NULL;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
while ((next = yanknode(&plan)) != NULL) {
|
|
|
|
/*
|
|
|
|
* if we encounter a ( expression ) then look for or's in
|
|
|
|
* the expr subplan.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (next->execute == f_expr)
|
1994-05-27 12:33:43 +00:00
|
|
|
next->p_data[0] = or_squish(next->p_data[0]);
|
|
|
|
|
1997-03-10 19:20:39 +00:00
|
|
|
/* if we encounter a not then look for or's in the subplan */
|
2001-05-03 18:05:35 +00:00
|
|
|
if (next->execute == f_not)
|
1994-05-27 12:33:43 +00:00
|
|
|
next->p_data[0] = or_squish(next->p_data[0]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we encounter an or, then place our collected plan in the
|
|
|
|
* or's first subplan and then recursively collect the
|
|
|
|
* remaining stuff into the second subplan and return the or.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
if (next->execute == f_or) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (result == NULL)
|
|
|
|
errx(1, "-o: no expression before -o");
|
|
|
|
next->p_data[0] = result;
|
|
|
|
next->p_data[1] = or_squish(plan);
|
|
|
|
if (next->p_data[1] == NULL)
|
|
|
|
errx(1, "-o: no expression after -o");
|
|
|
|
return (next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the node to our result plan */
|
|
|
|
if (result == NULL)
|
|
|
|
tail = result = next;
|
|
|
|
else {
|
|
|
|
tail->next = next;
|
|
|
|
tail = next;
|
|
|
|
}
|
|
|
|
tail->next = NULL;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
}
|