Sie sind auf Seite 1von 6

MC Press Online

A REXXample
Contributed by Craig Pelkie Sunday, 30 June 1991 Last Updated Sunday, 30 June 1991

One of the sensational, new features of Release 3.0 of OS/400 is the inclusion of REXX (Restructured EXtended eXecutor) language. Articles about REXX have been proclaiming it "the king of languages," or some such hyperbole. I, however, find it difficult to feel so effusive about a programming language, and prefer to scrutinize it to determine if it has any immediate or future value.

My initial impressions of REXX have left me ambivalent. I am somewhat drawn to REXX, as it is an integral component of the SAA Common Programming Interface (CPI); REXX is also referred to as the "Procedures Language" in that context. I am attracted to it for the same reason that I am warming to the C programming language: like it or not, it will probably become more important in the future to be able to work with a number of different computers and operating systems. Anything that can be learned on one system and applied to another is a pretty good deal.

I was also attracted to the interactive side of REXX: its facility in making and testing changes immediately. Also, when you make errors, the feedback is concise, and in most cases, reveals the error.

The downside of REXX, unfortunately, is that it's slow. This is most apparent in short REXX programs, such as the one illustrated in this article, since a REXX environment has to be created to interpret the REXX statements. This interpretation is repeated every time the REXX program is run. I fail to understand why REXX cannot be compiled, but that is not possible at this point.

What is REXX, and How Do I Know If I Have It?

If you have installed Release 3.0, then you have REXX capabilities.

My understanding is that REXX originated on mainframe systems. Mainframes have never been known for their friendliness, and REXX was introduced to help develop alternative command interfaces. REXX is, in and of itself, a control language, designed with the intent of getting input, performing string manipulations, and then passing control to the native command processors on the host environment.

In AS/400 terms, that means that you can, for example, use REXX instead of a CL program to perform string and numeric manipulation, then execute CL commands or call other programs. As you will see in the example, the string operations in REXX are much simpler than the equivalents in CL.

The CRTUSRSPC Command

This example actually came about because of another feature, the user space, introduced with Release 3.0. A user space is an object that you create that is, quite literally, just a space of bytes. Many of the new API commands use a user space object, and there are programs to create and manipulate user spaces.

OS/400 provides a DLTUSRSPC (Delete User Space) command, but there is no CRTUSRSPC (Create User Space) command. The API program to create a user space is QUSCRTUS, which needs six parameters. This program is described in the System Programmer's Interface Reference (SC21-8223). Since I was doing quite a bit of experimenting with user space objects, I wanted a CRTUSRSPC command. I also figured that since I was exploring new ground with the system programming APIs, now was a good time to investigate REXX.

Before studying the short REXX program, you should first understand the user space command. To create a user space object, you need to specify the name and library for the object. You must also specify an extended attribute for this type
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:58

MC Press Online

of object. Since there are no predefined meanings for the extended attribute, you can designate anything you wish. (Examples of extended attributes on program and file objects: Programs have extended attributes of RPG, CBL, C, etc.; files have extended attributes of PF, LF, DSPF, PRTF, etc.)

When creating a user space, you specify the initial size of the space, ranging from 1 byte to 16,776,704 bytes. The actual size of the space will be larger, however. Since a user space is an object, the system stores object type information within it, such as the creation date and time, the save date and time, and so on. So, if you specify a size of 1 byte, the size actually taken up by the space is 1024 bytes. When a program puts information into a space, the space will be extended up to the maximum size, as needed. The larger the size of the space, the longer it will take to create it, since the space is initialized to the default value that you specify. In most cases, you will initialize a space to blanks.

Finally, you can also specify the public authority and a text description for a user space.

The CRTUSRSPC command is shown in 1. This also includes a parameter to delete the user space if it already exists. The command definition source for CRTUSRSPC is shown in 2.

The CRTUSRSPC command is shown in Figure 1. This also includes a parameter to delete the user space if it already exists. The command definition source for CRTUSRSPC is shown in Figure 2.

Using REXX as a Command Processing Program

Now that you have some understanding of the CRTUSRSPC command and parameters, we can look at the command processing program. (CPP), written in REXX (see 3).

