Manipulating the Library List from Within RPG

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

Even though many people say the iSeries 400 is a closed and proprietary system, it does offer a huge number of "openness" application program interfaces (APIs).

I remember years ago when IBM Rochester held the first meetings to discuss the idea of adding APIs to the system. The first set of APIs included User Space, User Index, and Data Queue APIs. At the time, all the API names were designed to begin with the letters QUS. The "US" identifier stood for "USER"--IBM's term for "programmer" back then.

Today, there are so many APIs that the manual itself is over 3,000 pages long! I don't know the exact number of APIs available in OS/400, but it's hundreds and hundreds if not well over a thousand by now. So the naming convention of QUSxxxxx has long since been retired.

Rather than attempt to keep track of all the APIs and their purpose in life, I've tried to maintain an easy way to search and review the APIs I may need. I keep a copy of the OS/400 System API Reference manual in PDF form on my PC. When a new release of OS/400 becomes available, I usually download an updated copy of the API manual. It is available at http://publib.boulder.ibm.com/html/as400/v5r1/ic2924/books/c4158000.pdf.

What Good Are APIs?

APIs provide a program interface to system routines. Just like the CL command interface provides access to virtually every system function, APIs provide a way for high-level language (HLL) programs to run these same types of system functions.

For example, suppose you need to retrieve a system value while you're inside an RPG IV program. You could simply write a CL program that calls the RTVSYSVAL command and then return the corresponding value to the RPG program through a parameter; nothing wrong with that.

Using APIs, however, you could simply call the same interface that the RTVSYSVAL command uses from within RPG. The API is QWCRSVAL. A cool thing about the APIs is that added function is often provided. For example, the QWCRSVAL API allows you to retrieve several system values simultaneously. You could, for example, retrieve all the password-related system values with one call to QWCRSVAL.

Two very interesting APIs are the Retrieve Job Information API (which I use to retrieve the library list) and the Change Library List) API. These allow you to, as expected, retrieve or change the library list from within an HLL. But the great thing about these APIs, particularly the Change Library List API, is that they allow you to actually set the so-called product libraries as well as the current library and the library list.

Change Library List (QLICHGLL)

QLICHGLL allows you to modify the library list for your job. As I mentioned, you can change the regular "user portion" of the library list as well as the two product libraries and the current library.

Parameter
Input/Output
Definition
Current library
Input
Char(11)
Product library 1
Input
Char(11)
Product library 2
Input
Char(11)
User library list
Input
Array of Char(11)
User library list count
Input
Int(4) in RPG 9B0 or 10i0
Standard API error data structure
Output
Char(16)

The first parameter is the current library name. It must be specified (it cannot be blank), and it must be left-justified with a trailing blank. In addition to any valid library name, the current library parameter accepts the following special values:

*SAME--The current library is not changed.

*CRTDFT--There is no current library. The CRTxxxPGM command will use QGPL as the default library name.

The product library parameters have the same restrictions as the current library parameter. In addition, the product library parameters accept the following special values:

*NONE--The product library (if any) is removed. Either product library 1 or 2 may have this value.

*SAME--The product library is not changed. Either product library 1 or 2 may have this value.

The user library list parameter is an array of 11-position character values. Each array element may contain a library name, left-justified with a trailing blank in position 11.

The user library list count parameter tells the API how many library names have been specified in the User Library List. Effectively, this is the number of used array elements. This value is a 4-byte binary or 4-byte integer field.

The final parameter is the standard API error data structure. Often, we just ignore that parameter by passing it a value of X'00'.

The following CPF error messages may be generated by the QLICHGLL API:
CPF2106 Library list not changed.
CPF2110 Library &1 not found.
CPF2113 Cannot allocate library &1.
CPF2133 First product library on library list destroyed.
CPF2134 Second product library on library list destroyed.
CPF2137 Current library on library list destroyed.
CPF2176 Library &1 damaged.
CPF2182 Not authorized to library &1.
CPF2184 Library list not replaced.
CPF219A Library QTEMP cannot be specified.
CPF219F Number of libraries for library list not valid.
CPF24B4 Severe error while addressing parameter list.
CPF3C90 Literal value cannot be changed.
CPF3CF1 Error code parameter not valid.
CPF9872 Program or service program &1 in library &2 ended. Reason code &3.

