TechTip: Using Qshell from RPG to Compare Two Files

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

Most recently, I had a need to write some unit test scripts in RPG to compare a dynamically generated XML file to an existing XML document to determine whether or not they were exactly the same. I could have gone the route of coding everything in RPG using the IFS APIs, but I decided against that as the functionality already exists in Qshell's cmp (compare) command. By using the cmp command, I was able to interface with an API that has been tested by thousands of other developers, thus giving me a high level of confidence that the API will appropriately compare one file to another. Reusing what already exists is true SOA in action and is what we developers need to do to retain investment in our machines and take them to the next level in a fraction of the time.

All right! Enough intro. Let's see some code!

An obvious approach to using the Qshell APIs would be to first wrap them in an RPG program for ease of use. The example program below shows different parameters used to invoke the QSH_cmpSame RPG subprocedure.

 

     H bnddir('GENBND') dftactgrp(*no)

      /copy mcpress,QSHCp

     D result          s               n
      /free

       result =
         QSH_cmpSame('/home/aaron/compare1.txt': '/home/aaron/compare2.txt');

       result =
         QSH_cmpSame('/home/aaron/compare1.txt': '/home/aaron/compare3.txt');

       result =
         QSH_cmpSame(
           '/qsys.lib/aaronlib.lib/mcpress.file/compare1.mbr':
           '/qsys.lib/aaronlib.lib/mcpress.file/compare2.mbr');

       result =
         QSH_cmpSame(
           '/qsys.lib/aaronlib.lib/mcpress.file/compare1.mbr':
           '/qsys.lib/aaronlib.lib/mcpress.file/compare3.mbr');

       *inlr = *on;

      /end-free

 

 

The first line of interest is the /copy, where we bring in the prototype to make the call to QSH_cmpSame. The result variable is declared as a Boolean and will hold the result of each call to QSH_cmpSame.

Moving on to the mainline, we can see that both IFS files and source members can be compared. I didn't think this would work with the source members, but I couldn't pass up the opportunity to try! And work it did! See the code below for the contents of compare1.txt, compare2.txt, compare3.txt, compare1.mbr, compare2.mbr, and compare3.mbr. You will notice that they are incredibly small files and source members, and they are such because it serves the purpose of brevity and example. Note that compare1.txt and compare2.txt are exactly the same, so we could show an example of an exact match. On the flip side, compare1.txt and compare3.txt are slightly different and will produce the *OFF result. The .mbr files follow this same approach.

 

/home/aaron/compare1.txt:
<xml>sometext</xml>

/home/aaron/compare2.txt:
<xml>sometext</xml>

/home/aaron/compare3.txt:
<xml>sometext and some more</xml>


/qsys.lib/aaronlib.lib/mcpress.file/compare1.mbr:
     D var             s             10a 

/qsys.lib/aaronlib.lib/mcpress.file/compare2.mbr:
     D var             s             10a 

/qsys.lib/aaronlib.lib/mcpress.file/compare3.mbr:
     D var             s             20a

