      SUBROUTINE MAIN
      LOGICAL*1 EXP(128,10), LIN(512), ARG(128), TMPLIN(512)
      INTEGER PAT(132, 10)
      INTEGER I, GETARG, EXCEPT, ANDPAT, COUNT, ELEVEL, ITOC, GETPAT, MC
     *OUNT, GETLIN, MATCHD, STATUS, GMATCH, INDEXC, CASIND
      LOGICAL*1 USESTR(49)
      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
     *02/,USESTR(10)/105/,USESTR(11)/110/,USESTR(12)/100/,USESTR(13)/32/
     *,USESTR(14)/91/,USESTR(15)/45/,USESTR(16)/97/,USESTR(17)/99/,USEST
     *R(18)/105/,USESTR(19)/120/,USESTR(20)/93/,USESTR(21)/32/,USESTR(22
     *)/101/,USESTR(23)/120/,USESTR(24)/112/,USESTR(25)/114/,USESTR(26)/
     *101/,USESTR(27)/115/,USESTR(28)/115/,USESTR(29)/105/,USESTR(30)/11
     *1/,USESTR(31)/110/,USESTR(32)/32/,USESTR(33)/91/,USESTR(34)/101/,U
     *SESTR(35)/120/,USESTR(36)/112/,USESTR(37)/114/,USESTR(38)/101/,USE
     *STR(39)/115/,USESTR(40)/115/,USESTR(41)/105/,USESTR(42)/111/,USEST
     *R(43)/110/,USESTR(44)/93/,USESTR(45)/32/,USESTR(46)/46/,USESTR(47)
     */46/,USESTR(48)/46/,USESTR(49)/0/
      DATA EXCEPT/0/
      DATA ANDPAT/0/
      DATA COUNT /0/
      DATA CASIND /0/
      DATA ELEVEL/0/
      CALL QUERY(USESTR)
      I=1
23000 IF (.NOT.(GETARG(I, ARG, 128) .NE. -1))GOTO 23002
      IF (.NOT.(ARG(1) .EQ. 45))GOTO 23003
      CALL STRCPY( ARG, LIN)
      CALL FOLD(LIN)
      IF (.NOT.(INDEXC(LIN, 97) .GT. 0))GOTO 23005
      ANDPAT = 1
23005 CONTINUE
      IF (.NOT.(INDEXC(LIN, 99) .GT. 0))GOTO 23007
      COUNT = 1
23007 CONTINUE
      IF (.NOT.(INDEXC(LIN, 105) .GT. 0 ))GOTO 23009
      CASIND = 1
23009 CONTINUE
      IF (.NOT.(INDEXC(LIN, 120) .GT. 0))GOTO 23011
      EXCEPT = 1
23011 CONTINUE
      GOTO 23004
23003 CONTINUE
      IF (.NOT.(ELEVEL .LT. 10))GOTO 23013
      ELEVEL = ELEVEL + 1
      IF (.NOT.( CASIND .EQ. 1 ))GOTO 23015
      CALL FOLD(ARG)
23015 CONTINUE
      CALL STRCPY( ARG, EXP(1, ELEVEL))
      GOTO 23014
23013 CONTINUE
      CALL PUTLIN(43HMaximum number of expressions permitted is , 3)
      STATUS = ITOC(10, ARG, 128)
      CALL ERROR(ARG)
23014 CONTINUE
23004 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      IF (.NOT.(ELEVEL .EQ. 0))GOTO 23017
      CALL ERROR(USESTR)
23017 CONTINUE
      I=1
23019 IF (.NOT.(I .LE. ELEVEL))GOTO 23021
      IF (.NOT.(GETPAT(EXP(1,I), PAT(1,I)) .EQ. -3))GOTO 23022
      CALL PUTLIN(17Hillegal pattern: , 3)
      CALL ERROR(EXP(1,I))
23022 CONTINUE
23020 I=I+1
      GOTO 23019
23021 CONTINUE
      MCOUNT = 0
23024 IF (.NOT.(GETLIN(LIN, 1) .NE. -1))GOTO 23025
      IF (.NOT.( CASIND .EQ. 0 ))GOTO 23026
      MATCHD = GMATCH(LIN, PAT, ELEVEL, ANDPAT)
      GOTO 23027
23026 CONTINUE
      CALL STRCPY( LIN, TMPLIN)
      CALL FOLD(TMPLIN)
      MATCHD = GMATCH( TMPLIN, PAT, ELEVEL, ANDPAT)
23027 CONTINUE
      IF (.NOT.((MATCHD .EQ. 1 .AND. EXCEPT .EQ. 0) .OR. (MATCHD .EQ. 0 
     *.AND. EXCEPT .EQ. 1)))GOTO 23028
      IF (.NOT.(COUNT .EQ. 1))GOTO 23030
      MCOUNT = MCOUNT + 1
      GOTO 23031
23030 CONTINUE
      CALL PUTLIN(LIN, 2)
23031 CONTINUE
23028 CONTINUE
      GOTO 23024
23025 CONTINUE
      IF (.NOT.(COUNT .EQ. 1))GOTO 23032
      CALL PUTINT(MCOUNT, 1,2)
      CALL PUTCH(10,2)
23032 CONTINUE
      RETURN
      END
      INTEGER FUNCTION GMATCH(LIN, PAT, ELEVEL, ANDPAT)
      INTEGER ELEVEL, ANDPAT, MATCH, I, STATUS
      LOGICAL*1 LIN(100)
      INTEGER PAT(132, 10)
      GMATCH = ANDPAT
      I=1
23034 IF (.NOT.(I .LE. ELEVEL))GOTO 23036
      STATUS = MATCH(LIN, PAT(1,I))
      IF (.NOT.(ANDPAT .EQ. 0 .AND. STATUS .EQ. 1))GOTO 23037
      GMATCH = 1
      GOTO 23036
23037 CONTINUE
      IF (.NOT.(ANDPAT .EQ. 1 .AND. STATUS .EQ. 0))GOTO 23039
      GMATCH = 0
      GOTO 23036
23039 CONTINUE
23038 CONTINUE
23035 I=I+1
      GOTO 23034
23036 CONTINUE
      RETURN
      END
