      SUBROUTINE MAIN
      INTEGER PAGSIZ, LINSIZ, NCOLS, GUTSIZ, LINENO, NLINES, I, J, FD
      INTEGER READLN, CTOI, GETARG, MOD, MAX0, OPEN
      LOGICAL*1 ARG(512)
      COMMON /CCOL/ COL, NEXTBF, LINBUF(7000), LINPTR(1200)
      INTEGER COL
      INTEGER NEXTBF
      LOGICAL*1 LINBUF
      INTEGER LINPTR
      LOGICAL*1 USESTR(54)
      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
     *09/,USESTR(10)/99/,USESTR(11)/111/,USESTR(12)/108/,USESTR(13)/32/,
     *USESTR(14)/91/,USESTR(15)/45/,USESTR(16)/99/,USESTR(17)/110/,USEST
     *R(18)/93/,USESTR(19)/32/,USESTR(20)/91/,USESTR(21)/45/,USESTR(22)/
     *108/,USESTR(23)/110/,USESTR(24)/93/,USESTR(25)/32/,USESTR(26)/91/,
     *USESTR(27)/45/,USESTR(28)/119/,USESTR(29)/110/,USESTR(30)/93/,USES
     *TR(31)/32/,USESTR(32)/91/,USESTR(33)/45/,USESTR(34)/103/,USESTR(35
     *)/110/,USESTR(36)/93/,USESTR(37)/32/,USESTR(38)/91/,USESTR(39)/45/
     *,USESTR(40)/100/,USESTR(41)/110/,USESTR(42)/93/,USESTR(43)/32/,USE
     *STR(44)/91/,USESTR(45)/102/,USESTR(46)/105/,USESTR(47)/108/,USESTR
     *(48)/101/,USESTR(49)/93/,USESTR(50)/32/,USESTR(51)/46/,USESTR(52)/
     *46/,USESTR(53)/46/,USESTR(54)/0/
      CALL QUERY(USESTR)
      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, 512) .NE. -1))GOTO 23005
      IF (.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .NE. 0))GOTO 23006
      CALL COLARG (ARG, PAGSIZ, NCOLS, GUTSIZ, LINSIZ)
      GOTO 23007
23006 CONTINUE
      IF (.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .EQ. 0))GOTO 23008
      CALL DOCOL (PAGSIZ, NCOLS, GUTSIZ, LINSIZ, 1)
      GOTO 23009
23008 CONTINUE
      FD = OPEN(ARG, 1)
      IF (.NOT.(FD .EQ. -3))GOTO 23010
      CALL CANT(ARG)
23010 CONTINUE
      CALL DOCOL(PAGSIZ, NCOLS, GUTSIZ, LINSIZ, FD)
      CALL CLOSE (FD)
23009 CONTINUE
23007 CONTINUE
23004 I = I + 1
      GOTO 23003
23005 CONTINUE
      IF (.NOT.(FD .EQ. -3))GOTO 23012
      CALL DOCOL (PAGSIZ, NCOLS, GUTSIZ, LINSIZ, 1)
23012 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 23014
      NCOLS = J
      IF (.NOT.(NCOLS .LE. 0))GOTO 23016
      CALL ERROR (21Hinvalid column count.)
23016 CONTINUE
      GOTO 23015
23014 CONTINUE
      IF (.NOT.(ARG(2) .EQ. 108 .OR. ARG(2) .EQ. 76))GOTO 23018
      PAGSIZ = J
      IF (.NOT.(PAGSIZ .LE. 0))GOTO 23020
      CALL ERROR (18Hinvalid page size.)
23020 CONTINUE
      GOTO 23019
23018 CONTINUE
      IF (.NOT.( (ARG(2) .EQ. 119 .OR. ARG(2) .EQ. 87) .OR. (ARG(2) .EQ.
     * 115 .OR. ARG(2) .EQ. 115) ))GOTO 23022
      LINSIZ = J
      IF (.NOT.(LINSIZ .LE. 0))GOTO 23024
      CALL ERROR (21Hinvalid column width.)
23024 CONTINUE
      GOTO 23023
23022 CONTINUE
      IF (.NOT.(ARG(2) .EQ. 103 .OR. ARG(2) .EQ. 71))GOTO 23026
      GUTSIZ = J
      IF (.NOT.(GUTSIZ .LT. 0))GOTO 23028
      CALL ERROR (21Hinvalid gutter width.)
23028 CONTINUE
      GOTO 23027
