Writing filter programs in REXX

[Autolink] Menu

Filter programs are programs that read from standard input, convert the input in some way, and write the converted lines to standard output. Filter programs are heavily used in UNIX systems, but you can use them on any operating system supporting redirection of standard input and standard output with pipes, for example, OS/2.

In OS/2 you can write filter programs very simply in REXX (see the example below). But you should take care to be aware of the following points:

Write all messages (error messages, logos, etc.) to STDERR instead of STDOUT, e.g. use

 
  call LineOut "STDERR", "This is an error message"

Always use

 
  call trace "OFF"

as first statement in a filter program (after the required REXX comment delimiters on line 1, of course). This statement makes sure that your program ignores the environment variable RXTRACE.

Also be aware that the function LINES() does not work as expected in Object-Oriented REXX (see The function LINES() in Object REXX). Therefore, you must distinguish between the different REXX versions in your filter program.

 
/* ------------------------------------------------------------------ */
/* Simple filter program in REXX                                      */
/*                                                                    */
/* A filter program reads lines from STDIN, does something with them, */
/* and writes the changed lines to STDOUT                             */
/*                                                                    */
/* In this example we simply convert the lines to uppercase.          */
/* This program works for Classic REXX and for Object REXX.           */
/*                                                                    */

  call trace "OFF"

  signal on notready name ProgramEnd

                    /* check the REXX interpreter version             */
  parse version rexxVersion .
  if rexxVersion = "OBJREXX" then
  do;
                    /* current REXX version is Object REXX            */

                    /* main loop for Object REXX                      */
                    /* (The loop is aborted by a NOTREADY condition)  */
    do forever
      .output~lineout( convert( .input~linein ) )
    end /* do forever */

  end /* if rexxVersion = "OBJREXX" then */
  else
  do
                    /* current REXX version is Classic REXX           */

                    /* main loop for Classic REXX                     */
    do while lines( "STDIN" ) <> 0
      call lineOut "STDOUT", convert( lineIn( "STDIN" ) )
    end /* do while lines() <> 0 */

  end /* else */

programEnd:

exit 0

/* ------------------------------------------------------------------ */
/* this function returns the parameter in uppercase                   */
/*                                                                    */
Convert: PROCEDURE
  parse arg inputLine
  return translate( inputLine )


[Back: Writing OS independent programs]
[Next: Calling REXX programs in the CONFIG.SYS]