Now that you have some understanding of the CRTUSRSPC command and parameters, we can look at the command processing program. (CPP), written in REXX (see Figure 3).

When I created the CRTUSRSPC command, I indicated that the CPP was this REXX program. 4 shows the options used on the CRTCMD. The PGM parameter specifies the special value *REXX; the command prompter then prompts for the other REXX parameters (REXSRCFILE, REXSRCMBR, and REXCMDENV). Remember that since REXX programs are not compiled into objects, they are not referenced as objects in the CRTCMD PGM parameter. Instead, we point the command to the source member and source file containing the REXX program.

When I created the CRTUSRSPC command, I indicated that the CPP was this REXX program. Figure 4 shows the options used on the CRTCMD. The PGM parameter specifies the special value *REXX; the command prompter then prompts for the other REXX parameters (REXSRCFILE, REXSRCMBR, and REXCMDENV). Remember that since REXX programs are not compiled into objects, they are not referenced as objects in the CRTCMD PGM parameter. Instead, we point the command to the source member and source file containing the REXX program.

When the CRTUSRSPC command is used, the CRTUSRSPC REXX program is run. This program receives one argument. As a result of issuing the CRTUSRSPC command shown in 1, the argument passed to the REXX program is as shown in 5. That is, REXX sees the command keywords and values -- different from a compiled program, which sees only the values.

When the CRTUSRSPC command is used, the CRTUSRSPC REXX program is run. This program receives one argument. As a result of issuing the CRTUSRSPC command shown in Figure 1, the argument passed to the REXX program is as shown in Figure 5. That is, REXX sees the command keywords and values -- different from a compiled program, which sees only the values.

Whether you are using REXX or the compiled command processor, you have to prepare the values for the call to the QUSCRTUS program, the API for creating user space. The REXX way is shown in 3.

http://www.mcpressonline.com

Powered by Joomla!

Generated: 28 August, 2008, 23:58

MC Press Online

Whether you are using REXX or the compiled command processor, you have to prepare the values for the call to the QUSCRTUS program, the API for creating user space. The REXX way is shown in Figure 3.

REXX includes a parsing facility. There are many variations of parsing in REXX, one example of which is shown in 3. In this example, we are parsing by patterns. For example, to determine the library for the user space, we parse ("look for") the text string that is enclosed by the "USRSPC(" text string and the "/" text string. As shown in 5, REXX will pick out "QTEMP" and assign that string to the REXX variable "library." The "library" variable is declared contextually in REXX; that is, we don't have to tell it that it is a character variable, or assign a length to it. The length is as long as whatever is in it. In this case, after the first "parse arg" statement, "library" is known to REXX as a character string that contains 5 bytes, 'Q', 'T', 'E', 'M', 'P'.

REXX includes a parsing facility. There are many variations of parsing in REXX, one example of which is shown in Figure 3. In this example, we are parsing by patterns. For example, to determine the library for the user space, we parse ("look for") the text string that is enclosed by the "USRSPC(" text string and the "/" text string. As shown in Figure 5, REXX will pick out "QTEMP" and assign that string to the REXX variable "library." The "library" variable is declared contextually in REXX; that is, we don't have to tell it that it is a character variable, or assign a length to it. The length is as long as whatever is in it. In this case, after the first "parse arg" statement, "library" is known to REXX as a character string that contains 5 bytes, 'Q', 'T', 'E', 'M', 'P'.

The following "parse arg" statements pick out the remaining variables from the command string argument. In each case, we tell REXX to look for the variable contained between two other character strings.

At this point, you should stop and reflect. If your primary languages are RPG (any dialect) and OCL or CL, then you should realize that the simple "parse arg" REXX statement represents a lot of work in RPG, OCL or CL. I am not saying that the REXX example is any great improvement over what the command would pass to a CL program, but you should study the example and see some possibilities.

The next block of statements, after the "parse arg" set, is concerned with getting the parameters into shape for the API call. The API wants its parameters in a very precise way. For example, it wants the user space name and library in a character string of length 20: the first 10 bytes being the user space name, the last 10 the name of the library. The "spacelib" assignment statement gives a value to the "spacelib" variable by concatenating a single quote with the space name. The space name is left adjusted into a 10- character field. The library name is also left adjusted into 10 characters, then concatenated to the space name. Finally, another single quote is concatenated. The other statements perform similar manipulations.

