16
Sat, Nov
2 New Articles

A REXXample

APIs
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 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.

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.

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 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 = ' ' 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) 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: