/*REXX
 |
 | Name: COPYANSI
 |
 | Author: David Alcock
 |         davea@ticnet.com
 |
 | Purpose: Convert a file that has ANSI control characters.  Good
 |          for running prior to downloading to PC.
 |
 | Disclaimer: This REXX exec is FREEWARE.  Use at your own risk.  It
 |             is provided for your enjoyment and neither David
 |             Alcock or his employer provides any warranty for it's
 |             use.  I'd like to hear how it works on your system.
 |
 |             This software is not in the public domain but is
 |             available free of charge and with source code provided.
 |             It is copyright 1998-1999 by David Alcock.  All rights
 |             reserved.
 */
/**********************************************************************
*           M o d i f i c a t i o n   H i s t o r y
*
* Person     Date        Description
* ---------- ----------- ---------------------------------------------
* DGAlcock   16-OCT-1998 v1.0 Initial exec written
**********************************************************************/

vrm = '1.0'

mtype = address()
PARSE SOURCE s1 s2 s3 s4 s5 s6 s7 s8 s9

if mtype == "TSO" | mtype == "MVS" then ,
   if sysvar(systsoe) > "2040" then systemid = mvsvar(syssmfid)
   else do
        cvt    = storage(10,4)
        dcvt   = c2d(cvt)
        smca   = storage(c2x(d2c(dcvt+x2d(c5))),3)
        dsmca  = c2d(smca)
        systemid = storage(c2x(d2c(dsmca+x2d(10))),4)
        end
else systemid = ""

if substr(systemid,1,3) == "CSW" then "cls" /* clear screen for Dave */

say "COPYANSI - version "vrm "- Freeware - Convert ANSI utility"
/* say "Copyright 1998-1999 by David Alcock. All rights reserved." */
say " "
say ". Executing in Environment: "s1 "-" mtype
if systemid <> "" then say ". Executing on system: "systemid
say " "

/***********************************************************************
* Verify that the input dataset exists read the whole thing in
***********************************************************************/

arg sysut1_dsn
if sysut1_dsn == "" then ,
   sysut1_dsn = 'SPF8.LIST'

= LISTDSI(sysut1_dsn)
if x <> 0 then do
   say "Error accessing DSN:" sysut1_dsn
   say "> "sysmsglvl1
   say "> "sysmsglvl2
   say "> SYSREASON: "sysreason
   exit
   end

sysut1_pdsn = sysdsname
parse value sysut1_dsn with . "(" member ")" .
if member <> "" then do
   sysut1_pdsn = sysut1_pdsn"("strip(member)")"
   end
say "Processing input file "sysut1_pdsn

sysut1_dd = "SYU1"random()
address TSO "ALLOCATE FILE("sysut1_dd") DA('"sysut1_pdsn"') SHR REUSE"
"EXECIO * DISKR "sysut1_dd" (FINIS STEM sysut1_file."
address TSO "FREE FILE("sysut1_dd")"

/***********************************************************************
* Create the output dataset
***********************************************************************/

sysut2_dsn = sysvar(sysuid)".SPFTEMP1.ANSI"
say "Writing to DSN="sysut2_dsn
address TSO "DELETE '"sysut2_dsn"'"
address TSO "ALLOCATE FILE(SYSUT2) DA('"sysut2_dsn"')",
            "UNIT(SYSDA) NEW REUSE SPACE(15 15) TRACKS",
            "LRECL(137) BLKSIZE(141) RECFM(V B)"


/***********************************************************************
* Process the SYSUT1 (input) file and write stuff to the output file
***********************************************************************/

say "Processing "sysut1_file.0" lines of input"
added = 0
dropped = 0
address TSO "NEWSTACK"   /* Place to put SYSUT2 output */

do i = 1 to sysut1_file.0
   select
     when substr(sysut1_file.i,1,1) == "0" then do
          added = added + 1
          queue " "
          end
     when substr(sysut1_file.i,1,1) == "-" then do
          added = added + 2
          queue " "
          queue " "
          end
     when substr(sysut1_file.i,1,1) == "+" then do
          dropped = dropped + 1
          iterate
          end
     otherwise
       nop
       end
   line = substr(sysut1_file.i,2,length(sysut1_file.i)-1)
   if strip(line) <> "" then queue line

   end

/*********************************************************************
* That's all folks...
*********************************************************************/

if added == 0 then say "No lines were added"
else say added "Blank lines were added for advancing ANSI characters"
if dropped == 0 then say "No lines were dropped (no + ANSI characters)"
else say dropped "Lines were dropped"

queue ''      /* add null line to indicate End-Of-File */
address TSO "EXECIO * DISKW SYSUT2 (FINIS"
address TSO "DELSTACK"
address TSO "FREE FILE(SYSUT2)"