      SUBROUTINE MAIN
      INTEGER I, GETARG, J, INT, OPEN, K, OPENDR, DESC, GDRPRM
      LOGICAL*1 DOCDIR(40), ARG(40)
      COMMON / CMAN / LSTENT, LSTLOC, ENTPTR(100), ENTRY(2000)
      INTEGER LSTENT
      INTEGER LSTLOC
      INTEGER ENTPTR
      LOGICAL*1 ENTRY
      LSTENT = 0
      LSTLOC = 0
      CALL GETDIR(6, 6, DOCDIR)
      I=1
23000 IF(.NOT.(GETARG(I, ARG, 40) .NE. -1))GOTO 23002
      IF(.NOT.(ARG(1) .EQ. 63 .AND. ARG(2) .EQ. 0))GOTO 23003
      CALL ERROR(26Husage:  man [toolname] ...)
      GOTO 23004
23003 CONTINUE
      CALL STAKIT(ARG)
23004 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      IF(.NOT.(LSTENT .EQ. 0))GOTO 23005
      IF(.NOT.(OPENDR(DOCDIR, DESC) .EQ. -3))GOTO 23007
      CALL ERROR(45HCannot open directory of documentation files.)
23007 CONTINUE
23009 IF(.NOT.(GDRPRM(DESC, ARG) .NE. -1))GOTO 23010
      CALL STAKIT(ARG)
      GOTO 23009
23010 CONTINUE
      CALL CLOSDR(DESC)
      CALL SHELL(ENTPTR, LSTENT, ENTRY)
23005 CONTINUE
      I=1
23011 IF(.NOT.(I .LE. LSTENT))GOTO 23013
      J = 1
      CALL STCOPY(DOCDIR, 1, ARG, J)
      K = ENTPTR(I)
      CALL SCOPY(ENTRY(K), 1, ARG, J)
      INT = OPEN(ARG, 1)
      IF(.NOT.(INT .EQ. -3))GOTO 23014
      CALL PUTLIN(ENTRY(K), 3)
      CALL REMARK(29H : no manual entry available.)
      GOTO 23015
23014 CONTINUE
      CALL FCOPY(INT, 2)
      CALL CLOSE(INT)
23015 CONTINUE
23012 I=I+1
      GOTO 23011
23013 CONTINUE
      RETURN
      END
      SUBROUTINE STAKIT(BUF)
      LOGICAL*1 BUF(100)
      INTEGER NLEN, LENGTH
      COMMON / CMAN / LSTENT, LSTLOC, ENTPTR(100), ENTRY(2000)
      INTEGER LSTENT
      INTEGER LSTLOC
      INTEGER ENTPTR
      LOGICAL*1 ENTRY
      NLEN = LENGTH(BUF) + 1
      IF(.NOT.(LSTLOC + NLEN .GT. 2000 .OR. LSTENT .GE. 100))GOTO 23016
      CALL PUTLIN(BUF, 3)
      CALL REMARK(20H : too many entries.)
      GOTO 23017
23016 CONTINUE
      LSTENT = LSTENT + 1
      ENTPTR(LSTENT) = LSTLOC + 1
      CALL SCOPY(BUF, 1, ENTRY, LSTLOC + 1)
      LSTLOC = LSTLOC + NLEN
23017 CONTINUE
      RETURN
      END
      SUBROUTINE SHELL(LINPTR, NLINES, LINBUF)
      LOGICAL*1 LINBUF(100)
      INTEGER COMPAR
      INTEGER GAP, I, IG, J, K, LINPTR(100), NLINES
      GAP = NLINES / 2
23018 IF(.NOT.(GAP .GT. 0))GOTO 23020
      J = GAP + 1
23021 IF(.NOT.(J .LE. NLINES))GOTO 23023
      I = J - GAP
23024 IF(.NOT.(I .GT. 0))GOTO 23026
      IG = I + GAP
      IF(.NOT.(COMPAR(LINPTR(I), LINPTR(IG), LINBUF) .LE. 0))GOTO 23027
      GOTO 23026
23027 CONTINUE
      CALL EXCHAN(LINPTR(I), LINPTR(IG), LINBUF)
23025 I = I - GAP
      GOTO 23024
23026 CONTINUE
23022 J = J + 1
      GOTO 23021
23023 CONTINUE
23019 GAP = GAP / 2
      GOTO 23018
23020 CONTINUE
      RETURN
      END
      SUBROUTINE EXCHAN(LP1, LP2, LINBUF)
      LOGICAL*1 LINBUF(100)
      INTEGER K, LP1, LP2
      K = LP1
      LP1 = LP2
      LP2 = K
      RETURN
      END
      INTEGER FUNCTION COMPAR(LP1, LP2, LINBUF)
      LOGICAL*1 LINBUF(100)
      INTEGER I, J, LP1, LP2
      I = LP1
      J = LP2
23029 IF(.NOT.(LINBUF(I) .EQ. LINBUF(J)))GOTO 23030
      IF(.NOT.(LINBUF(I) .EQ. 0))GOTO 23031
      COMPAR = 0
      RETURN
23031 CONTINUE
      I = I + 1
      J = J + 1
      GOTO 23029
23030 CONTINUE
      IF(.NOT.(LINBUF(I) .LT. LINBUF(J)))GOTO 23033
      COMPAR = -1
      GOTO 23034
23033 CONTINUE
      COMPAR = +1
23034 CONTINUE
      RETURN
      END