The following code shows the contents of QSH_cmpSame. The first thing that needs to happen is the redirecting of what's called standard input, standard output, and standard error. Subroutine SetupIO opens files with specific descriptors to do the Qshell redirection. (Go to the LiveFireLabs site for a good, concise tutorial of UNIX file descriptors.) You could think of the redirecting being similar to an OVRDBF used to alter the member used when accessing a PF for I/O.

 

      //----------------------------------------------------------------------
      // Author: Aaron Bartell
      // Copyright 2007 MowYourLawn.com All Rights Reserved.
      //
      // Compile:
      //  CRTRPGMOD
      //    MODULE(MYLIB/QSHFN)
      //    SRCFILE(MYLIB/MCPRESS)
      //    SRCMBR(QSHFN)
      //
      //  CRTSRVPGM
      //    SRVPGM(MYLIB/QSHSV)
      //    MODULE(MYLIB/QSHFN)
      //    SRCFILE(MYLIB/MCPRESS)
      //
      // After creating the service program you will need to add it to the
      // binding directory of your choice and then reference that binding
      // directory below (i.e. replacing GENBND).
      //----------------------------------------------------------------------
     H nomain bnddir('QC2LE': 'GENBND')

      /copy mcpress,QSHCp

     D QzshSystem      PR            10I 0 extproc('QzshSystem')
     D   command                       *   value options(*string)

     D close           PR            10I 0 extproc('close')
     D  handle                       10I 0 value

     D unlink          PR            10i 0 extproc('unlink')
     D   path                          *   Value options(*string)

     D open            PR            10I 0 extproc('open')
     D  filename                       *   value options(*string)
     D  openflags                    10I 0 value
     D  mode                         10U 0 value options(*nopass)
     D  codepage                     10U 0 value options(*nopass)

     D stat            pr            10i 0 extproc('stat')
     D  filename                       *   value options(*string)
     D  statStruct                     *   value

     D                SDS
     D  dsJobNo              264    269A

     D stsBuff         ds                  align inz qualified
     D  perms                        10u 0
     D  fileID                       10u 0
     D  linkCount                     5u 0
     D  userIDNbr                    10u 0
     D  groupIdNbr                   10u 0
     D  bytesInFile                  10i 0
     D  timeLastAcc                  10i 0
     D  timeLastChg                  10i 0
     D  timeStsLastChg...
     D                               10i 0
     D  fileSysID                    10u 0
     D  blockSize                    10u 0
     D  allocBytes                   10u 0
     D  objectType                   11
     D  codePage                      5u 0
     D                               62
     D  generationID                 10u 0


      //------------------------------------------------------------------------
      // QSH_cmpSame
      //------------------------------------------------------------------------
     P QSH_cmpSame     B                   export
     D QSH_cmpSame     PI             1n
     D   pFile1                    1024a   varying const
     D   pFile2                    1024a   varying const

     D O_RDONLY        C                   1
     D O_WRONLY        C                   2
     D O_CREAT         C                   8
     D O_TRUNC         C                   64

     D cmd             S           2053A   varying
     D msg             S             52A
     D x               S             10I 0
     D rc1             s             10i 0
     D rc2             s             10i 0
     D result          s               n
     D stsBuff1        ds                  likeds(stsBuff)
     D stsBuff2        ds                  likeds(stsBuff)
      /FREE

       result = *off;
       exsr setupRedirect;

       if msg <> *blanks;
         return *off;
       endif;

       cmd = 'CMP ' + %trim(pFile1) + ' ' + %trim(pFile2);
       rc1 = QzshSystem(cmd);

       // Success is anything greater than -1.
       if rc1 < 0;
         return *off;
       endif;


       rc1 = stat('/tmp/stdout-'+dsJobNo: %addr(stsBuff1));
       rc2 = stat('/tmp/stderr-'+dsJobNo: %addr(stsBuff2));

       exsr closeRedirect;

       if stsBuff1.bytesInFile > 0 or
          stsBuff2.bytesInFile > 0 or
          rc1 < 0 or
          rc2 < 0;
         result = *off;
       else;
         result = *on;
       endif;

       return result;

       //------------------------------------------------------------------------
       // File descriptors 0, 1 & 2 are used by unix-environments for
       // stdin, stdout & stderr. Redirect those 3 descriptors to stream files.
       //------------------------------------------------------------------------
       begsr setupRedirect;
         for x = 0 to 2;
           callp close(x);
         endfor;

         msg = *blanks;

         // open up 0, 1, 2 as files.
         if open('/dev/qsh-stdin-null': O_RDONLY) <> 0;
           msg = 'Unable to redirect STDIN';
         endif;

         if open('/tmp/stdout-'+dsJobNo: O_WRONLY+O_CREAT+O_TRUNC: 511) <> 1;
           msg = 'Unable to redirect STDOUT';
         endif;

         if open('/tmp/stderr-'+dsJobNo: O_WRONLY+O_CREAT+O_TRUNC: 511) <> 2;
           msg = 'Unable to redirect STDERR';
         endif;

         // Error occurred!!!
         if msg <> *blanks;
           dsply '' ' ' msg;
           exsr closeRedirect;
         endif;

       endsr;

       //------------------------------------------------------------------------
       //  Close the descriptors opened by setupIO
       //------------------------------------------------------------------------
       begsr closeRedirect;

         for x = 0 to 2;
           callp close(x);
         endfor;

         unlink('/tmp/stdout-'+dsJobNo);
         unlink('/tmp/stderr-'+dsJobNo);

       endsr;
      /END-FREE

     P                 e

The second piece of interesting code is the composing of the CMP command and the subsequent calling of QzshSystem to process it. Exhaustive information on the QzshSystem API can be found here. But for the sake of layman conversation, it's simply an enabler of sorts that allows the execution of commands you would traditionally enter into a Qshell prompt interactively. To run an interactive Qshell session, simply enter STRQSH on the command line. You can find detailed information about the CMP here, but essentially, it simply does a byte-for-byte binary comparison of two files; and as I found out, it can also work in the QSYS.LIB side of the IFS.

The way this program determines success is by checking to see if any errors appeared in the standard out files or error files that were redirected to it earlier. This is done using the stat IFS API. If either file has content, then we know something out of the ordinary occurred during the comparison and *OFF should be returned. Run this program through debug and you can see what's in those files using DSPF '/tmp/stderr-999999', where 999999 is the job number.

The last order of action is to close all of the Qshell file descriptor redirects by executing sub routine CloseIO.

Additional References

Article: "TechTip: Qshell vs. PASE"
Article: "TechTip: Link Up with Qshell"
Article: "Exploring iSeries QSHELL"
Presentation:
"Qshell and OpenSSH for IBM System i"
IBM Information Center Web Page:
Using Qshell

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$