Simplified File Decompression Using ARexx
by Randy Finch
What Is ARexx?
First off, ARexx is not the nickname of one of the prehistoric dinosaurs. It is the Amiga version of the REXX language. ARexx was written by William "Bill" S. Hawes and is sold by him (see information at the end of this article). I have talked to Bill on the phone a couple of times and I met him about three years ago. He seems to be a very nice and talented guy. Bill became famous in the Amiga community several years ago when he made his program Conman freely distributable. Conman added command line editing and history to the CLI window. Bill added to Conman to create a complete command environment to replace the CLI called WShell. This product is available commercially. I have been using it for years and have been very pleased. I continue to use it even though Commodore now has a shell available with the operating system.
The pinnacle of Bill's success (so far) has to be ARexx. When I attended the 1988 Amiga Developer's Conference in D.C., pleas were made for the developers to incorporate ARexx interfaces into their products. These pleas paid off. Many, many Amiga software packages now have ARexx interfaces. It has become so commonplace that Commodore has made ARexx a standard part of release 2.0 of the operating system.
So Again, What Is ARexx?
ARexx is a high level programming language with a source level debugging facility and more. It can also be used as a command language. Command programs, more commonly known as batch files, scripts, or macros, can be used to extend the set of predefined operating system commands. No doubt most of you have written a script at one point or another where several commands were combined together to create a program that accomplished a task that no single command could have done alone. ARexx allows scripts to be written with the added flexibility of having a programming language available. Standard scripts rely totally on the operating system commands. This makes some tasks quite difficult because looping constructs may be hard to implement, variables may be needed, etc. ARexx allows these types of tasks to be implemented easily since the programming language and the operating system commands can be meshed in an easy and useful way. An example will be given later. Also, ARexx can communicate with application programs. This feature can be used to allow the user to customize a program, or it can be used to allow one program to control another program within the multitasking environment of the Amiga. This is a very powerful feature. This article will focus on ARexx as a programming and command language.
ARexx As A Programming Language
ARexx is an interpretive language. Programs can be created with a text editor and saved to disk as an ASCII file. The ARexx interpreter will read this file and execute it on the fly. In order to execute an ARexx program, a background program called the resident process must be loaded. The resident process program name is REXXMAST. If ARexx programs are to be executed frequently, the REXXMAST program can be loaded from within the STARTUP-SEQUENCE script file. The resident process will remain loaded and available until the RXC command is issued. An ARexx program, which should have a .rexx extension (e.g.- program.rexx), can be executed by issuing the command:
RX program [arguments]
If you use WShell, the RX command is not needed because the shell can launch ARexx programs without RX. When an attempt is made to execute a program, the current directory is searched first. If the program is not found there, the directory currently assigned to the rexx: device is searched.
ARexx Goes To The Library
The ARexx distribution disks contain two shared libraries that need to be copied to the libs: directory: REXXSYSLIB.LIBRARY and REXXSUPPORT.LIBRARY. The former contains functions that are used extensively during program interpretation. Although many of the functions are highly specific to the interpreter, some of the functions may be useful in user programs. This library is opened when REXXMAST is loaded. The latter library contains useful functions specific to the Amiga. It must be opened by the user. Also, ARexx makes use of the MATHIEEEDOUBBAS.LIBRARY shared library on the Workbench disk when performing floating point math.
An Example Program
At this point, let's look at an example program. It is entitled FX.rexx and is shown in Listing 1. Line numbers have been added so you can follow the program easily during the following discussion. FX.rexx is a generic file extraction program. I head up a small public domain library club in my area. Because of this, I frequently download programs from bulletin boards to include on library disks. Typically, the files on bulletin boards are compressed with one of several available compression programs. The three compression programs I use most frequently are ARC, ZOO, and LHARC. Files compressed with these three programs have standard extension names of .arc, .zoo, and .lzh, respectively. During a typical telecommunications session, I will download several compressed files, storing all of them in a directory on my hard disk. When the downloading process is complete, I create a subdirectory for each file and decompress each file into its corresponding subdirectory using the appropriate compression program. This can be a headache when many files have been downloaded. Therefore, I wrote FX.rexx to ease my burden. Let's take a detailed look at FX.rexx.
All Clauses Except Santa Claus
Line 1 is a comment line. All ARexx programs, for historical reasons, must begin with a comment line. Notice that ARexx comments are identical to C comments with an opening /* and a closing */.
Line 3 is a command clause. ARexx recognizes several types of clauses such as null clauses (e.g.- lines 1 and 2), label clauses (no examples), assignment clauses (e.g.- lines 8 and 9), instruction clauses (e.g.- lines 7 and 10), and command clauses (e.g.- lines 3 and 15). Any statement that cannot be classified as one of the first four clause types is classified as a command clause. The statement expression is evaluated and passed to the external host which could be the operating system, as in our case, or an application program. Notice that single quotes enclose the LIST command in line 3. This is not necessary as long as there is nothing in the command clause that can be confused with another clause type. In line 3, the > symbol causes problems since it is interpreted by ARexx to be a 'greater than' symbol rather than the intended redirection symbol for the LIST command. The purpose of line 3 is to create a quick list (only names) of the files in the current directory (which should be the one containing the downloaded files) in a file named RCFLIST on the ram: disk.
Line 5 is an instruction clause that calls one of many internal functions. The OPEN function opens the file ram:RCFLIST, that was just created, with a logical name of RCF. The 'R' means the file will be read.
Getting Looped
Line 7 is a DO instruction indicating that all statements up to its corresponding END instruction in line 24 are to be repeated until the end of file RCF is reached. The DO instruction in ARexx is very versatile, performing all the functions that FOR, DO..WHILE, and WHILE instructions in C perform.
Line 8 uses the internal READLN function to read a line from the RCF file and assign the string to the variable FN. Line 9 extracts the four right-hand characters from FN and assigns them to the variable RS.
Its The Same Old Line
Lines 10, 11, and 12 are actually one line. ARexx interprets the comma at the end of lines 10 and 11 to mean that the following line is a continuation of the current line. ARexx also allows several statements to reside on one line by separating them with a semi-colon. The IF instruction of lines 10-12 checks to see if the filename extension of the current filename, FN, matches one of the three that can be handled by the program. If it does, the statements within the THEN DO..END instructions (lines 13-23) are executed. If not, program execution resumes at line 24. The double equal sign, ==, is called an exact equality operator and indicates that a comparison should proceed character by character. If the equality operator, =, is used, leading blanks are ignored and the shorter string will be padded with blanks. The | symbol is a logical inclusive OR operator.
Here's Your Assignment
Line 14 assigns all the characters in the FN string except the last four, the filename extension, to the LS variable. Notice that the LENGTH function is called within the LEFT function.
Follow These Commands
Lines 15 and 16 begin to show the real power of ARexx. Each of these lines combines an operating system command with an ARexx variable. ARexx will always evaluate an expression before passing it on to the host. In these cases, string concatenation is involved along with a variable substitution. For instance, in line 15, the variable LS is substituted with its current value. Let us suppose that FN is equal to 'RAYTRACE.zoo'. Then LS will be equal to 'RAYTRACE'. Thus, line 15 will evaluate to 'MAKEDIR RAYTRACE' and line 16 to 'CD RAYTRACE'. These two lines have the effect of creating a subdirectory with the same name as the compressed file without the extension and then making this new directory the current directory. You might ask what would happen if line 15 was executed before LS had been assigned to anything. This brings up an interesting feature of ARexx. All variables are typeless; they are stored as strings. When an expression demands that a variable be considered something else such as numeric, the string is converted to the appropriate format and the expression is evaluated. When an unassigned variable is encountered within an expression, ARexx sets it equal to its own name. This means that the unassigned LS variable is equal to 'LS'. Therefore, if 'MAKEDIR ' LS is executed before LS is assigned, a directory with a name of LS would be created.
Make The Right Selection
Lines 17-21 make up a SELECT structure. Here, the appropriate compression program is executed based on the filename extension in the variable RS. WHEN instructions are used for selection. Note that for all three compression program calls following the THEN statements in lines 18-20, a / appears just before the closing single quote which is followed immediately by the variable LS. The expression in line 19 evaluates to ZOO x// /RAYTRACE.zoo. This command will call ZOO, which must be in your command path, telling it to extract the files in the RAYTRACE.zoo file one directory up in the hierarchy. All the extracted files will appear in the current directory, which is RAYTRACE (see line 16).
Line 22 is a CD command that changes the current directory back to the one in which the compressed files reside.
Wind Up
Line 23 is the END instruction that matches the DO instruction in line 13.
The END in line 24 matches the DO in line 7. When it executes, one of two things occurs. If the end of the RCF file has not been reached, the DO loop beginning in line 7 will execute again. If the end of the file has been reached, the loop is exited and lines 25 and 26 are executed, which close and delete the file that was created in line 3. It is not really necessary to close files in ARexx because the interpreter keeps a list of open files and closes them when a program exits. However, FX.rexx had to close ram:RCFLIST because this file needed to be deleted before the program exited.
Well, let's call it a wrap. I hope I have helped you to understand more about this important language for the Amiga called ARexx.
ARexx $49.95
WShell $50.00
William S. Hawes
POB 308
Maynard, MA 01754
(508) 568-8695
For further reading:
Cowlishaw, M. F.; The REXX Language: A Practical Guide to Programming; Prentice-Hall 1985.
Back to list of articles
Back to Randy Finch's Home Page
Last modified on April 1, 1996
Randy Finch at webmaster@rcfinch.com