Now Where Did I Leave That Source Member?
September 14, 2011 Bruce Guetzkow
Note: The code accompanying this article is available for download here.
During execution, programs rely strictly on their compiled objects. The source used to create the object is not used in any way for execution. There are, however, some instances where source is of value during processing.
In a source member, one could store FTP commands to be used with the FTP (Start TCP/IP File Transfer) command, SQL statements to be used with the RUNSQLSTM (Run SQL Statements) command, or REXX statements to be used with the STRREXPRC (Start REXX Procedure) command. Knowing where the proper source exists is critical to executing those commands.
Each of those commands, and most others like them, allow you to specify the source file and indicate a library of *LIBL. When the commands execute, the library list is scanned and the first library containing the specified source file is used. Often this is sufficient to find the source member in question. However, if there are multiple libraries in the list with the same source file name, and the first library is not the one needed, then *LIBL won’t work. So, sometimes it works, sometimes it doesn’t. (See File Members and the Library List for more information.) You can, of course, hard code the proper library name, but hard coding leads to other problems.
What is needed is a programmatic solution, and IBM has given us just that in the form of an API: QUSRMBRD (Retrieve Member Description). By specifying the member name and source file (you can also specify a data file) with a library name of *LIBL, API will return the library that contains the source file and source member. Now if you have multiple libraries in your library list with the same source file name, you can be assured that you will be able to retrieve the source member needed. To simplify things further, I’ve created a simple procedure that you can install into a service program that can be bound into any program.
The procedure accepts just two parameters: the name of the source file (i.e., QSQLSRC), and the name of the member (i.e., MYMBRNAME). The procedure returns the name of the library, or fills the return value with asterisks if none is found. As currently written, you will need to pass both of the parameters in as uppercase values. You could also modify the procedure to convert the parameters to uppercase.
The procedure has only a few executable lines of code. First, the standard error data structure used by many IBM APIs is reset to default values. The API is then called. If errors are found, the library name is set to all-asterisks. Last, the library name is returned to the caller through a variable parameter, which you can use in the library parameter of the appropriate command.
The QUSRMBRD API has seven parameters. The last two are optional, but are used in this example:
You’ll find this utility in the downloadable code. There are two source file members: an RPG source member and the corresponding prototype.
That’s it. You can check out the documentation in the IBM Information Center for other values returned by this format and the other formats available in the event that you need more than just the library name. Happy source-hunting!
Bruce Guetzkow, an independent IBM i programming consultant with GmanTech Consulting in southeastern Wisconsin, is a firm believer in practical programming. For over 25 years he has developed applications for IBM systems from mainframe to System/36 to IBM i on Power. You can read his Website blog, follow him on Twitter (@gmantechi), or catch him at a meeting of the Wisconsin Midrange Computer Professional Association (WMCPA), where he is the current webmaster. Send your questions or comments for Bruce to Ted Holt via the IT Jungle Contact page.
File Members and the Library List