; 18 aug 85 esj replaces s.e.fm.error.uil with kerdef
* 18 jly 85 ESJ CREATED FOR kermit
*
*-----------------------------  text-file-io  -------------------------
   E:F A:S(NWLS , STKARG)
*-----------------------------------------------------------------------
*
* PURPOSE
*
* INPUT
   I'R FD(*)            ; file descriptor block
*
* OUTPUT
*)
*
* GLOBALS DEFINITIONS
*  none
*
* LOCAL DEFINITIONS
   I'R NEWNAME(42)      ; dest name field
   I'R PACKNAME(42)     ; temp for packed filename
   I'R OPENMODE         ; open mode temp
   I'R TEMPMODE         ; open mode temp
   I'R OTSPC(9)         ;
   I'R STATUS(1)        ; FM error code
   I'R WRSPEC(3)        ; inspecs field for write call


* SUBROUTINES CALLED
*
* INSERTS
/INCLUDE BYU.PROG.KERMIT.SYM.EQU.KERDEF
/INCLUDE SYM.EQU.FM.OPEN
/INCLUDE SYM.EQU.FM.BASIC
/INCLUDE SYM.EQU.FM.READ

*--- put equates for the FD block here till ready to move to sym file---

      EQU   FD&CHAN = 0          ; channel number of the file once it's
                                 ; open

      EQU   FD&MAX  = 1          ; maximum length of the text record which
                                 ; can be handled by this FD. It is also
                                 ; the size of the FD&TXTB field.

      EQU   FD&NDX  = 2          ; index for next char to be read from TXTB

      EQU   FD&MODE = 3          ; mode of how the file was opened

      EQU   FD&LNPOS= 4          ; number of current line being read. 0 based

      EQU   FD&TXTC = 5          ; text count. The length of the text record
                                 ; currently in this FD

      EQU   FD&TXTB = FD&TXTC+1  ; text buffer

*-----------------------------------------------------------------------
*
* STATIC DATA INITIALIZATION
      V'S TXTSPEC =  03,
1                    %CATLOG,%%SEARCH,
1                    %FORMAT , %%TEXT,
1                    %RTNERR

      V'S BCDNAME = 4,$&BCD$
*
* RESTRICTIONS
*
* ERROR INDICATIONS
*
* METHOD
*
*
*---------------------------- START OF CODE ----------------------------
*  HOW TO USE
*     opentext is part of a simplified interface for manipulating
*     text files with the new file manager.  opentext is used to
*     open a text file for stream io.
*     opentext takes four arguments:
*     1) FD is a buffer that contains information about the file and
*        the record being manipulated.  the information in FD is to be
*        hidden from the user so that information can be added,deleted
*        or changed without the need to support existing structures.
*        The size of FD is defined by FD&SIZE. The buffer will need to
*        be created by an ALLOC call because the buffer is larger than a
*        local data section.
*     2) FILENAME is a string in kermit or cv format.  opentext will
*        make a copy of the file name and add an &bcd level to the name
*        and stick an ! on the end, if needed converting the file name
*        to a proper text file name.
*     3) TEXTMODE is a two character descripter for the open mode for
*        a text file.  The only modes supported are, RD for read,
*        MO for modify, and SU for supercede.
*
*     4) NAMETYPE defines the type of string in filename.  CV is a cv type
*        of string.  KM is a kermit type of string.
*
      E'O OPENTEXT.( FILENAME, TEXTMODE, NAMETYPE, FD)

#     E'E TYPE.(2,NAMETYPE)
      ; convert filename to &bcd type and put ! on end
      W'R NAMETYPE .E. $KM$
#        E'E HEXDMPP.(1,41,1,FILENAME)
         E'E PACK.(FILENAME, PACKNAME )
#        E'E HEXDMP.(2,42,2,PACKNAME)
         E'E FMEXPNM.(PACKNAME,BCDNAME,NEWNAME)
      O'E
         E'E FMEXPNM.(FILENAME,BCDNAME,NEWNAME)
      E'L


      W'R LDCHRT.(NEWNAME(1),NEWNAME-1) .NE. $!$
         E'E STCHRT.($!$,NEWNAME(1),NEWNAME)
         NEWNAME(0) = NEWNAME(0) + 1
      E'L

#     E'E TYPEMSG.($THIS SHOULD BE THE &BCD NAME!$)
#     E'E HEXDMP.(0,NEWNAME,0,NEWNAME)

      ; convert textmode to open file mode
      ; convert char string to cv char type and lower case
      TEMPMODE = TEXTMODE .LOR. 'A0A0'

#     E'E HEXDMP.(TEMPMODE,TEMPMODE,TEMPMODE,TEMPMODE)
      W'R TEMPMODE .E. $rd$
         ; read mode
         OPENMODE = %OREAD
*
      O'R TEMPMODE .E. $mo$
         ; modify mode
         OPENMODE = %OMODIFY
*
      O'R TEMPMODE .E. $su$
         ; supercede mode
         OPENMODE = %OSUPR

      O'E
         ; force openmode error
         OPENMODE = -1

      E'L
*
*  Set to no error
      STATUS = STATUS(1) = FM%NOERR

      ; call the fm and try to open
      E'E F&OPEN.(OPENMODE,
1                 %IDFILNM,
1                 NEWNAME ,
1                 TXTSPEC ,
1                 FD(FD&CHAN),
1                 OTSPC   ,
1                 STATUS  )

