Where I left off last night, FYI. Still not quite to the point it does eerything the other one does, but I think I've got the ! and ( ) behavior and structure for the option parsing right. Filling in the rest of the tests is less of a problem.
Attaching it here in case I drop my netbook or something... Rob
/* find.c - A hello world program. (Simple template for new commands.) * * Copyright 2014 Rob Landley <[email protected]> * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.c * Our "unspecified" behavior for no paths is to use "." * Parentheses can only stack 4096 deep USE_FIND(NEWTOY(find, "^HL", TOYFLAG_USR|TOYFLAG_BIN)) config FIND bool "find" default n help usage: find [-HL] [DIR...] [<options>] Search directories to display matching files. (Default is to search "." and match all.) -H Follow command line symlinks -L Follow all symlinks Match filters: -name <pattern> filename (with wildcards) -iname <pattern> case insensitive filename -type [bcdflps] type (block, char, dir, file, symlink, pipe, socket) -mtime <days> match last modified N days ago (24 hour period) Combine matches with: !, -a, -o, ( ) not, and, or, group expressions Numbers N may be: N exactly N -N less than N +N greater than N Actions: -exec -print -print0 */ // find . ! \( -name blah -print \) // find . -o #define FOR_find #include "toys.h" GLOBALS( char **filter; struct double_list *argdata; ) // Return numeric value with explicit sign static long itsasign(char *s, char *sign) { if (*s == '+' || *s == '-') *sign = *(s++); else if (!isdigit(*s)) error_exit("%s not +-N", s); return atolx(s); } static void do_print(struct dirtree *new, char c) { char *s=dirtree_path(new, 0); xprintf("%s%c", s, c); free(s); } // pending issues: // old false -a ! new false does not yield true. // // -user -group -newer evaluate once and save result (where?) // add -print if no action (-exec, -ok, -print) // Call this with static int do_find(struct dirtree *new) { int pcount = 0, print = 0, not = 0, active = !!new, test = active; char *s, **ss; if (active && new->parent && !dirtree_notdotdot(new)) return 0; // pcount: parentheses stack depth (using toybuf bytes, 4096 max depth) // test: result of most recent test // active: if 0 don't perform tests // not: a pending ! applies to this test (only set if performing tests) // print: saw one of print/ok/exec, no need for default -print if (TT.filter) for (ss = TT.filter; *ss; ss++) { int check = active && test; s = *ss; // handle ! ( ) using toybuf as a stack if (*s != '-') { if (s[1]) goto error; if (*s == '!') { // Don't invert if we're not making a decision if (check) not = !not; // Save old "not" and "active" on toybuf stack. // Deactivate this parenthetical if !test // Note: test value should never change while !active } else if (*s == '(') { if (pcount == sizeof(toybuf)) goto error; toybuf[pcount++] = not+(active<<1); if (!check) active = 0; // Pop status, apply deferred not to test } else if (*s == ')') { if (--pcount < 0) goto error; // Pop active state, apply deferred not (which was only set if checking) active = (toybuf[pcount]>>1)&1; if (active && (toybuf[pcount]&1)) test = !test; not = 0; } else goto error; continue; } else s++; if (!strcmp(s, "o") || !strcmp(s, "or")) { if (not) goto error; if (active) { if (!test) test = 1; else active = 0; // decision has been made until next ")" } // Mostly ignore NOP argument } else if (!strcmp(s, "a") || !strcmp(s, "and")) { if (not) goto error; } else if (!strcmp(s, "print") || !strcmp("print0", s)) { print++; if (check) do_print(new, s[6] ? 0 : '\n'); } else if (!strcmp(s, "nouser")) { } else if (!strcmp(s, "nogroup")) { } else if (!strcmp(s, "xdev")) { } else if (!strcmp(s, "prune")) { } else if (!strcmp(s, "depth")) { // Remaining filters take an argument } else { // -path -perm -type -links -user // -group -size -atime -ctime -mtime -newer -depth // -nouser -nogroup -xdev -prune -depth if (!strcmp(s, "name") || !strcmp(s, "iname")) { if (check) { // if (s[i] == 'i') { // if (!new) { // } else { // name = xstrdup(name); // while ( test = !fnmatch(ss[1], new->name, 0); } } else if (!strcmp(s, "exec") || !strcmp("ok", s)) { print++; if (check) error_exit("implement exec/ok"); } else goto error; // This test can go at the end because we do a syntax checking // pass first. Putting it here gets the error message (-unknown // vs -known noarg) right. if (!*++ss) error_exit("'%s' needs 1 arg", --s); } // Apply pending "!" to result if (active && not) test = !test; not = 0; } // If there was no action, print if (!print && test && new) do_print(new, '\n'); return DIRTREE_RECURSE|((toys.optflags&FLAG_L) ? DIRTREE_SYMFOLLOW : 0); error: error_exit("bad arg '%s'", *ss); } void find_main(void) { int i, len; char **ss = toys.optargs; // Distinguish paths from filters for (len = 0; toys.optargs[len]; len++) if (strchr("-!(", *toys.optargs[len])) break; TT.filter = toys.optargs+len; // use "." if no paths if (!*ss) { ss = (char *[]){"."}; len = 1; } // Collect filter data do_find(0); // Loop through paths for (i = 0; i < len; i++) { struct dirtree *new; new = dirtree_add_node(0, ss[i], toys.optflags&(FLAG_H|FLAG_L)); if (new) dirtree_handle_callback(new, do_find); } }
_______________________________________________ Toybox mailing list [email protected] http://lists.landley.net/listinfo.cgi/toybox-landley.net
