Автор: Academic Research and Data Center of USC

Источник: ARDC

Example REXX Programs
Related Documentation


The acronym REXX stands for REstructured eXtended eXecutor.  REXX is a language designed for the general CMS user that can be used to solve a variety of everyday computing problems.  Unlike the EXEC and EXEC2 processors, which have a similar purpose, REXX is easy to learn and easy to use.

This documentation was produced by the Academic Research and Data Center of USC.  Questions about its contents should be referred to a consultant at 777-6865.


A program written in the REXX language looks a lot like one written in PL/I, but REXX has numerous features that other languages do not have.  Some of the features of the language include:

English-like syntax
upper and lower-case support
free-format program structure
good debugging facilities
contextual data typing
structured programming framework

REXX can be used for:

Personal programming - REXX has powerful math and character-handling abilities.  It is well suited to the types of applications in which you might use BASIC or LOGO.

Command and macro programming - if you dislike the way a CMS or XEDIT command works or if you need a command that is not part of CMS, you can write your own commands with REXX (a REXX program is a CMS EXEC or XEDIT macro depending on its filetype).

Educational applications - REXX can be highly interactive, thus it could be used in Computer Assisted Instruction (CAI) applications.  It is particularly well-suited to teaching programming because of its structure, readability, and ease of use.  REXX could also be used in writing simulations of processes or systems.

Example REXX Programs

To give you an idea what a REXX program looks like and to demonstrate some of its features, several example programs are listed below with comments on each.  The first three are taken from VM/SP System Product Interpreter User's Guide (SC24-5238), published by IBM.

Example 1: TESTS EXEC


  /* Arithmetic test */

  credits = 0

  do until credits = 5

    a = random(1,9)   /* Choose a whole number     */

                      /* between 1 and 9.  Choose  */

                      /* at random.                */

    b = random(1,9)

    say "What is" a "+" b "?"

    pull answer

    if answer = "" then exit /* If ENTER key pressed, */

                             /* then leave program.   */

    if answer = a + b

    then credits = credits + 1

    else say a "+" b "is" a+b


 You indicate to CMS that you are using REXX by making the first line in your program a comment, in this case "/* Arithmetic test */".  Comments (enclosed in /* */) can appear anywhere in the program.  There are numerous built-in functions such as "random()" that make your work much easier.  Notice that the statements used are very similar to English statements.  If you write down each step of your task before you begin to write the REXX program, you will be able to use many of your sentences in almost the same form.



 /* Gives a warning when the user's A-disk is more than */

 /* eighty percent full.                                */



 if rc = 0 then do

   pull              /* Discard header */

   parse pull "-" percentage .

   if percentage > 80

   then say "Warning.  Your disk is" percentage"% full"


 else say "NEARFULL EXEC: unexpected return code" rc


NEARFULL warns the user when his or her disk is 80% full.  CMS commands (like MAKEBUF, QUERY, etc.) can be used in REXX as needed.  A program such as NEARFULL might be called from your PROFILE EXEC every time you log on.

Example 3: CENSUS EXEC


/* This program requests the user to provide a person's    */

 /* age and sex.  In reply, it displays a person's          */

 /* status.  Persons under the age of five are BABIES.      */

 /* Those aged five through twelve are BOYS or GIRLS.       */

 /* Those aged thirteen through nineteen are TEENAGERS.     */

 /* The rest are MEN or WOMEN.                              */


 /* Get input from user                                     */


 do until datatype(age,number) & age >= 0

   say "What is the person's age?"

   pull age


 do until sex = "M" | sex = "F"

   say "What is the person's sex (M or F)?"

   pull sex



 /* COMPUTE STATUS                                          */

 /*                                                         */

 /* Input:                                                  */

 /* AGE     Assumed to be 0 or a positive number.           */

 /* SEX     "M" is taken to be male;                        */

 /*         anything else is taken to be female.            */

 /*                                                         */

 /* Result:                                                 */

 /* STATUS  Possible values: BABY, BOY, GIRL, TEENAGER,     */

 /*         MAN, WOMAN.                                     */



   when age < 5 then status = "BABY"

   when age < 13 then do

     if sex = "M"

     then status = "BOY"

     else status = "GIRL"


   when age < 20 then status = "TEENAGER"


     if sex = "M"

     then status = "MAN"

     else status = "WOMAN"


 say "This person should be counted as a" status

CENSUS demonstrates one form of looping (DO UNTIL) and the use of the SELECT statement.  Note the structure and readability of the program.

Example 4: OUTPUT EXEC

Following is a useful program that you can use for capturing output sent to the CRT screen. It is an example of using REXX to provide an easy way of executing a complex CMS command (SPOOL), saving you time and effort.


/* OUTPUT EXEC                                           */

 /*                                                       */

 /*   This program starts or stops recording of output    */

 /* sent to the screen from CMS.  To start recording (or  */

 /* to stop recording), enter the command OUTPUT from the */

 /* command line.  If you have forgotten whether you are  */

 /* recording terminal output or not, enter OUTPUT CHECK. */

 /*   You might want to use this EXEC if you are running  */

 /* an interactive program and want to record the inter-  */

 /* action or you might use it to record error messages   */

 /* from a program or command (if you expect any).        */

 arg option

 /* Find out if output is being recorded or not.          */


 pull . . . . . state


 /* If user wants to check whether recording or not, call */

 /*   CHECKOPT subroutine.                                */

 if substr(option,1,1) = "C" then call chkopt

 /* If recording is off, turn it on.                      */

 if state = "STOP" then do


   say "Console output is now being sent to your virtual",



 /* If recording is on, turn it off.                      */

 else do


   say "Recording of terminal ouptut has been stopped."




   if state = "STOP" then say "Terminal output IS NOT",

              "being recorded."

   else say "Terminal output IS being recorded."



Related Documentation

The best way to learn to use REXX is to read and work the exercises in VM/SP System Product Interpreter User's Guide (SC24-5238), which is available for purchase from IBM and for reference at Computer Services.  The audience for this manual is the first-time REXX (or any other programming language) user.

More advanced information is contained in VM/SP System Product Interpreter Reference (SC24-5239), also available for purchase from IBM and for reference at Computer Services.  This manual is intended as a reference guide, listing all the REXX commands, functions, and error messages.

These manuals are available for reference in the CS Reference room, third floor, CS Building.  The CMS 'HELP' system includes information on all REXX commands and functions.  To examine this information, enter the command 'HELP REXX MENU' from the command line in CMS.