      SUBROUTINE MAIN
      INTEGER EXPRST, I, J
      INTEGER GETARG, INDEXC, GETPAT, EQUAL
      INTEGER PAT(132)
      INTEGER FD
      INTEGER OPEN
      LOGICAL*1 ARG(128)
      COMMON / CGREP / COUNT, HEADER, LIST, EXCEPT, CASIND
      INTEGER COUNT
      INTEGER HEADER
      INTEGER LIST
      INTEGER EXCEPT
      INTEGER CASIND
      LOGICAL*1 USESTR(44)
      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)/105/,USESTR(19)/108/,USESTR(20)/120/,USESTR(21)/93/,USESTR(
     *22)/32/,USESTR(23)/101/,USESTR(24)/120/,USESTR(25)/112/,USESTR(26)
     */114/,USESTR(27)/101/,USESTR(28)/115/,USESTR(29)/115/,USESTR(30)/1
     *05/,USESTR(31)/111/,USESTR(32)/110/,USESTR(33)/32/,USESTR(34)/91/,
     *USESTR(35)/102/,USESTR(36)/105/,USESTR(37)/108/,USESTR(38)/101/,US
     *ESTR(39)/93/,USESTR(40)/32/,USESTR(41)/46/,USESTR(42)/46/,USESTR(4
     *3)/46/,USESTR(44)/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
      CASIND = 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.(INDEXC(ARG, 99) .GT. 0))GOTO 23004
      COUNT = 1
23004 CONTINUE
      IF (.NOT.(INDEXC(ARG, 104) .GT. 0))GOTO 23006
      HEADER = 0
23006 CONTINUE
      IF (.NOT.(INDEXC(ARG, 105) .GT. 0))GOTO 23008
      CASIND = 1
23008 CONTINUE
      IF (.NOT.(INDEXC(ARG, 108) .GT. 0))GOTO 23010
      LIST = 1
23010 CONTINUE
      IF (.NOT.(INDEXC(ARG, 120) .GT. 0))GOTO 23012
      EXCEPT = 1
23012 CONTINUE
      GOTO 23003
23002 CONTINUE
      EXPRST = 1
23003 CONTINUE
23001 CONTINUE
      IF (.NOT.(GETARG(EXPRST, ARG, 128) .EQ. -1))GOTO 23014
      CALL ERROR(USESTR)
23014 CONTINUE
      IF (.NOT.( CASIND .EQ. 1 ))GOTO 23016
      CALL FOLD(ARG)
23016 CONTINUE
      IF (.NOT.(GETPAT(ARG, PAT) .EQ. -3))GOTO 23018
      CALL PUTLIN(ILLMSG, 3)
      CALL ERROR(ARG)
23018 CONTINUE
      I=1
      J=EXPRST+1
23020 IF (.NOT.(GETARG(J, ARG, 128) .NE. -1))GOTO 23022
      IF (.NOT.(EQUAL(ARG, MINUST) .EQ. 1))GOTO 23023
      FD = 1
      CALL STRCPY(STDINS, ARG)
      GOTO 23024
23023 CONTINUE
      FD = OPEN(ARG, 1)
      IF (.NOT.(FD .EQ. -3))GOTO 23025
      CALL CANT(ARG)
23025 CONTINUE
23024 CONTINUE
      CALL DOGREP(FD, ARG, PAT)
      IF (.NOT.(FD .NE. 1))GOTO 23027
      CALL CLOSE(FD)
23027 CONTINUE
23021 I=I+1
      J=J+1
      GOTO 23020
23022 CONTINUE
      IF (.NOT.(I .EQ. 1))GOTO 23029
      CALL DOGREP(1, STDINS, PAT)
23029 CONTINUE
      RETURN
      END
      SUBROUTINE DOGREP(FD, FILE, PAT)
      INTEGER FD
      INTEGER MCOUNT, MATCHD, PAT(132)
      INTEGER GETLIN, MATCH
      LOGICAL*1 FILE(40), LINE(512), TMPLIN(512)
      COMMON / CGREP / COUNT, HEADER, LIST, EXCEPT, CASIND
      INTEGER COUNT
      INTEGER HEADER
      INTEGER LIST
      INTEGER EXCEPT
      INTEGER CASIND
      MCOUNT = 0
23031 IF (.NOT.(GETLIN(LINE, FD) .NE. -1))GOTO 23032
      IF (.NOT.( CASIND .EQ. 1 ))GOTO 23033
      CALL STRCPY( LINE, TMPLIN)
      CALL FOLD(TMPLIN)
      MATCHD = MATCH( TMPLIN, PAT)
      GOTO 23034
23033 CONTINUE
      MATCHD = MATCH(LINE, PAT)
23034 CONTINUE
      IF (.NOT.((MATCHD .EQ. 1 .AND. EXCEPT .EQ. 0) .OR. (MATCHD .EQ. 0 
     *.AND. EXCEPT .EQ. 1)))GOTO 23035
      MCOUNT = MCOUNT + 1
      IF (.NOT.(MCOUNT .EQ. 1 .AND. COUNT .EQ. 0))GOTO 23037
      IF (.NOT.(LIST .EQ. 1))GOTO 23039
      CALL PUTLIN(FILE, 2)
      CALL PUTCH(10, 2)
      RETURN
23039 CONTINUE
      IF (.NOT.(HEADER .EQ. 1))GOTO 23041
      CALL PUTCH(10, 2)
      CALL PUTLIN(FILE, 2)
      CALL PUTCH(58, 2)
      CALL PUTCH(10, 2)
23041 CONTINUE
23040 CONTINUE
23037 CONTINUE
      IF (.NOT.(COUNT .EQ. 0))GOTO 23043
      CALL PUTLIN(LINE, 2)
23043 CONTINUE
23035 CONTINUE
      GOTO 23031
23032 CONTINUE
      IF (.NOT.(COUNT .EQ. 1))GOTO 23045
      CALL PUTINT(MCOUNT, 6, 2)
      CALL PUTCH(32, 2)
      CALL PUTLIN(FILE, 2)
      CALL PUTCH(10, 2)
23045 CONTINUE
      RETURN
      END
