option.c revision 1.2       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  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char sccsid[] = "@(#)option.c	5.8 (Berkeley) 6/4/91";
     39 #endif /* not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <fts.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include "find.h"
     48 
     49 typedef struct _option {
     50 	char *name;		/* option name */
     51 	enum ntype token;	/* token type */
     52 	PLAN *(*create)();	/* create function */
     53 #define	O_NONE		0x01	/* no call required */
     54 #define	O_ZERO		0x02	/* pass: nothing */
     55 #define	O_ARGV		0x04	/* pass: argv, increment argv */
     56 #define	O_ARGVP		0x08	/* pass: *argv, N_OK || N_EXEC */
     57 	int flags;
     58 } OPTION;
     59 
     60 OPTION options[] = {
     61 	"!",		N_NOT,		c_not,		O_ZERO,
     62 	"(",		N_OPENPAREN,	c_openparen,	O_ZERO,
     63 	")",		N_CLOSEPAREN,	c_closeparen,	O_ZERO,
     64 	"-a",		N_AND,		NULL,		O_NONE,
     65 	"-and",		N_AND,		NULL,		O_NONE,
     66 	"-atime",	N_ATIME,	c_atime,	O_ARGV,
     67 	"-ctime",	N_CTIME,	c_ctime,	O_ARGV,
     68 	"-depth",	N_DEPTH,	c_depth,	O_ZERO,
     69 	"-exec",	N_EXEC,		c_exec,		O_ARGVP,
     70 	"-follow",	N_FOLLOW,	c_follow,	O_ZERO,
     71 	"-fstype",	N_FSTYPE,	c_fstype,	O_ARGV,
     72 	"-group",	N_GROUP,	c_group,	O_ARGV,
     73 	"-inum",	N_INUM,		c_inum,		O_ARGV,
     74 	"-links",	N_LINKS,	c_links,	O_ARGV,
     75 	"-ls",		N_LS,		c_ls,		O_ZERO,
     76 	"-mtime",	N_MTIME,	c_mtime,	O_ARGV,
     77 	"-name",	N_NAME,		c_name,		O_ARGV,
     78 	"-newer",	N_NEWER,	c_newer,	O_ARGV,
     79 	"-nogroup",	N_NOGROUP,	c_nogroup,	O_ZERO,
     80 	"-nouser",	N_NOUSER,	c_nouser,	O_ZERO,
     81 	"-o",		N_OR,		c_or,		O_ZERO,
     82 	"-ok",		N_OK,		c_exec,		O_ARGVP,
     83 	"-or",		N_OR,		c_or,		O_ZERO,
     84 	"-perm",	N_PERM,		c_perm,		O_ARGV,
     85 	"-print",	N_PRINT,	c_print,	O_ZERO,
     86 	"-prune",	N_PRUNE,	c_prune,	O_ZERO,
     87 	"-size",	N_SIZE,		c_size,		O_ARGV,
     88 	"-type",	N_TYPE,		c_type,		O_ARGV,
     89 	"-user",	N_USER,		c_user,		O_ARGV,
     90 	"-xdev",	N_XDEV,		c_xdev,		O_ZERO,
     91 };
     92 
     93 /*
     94  * find_create --
     95  *	create a node corresponding to a command line argument.
     96  *
     97  * TODO:
     98  *	add create/process function pointers to node, so we can skip
     99  *	this switch stuff.
    100  */
    101 PLAN *
    102 find_create(argvp)
    103 	char ***argvp;
    104 {
    105 	register OPTION *p;
    106 	PLAN *new;
    107 	char **argv;
    108 	OPTION *option();
    109 
    110 	argv = *argvp;
    111 
    112 	if ((p = option(*argv)) == NULL) {
    113 		(void)fprintf(stderr, "find: unknown option %s.\n", *argv);
    114 		exit(1);
    115 	}
    116 	++argv;
    117 	if (p->flags & (O_ARGV|O_ARGVP) && !*argv) {
    118 		(void)fprintf(stderr,
    119 		    "find: %s requires additional arguments.\n", *--argv);
    120 		exit(1);
    121 	}
    122 
    123 	switch(p->flags) {
    124 	case O_NONE:
    125 		new = NULL;
    126 		break;
    127 	case O_ZERO:
    128 		new = (p->create)();
    129 		break;
    130 	case O_ARGV:
    131 		new = (p->create)(*argv++);
    132 		break;
    133 	case O_ARGVP:
    134 		new = (p->create)(&argv, p->token == N_OK);
    135 		break;
    136 	}
    137 	*argvp = argv;
    138 	return(new);
    139 }
    140 
    141 OPTION *
    142 option(name)
    143 	char *name;
    144 {
    145 	OPTION tmp;
    146 	int typecompare __P((const void *, const void *));
    147 
    148 	tmp.name = name;
    149 	return((OPTION *)bsearch(&tmp, options,
    150 	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
    151 }
    152 
    153 typecompare(a, b)
    154 	const void *a, *b;
    155 {
    156 	return(strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
    157 }
    158