From a Rexx program you can pass commands to Windows and Unix/Linux shells or to applications designed to work with Rexx. When used to run operating system commands, Rexx becomes a powerful substitute for the Windows Batch Facility or the Unix shell. You can use variables, control structures, mathematics, and parsing to create procedures that would be impossible to implement with the Windows Batch Facility.
Applications that are designed to work with Rexx are often referred to as scriptable applications. To work with Rexx, a scriptable application registers an environment with Rexx. An environment serves as a kind of workspace shared between Rexx and the application. Environments accept application subcommands issued from Rexx programs.
For example, many editors provide a command prompt or dialog box from which you can issue subcommands to set margins or add lines. If the editor is scriptable from Rexx, you can issue editor subcommands from a Rexx program. These Rexx programs are referred to as macros.
When an application runs a Rexx macro, Rexx directs commands to the application's environment. If you issue a subcommand that the application does not recognize, it might pass the command to Windows, depending on the application.
To let you specify which environment processes a command, Rexx includes an ADDRESS instruction. Starting your Rexx programs from the Windows command line makes Windows the default environment for Rexx commands.
Rexx makes it easy to issue commands. The basic rule is that whatever Rexx cannot process it passes to the default environment. You can:
Allow Rexx to evaluate part or all of a clause as an expression. Rexx automatically passes the resulting string to the default environment.
Enclose the entire clause in quotation marks. This makes it a literal string for Rexx to pass to the default environment.
Send a command explicitly to Windows or Unix/Linux by using the ADDRESS instruction.
Rexx processes your program one clause at a time. It examines each clause to determine if it is:
A directive, such as ::CLASS or ::METHOD
A message instruction, such as:
.array~new
A keyword instruction, such as:
say "Type total number"
or
pull input
A variable assignment (any valid symbol followed by an equal sign), such as:
price = cost * 1.2
A label for calling other routines
A null (empty) clause
If the clause is none of the above, Rexx evaluates the entire clause as an expression and passes the resulting string to Windows.
If the string is a valid operating system command, the OS processes it as though you had typed the string at the command prompt and pressed the Enter key.
The following example shows a Rexx clause that uses the DIR command to display a list of files in the current directory.
/* display current directory */ say "DIR command using Rexx" dir
The clause dir is not a Rexx instruction or a label, so Rexx evaluates it and passes the resulting string to Windows. Windows recognizes the string DIR as one of its commands and processes it.
Letting Rexx evaluate the command as an expression might cause problems, however. Try adding a path to the DIR command in the above program (such as, dir c:\config.sys). The Windows command in this case is an incorrect Rexx expression. The program ends with an error.
A safer way to issue commands is by enclosing the command in quotes, which makes the command a literal string. Rexx does not evaluate the contents of strings. Because the string is not a Rexx instruction or label, Rexx passes the string to Windows. Here is an example using the PATH command:
/* display current path */ say "PATH command using Rexx" "path"
The following example, DP.CMD, shows a program using the DIR and PATH commands. The PAUSE command is added to wait for the user to press a key before issuing the next instruction or command. Borders are added too.
/* DP.CMD -- Issue DIR and PATH commands to Windows */ say "="~copies(40) /* display line of equal */ /* signs (=) for a border */ "dir" /* display listing of */ /* the current directory */ "pause" /* pauses processing and */ /* tells user to "Press */ /* any key to continue." */ say "="~copies(40) /* display line of = */ "path" /* display the current */ /* PATH setting */
When you specify the following:
[C:\]rexx dp
a possible output would be:
======================================== The volume label in drive C is WIN. Directory of C:\EXAMPLES . <DIR> 10-16-94 12:43p .. <DIR> 10-16-94 12:43p EX4_1 CMD nnnn 10-16-94 1:08p DEMO TXT 117 10-16-94 1:10p 4 File(s) 12163072 bytes free Press any key when ready . . . ======================================== PATH=C:\WINDOWS [C:\]
Note: Whenever you execute a host command addressed to the Windows or Unix/Linux command shell, that is, an expression that is passed to the system by address CMD as described above, a new process is created in which the system command handler is executed. When the host command returns, any changes to the process environment, such as changing the directory, are not passed back to the process running the Rexx program. To avoid this potential problem, use a different function such as directory() instead of the command CD