home *** CD-ROM | disk | FTP | other *** search
-
- /* ----------------------------------------------------------------------
- * FILE: rh.c
- * (c) 1989 Ken Stauffer
- *
- * printhelp(), execute(), examine(), main()
- *
- *
- * ---------------------------------------------------------------------- */
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #define MAIN
- #include "rh.h"
-
- static char usage[]={
- "Usage: %s [-ahlr] [ [-e expr] | [-f filename [-mname]] ] file ...\n"
- };
-
-
- /* ----------------------------------------------------------------------
- * printhelp:
- * Print out the help screen. The string 's' is argv[0].
- * Called when the -h option is used.
- *
- */
-
- printhelp(s)
- char *s;
- {
- int i;
-
- printf(usage,s);
- printf("options:\n");
- printf("\t-h show this message\n");
- printf("\t-l long filename output\n");
- printf("\t-r makes %s non-recursive\n",s);
- printf("\t-a adds to the long output to make it longer\n");
- printf("\t-mname use the named macro 'name' from 'file'\n");
- printf("\t-e get expression from the command line\n");
- printf("\t-f get expression from file\n\n");
- printf("VALID EXPRESSIONS CONSIST OF:\n");
-
- printf("\tCONSTANTS/variables: see stat(2)\n");
- for(i=0; identifiers[i]; i++ )
- printf("%s%12s%s", (!(i%5==0)) ? "\t" : "" ,
- identifiers[i],
- (i%5==4 || !identifiers[i+1]) ? "\n" : " ");
-
- printf("\tC OPERATORS (precedence same as C):\n");
- printf("\t! ~ - * / %% + < <= > >= == != & ^ | << >> && || ?:\n");
- printf("\tSPECIAL OPERATORS:\n");
- printf("\t$username , \"*.c\" , [yyyy/mm/dd]\n\n");
-
- }
-
- /* ----------------------------------------------------------------------
- * execute:
- * Execute the program contained in the StackProgram[]
- * array.
- * Indexes the commands[] array to obtain the correct
- * function/operator. Programs are NULL terminated.
- * Returns the result of the expression.
- *
- */
-
- execute()
- {
- register long etype,eval;
- SP=0;
- for(PC=0; (etype=StackProgram[PC].i_type) ; PC++) {
- eval=StackProgram[PC].i_value;
-
- (*commands[ (etype!=FIELD) ? etype-START : etype-START+eval ] ) (eval);
-
- }
- return( Stack[0] );
- }
-
- /* ----------------------------------------------------------------------
- * examine:
- * This function is called for every file that 'rh' examines.
- * examine() first calls execute to see if the
- * expression is true, it then prints the file if the expression
- * evaluated to true (non-zero).
- *
- */
-
- examine(s,buf)
- char *s;
- struct stat *buf;
- {
- fname=s;
- globuf=buf;
-
- if( execute() )
- (dashl) ? printentry(globuf,fname) : printf("%s\n",fname);
- }
-
- /* ----------------------------------------------------------------------
- * main:
- * parse arguments.
- * two for loops are used to parse the argument list,
- * the first for loop process the arguments. The second
- * for loop process the characters within the current argument.
- * -l, -r, -h options can occur as often as desired.
- * -f and -e and -m can only occur once and MUST have an argument.
- *
- * parse expression.
- * the expression is parsed and if all is ok, then the
- * recursive hunt is started.
- *
- */
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int i,r,skip;
- char *j;
-
- /* defaults */
- dashe = 0; /* -e option */
- dashl = 0; /* -l */
- dashf = 0; /* -f */
- dashr = 1; /* -r */
- dashh = 0; /* -h */
- dashm = 0; /* -m */
- dasha = 0; /* -a */
-
- for(i=1; i<argc; i++ ) {
- if( *argv[i] != '-' ) break;
- skip = 0;
- for(j=argv[i]+1; *j; j++ ) {
- switch( *j ) {
- case 'l': dashl = 1; break;
- case 'r': dashr = 0; break;
- case 'h': dashh = 1; break;
- case 'a': dashl = dasha = 1; break;
- case 'm':
- if( dashm ) {
- fprintf(stderr,"%s: too many -m options\n", argv[0]);
- exit(-1);
- }
- dashm = 1;
- identifiers[ MACRO_INDEX ] = j+1;
- while(*j) j++; j--;
- break;
- case 'e':
- if( dashe ) {
- fprintf(stderr, "%s: too many -e options\n",argv[0]);
- exit(-1);
- }
- dashe = i+1;
- skip = 1;
- break;
- case 'f':
- if( dashf ) {
- fprintf(stderr, "%s: too many -f options\n",argv[0]);
- exit(-1);
- }
- dashf = i+1;
- skip = 1;
- break;
- default:
- fprintf(stderr,"%s: illegal option -%c, use -h for help\n",
- argv[0],*j);
- fprintf(stderr,usage, argv[0]);
- exit(-1);
- }
- if( dashf && dashe ) {
- fprintf(stderr, "%s: cannot have both -e and -f\n",argv[0]);
- exit(-1);
- }
- }
- if( skip ) i++;
- }
-
- if( dashh ) printhelp(argv[0]);
-
- if( dashm && !dashf ) {
- fprintf(stderr,"%s: -m option needs the -f option\n",argv[0]);
- exit(-1);
- }
- if( dashf >= argc ) {
- fprintf(stderr,"%s: -f missing an argument\n",argv[0]);
- exit(-1);
- }
- if( dashe >= argc ) {
- fprintf(stderr,"%s: -e missing an argument\n",argv[0]);
- exit(-1);
- }
-
- if( dashe ) { expstr = argv[dashe]; }
- else if( dashf ) {
- expstr = argv[dashf];
- if( (expfile = fopen(expstr,"r")) == NULL ) {
- fprintf(stdout,"%s: cannot open %s\n",
- argv[0],expstr);
- exit(-1);
- }
- } else expfile = stdin;
-
- expression();
-
- if( i >= argc ) {
- r=ftw(".",examine,(dashr)? DEPTH :1);
- if(r == -1) perror(".");
- } else
- for(; i<argc; i++) {
- r=ftw( argv[i],examine,(dashr)? DEPTH :1);
- if( r == -1 ) perror(argv[i]);
- }
- }
-