Among the most powerful OS/400 application program interfaces (APIs) are those that produce lists (e.g., a list of fields, members, objects, or jobs). These APIs perform functions similar to OS/400 commands which provide "outfile" processing. The main difference is that list APIs create entries in a user space instead of writing records to a database file. This makes the API method far more efficient than an equivalent CL command. (For a discussion of user space concepts, see the "What is a User Space?" sidebar on page 120 of MC's October 1993 issue or consult the System Programmers Interface Reference.)
Retrieving entries from a user space is usually done from within a high-level language such as RPG or COBOL. This works well when you want to use the information to display a subfile or produce a report. But what if you need to execute a CL command against each entry? In this case, it makes more sense to retrieve the entries directly from within a CL program. Two QUSRTOOL commands are specifically designed for that purpose: the Retrieve User Space Initialization (RTVUSRSPCI) and Retrieve User Space Entry (RTVUSRSPCE) commands.
Overview
The RTVUSRSPCI and RTVUSRSPCE commands are designed for execution from within a CL program. This CL program would typically perform the following tasks:
o Create a user space.
o Call one of the list APIs to load the user space.
o Execute the RTVUSRSPCI command to retrieve the user space initialization information.
o Execute the RTVUSRSPCE command in a loop to retrieve each entry in the user space.
The RTVUSRSPCI command retrieves the initialization information from a user space. This information contains the number of entries in the list, the starting position of the first entry, and the length of each entry. Your CL program uses the number of entries in the list to determine how many times to execute the RTVUSRSPCE command.
The RTVUSRSPCE command retrieves one entry from a user space. This command uses the starting position and entry length retrieved from the RTVUSRSPCI command. The RTVUSRSPCE command returns the user space entry to a variable in your CL program. Each execution of the RTVUSRSPCE command automatically updates the starting position value to point to the next entry in the list.
The RTVUSRSPCI Command
Required Parameters
USRSPC
Specify the qualified name of the user space whose initialization values are to be retrieved. The possible values are:
*LIBL: The library list is used to locate the user space.
*CURLIB: The user space is located in the current library.
library-name: Specify the name of the library that contains the user space.
user-space: Specify the name of the user space.
Optional Parameters
STRPOS
Specify the name of the CL variable to contain the starting position of the first entry in the list in the user space specified in USRSPC. STRPOS must be defined as a *CHAR field with a length of four bytes. The value returned is the binary equivalent of the starting position. This value may be specified in the STRPOS parameter of the RTVUSRSPCE command.
NBRENT
Specify the name of the CL variable to contain the number of entries in the list in the user space specified in USRSPC. NBRENT must be defined as a *DEC field with a length of nine bytes and zero decimal positions.
ENTLEN
Specify the name of the CL variable to contain the length of each entry in the list in the user space specified in USRSPC. ENTLEN must be defined as a *CHAR field, four bytes in length. The value returned is the binary equivalent of the entry length. This value may be specified in the ENTLEN parameter of the RTVUSRSPCE command.
Source Members
INFO: RTVUSRSPCI (QATTINFO) CDO: TAASPCB (QATTCMD) CPP: TAASPCBR (QATTRPG) The RTVUSRSPCE Command
Required Parameters
USRSPC
Specify the qualified name of the user space whose entries are to be retrieved. The possible values are:
*LIBL: The library list is used to locate the user space.
*CURLIB: The user space is located in the current library.
library-name: Specify the name of the library that contains the user space.
user-space: Specify the name of the user space.
Optional Parameters
ENTLEN
Specify the length of the user space entry to be retrieved. ENTLEN must be defined as a *CHAR field, four bytes in length, and must contain the binary value of the entry length. You can retrieve the formatted entry length value from the parameter returned by the RTVUSRSPCI command.
STRPOS
Specify the starting position within the user space. The entry is retrieved for the length specified in ENTLEN starting at this position. STRPOS must be defined as a *CHAR field, four bytes in length, and must contain the binary value of the entry length. You can retrieve the formatted start position value from the parameter returned by the RTVUSRSPCI command.
The STRPOS value is incremented for each execution of the RTVUSRSPCE command. This allows you to execute the RTV-USRSPCE command within a CL program loop, retrieving list entries in order.
RTNVAR
Specify the name of the CL program variable to contain the retrieved user space entry. RTNVAR must be defined as a *CHAR field with a length of 1000 bytes. The entry retrieved from the user space is left-adjusted into this field.
SETLR
Specify whether the processing program should set the LR indicator on or off. Because the processing program for the RTVUSRSPCE command is an RPG program, the user can control when the program is deactivated by setting this parameter.
*OFF: The RPG processing program is not deactivated upon return. This is the recommended setting when the command is executed more than a few times in succession. Leaving the program activated results in faster execution of the RTVUSRSPCE command, since the processing program does not have to be initialized each time it is called.
*ON: The RPG processing program is to set on the LR indicator and return. The user space is not accessed. This deactivates the program. You would normally specify *ON after the last execution of the RTVUSRSPCE command. If this value is specified and the RPG program has not been activated, the RPG H1 indicator is set on.
Source Members
INFO: RTVUSRSPCE (QATTINFO) CDO: TAASPCC (QATTCMD) CPP: TAASPCCR (QATTRPG)
Example
To demonstrate the use of the RTVUSRSPCI and RTVUSRSPCE commands, look at the CL program in 1. This program's task is to create a spool file using the Display Job Description (DSPJOBD) command for every job description in library QGPL.
To demonstrate the use of the RTVUSRSPCI and RTVUSRSPCE commands, look at the CL program in Figure 1. This program's task is to create a spool file using the Display Job Description (DSPJOBD) command for every job description in library QGPL.
This program performs the following tasks:
o Calls the Create User Space (QUSCRTUS) API to create user space TMPSPC in library QTEMP.
o Calls the List Object (QUSLOBJ) API to load the user space with information about every job description in library QGPL.
o Executes the RTVUSRSPCI command to retrieve the starting position of the list, the length of each entry in the list, and the number of entries in the list.
o Drops into a loop where it increments a counter and then compares the counter to the number of entries in the list. When the counter exceeds the number of entries in the list, the program branches to a label outside of the loop.
o Passes the entry length and starting position to the RTVUSRSPCE command to retrieve an entry from the list into a CL variable.
o Extracts the job description name from the CL variable, &RTNVAR.
o Executes the DSPJOBD command against the job description to create a spool file.
o Completes the loop by branching up to a label to retrieve the next entry in the list.
o When the loop terminates, executes the RTVUSRSPCE command, if necessary, to deactivate the RPG processing program.
Robin Klima is a senior technical editor of Midrange Computing.
These tools are documented in Midrange Computing's QUSRTOOL Command Reference. The manual contains explanations and syntax diagrams for more than 300 useful tools.
The Retrieve User Space Tools
Figure 1 Display Job Descriptions in QGPL
/*===============================================================*/ /* To compile: */ /* */ /* CRTCLPGM PGM(XXX/XRK004CL) SRCFILE(XXX/QCLSRC) */ /* */ /*===============================================================*/ XRK004CL: PGM DCL VAR(&ENTLEN) TYPE(*CHAR) LEN(4) DCL VAR(&NBRENT) TYPE(*DEC) LEN(9 0) DCL VAR(&JOBD) TYPE(*CHAR) LEN(10) DCL VAR(&STRPOS) TYPE(*CHAR) LEN(4) DCL VAR(&RTNVAR) TYPE(*CHAR) LEN(1000) DCL VAR(&COUNT) TYPE(*DEC) LEN(9 0) /* Create user space in QTEMP */ CALL PGM(QUSCRTUS) PARM('TMPSPC QTEMP' + 'USRSPC' X'00000020' ' ' '*ALL' + 'Temporary user space' '*YES' X'00000000') /* List job descriptions in QGPL */ CALL PGM(QUSLOBJ) PARM('TMPSPC QTEMP' + 'OBJL0100' '*ALL QGPL' '*JOBD') /* Retrieve initialization values */ RTVUSRSPCI USRSPC(QTEMP/TMPSPC) STRPOS(&STRPOS) + NBRENT(&NBRENT) ENTLEN(&ENTLEN) /* Increment counter */ LOOP: CHGVAR VAR(&COUNT) VALUE(&COUNT + 1) /* Compare counter to number of entries */ IF COND(&COUNT *GT &NBRENT) THEN(GOTO + CMDLBL(DONE)) /* Retrieve an entry */ RTVUSRSPCE USRSPC(QTEMP/TMPSPC) ENTLEN(&ENTLEN) + STRPOS(&STRPOS) RTNVAR(&RTNVAR) /* Extract job description from return variable */ CHGVAR VAR(&JOBD) VALUE(%SST(&RTNVAR 1 10)) /* Display the job description */ DSPJOBD JOBD(QGPL/&JOBD) OUTPUT(*PRINT) /* Go get next entry */ GOTO CMDLBL(LOOP) /* Set on LR if necessary */ DONE: IF COND(&NBRENT *GT 0) THEN(RTVUSRSPCE + USRSPC(QTEMP/TMPSPC) SETLR(*ON)) ENDPGM
LATEST COMMENTS
MC Press Online