      SUBROUTINE MAIN
      INTEGER EXPRST, I, J
      INTEGER GETARG, INDEX, GETPAT, EQUAL
      INTEGER FD
      INTEGER OPEN
      LOGICAL*1 ARG(128), PAT(128)
      COMMON / CGREP / COUNT, HEADER, LIST, EXCEPT
      INTEGER COUNT
      INTEGER HEADER
      INTEGER LIST
      INTEGER EXCEPT
      LOGICAL*1 USESTR(43)
      LOGICAL*1 ILLMSG(18)
      LOGICAL*1 MINUST(2)
      LOGICAL*1 STDINS(6)
      DATA USESTR(1)/117/,USESTR(2)/115/,USESTR(3)/97/,USESTR(4)/103/,US
     *ESTR(5)/101/,USESTR(6)/58/,USESTR(7)/32/,USESTR(8)/32/,USESTR(9)/1
     *03/,USESTR(10)/114/,USESTR(11)/101/,USESTR(12)/112/,USESTR(13)/32/
     *,USESTR(14)/91/,USESTR(15)/45/,USESTR(16)/99/,USESTR(17)/104/,USES
     *TR(18)/108/,USESTR(19)/120/,USESTR(20)/93/,USESTR(21)/32/,USESTR(2
     *2)/101/,USESTR(23)/120/,USESTR(24)/112/,USESTR(25)/114/,USESTR(26)
     */101/,USESTR(27)/115/,USESTR(28)/115/,USESTR(29)/105/,USESTR(30)/1
     *11/,USESTR(31)/110/,USESTR(32)/32/,USESTR(33)/91/,USESTR(34)/102/,
     *USESTR(35)/105/,USESTR(36)/108/,USESTR(37)/101/,USESTR(38)/32/,USE
     *STR(39)/46/,USESTR(40)/46/,USESTR(41)/46/,USESTR(42)/93/,USESTR(43
     *)/0/
      DATA ILLMSG(1)/105/,ILLMSG(2)/108/,ILLMSG(3)/108/,ILLMSG(4)/101/,I
     *LLMSG(5)/103/,ILLMSG(6)/97/,ILLMSG(7)/108/,ILLMSG(8)/32/,ILLMSG(9)
     */112/,ILLMSG(10)/97/,ILLMSG(11)/116/,ILLMSG(12)/116/,ILLMSG(13)/10
     *1/,ILLMSG(14)/114/,ILLMSG(15)/110/,ILLMSG(16)/58/,ILLMSG(17)/32/,I
     *LLMSG(18)/0/
      DATA MINUST(1)/45/,MINUST(2)/0/
      DATA STDINS(1)/115/,STDINS(2)/116/,STDINS(3)/100/,STDINS(4)/105/,S
     *TDINS(5)/110/,STDINS(6)/0/
      CALL QUERY(USESTR)
      COUNT = 0
      HEADER = 1
      LIST = 0
      EXCEPT = 0
      IF (.NOT.(GETARG(1, ARG, 128) .EQ. -1))GOTO 23000
      CALL ERROR(USESTR)
      GOTO 23001
23000 CONTINUE
      IF (.NOT.(ARG(1) .EQ. 45))GOTO 23002
      EXPRST = 2
      CALL FOLD(ARG)
      IF (.NOT.(INDEX(ARG, 99) .GT. 0))GOTO 23004
      COUNT = 1
23004 CONTINUE
      IF (.NOT.(INDEX(ARG, 104) .GT. 0))GOTO 23006
      HEADER = 0
23006 CONTINUE
      IF (.NOT.(INDEX(ARG, 108) .GT. 0))GOTO 23008
      LIST = 1
23008 CONTINUE
      IF (.NOT.(INDEX(ARG, 120) .GT. 0))GOTO 23010
      EXCEPT = 1
23010 CONTINUE
      GOTO 23003
23002 CONTINUE
      EXPRST = 1
23003 CONTINUE
23001 CONTINUE
      IF (.NOT.(GETARG(EXPRST, ARG, 128) .EQ. -1))GOTO 23012
      CALL ERROR(USESTR)
      GOTO 23013
23012 CONTINUE
      IF (.NOT.(GETPAT(ARG, PAT) .EQ. -3))GOTO 23014
      CALL PUTLIN(ILLMSG, 3)
      CALL ERROR(ARG)
23014 CONTINUE
23013 CONTINUE
      I=1
      J=EXPRST+1
23016 IF (.NOT.(GETARG(J, ARG, 128) .NE. -1))GOTO 23018
      IF (.NOT.(EQUAL(ARG, MINUST) .EQ. 1))GOTO 23019
      FD = 1
      CALL STRCPY(STDINS, ARG)
      GOTO 23020
23019 CONTINUE
      FD = OPEN(ARG, 1)
      IF (.NOT.(FD .EQ. -3))GOTO 23021
      CALL CANT(ARG)
23021 CONTINUE
23020 CONTINUE
      CALL DOGREP(FD, ARG, PAT)
      IF (.NOT.(FD .NE. 1))GOTO 23023
      CALL CLOSE(FD)
23023 CONTINUE
23017 I=I+1
      J=J+1
      GOTO 23016
23018 CONTINUE
      IF (.NOT.(I .EQ. 1))GOTO 23025
      CALL DOGREP(1, STDINS, PAT)
23025 CONTINUE
      RETURN
      END
      SUBROUTINE DOGREP(FD, FILE, PAT)
      INTEGER FD
      INTEGER MCOUNT, MATCHD
      INTEGER GETLIN, MATCH
      LOGICAL*1 FILE(40), PAT(128), LINE(512)
      COMMON / CGREP / COUNT, HEADER, LIST, EXCEPT
      INTEGER COUNT
      INTEGER HEADER
      INTEGER LIST
      INTEGER EXCEPT
      MCOUNT = 0
23027 IF (.NOT.(GETLIN(LINE, FD) .NE. -1))GOTO 23028
      MATCHD = MATCH(LINE, PAT)
      IF (.NOT.((MATCHD .EQ. 1 .AND. EXCEPT .EQ. 0) .OR. (MATCHD .EQ. 0 
     *.AND. EXCEPT .EQ. 1)))GOTO 23029
      MCOUNT = MCOUNT + 1
      IF (.NOT.(MCOUNT .EQ. 1 .AND. COUNT .EQ. 0))GOTO 23031
      IF (.NOT.(LIST .EQ. 1))GOTO 23033
      CALL PUTLIN(FILE, 2)
      CALL PUTCH(10, 2)
      RETURN
23033 CONTINUE
      IF (.NOT.(HEADER .EQ. 1))GOTO 23035
      CALL PUTCH(10, 2)
      CALL PUTLIN(FILE, 2)
      CALL PUTCH(58, 2)
      CALL PUTCH(10, 2)
23035 CONTINUE
23034 CONTINUE
23031 CONTINUE
      IF (.NOT.(COUNT .EQ. 0))GOTO 23037
      CALL PUTLIN(LINE, 2)
23037 CONTINUE
23029 CONTINUE
      GOTO 23027
23028 CONTINUE
      IF (.NOT.(COUNT .EQ. 1))GOTO 23039
      CALL PUTINT(MCOUNT, 6, 2)
      CALL PUTCH(32, 2)
      CALL PUTLIN(FILE, 2)
      CALL PUTCH(10, 2)
23039 CONTINUE
      RETURN
      END
