17
Wed, Apr
5 New Articles

Command Prompting 201

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

Last month, in "Command Prompting 101," I talked about the command definition object and how you can use it to pass parameters more accurately than you can by calling them with the CALL command.

Now, I want to illustrate several important command definition features, including qualified parameters and simple list parameters.

Qualified Parameters

If you've ever entered a CPYF or DSPOBJD command, you've no doubt typed an object name using qualified syntax. This is second nature to iSeries developers and is pervasive throughout the CL commands. Qualified parameters are entered in one format, converted by the CL processor to a fixed structure, and then passed to your program. This structure is as follows: positions 1 to 10—object name; positions 11 to 20—library name.

In the previous article, the command definition interfaced with an OS/400 API that may be one of the few that does not expect a qualified object name. Instead, two separate parameters, 10 bytes each, are passed in: one with the object name, the other with the library name. Normally, the qualified syntax is used, and that's what we're going to review here.

When an object is qualified on a command, the library name is specified, followed by an object name. The object is qualified to the library with the forward slash (/) character. The CL processor uses this forward slash to parse the library/object name. If no slash is detected, only the object name is specified.

The CL processor isolates each component and stores it in a temporary variable that is subsequently passed to your program. For example, suppose a fictitious command named ENCFILE (Encrypt File) is specified as follows:

     encfile  file(ARDATA/PAYMENTS)

In RPG IV, the parameter structure might be similar to the following:

     D InFile          DS 
     D   FileName                    10A
     D   LibName                     10A

In the RPG IV program, by accessing the FILENAME and LIBNAME subfields, you can easily access the parameter values.

Of course, with virtually everyone running OS/400 V5R1 and later today (not many V4Rx users left), we would want to create a data structure template and then use that template as the parameter definition. So perhaps a better model might be as follows:

     D QualFile        DS                  Qualified
     D                                     Based(NULL)
     D  file                         10A
     D  name                         10A   Overlay(file)
     D  library                      10A   
     D  lib                          10A   Overlay(library)

Then, in our program, we would declare our input parameter:

     D myPgmName       PR
     D  toFile                             LikeDS(QualFile)

Within our program, we could access the data from that parameter using qualified data structure syntax, as follows:

       if (tofile.library = '*LIBL');
           //  do library list processing here.
      endif;
       if (tofile.file = '*ALL');
           //  They want them all!
      endif;

The key to creating a CL command with a qualified parameter is the QUAL statement. The QUAL statement is a command definition statement that is used in groups of two or more. That's right; you can have a qualified parameter with more than two values. Think of the DSPJOB command and its JOB parameter, which uses three QUAL statements. The QUAL statement is similar to the basic PARM statement but does not have parameter name attributes. That is because the name is specified on an associated PARM statement.

To create a QUAL parameter, you need to be able to specify multiple definitions for the same parameter. To do this, specify a user-defined name for the TYPE parameter of the PARM statement. For example, normally the following would define a typical parameter in a command definition:

   PARM       KWD(TOFILE) TYPE(*NAME) LEN(10) +      
               SPCVAL((*ALL)) MIN(1) EXPR(*YES) +   
               PROMPT('File name')  

The TYPE keyword of the PARM statement indicates the data type for the parameter. In this example TYPE(*NAME) is specified. The basic types include *CHAR, *DEC, *INT2, *INT4, and *DATE, but there are others.

When TYPE(*NAME) is specified, it is similar to specifying TYPE(*CHAR); however, *NAME has some built-in checking, so the value specified for this parameter must be in valid object name syntax. If it is not, the CL processor will reject it—no coding required by you.

But PARM only supports unqualified parameters. To allow a qualified file name for our user-written command, we must specify a user-supplied value for the TYPE keyword. Instead of TYPE(*NAME), we must specify TYPE(user-specified) where "user-specified" is any value up to 10 characters in length. Here's an example:

   PARM       KWD(TOFILE) TYPE(Q1) +      
               PROMPT('File name')  

The value TYPE(Q1) is specified for the parameter type. But since a type of Q1 does not exist, we need to create it. Doing so is so easy that you might ask yourself, "It can't be that simple, can it?" Yes, it can, and here it is:

 Q1:   QUAL       TYPE(*NAME) MIN(1) EXPR(*YES)  
       QUAL       TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL) +    
                    (*CURLIB)) EXPR(*YES) PROMPT('Library')  

What we've done is created a set of additional QUAL statements. These QUAL statements simply mean that each of their values is qualified to the other. The first QUAL statement includes a label. This label matches the TYPE keyword of the PARM statement. The qualified parameter continues until another label or another PARM statement is detected.

