REXX Tips & Tricks, Version 2.80


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Writing OS independent programs



To write OS independent REXX programs, you can use the <PARSE SOURCE 
statement to distinguish between the different platforms (see example 
below and the chapter about PARSE SOURCE. The chapter titled Force a REXX 
program to run in a special way also discusses the use of PARSE SOURCE to 
identify the environment in which a REXX program is running and then 
process conditional commands. 
When writing programs for use on other platforms in addition to OS/2, 
remember that some of the features and functions in OS/2 REXX are not 
implemented in REXX on other platforms (or may be implemented in a 
different manner)! 
(see REXXTRY.CMD  for another example for OS independent REXX programs) 

 
/* ------------------------------------------------------------------ */
/*                                                                    */
/* APPLICATION - Cross Platform - CMS, OS/2 2.0 and TSO               */
/* FUNCTION    - Merge 4 comma-delimited input files into an          */
/*               outputfile, tagging each record with the name of     */
/*               it's corresponding input file                        */
/* USE:   OS/2 - MERGE f1.ext,f2.ext,f3.ext,f4.ext,outfile.ext        */
/*        TSO  - MERGE f1.qlf,f2.qlf,f3.qlf,f4.qlf,outfile.qlf        */
/*        CMS  - MERGE fn1 ft1 fm1,fn2 ft2 fm2,...fm4,ofn oft ofm     */
/* AUTHOR      - Michel Plungjan April '93                            */
/*               (see EMail Addresses)                                */
/*                                                                    */
/* History:                                                           */
/*  12.12.1995 /bs                                                    */
/*    - reformatted and added some comments                           */
/*  26.02.1996 /bs                                                    */
/*    - corrected a bug in the TSO read secition according to         */
/*      information from Michel Plungjan                              */
/*                                                                    */
/* ------------------------------------------------------------------ */

  arg InFile.1 "," InFile.2 "," InFile.3 "," InFile.4 "," Merged

  if InFile.1 = "/?" then
    signal HELP;

  call INIT

  j = 0;
  do i = 1 to 4
    FileName = Infile.i
    call READFILE;
    if InRec.0 > 0 then
    do k = 1 to InRec.0
      j = j + 1
      OutRec.j = strip(InRec.k,"T") substr(FileName,1,4)
    end; /* do k = 1 to InRec.0 */
  end; /* do i = 1 to 4 */

  if j > 0 then
  do
    OutRec.0 = j;
    call WRITEFILE;
  end /* if j > 0 then */
  else
  do
    XReason = "Input files empty...";
    XRc = 8;
    SIGNAL ABNORMAL_END
  end; /* else */

SIGNAL NORMAL_END;

/* ------------------------------------------------------------------ */
/*                                                                    */
READFILE:
  select

    when sys = 'CMS' then
    do

/* --------------------- code for CMS ------------------------------- */

      'EXECIO * DISKR' FileName '(FINIS STEM INREC.'
       hrc = rc
       if hrc <> 0 then
       do
         XRc = hrc
         XReason = 'Error when reading' FileName 'dataset'
         SIGNAL ABNORMAL_END
       end /* if hrc <> 0 then */
    end /* CMS */

    when sys = 'TSO' then
    do

/* --------------------- code for TSO ------------------------------- */

      'ALLOC DA('FileName') FI(INDD) SHR'
      hrc = rc
      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Allocation error of' FileName 'dataset'
        SIGNAL ABNORMAL_END
      end
      'EXECIO * DISKR INDD (FINIS STEM INREC.'              /* v2.10 */
      hrc = rc
      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Error when reading' FileName 'dataset'
        SIGNAL ABNORMAL_END
      end /* if hrc <> 0 then */
      'FREE FI(INDD)'
      hrc = rc
      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Error when freeing' FileName 'dataset, DDName INDD'
        SIGNAL ABNORMAL_END
      end /* if hrc <> 0 then */
    end /* TSO */

    when sys = 'OS/2' then
    do

/* --------------------- code for OS/2 ------------------------------ */

      do ii = 1 while lines(filename) > 0
        InRec.ii = linein(FileName)
      end; /* do ii = 1 while lines( fileName) > 0 */

      InRec.0 = ii - 1
      if (stream(FileName,'S') <> 'READY') then
      do
        XRc = 1
        XReason = 'Error when reading' InFile ,
                  'file, Error indicator is 'stream(FileName,'D')
        SIGNAL ABNORMAL_END
      end /* I/O Error */
    end /* OS/2 */

    otherwise
    do

/* --------------------- unknown OS --------------------------------- */

      XReason = 'This program does not know how the environment' sys,
                'uses I/O, please contact author'
      XRc = 8
      SIGNAL ABNORMAL_END
    end /* otherwise */

  end /* Select */
return

/* ------------------------------------------------------------------ */
/*                                                                    */
WRITEFILE:

  select

    when sys = 'CMS' then
    do

/* --------------------- code for CMS ------------------------------- */

      'EXECIO 'OutRec.0 'DISKW 'Merged '0 F 80 (FINIS STEM OUTREC.'
      hrc = rc
      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Error when writing' Merged 'dataset'
        SIGNAL ABNORMAL_END
      end /* if hrc <> 0 then */

    end /* CMS */

    when sys = 'TSO' then
    do

/* --------------------- code for TSO ------------------------------- */

      'ALLOC DA('Merged') FI(OUTDD) SHR' /* File must already exist */
      hrc = rc
      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Allocation error of' Merged 'dataset'
        SIGNAL ABNORMAL_END
      end /* if hrc <> 0 then */

      'EXECIO' OutRec.0 'DISKW OUTDD (FINIS STEM OUTREC.'
      hrc = rc

      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Error when writing' Merged 'dataset'
        SIGNAL ABNORMAL_END
      end /* if hrc <> 0 then */

      'FREE FI(OUTDD)'
      hrc = rc
      if hrc <> 0 then
      do
        XRc = hrc
        XReason = 'Error when freeing' Merged 'dataset, DDName OUTDD'
        SIGNAL ABNORMAL_END
      end /* if hrc <> 0 then */

    end /* TSO */

    when sys = 'OS/2' then
    do

/* --------------------- code for OS/2 ------------------------------ */

      do i = 1 to OutRec.0
        Dummy = lineout(Merged,OutRec.i);
      end; /* do i = 1 to OutRec.0 */

      rc = stream(Merged,'c','close')

      /* please put your own OS/2 error checking here */
    end /* OS/2 */

    otherwise
    do

/* --------------------- unknown OS --------------------------------- */

      XReason = 'This program does not know how the environment' sys,
                'uses I/O, please contact author'
      XRc = 8
      SIGNAL ABNORMAL_END
    end /* otherwise */

  end /* Select */
return;

/* ------------------------------------------------------------------ */
/* init global variables and get the current OS (in the var SYS)      */
/*                                                                    */
INIT:
  true  = 1;
  false = 0;
  XReason = 'Files merged, you may now sort the file 'Merged;
  XRc = 0
  parse source sys .
return

/* ------------------------------------------------------------------ */
/* show the usage help                                                */
/*                                                                    */
HELP:
  do i = 1 until pos('* ---',sourceline(i)) > 0
    say strip(sourceline(i))
  end /* do i = 1 ... */
exit;

/* ------------------------------------------------------------------ */
/*                                                                    */
ABNORMAL_END:
  say 'Program stopped due to'

/* ------------------------------------------------------------------ */
/*                                                                    */
NORMAL_END:
  say XReason 'return code:' Xrc
exit

  

Inf-HTML End Run - Successful