      SUBROUTINE MAIN
      INTEGER PAGSIZ, LINSIZ, NCOLS, GUTSIZ, LINENO, NLINES, I, J, FD
      INTEGER READLN, CTOI, GETARG, MOD, MAX0, OPEN
      LOGICAL*1 ARG(400)
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      I = 1
23000 IF(.NOT.(I .LE. 1200))GOTO 23002
      LINPTR(I) = 0
23001 I = I + 1
      GOTO 23000
23002 CONTINUE
      COL = 0
      NEXTBF = 1
      PAGSIZ = 55
      LINSIZ = 60
      NCOLS = 2
      GUTSIZ = 8
      FD = -3
      I = 1
23003 IF(.NOT.(GETARG(I, ARG, 400) .NE. -1))GOTO 23005
      IF(.NOT.(ARG(1) .EQ. 63 .AND. ARG(2) .EQ. 0))GOTO 23006
      CALL COLERR
23006 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .NE. 0))GOTO 23008
      CALL COLARG (ARG, PAGSIZ, NCOLS, GUTSIZ, LINSIZ)
      GOTO 23009
23008 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .EQ. 0))GOTO 23010
      CALL DOCOL (PAGSIZ, NCOLS, GUTSIZ, LINSIZ, 1)
      GOTO 23011
23010 CONTINUE
      FD = OPEN(ARG, 1)
      IF(.NOT.(FD .EQ. -3))GOTO 23012
      CALL CANT(ARG)
23012 CONTINUE
      CALL DOCOL(PAGSIZ, NCOLS, GUTSIZ, LINSIZ, FD)
      CALL CLOSE (FD)
23011 CONTINUE
23009 CONTINUE
23004 I = I + 1
      GOTO 23003
23005 CONTINUE
      IF(.NOT.(FD .EQ. -3))GOTO 23014
      CALL DOCOL (PAGSIZ, NCOLS, GUTSIZ, LINSIZ, 1)
23014 CONTINUE
      RETURN
      END
      SUBROUTINE COLARG (ARG, PAGSIZ, NCOLS, GUTSIZ, LINSIZ)
      INTEGER PAGSIZ, NCOLS, GUTSIZ, LINSIZ, J
      INTEGER CTOI
      LOGICAL*1 ARG(100)
      J = 3
      J = CTOI(ARG, J)
      IF(.NOT.(ARG(2) .EQ. 99 .OR. ARG(2) .EQ. 67))GOTO 23016
      NCOLS = J
      IF(.NOT.(NCOLS .LE. 0))GOTO 23018
      CALL ERROR (21Hinvalid column count.)
23018 CONTINUE
      GOTO 23017
23016 CONTINUE
      IF(.NOT.(ARG(2) .EQ. 108 .OR. ARG(2) .EQ. 76))GOTO 23020
      PAGSIZ = J
      IF(.NOT.(PAGSIZ .LE. 0))GOTO 23022
      CALL ERROR (18Hinvalid page size.)
23022 CONTINUE
      GOTO 23021
23020 CONTINUE
      IF(.NOT.( (ARG(2) .EQ. 119 .OR. ARG(2) .EQ. 87) .OR. (ARG(2) .EQ. 
     *115 .OR. ARG(2) .EQ. 115) ))GOTO 23024
      LINSIZ = J
      IF(.NOT.(LINSIZ .LE. 0))GOTO 23026
      CALL ERROR (21Hinvalid column width.)
23026 CONTINUE
      GOTO 23025
23024 CONTINUE
      IF(.NOT.(ARG(2) .EQ. 103 .OR. ARG(2) .EQ. 71))GOTO 23028
      GUTSIZ = J
      IF(.NOT.(GUTSIZ .LT. 0))GOTO 23030
      CALL ERROR (21Hinvalid gutter width.)
23030 CONTINUE
      GOTO 23029
23028 CONTINUE
      IF(.NOT.(ARG(2) .EQ. 100 .OR. ARG(2) .EQ. 68))GOTO 23032
      PAGSIZ = 23
      LINSIZ = 10
      NCOLS = 7
      GUTSIZ = 1
      IF(.NOT.(J .GT. 0))GOTO 23034
      LINSIZ = J
      NCOLS = MAX0(1, 81/(LINSIZ+1))
      IF(.NOT.(NCOLS .GT. 1))GOTO 23036
      GUTSIZ = (79 - (LINSIZ+1)*NCOLS)/(NCOLS - 1)+1
      IF(.NOT.(GUTSIZ .LE. 0))GOTO 23038
      NCOLS = NCOLS - 1
23038 CONTINUE
23036 CONTINUE
23034 CONTINUE
      GOTO 23033
23032 CONTINUE
      CALL REMARK (22Hignoring invalid flag.)