Retrieve Job Information (QUSRJOBI)

The QUSRJOBI API is an old API, but it has been continually updated throughout the years. QUSRJOBI can retrieve most information about the current running job, including the library list.

Parameter
Input/Output
Definition
Library list
Output
Char(*)
Length of first parameter variable
Input
Int(4)
Format name
Input
Char(8) 'JOBI0700'
Qualified job name
Input
Char(26)
Internal job identifier
Input
Char(16)
Standard API error data structure
Output
Char(16)

The first parameter, library list, is a return value--a data structure that contains all the libraries on the library list, including system portion, user portion, product libraries, and the current library name. Note that the QLICHGLL API does not support changing the system portion of the library list. But that is typically never done in production code, so this shouldn't be an issue.

The second parameter is the length of the data structure being used to receive the library list (i.e., the length of the first parameter).

The third parameter is the name of the format used by the API. This format controls what type of information the API returns. The QUSRJOBI API can return a lot of different information, so the convention for this and many other APIs is to accept a format ID that indicates which pieces of information the API should return. To retrieve the library list, format ID 'JOBI0700' is passed.

The fourth parameter is the qualified job name for the job whose information is being retrieved. Yes, you can retrieve the library list for another job if you want. But a more practical use is to retrieve the library list for the current job. So rather than pass in a 26-byte data structure simply create a field that is 26 positions in length and set the first character of the field to an asterisk (*). Leave the rest of the field blank.

The fifth parameter is the internal job name. Leave this parameter blank.

The sixth and final parameter is the standard API error data structure.

Wrapping It All Up

Logically, we would need to call QUSRJOBI to retrieve the current library list, modify the list, and then call QLICHGLL to set the list to what we want.

But wouldn't it be more useful to have this function wrapped in a usable RPG IV procedure? Listed in Figure 1 is the RPG IV source for two procedure prototypes, and they are:

GETLIBL--Retrieve the library list for the job.

SETLIBL--Change the library list to the new list specified.

To use the procedures, you need to include the prototypes for them in your code using either the /COPY or /INCLUDE compiler directives.

      ** GETLIBL - Retrieve the specified library list
      **   Usage:  libl = GetLibl( list-type : rtn-Count )
      **   Note: all parameters are optional.

      **   Return Value:
      **     LIBL - An array that receives the library list.
      **   Parameters:
      **     RTN-COUNT - An optional variable that recieves
      **                 a count of the number of library returned.
      **     LIST-TYPE - An optional parameter that controls which
      **                 library list is to be returned. The named
      **                 constants in the LIBL include member may
      **                 be used for this value.

     D GetLibl         PR            11A   DIM(250)
     D  lType                         5I 0 Const Options(*NOPASS)
     D  nLiblCount                    5I 0 Options(*NOPASS)


      ** SETLIBL – Set (change) the library list
      **   Usage:  libl = SetLibl( libl : list-type )

      **   Return Value:
      **     Reserved -  Nothing returned at this time.
      **   Parameters:
      **     LIBL      - An array of 11-character elements.
      **                 Each library name to be placed on the library
      **                 must be stored in a corresponding element.
      **     LIST-TYPE - An optional parameter that controls which
      **                 library list is to be changed. The named
      **                 constants in the LIBL include member may
      **                 be used for this value. The default is
      **                 the user portion of the library list.
     D SetLibl         PR             1N
     D  Libl                         11A   Const DIM(250)
     D  lType                         5I 0 Const Options(*NOPASS)


      ** ADDLIBLE – Add Library List Entries
      **   Usage:  count = AddLiblE( libl : pos-lib : before/after-flag) )

      **   Return Value:
      **     count     - A count of the number of libraries on the library list.
      **   Parameters:
      **     LIBL      - A character string of one or more library 
      **                 names separated by one or more blanks.
      **     POSLIB    - An optional parameter that may contain the 
      **                 name of a library used to control the placement
      **                 of the library names being added to the list.
      **                 Special values *FIRST, and *LAST may also be 
      **                 specified to insert the library names at the start
      **                 or end of the library list.
      **     POS       - An optional parameter that indicates the order
      **                 in which the new library name(s) are inserted
      **                 into the library list. *ON = BEFORE the POSLIB
      **                                        *OFF = AFTER the POSLIB
     D AddLiblE        PR            10I 0
     D  InLibl                     2750A   Const VARYING
     D  PosLib                       10A   Const VARYING
     D                                     Options(*NOPASS)
     D  Pos                           1N   Const
     D                                     Options(*NOPASS)

      ** Library list types (parameter 1) named constants
     D llUSRLIBL       C                   Const(0)
     D llCURLIB        C                   Const(1)
     D llSYSLIBL       C                   Const(2)
     D llPRODLIBL      C                   Const(3)

