;
;  PROGRAM:  SYSTEST1
;  AUTHOR:  Richard Conn
;  PURPOSE:  This program creates a file and then accepts lines of text
;               to input into that file.
;  NOTE:  This test illustrates the use of the byte-oriented file
;               output routines and the use of SYSLIB.
;

;  Define the <CR> and <LF> constants
cr      equ     0dh
lf      equ     0ah

;  External Definitions of Routines to be Used
       ext     fname           ; Convert file name into FCB format
       ext     print           ; Print string
       ext     bbline          ; Input Line Editor
       ext     fo0$open        ; Open File for Output
       ext     fo0$close       ; Close File
       ext     f0$put          ; Write Byte to File

;
;  This part of the program prompts the user for input and inputs a line
;
       call    print   ; print prompt to user
       db      'SYSTEST1 - Byte-Oriented File Output Demonstration'
       db      cr,lf,'Name of File to Create? ',0
       xra     a       ; A=0 so BBLINE does not capitalize line
       call    bbline  ; input file name from user
       ora     a       ; check char count for zero
       rz              ; return to CP/M if no line input

;
;  The file name specified by the user is converted into the FCB format
;  and stored into an FCB
;
;  First char of filename is pointed to by HL, as returned by BBLINE
;
       lxi     d,fcb   ; load fcb
       call    fname

;
;  Now we open the file for byte-oriented output; since FNAME does not
;  affect DE, DE still points to the FCB
;
       call    fo0$open        ; open file for output
       jz      loop    ; ok to proceed

;
;  File could not be opened -- print error message and abort
;
       call    print
       db      cr,lf,'Cannot Open File -- Abort',0
       ret

;
;  This loop prompts the user for a line and stores it in the file.
;  If the user types an empty line (just <CR>), we exit and close the
;  output file.
;
loop:
       call    print   ; print prompt
       db      cr,lf,'Input Line (<CR>=Done)? ',0
       xra     a       ; A=0 so BBLINE does not capitalize line
       call    bbline  ; get line from user
       ora     a       ; check char count
       jz      done    ; done if no chars

;
;  This loop writes the string pted to by HL (from BBLINE) to disk.
;
oloop:
       mov     a,m     ; get char
       ora     a       ; done if zero
       jz      odone
       call    f0$put  ; write to disk
       jnz     derr    ; check for disk error
       inx     h       ; pt to next char to output
       jmp     oloop

;
;  This routine terminates the string just written to disk with a
;  <CR> <LF> pair, and the creation of the file is continued.
;
odone:
       mvi     a,cr    ; new line
       call    f0$put
       mvi     a,lf
       call    f0$put
       jmp     loop

;
;  The user has typed an empty line (just <CR>), so we close the file
;  and exit.
;
done:
       call    fo0$close       ; close file
       ret

;
;  Error message and abort if error occurs while writing to disk.
;
derr:
       call    print
       db      cr,lf,'Disk Output Error',0
       ret

;
;  FCB used by program
;
fcb:
       ds      36

       end