Wednesday, October 23, 2024

Retrieving the library that contains the currently running program

The idea for this post came from a question. The questioner had a situation where, due to a mixed-up library list, someone had run the wrong version of a program from another library. They already had the program's name on their display files, and they asked was it possible to retrieve the library the current executing program is in, and add that to the their display files.

After some discussion we decided that any change would be needed in both RPG and CL programs. Fortunately, this is simple in both languages.

Before I start explaining my RPG and CL code I need to have a display file within which are fields for the program and the program's library. I created a simple display file to do this, called TESTDSPF:

01 A                                      DSPSIZ(24 80 *DS3)
02 A          R SCREEN
03 A                                  3  2'Program name  . :'
04 A            PGMNAME       10   O  3 20
05 A                                  4  2'Program library :'
06 A            PGMLIB    R        O  4 20REFFLD(PGMNAME  *SRC)

Line 4: This is the display file's field for the program name.

Line 6: This field is for the program's library. I have defined it using the REFFLD to have the same attributes as the program name field.

Let me start with the RPG program. RPG's program status data structure has a subfield that contains this information. My program using the program status data structure looks like:

01  **free
02  ctl-opt main(Main) dftactgrp(*no) ;

03  dcl-f TESTDSPF workstn ;

04  dcl-ds Pgm psds qualified ;
05    ProgramName char(10) pos(1) ;
06    ProgramLibrary char(10) pos(81) ;
07  end-ds ;

08  dcl-proc Main ;
09    PGMNAME = Pgm.ProgramName ;
10    PGMLIB = Pgm.ProgramLibrary ;

11    exfmt SCREEN ;

12  on-exit ;
13    close *all ;
14  end-proc ;

Line 2: I have written this program with a Main procedure, therefore, it cannot be run in the default activation group.

Line 3: Here is the definition of the display file, TESTDSPF.

Line 4 – 7: Rather than include my "copybook" of the RPG status data structure here, I have just defined the subfields I am interested in.

Line 5: I am interested in retrieving the program's name.

Line 6: And the library this program is in.

Line 8: Start of the main procedure.

Lines 9 and 10: Move the program status data structure subfields to the display file's fields.

Line 11: Display the display file's record format.

Line 12: Start of the On exit group for the Main procedure. This will be executed if the program ends normally or abnormally.

Line 13: As the display file was opened in a procedure it is not closed when the program ends. I need to close it. I use the close all to close all open files in one operation.

After compiling this program, TESTRPG, I call it, and the following displayed:


 Program name  . : TESTRPG
 Program library : MYLIB

In an earlier post I showed how I could get the current program's name and library in a CL program using the Materialize program name MI program, _MATPGMNM. The program below is based upon what I described there:

01  PGM

02  DCL VAR(&DATA) TYPE(*CHAR) LEN(80)
03  DCLF FILE(TESTDSPF)

04  CHGVAR VAR(%BIN(&DATA 1 4)) VALUE(80)
05  CHGVAR VAR(%BIN(&DATA 5 4)) VALUE(80)
06  CHGVAR VAR(%BIN(&DATA 9 4)) VALUE(0)
07  CHGVAR VAR(%BIN(&DATA 13 4)) VALUE(0)

08  CALLPRC PRC('_MATPGMNM') PARM((&DATA))

09  CHGVAR VAR(&PGMNAME) VALUE(%SST(&DATA 51 10))
10  CHGVAR VAR(&PGMLIB) VALUE(%SST(&DATA 19 10))

11  SNDRCVF

12  ENDPGM

Line 2: This is the parameter used by _MATPGMNM.

Line 3: Definition of the display file.

Lines 4 – 7: Changing the parameter field to contain the binary numbers necessary. I am sure they must mean something, alas, I just know they have to be those values in those places for _MATPGMNM to return a valid result.

Line 5: Call _MATPGMNM.

Lines 9 and 10: Substring the program name and library from the parameter field into the display files' fields.

Line 11: Display the display file's only record format. As there is only one record format in the display file I do not have to give its name in the SNDRCVF command.

After compiling this program, TESTCL, I can then call it. The following is displayed.


 Program name  . : TESTCL
 Program library : MYLIB

This shows how simple the change is. We talked about only changing some of the questioner's programs, and even that will take them some time to do.

 

This article was written for IBM i 7.5, and should work for some earlier releases too.

2 comments:

  1. Hi Simon. A module with just a linear main procedure (MAIN keyword) can be in the default activation group.
    - Barbara

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.