[USflag] The American Programmer [USflag]
Home Programming Books for Computer Professionals Privacy Terms
           Home   > Programming   > REXX Programming   > REXX Language   > REXX on OS/2
           Home   > Programming   > Manuals   > REXX Manuals   > REXX on OS/2
           Home   > Programming   > Just Enough   > REXX on OS/2

Major points to know about REXX under OS/2.

This tutorial accompanies the book The REXX Language on TSO.

Buy the book, then print out this document. Insert the pages into your REXX book.

This is taken from my out-of-print book on OS/2 REXX.

You’ll also need my book "REXX Reference for TSO, OS/2 and CMS".

Information about these books

This short paper will show some of the differences between REXX on TSO and REXX on OS/2.

 

A REXX program is a disk file that is on your personal computer and is accessible to your OS/2 system.

The file contains records or lines.

Each record may contain one or more OS/2 commands and/or REXX instructions. The records are written in ordinary ASCII format, they are what is commonly called a text file or a DOS file.

Please note that most word processors can create this type of file but only if explicitly requested.

A REXX program file name has the extension ".CMD." Batch Files under OS/2 may have the extension ".CMD," too, or more commonly ".BAT." If you happen to have two programs, one with an extension of .CMD and another with an extension of .BAT, OS/2 will find and execute the one with the extension of .CMD and ignore the other. To summarize:

* REXX programs must have an extension of .CMD.

* OS/2 Batch Files may have an extension of either .CMD or .BAT.

* A program with an extension of .CMD takes precedence over one with .BAT.

REXX programs may be placed in any directory, from the Root directory down to the lowest level subdirectory. You will see later in this book that I recommend placing your REXX programs in a subdirectory named REXXPRGS.

Your REXX program must contain a REXX comment on its first line. A REXX comment consists of the forward slash, an asterisk, some optional comments, another asterisk, and another forward slash. The comment tells OS/2 that it is looking at a REXX program, not an OS/2 Batch File. An example of a REXX comment is: /*MYFIRST.CMD, a REXX program */

You must execute your REXX program while in an OS/2 Window, in OS/2 Full Screen or from the program icon in an open file directory. In order to use the Presentation Manager you must be in an OS/2 Window or in OS/2 Full Screen.

Here is a summary of what really matters to OS/2:

* It must be able to find the program

- in the default directory, or

- through the OS/2 PATH command in the file CONFIG.SYS,

- from the complete drive and directory specified when executing.

* The program has the extension ".CMD".

* The program contains a REXX comment /* */ on the first line.

A REXX program executes interactively in an OS/2 Window, under the OS/2 Presentation Manager, or under OS/2 Full Screen.

It will not execute in a DOS Window or when DOS has been booted.

You can watch it execute, wait for it to finish, or cancel it if you wish. You may also open or use other OS/2 windows or features and do something else, such as word processing, while your REXX program is running.

OS/2 is a multitasking operating system and will continue to run your REXX program at the same time as your word processing.


Reading and Writing files

You can read and write files on your hard disk and on your floppy disks. You can also read and write other types of devices that I will call generically "files." The full list follows:

 

COM1: Communications port 1

COM2: Communications port 2

KBD: The keyboard

LPT1: Printer 1

LPT2: Printer 2

PRN: Current default printer

STDERR: Device for error messages (screen)

STDIN: Standard input device (the keyboard, generally)

STDOUT: Standout output device (the screen, generally)

disk files Specify the name of the file.


What you should do before reading or writing.

You do not have to open or close a file explicitly on OS/2. The first time you read or write, OS/2 will open a file if necessary, and when you end your program it will close the file. But since there may be times when you want to do it yourself, I'll show you how to do it. See the example just below

. Opening it with the keyword OPEN allows you to both read and write to the file. If you want to allow reading only and not writing, use OPEN READ instead of OPEN. If you want to allow writing only and not reading, use OPEN WRITE.

 

 

/*openfile.cmd */

/* opens a disk file */

Disk_file = 'c:\rexxprgs\samp1.dat'

