1 /* tree.c -- helper functions to build and evaluate the expression tree.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 #include "../gnulib/lib/xalloc.h"
25 # define _(Text) gettext (Text)
30 # define N_(String) gettext_noop (String)
32 /* See locate.c for explanation as to why not use (String) */
33 # define N_(String) String
36 static struct predicate
*scan_rest
PARAMS((struct predicate
**input
,
37 struct predicate
*head
,
38 short int prev_prec
));
39 static void merge_pred
PARAMS((struct predicate
*beg_list
, struct predicate
*end_list
, struct predicate
**last_p
));
40 static struct predicate
*set_new_parent
PARAMS((struct predicate
*curr
, enum predicate_precedence high_prec
, struct predicate
**prevp
));
42 /* Return a pointer to a tree that represents the
43 expression prior to non-unary operator *INPUT.
44 Set *INPUT to point at the next input predicate node.
46 Only accepts the following:
49 expression [operators of higher precedence]
51 (arbitrary expression)
52 <uni_op>(arbitrary expression)
54 In other words, you can not start out with a bi_op or close_paren.
56 If the following operator (if any) is of a higher precedence than
57 PREV_PREC, the expression just nabbed is part of a following
58 expression, which really is the expression that should be handed to
59 our caller, so get_expr recurses. */
62 get_expr (struct predicate
**input
, short int prev_prec
)
64 struct predicate
*next
= NULL
;
67 error (1, 0, _("invalid expression"));
69 switch ((*input
)->p_type
)
72 error (1, 0, _("invalid expression"));
76 error (1, 0, _("invalid expression; you have used a binary operator with nothing before it."));
80 error (1, 0, _("invalid expression; you have too many ')'"));
85 *input
= (*input
)->pred_next
;
90 *input
= (*input
)->pred_next
;
91 next
->pred_right
= get_expr (input
, NEGATE_PREC
);
95 *input
= (*input
)->pred_next
;
96 next
= get_expr (input
, NO_PREC
);
98 || ((*input
)->p_type
!= CLOSE_PAREN
))
99 error (1, 0, _("invalid expression; I was expecting to find a ')' somewhere but did not see one."));
100 *input
= (*input
)->pred_next
; /* move over close */
104 error (1, 0, _("oops -- invalid expression type!"));
108 /* We now have the first expression and are positioned to check
109 out the next operator. If NULL, all done. Otherwise, if
110 PREV_PREC < the current node precedence, we must continue;
111 the expression we just nabbed is more tightly bound to the
112 following expression than to the previous one. */
115 if ((int) (*input
)->p_prec
> (int) prev_prec
)
117 next
= scan_rest (input
, next
, prev_prec
);
119 error (1, 0, _("invalid expression"));
124 /* Scan across the remainder of a predicate input list starting
125 at *INPUT, building the rest of the expression tree to return.
126 Stop at the first close parenthesis or the end of the input list.
127 Assumes that get_expr has been called to nab the first element
128 of the expression tree.
130 *INPUT points to the current input predicate list element.
131 It is updated as we move along the list to point to the
132 terminating input element.
133 HEAD points to the predicate element that was obtained
134 by the call to get_expr.
135 PREV_PREC is the precedence of the previous predicate element. */
137 static struct predicate
*
138 scan_rest (struct predicate
**input
,
139 struct predicate
*head
,
142 struct predicate
*tree
; /* The new tree we are building. */
144 if ((*input
== NULL
) || ((*input
)->p_type
== CLOSE_PAREN
))
147 while ((*input
!= NULL
) && ((int) (*input
)->p_prec
> (int) prev_prec
))
149 switch ((*input
)->p_type
)
155 /* I'm not sure how we get here, so it is not obvious what
156 * sort of mistakes might give rise to this condition.
158 error (1, 0, _("invalid expression"));
162 (*input
)->pred_left
= tree
;
164 *input
= (*input
)->pred_next
;
165 tree
->pred_right
= get_expr (input
, tree
->p_prec
);
173 _("oops -- invalid expression type (%d)!"),
174 (int)(*input
)->p_type
);
181 /* Optimize the ordering of the predicates in the tree. Rearrange
182 them to minimize work. Strategies:
183 * Evaluate predicates that don't need inode information first;
184 the predicates are divided into 1 or more groups separated by
185 predicates (if any) which have "side effects", such as printing.
186 The grouping implements the partial ordering on predicates which
187 those with side effects impose.
189 * Place -name, -iname, -path, -ipath, -regex and -iregex at the front
190 of a group, with -name, -iname, -path and -ipath ahead of
191 -regex and -iregex. Predicates which are moved to the front
192 of a group by definition do not have side effects. Both
193 -regex and -iregex both use pred_regex.
195 This routine "normalizes" the predicate tree by ensuring that
196 all expression predicates have AND (or OR or COMMA) parent nodes
197 which are linked along the left edge of the expression tree.
198 This makes manipulation of subtrees easier.
200 EVAL_TREEP points to the root pointer of the predicate tree
201 to be rearranged. opt_expr may return a new root pointer there.
202 Return true if the tree contains side effects, false if not. */
205 opt_expr (struct predicate
**eval_treep
)
207 /* List of -name and -path predicates to move. */
208 struct predicate
*name_list
= NULL
;
209 struct predicate
*end_name_list
= NULL
;
210 /* List of -regex predicates to move. */
211 struct predicate
*regex_list
= NULL
;
212 struct predicate
*end_regex_list
= NULL
;
213 struct predicate
*curr
;
214 struct predicate
**prevp
; /* Address of `curr' node. */
215 struct predicate
**last_sidep
; /* Last predicate with side effects. */
217 enum predicate_type p_type
;
218 boolean has_side_effects
= false; /* Return value. */
219 enum predicate_precedence prev_prec
, /* precedence of last BI_OP in branch */
220 biop_prec
; /* topmost BI_OP precedence in branch */
223 if (eval_treep
== NULL
|| *eval_treep
== NULL
)
226 /* Set up to normalize tree as a left-linked list of ANDs or ORs.
227 Set `curr' to the leftmost node, `prevp' to its address, and
228 `pred_func' to the predicate type of its parent. */
230 prev_prec
= AND_PREC
;
232 while (curr
->pred_left
!= NULL
)
234 prevp
= &curr
->pred_left
;
235 prev_prec
= curr
->p_prec
; /* must be a BI_OP */
236 curr
= curr
->pred_left
;
239 /* Link in the appropriate BI_OP for the last expression, if needed. */
240 if (curr
->p_type
!= BI_OP
)
241 set_new_parent (curr
, prev_prec
, prevp
);
244 /* Normalized tree. */
245 fprintf (stderr
, "Normalized Eval Tree:\n");
246 print_tree (stderr
, *eval_treep
, 0);
249 /* Rearrange the predicates. */
251 biop_prec
= NO_PREC
; /* not COMMA_PREC */
252 if ((*prevp
) && (*prevp
)->p_type
== BI_OP
)
253 biop_prec
= (*prevp
)->p_prec
;
254 while ((curr
= *prevp
) != NULL
)
256 /* If there is a BI_OP of different precedence from the first
257 in the pred_left chain, create a new parent of the
258 original precedence, link the new parent to the left of the
259 previous and link CURR to the right of the new parent.
260 This preserves the precedence of expressions in the tree
261 in case we rearrange them. */
262 if (curr
->p_type
== BI_OP
)
264 if (curr
->p_prec
!= biop_prec
)
265 curr
= set_new_parent(curr
, biop_prec
, prevp
);
268 /* See which predicate type we have. */
269 p_type
= curr
->pred_right
->p_type
;
270 pred_func
= curr
->pred_right
->pred_func
;
276 /* Don't rearrange the arguments of the comma operator, it is
278 if (biop_prec
== COMMA_PREC
)
281 /* If it's one of our special primaries, move it to the
282 front of the list for that primary. */
283 if (pred_func
== pred_name
|| pred_func
== pred_path
||
284 pred_func
== pred_iname
|| pred_func
== pred_ipath
)
286 *prevp
= curr
->pred_left
;
287 curr
->pred_left
= name_list
;
290 if (end_name_list
== NULL
)
291 end_name_list
= curr
;
296 if (pred_func
== pred_regex
)
298 *prevp
= curr
->pred_left
;
299 curr
->pred_left
= regex_list
;
302 if (end_regex_list
== NULL
)
303 end_regex_list
= curr
;
311 /* For NOT, check the expression trees below the NOT. */
312 curr
->pred_right
->side_effects
313 = opt_expr (&curr
->pred_right
->pred_right
);
317 /* For nested AND or OR, recurse (AND/OR form layers on the left of
318 the tree), and continue scanning this level of AND or OR. */
319 curr
->pred_right
->side_effects
= opt_expr (&curr
->pred_right
);
322 /* At this point, get_expr and scan_rest have already removed
323 all of the user's parentheses. */
326 error (1, 0, _("oops -- invalid expression type!"));
330 if (curr
->pred_right
->side_effects
== true)
334 /* Incorporate lists and reset list pointers for this group. */
335 if (name_list
!= NULL
)
337 merge_pred (name_list
, end_name_list
, last_sidep
);
338 name_list
= end_name_list
= NULL
;
341 if (regex_list
!= NULL
)
343 merge_pred (regex_list
, end_regex_list
, last_sidep
);
344 regex_list
= end_regex_list
= NULL
;
347 has_side_effects
= true;
350 prevp
= &curr
->pred_left
;
353 /* Do final list merges. */
355 if (name_list
!= NULL
)
356 merge_pred (name_list
, end_name_list
, last_sidep
);
357 if (regex_list
!= NULL
)
358 merge_pred (regex_list
, end_regex_list
, last_sidep
);
360 return (has_side_effects
);
363 /* Link in a new parent BI_OP node for CURR, at *PREVP, with precedence
366 static struct predicate
*
367 set_new_parent (struct predicate
*curr
, enum predicate_precedence high_prec
, struct predicate
**prevp
)
369 struct predicate
*new_parent
;
371 new_parent
= (struct predicate
*) xmalloc (sizeof (struct predicate
));
372 new_parent
->p_type
= BI_OP
;
373 new_parent
->p_prec
= high_prec
;
374 new_parent
->need_stat
= false;
375 new_parent
->need_type
= false;
380 new_parent
->pred_func
= pred_comma
;
383 new_parent
->pred_func
= pred_or
;
386 new_parent
->pred_func
= pred_and
;
392 new_parent
->side_effects
= false;
393 new_parent
->no_default_print
= false;
394 new_parent
->args
.str
= NULL
;
395 new_parent
->pred_next
= NULL
;
397 /* Link in new_parent.
398 Pushes rest of left branch down 1 level to new_parent->pred_right. */
399 new_parent
->pred_left
= NULL
;
400 new_parent
->pred_right
= curr
;
404 new_parent
->p_name
= (char *) find_pred_name (new_parent
->pred_func
);
410 /* Merge the predicate list that starts at BEG_LIST and ends at END_LIST
411 into the tree at LAST_P. */
414 merge_pred (struct predicate
*beg_list
, struct predicate
*end_list
, struct predicate
**last_p
)
416 end_list
->pred_left
= *last_p
;
420 /* Find the first node in expression tree TREE that requires
421 a stat call and mark the operator above it as needing a stat
422 before calling the node. Since the expression precedences
423 are represented in the tree, some preds that need stat may not
424 get executed (because the expression value is determined earlier.)
425 So every expression needing stat must be marked as such, not just
426 the earliest, to be sure to obtain the stat. This still guarantees
427 that a stat is made as late as possible. Return true if the top node
428 in TREE requires a stat, false if not. */
431 mark_stat (struct predicate
*tree
)
433 /* The tree is executed in-order, so walk this way (apologies to Aerosmith)
434 to find the first predicate for which the stat is needed. */
435 switch (tree
->p_type
)
439 return tree
->need_stat
;
442 if (mark_stat (tree
->pred_right
))
443 tree
->need_stat
= true;
447 /* ANDs and ORs are linked along ->left ending in NULL. */
448 if (tree
->pred_left
!= NULL
)
449 mark_stat (tree
->pred_left
);
451 if (mark_stat (tree
->pred_right
))
452 tree
->need_stat
= true;
457 error (1, 0, _("oops -- invalid expression type in mark_stat!"));
462 /* Find the first node in expression tree TREE that we will
463 need to know the file type, if any. Operates in the same
467 mark_type (struct predicate
*tree
)
469 /* The tree is executed in-order, so walk this way (apologies to Aerosmith)
470 to find the first predicate for which the type information is needed. */
471 switch (tree
->p_type
)
475 return tree
->need_type
;
478 if (mark_type (tree
->pred_right
))
479 tree
->need_type
= true;
483 /* ANDs and ORs are linked along ->left ending in NULL. */
484 if (tree
->pred_left
!= NULL
)
485 mark_type (tree
->pred_left
);
487 if (mark_type (tree
->pred_right
))
488 tree
->need_type
= true;
493 error (1, 0, _("oops -- invalid expression type in mark_type!"));