TIP: Click on subject to list as thread! ANSI
echo: os2rexx
to: John Clarke
from: David Noon
date: 1999-10-09 20:29:00
subject: TEE command

In a message dated 10-09-99, David Noon said to John Clarke about "TEE
command"

Hi John,

DN>Since this is the OS2REXX echo, we can write our own!

We can also improve it by removing the argument parsing and placing that
into a new class library. This allows us to reuse that code wherever we want
to convert an argument string into an array of values, in the manner typical
of C/C++ start-up code.

================================= TEE.CMD ==================================
/* REXX version of the TEE command */

/* Build array of program arguments. Use the typical C/C++ name. */
argv = .ARGARRAY~NEW(ARG(1))

/* Remove the /A flag if supplied */
IF argv[1]~TRANSLATE() == '/A' THEN
DO
   argv~REMOVE(1)
   Append_flag = 'APPEND'
END
ELSE
   Append_flag = 'REPLACE'

/* Make sure at least 1 file was specified */
IF argv~ITEMS() = 0 THEN EXIT 12

/* Open a corresponding array of stream objects */
File_stream = .ARRAY~NEW(argv~ITEMS())
i = 1
DO File_name OVER argv
   File_stream[j] = .STREAM~NEW(File_name)~~OPEN('WRITE '||Append_flag)
   i = i + 1
END

/* Finished with these */
DROP argv Append_flag File_name

/* Open standard devices */
Stdin = .STREAM~NEW('STDIN:')~~OPEN('READ')
Stdout = .STREAM~NEW('STDOUT:')~~OPEN('WRITE')

/* Copy the lines across */
DO WHILE Stdin~STATE() == 'READY'
    Line_buff = Stdin~LINEIN()
    DO Output_file OVER File_stream
       Output_file~LINEOUT(Line_buff)
    END
    Stdout~LINEOUT(Line_buff)
END

DROP Output_file

/* Close all the files */
DO i = File_stream~FIRST() TO File_stream~LAST()
  File_stream[i]~CLOSE()
END

Stdin~CLOSE()
Stdout~CLOSE()

EXIT 0

::REQUIRES 'ARGARRAY.CMD'
============================================================================

And here is the class for forming arrays of arguments. It is actually a
useful piece of code. You should put it in a PATH directory.

============================== ARGARRAY.CMD ================================
/* REXX class library to build argument arrays from character strings */
/* such as command tails.                                             */

/* Author: David W. Noon, October 1999                                */

/* You may use this code freely, and redistribute it provided the     */
/* original copyright caveat is retained and no charge is levied      */
/* beyond the price of its distribution medium.                       */

/* No warranty is expressed or implied as to the suitability of this  */
/* software to perform any given task, nor will the author accept     */
/* liability for any damage or loss incurred by its use.              */

/* Copyright (C) 1999, David W. Noon.  All rights reserved.           */

::CLASS ARGARRAY PUBLIC

::METHOD INIT
   EXPOSE Arg_array

   /* Only allow a single argument */
   IF ARG() < 1 THEN RAISE SYNTAX 93.901 ADDITIONAL 1
   IF ARG() > 1 THEN RAISE SYNTAX 93.902 ADDITIONAL 1

   /* Extract argument string */
   Arg_string = ARG(1)~STRIP()

   /* Ensure it isn't a null string */
   IF Arg_string~LENGTH() = 0 THEN RAISE SYNTAX 93.911 ADDITIONAL 1

   /* Build an array. */
   Arg_array = .ARRAY~NEW(1)

   /* Now parse out the arguments into array elements */
   DO i = 1 BY 1 UNTIL Arg_string~LENGTH() = 0
      SELECT
         WHEN Arg_string~LEFT(1) == "'" THEN
         DO
            r = Arg_string~VERIFY("'",'M',2)
            DO WHILE r > 0 & Arg_string~SUBSTR(r+1,1) == "'"
               r = Arg_string~VERIFY("'",'M',r+2)
            END

            /* See if we have an unmatched apostrophe */
            IF r = 0 THEN RAISE SYNTAX 6.2

            /* Ensure next character is comma or space */
            IF r < Arg_string~LENGTH() THEN
               IF VERIFY(' ,',Arg_string~SUBSTR(r+1,1),'M') = 0 THEN
                  RAISE SYNTAX 22.1 ADDITIONAL(Arg_string~LEFT(r+1))

            Arg_array~PUT(SQUOTE(Arg_string~SUBSTR(2,r-2),"'"),i)
         END

         WHEN Arg_string~LEFT(1) == '"' THEN
         DO
            r = Arg_string~VERIFY('"','M',2)
            DO WHILE r > 0 & Arg_string~SUBSTR(r+1,1) == '"'
               r = Arg_string~VERIFY('"','M',r+2)
            END

            /* See if we have an unmatched quote */
            IF r = 0 THEN RAISE SYNTAX 6.3

            /* Ensure next character is comma or space */
            IF r < Arg_string~LENGTH() THEN
               IF VERIFY(' ,',Arg_string~SUBSTR(r+1,1),'M') = 0 THEN
                  RAISE SYNTAX 22.1 ADDITIONAL(Arg_string~LEFT(r+1))

            Arg_array~PUT(SQUOTE(Arg_string~SUBSTR(2,r-2),'"'),i)
         END

         WHEN Arg_string~LEFT(1) == ',' THEN
         DO
            r = 1
            Arg_array~PUT('',i)
         END

         OTHERWISE
         DO
            r = Arg_string~VERIFY(" ,",'M')
            IF r = 0 THEN r = Arg_string~LENGTH() + 1
            Arg_array~PUT(Arg_string~SUBSTR(1,r-1),i)
         END
      END

      Arg_string = Arg_string~DELSTR(1,r)~STRIP('L')
   END

RETURN

/* Fake inheritance */
::METHOD UNKNOWN
   EXPOSE Arg_array

   USE ARG Method_name, Parm_array

   FORWARD MESSAGE(Method_name) TO(Arg_array) ARGUMENTS(Parm_array) CONTINUE
RETURN RESULT

/* Internal subroutine to condense doubled quote marks */
::ROUTINE SQUOTE

   Str = ARG(1)
   Qt = ARG(2)

   p = VERIFY(Str,Qt,'M')
   DO WHILE p > 0
      IF Str~SUBSTR(p+1,1) == Qt THEN Str = Str~DELSTR(p+1,1)
      p = VERIFY(Str,Qt,'M',p+1)
   END

RETURN Str
============================================================================

And this still applies to TEE.CMD ...

DN>As is my usual practice in this echo, I typed this code directly into my
DN>QWK reader, so it is totally untested. Note that it is in Object REXX.

Regards

Dave

___
 * MR/2 2.25 #353 * Buy the dog, get the fleas free of charge.

--- Maximus/2 3.01
45
* Origin: Air Applewood, OS/2 Gateway to Essex 44-1279-792300 (2:257/609)

SOURCE: echoes via The OS/2 BBS

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.