CALL STREAM disk_file,'C','OPEN'

If result = 'READY:'

then say disk_file 'opened successfully'

else say disk_file 'could not be opened'

 

How to close a file and how to tell if you were able to close it successfully.

/*closfile.cmd*/

/* closes a disk file */

Disk_file = 'c:\rexxprgs\samp1.dat'

CALL STREAM disk_file,'C','CLOSE'

SELECT

When result = 'READY:'

then say disk_file 'closed successfully'

When result = ""

then say disk_file 'was already closed'

otherwise say disk_file 'could not be closed, unknown reason'

END

Checking if it exists

Even more important than opening a file is checking to see if the file exists before trying to use it. This is important because

* if you are going to read the file it must exist, and

* if you are going to write to it, and it exists you should know it.

The example just below in EXISTS.CMD shows how to determine if a disk file exists. Another way is shown right after that.in OKUSE.CMD. It checks to see if the file is ready to be used in input or output; if it is ready for use, it must exist!

/*exists.cmd */

/* tells if a disk file exists or not */

Disk_file = 'c:\rexxprgs\samp1.dat'

If STREAM(disk_file,'C','QUERY EXISTS') = ""

then say disk_file 'does not exist'

else say disk_file 'exists as specified'

/*okuse.cmd*/

/*tells if a disk file is ready for use or not*/

Disk_file = 'c:\rexxprgs\samp1.dat'

If STREAM(disk_file,'S') = 'READY'

then say disk_file 'can be used in reading or writing'

else say disk_file 'cannot be used in reading or writing'

say "going to open it now"

call STREAM disk_file,'C','OPEN READ'

If STREAM(disk_file,'S') = 'READY'

then say disk_file 'can be used in reading or writing'

else say disk_file 'cannot be used in reading or writing'

Reading a file one line at a time.

You can read one line, or record, at a time from any of the files or devices mentioned above. This is done with the function LINEIN. The first time you do a LINEIN it gives you the first line in the file. If you continue to do LINEIN over and over again, you will receive all the lines in the file. If you are reading from a device other than a disk file, and there is no data available just yet, the program will wait until there is one available. The example just below in READ04.CMD shows how you might read a disk file one line at a time.

/*read04.cmd*/

/* read one line at a time from a disk file, until the end */

SIGNAL ON NOTREADY

/* does it exist? */

Disk_file = 'c:\rexxprgs\samp1.dat'

If STREAM(disk_file,'C','QUERY EXISTS') = ""

then

do

say disk_file 'does not exist'

exit

end

Do while LINES(disk_file) > 0

Line_read = LINEIN(disk_file)

Say 'Just read the line' Line_read

end

EXIT

NOTREADY:

Say 'File error condition has occured on line ' sigl

Say 'Program statement is ' sourceline(sigl)

Say 'Program terminated'

EXIT

WRITING ONE LINE AT A TIME

This is very similar to reading one line at a time. The example just below in WRIT05.CMD is a program that writes three sentences to a file one line at a time.

/*writ05.cmd*/

/* write three sentences, one at a time, to a disk file */

SIGNAL ON NOTREADY

/* does it exist? */

Disk_file = 'c:\rexxprgs\samp1.tmp'

If STREAM(disk_file,'C','QUERY EXISTS') = ""

then say disk_file 'does not exist, creating'

else

do

say disk_file 'exists, deleting'

'erase ' disk_file

end

Call lineout disk_file, 'REXX is fun'

Call lineout disk_file, 'I like OS/2 REXX'

Call lineout disk_file, 'I will use REXX for all my programs'

/* close it */

Call stream disk_file , 'C', 'CLOSE'

/* did it work?*/

'type ' disk_file

'erase ' disk_file

EXIT

NOTREADY:

Say 'File error condition has occured on line ' sigl

Say 'Program statement is ' sourceline(sigl)

Say 'Program terminated'

EXIT

Top of Page


List of books on REXX and other mainframe topics

[Books Computer]

Home Programming Books for Computer Professionals Privacy Terms Contact |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |

[link page]