A Command Validation Program Template
I’ve created a CL template with a generic error-handling routine to make writing command validation programs much easier. With my template, I don’t have to monitor for a specific message unless I want to, and messages I don’t monitor are handled by a global Monitor Message (MONMSG) command that branches to the error handler.
Figure 1 shows a command validation program for a simple command that accepts a qualified file name and customer number. The highlighted portions are the template code, which goes into all my command validation programs. The code shown here is abbreviated because of space constraints, but you can download the better-documented code and command source at www.midrangecomputing.com/mc.
The Check Object (CHKOBJ) command lets me validate the file and library names without having to write error-handling logic. If an error is found, the generic error logic sends a message to the command processor. The text of the message comes from the message sent by the operating system, and I retrieve it by using the Receive Message (RCVMSG) command. However, I validate the customer number by hand, generating an error message whose text I explicitly define if the customer number is invalid.
Message CPD0006 is used to send user-defined messages from validity-checking programs. The operating system uses the first four bytes of the Message data (MSGDTA) parameter for its own purposes, so the error message itself must be started in position 5. For more information, you can look up the CPD0006 message by using DSPMSGD RANGE(CPD0006).
Finally, CPF0002 is sent as an escape message to tell the system not to execute the command.
— Jeff Kinzer
Pgm ( &CpyFile &Cust )
Dcl &CpyFile *Char ( 20 )
Dcl &Cust *Char ( 5 )
Dcl &FromFile *Char ( 10 )
Dcl &FromLibr *Char ( 10 )
Dcl &ErrorSw *Lgl
Dcl &Errors *Char ( 1 ) ( 'N' )
Dcl &MsgDta *Char ( 100 )
Dcl &MsgF *Char ( 10 )
Dcl &MsgFLib *Char ( 10 )
Dcl &MsgID *Char ( 7 )
Dcl &MsgText *Char ( 132 )
/* Global error monitor */
MonMsg ( CPF0000 MCH0000 ) Exec( Goto SysError )
/* Extract File & Libr Names from Input Parameter */
ChgVar &FromFile ( %Sst( &CpyFile 1 10 ) )
ChgVar &FromLibr ( %Sst( &CpyFile 11 10 ) )
/* Validate File Name - Any Errors will be */
/* handled by the generic error-handling logic */ ChkObj &FromLibr/&FromFile ( *File )
/* Validate Cust Nbr - internally handle the */
/* message for invalid customers. */ If (( &Cust *LT '00001' ) *Or ( &Cust *GT '00003' )) Do
ChgVar &MsgDta ( 'The customer number you +
entered is not valid.' )
SndPgmMsg MsgId( CPD0006 ) MsgF( QCPFMSG ) +
MsgDta('0000' *Cat &MsgDta ) MsgType( *DIAG )
ChgVar &Errors ( 'Y' )
GoTo EndPgm
EndDo
/* If No Errors - End Program */
Goto EndPgm
/* Begin Global Error handler */
SysError:
If ( &ErrorSw ) +
SndPgmMsg MsgID( CPF9897 ) MsgF( QSYS/QCPFMSG ) +
MsgDta( 'An unexpected error occurred during +
execution of the error monitoring logic.' ) +
MsgType( *Escape )
ChgVar &ErrorSw ( '1' )
/* Get the System Generated Error Message */
ChgVar &Errors ( 'Y' )
RcvMsg MsgType( *Excp ) Msg( &MsgText ) MsgDta( &MsgDta ) +
MsgID( &MsgId ) MsgF( &MsgF ) MsgFLib( &MsgFLib )
/* Send the System Generated Message back to the Pgm's Queue */
SndPgmMsg MsgId( CPD0006 ) MsgF( QCPFMSG ) +
MsgDta('0000' *Cat &MsgText ) MsgType( *Diag )
EndPgm: /* End of program */
/* If any errors were found, send Escape msg to Pgm's */
/* queue to keep command from running */
If ( &Errors *EQ 'Y' ) Do
SndPgmMsg MsgId( CPF0002 ) MsgF( QCPFMSG ) MsgType( *Escape )
EndDo
EndPgm
Figure 1: This command validation program accepts qualified file names and customer numbers.
DIM and Subprocedures
The RPG code in Figure 2 does not compile under V4R5 or earlier. When the RPG compiler encounters the localArray variable, it generates error message RNF3320 (“The keyword parameter is not defined”) because, at that point, it has not yet defined the variable var1.
As of V5R1, this logic has been changed. Specifically, the argument to the DIM keyword no longer has to be defined when the compiler encounters it. Under V5R1, the code in Figure 2 compiles successfully.
— Barbara Morris
IBM Toronto
D var1 s 25a
D subproc pr
C callp subproc
C return
P subproc b
D localArray s 1a dim(%size(var1))
P subproc e
Figure 2: A change in compiler logic allows you to create more powerful subprocedures.
More PDM Options
I so enjoyed the list of PDM options you provided in “A Plethora of PDM Options” in TechTalk in the April issue of MC that I wanted to submit some of my personal favorites, which you can see in Figure 3. My favorite on this list is Browse PDM Options (BO). I work on several systems and have defined unique options for each, but I sometimes forget what option code I used for a function. BO lets me search for the unique command, program, or parameter.
— Bruce Guetzkow
Option Command
T /* Test Display File */ STRSDA OPTION(3) TSTFILE(&L/&N)
R /* Run QMQRY in batch */ ? SBMJOB CMD(STRQMQRY QMQRY(&L/&N)) JOB(&N)
O /* Browse PDM options */ DSPPFM FILE(INSTALL/QAUOOPT) MBR(QAUUOPT)
CV /* Convert to RPG IV */ ? SBMJOB CMD(CVTRPG FROMSRCF(&L/&F) FROMSRCMBR(&N)
TOSRCF(&L/QRPGLESRC) TOSRCMBR(&N)) JOB(&N)
DO /* Display Overrides */ DSPOVR
RL /* Display Release Level */ DSPDTAARA QSS1MRI
QE /* Send Message */ CALL PGM(QEZSNDMG)
SM /* Send Message */ ? SNDMSG
ST /* Display System Sts */ DSPSYSSTS
SX /* DSPSRVPGM */ DSPSRVPGM ??SRVPGM(&N) ?*DETAIL(*SRVPGM *PROCEXP *MODULE)
Figure 3: These are useful PDM options.
LATEST COMMENTS
MC Press Online