home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / rh2 / part01 / rh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-21  |  5.9 KB  |  268 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(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-v       verbose output\n");
  39.     printf("\t-e       get expression from the command line\n");
  40.     printf("\t-f       get expression from file\n");
  41.     printf("\t-x       execute a unix command for matching files\n\n");
  42.  
  43.     printf("\tvalid symbols:\n");
  44.     for(i=1, p=symbols; p; p=p->next, i++)
  45.         printf("%12s%s", p->name,
  46.             ((i-1)%5==4 || !p->next ) ? "\n" : " ");
  47.  
  48.     printf("\tC operators:\n");
  49.     printf("\t! ~ - * / %% + < <= > >= == != & ^ | << >> && || ?:\n");
  50.     printf("\tspecial operators:\n");
  51.     printf("\t$username , \"*.c\" , [yyyy/mm/dd]\n\n");
  52. }
  53.  
  54. /* ----------------------------------------------------------------------
  55.  * execute:
  56.  *    Execute the program contained in the StackProgram[]
  57.  *    array. Each element of the StackProgram[] array contains
  58.  *    a pointer to a function.
  59.  *    Programs are NULL terminated.
  60.  *    Returns the result of the expression.
  61.  *
  62.  */
  63.  
  64. execute()
  65. {
  66.     register long eval;
  67.     register int (*efunc)();
  68.  
  69.     SP=0;
  70.     for(PC=startPC; (efunc=StackProgram[PC].func) ; PC++) {
  71.         eval=StackProgram[PC].value;
  72.         (*efunc)(eval);
  73.         if( SP >= MEM ) {
  74.             fprintf(stderr,"stack overflow\n");
  75.             exit(1);
  76.         }
  77.     }
  78.     return( Stack[0] );
  79. }
  80.  
  81. /* ----------------------------------------------------------------------
  82.  * exam1: exam2: exam3:
  83.  *    One of these functions is called for every file that 'rh' examines.
  84.  *    exam{1,2,3}() first calls execute to see if the
  85.  *    expression is true, it then prints the file if the expression
  86.  *    evaluated to true (non-zero).
  87.  *
  88.  */
  89.  
  90. /* print file out by itself */
  91. exam1()
  92. {
  93.     if( execute() ) printf("%s\n",attr.fname);
  94. }
  95.  
  96. /* long output of file */
  97. exam2()
  98. {
  99.     if( execute() ) printentry(attr.verbose,attr.buf,attr.fname);
  100. }
  101.  
  102. /* do a system(3) call to desired command */
  103. exam3()
  104. {
  105.     char command[ 2048 + 1 ];
  106.     char *p,*q,*r,*strrchr();
  107.     int rv;
  108.  
  109.     if( execute() ) {
  110.         p=command;
  111.         q=attr.command;
  112.         while( *q ) {
  113.             if( *q != '%' ) *p++ = *q++;
  114.             else {
  115.                 q += 1;
  116.                 if( *q == 's' ) {
  117.                     r = attr.fname;
  118.                     while( *p++ = *r++ );
  119.                     p -= 1;
  120.                 } else if( *q == 'S' ) {
  121.                     r = strrchr(attr.fname,'/');
  122.                     r = (r) ? r+1 : attr.fname;
  123.                     while( *p++ = *r++ );
  124.                     p -= 1;
  125.                 } else *p++ = '%';
  126.                 q += 1;
  127.             }
  128.         }
  129.         *p = '\0';
  130.         rv = system(command);
  131.         if( attr.verbose ) printf("%s exit(%d)\n",command,rv);
  132.     }
  133. }
  134.  
  135.  
  136. /* ----------------------------------------------------------------------
  137.  * main:
  138.  *    parse arguments.
  139.  *    gnu getopt() is used here.
  140.  *    -l, -r, -h, -v options can occur as often as desired.
  141.  *    -f,-x and -e can only occur once and MUST have an argument.
  142.  *
  143.  *    Read and "compile" the $HOME/.rhrc file, if it exists.
  144.  *    Read and "compile" any -f filename, if present.
  145.  *    Read and "compile" any -e expression, if present.
  146.  *    If after all that no start expression is found then read from
  147.  *    stdin for one.
  148.  *    Perform the recursive hunt on remaining arguments.
  149.  *
  150.  */
  151.  
  152. main(argc,argv)
  153. int argc;
  154. char *argv[];
  155. {
  156.     extern int optind;
  157.     extern char *optarg;
  158.     char *dashe,*dashf,*strcat(),*getenv(),initfile[ 1024+1 ];
  159.     int i,r;
  160.     int dashr,dashh,dashl;
  161.     int (*examptr)();
  162.  
  163.     /* defaults */
  164.     dashe = NULL;        /* -e option */
  165.     dashl = 0;        /* -l */
  166.     dashf = NULL;        /* -f */
  167.     dashr = 1;        /* -r */
  168.     dashh = 0;        /* -h */
  169.     attr.verbose = 0;    /* -v */
  170.     attr.command = NULL;    /* -x */
  171.     examptr = exam1;    /* default output function */
  172.  
  173.     while ((i = getopt(argc, argv, "lrhvx:e:f:")) != EOF) {
  174.         switch( i ) {
  175.         case 'l': examptr = exam2; dashl = 1; break;
  176.         case 'r': dashr = 0; break;
  177.         case 'h': dashh = 1; break;
  178.         case 'v': attr.verbose = 1; break;
  179.         case 'x': 
  180.            if( attr.command ) {
  181.             fprintf(stderr, "%s: too many -x options\n",argv[0]);
  182.             exit(1);
  183.            }
  184.            examptr = exam3;
  185.            attr.command = optarg;
  186.            break;
  187.         case 'e':
  188.            if( dashe ) {
  189.             fprintf(stderr, "%s: too many -e options\n",argv[0]);
  190.             exit(1);
  191.            }
  192.            dashe = optarg;
  193.            break;
  194.         case 'f':
  195.            if( dashf ) {
  196.             fprintf(stderr, "%s: too many -f options\n",argv[0]);
  197.             exit(1);
  198.            }
  199.            dashf = optarg;
  200.            break;
  201.         default:
  202.            fprintf(stderr,"%s: use -h for help\n", argv[0],i);
  203.            fprintf(stderr,usage, argv[0]);
  204.            exit(1);
  205.         }
  206.         if( attr.command && dashl ) {
  207.             fprintf(stderr,
  208.                 "%s: cannot have both -x and -l options\n",
  209.                 argv[0]);
  210.             exit(1);
  211.         }
  212.     }
  213.  
  214.     PC = 0;
  215.     startPC = -1;
  216.     rhinit();
  217.     if( dashh ) printhelp(argv[0]);
  218.  
  219.     expfname = getenv( HOMEENV );
  220.     if( expfname ) {
  221.         strcpy(initfile,expfname);
  222.         expfname = strcat(initfile,RHRC);
  223.         if( (expfile = fopen(expfname,"r")) != NULL ) {
  224.             expstr = NULL;
  225.             program();
  226.         }
  227.     }
  228.  
  229.     if( dashf ) {
  230.         expstr = NULL;
  231.         expfname = dashf;
  232.             if( (expfile = fopen(expfname,"r")) == NULL ) {
  233.             fprintf(stderr,"%s: ", argv[0]);
  234.             perror(expfname);
  235.             exit(1);
  236.         }
  237.         program();
  238.     }
  239.     if( dashe ) {
  240.         expfile = NULL;
  241.         expstr = dashe;
  242.         program();
  243.     }
  244.     if( startPC == -1 ) {
  245.         expstr = NULL;
  246.         expfname = "stdin";
  247.         expfile = stdin;
  248.         program();
  249.     }
  250.  
  251.     if( startPC == -1 ) {
  252.         fprintf(stderr,"%s: no start expression specified\n",
  253.             argv[0] );
  254.         exit(1);
  255.     }
  256.     rhfinish();
  257.  
  258.     if( optind >= argc ) {
  259.         r=ftrw(".",examptr,(dashr)? DEPTH :1);
  260.         if(r == -1) perror(".");
  261.     } else
  262.         for(; optind<argc; optind++) {
  263.             r=ftrw(argv[optind],examptr,(dashr)? DEPTH :1);
  264.             if( r == -1 ) perror(argv[optind]);
  265.         }
  266.     exit(0);
  267. }
  268.