1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Cimarron D. Taylor of the University of California, Berkeley. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)option.c 5.8 (Berkeley) 06/04/91"; 13 #endif /* not lint */ 14 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #include <fts.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include "find.h" 22 23 typedef struct _option { 24 char *name; /* option name */ 25 enum ntype token; /* token type */ 26 PLAN *(*create)(); /* create function */ 27 #define O_NONE 0x01 /* no call required */ 28 #define O_ZERO 0x02 /* pass: nothing */ 29 #define O_ARGV 0x04 /* pass: argv, increment argv */ 30 #define O_ARGVP 0x08 /* pass: *argv, N_OK || N_EXEC */ 31 int flags; 32 } OPTION; 33 34 OPTION options[] = { 35 "!", N_NOT, c_not, O_ZERO, 36 "(", N_OPENPAREN, c_openparen, O_ZERO, 37 ")", N_CLOSEPAREN, c_closeparen, O_ZERO, 38 "-a", N_AND, NULL, O_NONE, 39 "-and", N_AND, NULL, O_NONE, 40 "-atime", N_ATIME, c_atime, O_ARGV, 41 "-ctime", N_CTIME, c_ctime, O_ARGV, 42 "-depth", N_DEPTH, c_depth, O_ZERO, 43 "-exec", N_EXEC, c_exec, O_ARGVP, 44 "-follow", N_FOLLOW, c_follow, O_ZERO, 45 "-fstype", N_FSTYPE, c_fstype, O_ARGV, 46 "-group", N_GROUP, c_group, O_ARGV, 47 "-inum", N_INUM, c_inum, O_ARGV, 48 "-links", N_LINKS, c_links, O_ARGV, 49 "-ls", N_LS, c_ls, O_ZERO, 50 "-mtime", N_MTIME, c_mtime, O_ARGV, 51 "-name", N_NAME, c_name, O_ARGV, 52 "-newer", N_NEWER, c_newer, O_ARGV, 53 "-nogroup", N_NOGROUP, c_nogroup, O_ZERO, 54 "-nouser", N_NOUSER, c_nouser, O_ZERO, 55 "-o", N_OR, c_or, O_ZERO, 56 "-ok", N_OK, c_exec, O_ARGVP, 57 "-or", N_OR, c_or, O_ZERO, 58 "-perm", N_PERM, c_perm, O_ARGV, 59 "-print", N_PRINT, c_print, O_ZERO, 60 "-prune", N_PRUNE, c_prune, O_ZERO, 61 "-size", N_SIZE, c_size, O_ARGV, 62 "-type", N_TYPE, c_type, O_ARGV, 63 "-user", N_USER, c_user, O_ARGV, 64 "-xdev", N_XDEV, c_xdev, O_ZERO, 65 { NULL }, 66 }; 67 68 /* 69 * find_create -- 70 * create a node corresponding to a command line argument. 71 * 72 * TODO: 73 * add create/process function pointers to node, so we can skip 74 * this switch stuff. 75 */ 76 PLAN * 77 find_create(argvp) 78 char ***argvp; 79 { 80 register OPTION *p; 81 PLAN *new; 82 char **argv; 83 OPTION *option(); 84 85 argv = *argvp; 86 87 if ((p = option(*argv)) == NULL) { 88 (void)fprintf(stderr, "find: unknown option %s.\n", *argv); 89 exit(1); 90 } 91 ++argv; 92 if (p->flags & (O_ARGV|O_ARGVP) && !*argv) { 93 (void)fprintf(stderr, 94 "find: %s requires additional arguments.\n", *--argv); 95 exit(1); 96 } 97 98 switch(p->flags) { 99 case O_NONE: 100 new = NULL; 101 break; 102 case O_ZERO: 103 new = (p->create)(); 104 break; 105 case O_ARGV: 106 new = (p->create)(*argv++); 107 break; 108 case O_ARGVP: 109 new = (p->create)(&argv, p->token == N_OK); 110 break; 111 } 112 *argvp = argv; 113 return(new); 114 } 115 116 OPTION * 117 option(name) 118 char *name; 119 { 120 OPTION tmp; 121 int typecompare __P((const void *, const void *)); 122 123 tmp.name = name; 124 return((OPTION *)bsearch(&tmp, options, 125 sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare)); 126 } 127 128 typecompare(a, b) 129 const void *a, *b; 130 { 131 return(strcmp(((OPTION *)a)->name, ((OPTION *)b)->name)); 132 } 133