      SUBROUTINE MAIN
      INTEGER GETARG, OPEN
      INTEGER I, FD
      LOGICAL*1 ARG(40)
      CALL QUERY(24Husage:  tsort [file] ...)
      I=1
23000 IF (.NOT.(GETARG(I, ARG, 40) .NE. -1))GOTO 23002
      IF (.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .EQ. 0))GOTO 23003
      FD = 1
      GOTO 23004
23003 CONTINUE
      FD = OPEN(ARG, 1)
      IF (.NOT.(FD .EQ. -3))GOTO 23005
      CALL CANT(ARG)
23005 CONTINUE
23004 CONTINUE
      CALL TPSORT (FD)
      IF (.NOT.(FD .NE. 1))GOTO 23007
      CALL CLOSE(FD)
23007 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      IF (.NOT.(I .EQ. 1))GOTO 23009
      CALL TPSORT (1)
23009 CONTINUE
      RETURN
      END
      SUBROUTINE TPSORT (FD)
      LOGICAL*1 LINBUF(512), SYMBUF(120)
      INTEGER I, J, F, R, N, FD
      INTEGER GETWRD, GETLIN, LOOKS
      COMMON /CTSORT/ HASH(128), NXTSYM, NXTFRE, BUF(5000)
      INTEGER HASH
      INTEGER NXTSYM
      INTEGER NXTFRE
      INTEGER BUF
      NXTSYM = 1
      NXTFRE = 5000
      I = 1
23011 IF (.NOT.(I .LE. 128))GOTO 23013
      HASH(I) = 0
23012 I = I + 1
      GOTO 23011
23013 CONTINUE
23014 IF (.NOT.(GETLIN(LINBUF, FD) .NE. -1))GOTO 23015
      I = 1
      IF (.NOT.(GETWRD(LINBUF, I, SYMBUF) .LE. 0))GOTO 23016
      GOTO 23014
23016 CONTINUE
      J = LOOKS(SYMBUF)
23018 IF (.NOT.(GETWRD(LINBUF, I, SYMBUF) .GT. 0))GOTO 23019
      CALL ENTPRC(J, LOOKS(SYMBUF))
      GOTO 23018
23019 CONTINUE
      GOTO 23014
23015 CONTINUE
      F = 0
      I = 1
23020 IF (.NOT.(I .LT. NXTSYM .AND. F .EQ. 0))GOTO 23022
      IF (.NOT.(BUF(I+1) .EQ. 0))GOTO 23023
      F = I
23023 CONTINUE
23021 I = I + 3
      GOTO 23020
23022 CONTINUE
      R = F
23025 IF (.NOT.(I .LT. NXTSYM))GOTO 23027
      IF (.NOT.(BUF(I+1) .EQ. 0))GOTO 23028
      BUF(R+1) = I
      R = I
23028 CONTINUE
23026 I = I + 3
      GOTO 23025
23027 CONTINUE
      N = NXTSYM
23030 IF (.NOT.(F .GT. 0))GOTO 23032
      CALL ICOPYS (BUF, BUF(F+0), LINBUF, 1)
      CALL PUTLIN(LINBUF, 2)
      CALL PUTCH(10, 2)
      I = BUF(F+2)
23033 IF (.NOT.(I .GT. 0))GOTO 23035
      J = BUF(I+1)
      BUF(J+1) = BUF(J+1) - 1
      IF (.NOT.(BUF(J+1) .EQ. 0))GOTO 23036
      BUF(R+1) = J
      R = J
23036 CONTINUE
23034 I = BUF(I+0)
      GOTO 23033
23035 CONTINUE
      N = N - 3
23031 F = BUF(F+1)
      GOTO 23030
23032 CONTINUE
      IF (.NOT.(N .GT. 1))GOTO 23038
      CALL ERROR(9Hcircular.)
