A (Relatively) Easy Way to Process Parameters as an Array
March 1, 2006 Dominic Lefevre
In Printing from Qshell, Ted Holt lamented that there seems to be no useful way to treat parameter lists in RPG programs and procedures as arrays, as the C language does. This need arises for me every once in a while, and while I’ve never found a completely satisfactory way to handle it, I have found a way that works. First, it might be good to provide an example in C, for those who do not know that language, in order to illustrate the advantage of treating parameters as an array. The following C program prints out the parameters that are passed to it. #include <stdio.h> main(int argc, char *argv[]) { int ndx; for (ndx = 1; ndx < argc; ndx++) { printf("Arg %2d is "%s"n", ndx, argv[ndx]); } return(0); } Notice that the main function receives two arguments from the caller. The first one, argc, is an integer value that tells the number of parameters that were passed to the program. It is the C counterpart to RPG’s %PARMS built-in function. The second one, argv, is an array of pointers to the parameters. The for loop presents a numbered list of the parameters. For example, suppose I call the program and pass it three parameters. CALL MYCPGM PARM('a' 'bb' 'ccc') This is the output I will see. Arg 1 is "a" Arg 2 is "bb" Arg 3 is "ccc" Now, think about this. How would you modify this C program if you wanted to pass four parameters to it? Five parameters? Ten parameters? Twenty? The answer is that you would not have to modify the program at all. The RPG convention is different. Each parameter must be given its own name and must be referenced by that name, rather than by its position in a list. If you want to pass additional parameters, you have to modify the parameter list and add additional code to process the added parameters. I cannot change the requirement to modify the parameter list, but I can avoid the additional code that must process the extra parameters. The following RPG program provides a simple illustration. I have designed it to process from zero to 12 parameters. D MYRPGPGM PR EXTPGM('MYRPGPGM') D Action01 LIKE(Action) OPTIONS(*NoPass) D Action02 LIKE(Action) OPTIONS(*NoPass) D Action03 LIKE(Action) OPTIONS(*NoPass) D Action04 LIKE(Action) OPTIONS(*NoPass) D Action05 LIKE(Action) OPTIONS(*NoPass) D Action06 LIKE(Action) OPTIONS(*NoPass) D Action07 LIKE(Action) OPTIONS(*NoPass) D Action08 LIKE(Action) OPTIONS(*NoPass) D Action09 LIKE(Action) OPTIONS(*NoPass) D Action10 LIKE(Action) OPTIONS(*NoPass) D Action11 LIKE(Action) OPTIONS(*NoPass) D Action12 LIKE(Action) OPTIONS(*NoPass) D MYRPGPGM PI D Action01 LIKE(Action) OPTIONS(*NoPass) D Action02 LIKE(Action) OPTIONS(*NoPass) D Action03 LIKE(Action) OPTIONS(*NoPass) D Action04 LIKE(Action) OPTIONS(*NoPass) D Action05 LIKE(Action) OPTIONS(*NoPass) D Action06 LIKE(Action) OPTIONS(*NoPass) D Action07 LIKE(Action) OPTIONS(*NoPass) D Action08 LIKE(Action) OPTIONS(*NoPass) D Action09 LIKE(Action) OPTIONS(*NoPass) D Action10 LIKE(Action) OPTIONS(*NoPass) D Action11 LIKE(Action) OPTIONS(*NoPass) D Action12 LIKE(Action) OPTIONS(*NoPass) D pAction S * D Action S 256A BASED(pAction) D pParms S * DIM(12) D N S 10U 0 /free pParms(01) = %ADDR(Action01); pParms(02) = %ADDR(Action02); pParms(03) = %ADDR(Action03); pParms(04) = %ADDR(Action04); pParms(05) = %ADDR(Action05); pParms(06) = %ADDR(Action06); pParms(07) = %ADDR(Action07); pParms(08) = %ADDR(Action08); pParms(09) = %ADDR(Action09); pParms(10) = %ADDR(Action10); pParms(11) = %ADDR(Action11); pParms(12) = %ADDR(Action12); DOU '0'; N += 1; IF N > %ELEM(pParms) OR N > %PARMS() OR pParms(N) = *NULL; LEAVE; ENDIF; pAction = pParms(N); // variable Action contains the current parm ENDDO; My technique is to imitate the behavior of C programs. That is, I load an array of pointers–pParms–with the addresses of the parameters. The %AADR function returns a null value for any parameter that is not passed. To access the Nth parm, I set pointer pAction to the address loaded in the Nth element of the array. Since the Action variable is based on the pAction pointer, the Action variable provides me access to the parameter. All of the following are legitimate CL calls to the RPG program. CALL MYRPGPGM CALL MYRPGPGM (&VAR) CALL MYRPGPGM (&VAR &SOMEVAR) CALL MYRPGPGM (&VAR &SOMEVAR &ANOTHERVAR) CALL MYRPGPGM (&V1 &V2 &V3 &V4 &V5 &V6 &V7 + &V8 &V9 &V10 &V11 &V12) In fact, the CALL command can pass more than 12 parameters. However, the RPG program will ignore the additional parameters. If I decide that the RPG program should accept more parameters, I must do the following:
But I do not have to add more calculation logic to handle the additional parameters. A couple more points are in order. First, it is not necessary to use an array of pointers. I could have just as easily, or perhaps more easily, defined pParms as an array of characters. However, doing so would have eaten up much more memory. Using pointers makes for a smaller program. Second, another way to pass an array of values to an RPG program is to create a CL command that includes a list parameter. However, this method is not always available. The Printing from Qshell article to which I referred earlier is a good example. |
I was working on program i need to pass the argv from c program to rpgle. I came across your post and i found a easy way to do the parameter passing. I’m positing it here for some one.
C Program:
#include
void C_PARMS(char **);
int main(int argc, char **argv) {
C_PARMS(argv);
}
RPGLE Module:
H NOMAIN
D c_parms Pr
D * Dim(100)
P c_parms B Export
D Pi
D P_Args * Dim(100)
*
D arg S 100A
D i S 2S 0
*
/Free
i = 1;
DoW (P_Args(i) *NULL);
arg = %Str(P_Args(i));
i += 1;
EndDo;
Return;
/End-Free
P c_parms E
Sorry for alignment.