IBM Support

Example of Using QzshSystem() to Run a Qshell Command in an RPG ILE Program

Troubleshooting


Problem

The information in this document will illustrate how to use the QzshSystem() in an RPG ILE program.

Resolving The Problem

Caution: This is an example only. IBM accepts no responsibility for its correctness.

Note: To use the QzshSystem() API, you need to first open three temporary files and verify that those files get descriptor numbers 0, 1, and 2 before calling the API. You must close the files before the program ends.

The following is the source for RPG program OPENSTDIO to open the three standard I/O file descriptors stdin (0), stdout (1), and stderr (2).

You should call this program as the first step in your job. If any other activity in the job opens one of these file descriptors incorrectly, this program will fail and your Java or QSHELL call might not work correctly.

Create the ILE RPG program called OPENSTDIO. The following is the ILE RPG source code, source member type RPGLE. You can compile it using Option 14 in PDM or by using the CRTBNDRPG command.

The main procedure of the program ends with some rudimentary code to report the error; it uses a DSPLY operation and then it calls the *PSSR subroutine with ENDSR *CANCL which causes an exception to be sent to the program's caller. You may want to replace this code with a better mechanism for reporting the error.


Here is the RPG ILE program that will open the three standard I/O file descriptors:

     h thread(*serialize) bnddir('QC2LE') dftactgrp(*no)

     D O_CREAT         C                   x'00000008'
     D O_TRUNC         C                   x'00000040'
     D O_RDONLY        C                   x'00000001'
     D O_WRONLY        C                   x'00000002'
     D O_RDWR          C                   x'00000004'

     D O_ACCMODE       c                   %BITOR(O_RDONLY
     D                                          : %BITOR(O_WRONLY
     D                                                 : O_RDWR))

     D S_IRUSR         C                   x'0100'
     D S_IROTH         C                   x'0004'
     D S_IWUSR         C                   x'0080'
     D S_IWOTH         C                   x'0002'

     D chk             pr              n
     D   descriptor                  10i 0 value
     D   mode                        10i 0 value
     D   aut                         10i 0 value
     D   other_valid_mode...
     D                               10i 0 value

     D ok              s               n

      /free
       // Validate or open descriptors 0, 1 and 2
       ok = chk (0
               : 0 + O_CREAT + O_TRUNC + O_RDWR
               : 0 + S_IRUSR + S_IROTH
               : 0 + O_RDONLY)
       and  chk (1
               : 0 + O_CREAT + O_TRUNC + O_WRONLY
               : 0 + S_IWUSR + S_IWOTH
               : 0 + O_RDWR)
       and  chk (2
               : 0 + O_CREAT + O_TRUNC + O_WRONLY
               : 0 + S_IWUSR + S_IWOTH
               : 0 + O_RDWR);

       // If the descriptors were not all correct,
       // signal an exception to our caller
       if not ok;
          // !!! Additional coding required:
          // !!!   At this point, the code should signal an error condition,
          // !!!   or a parameter could be added to return the fact that
          // !!!   this program did not open the descriptors correctly
          // !!! For now, it will use DSPLY and then call the *PSSR ending with
          // !!! *CANCL to signal an exception
          dsply ('Descriptors 0, 1 and 2 not opened successfully.');
          exsr *pssr;
       endif;
       *inlr = '1';

       begsr *pssr;
          // endsr *cancl will signal an exception to the caller
       endsr '*CANCL';
      /end-free

     P chk             b
     D chk             pi              n
     D   descriptor                  10i 0 value
     D   mode                        10i 0 value
     D   aut                         10i 0 value
     D   other_valid_mode...
     D                               10i 0 value

     D open            pr            10i 0 extproc('open')
     D   filename                      *   value options(*string)
     D   mode                        10i 0 value
     D   aut                         10i 0 value
     D   unused                      10i 0 value options(*nopass)

     D closeFile       pr            10i 0 extproc('close')
     D   handle                      10i 0 value

     D fcntl           pr            10I 0 extproc('fcntl')
     D   descriptor                  10I 0 value
     D   action                      10I 0 value
     D   arg                         10I 0 value options(*nopass)

     D F_GETFL         c                   x'06'

     D flags           s             10i 0
     D new_desc        s             10i 0

     D actual_acc      s             10i 0
     D required_acc    s             10i 0
     D allowed_acc     s             10i 0
      /free
          flags = fcntl (descriptor : F_GETFL);
          if flags < 0;
             // no flags returned, attempt to open this descriptor
             new_desc = open ('/dev/null' : mode : aut);
             if new_desc <> descriptor;
                // we didn't get the right descriptor number, so
                // close the one we got and return '0'
                if new_desc >= 0;
                   closeFile (new_desc);
                endif;
                return '0';
             endif;
          else;
             // check if the file was opened with the correct
             // access mode
             actual_acc = %bitand (flags : O_ACCMODE);
             required_acc = %bitand (mode : O_ACCMODE);
             allowed_acc = %bitand (other_valid_mode : O_ACCMODE);
             if  actual_acc <> required_acc
             and actual_acc <> allowed_acc;
                return '0';
             endif;
          endif;

          return '1';
      /end-free
     P chk             e
                                                         



Here is the RPG ILE code to call the API.

Note: You must first compile it as a module using the 15 option in PDM or CRTRPGMOD command. Then you need to issue a CRTPGM command similar to the following:

CRTPGM PGM(libraryname/TEST) MODULE(libraryname/TEST) BNDSRVPGM((QSHELL/QZSHAPI))

You must change the command field. For example, instead of /home/test.txt, you need to put a directory that exists on your system. After running the program, you will have a spooled file named QPRINT which has the same data in it as the /home/test.txt has in it.

Example

H DEBUG(*YES)                                                          
DQzshSystem       PR            10I 0 ExtProc('QzshSystem')            
D                                 *   value Options(*String)          
DCommand          s             44A                                    
D rc_qzsh         s             10I 0                                  
 /free                                                                
   Command = 'pr -t ' + '/home/test.txt' + ' | ' +                
   'Rfile -wQ qprint';                                                                                                              
   rc_qzsh = QzshSystem(Command);                                                                                              
 /end-free                                                            
C                   seton                                        lr    

[{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Platform":[{"code":"PF012","label":"IBM i"}],"Version":"6.1.0"}]

Historical Number

557008957

Document Information

Modified date:
18 December 2019

UID

nas8N1012360