Figure 1: Prototypes for Library List Procedures

Listed in Figure 2 is the RPG IV source for two procedures that wrap the functionality of these two APIs to provide a simplified interface to their function.

     H NOMAIN
      /COPY QRPGLESRC,LIBL

      ** Maximum libraries allowed on library list
     D MAXLIBL         C                   Const(250)
      ** Field reference for Library Name (APIs use 11-pos name)
     D LIBNAME         S             11A
      ** API Error data structure
     D ll_Error        DS                  EXPORT
     D  ll_DSLen                     10I 0 Inz(%Size(ll_Error))
     D  ll_ByteRtn                   10I 0 Inz
     D  ll_cpfmsgid                   7A
     D  ll_reserved                   1A
     D  ll_msgdata                  132A

     D QUSRJOBI        PR                  EXTPGM('QUSRJOBI')
     D  libl                       6000A   OPTIONS(*VARSIZE)
     D  nLibl                        10I 0 Const
     D  FmtName                       8A   Const
     D  Job                          26A   Const
     D  InternJob                    16A   Const
     D  Api_Error                    16A

     D QLICHGLL        PR                  EXTPGM('QLICHGLL')
     D  CurLib                       11A   Const
     D  ProdLib1                     11A   Const
     D  ProdLib2                     11A   Const
     D  UsrLibl                      11A   Const DIM(MAXLIBL)
     D  nUsrLiblCount                10I 0 Const
     D  Api_Error                    16A

     ** ------------------------------------------
     ** G E T L I B L -- (Get Library List)
     ** ------------------------------------------
     P GetLibl         B                   EXPORT
      ** GETLIBL - Retrieve the specified library list
      **   Usage:  libl = GetLibl( list-type : rtn-Count )
      **   Note: all parameters are optional.

      **   Return Value:
      **     LIBL - An array that receives the library list.
      **   Parameters:
      **     RTN-COUNT - An optional variable that recieves
      **                 a count of the number of library returned.
      **     LIST-TYPE - An optional parameter that controls which
      **                 library list is to be returned.
      **                 Use the named constants in the prototype
      **                 include member for the LIST-TYPE.
     D GetLibl         PI            11A   DIM(250)
     D  lType                         5I 0 Const Options(*NOPASS)
     D  nLiblCount                    5I 0 Options(*NOPASS)

      ** Return value is returned in LIBLARRAY
     D Libl            DS
     D* LiblArray                          DIM(LMAXLIBL) LIKE(LIBNAME)
      ** Due to a bug in the RPG compiler, a global named constant
      ** cannot be specifed on the DIM keyword. :(
     D  LiblArray                          DIM(250) LIKE(LIBNAME)

     D nCount          S             10I 0
     D nSysLSize       S             10I 0
     D nUsrLSize       S             10I 0
     D nCurLSize       S             10I 0
     D nProdLSize      S             10I 0

     DLiblDS           DS                  INZ
     D nBytesRtn                     10I 0
     D nBytesAvail                   10I 0
     D j_Name                        10A
     D j_User                        10A
     D j_Nbr                          6A
     D ij_ID                         16A
     D j_Status                      10A
     D j_Type                         1A
     D j_subtype                      1A
     D j_Reserved                     2A
     D nSysLib                       10I 0
     D nProdLib                      10I 0
     D nCurLib                       10I 0
     D nUsrLib                       10I 0
     D LibLists                    3100A

     D SysLibl         s           2750A   Based(pSysLibl)
     D ProdLibl        s           2750A   Based(pProdLibL)
     D CurLib          s           2750A   Based(pCurLib)
     D UsrLibl         s           2750A   Based(pUsrLibl)

     C                   CALLP     QusRJobI(LiblDS : %Size(LiblDS):
     C                              'JOBI0700' : '*' : ' ' : ll_Error)

     C                   Eval      nSysLSize  = nSysLib  * %size(LibName)
     C                   Eval      nProdLSize = nProdLib * %size(LibName)
     C                   Eval      nCurLSize  = nCurLib  * %size(LibName)
     C                   Eval      nUsrLSize  = nUsrLib  * %size(LibName)

     C                   Eval      pSysLibl  = %addr(LibLists)
     C                   Eval      pProdLibl = pSysLibl + nSysLSize
     C                   Eval      pCurLib   = pProdLibl + nProdLSize
     C                   Eval      pUsrLibl  = pCurLib + nCurLSize

     C                   Select
     C                   When      %Parms = 0 or lType = llUsrLibl
     C                   eval      Libl = %subst(UsrLibl : 1 : nUsrLSize)
     C                   eval      nCount = nUsrLib
     C                   When      lType = llCurlib
     C                   eval      Libl = %subst(CurLib : 1 : nCurLSize)
     C                   eval      nCount = nCurLib
     C                   When      lType = LLSysLibl
     C                   eval      Libl = %subst(SysLibl : 1 : nSysLSize)
     C                   eval      nCount = nSysLib
     C                   When      lType = llPRODLIBL
     C                   eval      Libl = %subst(ProdLibl : 1 : nProdLSize)
     C                   eval      nCount = nProdLib
     C                   other
     C                   eval      Libl = %subst(UsrLibl : 1 : nUsrLSize)
     C                   eval      nCount = nUsrLib
     C                   endsl
     C                   if        %Parms >= 2
     C                   Eval      nLibLCount = nCount
     C                   endif
     C                   return    LiblArray
     P GetLibl         E

      ** ----------------------------------------
      ** S E T L I B L - Change Library List
      ** ----------------------------------------
     P SetLibl         B                   EXPORT
     D SetLibl         PI             1N
     D  InLibl                       11A   Const DIM(250)
     D  lType                         5I 0 Const Options(*NOPASS)

     D CurLib          s                   Like(LIBNAME)
     D ProdLib1        s                   Like(LIBNAME)
     D ProdLib2        s                   Like(LIBNAME)

     D i               S             10I 0 Inz(0)

     C                   Dow       InLibl(i+1) <> *BLANKS
     C                   Eval      i = i + 1
     C                   enddo
     C                   Select
     C                   When      %Parms < 2 or lType = llUSRLIBL
     C                   CallP     QliChgLL('*SAME':'*SAME':'*SAME':
     C                                      InLibl : i :
     C                                      ll_Error )
     C                   When      lType = llCURLIB
     C                   Eval      CurLib  = InLibl(1)
     C                   CallP     QliChgLL(CurLib :'*SAME':'*SAME':' ': -1
     C                                    : ll_Error )
     C                   When      lType = llPRODLIBL
     C                   Eval      ProdLib1 = InLibl(1)
     C                   Eval      ProdLib2 = InLibl(2)
     C                   If        ProdLib2 = *BLANKS
     C                   Eval      ProdLib2 = '*SAME'
     C                   endif
     C                   CallP     QliChgLL('*SAME':ProdLib1:ProdLib2:' ':-1
     C                                    : ll_Error )
     C                   endsl
     C
     C                   return    *ON
     P SetLibl         E

      ** ----------------------------------------------
          A D D L I B L E  -  Add Library List Entries
      ** ----------------------------------------------
     P AddLiblE        B                   EXPORT

     D AddLiblE        PI            10I 0
     D  InLibl                     2750A   Const VARYING
     D  PosLib                       10A   Const VARYING
     D                                     Options(*NOPASS)
     D  Pos                           1N   Const
     D                                     Options(*NOPASS)

     D  AddLibl        S                   DIM(250) LIKE(LIBNAME)
     D  OrgLibl        S                   DIM(250) LIKE(LIBNAME)
     D  NewLibl        S                   DIM(250) LIKE(LIBNAME)

      ** *ON = place new lib(s) after otherwise place before
     D lAfter          S              1N   INZ(*OFF)
     D PlaceLib        S                   LIKE(LIBNAME) INZ('*FIRST')
     D i               S             10I 0
     D x               S             10I 0
     D nPos            S             10I 0
     D nStart          S             10I 0
     D nLiblCount      S              5I 0
     D nAddLiblCount   S              5I 0
     C                   if        %Parms >= 3
     C                   Eval      lAfter = Pos
     C                   endif
     C                   if        %Parms >= 2
     C                   Eval      PlaceLib = PosLib
     C                   endif

     C                   Eval      nStart = 1
     C                   Eval      OrgLibl = GetLibl(llUsrLibl: nLiblCount)
     C                   Eval      nPos = %Scan(' ':InLibl)
     C                   Dow       nPos > 0 and i < MAXLIBL
     C                   Eval      i = i + 1
     C                   Eval      AddLibl(i) = %subst(InLibl : nStart :
     C                                               (nPos - nStart)+ 1)
     C                   Eval      nStart = nPos+1
     C                   Eval      nPos = %Scan(' ':InLibl:nStart)
     C                   enddo
     C                   If        %Len(InLibl) > nStart
     C                   Eval      i = i + 1
     C                   Eval      AddLibl(i) = %subst(InLibl : nStart :
     C                                           (%Len(InLibl)- nStart)+ 1)
     C                   endif

     C                   eval      nAddLiblCount = i
      ** Insert the new library(s) at the beginning
      ** of the existing library list
     C                   if        PlaceLib = '*FIRST'
     C                   eval      NewLibl = AddLibl
     C                   Eval      x = nAddLiblCount + 1
     C*                  eval      NewLibl(x) = OrgLibl
     C                   MOVEA     OrgLibl       NewLibl(x)
     C                   Eval      nLiblCount = nAddLiblCount + nLiblCount
     C                   else

     ** Insert before or after an existing library name
     C                   Eval      X = 1
     C                   for       i = 1 TO nLiblCount

      ** Detect match on Placement-Libary name?
     C                   if        OrgLibl(i) = PlaceLib
      ** Insert *AFTER
     C                   if        lAfter
      ** Copy the Placement Library to the list
     C                   Eval      NewLibl(x) = OrgLibl(i)
     C                   Eval      X = X + 1
      ** Add the new library names after the placement library
     C                   MOVEA     AddLibl       NewLibl(x)
      ** Copy the rest of the original library list
     C                   Eval      X = X + nAddLiblCount
     C                   Eval      i = i + 1
     C                   MOVEA     OrgLibl(i)    NewLibl(x)

      ** Insert *BEFORE
     C                   else
      ** Add the new library names before the placement library
     C                   MOVEA     AddLibl       NewLibl(x)
     C                   Eval      X = X + nAddLiblCount
      ** Copy the Placement Library to the list
     C                   Eval      NewLibl(x) = OrgLibl(i)
      ** Copy the rest of the original library list
     C                   Eval      X = X + nAddLiblCount
     C                   Eval      i = i + 1
     C                   MOVEA     OrgLibl(i)    NewLibl(x)
     C                   endif
      ** Done processing the library list
     C                   LEAVE

      ** NO-match on Placement-Libary name
     C                   else
      ** Just copy the library to the new list
     C                   Eval      NewLibl(x) = OrgLibl(i)
     C                   Eval      X = X + 1
     C                   endif
     C                   endfor

     C                   eval      nLiblCount = X - 1
     C                   endif
      ** Now do a Change Library List via the QLICHGLL API
     C                   CallP     SetLibl(NewLibl)
     C                   Return    X - 1
     P AddLiblE        E

Figure 2: Source code for Library List Procedures

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$