          SUBROUTINE (TBYTES,DOSNM,PATH,DL.TYPE,HDRS,ERR.CODE)
** Version# 10 - 11/30/2005 - 11:34am - BILLW - develop

*** Subroutine - UNIX.TO.PC
*---------------------------Program Description---------------------------*
*** This routine transfers data to the users PC using the download
*** functions of Eterm. If the Eterm version is capable of doing a
*** checksumed download (The version is check by this routine), all
*** downloads will use the checksum protocol. This allows error messages to
*** be generated if the requested output file can not be opened or if there
*** is an error during the download process. These errors appear on the
*** status line of the Eterm window. If The version of Eterm in use does
*** not supported the checksum download function, the older download
*** functions are automatically used and no error messages are generated.
*--------------------------Parameter Definition---------------------------*
*** TBYTES   - The number of bytes transfered to the PC is returned   (OUT)
***          - here.
*** DOSNM    - The name of the file on the PC that data will be sent  [IN]
***          - to.
*** PATH     - The name of the file on the Unix (or NT system).       (IN)
***          -  If this name is not supplied (is NULL) The name
***          - /usr/tmp/<the name part of DOSNM> is used.
***          -  The path may be specified as either a path name or
***          - filename,record id.
*** DL.TYPE  - This argument indicates the type of processing to be   [IN]
***          - performed on the input data. Acceptable values are:
***          -  1: Data is read as ascii (READSEQ). Each line is
***          -     terminated with a LF
***          -  2: Data is read as ascii(READSEQ). Each line is
***          -     terminated with a CR/LF
***          -  3: Data is read as ascii(READSEQ). The file header
***          -     (first line) is examined to determine columns.
***          -     Each line is broken down into the columns
***          -     determined from the header. Each column is blank
***          -     stripped. Columns are seperated by a single TAB
***          -     character. Each line is terminated with a CR/LF
***          -  4: Processing is the same as for type 3 but multiple
***          -     TABs seperate columns as needed assuming there are
***          -     tabstops every 6 characters
***          -  5: Processing occurs as in type 3 but columns are
***          -     seperated by a comma. For each column in the
***          -     header which contained nonnumeric, nonblank data,
***          -     that column on each line is quoted.  This is
***          -     basically CSV (spreadsheet) format
***          -  6: Data is transfered exactly as it appears in the
***          -     input file except that any linefeed characters are
***          -     removed.
***          -  7: Data is transfered exactly as it appears in the
***          -     input file.
*** HDRS     - AM delimited list of headers to be downloaded with the (IN)
***          - report data
*** ERR.CODE - A numeric code indicating the type of error. Possible  (OUT)
***          - values are:
***          -  0: No error.
***          -  1: Source file (host) can not be opened.
***          -  2: Destination file (PC) can not be opened.
***          -  3: Transfer error.
*----------------------------Common Variables-----------------------------*
*** OS.TYPE$ is used to determine if this is an NT system in order to
*** determine the level of Eterm required for checksum downloads. This is
*** required due to the fact that some early NT installations used SLink to
*** support serial communications. SLink had some problems with certain
*** character sequences. This code is probably not required anymore since
*** most NT installations use Elink.
*-------------------------------------------------------------------------*


          PITCH    = 6
          EOF      = NO
          TBYTES   = 0
          DOSNAME  = DOSNM
          ERR.CODE = 0
          CONVERT '\' TO '/' IN DOSNAME
          BEGIN CASE
          CASE OS.TYPE$ = 'NT'
             TEST.VERSION = '2.03'
          CASE OTHERWISE
             TEST.VERSION = '.33'
          END CASE
          TEST.ETERM.VERSION ETERM.OK,TEST.VERSION
          IF DL.TYPE=3 OR DL.TYPE=4 OR DL.TYPE=5 THEN
             FORMAT = PC.COLUMNS(PATH)
             COLS   = DCOUNT(FORMAT,AM)
          END

          IF PATH='' THEN
             PATH = DOSNAME
             IF INDEX(PATH,':',1) THEN PATH=FIELD(PATH,':',2)
             SEGS = DCOUNT(PATH,'/')
             TMPNAME = FIELD(PATH,'/',SEGS)
             PATH = '/usr/tmp/':TMPNAME
          END

          IF INDEX(PATH,',',1) THEN
             OPENSEQ FIELD(PATH,',',1),FIELD(PATH,',',2) TO TMPFILE ELSE
                ERR.CODE = 1
                RETURN
             END
          END ELSE
             OPENSEQ PATH TO TMPFILE ELSE
                ERR.CODE = 1
                RETURN
             END
          END

          SEEK TMPFILE,0,0 ELSE RETURN

          BEGIN CASE
          CASE ETERM.OK
             PRINT CHAR(1):' DOWNLOADING : ':DOSNAME
             CKSCTL = 0
             *Do not allow the name to start with a /
             CONVERT '/' TO '\' IN DOSNAME
             CKS.SEND CKSBUF,CKSCTL,DOSNAME,RESULT
             IF RESULT#0 THEN
                CLOSESEQ TMPFILE
                PRINT CHAR(1):' DOWNLOAD IMPOSSIBLE'
                ERR.CODE = 2
                RETURN
             END
          CASE DL.TYPE > 5
             PRINT CHAR(27):'DR~':DOSNAME
          CASE OTHERWISE
             PRINT CHAR(27):'DR':DOSNAME
          END CASE


          HDR.LNS  = DCOUNT(HDRS,AM)
          HDR.LN   = 1

          LOOP
             *** If the headers are passed in, write the header out.
             IF HDRS AND HDR.LN <= HDR.LNS THEN
                BUF     = HDRS<HDR.LN>
                HDR.LN += 1
             END ELSE
                *** If the header is done, do the appropriate read.
                IF DL.TYPE < 6 THEN
                   READSEQ BUF FROM TMPFILE ELSE GOTO FINISH
                END ELSE
                   READBLK BUF FROM TMPFILE,512 ELSE GOTO FINISH
                END
             END

             BEGIN CASE
             CASE DL.TYPE = 1
                NULL
             CASE DL.TYPE = 2
                CT = LEN(BUF)
                IF BUF[CT,1] # CR% THEN BUF := CR%
             CASE DL.TYPE = 3 OR DL.TYPE = 4
                NEWBUF = ''
                FOR X = 1 TO COLS
                   C1 = FORMAT<X,1>
                   C2 = FORMAT<X,2>
                   D  = TRIM(BUF[C1,C2],' ','B')
                   IF X > 1 THEN NEWBUF := CHAR(9)
                   NEWBUF := D
                   IF DL.TYPE = 4 AND X < COLS THEN
                       NEWBUF := STR(CHAR(9),INT((C2-LEN(D))/PITCH))
                   END
                NEXT X
                BUF = NEWBUF
                CT  = LEN(BUF)
                IF BUF[CT,1] # CR% THEN BUF := CR%
             CASE DL.TYPE = 5
                NEWBUF = ''
                FOR X = 1 TO COLS
                   MODE = FORMAT<X,3>
                   C1   = FORMAT<X,1>
                   C2   = FORMAT<X,2>
                   D    = TRIM(BUF[C1,C2])
                   CONVERT \"',\ TO '^' IN D
                   IF MODE = 'A' THEN NEWBUF := '"':D:'"' ELSE NEWBUF := D
                   IF X < COLS THEN NEWBUF := ','
                NEXT X
                BUF = NEWBUF
             CASE DL.TYPE=6
                CONVERT CHAR(10) TO '' IN BUF
             CASE DL.TYPE=7
                NULL
             END CASE

             IF DL.TYPE <= 5 THEN
                CONVERT CHAR(127) TO '.' IN BUF
                CONVERT CHAR(255) TO '.' IN BUF
                LOOP
                   XX = INDEX(BUF,CHAR(16),1)
                   IF NOT(XX) THEN EXIT
                   HOR = SEQ(BUF[XX+1,1])-32
                   IF HOR > XX-1 THEN SPCS = SPACE(HOR-XX+1) ELSE SPCS = ''
                   BUF = BUF[1,XX-1]:SPCS:BUF[XX+2,9999]
                REPEAT
             END

             BYTES = LEN(BUF)
             TBYTES += BYTES
             IF ETERM.OK THEN
                IF DL.TYPE<5 THEN
                   BUF := CHAR(10)
                   TBYTES += 1
                END
                IF DL.TYPE=5 THEN
                   BUF := CHAR(13):CHAR(10)
                   TBYTES += 2
                END
                CKS.SEND CKSBUF,CKSCTL,BUF,RESULT
                IF RESULT#0 THEN
                   CLOSESEQ TMPFILE
                   CKS.SEND '',CKSCTL,'',X
                   PRINT CHAR(1):'DOWNLOAD FAILED'
                   ERR.CODE = 3
                   RETURN
                END
             END ELSE
                IF DL.TYPE > 5 THEN
                   PRINT OCONV(BUF,'MX'):
                END ELSE
                   CONVERT CHAR(13) TO CHAR(127) IN BUF
                   PRINT BUF:
                END
                IF DL.TYPE<5 THEN PRINT CHAR(255):;           TBYTES += 1
                IF DL.TYPE=5 THEN PRINT CHAR(127):CHAR(255):; TBYTES += 2
             END
          REPEAT

FINISH:   BEGIN CASE
          CASE ETERM.OK
             CKS.SEND CKSBUF,CKSCTL,'',RESULT
             IF RESULT#0 THEN
                PRINT CHAR(1):'DOWNLOAD FAILED'
                ERR.CODE = 3
             END
             ELSE PRINT CHAR(1)
          CASE OTHERWISE
             PRINT CHAR(13):
          END CASE
          CLOSESEQ TMPFILE

          RETURN
!BILLW~11/30/05~11:34