23038 CONTINUE
      RETURN
      END
      SUBROUTINE ENTPRC(A, B)
      INTEGER A, B
      INTEGER P
      INTEGER NALLOC
      COMMON /CTSORT/ HASH(128), NXTSYM, NXTFRE, BUF(5000)
      INTEGER HASH
      INTEGER NXTSYM
      INTEGER NXTFRE
      INTEGER BUF
      BUF(B+1) = BUF(B+1) + 1
      P = NALLOC(2)
      BUF(P+0) = BUF(A+2)
      BUF(P+1) = B
      BUF(A+2) = P
      RETURN
      END
      INTEGER FUNCTION LOOKS(S)
      LOGICAL*1 S(120), LIN(120)
      INTEGER I
      INTEGER LENGTH, NALLOC, EQUAL, SYMALC
      COMMON /CTSORT/ HASH(128), NXTSYM, NXTFRE, BUF(5000)
      INTEGER HASH
      INTEGER NXTSYM
      INTEGER NXTFRE
      INTEGER BUF
      I = HASH(S(1)+1)
23040 IF (.NOT.(I .GT. 0))GOTO 23042
      CALL ICOPYS (BUF, I+2, LIN, 1)
      IF (.NOT.(EQUAL(S, LIN) .EQ. 1))GOTO 23043
      LOOKS=(BUF(I+1))
      RETURN
23043 CONTINUE
23041 I = BUF(I+0)
      GOTO 23040
23042 CONTINUE
      I = NALLOC(2 + 1 + LENGTH(S) + 1)
      BUF(I+0) = HASH(S(1)+1)
      HASH(S(1)+1) = I
      BUF(I+1) = SYMALC(I+2)
      CALL SCOPYI(S, 1, BUF, I + 2)
      LOOKS=(BUF(I+1))
      RETURN
      END
      INTEGER FUNCTION NALLOC(N)
      INTEGER N
      COMMON /CTSORT/ HASH(128), NXTSYM, NXTFRE, BUF(5000)
      INTEGER HASH
      INTEGER NXTSYM
      INTEGER NXTFRE
      INTEGER BUF
      NXTFRE = NXTFRE - N
      IF (.NOT.(NXTFRE .LT. NXTSYM))GOTO 23045
      CALL ERROR(15Hout of storage.)
23045 CONTINUE
      NALLOC=(NXTFRE + 1)
      RETURN
      END
      INTEGER FUNCTION SYMALC(S)
      INTEGER S
      INTEGER P
      COMMON /CTSORT/ HASH(128), NXTSYM, NXTFRE, BUF(5000)
      INTEGER HASH
      INTEGER NXTSYM
      INTEGER NXTFRE
      INTEGER BUF
      P = NXTSYM
      NXTSYM = NXTSYM + 3
      IF (.NOT.(NXTSYM .GT. NXTFRE))GOTO 23047
      CALL ERROR(15Hout of storage.)
23047 CONTINUE
      BUF(P+0) = S
      BUF(P+1) = 0
      BUF(P+2) = 0
      SYMALC=(P)
      RETURN
      END
      SUBROUTINE ICOPYS(FROM, I, TO, J)
      INTEGER FROM(100)
      LOGICAL*1 TO(100)
      INTEGER I, J, K1, K2
      K2 = J
      K1 = I
23049 IF (.NOT.(FROM(K1) .NE. 0))GOTO 23051
      TO(K2) = FROM(K1)
      K2 = K2 + 1
23050 K1 = K1 + 1
      GOTO 23049
23051 CONTINUE
      TO(K2) = 0
      RETURN
      END
      SUBROUTINE SCOPYI(FROM, I, TO, J)
      LOGICAL*1 FROM(100)
      INTEGER TO(100)
      INTEGER I, J, K1, K2
      K2 = J
      K1 = I
23052 IF (.NOT.(FROM(K1) .NE. 0))GOTO 23054
      TO(K2) = FROM(K1)
      K2 = K2 + 1
23053 K1 = K1 + 1
      GOTO 23052
23054 CONTINUE
      TO(K2) = 0
      RETURN
      END