What you should notice in this example is the usage of REXX functions, which is similar to functions in other high-level languages. REXX defines about 60 character, bit, and numeric functions: a large part of learning REXX involves familiarizing yourself with these functions, then using them as needed.

For example, the assignment for the "size" parameter uses a numeric function, D2X, that converts a decimal number to its hexadecimal equivalent (since that is what the QUSCRTUS API demands). If you didn't know that the QUSRTOOL library provides a CVTHEX (Convert to Hex) command, you could spend most of an afternoon programming the equivalent function in RPG. So yes, I am enthused about a language that takes care of some of these nettlesome details in such a straightforward manner.

The third main section of the REXX program deletes the existing user space, if that was requested on the command prompt. Of particular interest here is the DLTUSRSPC command. When we created the CRTUSRSPC command, we indicated (by default) that the command environment was to be the OS/400 system control language environment. This is the meaning of the default on the REXCMDENV parameter. Because of this default, the DLTUSRSPC command is understood as an AS/400 command, and is processed as if entered from a command line. In order to pass this, though, we format it in REXX as a character string. In this case, the literal "DLTUSRSPC USRSPC(" is immediately followed by the name of the user space, in the familiar library/space format, followed by the closing parenthesis. REXX evaluates the entire string, recognizes that DLTUSRSPC is not a REXX command, and passes the string to the command processor.

We can, as needed, determine the results of executing a command from REXX. This would be similar to using the MONMSG command in a CL program; however, we will defer that discussion to a future issue.
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:58

MC Press Online

The final statement in the REXX program is a call to the QUSCRTUS API program. In this case, the command "CALL" is enclosed in double quotes. This is needed to inform REXX that we are referring to the OS/400 "CALL" command, and not the REXX "CALL" statement. This is followed by the character string that names the QUSCRTUS program and its parameters.

Testing the Example

To test the example, all you need to do is enter the source as shown in2 and 3, then run the CRTCMD command as shown in 4. Because you don't compile a REXX program, there is no compile step prior to creating the command. This is what I was referring to when I said that I liked REXX because it allowed an interactive approach.

To test the example, all you need to do is enter the source as shown in Figures 2 and 3, then run the CRTCMD command as shown in Figure 4. Because you don't compile a REXX program, there is no compile step prior to creating the command. This is what I was referring to when I said that I liked REXX because it allowed an interactive approach.

Whither REXX?

So what to do with REXX? Maybe it's just the misfortune of programming on a B10. When I manually enter the QUSCRTUS API request at a command line, the response is subsecond. When I use my CRTUSRSPC command, the response is between 6 and 8 seconds. I can only speculate that the more muscular AS/400s enjoy much better response times. My concern though, as a developer, is that it may be inadvisable to create public programs that use REXX.

However, for internal development, I will trade execution speed for development speed. Granted, if this is your initial exposure to REXX, you won't notice any productivity gains, since the learning curve is there (again). But I also know that I've spent many hours over the past several years writing string processing routines in RPG and CL--routines which, for the most part, were specific to the particular application. I like the fact that REXX includes so many functions required for that type of programming.

References

The AS/400 manuals are Procedures Language 400/REXX Programmer's Guide (SC24- 5513) and Procedures Language 400/REXX Reference (SC24-5512). These are quite extensive and obviously specific to the AS/400 implementation of REXX. If you want a quicker read, with the SAA check boxes you can order the SAA-CPI Procedures Language Reference (SC26-4358), which contains all of the information in the SAA-CPI manual. All of these manuals are lighter in tone than other IBM programming manuals. It seems that the people writing the manuals actually use REXX, like it, and have a sense of humor.

