;**********************************************
; ADDLF - ADD Line Feed
;
; Adds in a LF to files that only contain CRs
;
; by Dave Heyliger - AMUS Staff
;
; Directions: just type ADDLF and follow 'em!
;**********************************************

	SEARCH SYS				;search the normals
	SEARCH SYSSYM
	SEARCH TRM
	SEARCH AAA				;search AAA.UNV (our macros)

	VMAJOR=1.				;version number 1.0(100)
	VMINOR=0.
	VEDIT=100.				;original by Dave Heyliger

	.OFINI					;define variables
	.OFDEF	IDDB,D.DDB			;Input DDB for input file
	.OFDEF	ODDB,D.DDB			;Output DDB for output file
	.OFSIZ	IMPSIZ

	;start of program - create header
	;		    A4 will constantly point to output DDB
	;		    A5 will constantly point to input DDB
	PHDR	-1,0,PH$REE!PH$REU		;re-entrant, re-usable
	GETIMP	IMPSIZ,A3			;A3 points to variables
	LEA	A5,IDDB(A3)			;A5 points to input file
	LEA	A4,ODDB(A3)			;A4 point to output file

	;give some simple instructions
	PRTTAB	-1,0				;clear screen
	PRTTAB	6,34				;tab to here
	TYPE	<ADD LINEFEED Utility:>		;What this program does
	PRTTAB	10,24				;tab to here
	TYPE	<adds a LF to files that don't have them!>

	;get input file and output file (not intensive error checking)
	PRTTAB	12,20				;tab to here
	TYPE	<Enter in the file needing LFs    : >	;get input filename
	KBD					;A2 will point to the filename
	FSPEC	@A5				;create RAD50 filespec in DDB
	PRTTAB	13,20				;tab here
	TYPE	<Enter in a name for updated file : >	;get output filename
	KBD					;A2 will point to the filename
	FSPEC	@A4				;create RAD50 filespec in DDB

	;find device driver for file I/O and create two 512 block buffers
	INIT	@A5				;initialize both files
	INIT	@A4

	;look for input/output files - give error message if booboo
	LOOKUP	@A5				;if input file not found
	JNE	BOOBOO				;error
	LOOKUP	@A4				;if output file found
	JEQ	BOOBOO				;error

	;open input file, output file. Give fancy screen display!
	OPENI	@A5				;open input file
	OPENO	@A4				;open output file
	PRTTAB	15,1				;tab to here
	TYPE	<Working..>			;gizmo effects!

	;read input file byte-by-byte, write to output file byte-by-byte,
	;but add a LF if you ever hit a CR on the input file
BYB:	FILINB	@A5				;get a byte from input file
	TST	IDDB+D.SIZ(A3)			;eof?
	BEQ	EOF				;yup, go here
	FILOTB	@A4				;nope, write the byte
	CMPB	D1,#15				;was it a CR?
	BNE	BYB				;nope, go get next byte
	MOV	#12,D1				;yup, move in a line feed
	FILOTB	@A4				;and write it out to the file
	TYPE	<.>				;gizmo effects upon each LF
	BR	BYB				;go get another input byte

	;if no input file or output file exists, type error message
BOOBOO:	PRTTAB	15,23				;tab to here
	TYPE	<File spec error or file already exists>	;message
	MOV	#7,D1				;get a bell
	TTY					;beep!
	BR	EXIT				;and quit

	;EOF hit, so close files, finish gizmo effects!
EOF:	CLOSE	@A5				;close input file
	CLOSE	@A4				;close output file
EXIT:	TYPECR	<!>				;"!" - done!
	EXIT					;get out of dodge

	END					;end of code