Accessing Command-Line Arguments in Open PL/I Programs

It is sometimes useful to be able to execute a program specifying one or more arguments on the command line in order to cause a variation in the program's behavior. For example, to run a departmental expense report program, you might issue the command deptexp summary to indicate that you wish to see only category totals rather than all the line items.

Command-line arguments can be accessed in a PL/I main program using the following technique:

/* PL/I main program accessing command line args. */ 

PMAIN: PROCEDURE OPTIONS(MAIN); 

   dcl getargc entry() returns(fixed bin(31)) external('__lpi_get_argc' ); 
   dcl getargv entry() returns(pointer) external('__lpi_get_argv' );    
   DECLARE ARGC_     FIXED BIN(31) NATIVE init(getargc()); 
   DECLARE ARGV_     POINTER              init(getargv());
   DECLARE ARG(10)   POINTER BASED; 
   DECLARE ARGS      CHAR(50) BASED; 
   DECLARE ARGSV     CHAR(50) VARYING; 
   DECLARE I         FIXED BIN(31); 

   PUT SKIP EDIT(ARGC_)(f(2)); 
   DO I = 1 TO ARGC_; 
      ARGSV = SUBSTR(ARGV_->ARG(I)->ARGS,1,
            INDEX(ARGV_->ARG(I)->ARGS,BYTE(0))-1); 
      PUT SKIP EDIT('"',argsv,'"')(a,a,a); 
      END; 
   PUT SKIP; 

   END PMAIN;

If your PL/I program is controlled by a C main program, you can pass the command-line arguments through to the PL/I program in the following manner:

/* C main program calling PL/I subroutine */ 

main(argc,argv) 
   int argc; 
   char **argv;
{ 
   PSUBR(argc,argv); 
}

/* PL/I subroutine receiving command line args 
   from C main program */ 

PSUBR: PROCEDURE(ARGC,ARGV); 

   DECLARE ARGC      FIXED BIN(31) VALUE;
   DECLARE ARGV      POINTER VALUE; 
   DECLARE ARG(10)   POINTER BASED; 
   DECLARE ARGS      CHAR(50) BASED; 
   DECLARE ARGSV     CHAR(50) VARYING; 
   DECLARE I         FIXED BIN(31); 

   PUT SKIP EDIT(ARGC)(f(2)); 
   DO I = 1 TO ARGC; 
      ARGSV = SUBSTR(ARGV->ARG(I)->ARGS,1, 
            INDEX(ARGV->ARG(I)->ARGS,BYTE(0)));
      PUT SKIP EDIT('"',ARGSV,'"')(a,a,a); 
      END; 
   PUT SKIP; 

END PSUBR;