/*- * Copyright (c) 1991, 1993, 1994 * 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. * * %sccs.include.redist.c% */ #ifndef lint static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 08/05/94"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include "find.h" /* * find_formplan -- * process the command line and create a "plan" corresponding to the * command arguments. */ PLAN * find_formplan(argv) char **argv; { PLAN *plan, *tail, *new; /* * for each argument in the command line, determine what kind of node * it is, create the appropriate node type and add the new plan node * to the end of the existing plan. The resulting plan is a linked * list of plan nodes. For example, the string: * * % find . -name foo -newer bar -print * * results in the plan: * * [-name foo]--> [-newer bar]--> [-print] * * in this diagram, `[-name foo]' represents the plan node generated * by c_name() with an argument of foo and `-->' represents the * plan->next pointer. */ for (plan = tail = NULL; *argv;) { if (!(new = find_create(&argv))) continue; if (plan == NULL) tail = plan = new; else { tail->next = new; tail = new; } } /* * if the user didn't specify one of -print, -ok or -exec, then -print * is assumed so we bracket the current expression with parens, if * necessary, and add a -print node on the end. */ if (!isoutput) { if (plan == NULL) { new = c_print(); tail = plan = new; } else { new = c_openparen(); new->next = plan; plan = new; new = c_closeparen(); tail->next = new; tail = new; new = c_print(); tail->next = new; tail = new; } } /* * the command line has been completely processed into a search plan * except for the (, ), !, and -o operators. Rearrange the plan so * that the portions of the plan which are affected by the operators * are moved into operator nodes themselves. For example: * * [!]--> [-name foo]--> [-print] * * becomes * * [! [-name foo] ]--> [-print] * * and * * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] * * becomes * * [expr [-depth]-->[-name foo] ]--> [-print] * * operators are handled in order of precedence. */ plan = paren_squish(plan); /* ()'s */ plan = not_squish(plan); /* !'s */ plan = or_squish(plan); /* -o's */ return (plan); } FTS *tree; /* pointer to top of FTS hierarchy */ /* * find_execute -- * take a search plan and an array of search paths and executes the plan * over all FTSENT's returned for the given search paths. */ int find_execute(plan, paths) PLAN *plan; /* search plan */ char **paths; /* array of pathnames to traverse */ { register FTSENT *entry; PLAN *p; int rval; if ((tree = fts_open(paths, ftsoptions, (int (*)())NULL)) == NULL) err(1, "ftsopen"); for (rval = 0; (entry = fts_read(tree)) != NULL;) { switch (entry->fts_info) { case FTS_D: if (isdepth) continue; break; case FTS_DP: if (!isdepth) continue; break; case FTS_DNR: case FTS_ERR: case FTS_NS: (void)fflush(stdout); warnx("%s: %s", entry->fts_path, strerror(entry->fts_errno)); rval = 1; continue; #ifdef FTS_W case FTS_W: continue; #endif /* FTS_W */ } #define BADCH " \t\n\\'\"" if (isxargs && strpbrk(entry->fts_path, BADCH)) { (void)fflush(stdout); warnx("%s: illegal path", entry->fts_path); rval = 1; continue; } /* * Call all the functions in the execution plan until one is * false or all have been executed. This is where we do all * the work specified by the user on the command line. */ for (p = plan; p && (p->eval)(p, entry); p = p->next); } if (errno) err(1, "fts_read"); return (rval); }