home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / ut-c.lbr / FIND.CZ / FIND.C
Encoding:
C/C++ Source or Header  |  1993-10-25  |  4.5 KB  |  167 lines

  1. /* find.c -- UTOOL. Find patterns in text files (ver. 3) 
  2.  
  3.      author: David H. Wolen
  4.      last change: 5/2/83
  5.  
  6.      usage:     find [-axn] pattern1 [pattern2 ... pattern10]
  7.  
  8.      options:  -a   output lines which contain all patterns
  9.                     (default is output lines matching any pattern)
  10.                -x   output lines that fail the match criteria
  11.                -n   line numbers for output
  12.                -c   just output count of matching lines
  13.                -f   fold (ignore case in character comparisons)
  14.  
  15.      input:    STDIN
  16.      output:   STDOUT
  17.  
  18.      notes:    1. -x pat1 pat2 : output if line contains neither pat
  19.                2. -ax pat1 pat2 : output if line doesn't contain both pats
  20.                3. metacharacters in pattern
  21.                     %    match pattern at beginning of line
  22.                     # #  enclose capital letters
  23.                     " "  enclose embedded blanks
  24.                     ?    match any char except newline
  25.                     $    end of line
  26.                     [c1-c2]  character class
  27.                     [!c1-c2] negated character class
  28.                     *    closure (0 or more occurrences of prev pattern)
  29.                     escaped chars:  \n (newline)  \t (tab)  \r (cr)
  30.                          \f (form feed)  \b (backspace)  \q (")  \p (#)
  31.                4. first pattern can't begin with "-"
  32.                5. up to MAXPATS patterns allowed (currently 10)
  33.                6. if -c and -n, the latter will be ignored
  34.  
  35.      linkage:  a:clink find -f dio -ca (uses deff3.crl)
  36.  
  37. */
  38.  
  39. #include "a:bdscio.h"
  40. #include "dio.h"
  41.  
  42. #define  STDIN  0
  43. #define  MAXPATS 10      /* max patterns */
  44.  
  45.  
  46.  
  47. main(argc,argv)
  48. int  argc;
  49. char *argv[];
  50. {
  51.      int  lineno, except, number, all, numpats, print,
  52.           count, icount, fold;
  53.      char line[MAXLINE], *s, newarg[MAXLINE], foldtemp[MAXLINE],
  54.           pat[MAXPATS][MAXLINE];
  55.  
  56.      dioinit(&argc,argv);
  57.      lineno=0;
  58.      except=number=all=count=fold=FALSE;
  59.      numpats=icount=0;
  60.  
  61.      /* process options */
  62.  
  63.      while(--argc > 0 && (*++argv)[0] == '-')
  64.           for(s=argv[0]+1; *s != '\0'; s++)
  65.                switch(*s)
  66.                     {case 'A':  all=TRUE;     break;
  67.                      case 'N':  number=TRUE;  break;
  68.                      case 'X':  except=TRUE;  break;
  69.                      case 'C':  count=TRUE;   break;
  70.                      case 'F':  fold=TRUE;    break;
  71.                      default:   error("find: invalid option");
  72.                     }
  73.  
  74.      /* process patterns */
  75.  
  76.      if(argc <= 0)  error("find: missing pattern");
  77.  
  78.      while(argc > 0)
  79.           {if(++numpats > MAXPATS)  error("find: too many patterns");
  80.           rcarg(newarg,*argv++);
  81.           if(fold)
  82.                {stoupper(foldtemp,newarg);
  83.                strcpy(newarg,foldtemp);
  84.                }
  85.           if(!getpat(newarg,pat[numpats-1]))
  86.                error("find: illegal pattern");
  87.           argc--;
  88.           }
  89.  
  90.      /* look for patterns in stdin */
  91.  
  92.      while(fgets(line,STDIN))
  93.           {lineno++;
  94.           if(fold)
  95.                stoupper(foldtemp,line);
  96.           else
  97.                strcpy(foldtemp,line);
  98.           if(gmatch(foldtemp,pat,numpats,all))
  99.                print=TRUE;
  100.           else
  101.                print=FALSE;
  102.  
  103.           if(except)  print= !print;
  104.  
  105.           if(print && !count)
  106.                {if(number)  printf("%d: ",lineno);
  107.                printf("%s",line);
  108.                }
  109.           else if(print && count)
  110.                icount++;
  111.           }
  112.  
  113.      if(count)
  114.           printf("%d matched\n",icount);
  115.  
  116.      dioflush();
  117. }
  118.  
  119.  
  120.  
  121. /* gmatch -- match patterns  */
  122. gmatch(line,pat,numpats,all)
  123. char *line, pat[MAXPATS][MAXLINE];
  124. int  numpats, all;
  125. {
  126.      int  gotone, i;
  127.  
  128.      for(i=0; i < numpats; i++)
  129.           {gotone=match(line,pat[i]);
  130.           if(!all && gotone)  return(TRUE);
  131.           else if(all && !gotone)  return(FALSE);
  132.           }
  133.  
  134.      return(all);
  135. }
  136.  
  137.  
  138.  
  139. /*  match -- find match anywhere in line  */
  140. match(lin,pat)
  141. char *lin, *pat;
  142. {
  143.      int  i, pos;
  144.  
  145.      pos= -1;
  146.      i=0;
  147.  
  148.      while(lin[i] != '\0' && pos < 0)
  149.           {pos=amatch(lin,i,pat,0);
  150.           i++;
  151.           }
  152.  
  153.      return(pos >= 0);
  154. }
  155.  
  156.  
  157.  
  158. /* stoupper -- convert string to upper case
  159.      usage: stoupper(outstr,instr);
  160. */
  161. stoupper(s,t)
  162. char *s, *t;
  163. {
  164.      while(*s++ = toupper(*t++))
  165.           ;
  166. }
  167.