1 /* tree.c -- helper functions to build and evaluate the expression tree.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000, 2003, 2004, 2005,
3 2006, 2007, 2010, 2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* config.h must always come first. */
37 # define _(Text) gettext (Text)
42 # define N_(String) gettext_noop (String)
44 /* See locate.c for explanation as to why not use (String) */
45 # define N_(String) String
50 /* All predicates for each path to process. */
51 static struct predicate *predicates = NULL;
53 /* The root of the evaluation tree. */
54 static struct predicate *eval_tree = NULL;
56 /* The last predicate allocated. */
57 static struct predicate *last_pred = NULL;
59 /* The starting points. */
60 static char **start_points;
61 static size_t num_start_points = 0;
65 static struct predicate *scan_rest (struct predicate **input,
66 struct predicate *head,
68 static void merge_pred (struct predicate *beg_list, struct predicate *end_list, struct predicate **last_p);
69 static struct predicate *set_new_parent (struct predicate *curr, enum predicate_precedence high_prec, struct predicate **prevp);
70 static const char *cost_name (enum EvaluationCost cost);
73 /* Return true if the indicated path name is a start
74 point or not. If no start points were given on the
75 command line, we return true for ".".
78 matches_start_point (const char *glob, bool foldcase)
80 int fnmatch_flags = 0;
82 fnmatch_flags |= FNM_CASEFOLD;
87 for (i=0; i<num_start_points; i++)
89 if (fnmatch (glob, start_points[i], fnmatch_flags) == 0)
96 return fnmatch (glob, ".", fnmatch_flags) == 0;
101 /* Return a pointer to a tree that represents the
102 expression prior to non-unary operator *INPUT.
103 Set *INPUT to point at the next input predicate node.
105 Only accepts the following:
108 expression [operators of higher precedence]
110 (arbitrary expression)
111 <uni_op>(arbitrary expression)
113 In other words, you cannot start out with a bi_op or close_paren.
115 If the following operator (if any) is of a higher precedence than
116 PREV_PREC, the expression just nabbed is part of a following
117 expression, which really is the expression that should be handed to
118 our caller, so get_expr recurses. */
120 static struct predicate *
121 get_expr (struct predicate **input,
123 const struct predicate* prev_pred)
125 struct predicate *next = NULL;
126 struct predicate *this_pred = (*input);
129 error (EXIT_FAILURE, 0, _("invalid expression"));
131 switch ((*input)->p_type)
134 error (EXIT_FAILURE, 0, _("invalid expression"));
138 /* e.g. "find . -a" */
139 error (EXIT_FAILURE, 0,
140 _("invalid expression; you have used a binary operator '%s' with nothing before it."),
145 if ((UNI_OP == prev_pred->p_type
146 || BI_OP == prev_pred->p_type)
147 && !this_pred->artificial)
149 /* e.g. "find \( -not \)" or "find \( -true -a \" */
150 error (EXIT_FAILURE, 0,
151 _("expected an expression between '%s' and ')'"),
154 else if ( (*input)->artificial )
156 /* We have reached the end of the user-supplied predicates
159 /* e.g. "find . -true -a" */
160 error (EXIT_FAILURE, 0,
161 _("expected an expression after '%s'"), prev_pred->p_name);
165 error (EXIT_FAILURE, 0,
166 _("invalid expression; you have too many ')'"));
172 *input = (*input)->pred_next;
177 *input = (*input)->pred_next;
178 next->pred_right = get_expr (input, NEGATE_PREC, next);
182 if ( (NULL == (*input)->pred_next) || (*input)->pred_next->artificial )
184 /* user typed something like "find . (", and so the ) we are
185 * looking at is from the artificial "( ) -print" that we
188 error (EXIT_FAILURE, 0,
189 _("invalid expression; expected to find a ')' but didn't see one. Perhaps you need an extra predicate after '%s'"),
192 prev_pred = (*input);
193 *input = (*input)->pred_next;
194 if ( (*input)->p_type == CLOSE_PAREN )
196 error (EXIT_FAILURE, 0,
197 _("invalid expression; empty parentheses are not allowed."));
199 next = get_expr (input, NO_PREC, prev_pred);
201 || ((*input)->p_type != CLOSE_PAREN))
202 error (EXIT_FAILURE, 0,
203 _("invalid expression; I was expecting to find a ')' somewhere but did not see one."));
205 *input = (*input)->pred_next; /* move over close */
209 error (EXIT_FAILURE, 0, _("oops -- invalid expression type!"));
213 /* We now have the first expression and are positioned to check
214 out the next operator. If NULL, all done. Otherwise, if
215 PREV_PREC < the current node precedence, we must continue;
216 the expression we just nabbed is more tightly bound to the
217 following expression than to the previous one. */
220 if ((int) (*input)->p_prec > (int) prev_prec)
222 next = scan_rest (input, next, prev_prec);
224 error (EXIT_FAILURE, 0, _("invalid expression"));
229 /* Scan across the remainder of a predicate input list starting
230 at *INPUT, building the rest of the expression tree to return.
231 Stop at the first close parenthesis or the end of the input list.
232 Assumes that get_expr has been called to nab the first element
233 of the expression tree.
235 *INPUT points to the current input predicate list element.
236 It is updated as we move along the list to point to the
237 terminating input element.
238 HEAD points to the predicate element that was obtained
239 by the call to get_expr.
240 PREV_PREC is the precedence of the previous predicate element. */
242 static struct predicate *
243 scan_rest (struct predicate **input,
244 struct predicate *head,
247 struct predicate *tree; /* The new tree we are building. */
249 if ((*input == NULL) || ((*input)->p_type == CLOSE_PAREN))
252 while ((*input != NULL) && ((int) (*input)->p_prec > (int) prev_prec))
254 switch ((*input)->p_type)
260 /* I'm not sure how we get here, so it is not obvious what
261 * sort of mistakes might give rise to this condition.
263 error (EXIT_FAILURE, 0, _("invalid expression"));
268 struct predicate *prev = (*input);
269 (*input)->pred_left = tree;
271 *input = (*input)->pred_next;
272 tree->pred_right = get_expr (input, tree->p_prec, prev);
280 error (EXIT_FAILURE, 0,
281 _("oops -- invalid expression type (%d)!"),
282 (int)(*input)->p_type);
289 /* Returns true if the specified predicate is reorderable. */
291 predicate_is_cost_free (const struct predicate *p)
293 if (pred_is(p, pred_name) ||
294 pred_is(p, pred_path) ||
295 pred_is(p, pred_iname) ||
296 pred_is(p, pred_ipath))
298 /* Traditionally (at least 4.1.7 through 4.2.x) GNU find always
299 * optimised these cases.
303 else if (options.optimisation_level > 0)
305 if (pred_is(p, pred_and) ||
306 pred_is(p, pred_negate) ||
307 pred_is(p, pred_comma) ||
311 return NeedsNothing == p->p_cost;
319 /* Prints a predicate */
320 void print_predicate (FILE *fp, const struct predicate *p)
324 fprintf (fp, "%s %s", p->p_name, p->arg_text);
328 fprintf (fp, "%s", p->p_name);
335 struct predicate *head;
336 struct predicate *tail;
340 predlist_init (struct predlist *p)
342 p->head = p->tail = NULL;
346 predlist_insert (struct predlist *list,
347 struct predicate *curr,
348 struct predicate **pprev)
350 struct predicate **insertpos = &(list->head);
352 *pprev = curr->pred_left;
353 curr->pred_left = (*insertpos);
355 if (NULL == list->tail)
356 list->tail = list->head;
360 pred_cost_compare (const struct predicate *p1, const struct predicate *p2, bool wantfailure)
362 if (p1->p_cost == p2->p_cost)
364 if (p1->est_success_rate == p2->est_success_rate)
366 else if (wantfailure)
367 return p1->est_success_rate < p2->est_success_rate ? -1 : 1;
369 return p1->est_success_rate < p2->est_success_rate ? 1 : -1;
373 return p1->p_cost < p2->p_cost ? -1 : 1;
379 predlist_merge_sort (struct predlist *list,
380 struct predicate **last)
382 struct predlist new_list;
383 struct predicate *p, *q;
385 if (NULL == list->head)
386 return; /* nothing to do */
388 if (options.debug_options & DebugTreeOpt)
390 fprintf (stderr, "%s:\n", "predlist before merge sort");
391 print_tree (stderr, list->head, 2);
394 calculate_derived_rates (list->head);
395 predlist_init (&new_list);
398 /* remove head of source list */
400 list->head = list->head->pred_left;
403 /* insert it into the new list */
404 for (p=new_list.head; p; p=p->pred_left)
406 /* If these operations are OR operations, we want to get a
407 * successful test as soon as possible, to take advantage of
408 * the short-circuit evaluation. If they're AND, we want to
409 * get an unsuccessful result early for the same reason.
410 * Therefore we invert the sense of the comparison for the
411 * OR case. We only want to invert the sense of the success
412 * rate comparison, not the operation cost comparison. Hence we
413 * pass a flag into pred_cost_compare().
415 const bool wantfailure = (OR_PREC != p->p_prec);
416 if (pred_cost_compare (p->pred_right, q->pred_right, wantfailure) >= 0)
421 /* insert into existing list */
422 q->pred_left = p->pred_left;
423 if (NULL == q->pred_left)
429 q->pred_left = new_list.head; /* prepend */
431 if (NULL == new_list.tail)
432 new_list.tail = q; /* first item in new list */
435 if (options.debug_options & DebugTreeOpt)
437 fprintf (stderr, "%s:\n", "predlist after merge sort");
438 print_tree (stderr, new_list.head, 2);
441 calculate_derived_rates(new_list.head);
442 merge_pred (new_list.head, new_list.tail, last);
443 predlist_init (list);
447 merge_lists (struct predlist lists[], int nlists,
448 struct predlist *name_list,
449 struct predlist *regex_list,
450 struct predicate **last)
453 static void (*mergefn)(struct predlist *, struct predicate**);
455 mergefn = predlist_merge_sort;
457 mergefn (name_list, last);
458 mergefn (regex_list, last);
460 for (i=0; i<nlists; i++)
461 mergefn (&lists[i], last);
467 subtree_has_side_effects (const struct predicate *p)
471 return p->side_effects
472 || subtree_has_side_effects (p->pred_left)
473 || subtree_has_side_effects (p->pred_right);
483 worst_cost (const struct predicate *p)
487 unsigned int cost_r, cost_l, worst;
488 cost_l = worst_cost (p->pred_left);
489 cost_r = worst_cost (p->pred_right);
490 worst = (cost_l > cost_r) ? cost_l : cost_r;
491 if (worst < p->p_cost)
504 perform_arm_swap (struct predicate *p)
506 struct predicate *tmp = p->pred_left->pred_right;
507 p->pred_left->pred_right = p->pred_right;
511 /* Consider swapping p->pred_left->pred_right with p->pred_right,
512 * if that yields a faster evaluation. Normally the left predicate is
515 * If the operation is an OR, we want the left predicate to be the one that
516 * succeeds most often. If it is an AND, we want it to be the predicate that
519 * We don't consider swapping arms of an operator where their cost is
520 * different or where they have side effects.
522 * A viable test case for this is
523 * ./find -D opt -O3 . \! -type f -o -type d
524 * Here, the ! -type f should be evaluated first,
525 * as we assume that 95% of inodes are vanilla files.
528 consider_arm_swap (struct predicate *p)
530 int left_cost, right_cost;
531 const char *reason = NULL;
532 struct predicate **pl, **pr;
534 if (BI_OP != p->p_type)
535 reason = "Not a binary operation";
539 if (NULL == p->pred_left || NULL == p->pred_right)
540 reason = "Doesn't have two arms";
546 if (NULL == p->pred_left->pred_right)
547 reason = "Left arm has no child on RHS";
550 pl = &p->pred_left->pred_right;
554 if (subtree_has_side_effects (*pl))
555 reason = "Left subtree has side-effects";
559 if (subtree_has_side_effects (*pr))
560 reason = "Right subtree has side-effects";
565 left_cost = worst_cost (*pl);
566 right_cost = worst_cost (*pr);
568 if (left_cost < right_cost)
570 reason = "efficient as-is";
577 if (left_cost == right_cost)
579 /* it's a candidate */
580 float succ_rate_l = (*pl)->est_success_rate;
581 float succ_rate_r = (*pr)->est_success_rate;
583 if (options.debug_options & DebugTreeOpt)
585 fprintf (stderr, "Success rates: l=%f, r=%f\n", succ_rate_l, succ_rate_r);
588 if (pred_is (p, pred_or))
590 want_swap = succ_rate_r < succ_rate_l;
592 reason = "Operation is OR; right success rate >= left";
594 else if (pred_is (p, pred_and))
596 want_swap = succ_rate_r > succ_rate_l;
598 reason = "Operation is AND; right success rate <= left";
603 reason = "Not 'AND' or 'OR'";
613 if (options.debug_options & DebugTreeOpt)
615 fprintf (stderr, "Performing arm swap on:\n");
616 print_tree (stderr, p, 0);
618 perform_arm_swap (p);
624 if (options.debug_options & DebugTreeOpt)
626 fprintf (stderr, "Not an arm swap candidate (%s):\n", reason);
627 print_tree (stderr, p, 0);
633 do_arm_swaps (struct predicate *p)
641 if (consider_arm_swap (p)
642 || do_arm_swaps (p->pred_left)
643 || do_arm_swaps (p->pred_right))
658 /* Optimize the ordering of the predicates in the tree. Rearrange
659 them to minimize work. Strategies:
660 * Evaluate predicates that don't need inode information first;
661 the predicates are divided into 1 or more groups separated by
662 predicates (if any) which have "side effects", such as printing.
663 The grouping implements the partial ordering on predicates which
664 those with side effects impose.
666 * Place -name, -iname, -path, -ipath, -regex and -iregex at the front
667 of a group, with -name, -iname, -path and -ipath ahead of
668 -regex and -iregex. Predicates which are moved to the front
669 of a group by definition do not have side effects. Both
670 -regex and -iregex both use pred_regex.
672 If higher optimisation levels have been selected, reordering also
673 occurs according to the p_cost member of each predicate (which
674 reflects the performance cost of the test). The ordering also
675 bears in mind whether these operations are more likely to succeed
676 or fail. When evauating a chain of OR conditions, we prefer
677 tests likely to succeed at the front of the list. For AND, we
678 prefer tests likely to fail at the front of the list.
680 This routine "normalizes" the predicate tree by ensuring that all
681 expression predicates have 'AND' (or 'OR' or 'COMMA') parent
682 nodes which are linked along the left edge of the expression
683 tree. This makes manipulation of subtrees easier.
685 EVAL_TREEP points to the root pointer of the predicate tree
686 to be rearranged. opt_expr may return a new root pointer there.
687 Return true if the tree contains side effects, false if not. */
690 opt_expr (struct predicate **eval_treep)
692 struct predlist regex_list={NULL,NULL}, name_list={NULL,NULL};
693 struct predlist cbo_list[NumEvaluationCosts];
695 struct predicate *curr;
696 struct predicate **prevp; /* Address of `curr' node. */
697 struct predicate **last_sidep; /* Last predicate with side effects. */
699 enum predicate_type p_type;
700 bool has_side_effects = false; /* Return value. */
701 enum predicate_precedence prev_prec, /* precedence of last BI_OP in branch */
702 biop_prec; /* topmost BI_OP precedence in branch */
704 if (eval_treep == NULL || *eval_treep == NULL)
707 for (i=0; i<NumEvaluationCosts; i++)
708 predlist_init (&cbo_list[i]);
710 /* Set up to normalize tree as a left-linked list of ANDs or ORs.
711 Set `curr' to the leftmost node, `prevp' to its address, and
712 `pred_func' to the predicate type of its parent. */
714 prev_prec = AND_PREC;
716 while (curr->pred_left != NULL)
718 prevp = &curr->pred_left;
719 prev_prec = curr->p_prec; /* must be a BI_OP */
720 curr = curr->pred_left;
723 /* Link in the appropriate BI_OP for the last expression, if needed. */
724 if (curr->p_type != BI_OP)
725 set_new_parent (curr, prev_prec, prevp);
727 if (options.debug_options & (DebugExpressionTree|DebugTreeOpt))
729 /* Normalized tree. */
730 fprintf (stderr, "Normalized Eval Tree:\n");
731 print_tree (stderr, *eval_treep, 0);
734 /* Rearrange the predicates. */
736 biop_prec = NO_PREC; /* not COMMA_PREC */
737 if ((*prevp) && (*prevp)->p_type == BI_OP)
738 biop_prec = (*prevp)->p_prec;
739 while ((curr = *prevp) != NULL)
741 /* If there is a BI_OP of different precedence from the first
742 in the pred_left chain, create a new parent of the
743 original precedence, link the new parent to the left of the
744 previous and link CURR to the right of the new parent.
745 This preserves the precedence of expressions in the tree
746 in case we rearrange them. */
747 if (curr->p_type == BI_OP)
749 if (curr->p_prec != biop_prec)
750 curr = set_new_parent (curr, biop_prec, prevp);
753 /* See which predicate type we have. */
754 p_type = curr->pred_right->p_type;
755 pred_func = curr->pred_right->pred_func;
762 /* Don't rearrange the arguments of the comma operator, it is
764 if (biop_prec == COMMA_PREC)
767 /* If this predicate has no side effects, consider reordering it. */
768 if (!curr->pred_right->side_effects)
772 /* If it's one of our special primaries, move it to the
773 front of the list for that primary. */
774 if (predicate_is_cost_free (curr->pred_right))
776 if (options.debug_options & DebugTreeOpt)
778 fprintf (stderr, "-O%d: promoting cheap predicate ",
779 (int)options.optimisation_level);
780 print_predicate (stderr, curr->pred_right);
781 fprintf (stderr, " into name_list\n");
783 predlist_insert (&name_list, curr, prevp);
787 if (pred_func == pred_regex)
789 predlist_insert (®ex_list, curr, prevp);
793 reorder = ((options.optimisation_level > 1)
794 && (NeedsType == curr->pred_right->p_cost
795 || NeedsInodeNumber == curr->pred_right->p_cost)
796 && !curr->pred_right->need_stat) ||
797 (options.optimisation_level > 2);
801 if (options.debug_options & DebugTreeOpt)
803 fprintf (stderr, "-O%d: categorising predicate ",
804 (int)options.optimisation_level);
805 print_predicate (stderr, curr->pred_right);
806 fprintf (stderr, " by cost (%s)\n",
807 cost_name(curr->pred_right->p_cost));
809 predlist_insert (&cbo_list[curr->pred_right->p_cost], curr, prevp);
817 /* For NOT, check the expression trees below the NOT. */
818 curr->pred_right->side_effects
819 = opt_expr (&curr->pred_right->pred_right);
823 /* For nested 'AND' or 'OR', recurse (AND/OR form layers on
824 the left of the tree), and continue scanning this level
826 curr->pred_right->side_effects = opt_expr (&curr->pred_right);
829 /* At this point, get_expr and scan_rest have already removed
830 all of the user's parentheses. */
833 error (EXIT_FAILURE, 0, _("oops -- invalid expression type!"));
837 if (curr->pred_right->side_effects == true)
841 /* Incorporate lists and reset list pointers for this group. */
842 merge_lists (cbo_list, NumEvaluationCosts, &name_list, ®ex_list, last_sidep);
843 has_side_effects = true;
846 prevp = &curr->pred_left;
849 /* Do final list merges. */
851 merge_lists (cbo_list, NumEvaluationCosts, &name_list, ®ex_list, last_sidep);
852 return has_side_effects;
856 constrain_rate (float rate)
866 /* Link in a new parent BI_OP node for CURR, at *PREVP, with precedence
869 static struct predicate *
870 set_new_parent (struct predicate *curr, enum predicate_precedence high_prec, struct predicate **prevp)
872 struct predicate *new_parent;
874 new_parent = xmalloc (sizeof (struct predicate));
875 new_parent->p_type = BI_OP;
876 new_parent->p_prec = high_prec;
877 new_parent->need_stat = false;
878 new_parent->need_type = false;
879 new_parent->need_inum = false;
880 new_parent->p_cost = NeedsNothing;
881 new_parent->arg_text = NULL;
886 new_parent->pred_func = pred_comma;
887 new_parent->p_name = ",";
888 new_parent->est_success_rate = 1.0;
891 new_parent->pred_func = pred_or;
892 new_parent->p_name = "-o";
893 new_parent->est_success_rate = constrain_rate (curr->est_success_rate);
896 new_parent->pred_func = pred_and;
897 new_parent->p_name = "-a";
898 new_parent->est_success_rate = constrain_rate (curr->est_success_rate);
904 new_parent->side_effects = false;
905 new_parent->no_default_print = false;
906 new_parent->args.str = NULL;
907 new_parent->pred_next = NULL;
909 /* Link in new_parent.
910 Pushes rest of left branch down 1 level to new_parent->pred_right. */
911 new_parent->pred_left = NULL;
912 new_parent->pred_right = curr;
918 /* Merge the predicate list that starts at BEG_LIST and ends at END_LIST
919 into the tree at LAST_P. */
922 merge_pred (struct predicate *beg_list, struct predicate *end_list, struct predicate **last_p)
924 end_list->pred_left = *last_p;
928 /* Find the first node in expression tree TREE that requires
929 a stat call and mark the operator above it as needing a stat
930 before calling the node. Since the expression precedences
931 are represented in the tree, some preds that need stat may not
932 get executed (because the expression value is determined earlier.)
933 So every expression needing stat must be marked as such, not just
934 the earliest, to be sure to obtain the stat. This still guarantees
935 that a stat is made as late as possible. Return true if the top node
936 in TREE requires a stat, false if not. */
939 struct pred_cost_lookup
942 enum EvaluationCost cost;
944 static struct pred_cost_lookup costlookup[] =
946 { pred_amin , NeedsStatInfo },
947 { pred_and , NeedsNothing, },
948 { pred_anewer , NeedsStatInfo, },
949 { pred_atime , NeedsStatInfo, },
950 { pred_closeparen, NeedsNothing },
951 { pred_cmin , NeedsStatInfo, },
952 { pred_cnewer , NeedsStatInfo, },
953 { pred_comma , NeedsNothing, },
954 { pred_context , NeedsAccessInfo },
955 { pred_ctime , NeedsStatInfo, },
956 { pred_delete , NeedsSyncDiskHit },
957 { pred_empty , NeedsStatInfo },
958 { pred_exec , NeedsEventualExec },
959 { pred_execdir , NeedsEventualExec },
960 { pred_executable, NeedsAccessInfo },
961 { pred_false , NeedsNothing },
962 { pred_fprint , NeedsNothing },
963 { pred_fprint0 , NeedsNothing },
964 { pred_fprintf , NeedsNothing },
965 { pred_fstype , NeedsStatInfo }, /* true for amortised cost */
966 { pred_gid , NeedsStatInfo },
967 { pred_group , NeedsStatInfo },
968 { pred_ilname , NeedsLinkName },
969 { pred_iname , NeedsNothing },
970 { pred_inum , NeedsInodeNumber },
971 { pred_ipath , NeedsNothing },
972 { pred_links , NeedsStatInfo },
973 { pred_lname , NeedsLinkName },
974 { pred_ls , NeedsStatInfo },
975 { pred_fls , NeedsStatInfo },
976 { pred_mmin , NeedsStatInfo },
977 { pred_mtime , NeedsStatInfo },
978 { pred_name , NeedsNothing },
979 { pred_negate , NeedsNothing, },
980 { pred_newer , NeedsStatInfo, },
981 { pred_newerXY , NeedsStatInfo, },
982 { pred_nogroup , NeedsStatInfo }, /* true for amortised cost if caching is on */
983 { pred_nouser , NeedsStatInfo }, /* true for amortised cost if caching is on */
984 { pred_ok , NeedsUserInteraction },
985 { pred_okdir , NeedsUserInteraction },
986 { pred_openparen , NeedsNothing },
987 { pred_or , NeedsNothing, },
988 { pred_path , NeedsNothing },
989 { pred_perm , NeedsStatInfo },
990 { pred_print , NeedsNothing },
991 { pred_print0 , NeedsNothing },
992 { pred_prune , NeedsNothing },
993 { pred_quit , NeedsNothing },
994 { pred_readable , NeedsAccessInfo },
995 { pred_regex , NeedsNothing },
996 { pred_samefile , NeedsStatInfo },
997 { pred_size , NeedsStatInfo },
998 { pred_true , NeedsNothing },
999 { pred_type , NeedsType },
1000 { pred_uid , NeedsStatInfo },
1001 { pred_used , NeedsStatInfo },
1002 { pred_user , NeedsStatInfo },
1003 { pred_writable , NeedsAccessInfo },
1004 { pred_xtype , NeedsType } /* roughly correct unless most files are symlinks */
1006 static int pred_table_sorted = 0;
1009 check_sorted (void *base, size_t members, size_t membersize,
1010 int (*cmpfn)(const void*, const void*))
1012 const char *p = base;
1014 for (i=1u; i<members; ++i)
1016 int result = cmpfn (p+i*membersize, p+(i-1)*membersize);
1019 result = cmpfn (p+(i-1)*membersize, p+i*membersize);
1020 assert (result <= 0);
1027 cost_table_comparison (const void *p1, const void *p2)
1029 /* We have to compare the function pointers with memcmp(),
1030 * because ISO C does not allow magnitude comparison of
1031 * function pointers (just equality testing).
1033 const struct pred_cost_lookup *pc1 = p1;
1034 const struct pred_cost_lookup *pc2 = p2;
1037 char mem[sizeof (PRED_FUNC)];
1042 return memcmp (u1.mem, u2.mem, sizeof(u1.pfn));
1045 static enum EvaluationCost
1046 get_pred_cost (const struct predicate *p)
1048 enum EvaluationCost data_requirement_cost = NeedsNothing;
1049 enum EvaluationCost inherent_cost = NeedsUnknown;
1053 data_requirement_cost = NeedsStatInfo;
1055 else if (p->need_inum)
1057 data_requirement_cost = NeedsInodeNumber;
1059 else if (p->need_type)
1061 data_requirement_cost = NeedsType;
1065 data_requirement_cost = NeedsNothing;
1068 if (pred_is (p, pred_exec) || pred_is(p, pred_execdir))
1070 if (p->args.exec_vec.multiple)
1071 inherent_cost = NeedsEventualExec;
1073 inherent_cost = NeedsImmediateExec;
1075 else if (pred_is (p, pred_fprintf))
1077 /* the parser calculated the cost for us. */
1078 inherent_cost = p->p_cost;
1082 struct pred_cost_lookup key;
1085 if (!pred_table_sorted)
1088 sizeof(costlookup)/sizeof(costlookup[0]),
1089 sizeof(costlookup[0]),
1090 cost_table_comparison);
1092 if (!check_sorted (costlookup,
1093 sizeof(costlookup)/sizeof(costlookup[0]),
1094 sizeof(costlookup[0]),
1095 cost_table_comparison))
1097 error (EXIT_FAILURE, 0,
1098 "failed to sort the costlookup array");
1100 pred_table_sorted = 1;
1102 key.fn = p->pred_func;
1103 entry = bsearch (&key, costlookup,
1104 sizeof(costlookup)/sizeof(costlookup[0]),
1105 sizeof(costlookup[0]),
1106 cost_table_comparison);
1109 inherent_cost = ((const struct pred_cost_lookup*)entry)->cost;
1113 /* This message indicates a bug. If we issue the message, we
1114 actually have two bugs: if find emits a diagnostic, its result
1115 should be nonzero. However, not having an entry for a predicate
1116 will not affect the output (just the performance) so I think it
1117 would be confusing to exit with a nonzero status.
1120 _("warning: there is no entry in the predicate evaluation "
1121 "cost table for predicate %s; please report this as a bug"),
1123 inherent_cost = NeedsUnknown;
1127 if (inherent_cost > data_requirement_cost)
1128 return inherent_cost;
1130 return data_requirement_cost;
1134 estimate_costs (struct predicate *tree)
1138 estimate_costs (tree->pred_right);
1139 estimate_costs (tree->pred_left);
1141 tree->p_cost = get_pred_cost(tree);
1146 get_eval_tree (void)
1152 getrate (const struct predicate *p)
1155 return p->est_success_rate;
1162 calculate_derived_rates (struct predicate *p)
1167 calculate_derived_rates (p->pred_right);
1169 calculate_derived_rates (p->pred_left);
1171 assert (p->p_type != CLOSE_PAREN);
1172 assert (p->p_type != OPEN_PAREN);
1177 assert (NULL == p->pred_right);
1178 assert (NULL == p->pred_left);
1179 return p->est_success_rate;
1182 assert (NULL == p->pred_right);
1183 assert (NULL == p->pred_left);
1184 return p->est_success_rate;
1187 /* Unary operators must have exactly one operand */
1188 assert (pred_is (p, pred_negate));
1189 assert (NULL == p->pred_left);
1190 p->est_success_rate = (1.0 - p->pred_right->est_success_rate);
1191 return p->est_success_rate;
1196 /* Binary operators must have two operands */
1197 if (pred_is (p, pred_and))
1199 rate = getrate (p->pred_right) * getrate(p->pred_left);
1201 else if (pred_is (p, pred_comma))
1205 else if (pred_is (p, pred_or))
1207 rate = getrate (p->pred_right) + getrate(p->pred_left);
1211 /* only and, or and comma are BI_OP. */
1215 p->est_success_rate = constrain_rate (rate);
1217 return p->est_success_rate;
1221 p->est_success_rate = 1.0;
1222 return p->est_success_rate;
1228 /* opt_expr() rearranges predicates such that each left subtree is
1229 * rooted at a logical predicate (e.g. '-a' or '-o').
1230 * check_normalization() asserts that this property still holds.
1234 check_normalization (struct predicate *p, bool at_root)
1238 assert (BI_OP == p->p_type);
1243 assert (BI_OP == p->pred_left->p_type);
1244 check_normalization(p->pred_left, false);
1248 check_normalization (p->pred_right, false);
1253 build_expression_tree (int argc, char *argv[], int end_of_leading_options)
1255 const struct parser_table *parse_entry; /* Pointer to the parsing table entry for this expression. */
1256 char *predicate_name; /* Name of predicate being parsed. */
1257 struct predicate *cur_pred;
1258 const struct parser_table *entry_close, *entry_print, *entry_open;
1263 /* Find where in ARGV the predicates begin by skipping the list of
1264 * start points. As a side effect, also figure out which is the
1265 * first and last start point.
1267 start_points = argv + end_of_leading_options;
1268 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i], true); i++)
1273 /* Enclose the expression in `( ... )' so a default -print will
1274 apply to the whole expression. */
1275 entry_open = find_parser ("(");
1276 entry_close = find_parser (")");
1277 entry_print = find_parser ("print");
1278 assert (entry_open != NULL);
1279 assert (entry_close != NULL);
1280 assert (entry_print != NULL);
1282 parse_openparen (entry_open, argv, &argc);
1283 last_pred->p_name = "(";
1284 predicates->artificial = true;
1285 parse_begin_user_args (argv, argc, last_pred, predicates);
1286 pred_sanity_check (last_pred);
1288 /* Build the input order list. */
1291 state.already_issued_stat_error_msg = false;
1292 if (!looks_like_expression (argv[i], false))
1294 error (0, 0, _("paths must precede expression: %s"), argv[i]);
1295 usage (stderr, 1, NULL);
1298 predicate_name = argv[i];
1299 parse_entry = find_parser (predicate_name);
1300 if (parse_entry == NULL)
1302 /* Command line option not recognized */
1303 error (EXIT_FAILURE, 0, _("unknown predicate `%s'"), predicate_name);
1306 /* We have recognised a test of the form -foo. Eat that,
1307 * unless it is a predicate like -newerXY.
1309 if (parse_entry->type != ARG_SPECIAL_PARSE)
1314 if (!(*(parse_entry->parser_func)) (parse_entry, argv, &i))
1318 if ( (ARG_SPECIAL_PARSE == parse_entry->type) && (i == oldi) )
1320 /* The special parse function spat out the
1321 * predicate. It must be invalid, or not tasty.
1323 error (EXIT_FAILURE, 0, _("invalid predicate `%s'"),
1328 error (EXIT_FAILURE, 0, _("invalid argument `%s' to `%s'"),
1329 argv[i], predicate_name);
1334 /* Command line option requires an argument */
1335 error (EXIT_FAILURE, 0,
1336 _("missing argument to `%s'"), predicate_name);
1341 last_pred->p_name = predicate_name;
1343 /* If the parser consumed an argument, save it. */
1345 last_pred->arg_text = argv[oldi];
1347 last_pred->arg_text = NULL;
1349 pred_sanity_check(last_pred);
1350 pred_sanity_check(predicates); /* XXX: expensive */
1352 parse_end_user_args (argv, argc, last_pred, predicates);
1353 if (predicates->pred_next == NULL)
1355 /* No predicates that do something other than set a global variable
1356 were given; remove the unneeded initial `(' and add `-print'. */
1357 cur_pred = predicates;
1358 predicates = last_pred = predicates->pred_next;
1360 parse_print (entry_print, argv, &argc);
1361 last_pred->p_name = "-print";
1362 pred_sanity_check(last_pred);
1363 pred_sanity_check(predicates); /* XXX: expensive */
1365 else if (!default_prints (predicates->pred_next))
1367 /* One or more predicates that produce output were given;
1368 remove the unneeded initial `('. */
1369 cur_pred = predicates;
1370 predicates = predicates->pred_next;
1371 pred_sanity_check (predicates); /* XXX: expensive */
1376 /* `( user-supplied-expression ) -print'. */
1377 parse_closeparen (entry_close, argv, &argc);
1378 last_pred->p_name = ")";
1379 last_pred->artificial = true;
1380 pred_sanity_check (last_pred);
1381 parse_print (entry_print, argv, &argc);
1382 last_pred->p_name = "-print";
1383 last_pred->artificial = true;
1384 pred_sanity_check (last_pred);
1385 pred_sanity_check (predicates); /* XXX: expensive */
1388 if (options.debug_options & (DebugExpressionTree|DebugTreeOpt))
1390 fprintf (stderr, "Predicate List:\n");
1391 print_list (stderr, predicates);
1394 /* do a sanity check */
1395 check_option_combinations (predicates);
1396 pred_sanity_check (predicates);
1398 /* Done parsing the predicates. Build the evaluation tree. */
1399 cur_pred = predicates;
1400 eval_tree = get_expr (&cur_pred, NO_PREC, NULL);
1401 calculate_derived_rates (eval_tree);
1403 /* Check if we have any left-over predicates (this fixes
1404 * Debian bug #185202).
1406 if (cur_pred != NULL)
1408 /* cur_pred->p_name is often NULL here */
1409 if (pred_is (cur_pred, pred_closeparen))
1411 /* e.g. "find \( -true \) \)" */
1412 error (EXIT_FAILURE, 0, _("you have too many ')'"));
1416 if (cur_pred->p_name)
1417 error (EXIT_FAILURE, 0,
1418 _("unexpected extra predicate '%s'"), cur_pred->p_name);
1420 error (EXIT_FAILURE, 0, _("unexpected extra predicate"));
1424 if (options.debug_options & (DebugExpressionTree|DebugTreeOpt))
1426 fprintf (stderr, "Eval Tree:\n");
1427 print_tree (stderr, eval_tree, 0);
1430 estimate_costs (eval_tree);
1432 /* Rearrange the eval tree in optimal-predicate order. */
1433 opt_expr (&eval_tree);
1435 /* Check that the tree is in normalised order (opt_expr does this) */
1436 check_normalization (eval_tree, true);
1438 do_arm_swaps (eval_tree);
1440 /* Check that the tree is still in normalised order */
1441 check_normalization (eval_tree, true);
1443 if (options.debug_options & (DebugExpressionTree|DebugTreeOpt))
1445 fprintf (stderr, "Optimized Eval Tree:\n");
1446 print_tree (stderr, eval_tree, 0);
1447 fprintf (stderr, "Optimized command line:\n");
1448 print_optlist (stderr, eval_tree);
1449 fprintf (stderr, "\n");
1455 /* Initialize the performance data for a predicate.
1458 init_pred_perf (struct predicate *pred)
1460 struct predicate_performance_info *p = &pred->perf;
1461 p->visits = p->successes = 0;
1466 get_new_pred_noarg (const struct parser_table *entry)
1468 struct predicate *p = get_new_pred (entry);
1477 /* Return a pointer to a new predicate structure, which has been
1478 linked in as the last one in the predicates list.
1480 Set `predicates' to point to the start of the predicates list.
1481 Set `last_pred' to point to the new last predicate in the list.
1483 Set all cells in the new structure to the default values. */
1486 get_new_pred (const struct parser_table *entry)
1488 register struct predicate *new_pred;
1491 /* Options should not be turned into predicates. */
1492 assert (entry->type != ARG_OPTION);
1493 assert (entry->type != ARG_POSITIONAL_OPTION);
1495 if (predicates == NULL)
1497 predicates = (struct predicate *)
1498 xmalloc (sizeof (struct predicate));
1499 last_pred = predicates;
1503 new_pred = xmalloc (sizeof (struct predicate));
1504 last_pred->pred_next = new_pred;
1505 last_pred = new_pred;
1507 last_pred->parser_entry = entry;
1508 last_pred->pred_func = NULL;
1509 last_pred->p_name = NULL;
1510 last_pred->p_type = NO_TYPE;
1511 last_pred->p_prec = NO_PREC;
1512 last_pred->side_effects = false;
1513 last_pred->no_default_print = false;
1514 last_pred->need_stat = true;
1515 last_pred->need_type = true;
1516 last_pred->need_inum = false;
1517 last_pred->p_cost = NeedsUnknown;
1518 last_pred->arg_text = "ThisShouldBeSetToSomethingElse";
1519 last_pred->args.str = NULL;
1520 last_pred->args.scontext = NULL;
1521 last_pred->pred_next = NULL;
1522 last_pred->pred_left = NULL;
1523 last_pred->pred_right = NULL;
1524 last_pred->literal_control_chars = options.literal_control_chars;
1525 last_pred->artificial = false;
1526 last_pred->est_success_rate = 1.0;
1527 init_pred_perf (last_pred);
1531 /* Return a pointer to a new predicate, with operator check.
1532 Like get_new_pred, but it checks to make sure that the previous
1533 predicate is an operator. If it isn't, the AND operator is inserted. */
1536 get_new_pred_chk_op (const struct parser_table *entry,
1539 struct predicate *new_pred;
1540 static const struct parser_table *entry_and = NULL;
1542 /* Locate the entry in the parser table for the "and" operator */
1543 if (NULL == entry_and)
1544 entry_and = find_parser ("and");
1546 /* Check that it's actually there. If not, that is a bug.*/
1547 assert (entry_and != NULL);
1550 switch (last_pred->p_type)
1553 error (EXIT_FAILURE, 0, _("oops -- invalid default insertion of and!"));
1558 /* We need to interpose the and operator. */
1559 new_pred = get_new_pred_noarg (entry_and);
1560 new_pred->pred_func = pred_and;
1561 new_pred->p_name = "-a";
1562 new_pred->p_type = BI_OP;
1563 new_pred->p_prec = AND_PREC;
1564 new_pred->need_stat = false;
1565 new_pred->need_type = false;
1566 new_pred->need_inum = false;
1567 new_pred->arg_text = NULL;
1568 new_pred->args.str = NULL;
1569 new_pred->side_effects = false;
1570 new_pred->no_default_print = false;
1577 new_pred = get_new_pred (entry);
1578 new_pred->arg_text = arg;
1579 new_pred->parser_entry = entry;
1585 enum EvaluationCost cost;
1588 struct cost_assoc cost_table[] =
1590 { NeedsNothing, "Nothing" },
1591 { NeedsInodeNumber, "InodeNumber" },
1592 { NeedsType, "Type" },
1593 { NeedsStatInfo, "StatInfo" },
1594 { NeedsLinkName, "LinkName" },
1595 { NeedsAccessInfo, "AccessInfo" },
1596 { NeedsSyncDiskHit, "SyncDiskHit" },
1597 { NeedsEventualExec, "EventualExec" },
1598 { NeedsImmediateExec, "ImmediateExec" },
1599 { NeedsUserInteraction, "UserInteraction" },
1600 { NeedsUnknown, "Unknown" }
1606 const char *prec_name;
1609 static struct prec_assoc prec_table[] =
1612 {COMMA_PREC, "comma"},
1615 {NEGATE_PREC, "negate"},
1623 const char *type_name;
1626 static struct op_assoc type_table[] =
1629 {PRIMARY_TYPE, "primary"},
1632 {OPEN_PAREN, "open_paren "},
1633 {CLOSE_PAREN, "close_paren "},
1638 cost_name (enum EvaluationCost cost)
1641 unsigned int n = sizeof (cost_table)/sizeof(cost_table[0]);
1643 for (i = 0; i<n; ++i)
1644 if (cost_table[i].cost == cost)
1645 return cost_table[i].name;
1651 type_name (short type)
1655 for (i = 0; type_table[i].type != (short) -1; i++)
1656 if (type_table[i].type == type)
1658 return type_table[i].type_name;
1662 prec_name (short prec)
1666 for (i = 0; prec_table[i].prec != (short) -1; i++)
1667 if (prec_table[i].prec == prec)
1669 return prec_table[i].prec_name;
1673 /* Walk the expression tree NODE to stdout.
1674 INDENT is the number of levels to indent the left margin. */
1677 print_tree (FILE *fp, struct predicate *node, int indent)
1683 for (i = 0; i < indent; i++)
1685 fprintf (fp, "pred=[");
1686 print_predicate (fp, node);
1687 fprintf (fp, "] type=%s prec=%s",
1688 type_name (node->p_type), prec_name (node->p_prec));
1689 fprintf (fp, " cost=%s rate=%#03.2g %sside effects ",
1690 cost_name (node->p_cost),
1691 node->est_success_rate,
1692 (node->side_effects ? "" : "no "));
1694 if (node->need_stat || node->need_type || node->need_inum)
1698 fprintf (fp, "Needs ");
1699 if (node->need_stat)
1701 fprintf (fp, "stat");
1704 if (node->need_inum)
1706 fprintf (fp, "%sinode", comma ? "," : "");
1709 if (node->need_type)
1711 fprintf (fp, "%stype", comma ? "," : "");
1717 for (i = 0; i < indent; i++)
1719 if (NULL == node->pred_left && NULL == node->pred_right)
1721 fprintf (fp, "no children.\n");
1725 if (node->pred_left)
1727 fprintf (fp, "left:\n");
1728 print_tree (fp, node->pred_left, indent + 1);
1732 fprintf (fp, "no left.\n");
1735 for (i = 0; i < indent; i++)
1737 if (node->pred_right)
1739 fprintf (fp, "right:\n");
1740 print_tree (fp, node->pred_right, indent + 1);
1744 fprintf (fp, "no right.\n");