C     RENBR(FIXCRLF/CUT FILE INTO EQUAL LENGTH LINES)
C
C     BY DONALD E. BARTH
C
      CHARACTER*1 LTRNOW,LTRONE,LTRTWO,LTRLIN(80),CHAR
      CHARACTER*80 FILINP,FILOUT
      DATA LMTLIN/80/
      DATA ITTY,JTTY,IDISK,JDISK/0,0,1,2/
C
C     IDENTIFY THIS PROGRAM
      WRITE(JTTY,1)
    1 FORMAT(' FIXCRLF'/
     1' Inserts CRLF after fixed length records.'/
     1' Removes nulls and converts FF or lone CR or LF to CRLF.')
C
C     OPEN NEXT INPUT FILE
    2 WRITE(JTTY,3)
    3 FORMAT(' Input file? ',\)
      READ(ITTY,4)FILINP
    4 FORMAT(1A80)
      IF(FILINP.EQ.' ')GO TO 6
      OPEN(UNIT=IDISK,FILE=FILINP,STATUS='OLD',IOSTAT=ICHECK,
     1 FORM='BINARY')
      IF(ICHECK.EQ.0)GO TO 8
      WRITE(JTTY,5)
    5 FORMAT(' Cannot open input file')
      GO TO 2
    6 WRITE(JTTY,7)
    7 FORMAT(' Name of input file must be specified')
      GO TO 2
    8 CONTINUE
C
C     OPEN OUTPUT FILE
    9 WRITE(JTTY,10)
   10 FORMAT(' Output file? ',\)
      READ(ITTY,11)FILOUT
   11 FORMAT(1A80)
      IF(FILOUT.EQ.' ')GO TO 15
      OPEN(UNIT=JDISK,FILE=FILOUT,STATUS='OLD',IOSTAT=ICHECK)
      IF(ICHECK.NE.0)GO TO 17
      CLOSE(UNIT=JDISK)
   12 WRITE(JTTY,13)
   13 FORMAT(' File already exists. Replace it? ',\)
      READ(ITTY,14)LTRNOW
   14 FORMAT(1A1)
      IF(LTRNOW.EQ.'Y')GO TO 17
      IF(LTRNOW.EQ.'y')GO TO 17
      IF(LTRNOW.EQ.'N')GO TO 9
      IF(LTRNOW.EQ.'n')GO TO 9
      GO TO 12
   15 WRITE(JTTY,16)
   16 FORMAT(' Name of output file must be specified')
      GO TO 9
   17 OPEN(UNIT=JDISK,FILE=FILOUT,STATUS='NEW',IOSTAT=ICHECK,
     1 FORM='BINARY')
      IF(ICHECK.EQ.0)GO TO 19
      WRITE(JTTY,18)
   18 FORMAT(' Cannot open output file')
      GO TO 9
   19 CONTINUE
C
C     ASK LENGTH OF LINES
   20 WRITE(JTTY,21)
   21 FORMAT(
     1' Line length not counting CRLF (0 to only change LF to CRLF)? ',
     2\)
      READ(ITTY,22)LTRLIN
   22 FORMAT(9999A1)
C     SUBROUTINE DAHEFT(KONTRL,ITRAIL,IEXTRA,IBUFFR,MAXBFR,
C    1    LOWBFR,KIND  ,ISHIFT,JSHIFT,KSHIFT,LSHIFT,IVALUE,
C    2    VALUE )
      LOWBFR=1
      CALL DAHEFT(0,0,0,LTRLIN,LMTLIN,
     1 LOWBFR,KIND,ISHIFT,JSHIFT,KSHIFT,LSHIFT,IVALUE,
     2 VALUE )
      IF(KIND.NE.3)GO TO 25
      IF(IVALUE.LT.0)GO TO 25
   23 IF(LOWBFR.GT.LMTLIN)GO TO 24
      IF(LTRLIN(LOWBFR).NE.' ')GO TO 25
      LOWBFR=LOWBFR+1
      GO TO 23
   24 GO TO 27
   25 WRITE(JTTY,26)
   26 FORMAT(
     1' Type the length of the lines into',
     2' which the file is to be split.'/
     3' Do not include the trailing CRLF in this line length.'/
     4' Type zero to merely change lone LF characters to CRLF pairs')
      GO TO 20
   27 LNGLIN=IVALUE
C
C     INITIALIZE
C
C     KNTINP = NUMBER OF CHARACTERS READ FROM INPUT FILE
C     KNTOUT = NUMBER OF CHARACTERS WRITTEN TO OUTPUT FILE
C     KNTLIN = NUMBER OF END OF LINES WRITTEN
C     IONLIN = LENGTH OF LINE NOW BEING OUTPUT
C     NEWLIN = NUMBER OF END OF LINES STILL TO BE OUTPUT
C     KNTCR  = NUMBER OF CARRIAGE RETURNS NOT YET OUTPUT
C     KNTLF  = NUMBER OF LINE FEEDS NOT YET OUTPUT
C     MSTLIN = LENGTH OF LONGEST OUTPUT LINE
C
      KNTINP=0
      KNTOUT=0
      KNTLIN=0
      IONLIN=0
      NEWLIN=0
      KNTCR=0
      KNTLF=0
      MSTLIN=0
      KNTNUL=0