#     E'E HEXDMP.(1,1,1,STATUS)
*
      FD(FD&NDX)   = 0            ; make text buffer empty
      FD(FD&TXTC)  = 0
      FD(FD&MODE)  = OPENMODE     ; set up access protection
      FD(FD&LNPOS) = 0

      F:N STATUS


*------------  end of opentext  -------------------------------------
*------------  start of dgetch  --------------------------------------
*     dgetch is part of a simplified interface for Manipulating
*     text files with the new file manager.  dgetch is used to
*     read characters from a text file for stream io.
*     dgetch takes two arguments:
*     1) CHAR is a single char from the file described in FD.  The
*        character is in kermit format.  When end of file is reached,
*        CHAR will equal -1.  End of line is indicated by CR.
*     2) FD is a buffer that contains information about the file and
*        the record being manipulated.  the information in FD is to be
*        hidden from the user so that information can be added,deleted
*        or changed without the need to support existing structures.
*        The size of FD is defined by FD&SIZE. The buffer will need to
*        be created by an ALLOC call because the buffer is larger than a
*        local data section.
*
   V'S RDSPC = 02,
1              %RTNERR,
1              %RETURN,1,
1                     %%BYTCNT

      E'O DGETCH.(CHAR, FD)

      STATUS = STATUS(1) = 0

      ;if char index = 0 read in line and stick cr at end
      W'R FD(FD&NDX) .E. 0

         E'E F&READ.(FD(FD&CHAN),
1                    1           ,
1                    FD(FD&TXTB) ,
1                    RDSPC       ,
1                    OTSPC       ,
1                    STATUS      )

         W'R STATUS .GE. FM%NOERR
            FD(FD&TXTC) = OTSPC(3)
            E'E STBYTT.('8D', FD(FD&TXTB), FD(FD&TXTC))
            FD(FD&TXTC) = FD(FD&TXTC) + 1
         E'L

      E'L

      W'R STATUS .GE.FM%NOERR
         ; get a char from the input stream
         CHAR = LDBYTT.(FD(FD&TXTB), FD(FD&NDX)) .LAND. '7F'

         W'R CHAR .E. '000D'
            FD(FD&NDX) = 0
            FD(FD&LNPOS) = FD(FD&LNPOS) + 1
         O'E
            FD(FD&NDX) = FD(FD&NDX) + 1
         E'L

      O'R STATUS .E. UL%RDEOF
         ; if at eof set char = -1
         CHAR = -1

      E'L

      F'N STATUS

*-------------- end of dgetch  -----------------------------------------
*-------------- start of dputch  ---------------------------------------
*     dputch is part of a simplified interface for manipulating
*     text files with the new file manager.  dputch is used to
*     write a stream of characters to a text file.
*     dputch takes two arguments:
*     1) CHAR is a single char from the file described in FD.  The
*        character is in kermit format.  The end of line is indicated by
*        the symbol CR.
*     1) FD is a buffer that contains information about the file and
*        the record being manipulated.  the information in FD is to be
*        hidden from the user so that information can be added,deleted
*        or changed without the need to support existing structures.
*        The size of FD is defined by FD&SIZE. The buffer will need to
*        be created by an ALLOC call because the buffer is larger than a
*        local data section.

      E'O DPUTCH.(CHAR, FD)

      STATUS = 0
#     E'E HEXDMP.(0,0,0,CHAR)
#     E'E HEXDMP.(1,50,1,FD(FD&TXTC))
      W'R CHAR .NE. '000D'
         W'R CHAR .E. '000A' .AND. FD(FD&TXTC) .E. 0 , T:O EXITPCH
         FD(FD&NDX) = FD(FD&NDX) + 1
         W'R PACKLINE.((CHAR), FD(FD&TXTC)) .NE. -1, T:O EXITPCH
      E'L
      WRSPEC(0) = 2
      WRSPEC(1) = %RECLEN
      WRSPEC(2) = FD(FD&TXTC)
      WRSPEC(3) = %RTNERR
      E'E F&WRITE.(FD(FD&CHAN), 1, FD(FD&TXTB), WRSPEC, OTSPC, STATUS)

      FD(FD&TXTC)  = FD(FD&NDX) = 0
      FD(FD&LNPOS) = FD(FD&LNPOS) + 1

EXITPCH C'E
      F'N STATUS


*-------------- end of dputch  -----------------------------------------
*-------------- start of closstrm -------------------------------------
*     closstrm is part of a simplified interface for manipulating
*     text files with the new file manager.  closstrm is used to
*     close a text file used for stream io.
*     closstrm takes one argument:
*     1) FD is a buffer that contains information about the file and
*        the record being manipulated.  the information in FD is to be
*        hidden from the user so that information can be added,deleted
*        or changed without the need to support existing structures.
*        The size of FD is defined by FD&SIZE. The buffer will need to
*        be created by an ALLOC call because the buffer is larger than a
*        local data section.
*
      V'S CLOSPC = 1,
1                  %RTNERR

      E'O CLOSTEXT.(FD)

#     E'E TYPEMSG.($ CLOSSTRM!$)
#     E'E HEXDMP.(0,0,0,FD(FD&TXTC))
      W'R FD(FD&TXTC) .NE. 0
         ;  the line buffer is not empty, so flush the line to disk
         E'E DPUTCH.('000D', FD)
      E'L

      E'E F&CLOSE.(FD(FD&CHAN),CLOSPC, STATUS)

      F'N STATUS

*-------------- end of clostext -------------------------------------
*
      E:N
