Making RPG Even More “Special”

Simpler steps with INFDS and a look at Open Access

This is the promised follow-on to the March EXTRA article on SPECIAL files, “Get More from RPG.” Before we do anything else, we want to mention two things. First, several people have told us they have problems getting their heads around how SPECIAL files work. As with many new concepts, it’s often easier to understand when you see it running in debug mode. So to try to make things clearer, we’ve posted a video (link not active) of a debug session using this month's programs.

Second, we must apologize for some misinformation in that first article. We stated there that one of the disadvantages of SPECIAL files was that they had to be program described. Turns out we were wrong about that. SPECIAL files can indeed be externally described, as IBM’s Barbara Morris and Dan Cruikshank both pointed out to us. More on Dan’s comments in a moment.

Quite how we’ve remained under this misapprehension for all these years is beyond us. We’ve written multiple articles on the subject and given many presentations, and during all that time nobody has ever pointed it out. Sorry, but at least it’s nice to know that these old dogs can still learn new tricks!

The fact that we could use externally described files led us to consider that perhaps we could also make use of the INFDS to provide a simpler method of identifying the actions to be taken by the SPECIAL file handler. That turned out to be in the category of “useful but ...” as we’ll demonstrate. Once we’ve shared the differences in these new versions of the programs, we’ll take a quick look at how RPG’s new Open Access facility compares with the SPECIAL file.

Why Use the INFDS?

Many of our earlier explorations with SPECIAL files revolved around converting printer files to browser output. We’d always set up and passed an additional parameter to let the SPECIAL file determine the type of record being written. It occurred to us recently that if we specified an INFDS structure for the file, we’d then be able to simply pass that instead and utilize the file and/or record name within the structure to determine the action to be taken. This would make it far easier to write truly generic SPECIAL files that could adapt their behavior to the file and record in use.

Unfortunately, only part of the INFDS is available to be passed to our SPECIAL file program—specifically only those first 80 characters directly controlled by RPG. Additionally, for legacy reasons, the file and record names within that section are only eight characters long, not 10! This doesn’t nullify its use, but it does mean that it’s only really useful if the names you wish to use can be differentiated in the first eight characters. Needless to say, we’ll make sure our example fits that mold. You may be thinking that you can get the full 10-character names from the INFDS, but unfortunately, the longer names are only in the part of the INFDS not specifically controlled by RPG and therefore unavailable. (If you want details on this, see Chapter 5 of the RPG Reference Manual.) So, we’re stuck with the short versions of the fields.

Modifying the USESPECIAL Program

We’re implementing two primary features into our SPECIAL file example. First, we’ll use an externally described file (now that we know we can!), which simplifies the original set of programs because we don’t need the /Copy member to bring in the format of the data. Second, we’ll use the INFDS to illustrate the potential of adding some flexibility to the SPECIAL file program that wasn’t in the original version. In this example, the flexibility added was to allow the handling of two levels of detail about the IFS files and folders in the directory, based on the record format name specified. As we did in March, we’ll highlight sections of the code in this article, but if you would prefer to see the full source code you can download it from our website.

That said, let’s look at the changes we made to the program USESPECIAL from the March EXTRA. The first change was to modify the F-spec to use an external file description and to add the INFDS keyword. We also added a /COPY to bring in the INFDS definition. Our standard INFDS definition (ShortINFDS) only includes the fields, not the actual DS entry. This allows us the flexibility to add keywords to the DS we may need, such as QUALIFIED or BASED as we’ll do in this new example.

FLongDirE  IF   E             SPECIAL PgmName('DIRREADER2')
F                                     PList(DirReaderParm)
F                                     UsrOpn
F                                     INFDS(INFDS)
     
D INFDS           DS                  QUALIFIED
 /Copy ExtraSrc,ShortINFDS

The next change was to modify the READ operation to use the record format name (the previous version used the file name) so that the record name field in the INFDS would be populated by RPG and we could therefore reference it in the SPECIAL file.

   Read(E) LongRec;

Additionally, we extended the additional parameter list to pass the INFDS as the sixth parameter. (Remember from the earlier article that the PLIST only specifies the additional parameters beyond the four standard SPECIAL file parameters that are always passed.) The SPECIAL file program uses this additional information as we’ll show. The PLIST DirReaderParm now looks like this:

C     DirReaderParm PList                         
C                   Parm            directoryName 
C                   Parm            INFDS    

That’s it for the changes to this program. Now let’s look at the modifications to the SPECIAL file program itself.


Simpler steps with INFDS and a look at Open Access

Modifying the SPECIAL Program

Since we’re now receiving the INFDS as a parameter, we must establish a TEMPLATE for it so we can use the LIKEDS keyword to describe the parameter itself. Those of you who are “playing along” but aren’t yet on V6 can simply remove the TEMPLATE keyword without impact (except that your program will use more storage space).

 
D template_INFDS  DS                  Template 
 /Copy EXTRASRC,ShortINFDS