Perhaps your site received the manuals as part of your upgrade or new order, but I had to special-order them. I think this is peculiar for a function that is provided as part of OS/400. You may have to place a special order, also. A REXXample Figure 1 The CRTUSRSPC command prompt Figure 1: The CRTUSRSPC Command Prompt Create User Space (CRTUSRSPC) Type choices, press Enter. User space: . . . . . . . . . . __________ Name Library: . . . . . . . . . . . *CURLIB___ Name, *CURLIB Extended attribute: . . . . . . USRSPC____ Name Initial size (1..16776704): . . 1024______ 1-16776704 Initial value: . . . . . . . . . _ Character value Delete existing user space: . . N N, Y
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:58

MC Press Online

Public authority: . . . . . . . *ALL____ *CHANGE, *ALL, *USE, *EXCLUDE Text description: . . . . . . . *BLANK______________________________________ ______

Bottom F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display F24=More keys A REXXample Figure 2 CRTUSRSPC command source code Figure 2: CRTUSRSPC Command Source Code

/********************************************************************/ /* CRTUSRSPC - CREATE USER SPACE /********************************************************************/

*/

CRTUSRSPC: CMD PROMPT('Create User Space') PARM KWD(USRSPC) TYPE(QUAL1) MIN(1) + PROMPT('User space:') PARM KWD(EXTATR) TYPE(*SNAME) LEN(10) + DFT(USRSPC) PROMPT('Extended attribute:') PARM KWD(SIZE) TYPE(*DEC) LEN(8) DFT(1024) + RANGE(1 16776704) + PROMPT('Initial size (1..16776704):') PARM KWD(VALUE) TYPE(*CHAR) LEN(1) + PROMPT('Initial value:') PARM KWD(DELETE) TYPE(*CHAR) LEN(1) RSTD(*YES) + DFT(N) VALUES(N Y) PROMPT('Delete + existing user space:') PARM KWD(PUBAUT) TYPE(*CHAR) LEN(8) RSTD(*YES) + DFT(*ALL) VALUES(*CHANGE *ALL *USE + *EXCLUDE) PROMPT('Public authority:') PARM KWD(TEXT) TYPE(*CHAR) LEN(50) DFT(*BLANK) + PROMPT('Text description:') QUAL1: QUAL TYPE(*NAME) LEN(10) QUAL TYPE(*NAME) LEN(10) DFT(*CURLIB) + SPCVAL((*CURLIB)) PROMPT('Library:') A REXXample Figure 3 Command Processing Program for CRTUSRSPC command Figure 3: Command Processing Program Source for CRTUSRSPC Command /********************************************************************/ /* Create User Space command processor */ /********************************************************************/ parse arg 'USRSPC(' library '/' parse arg '/' space ')' parse arg 'EXTATR(' extatr ')' parse arg 'SIZE(' size ')' parse arg 'VALUE(' value ')' parse arg 'DELETE(' delete ')' parse arg 'PUBAUT(' pubaut ')' parse arg 'TEXT(''' text ''')' spacelib = '''' |||| left(space,10) |||| left(library,10) ||| extatr = '''' |||| extatr |||| size = 'x' |||| '''' |||| d2x(size,8) ||| if value = '*N' then value = ' '
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:58

MC Press Online

value = '''' |||| value |||| pubaut = '''' |||| left(pubaut,10) |||| text = '''' |||| left(text,50) |||| if delete = 'Y' then do usrspc = library '/' space 'DLTUSRSPC USRSPC('usrspc')' end "CALL" 'QUSCRTUS ('spacelib extatr size value pubaut text')' A REXXample Figure 4 Creating the CRTUSRSPC command Figure 4: Creating the CRTUSRSPC Command CRTCMD CMD(QGPL/CRTUSRSPC) + PGM(*REXX) + SRCFILE(QGPL/QCMDSRC) + REXSRCFILE(QGPL/QREXSRC) A REXXample Figure 5 Executing the CRTUSRSPC command Figure 5: Executing the CRTUSRSPC Command CRTUSRSPC USRSPC(QTEMP/USRSPC) EXTATR(USRSPC) + SIZE(1024) VALUE(' ') DELETE(N) + PUBAUT(*ALL) TEXT(*BLANK)

http://www.mcpressonline.com

Powered by Joomla!

Generated: 28 August, 2008, 23:58

Das könnte Ihnen auch gefallen