home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / util / gngp.c < prev    next >
C/C++ Source or Header  |  1992-03-02  |  3KB  |  136 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. #include <ctype.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include "libc.h"
  11. #include "news.h"
  12. #include "fgetfln.h"
  13. #include "ngmatch.h"
  14.  
  15. /* imports */
  16. extern FILE *efopen();
  17.  
  18. char *progname;
  19. int debug = 0;
  20.  
  21. /*
  22.  * if true, match only ng at start of line, followed by whitespace or newline.
  23.  */
  24. static int anchored = 0;
  25. static int reverse = 0;    /* iff true, reverse argument & file roles */
  26. static int exclude = 0;    /* iff true, print lines *not* matched */
  27. static NGPAT *ngpat;
  28.  
  29. /*
  30.  * main - parse arguments and handle options
  31.  */
  32. main(argc, argv)
  33. int argc;
  34. char *argv[];
  35. {
  36.     int c, status = 0, errflg = 0;
  37.     char *patarg;
  38.  
  39.     progname = argv[0];
  40.     while ((c = getopt(argc, argv, "adrv")) != EOF)
  41.         switch (c) {
  42.         case 'a':        /* anchored at start of line */
  43.             anchored++;
  44.             break;
  45.         case 'd':
  46.             matchdebug(1);    /* all debugging on */
  47.             debug++;
  48.             break;
  49.         case 'r':    /* reverse roles: ngs in arg., patterns in file */
  50.             reverse++;
  51.             break;
  52.         case 'v':
  53.             exclude++;
  54.             break;
  55.         default:
  56.             errflg++;
  57.             break;
  58.         }
  59.     if (errflg || optind == argc) {
  60.         (void) fprintf(stderr, "usage: %s [-adrv] ng_pattern [file...]\n",
  61.             progname);
  62.         exit(2);
  63.     }
  64.     patarg = argv[optind];
  65.     if (!reverse) {
  66.         ngpat = ngparse(patarg);
  67.         if (ngpat == NULL)
  68.             error("can't parse pattern `%s'", patarg);
  69.     }
  70.     if (optind == argc-1)
  71.         status |= process(patarg, stdin, "stdin");
  72.     else {
  73.         while (++optind < argc) {
  74.             FILE *in = efopen(argv[optind], "r");
  75.  
  76.             status |= process(patarg, in, argv[optind]);
  77.             (void) fclose(in);
  78.         }
  79.     }
  80.     exit(status != 0? 0: 1);
  81. }
  82.  
  83. /*
  84.  * process - process input file
  85.  */
  86. process(pattern, in, inname)
  87. register char *pattern;
  88. register FILE *in;
  89. char *inname;
  90. {
  91.     register char *line;
  92.     register int status = 0;
  93.  
  94.     while ((line = fgetln(in)) != NULL)
  95.         if (anchored)
  96.             status |= gngp(pattern, line);
  97.         else {
  98.             register char *start;
  99.  
  100.             for (start = line; *start != '\0'; start++)
  101.                 status |= gngp(pattern, start);
  102.         }
  103.     return status;
  104. }
  105.  
  106. int
  107. gngp(pattern, text)
  108. register char *pattern, *text;
  109. {
  110.     register int returned;
  111.     register char *whitesp;
  112.     register char savewhite;
  113.  
  114.     if (anchored)
  115.         /* strpbrk(text, " \t\n") is too slow; do it long-hand */
  116.         for (whitesp = text; (savewhite = *whitesp) != '\0'; whitesp++)
  117.             if (isascii(savewhite) && isspace(savewhite)) {
  118.                 *whitesp = '\0';
  119.                 break;
  120.             }
  121.  
  122.     if (!reverse)
  123.         returned = ngpatmat(ngpat, text);
  124.     else
  125.         returned = ngmatch(text, pattern);
  126.     if (exclude)
  127.         returned = !returned;
  128.  
  129.     if (anchored)
  130.         *whitesp = savewhite;
  131.  
  132.     if (returned)
  133.         (void) fputs(text, stdout);
  134.     return returned;
  135. }
  136.