23033 CONTINUE
23029 CONTINUE
23025 CONTINUE
23021 CONTINUE
23017 CONTINUE
      RETURN
      END
      SUBROUTINE COLERR
      CALL ERROR (50Husage: mcol [-cn] [-ln] [-wn] [-gn] [-dn] [file] .)
      RETURN
      END
      SUBROUTINE DOCOL (PAGSIZ, NCOLS, GUTSIZ, LINSIZ, FD)
      INTEGER PAGSIZ, NCOLS, GUTSIZ, LINSIZ, FD, NLINES, LINENO, I
      INTEGER READLN
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      NLINES = PAGSIZ*NCOLS
      IF(.NOT.(NLINES .GT. 1200))GOTO 23040
      CALL ERROR (15Htoo many lines.)
23040 CONTINUE
      LINENO = 1
23042 IF(.NOT.(READLN(I, LINSIZ, FD) .NE. -1))GOTO 23044
      CALL INJECT(I, LINENO)
      IF(.NOT.(LINENO .GE. NLINES))GOTO 23045
      CALL OUTBUF(PAGSIZ, LINSIZ, GUTSIZ)
      LINENO = 0
23045 CONTINUE
23043 LINENO = LINENO + 1
      GOTO 23042
23044 CONTINUE
      IF(.NOT.(LINENO .GT. 1))GOTO 23047
      PAGSIZ = LINENO/NCOLS
      IF(.NOT.(MOD(LINENO, NCOLS) .NE. 0))GOTO 23049
      PAGSIZ = PAGSIZ + 1
23049 CONTINUE
      CALL OUTBUF(PAGSIZ, LINSIZ, GUTSIZ)
23047 CONTINUE
      RETURN
      END
      SUBROUTINE INJECT(PTR, LINENO)
      INTEGER PTR, LINENO
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      IF(.NOT.(LINENO .GT. 1200))GOTO 23051
      CALL ERROR(26Hinsufficient buffer space.)
23051 CONTINUE
      LINPTR(LINENO) = PTR
      RETURN
      END
      SUBROUTINE OUTBUF(PAGSIZ, LINSIZ, GUTSIZ)
      INTEGER PAGSIZ, LINSIZ, GUTSIZ
      INTEGER I, J
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      I = 1
23053 IF(.NOT.(LINPTR(I) .NE. 0))GOTO 23055
      CALL OUTLIN(LINBUF(LINPTR(I)))
      LINPTR(I) = 0
      J = I + PAGSIZ
23056 IF(.NOT.(LINPTR(J) .NE. 0))GOTO 23058
      CALL OUTTAB((LINSIZ + GUTSIZ)*((J - 1)/PAGSIZ))
      CALL OUTLIN(LINBUF(LINPTR(J)))
      LINPTR(J) = 0
23057 J = J + PAGSIZ
      GOTO 23056
23058 CONTINUE
      CALL OUTCH(10)
23054 I = I + 1
      GOTO 23053
23055 CONTINUE
      NEXTBF = 1
      RETURN
      END
      SUBROUTINE OUTCH(C)
      LOGICAL*1 C
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      CALL PUTC(C)
      IF(.NOT.(C .EQ. 10))GOTO 23059
      COL = 0
      GOTO 23060
23059 CONTINUE
      COL = COL + 1
23060 CONTINUE
      RETURN
      END
      SUBROUTINE OUTLIN(STR)
      LOGICAL*1 STR(100)
      INTEGER I
      I = 1
23061 IF(.NOT.(STR(I) .NE. 0))GOTO 23063
      CALL OUTCH(STR(I))
23062 I = I + 1
      GOTO 23061
23063 CONTINUE
      RETURN
      END
      SUBROUTINE OUTTAB(N)
      INTEGER N
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
23064 IF(.NOT.(COL .LT. N))GOTO 23065
      CALL OUTCH(32)
      GOTO 23064
23065 CONTINUE
      RETURN
      END
      INTEGER FUNCTION READLN(P, LINSIZ, FD)
      INTEGER P, LINSIZ, FD
      INTEGER I
      LOGICAL*1 GETCH
      LOGICAL*1 C
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      P = NEXTBF
      I = 1
23066 IF(.NOT.(GETCH(C, FD) .NE. -1))GOTO 23068
      IF(.NOT.(C .EQ. 10))GOTO 23069
      GOTO 23068
23069 CONTINUE
      IF(.NOT.(I .LE. LINSIZ))GOTO 23071
      IF(.NOT.(NEXTBF .GE. 7000))GOTO 23073
      CALL ERROR(26Hinsufficient buffer space.)
23073 CONTINUE
      LINBUF(NEXTBF) = C
      NEXTBF = NEXTBF + 1
23071 CONTINUE
23067 I = I + 1
      GOTO 23066
23068 CONTINUE
      IF(.NOT.(C .EQ. -1 .AND. I .EQ. 1))GOTO 23075
      READLN=(-1)
      RETURN
23075 CONTINUE
      LINBUF(NEXTBF) = 0
      NEXTBF = NEXTBF + 1
      READLN=(I - 1)
      RETURN
      END