23026 CONTINUE
      IF (.NOT.(ARG(2) .EQ. 100 .OR. ARG(2) .EQ. 68))GOTO 23030
      PAGSIZ = 23
      LINSIZ = 10
      NCOLS = 7
      GUTSIZ = 1
      IF (.NOT.(J .GT. 0))GOTO 23032
      LINSIZ = J
      NCOLS = MAX0(1, 81/(LINSIZ+1))
      IF (.NOT.(NCOLS .GT. 1))GOTO 23034
      GUTSIZ = (79 - (LINSIZ+1)*NCOLS)/(NCOLS - 1)+1
      IF (.NOT.(GUTSIZ .LE. 0))GOTO 23036
      NCOLS = NCOLS - 1
23036 CONTINUE
23034 CONTINUE
23032 CONTINUE
      GOTO 23031
23030 CONTINUE
      CALL REMARK (22Hignoring invalid flag.)
23031 CONTINUE
23027 CONTINUE
23023 CONTINUE
23019 CONTINUE
23015 CONTINUE
      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 23038
      CALL ERROR (15Htoo many lines.)
23038 CONTINUE
      LINENO = 1
23040 IF (.NOT.(READLN(I, LINSIZ, FD) .NE. -1))GOTO 23042
      CALL INJECT(I, LINENO)
      IF (.NOT.(LINENO .GE. NLINES))GOTO 23043
      CALL OUTBUF(PAGSIZ, LINSIZ, GUTSIZ)
      LINENO = 0
23043 CONTINUE
23041 LINENO = LINENO + 1
      GOTO 23040
23042 CONTINUE
      IF (.NOT.(LINENO .GT. 1))GOTO 23045
      PAGSIZ = LINENO/NCOLS
      IF (.NOT.(MOD(LINENO, NCOLS) .NE. 0))GOTO 23047
      PAGSIZ = PAGSIZ + 1
23047 CONTINUE
      CALL OUTBUF(PAGSIZ, LINSIZ, GUTSIZ)
23045 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 23049
      CALL ERROR(26Hinsufficient buffer space.)
23049 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
23051 IF (.NOT.(LINPTR(I) .NE. 0))GOTO 23053
      CALL OUTLIN(LINBUF(LINPTR(I)))
      LINPTR(I) = 0
      J = I + PAGSIZ
23054 IF (.NOT.(LINPTR(J) .NE. 0))GOTO 23056
      CALL OUTTAB((LINSIZ + GUTSIZ)*((J - 1)/PAGSIZ))
      CALL OUTLIN(LINBUF(LINPTR(J)))
      LINPTR(J) = 0
23055 J = J + PAGSIZ
      GOTO 23054
23056 CONTINUE
      CALL OUTCH(10)
23052 I = I + 1
      GOTO 23051
23053 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 PUTCH(C,2)
      IF (.NOT.(C .EQ. 10))GOTO 23057
      COL = 0
      GOTO 23058
23057 CONTINUE
      COL = COL + 1
23058 CONTINUE
      RETURN
      END
      SUBROUTINE OUTLIN(STR)
      LOGICAL*1 STR(100)
      INTEGER I
      I = 1
23059 IF (.NOT.(STR(I) .NE. 0))GOTO 23061
      CALL OUTCH(STR(I))
23060 I = I + 1
      GOTO 23059
23061 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
23062 IF (.NOT.(COL .LT. N))GOTO 23063
      CALL OUTCH(32)
      GOTO 23062
23063 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
23064 IF (.NOT.(GETCH(C, FD) .NE. -1))GOTO 23066
      IF (.NOT.(C .EQ. 10))GOTO 23067
      GOTO 23066
23067 CONTINUE
      IF (.NOT.(I .LE. LINSIZ))GOTO 23069
      IF (.NOT.(NEXTBF .GE. 7000))GOTO 23071
      CALL ERROR(26Hinsufficient buffer space.)
23071 CONTINUE
      LINBUF(NEXTBF) = C
      NEXTBF = NEXTBF + 1
23069 CONTINUE
23065 I = I + 1
      GOTO 23064
23066 CONTINUE
      IF (.NOT.(C .EQ. -1 .AND. I .EQ. 1))GOTO 23073
      READLN=(-1)
      RETURN
23073 CONTINUE
      LINBUF(NEXTBF) = 0
      NEXTBF = NEXTBF + 1
      READLN=(I - 1)
      RETURN
      END
