25
Thu, Apr
1 New Articles

Practical RPG: Managing Copy Files

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

The more you use APIs, the more you need copy files, and this article shows you how to manage them.

 

One of the greatest strengths of the IBM i is its ever-expanding collection of APIs. These APIs provide everything from system function access to database access to programming to utility functions used by other functions. Pretty much anything you can do from a command line or a menu you can do from an API, which allows you to embed those features into your ILE programs. However, in order to access all of those functions, you need to define the interface to each one, and that is best done through the use of copy files. The problem is that copy files can quickly get out of hand; this article presents a framework to keep them under control.

 

Reducing Maintenance

IBM's catalog of APIs is gigantic. There are literally thousands of APIs out there. Don't believe me? Go to the API finder for 7.2, type a blank into the name in the "Find by Name" section, and hit Enter. You'll find over 3,000 APIs.

 

To call these APIs from RPG, you'll need at minimum a prototype for the procedure and in many cases at least one (if not several) data structure definitions. Not to mention the fact that many of these APIs require other APIs. So using a single function may require several prototypes and a bunch of data structures. Not terrible, but what usually happens is that once you've used the function in one program, you'll need it somewhere else. After you've created those definitions and had to copy them manually into several programs, you'll get tired of it (especially if you need to make changes). So almost immediately you'll want a way to reduce the maintenance burden, and that's the copy file. For example, I use a lot of simple system APIs, including sleep and system. They don't really belong with anything else, so I include those prototypes in a simple utility copy file. Also, a lot of APIs use IBM's standard error data structure, so it makes sense to include that in that same utility file. I have a file like this in nearly every development environment I use. With minor tweaks, it usually ends up looking something like this:

 

     // Standard data structures

 

      dcl-ds api_ErrorDs template qualified;

       BytesPrv int(10);

       BytesAvl int(10);

       MessageId char(07);

       reserved char(01);

       MessageDta char(99);

      end-ds;

 

      // C Utilities

 

      dcl-pr strerror pointer extproc('strerror');

       errnum int(10) value;

      end-pr;

 

      dcl-pr sleep int(10) extproc('sleep');

       seconds uns(10) value;

      end-pr;

 

      dcl-pr system int(10) extproc('system');

       command pointer value options(*string);

      end-pr;

 

All I need to do is include that copy file, and now I have sleep and system, as well as the standard error data structure. Note that I used the qualified and template keywords on my included data structures; that means they don't take up compiler space and they also don't conflict with other variable names.

 

Next, I start grouping related APIs together, creating one copy file for each. And that way, I get something like this:

 

     /copy QCPYLESRC,DEF_IFS

     /copy QCPYLESRC,DEF_UTL

 

The above might be the /COPY statements for a program that monitors the IFS. It uses some IFS routines and also uses the strerror function to check for errors, so it includes two copy files. The name for your copy file members is entirely up to you, but I've found that using DEF_ as a prefix allows me to segregate out everything that's related to the copy file. It gives me only six characters for the rest of the member name, but we're RPG programmers; we can name anything with six characters! Seriously, that limitation isn't nearly as bad as it seems since you would typically need at most only a couple of dozen copy files.

 

But copy files by themselves can just as easily spiral out of control. Let me give you a simple example. There are many situations where I need a list of objects. One example is a list of output queues. Our IT staff is constantly adding or removing printer devices and output queues, and the easiest way for our users to select the right one is just to list all the objects of type *OUTQ. In the past, I've done that by doing DSPOBJD to a work file, and that's still a viable approach, but an alternative way is to use the object APIs (specifically QUSROBJD). This API in particular is a bit more complex than others because it also requires you to use the list APIs and the user space APIs, but that also makes it a really good one to learn because, once you've done so, all the other APIs begin to really fall into place (you'll find yourself using the user space and list APIs in many other situations).

 

API Dependence

And here's where it gets messy. Let's say I have a copy file for user spaces. And then I have another one for the list APIs. The list APIs require the use of a user space, so I have to also include the copy file for user spaces. So do I just assume that if someone is including the copy file for list APIs that they're also including the user space API definitions? Or do I make the list API copy file also include the user space copy file? That could work, but what if I need the user space APIs for another reason? Now I may attempt to include the same copy file twice, and that could be a problem. As you can see, this interdependence can get messy. But you can reduce that clutter that by using a copy file manager. This is a top-level copy file that controls all your other copy files.

 

So let's take a look at what we might have in that copy file. I'll stick with my naming convention of DEF_ for everything. In this case, the controlling copy file is called is called DEF_SYS. It's used to define all your system APIs.

 

      // Cross includes

      // These are used when one definition requires another

     /IF DEFINED(DEF_IFS)

     /DEFINE DEF_UTL

     /ENDIF

 

     /IF DEFINED(DEF_OBJ)

     /DEFINE DEF_LST

     /ENDIF

 

     /IF DEFINED(DEF_LST)

     /DEFINE DEF_USR

     /ENDIF

 

