home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 400-499 / ff473.lzh / CNewsSrc / cnews_src.lzh / misc / gngp.c < prev    next >
C/C++ Source or Header  |  1989-10-14  |  2KB  |  117 lines

  1. /*
  2.  * gngp - globally match newsgroup pattern and print
  3.  *    like grep, but for newsgroup patterns instead of regular expressions
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. char *progname;
  9. int debug = 0;
  10.  
  11. /*
  12.  * if true, match only ng at start of line, followed by whitespace or newline.
  13.  */
  14. int anchored = 0;
  15. int reverse = 0;    /* iff true, reverse argument & file roles */
  16.  
  17. FILE *efopen();
  18.  
  19. /*
  20.  * main - parse arguments and handle options
  21.  */
  22. main(argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.     int c, status = 0;
  27.     int errflg = 0;
  28.     FILE *in;
  29.     extern int optind;
  30.     extern char *optarg;
  31.  
  32.     progname = argv[0];
  33.     while ((c = getopt(argc, argv, "adr")) != EOF)
  34.         switch (c) {
  35.         case 'a':        /* anchored at start of line */
  36.             anchored++;
  37.             break;
  38.         case 'd':
  39.             matchdebug(1);    /* all debugging on */
  40.             debug++;
  41.             break;
  42.         case 'r':    /* reverse roles: ngs in arg., patterns in file */
  43.             reverse++;
  44.             break;
  45.         default:
  46.             errflg++;
  47.             break;
  48.         }
  49.     if (errflg || optind == argc) {
  50.         (void) fprintf(stderr, "usage: %s [-adr] ng_pattern [file...]\n",
  51.             progname);
  52.         exit(2);
  53.     }
  54.     if (optind == argc-1)
  55.         status |= process(argv[optind], stdin, "stdin");
  56.     else {
  57.         int patind = optind;
  58.  
  59.         for (optind++; optind < argc; optind++) {
  60.             in = efopen(argv[optind], "r");
  61.             status |= process(argv[patind], in, argv[optind]);
  62.             (void) fclose(in);
  63.         }
  64.     }
  65.     exit(status != 0? 0: 1);
  66. }
  67.  
  68. /*
  69.  * process - process input file
  70.  */
  71. process(pattern, in, inname)
  72. char *pattern;
  73. FILE *in;
  74. char *inname;
  75. {
  76.     int status = 0;
  77.     char line[BUFSIZ];
  78.  
  79.     while (fgets(line, sizeof line, in) != NULL)
  80.         if (anchored)
  81.             status |= gngp(pattern, line);
  82.         else {
  83.             register char *start;
  84.  
  85.             for (start = line; *start != '\0'; start++)
  86.                 status |= gngp(pattern, start);
  87.         }
  88.     return status;
  89. }
  90.  
  91. int
  92. gngp(pattern, text)
  93. register char *pattern, *text;
  94. {
  95.     int returned;
  96.     char savewhite;
  97.     char *whitesp;
  98.  
  99.     if (anchored) {
  100.         extern char *strpbrk();
  101.  
  102.         whitesp = strpbrk(text, " \t\n");
  103.         if (whitesp != NULL) {
  104.             savewhite = *whitesp;
  105.             *whitesp = '\0';
  106.         }
  107.     }
  108.     returned = (!reverse? ngmatch(pattern, text): ngmatch(text, pattern));
  109.     if (anchored) {
  110.         if (whitesp != NULL)
  111.             *whitesp = savewhite;
  112.     }
  113.     if (returned)
  114.         (void) fputs(text, stdout);
  115.     return returned;
  116. }
  117.