      SUBROUTINE MAIN
      LOGICAL*1 LIN(400), NEW(400), PAT(128,10)
      LOGICAL*1 ARG(128), FROM(128), TO(128)
      INTEGER ADDSET, AMATCH, GETARG, GETLIN, GETPAT, GETSUB
      INTEGER I, JUNK, K, LASTM, M , INDEX
      INTEGER EXCEPT, ANDPAT, NARG, FRARG, TOARG, NPAT, ITOC, STATUS, GM
     *ATCH
      EXCEPT = 0
      ANDPAT = 0
      NARG = 0
      I=1
23000 IF(.NOT.(GETARG(I, ARG, 128) .NE. -1))GOTO 23002
      IF(.NOT.(ARG(1) .EQ. 63 .AND. ARG(2) .EQ. 0))GOTO 23003
      CALL CHERR
      GOTO 23004
23003 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 45))GOTO 23005
      CALL SCOPY(ARG, 1, LIN, 1)
      CALL FOLD(LIN)
      IF(.NOT.(INDEX(LIN, 97) .GT. 0))GOTO 23007
      ANDPAT = 1
23007 CONTINUE
      IF(.NOT.(INDEX(LIN, 120) .GT. 0))GOTO 23009
      EXCEPT = 1
23009 CONTINUE
      CALL DELARG(I)
      I = I - 1
      GOTO 23006
23005 CONTINUE
      NARG = NARG + 1
23006 CONTINUE
23004 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      IF(.NOT.(NARG .EQ. 0))GOTO 23011
      CALL CHERR
      GOTO 23012
23011 CONTINUE
      IF(.NOT.(NARG .EQ. 1 .OR. NARG .EQ. 2))GOTO 23013
      FRARG = 1
      TOARG = 2
      NPAT = 1
      GOTO 23014
23013 CONTINUE
      TOARG = NARG
      FRARG = NARG - 1
      NPAT = NARG - 2
23014 CONTINUE
23012 CONTINUE
      IF(.NOT.(NPAT .GT. 10))GOTO 23015
      CALL PUTLIN(43HMaximum number of expressions permitted is , 3)
      I = ITOC(10, ARG, 128)
      CALL ERROR(ARG)
23015 CONTINUE
      JUNK = GETARG(FRARG, ARG, 128)
      IF(.NOT.(GETPAT(ARG, FROM) .EQ. -3))GOTO 23017
      CALL ERROR(25Hillegal fromexpr pattern.)
23017 CONTINUE
      IF(.NOT.(GETARG(TOARG, ARG, 128) .EQ. -1))GOTO 23019
      ARG(1) = 0
23019 CONTINUE
      IF(.NOT.(GETSUB(ARG, TO) .EQ. -3))GOTO 23021
      CALL ERROR(15Hillegal toexpr.)
23021 CONTINUE
      I=1
23023 IF(.NOT.(I .LE. NPAT))GOTO 23025
      JUNK = GETARG(I, ARG, 128)
      IF(.NOT.(GETPAT(ARG, PAT(1,I)) .EQ. -3))GOTO 23026
      CALL PUTLIN(17Hillegal pattern: , 3)
      CALL ERROR(ARG)
23026 CONTINUE
23024 I=I+1
      GOTO 23023
23025 CONTINUE
23028 IF(.NOT.(GETLIN(LIN, 1) .NE. -1))GOTO 23029
      STATUS = GMATCH(LIN, PAT, NPAT, ANDPAT)
      IF(.NOT.((STATUS .EQ. 1 .AND. EXCEPT .EQ. 0) .OR. (STATUS .EQ. 0 .
     *AND. EXCEPT .EQ. 1)))GOTO 23030
      K = 1
      LASTM = 0
      I =1
23032 IF(.NOT.(LIN(I) .NE. 0))GOTO 23034
      M = AMATCH(LIN, I, FROM)
      IF(.NOT.(M .GT. 0 .AND. LASTM .NE. M))GOTO 23035
      CALL CATSUB(LIN, I, M, TO, NEW, K, 400)
      LASTM = M
23035 CONTINUE
      IF(.NOT.(M .EQ. 0 .OR. M .EQ. I))GOTO 23037
      JUNK = ADDSET(LIN(I), NEW, K, 400)
      I = I + 1
      GOTO 23038
23037 CONTINUE
      I = M
23038 CONTINUE
23033 GOTO 23032
23034 CONTINUE
      IF(.NOT.(ADDSET(0, NEW, K, 400) .EQ. 0))GOTO 23039
      K = 400
      JUNK = ADDSET(0, NEW, K, 400)
      CALL REMARK(16Hline truncated:.)
      CALL PUTLIN(NEW, 3)
      CALL PUTCH(10, 3)
23039 CONTINUE
      CALL PUTLIN(NEW, 2)
      GOTO 23031
23030 CONTINUE
      CALL PUTLIN(LIN, 2)
23031 CONTINUE
      GOTO 23028
23029 CONTINUE
      RETURN
      END
      SUBROUTINE CATSUB(LIN, FROM, TO, SUB, NEW, K, MAXNEW)
      INTEGER ADDSET
      INTEGER FROM, I, J, JUNK, K, MAXNEW, TO
      LOGICAL*1 LIN(400), NEW(MAXNEW), SUB(128)
      I = 1
23041 IF(.NOT.(SUB(I) .NE. 0))GOTO 23043
      IF(.NOT.(SUB(I) .EQ. (-3)))GOTO 23044
      J = FROM
23046 IF(.NOT.(J .LT. TO))GOTO 23048
      JUNK = ADDSET(LIN(J), NEW, K, MAXNEW)
23047 J = J + 1
      GOTO 23046
23048 CONTINUE
      GOTO 23045
23044 CONTINUE
      JUNK = ADDSET(SUB(I), NEW, K, MAXNEW)
23045 CONTINUE
23042 I = I + 1
      GOTO 23041
23043 CONTINUE
      RETURN
      END
      INTEGER FUNCTION GETSUB(ARG, SUB)
      LOGICAL*1 ARG(128), SUB(128)
      INTEGER MAKSUB
      GETSUB = MAKSUB(ARG, 1, 0, SUB)
      RETURN
      END
      INTEGER FUNCTION MAKSUB(ARG, FROM, DELIM, SUB)
      LOGICAL*1 ESC
      LOGICAL*1 ARG(128), DELIM, SUB(128)
      INTEGER ADDSET
      INTEGER FROM, I, J, JUNK
      J = 1
      I = FROM
23049 IF(.NOT.(ARG(I) .NE. DELIM .AND. ARG(I) .NE. 0))GOTO 23051
      IF(.NOT.(ARG(I) .EQ. 38))GOTO 23052
      JUNK = ADDSET((-3), SUB, J, 128)
      GOTO 23053
23052 CONTINUE
      JUNK = ADDSET(ESC(ARG, I), SUB, J, 128)
23053 CONTINUE
23050 I = I + 1
      GOTO 23049
23051 CONTINUE
      IF(.NOT.(ARG(I) .NE. DELIM))GOTO 23054
      MAKSUB = -3
      GOTO 23055
23054 CONTINUE
      IF(.NOT.(ADDSET(0, SUB, J, 128) .EQ. 0))GOTO 23056
      MAKSUB = -3
      GOTO 23057
23056 CONTINUE
      MAKSUB = I
23057 CONTINUE
23055 CONTINUE
      RETURN
      END
      SUBROUTINE CHERR
      CALL ERROR(43Husage:  ch [-ax] [expression ...] from [to])
      RETURN
      END