Now, let's put it all together. The following is the command definition syntax for a simple qualified parameter of library/object with the library name being optional.

       PARM       KWD(TOFILE) TYPE(Q1) + 
                    PROMPT('File name')  
 Q1:   QUAL       TYPE(*NAME) MIN(1) EXPR(*YES) 
       QUAL       TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL) +  
                    (*CURLIB)) EXPR(*YES) PROMPT('Library')

The following RPG IV code will receive this parameter:

     D qFile           DS                  Qualified
     D                                     Based(NULL)
     D  file                         10A
     D  name                         10A   Overlay(File)  
     D  Library                      10A   
     D  lib                          10A   Overlay(Library) 

     D myPgmName       PR
     D  toFile                             LikeDS(qFile)

     D myPgmName       PI
     D  toFile                             LikeDS(qFile)

Simple Lists

The next style of command parameter I want to illustrate is simple lists. These are fundamental to several commands, such as DSPOBJD's OBJTYPE parameter as well as the OBJ, LIB, DEV, and OBJTYPE parameters of the SAVOBJ command.

This one is very easy, but not very obvious. Here's how it works: To allow any parameter to be specified more than once, the PARM statement's MAX keyword must be specified. For example, the following command definition source allows an object type to be specified up to 10 times:

    PARM     KWD(OBJTYPE) TYPE(*CHAR) LEN(10) +      
               SPCVAL((*ALL)) MIN(1) MAX(10) +
               EXPR(*YES) +
               PROMPT('Object type')  

The parameter OBJTYPE is declared and must have at least one object type specified but no more than 10 object types specified. For example, the following are valid options for this parameter:

  myCMD   OBJTYPE(*FILE *LIB *DTAQ *PGM *SRVPGM)
  myCMD   OBJTYPE(*FILE *DTAQ)
  myCMD   OBJTYPE(*PGM *SRVPGM)
  myCMD   OBJTYPE(*LIB)
  myCMD   OBJTYPE(*ALL)

The OBJTYPE(*ALL) option is a special case. Notice that in the example PARM statement above, I included the SPCVAL((*ALL)) keyword. This forces the parameter to accept the value *ALL even though it may not conform to the rest of the parameter's criteria.

In our example, SPCVAL((*ALL)) is sort of out of place because no restrictions on object type, other than the length, have been specified. What would be more practical is if we could simply limit the object type to only *ALL if and when it is specified. This means that when OBJTYPE(*ALL) is specified, no other object types are allowed. Well, we can do that. Simply change SPCVAL (special value) to SNGVAL (single value), and that handles it for us:

    PARM     KWD(OBJTYPE) TYPE(*CHAR) LEN(10) + 
               SNGVAL((*ALL)) MIN(1) MAX(10) +
               EXPR(*YES) +
               PROMPT('Object type')  

One final adjustment: It doesn't "cost" anything to allow more or fewer parameters via the MAX keyword. If the application may accept a large number now or sometime in the future, you can specify the larger limitations today rather than change things later on. Command definition parameters accept up to 300 for the MAX keyword, so let's change it to 300:

PARM KWD(OBJTYPE) TYPE(*CHAR) LEN(10) +
SNGVAL((*ALL)) MIN(1) MAX(300) +
EXPR(*YES) +
PROMPT('Object type')

The RPG IV code to process this kind of parameter is much easier than the old RPG III code was. RPG IV is so much more capable that I wouldn't even try this today with RPG III.

When a list is passed as a parameter, it is prefixed with a 2-byte integer (5i0 value) that identifies the number of items the end-user specified for the parameter. This is the number of items on your list parameter.

The following data structure can be used to declare this type of parameter. If desired, change the subfield named OBJ to the name of the parameter itself, and modify its data type and length to match that of your own command's parameter definition.

     D ItemList        DS                  Qualified
     D                                     Based(NULL)
     D  count                         5i 0
     D  OBJ                          10A   Dim(300) 

To declare this as a parameter, use the following code in an RPG IV source member:

     D myPgmName       PR
     D  objlist                            LikeDS(itemList)

     D myPgmName       PI
     D  objlist                            LikeDS(itemList)

To access the elements of the list parameter, use qualified array syntax, like so:

     for  i = 1 to objlist.count;
          myType = objlist.obj(i); // Get the list item
          //  TODO: Do something with it.
     endfor;

By combining qualified parameters and list parameters, you can handle 80 percent of all situations involving complex parameters definitions. In the next issue of RPG Developer, we'll go over the less-often-used mixed or "element" list parameters.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

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: