Home | History | Annotate | Line # | Download | only in find
operator.c revision 1.8
      1 /*	$NetBSD: operator.c,v 1.8 2003/08/07 11:13:43 agc Exp $	*/
      2 
      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/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from: @(#)operator.c	8.1 (Berkeley) 6/6/93";
     39 #else
     40 __RCSID("$NetBSD: operator.c,v 1.8 2003/08/07 11:13:43 agc Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 
     46 #include <err.h>
     47 #include <fts.h>
     48 #include <stdio.h>
     49 
     50 #include "find.h"
     51 
     52 static PLAN *yanknode __P((PLAN **));
     53 static PLAN *yankexpr __P((PLAN **));
     54 
     55 /*
     56  * yanknode --
     57  *	destructively removes the top from the plan
     58  */
     59 static PLAN *
     60 yanknode(planp)
     61 	PLAN **planp;		/* pointer to top of plan (modified) */
     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 N_EXPR node containing a list of simple nodes.
     77  */
     78 static PLAN *
     79 yankexpr(planp)
     80 	PLAN **planp;		/* pointer to top of plan (modified) */
     81 {
     82 	PLAN *next;		/* temp node holding subexpression results */
     83 	PLAN *node;		/* pointer to returned node or expression */
     84 	PLAN *tail;		/* pointer to tail of subplan */
     85 	PLAN *subplan;		/* pointer to head of ( ) expression */
     86 
     87 	/* first pull the top node from the plan */
     88 	if ((node = yanknode(planp)) == NULL)
     89 		return (NULL);
     90 
     91 	/*
     92 	 * If the node is an '(' then we recursively slurp up expressions
     93 	 * until we find its associated ')'.  If it's a closing paren we
     94 	 * just return it and unwind our recursion; all other nodes are
     95 	 * complete expressions, so just return them.
     96 	 */
     97 	if (node->type == N_OPENPAREN)
     98 		for (tail = subplan = NULL;;) {
     99 			if ((next = yankexpr(planp)) == NULL)
    100 				err(1, "(: missing closing ')'");
    101 			/*
    102 			 * If we find a closing ')' we store the collected
    103 			 * subplan in our '(' node and convert the node to
    104 			 * a N_EXPR.  The ')' we found is ignored.  Otherwise,
    105 			 * we just continue to add whatever we get to our
    106 			 * subplan.
    107 			 */
    108 			if (next->type == N_CLOSEPAREN) {
    109 				if (subplan == NULL)
    110 					errx(1, "(): empty inner expression");
    111 				node->p_data[0] = subplan;
    112 				node->type = N_EXPR;
    113 				node->eval = f_expr;
    114 				break;
    115 			} else {
    116 				if (subplan == NULL)
    117 					tail = subplan = next;
    118 				else {
    119 					tail->next = next;
    120 					tail = next;
    121 				}
    122 				tail->next = NULL;
    123 			}
    124 		}
    125 	return (node);
    126 }
    127 
    128 /*
    129  * paren_squish --
    130  *	replaces "parentheisized" plans in our search plan with "expr" nodes.
    131  */
    132 PLAN *
    133 paren_squish(plan)
    134 	PLAN *plan;		/* plan with ( ) nodes */
    135 {
    136 	PLAN *expr;		/* pointer to next expression */
    137 	PLAN *tail;		/* pointer to tail of result plan */
    138 	PLAN *result;		/* pointer to head of result plan */
    139 
    140 	result = tail = NULL;
    141 
    142 	/*
    143 	 * the basic idea is to have yankexpr do all our work and just
    144 	 * collect it's results together.
    145 	 */
    146 	while ((expr = yankexpr(&plan)) != NULL) {
    147 		/*
    148 		 * if we find an unclaimed ')' it means there is a missing
    149 		 * '(' someplace.
    150 		 */
    151 		if (expr->type == N_CLOSEPAREN)
    152 			errx(1, "): no beginning '('");
    153 
    154 		/* add the expression to our result plan */
    155 		if (result == NULL)
    156 			tail = result = expr;
    157 		else {
    158 			tail->next = expr;
    159 			tail = expr;
    160 		}
    161 		tail->next = NULL;
    162 	}
    163 	return (result);
    164 }
    165 
    166 /*
    167  * not_squish --
    168  *	compresses "!" expressions in our search plan.
    169  */
    170 PLAN *
    171 not_squish(plan)
    172 	PLAN *plan;		/* plan to process */
    173 {
    174 	PLAN *next;		/* next node being processed */
    175 	PLAN *node;		/* temporary node used in N_NOT processing */
    176 	PLAN *tail;		/* pointer to tail of result plan */
    177 	PLAN *result;		/* pointer to head of result plan */
    178 
    179 	tail = result = next = NULL;
    180 
    181 	while ((next = yanknode(&plan)) != NULL) {
    182 		/*
    183 		 * if we encounter a ( expression ) then look for nots in
    184 		 * the expr subplan.
    185 		 */
    186 		if (next->type == N_EXPR)
    187 			next->p_data[0] = not_squish(next->p_data[0]);
    188 
    189 		/*
    190 		 * if we encounter a not, then snag the next node and place
    191 		 * it in the not's subplan.  As an optimization we compress
    192 		 * several not's to zero or one not.
    193 		 */
    194 		if (next->type == N_NOT) {
    195 			int notlevel = 1;
    196 
    197 			node = yanknode(&plan);
    198 			while (node != NULL && node->type == N_NOT) {
    199 				++notlevel;
    200 				node = yanknode(&plan);
    201 			}
    202 			if (node == NULL)
    203 				errx(1, "!: no following expression");
    204 			if (node->type == N_OR)
    205 				errx(1, "!: nothing between ! and -o");
    206 			if (node->type == N_EXPR)
    207 				node = not_squish(node);
    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)
    232 	PLAN *plan;		/* plan with ors to be squished */
    233 {
    234 	PLAN *next;		/* next node being processed */
    235 	PLAN *tail;		/* pointer to tail of result plan */
    236 	PLAN *result;		/* pointer to head of result plan */
    237 
    238 	tail = result = next = NULL;
    239 
    240 	while ((next = yanknode(&plan)) != NULL) {
    241 		/*
    242 		 * if we encounter a ( expression ) then look for or's in
    243 		 * the expr subplan.
    244 		 */
    245 		if (next->type == N_EXPR)
    246 			next->p_data[0] = or_squish(next->p_data[0]);
    247 
    248 		/* if we encounter a not then look for not's in the subplan */
    249 		if (next->type == N_NOT)
    250 			next->p_data[0] = or_squish(next->p_data[0]);
    251 
    252 		/*
    253 		 * if we encounter an or, then place our collected plan in the
    254 		 * or's first subplan and then recursively collect the
    255 		 * remaining stuff into the second subplan and return the or.
    256 		 */
    257 		if (next->type == N_OR) {
    258 			if (result == NULL)
    259 				errx(1, "-o: no expression before -o");
    260 			next->p_data[0] = result;
    261 			next->p_data[1] = or_squish(plan);
    262 			if (next->p_data[1] == NULL)
    263 				errx(1, "-o: no expression after -o");
    264 			return (next);
    265 		}
    266 
    267 		/* add the node to our result plan */
    268 		if (result == NULL)
    269 			tail = result = next;
    270 		else {
    271 			tail->next = next;
    272 			tail = next;
    273 		}
    274 		tail->next = NULL;
    275 	}
    276 	return (result);
    277 }
    278