The parameter list received by the SPECIAL file must be extended to accommodate the INFDS, but we also made another small change in the interests of flexibility. Notice the definition of the fourth parameter—the record buffer (recordData). We’ve changed its definition to being a single character. Why? To maximize flexibility, we’ll populate BASED data structures, using the address of this field as the basing pointer. You can see below the two definitions of these BASED structures. This works because program parameters are actually passed as pointers. Since we’ll be accessing the data from this parameter via the BASED DSs, the length of the buffer parameter is irrelevant. It’s important to note that we can do this here because we’ll never actually pass data back to the USESPECIAL program by moving it to the recordData field. Notice also that we referenced our INFDS template to define the layout of the sixth parameter.

D DirReader       PI 
  // First four are the standard parameter definitions 
  //   passed for SPECIAL files from the RPG program 
D  action                        1a 
D  status                        1a 
D  error                         5s 0 
 // Length of data will actually be determined by which record
 //   layout we map to - the length here is irrelevant
D  recordData                    1a 
 // The extra parameters specified within the RPG program 
 //   The name of the directory to open and the INFDS
D  dirName                     640a   Varying 
D  INFDS                       LikeDS(template_INFDS) 
 
 // Definitions for directory entry, record layouts etc. 
D  entryName      S            640a   Varying 

D LongEntry     E DS                  ExtName(LONGDIRE)
D                                     Based(p_data) 
D                                     Qualified 

D ShortEntry    E DS                  Extname(SHORTDIRE)
D                                     Based(p_data) 
D                                     Qualified

The main changes to the code itself consist of setting the basing pointer for the record buffer and adding logic to determine which record we’re processing this time. These are the relevant sections of the changed code:

p_data = %Addr(recordData); // Set record basing pointer
...
If INFDS.RECORD = 'LONGREC'; 
  longEntry.type     = st_objtype; 
  ...
  longEntry.modified = adjustedEpoch + %seconds( st_mtime ); 
Else; 
  shortEntry.type = st_objtype; 
  shortEntry.name = entryName;
EndIf;

As you can see, we populate the record buffer using either the longEntry or shortEntry data structures depending on the record type in the INFDS. In the full code on our website, you’ll find a second program that uses this SPECIAL file, but which uses the SHORTDIRE file and therefore retrieves only a subset of the information.


Simpler steps with INFDS and a look at Open Access

Print

RPG Open Access

We’ve already shown the SPECIAL file program can be made to react to different file layouts etc. But it has to “know” about the record formats it’s using to react appropriately. This is due in part to the paucity of information passed to the SPECIAL file program by RPG. Certainly, through the use of APIs, it’s possible to get past this problem and retrieve the file layouts dynamically—but it’s not simple.

Open Access (OA) offers the possibility of getting around these kinds of problems by providing the complete details of each record layout to the handler program. These details are comprehensive, including the field name, size, data type, decimal places, date format, and of course the value of the field.

In fact, OA actually offers two modes of operation. The first is the one we’ve just described where full details of the file are passed to the handler. The second uses a simple buffer mapping strategy similar to that used with the SPECIAL files. Used in this mode, there are still significant advantages over SPECIAL files:

  • OA provides access to the full file and record names (not just the first eight characters).
  • OA can be utilized with all types of files including keyed files and display subfiles—it’s not limited to sequential access files.
  • OA can use operation codes such a CHAIN that are unavailable to SPECIAL files.

Another big difference between SPECIAL files and OA handlers is that OA allows the handler to provide much greater feedback to the using program in the form of I/O feedback values. SPECIAL files can only really signal conditions such as error, end-of-file and OK.

We’d be remiss to omit the fact that OA handlers can also be written as either subprocedures or programs. SPECIAL files can only be programs.

We’ll be working on a number of sample OA handlers over the coming months and sharing some of them with you here. Don’t hold your breath waiting for us to produce a generic 5250-to-browser handler though. There’s a reason why IBM involved ISVs such as Profound Logic and looksoftware early on in the development of OA—writing a comprehensive handler for such tasks is far from trivial and requires a great deal of experience in the arena. On the other hand, writing handlers to convert an external print file to output to a browser or an Excel spreadsheet is far more manageable. Other examples of the kinds of things we’ll be working on are handlers to enable reading and writing of IFS files, Web services and more. Hopefully many more people will be working on handlers and we should see some interesting results.

IBM’s Use of SPECIAL Files in Modernization Projects

Now back to IBM’s Dan Cruikshank. In addition to pointing out our mistake regarding externally described files, he told us about the way in which his group at IBM (DB2 for i Optimization and Training) use SPECIAL files to assist in converting traditional DDS sites to defining their databases with DDL and using SQL for data access. They’ve a number of SPECIAL file templates and use these to replace the use of an existing conventional DDS defined files. To make the system even more powerful, they pass additional parameters to the SPECIAL file to allow for the specification of selection criteria, and other SQL options such as ORDER BY and WITH clauses. In this way, they can provide the effect of Open Query files, logical files, etc., from a single program.

If you’re interested in these techniques, you might want to check out the modernization workshops offered by Dan’s group. They currently have two: Introduction to Data Centric Programming, which focuses on embedded SQL programming using more advanced database capability beyond DDS, and Data Access Modernization, which covers a modernization strategy including DDS to DDL, bridging techniques, identifying candidates for reengineering, etc. Contact Dan directly if you’re interested in these workshops.

That’s all from us this month. Have fun—life’s too short for work to be boring.