C
C     COPY THE FILE, INSERTING END OF LINES AS NEEDED
   28 READ(IDISK,END=33)LTRONE
      KNTINP=KNTINP+1
      KODE=ICHAR(LTRONE)
      IF(KODE.EQ.0)GO TO 40
      IF(KODE.EQ.10)GO TO 31
      IF(KODE.EQ.12)GO TO 31
      IF(KODE.EQ.13)GO TO 32
      IF(NEWLIN.NE.0)GO TO 29
      IF(LNGLIN.LE.0)GO TO 30
      IF(IONLIN.LT.LNGLIN)GO TO 30
C
C     INSERT   CR = 13   LF = 10   AT START OF NEW LINE
   29 LTRTWO=CHAR(13)
      WRITE(JDISK)LTRTWO
      LTRTWO=CHAR(10)
      WRITE(JDISK)LTRTWO
      KNTOUT=KNTOUT+2
      NEWLIN=NEWLIN-1
      KNTLIN=KNTLIN+1
      CALL RUNLIN(KNTLIN,ITTY,JTTY)
      IF(NEWLIN.GT.0)GO TO 29
      KNTCR=0
      KNTLF=0
      NEWLIN=0
      IONLIN=0
C
C     OUTPUT THE SINGLE NEW CHARACTER
   30 KNTOUT=KNTOUT+1
      IONLIN=IONLIN+1
      WRITE(JDISK)LTRONE
      IF(MSTLIN.LT.IONLIN)MSTLIN=IONLIN
      GO TO 28
C
C     LINE FEED (10) OR FORM FEED (12)
   31 KNTLF=KNTLF+1
      NEWLIN=KNTLF
      IF(NEWLIN.LT.KNTCR)NEWLIN=KNTCR
      GO TO 28
C
C     CARRIAGE RETURN (13)
   32 KNTCR=KNTCR+1
      NEWLIN=KNTLF
      IF(NEWLIN.LT.KNTCR)NEWLIN=KNTCR
      GO TO 28
C
C     NULL (0)
   40 KNTNUL=KNTNUL+1
      GO TO 28
C
C     INSERT   CR = 13   LF = 10  AT END OF FILE
   33 IF(NEWLIN.NE.0)GO TO 34
      IF(IONLIN.EQ.0)GO TO 35
   34 LTRTWO=CHAR(13)
      WRITE(JDISK)LTRTWO
      LTRTWO=CHAR(10)
      WRITE(JDISK)LTRTWO
      KNTOUT=KNTOUT+2
      NEWLIN=NEWLIN-1
      KNTLIN=KNTLIN+1
      CALL RUNLIN(KNTLIN,ITTY,JTTY)
      IF(NEWLIN.GT.0)GO TO 34
   35 CONTINUE
C
C     ALL DONE
      WRITE(JTTY,36)KNTINP
      WRITE(JTTY,37)KNTOUT
      WRITE(JTTY,38)KNTLIN
      WRITE(JTTY,39)MSTLIN
      WRITE(JTTY,41)KNTNUL
   36 FORMAT(' ',1I10,' bytes read')
   37 FORMAT(' ',1I10,' bytes written')
   38 FORMAT(' ',1I10,' lines written')
   39 FORMAT(' ',1I10,' length of longest output line')
   41 FORMAT(' ',1I10,' null characters removed')
C
C     ALL DONE
      END
      SUBROUTINE RUNLIN(LINE,ITTY,JTTY)
      IF(LINE.EQ.1)WRITE(JTTY,1)LINE
      IF(LINE.GT.        1.AND.LINE.LT.        10)WRITE(JTTY,2)LINE
      IF(LINE.GE.       10.AND.LINE.LT.       100)WRITE(JTTY,3)LINE
      IF(LINE.GE.      100.AND.LINE.LT.      1000)WRITE(JTTY,4)LINE
      IF(LINE.GE.     1000.AND.LINE.LT.     10000)WRITE(JTTY,5)LINE
      IF(LINE.GE.    10000.AND.LINE.LT.    100000)WRITE(JTTY,6)LINE
      IF(LINE.GE.   100000.AND.LINE.LT.   1000000)WRITE(JTTY,7)LINE
      IF(LINE.GE.  1000000.AND.LINE.LT.  10000000)WRITE(JTTY,8)LINE
      IF(LINE.GE. 10000000.AND.LINE.LT. 100000000)WRITE(JTTY,9)LINE
      IF(LINE.GE.100000000.AND.LINE.LT.1000000000)WRITE(JTTY,10)LINE
C                123456789             1234567890
    1 FORMAT(' ',1I1)
    2 FORMAT('+',1I1)
    3 FORMAT('+',1I2)
    4 FORMAT('+',1I3)
    5 FORMAT('+',1I4)
    6 FORMAT('+',1I5)
    7 FORMAT('+',1I6)
    8 FORMAT('+',1I7)
    9 FORMAT('+',1I8)
   10 FORMAT('+',1I9)
      RETURN
      END
