home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / RH2OS2.ZIP / SOURCE.ZIP / RH.C < prev    next >
C/C++ Source or Header  |  1990-06-26  |  9KB  |  328 lines

  1.  
  2. /* ----------------------------------------------------------------------
  3.  * FILE: rh.c
  4.  * VERSION: 2
  5.  * Written by: Ken Stauffer
  6.  *
  7.  * printhelp(), execute(), exam1(), exam2(), exam3(), main()
  8.  *
  9.  *
  10.  * ---------------------------------------------------------------------- */
  11.  
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include "rh.h"
  15.  
  16. static char usage[]={
  17. "Usage: %s [-vhlr] [ [-e expr] | [-f filename ] ] [-x command ] file ...\n"
  18. };
  19.  
  20. /* ----------------------------------------------------------------------
  21.  * printhelp:
  22.  *    Print out the help screen. The string 's' is argv[0].
  23.  *    Called when the -h option is used.
  24.  *
  25.  */
  26.  
  27. printhelp(s)
  28. char *s;
  29. {
  30.     int i;
  31.     struct symbol *p;
  32.  
  33.         printf("\n");
  34.     printf(usage,s);
  35.         printf("\nOptions:\n");
  36.         printf("  -h       show this message\n");
  37.         printf("  -l       long filename output\n");
  38.         printf("  -r       makes %s non-recursive\n",s);
  39.         printf("  -v       verbose output\n");
  40.         printf("  -e       get expression from the command line\n");
  41.         printf("  -f       get expression from file\n");
  42.         printf("  -x       execute a unix command for matching files\n");
  43.  
  44.         printf("\nValid symbols:\n  ");
  45.     for(i=1, p=symbols; p; p=p->next, i++)
  46.                 printf("%-10s%s", p->name,
  47.                         ((i-1)%7==6 || !p->next ) ? "\n  " : " ");
  48.  
  49.         printf("\nC operators:  ");
  50.         printf("! ~ - * / %% + < <= > >= == != & ^ | << >> && || ?:\n");
  51.  
  52.         printf("Special operators:  ");
  53. #ifdef MSDOS
  54.         printf("\"*.c\" , [yyyy/mm/dd]\n");
  55. #else
  56.         printf("$username , \"*.c\" , [yyyy/mm/dd]\n");
  57. #endif
  58.  
  59.         exit(1);
  60. }
  61.  
  62. /* ----------------------------------------------------------------------
  63.  * execute:
  64.  *    Execute the program contained in the StackProgram[]
  65.  *    array. Each element of the StackProgram[] array contains
  66.  *    a pointer to a function.
  67.  *    Programs are NULL terminated.
  68.  *    Returns the result of the expression.
  69.  *
  70.  */
  71.  
  72. execute()
  73. {
  74.     register long eval;
  75.     register int (*efunc)();
  76.  
  77.     SP=0;
  78.     for(PC=startPC; (efunc=StackProgram[PC].func) ; PC++) {
  79.         eval=StackProgram[PC].value;
  80.         (*efunc)(eval);
  81.         if( SP >= MEM ) {
  82.             fprintf(stderr,"stack overflow\n");
  83.             exit(1);
  84.         }
  85.     }
  86.     return( Stack[0] );
  87. }
  88.  
  89. /* ----------------------------------------------------------------------
  90.  * exam1: exam2: exam3:
  91.  *    One of these functions is called for every file that 'rh' examines.
  92.  *    exam{1,2,3}() first calls execute to see if the
  93.  *    expression is true, it then prints the file if the expression
  94.  *    evaluated to true (non-zero).
  95.  *
  96.  */
  97.  
  98. /* print file out by itself */
  99. exam1()
  100. {
  101.     if( execute() ) printf("%s\n",attr.fname);
  102. }
  103.  
  104. /* long output of file */
  105. exam2()
  106. {
  107.     if( execute() ) printentry(attr.verbose,attr.buf,attr.fname);
  108. }
  109.  
  110. /* do a system(3) call to desired command */
  111. exam3()
  112. {
  113.     char command[ 2048 + 1 ];
  114.         char *p,*q,*r,*s,*strrchr(), *strchr();
  115.     int rv;
  116.  
  117.     if( execute() ) {
  118.         p=command;
  119.         q=attr.command;
  120.         while( *q ) {
  121.             if( *q != '%' ) *p++ = *q++;
  122.                         else
  123.                                 switch ( ++q, *q++ )
  124.                                 {
  125.                                 case 'f':
  126.                                 case 's':
  127.                                   r = attr.fname;
  128.                                   while( *p++ = *r++ );
  129.                                   p--;
  130.                                   break;
  131.                                 case 'S':
  132.                                   r = strrchr(attr.fname,'/');
  133.                                   r = (r) ? r+1 : attr.fname;
  134.                                   while( *p++ = *r++ );
  135.                                   p--;
  136.                                   break;
  137.                                 case 'p':
  138.                                   if ( r = strrchr(attr.fname,'/') )
  139.                                     for ( s = attr.fname; s <= r; )
  140.                                       *p++ = *s++;
  141.                                   break;
  142.                                 case 'n':
  143.                                   r = strrchr(attr.fname,'/');
  144.                                   r = (r) ? r+1 : attr.fname;
  145.                                   s = strrchr(r,'.');
  146.                                   s = (s) ? s : strchr(r,0);
  147.                                   while ( r < s )
  148.                                     *p++ = *r++;
  149.                                   break;
  150.                                 case 'e':
  151.                                   if ( r = strrchr(attr.fname,'.') )
  152.                                   {
  153.                                     while ( *p++ = *r++ );
  154.                                     p--;
  155.                                   }
  156.                                   break;
  157.                                 case '%':
  158.                                   *p++ = '%';
  159.                                   break;
  160.                                 }
  161.         }
  162.         *p = '\0';
  163.         rv = system(command);
  164.         if( attr.verbose ) printf("%s exit(%d)\n",command,rv);
  165.     }
  166. }
  167.  
  168.  
  169. /* ----------------------------------------------------------------------
  170.  * main:
  171.  *    parse arguments.
  172.  *    gnu getopt() is used here.
  173.  *    -l, -r, -h, -v options can occur as often as desired.
  174.  *    -f,-x and -e can only occur once and MUST have an argument.
  175.  *
  176.  *    Read and "compile" the $HOME/.rhrc file, if it exists.
  177.  *    Read and "compile" any -f filename, if present.
  178.  *    Read and "compile" any -e expression, if present.
  179.  *    If after all that no start expression is found then read from
  180.  *    stdin for one.
  181.  *    Perform the recursive hunt on remaining arguments.
  182.  *
  183.  */
  184.  
  185. main(argc,argv)
  186. int argc;
  187. char *argv[];
  188. {
  189.     extern int optind;
  190.     extern char *optarg;
  191.     char *dashe,*dashf,*strcat(),*getenv(),initfile[ 1024+1 ];
  192.     int i,r;
  193.     int dashr,dashh,dashl;
  194.     int (*examptr)();
  195.  
  196.     /* defaults */
  197.     dashe = NULL;        /* -e option */
  198.     dashl = 0;        /* -l */
  199.     dashf = NULL;        /* -f */
  200.     dashr = 1;        /* -r */
  201.     dashh = 0;        /* -h */
  202.     attr.verbose = 0;    /* -v */
  203.     attr.command = NULL;    /* -x */
  204.     examptr = exam1;    /* default output function */
  205.  
  206.     while ((i = getopt(argc, argv, "lrhvx:e:f:")) != EOF) {
  207.         switch( i ) {
  208.         case 'l': examptr = exam2; dashl = 1; break;
  209.         case 'r': dashr = 0; break;
  210.         case 'h': dashh = 1; break;
  211.         case 'v': attr.verbose = 1; break;
  212.         case 'x':
  213.            if( attr.command ) {
  214.             fprintf(stderr, "%s: too many -x options\n",argv[0]);
  215.             exit(1);
  216.            }
  217.            examptr = exam3;
  218.            attr.command = optarg;
  219.            break;
  220.         case 'e':
  221.            if( dashe ) {
  222.             fprintf(stderr, "%s: too many -e options\n",argv[0]);
  223.             exit(1);
  224.            }
  225.            dashe = optarg;
  226.            break;
  227.         case 'f':
  228.            if( dashf ) {
  229.             fprintf(stderr, "%s: too many -f options\n",argv[0]);
  230.             exit(1);
  231.            }
  232.            dashf = optarg;
  233.            break;
  234.         default:
  235.            fprintf(stderr,"%s: use -h for help\n", argv[0],i);
  236.            fprintf(stderr,usage, argv[0]);
  237.            exit(1);
  238.         }
  239.         if( attr.command && dashl ) {
  240.             fprintf(stderr,
  241.                 "%s: cannot have both -x and -l options\n",
  242.                 argv[0]);
  243.             exit(1);
  244.         }
  245.     }
  246.  
  247.     PC = 0;
  248.     startPC = -1;
  249.     rhinit();
  250.     if( dashh ) printhelp(argv[0]);
  251.  
  252. #ifdef MSDOS
  253.         _searchenv("RH.INI", "INIT", initfile);
  254.         if ( initfile[0] == 0 )
  255.           _searchenv("RH.INI", "PATH", initfile);
  256.  
  257.         expfname = initfile;
  258.         if ( expfname[0] != 0 )
  259.           if( (expfile = fopen(expfname,"r")) != NULL ) {
  260.                   expstr = NULL;
  261.                   program();
  262.           }
  263. #else
  264.     expfname = getenv( HOMEENV );
  265.     if( expfname ) {
  266.         strcpy(initfile,expfname);
  267.         expfname = strcat(initfile,RHRC);
  268.         if( (expfile = fopen(expfname,"r")) != NULL ) {
  269.             expstr = NULL;
  270.             program();
  271.         }
  272.     }
  273. #endif
  274.  
  275.     if( dashf ) {
  276.         expstr = NULL;
  277.                 expfname = dashf;
  278.                 if ( strcmp(expfname, "-") == 0 )
  279.                 {
  280.                   expfname = "stdin";
  281.                   expfile = stdin;
  282.                   printf("\n%s (file finder) version 2\n"
  283.                          "Please enter expression:\n", argv[0]);
  284.                 }
  285.                 else
  286.                   if( (expfile = fopen(expfname,"r")) == NULL ) {
  287.                           fprintf(stderr,"%s: ", argv[0]);
  288.                           perror(expfname);
  289.                           exit(1);
  290.                   }
  291.         program();
  292.         }
  293.  
  294.     if( dashe ) {
  295.         expfile = NULL;
  296.         expstr = dashe;
  297.         program();
  298.         }
  299.  
  300.         if( startPC == -1 ) {
  301. #ifdef MSDOS
  302.                 printhelp(argv[0]);
  303. #else
  304.         expstr = NULL;
  305.         expfname = "stdin";
  306.         expfile = stdin;
  307.                 program();
  308. #endif
  309.     }
  310.  
  311.     if( startPC == -1 ) {
  312.         fprintf(stderr,"%s: no start expression specified\n",
  313.             argv[0] );
  314.         exit(1);
  315.     }
  316.     rhfinish();
  317.  
  318.     if( optind >= argc ) {
  319.         r=ftrw(".",examptr,(dashr)? DEPTH :1);
  320.         if(r == -1) perror(".");
  321.     } else
  322.         for(; optind<argc; optind++) {
  323.             r=ftrw(argv[optind],examptr,(dashr)? DEPTH :1);
  324.             if( r == -1 ) perror(argv[optind]);
  325.         }
  326.     exit(0);
  327. }
  328.