These first lines identify cross-functional dependencies. If you use the IFS routines, you need the utility functions. So if DEF_IFS is defined, then you will also define DEF_UTL. You may be a little confused by the concept of /DEFINE, but all that does is tell the compiler that some condition has been set to true. We'll see how that works in a moment.

 

      // Now bring in everything required

      // IFS Routines

     /IF DEFINED(DEF_IFS)

     /COPY QCPYLESRC,DEF_IFS

     /ENDIF

 

      // List processing

     /IF DEFINED(DEF_LST)

     /COPY QCPYLESRC,DEF_LST

     /ENDIF

 

      // Object processing

     /IF DEFINED(DEF_OBJ)

     /COPY QCPYLESRC,DEF_OBJ

     /ENDIF

 

    // User space processing

     /IF DEFINED(DEF_USR)

     /COPY QCPYLESRC,DEF_USR

     /ENDIF

 

      // Utilities

     /IF DEFINED(DEF_UTL)

     /COPY QCPYLESRC,DEF_UTL

     /ENDIF

 

This section of the copy file then includes all of the individual copy files. Each condition that is set includes another copy file. The copy files are included only once, so there is no concern about collisions or multiple inclusions. And the programs themselves need only worry about including the files that they actually use; required files are included automatically.

 

Using This in RPG Programs

Now that you have DEF_SYS set up, it's very easy to use in your application programs. Here's an example of the only lines of code required to add IFS support to an RPG program:

 

     /define DEF_IFS

     /copy QCPYLESRC,DEF_SYS

 

By adding these two lines to your RPG source, you will automatically include two copy files: DEF_IFS and the required file DEF_UTL. You define DEF_IFS and then incldue DEF_SYS, which does the rest of the work. In the cross include section, DEF_IFS causes DEF_UTL to be set. Then in the main include section, the copy files DEF_IFS and DEF_UTL are both included. I'l show you the two lines of code we need for object list support and leave it as an exercise for you to figure out which files get included:

 

     /define DEF_OBJ

     /copy QCPYLESRC,DEF_SYS

 

Encapsulation

Of course, even if you're managing your copy files properly, having all of these API calls in your application programs can add unnecessary technical complexity to your business logic. Someone who just wants a list of output queues doesn't need to know anything about user spaces; that's programming clutter that can confuse the issue. We can get around that by using encapsulation, in which we take all the complexities of the API calls and hide them within some application-friendly procedures. These procedures are typically placed into service programs and end up acting like your own business logic APIs. So instead of using the object APIs (and consequently the list and user space APIs) to get a list of printers, we might instead create our own simple API that returns a list of printers called, say, GetPrinters (we'll talk another day about naming conventions, but this is good enough for now). The prototype is very simple:

 

      dcl-pr GetPrinters;

       aPrinters char(10) dim(C_MAX_PRINTERS);

      end-pr;

 

The call is simply GetPrinters(aPrinters). That's it. Under the covers, GetPrinters calls the system APIs. There's no real need for error handling; GetPrinters can send an escape message if something goes really wrong. As I said above, you'll put this function into a service program, and you'll probably want a copy file for the definition of this function. Note the constant C_MAX_PRINTERS; that allows us to set an arbitrary limit to the number of printers and then simply recompile the affected programs if the number needs to be increased. That value would be in the copy file along with the prototype. All of our earlier discussions about copy file management still apply, but for the application programmer, the APIs are much less technical and much more application-oriented. Instead, our application program looks like this:


     /define DEF_PRINTERS

     /copy QCPYLESRC,DEF_APPL

      dcl-s aPrinters char(10) dim(C_MAXPRINTERS);

(...)

      GetPrinters(aPrinters);

 

Now all the complexity of the system APIs is hidden behind a simple intuitive call. So the code for GetPrinter uses the system APIs, and the application programs use our own application APIs. Both can be implemented via our copy file approach, and in the end your programming becomes much more manageable.

Joe Pluta

Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. He has been extending the IBM midrange since the days of the IBM System/3. Joe uses WebSphere extensively, especially as the base for PSC/400, the only product that can move your legacy systems to the Web using simple green-screen commands. He has written several books, including Developing Web 2.0 Applications with EGL for IBM i, E-Deployment: The Fastest Path to the Web, Eclipse: Step by Step, and WDSC: Step by Step. Joe performs onsite mentoring and speaks at user groups around the country. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it..


MC Press books written by Joe Pluta available now on the MC Press Bookstore.

Developing Web 2.0 Applications with EGL for IBM i Developing Web 2.0 Applications with EGL for IBM i
Joe Pluta introduces you to EGL Rich UI and IBM’s Rational Developer for the IBM i platform.
List Price $39.95

Now On Sale

WDSC: Step by Step WDSC: Step by Step
Discover incredibly powerful WDSC with this easy-to-understand yet thorough introduction.
List Price $74.95

Now On Sale

Eclipse: Step by Step Eclipse: Step by Step
Quickly get up to speed and productivity using Eclipse.
List Price $59.00

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: