Others will think you work for IBM.
A System i developer recently informed me that after several years of programming, he is finally learning to create commands. I am amazed that many developers have worked many years with the System i and its predecessors, yet have never created a command, but it seems to be very common.
This developer mentioned two areas that have given him trouble, and I was able to help him. First, he wanted to understand how to qualify an object. Second, he wanted to add help text to his command. I have seen this guy's code, and I know that he is very capable. I have no doubt that the commands he produces will be indistinguishable from those IBM ships with i5/OS. Here's the information I gave him. I hope it will be of benefit to many others.
Faux Pas # 1: Using Separate Parameters Rather Than Qualification
It is easier to use separate parameters for object and name than to qualify an object, but the easy way is not always the best way. A few IBM commands use separate parameters for object name and library name. Two examples are Create Duplicate Object (CRTDUPOBJ) and Save Object (SAVOBJ). However, most IBM commands use qualification to combine object name and library name into one parameter. Your commands will look more "IBM-ish" if you use qualification to combine related values, rather than use separate parameters for each related value.
See for yourself. Let's consider a command that requires the name of a file and the library in which that file is located. First, here's the command with two parameters.
Figure 1: This is a bad example. (Click images to enlarge.)
Take a look at the same command with a qualified file name.
Figure 2: This is a good example.
The process of qualifying is not difficult. The three most common qualification scenarios are qualified object, job name, and file name with member. Let's look at each one in turn. I will show you how to code the qualification in command source code and how to process the qualified value in a CL procedure.
Scenario 1: Qualified Object (Object/Library)
When you define a command parameter to represent an object that resides in the library file system, you will probably need to include a way for the user to tell which library holds the object. To define a qualified object, you'll need two QUAL commands for object and library. The first QUAL must have a label to name the qualification data type. Here is source for a command I'll call DOIT (Do It).
CMD PROMPT('Do It')
PARM KWD(FILE) TYPE(Q1) PROMPT('Database file')
Q1: QUAL TYPE(*NAME) MIN(1) EXPR(*YES)
QUAL TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL)) +
EXPR(*YES) PROMPT('Library')
This command includes a parameter of data type Q1 for a file. But what is type Q1? Since Q1 is not one of the predefined data types, the command compiler looks for a QUAL command labeled Q1. The compiler takes the labeled QUAL and any unlabeled QUAL commands that immediately follow. In this case, two QUAL commands make up the Q1 data type. The first QUAL is for the file name, and the second QUAL command is for the library name.
Notice the locations of the PROMPT parameters. I've included a PROMPT string on the PARM command to indicate the purpose of the parameter. Of the two QUAL commands, however, only the last QUAL, which defines the library, needs a PROMPT. I use this method because it makes my qualified parameters look just like those of the IBM-defined commands.
The user can enter the command parameter in two ways: object name only, and library followed by a slash followed by the object name.
DOIT FILE(SOMEFILE)
DOIT FILE(SOMELIB/SOMEFILE)
The command processing program (CPP) receives one parameter that contains the two values with no separation. In this example, both QUAL commands are defined to be of type *NAME, which i5/OS automatically defines as 10 bytes. Therefore, the CPP receives one 20-byte variable, with the file name in the first 10 bytes and the library name in the last 10 bytes. Here's a fragment of a CL program to process the command source given above.
PGM PARM(&QUALFILE)
DCL VAR(&QUALFILE) TYPE(*CHAR) LEN(20)
CHKOBJ OBJ(%SST(&QUALFILE 11 10)/%SST(&QUALFILE 1 10)) +
OBJTYPE(*FILE) AUT(*OBJEXIST)
MONMSG MSGID(CPF9800) EXEC(DO)
/* the file does not exist; do whatever */
ENDDO
The CL procedure receives the 20-byte qualified object name as &QUALFILE. Bytes 1 through 10 are the file name. Bytes 11 through 20 are the library name. In this example, I've used the substring function, %SST, to refer to the file and library name in the Check Object (CHKOBJ) command.
I almost always prefer to extract the individual parts of a qualified value into variables of their own. The following example is equivalent in function to the previous one. It differs in that the &QUALFILE variable is divided into the &FILE and &LIB variables.
PGM PARM(&QUALFILE)
DCL VAR(&QUALFILE) TYPE(*CHAR) LEN(20)
DCL VAR(&FILE) TYPE(*CHAR) LEN(10)
DCL VAR(&LIB) TYPE(*CHAR) LEN(10)
CHGVAR VAR(&FILE) VALUE(%SST(&QUALFILE 1 10))
CHGVAR VAR(&LIB) VALUE(%SST(&QUALFILE 11 10))
CHKOBJ OBJ(&LIB/&FILE) OBJTYPE(*FILE) AUT(*OBJEXIST)
MONMSG MSGID(CPF9800) EXEC(DO)
/* the file does not exist; do whatever */
ENDDO
Scenario 2: Qualified Job Name (Number/User/Jobname)
Qualification of a job name is similar to qualification of an object. The difference is that there are three values, rather than two, in a qualified job name. Here is the command source code for defining a job name.
CMD PROMPT('Do It')
PARM KWD(JOB) TYPE(JOB) DFT(*) SNGVAL((*)) +
PROMPT('Job name')
JOB: QUAL TYPE(*NAME) LEN(10) MIN(1)
QUAL TYPE(*NAME) LEN(10) MIN(1) PROMPT('User')
QUAL TYPE(*CHAR) LEN(6) RANGE(000000 999999) +
MIN(1) FULL(*YES) PROMPT('Number')
The three QUAL commands that define the JOB data type define job name, user, and job number, respectively. As with qualified objects, the three parts that make up the parameter must be entered in reverse order. That is, the first value represents the last QUAL.
DOIT JOB(106798/JSMITH/MYTUBE)
The CPP receives the qualified job name as a string of 26 characters. That is, the three parts of the parameter are concatenated to form the single parameter that the CPP receives.
Notice also that I have defined a single value to allow an asterisk in the PARM parameter. A single value is a value that can be used instead of the fully qualified value. The CPP is to interpret an asterisk to mean the current job.
DOIT JOB(*)
When you include single values in qualified parameters, you must make the CPP determine if a single value was passed or a qualified value was passed.
In the following program, I use an IF to check for a single asterisk in &QUALJOB. If that condition is true, I retrieve job name, user, and job number from the job attributes. If &QUALJOB is not a single asterisk, it must be a qualified job name, and I extract the three values from the parameter into the job name, user, and job number variables. Regardless of how the three variables are loaded, they serve to tell the Work Job (WRKJOB) command which job to display information about open files in.
PGM PARM(&QUALJOB)
DCL VAR(&QUALJOB) TYPE(*CHAR) LEN(26)
DCL VAR(&JOBNAME) TYPE(*CHAR) LEN(10)
DCL VAR(&JOBUSER) TYPE(*CHAR) LEN(10)
DCL VAR(&JOBNUMBER) TYPE(*CHAR) LEN(6)
/* single value of * means the current job */
IF COND(&QUALJOB *EQ '*') THEN(DO)
RTVJOBA JOB(&JOBNAME) USER(&JOBUSER) NBR(&JOBNUMBER)
ENDDO
/* the job name is qualified */
ELSE CMD(DO)
CHGVAR VAR(&JOBNAME) VALUE(%SST(&QUALJOB 1 10))
CHGVAR VAR(&JOBUSER) VALUE(%SST(&QUALJOB 11 10))
CHGVAR VAR(&JOBNUMBER) VALUE(%SST(&QUALJOB 21 6))
ENDDO
WRKJOB JOB(&JOBNUMBER/&JOBUSER/&JOBNAME) OPTION(*OPNF)
Scenario 3: Qualified Database File with Member (Object/Library Member)
IBM has two ways to qualify file names when a database member is involved. In some commands, such as Copy File (CPYF) and Override with Database File (OVRDBF), the file is qualified as any other object would be, and the member is specified in a separate parameter.
OVRDBF FILE(SOMEFILE) +
TOFILE(MYLIB/MYFILE) +
MBR(MYMBR)
In other commands, like Copy to Import File (CPYTOIMPF), the member is included as a second element in the qualified parameter.
CPYTOIMPF FROMFILE(MYLIB/MYFILE MYMBR) +
TOSTMF('mydata.dat')
Here is a command in which the file member is included in the qualification.
CMD PROMPT('Do It')
PARM KWD(FILE) TYPE(DBFILE) MIN(1) +
PROMPT('Database file')
DBFILE: ELEM TYPE(QUALFILE) MIN(1) PROMPT('File')
ELEM TYPE(*NAME) LEN(10) DFT(*FIRST) +
SPCVAL((*FIRST)) EXPR(*YES) PROMPT('Member')
QUALFILE: QUAL TYPE(*NAME) LEN(10) MIN(1) EXPR(*YES)
QUAL TYPE(*NAME) LEN(10) DFT(*LIBL) +
SPCVAL((*LIBL) (*CURLIB)) EXPR(*YES) +
PROMPT('Library')
In this case, the file parameter has two elements: the qualified file and the member. Both make up the DBFILE data type. The first of those two elements is further defined by the QUALFILE data type.
The CPP receives the FILE parameter as a 32-byte string. The first two positions always contain the number two, to indicate the number of elements, in binary format. You can ignore them. The information you need begins in position three and spans 30 positions: 10 for file name, 10 for library name, and 10 for member name. Use the substring function to extract that information in a CL procedure.
PGM PARM(&QUALFILE)
DCL VAR(&QUALFILE) TYPE(*CHAR) LEN(32)
DCL VAR(&FILE) TYPE(*CHAR) LEN(10)
DCL VAR(&LIB) TYPE(*CHAR) LEN(10)
DCL VAR(&MBR) TYPE(*CHAR) LEN(10)
CHGVAR VAR(&FILE) VALUE(%SST(&QUALFILE 3 10))
CHGVAR VAR(&LIB) VALUE(%SST(&QUALFILE 13 10))
CHGVAR VAR(&MBR) VALUE(%SST(&QUALFILE 23 10))
Faux Pas # 2: No Command Help
I've given you the skeleton source code you need to define qualified parameters as IBM does. This will go a long way toward making your commands look and act like those of IBM's. There is one other thing that you need to do: You need to provide some help text. Absence of help text is a dead giveaway that your command did not come from IBM. Fortunately, defining help text is easy if you are running V5R3 or above.
In V5R3, IBM added the Generate Command Documentation (GENCMDDOC) command to i5/OS. This wonderful command relieves the developer of the grunt work in developing HTML and UIM (help text) documentation. It's a little goofy in the way it works, in that it doesn't return error messages to a green-screen session. You have to look at the job log to see if the command succeeds or not. Another quirk is that it may not be finished when the input-inhibited light goes away, because the command kicks off a Java application that runs in its own little world. But the good outweighs the bad.
To generate help text for the DOIT command, I ran the GENCMDDOC command from an interactive session.
GENCMDDOC CMD(MYLIB/DOIT)
TODIR('/qsys.lib/mylib.lib/qpnlsrc.file')
TOSTMF(doit.mbr)
GENOPT(*UIM)
In return, I got a source member called DOIT[THH1] in file QPLNSRC of library MYLIB. If you prefer, you can generate the source in the IFS. The following command creates the UIM source code in stream file /home/mydir/MYLIB_DOIT.UIM.
GENCMDDOC CMD(MYLIB/DOIT)
TODIR('/home/mydir')
TOSTMF(*CMD)
GENOPT(*UIM)
Wherever you put the source, you'll need to edit it. Open the source member using your editor of choice and start looking for three periods inside angle brackets, like this:
You must have <...>
These placeholders indicate where you need to fill in the blanks. Either replace the placeholders or delete the sentences or sections in which they are found. The following example shows how you might replace a placeholder with meaningful text.
You must have security administrator authority to run this command.
Once you've replaced the placeholders with meaningful text, create the panel group.
CRTPNLGRP PNLGRP(MYLIB/DOIT)
SRCFILE(MYLIB/QPNLSRC)
SRCMBR(DOIT)
REPLACE(*YES)
Attach the panel group to the command, either by creating the command or changing the command.
CRTCMD CMD(MYLIB/DOIT)
PGM(*LIBL/DOITPGM)
SRCFILE(MYLIB/QCMDSRC)
SRCMBR(DOIT)
HLPPNLGRP(MYLIB/DOIT)
HLPID(*CMD)
REPLACE(*YES)
CHGCMD CMD(MYLIB/DOIT)
HLPPNLGRP(MYLIB/DOIT)
HLPID(*CMD)
They'll Think You Work for IBM
One of the highest compliments I've ever been paid occurred on those occasions when someone thought a CL command that I'd written was part of the operating system. It's not that I'm a super programmer, but rather that I follow (as far as is practical) the standards IBM developers have set.
Learning to qualify objects and define help text is not difficult. Whether you're an old-timer or a newbie, if you've never learned to define your own commands, now is a great time to start.
LATEST COMMENTS
MC Press Online