home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / rh / rh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-23  |  4.9 KB  |  215 lines

  1.  
  2. /* ----------------------------------------------------------------------
  3.  * FILE: rh.c
  4.  * (c) 1989 Ken Stauffer
  5.  * 
  6.  * printhelp(), execute(), examine(), main()
  7.  *
  8.  *
  9.  * ---------------------------------------------------------------------- */
  10.  
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #define MAIN
  14. #include "rh.h"
  15.  
  16. static char usage[]={
  17. "Usage: %s [-ahlr] [ [-e expr] | [-f filename [-mname]] ] file ...\n"
  18. };
  19.  
  20.  
  21. /* ----------------------------------------------------------------------
  22.  * printhelp:
  23.  *    Print out the help screen. The string 's' is argv[0].
  24.  *    Called when the -h option is used.
  25.  *
  26.  */
  27.  
  28. printhelp(s)
  29. char *s;
  30. {
  31.    int i;
  32.  
  33.     printf(usage,s);
  34.     printf("options:\n");
  35.     printf("\t-h       show this message\n");
  36.     printf("\t-l       long filename output\n");
  37.     printf("\t-r       makes %s non-recursive\n",s);
  38.     printf("\t-a       adds to the long output to make it longer\n");
  39.     printf("\t-mname   use the named macro 'name' from 'file'\n");
  40.     printf("\t-e       get expression from the command line\n");
  41.     printf("\t-f       get expression from file\n\n");
  42.     printf("VALID EXPRESSIONS CONSIST OF:\n");
  43.  
  44.     printf("\tCONSTANTS/variables: see stat(2)\n");
  45.     for(i=0; identifiers[i]; i++ )
  46.         printf("%s%12s%s", (!(i%5==0)) ? "\t" : "" , 
  47.             identifiers[i],
  48.             (i%5==4 || !identifiers[i+1]) ? "\n" : " ");
  49.  
  50.     printf("\tC OPERATORS (precedence same as C):\n");
  51.     printf("\t! ~ - * / %% + < <= > >= == != & ^ | << >> && || ?:\n");
  52.     printf("\tSPECIAL OPERATORS:\n");
  53.     printf("\t$username , \"*.c\" , [yyyy/mm/dd]\n\n");
  54.  
  55. }
  56.  
  57. /* ----------------------------------------------------------------------
  58.  * execute:
  59.  *    Execute the program contained in the StackProgram[]
  60.  *    array.
  61.  *    Indexes the commands[] array to obtain the correct
  62.  *    function/operator. Programs are NULL terminated.
  63.  *    Returns the result of the expression.
  64.  *
  65.  */
  66.  
  67. execute()
  68. {
  69.     register long etype,eval;
  70.     SP=0;
  71.     for(PC=0; (etype=StackProgram[PC].i_type) ; PC++) {
  72.         eval=StackProgram[PC].i_value;
  73.  
  74.     (*commands[ (etype!=FIELD) ? etype-START : etype-START+eval ] ) (eval);
  75.  
  76.     }
  77.     return( Stack[0] );
  78. }
  79.  
  80. /* ----------------------------------------------------------------------
  81.  * examine:
  82.  *    This function is called for every file that 'rh' examines.
  83.  *    examine() first calls execute to see if the
  84.  *    expression is true, it then prints the file if the expression
  85.  *    evaluated to true (non-zero).
  86.  *
  87.  */
  88.  
  89. examine(s,buf)
  90. char *s;
  91. struct stat *buf;
  92. {
  93.     fname=s;
  94.     globuf=buf;
  95.  
  96.     if( execute() )
  97.         (dashl) ? printentry(globuf,fname) : printf("%s\n",fname);
  98. }
  99.  
  100. /* ----------------------------------------------------------------------
  101.  * main:
  102.  *    parse arguments.
  103.  *    two for loops are used to parse the argument list,
  104.  *    the first for loop process the arguments. The second
  105.  *    for loop process the characters within the current argument.
  106.  *    -l, -r, -h options can occur as often as desired.
  107.  *    -f and -e and -m can only occur once and MUST have an argument.
  108.  *
  109.  *    parse expression.
  110.  *    the expression is parsed and if all is ok, then the
  111.  *    recursive hunt is started.
  112.  *
  113.  */
  114.  
  115. main(argc,argv)
  116. int argc;
  117. char *argv[];
  118. {
  119.     int i,r,skip;
  120.     char *j;
  121.  
  122.     /* defaults */
  123.     dashe = 0;        /* -e option */
  124.     dashl = 0;        /* -l */
  125.     dashf = 0;        /* -f */
  126.     dashr = 1;        /* -r */
  127.     dashh = 0;        /* -h */
  128.     dashm = 0;        /* -m */
  129.     dasha = 0;        /* -a */
  130.     
  131.    for(i=1; i<argc; i++ ) {
  132.     if( *argv[i] != '-' ) break;
  133.     skip = 0;
  134.     for(j=argv[i]+1; *j; j++ ) {
  135.        switch( *j ) {
  136.        case 'l': dashl = 1; break;
  137.        case 'r': dashr = 0; break;
  138.        case 'h': dashh = 1; break;
  139.        case 'a': dashl = dasha = 1; break;
  140.        case 'm':
  141.         if( dashm ) {
  142.             fprintf(stderr,"%s: too many -m options\n", argv[0]);
  143.             exit(-1);
  144.         }
  145.         dashm = 1;
  146.         identifiers[ MACRO_INDEX ] = j+1;
  147.         while(*j) j++; j--;
  148.         break;
  149.        case 'e':
  150.         if( dashe ) {
  151.             fprintf(stderr, "%s: too many -e options\n",argv[0]);
  152.             exit(-1);
  153.         }
  154.         dashe = i+1;
  155.         skip = 1;
  156.         break;
  157.        case 'f':
  158.         if( dashf ) {
  159.             fprintf(stderr, "%s: too many -f options\n",argv[0]);
  160.             exit(-1);
  161.         } 
  162.         dashf = i+1;
  163.         skip = 1;
  164.         break;
  165.        default:
  166.         fprintf(stderr,"%s: illegal option -%c, use -h for help\n",
  167.         argv[0],*j);
  168.         fprintf(stderr,usage, argv[0]);
  169.         exit(-1);
  170.        }
  171.        if( dashf && dashe ) {
  172.         fprintf(stderr, "%s: cannot have both -e and -f\n",argv[0]);
  173.         exit(-1);
  174.        }
  175.     }
  176.     if( skip ) i++;
  177.    }
  178.  
  179.     if( dashh ) printhelp(argv[0]);
  180.  
  181.     if( dashm && !dashf ) {
  182.         fprintf(stderr,"%s: -m option needs the -f option\n",argv[0]);
  183.         exit(-1);
  184.     }
  185.     if( dashf >= argc ) {
  186.         fprintf(stderr,"%s: -f missing an argument\n",argv[0]);
  187.         exit(-1);
  188.     }
  189.     if( dashe >= argc ) {
  190.         fprintf(stderr,"%s: -e missing an argument\n",argv[0]);
  191.         exit(-1);
  192.     }
  193.     
  194.     if( dashe ) { expstr = argv[dashe]; }
  195.     else if( dashf ) {
  196.         expstr = argv[dashf];
  197.         if( (expfile = fopen(expstr,"r")) == NULL ) {
  198.             fprintf(stdout,"%s: cannot open %s\n",
  199.                 argv[0],expstr);
  200.             exit(-1);
  201.         }
  202.     } else expfile = stdin;
  203.     
  204.     expression();
  205.  
  206.     if( i >= argc ) {
  207.         r=ftw(".",examine,(dashr)? DEPTH :1);
  208.         if(r == -1) perror(".");
  209.     } else
  210.         for(; i<argc; i++) {
  211.             r=ftw( argv[i],examine,(dashr)? DEPTH :1);
  212.             if( r == -1 ) perror(argv[i]);
  213.         }
  214. }
  215.