A very useful aspect of the AS/400 and S/38 computers is that they enable you to write your own CL commands. Unfortunately, the documentation is not always clear in explaining the parameters and values of the command definition statements. An example is the return value parameter of the parameter (PARM) command. This parameter allows a command to return a value to the program using it through a CL variable.
The shop where I work keeps a separate database for each location. Normally we use the library list from the user s job description to control which database is accessed; however, programs used by Operations must have the ability to access more than one database. It seemed that we could write a command to retrieve information about a database from a file. That would make it easier to write other programs.
With this approach there would be no need for the programmer to know how the underlying programs worked, and in most cases, they could be changed without changing the programs that used the command. In addition, a CL program can only read one file, and the command would save this resource for some other use, such as a display file.
A problem arises when using return values in a command: they are allowed to be optional parameters. This is great if you only need one or two values to be returned - why clutter the program with variables that will not be used? But what happens in the command processing program (CPP) is that unpassed parameters cause escape message MCH3601 to be issued. The AS/400 text for this message is, "Referenced location in a space that does not contain a pointer." That s it! No suggestions on how to fix it or what it really means. An ancient S/38 user dubbed this the "Confuse Programmer" message. I take it to mean that since parameters are passed by reference instead of by value, what is passed is a pointer to the data. Since nothing has been passed, no pointer has been created. Thus the message.
The good news is that it can be handled by a MONMSG command. What may not be obvious is that when you call a secondary from your CPP, you will have the same problem unless you create a duplicate set of variables to handle any parameters that may not have been specified in the command. By using this technique, you can use CHGVAR to change the duplicate variable to the value of the parameter. You can see this technique at work in Figures 4a through 4d.
A simplified version of our Retrieve Database Attributes (RTVDBATTR) command is shown in 4a. It has one required parameter, ACTION, and four optional parameters that return values - that is, they have RTNVAL (*YES). 4b lists the CPP, which is CL program DB001CL. Notice that parameters DATABASE, JOBD, JOBDLIB, and STATUS have two CL variables each. For instance, parameter JOBD has &PJOBD and &JOBD, both matching the type and length.
A simplified version of our Retrieve Database Attributes (RTVDBATTR) command is shown in Figure 4a. It has one required parameter, ACTION, and four optional parameters that return values - that is, they have RTNVAL (*YES). Figure 4b lists the CPP, which is CL program DB001CL. Notice that parameters DATABASE, JOBD, JOBDLIB, and STATUS have two CL variables each. For instance, parameter JOBD has &PJOBD and &JOBD, both matching the type and length.
First, we issue a CHGVAR to copy &PDATABASE into &DATABASE. Because the DATABASE parameter is optional, however, the statement may receive an MCH3601 message. You can recover by using CHGVAR within a MONMSG, in order to initialize the duplicate variable to some default value before calling the second program, DB001RG, which is shown in 4c.
First, we issue a CHGVAR to copy &PDATABASE into &DATABASE. Because the DATABASE parameter is optional, however, the statement may receive an MCH3601 message. You can recover by using CHGVAR within a MONMSG, in order to initialize the duplicate variable to some default value before calling the second program, DB001RG, which is shown in Figure 4c.
Before returning from the CPP, each parameter variable is changed to the value of its corresponding duplicate and monitored again for MCH3601. Only those passed will be returned and those not passed will have been gracefully ignored.
Program DB002CL (4d) shows an example of using the RTVDBATTR command.
Program DB002CL (Figure 4d) shows an example of using the RTVDBATTR command.
- Joseph Cattano
TechTalk: Commands with Optional Return Parameter
Figure 4A Command RTVDBATTR
Figure 4a: The RTVDBATTR Command RTVDBATTR: CMD PROMPT( Retrieve Data Base Attributes ) PARM KWD(ACTION) TYPE(*CHAR) LEN(6) RSTD(*YES) + DFT(*FIRST) VALUES(*FIRST *NEXT *PREV + *FIND *CLOSE) PROMPT( Attribute file + action: ) /* The following parameters are optional */ PARM KWD(DATABASE) TYPE(*CHAR) LEN(8) + RTNVAL(*YES) PROMPT( CL var for + database: (8) ) PARM KWD(JOBD) TYPE(*CHAR) LEN(10) RTNVAL(*YES) + PROMPT( CL var for job descript: (10) ) PARM KWD(JOBDLIB) TYPE(*CHAR) LEN(10) + RTNVAL(*YES) PROMPT( CL var for jobd + library: (10) ) PARM KWD(STATUS) TYPE(*CHAR) LEN(9) RTNVAL(*YES) + PROMPT( CL var for command status: (9) )
TechTalk: Commands with Optional Return Parameter
Figure 4B CL program DB001CL
Figure 4b: THE DB001CL Program DB001CL: PGM PARM(&PACTION &PDATABASE &PJOBD &PJOBDLIB + &PSTATUS) /* Parameters */ DCL VAR(&PACTION) TYPE(*CHAR) LEN(6) /* for + file: *FIRST *NEXT *PREV *FIND *CLOSE */ DCL VAR(&PDATABASE) TYPE(*CHAR) LEN(8) /* + Database to perform action on or returned */ DCL VAR(&PJOBD) TYPE(*CHAR) LEN(10) /* Job + description returned */ DCL VAR(&PJOBDLIB) TYPE(*CHAR) LEN(10) /* Job + description library returned*/ DCL VAR(&PSTATUS) TYPE(*CHAR) LEN(9) /* Return + status: *BOF *EOF *FOUND *NOTFOUND */ /* Program variables */ /* Duplicates of return parms above */ DCL VAR(&DATABASE) TYPE(*CHAR) LEN(8) DCL VAR(&JOBD) TYPE(*CHAR) LEN(10) DCL VAR(&JOBDLIB) TYPE(*CHAR) LEN(10) DCL VAR(&STATUS) TYPE(*CHAR) LEN(9) DCL VAR(&MSGID) TYPE(*CHAR) LEN(7) DCL VAR(&MSGDATA) TYPE(*CHAR) LEN(512) MONMSG MSGID(CPF0000 MCH0000) EXEC(GOTO + CMDLBL(ERRORTRAP)) /* Global monitor */ /* With the exception of &PACTION and &PDATABASE, all other + parameters are strictly return variables. &PACTION is + always passed. &PDATABASE may or may not be passed and returned */ CHGVAR VAR(&DATABASE) VALUE(&PDATABASE) MONMSG MSGID(MCH3601) EXEC(CHGVAR VAR(&DATABASE) + VALUE( )) /* Parameter not passed but + needed by the secondary command + processor. */ /* Secondary command processor */ CALL PGM(DB001RG) PARM(&PACTION &DATABASE + &JOBD &JOBDLIB &STATUS) /* Attempt to pass back all return values, but ignore any + parameters that were not specified. */ CHGVAR VAR(&PDATABASE) VALUE(&DATABASE) MONMSG MSGID(MCH3601) CHGVAR VAR(&PJOBD) VALUE(&JOBD) MONMSG MSGID(MCH3601) CHGVAR VAR(&PJOBDLIB) VALUE(&JOBDLIB) MONMSG MSGID(MCH3601) CHGVAR VAR(&PSTATUS) VALUE(&STATUS) MONMSG MSGID(MCH3601) RETURN ERRORTRAP: RCVMSG MSGTYPE(*EXCP) MSGDTA(&MSGDATA) MSGID(&MSGID) SNDPGMMSG MSGID(&MSGID) MSGF(QCPFMSG) MSGDTA(&MSGDATA) + MSGTYPE(*ESCAPE) ENDPGM
TechTalk: Commands with Optional Return Parameter
Figure 4C RPG program DB001RG
Figure 4c: The DB001RG Program ....1.... ....2.... ....3.... ....4.... ....5.... ....6.... ....7.... FDBATTR IF E K DISK * PARMs 2 - 4 are filled from this file and are the field names *===============================================================* C *ENTRY PLIST C PARM ACTION 6 C PARM DBASE C PARM JOBD C PARM JOBDLB C PARM STATUS 9 *---------------------------------------------------------------* C MOVEL * STATUS C MOVE FOUND STATUS * C ACTION CASEQ *FIRST FIRST Get first DB rec C ACTION CASEQ *NEXT NEXT Get next DB rec C ACTION CASEQ *PREV PREV Get previous DB C ACTION CASEQ *FIND FIND Find a DB rec C ACTION CASEQ *CLOSE CLOSE Close the file C END C RETRN *===============================================================* C FIRST BEGSR C *LOVAL SETLLDBATTRIB C READ DBATTRIB LR C LR MOVE EOF STATUS C ENDSR *---------------------------------------------------------------* C NEXT BEGSR C READ DBATTRIB 99 C 99 MOVE EOF STATUS C ENDSR *---------------------------------------------------------------* C PREV BEGSR C READPDBATTRIB 99 C 99 MOVE BOF STATUS C ENDSR *---------------------------------------------------------------* C FIND BEGSR C PLT CHAINDBATTRIB 99 C 99 MOVE NOTFOUND STATUS C ENDSR *---------------------------------------------------------------* C CLOSE BEGSR C MOVE EOF STATUS C MOVE 1 *INLR C ENDSR ....1.... ....2.... ....3.... ....4.... ....5.... ....6.... ....7....
TechTalk: Commands with Optional Return Parameter
Figure 4D CL program DB002CL
Figure 4d: The DB002CL Program DB002CL: PGM DCL VAR(&JOBD) TYPE(*CHAR) LEN(10) DCL VAR(&STATUS) TYPE(*CHAR) LEN(9) NEXTDB: RTVDBATTR ACTION(*NEXT) JOBD(&JOBD) STATUS(&STATUS) IF COND(&STATUS = *EOF) THEN(RETURN) SBMJOB JOB(REPORTS) JOBD(&JOBD) RQSDTA( CALL + DBREPORTS ) GOTO CMDLBL(NEXTDB) ENDPGM
LATEST COMMENTS
MC Press Online