     **
     **  Program . . : SEC107
     **  Description : Extract and print SSL trace data
     **  Author  . . : Carsten Flensburg
     **
     **
     **
     **  Compile instructions:
     **    CrtRpgMod  Module( SEC107 )
     **               DbgView( *LIST )
     **
     **    CrtPgm     Pgm( SEC107 )
     **               Module( SEC107 )
     **               ActGrp( *NEW )
     **
     **
     **  Prerequisite: The following PTF must be applied for the appropriate release:
     **
     **                Release  PTF:
     **                610-     MF59767
     **                611-     MF59784
     **                710-     MF59800
     **                720-     MF59777
     **
     **
     **  Programmer's notes:
     **
     **  Instructions: TRCINT - Trace Internal command
     **
     **    IBM on-line technote documentation:
     **      http://www-01.ibm.com/support/docview.wss?uid=nas8N1020594
     **
     **
     **      Protocol        Trace
     **      version:        Identifier:
     **      TLSV1.2         17004
     **      TLSV1.1         17003
     **      TLSV1.0         17002
     **      SSLV3           17001
     **      SSLV2           17000
     **
     **    To trace all SSL protocol versions issue the following command to start the trace:
     **
     **      TRCINT SET(*ON)
     **             TRCTBL('SSL-1700x')
     **             SIZE(512 *MB)
     **             TRCTYPE(*SCKSSL)
     **             SLTTRCPNT((17000 17009))
     **
     **    Wait the desired period of time for new SSL connections to establish and end the trace
     **    with the command to output tracedata to a spooled file:
     **
     **      TRCINT SET(*OFF)
     **             TRCTBL('SSL-1700x')
     **             OUTPUT(*PRINT)
     **
     **    Or - to output tracedata to physical file:
     **
     **      TRCINT SET(*OFF)
     **             TRCTBL('SSL-1700x')
     **             OUTPUT(*OUTFILE)
     **             OUTFILE(QGPL/SSL1700X)
     **             OUTMBR(*FIRST *REPLACE)
     **
     **    To delete the trace issue the following command:
     **
     **      TRCINT SET(*END)
     **             TRCTBL('SSL-1700x')
     **
     **    SST Work with-panel:
     **
     **                               Work with Internal Traces
     **
     **     Type options, press Enter.
     **       2=Allocate    3=Work with enable details    4=Delete
     **       5=Display     6=Dump    7=Activate    8=Deactivate    9=Clear
     **
     **     Opt  Trace Name                      Updated   -------- Status --------
     **          SSL-1700X                       26-03-15  Active          Enabled
     **
     **
     **-- Header specifications:
     H BndDir( 'QC2LE' )  Option( *SrcStmt: *NoDebugIo )  DatEdit( *DMY/ )

     **-- Printer file:
     FQSYSPRT   O    F  198        Printer  InfDs( PrtInf )  OflInd( *InOf )
     F                                      UsrOpn

     **-- Printer file information:
     D PrtInf          Ds                   Qualified
     D  OvfLin                        5i 0  Overlay( PrtInf: 188 )
     D  CurLin                        5i 0  Overlay( PrtInf: 367 )
     D  CurPag                        5i 0  Overlay( PrtInf: 369 )
     **-- System information:
     D PgmSts         SDs                   Qualified
     D  PgmNam           *Proc

     **-- Global variables:
     D pRFILE          s               *
     D rc              s             10i 0
     D Idx             s             10i 0
     D StrBuf          s          10240a
     D RtnBuf          s          10240a
     D FldVal          s           1024a   Varying
     D RtnVal          s           1024a   Varying
     D CmdStr          s           4096a   Varying
     **
     D Time            s              6s 0
     D NbrRcds         s             10u 0
     **
     D HI_Time         s               t   Inz( *HiVal )
     D HI_Date         s               d   DatFmt( *DMY )  Inz( *HiVal )
     D LO_Date         s               d   DatFmt( *DMY )  Inz( *LoVal )
     D PerBegDts       s               z
     D PerEndDts       s               z
     **
     D Itm             s              5i 0
     **
     D TrcRcd          Ds                  Qualified
     D  TrcTyp                       20a
     D  TrcId                         8a
     D  TrcDat                        8a
     D  TrcTimMs                     12a
     D   TrcTim                       6a   Overlay( TrcTimMs: 1 )
     D   TrcMs                        6s 0 Overlay( TrcTimMs: 7 )
     D  TrcTde                       12a
     D  SslPtc                        7a
     D  SslAlg                       36a
     D  LclPort                       5a
     D  LclIp                        24a
     D  RmtPort                       5a
     D  RmtIp                        24a
     D  JobNam_q                     26a
     D  JobNam                       10a   Overlay( JobNam_q:  1 )
     D  UsrPrf                       10a   Overlay( JobNam_q: 11 )
     D  JobNbr                        6a   Overlay( JobNam_q: 21 )
     D  AppNam                       28a
     D  TrcDts                         z
     D  TrcDta                       50a   Dim( 32 )
     **-- Global constants:
     D USR_SPC_Q       c                   'DBFLST    QTEMP'
     D NO_LOCK         c                   x'00000001'
     D NULL            c                   ''

     **-- Api error data structure:
     D ERRC0100        Ds                  Qualified
     D  BytPro                       10i 0 Inz( %Size( ERRC0100 ))
     D  BytAvl                       10i 0 Inz
     D  MsgId                         7a
     D                                1a
     D  MsgDta                      128a
     **-- I/O feedback structure:
     D RIOFB           Ds                  Qualified  Based( pRIOFB )
     D  pKey                           *
     D  pSysParm                       *
     D  RcdRrn                       10u 0
     D  NbrBytRw                     10i 0
     D  BlkCnt                        5i 0
     D  BlkFllBy                      1a
     D  BitFld                        1a
     D  Rsv                          20a
     **-- User space pointers:
     D pUsrSpc         s               *   Inz( *Null )
     D pHdrInf         s               *   Inz( *Null )
     D pLstEnt         s               *   Inz( *Null )
     **-- User space generic header:
     D UsrSpc          Ds                  Qualified  Based( pUsrSpc )
     D  OfsHdr                       10i 0 Overlay( UsrSpc: 117 )
     D  OfsLst                       10i 0 Overlay( UsrSpc: 125 )
     D  NumLstEnt                    10i 0 Overlay( UsrSpc: 133 )
     D  SizLstEnt                    10i 0 Overlay( UsrSpc: 137 )
     **-- API header information:
     D HdrInf          Ds                  Qualified  Based( pHdrInf )
     D  FilNamU                      10a
     D  FilLibU                      10a
     D  FilTyp                       10a
     D  RcdFmtNamU                   10a
     D  RcdLen                       10i 0
     D  RcdFmtId                     13a
     D  RcdTxtDsc                    50a
     D                                1a
     D  RcdTxtCcsId                  10i 0
     D  VarLenFldIn                   1a
     D  GphFldInd                     1a
     D  DatTimFldIn                   1a
     D  NulCapFldIn                   1a
     **-- API format FLDL0100:
     D FLDL0100        Ds                  Qualified  Based( pLstEnt )
     D  FldNm                        10a
     D  DtaTyp                        1a
     D  DtaUse                        1a
     D  OutBufPos                    10i 0
     D  InpBufPos                    10i 0
     D  Len                          10i 0
     D  Digits                       10i 0
     D  DecPos                       10i 0
     D  TxtDsc                       50a
     D  EdtCod                        2a
     D  EdtWrdLen                    10i 0
     D  EdtWrd                       64a
     D  ColHdg1                      20a
     D  ColHdg2                      20a
     D  ColHdg3                      20a
     D  IntFldNam                    10a
     D  AltFldNam                    30a
     D  AltFldNamLn                  10i 0
     D  NbrChrDbcs                   10i 0
     D  AlwNull                       1a
     D  HstVarInd                     1a
     D  DatTimFmt                     4a
     D  DatTimSep                     1a
     D  VarFldLenIn                   1a
     D  TxtDscCcsId                  10i 0
     D  DtaCcsId                     10i 0
     D  ColHdgCcsId                  10i 0
     D  EdtWrdCcsId                  10i 0
     D  Ucs2DspFldL                  10i 0

     **-- List fields:
     D LstFld          Pr              *
     D  PxUsrSpc                     20a   Const
     D  PxFilNam                     10a   Const
     D  PxLibNam                     10a   Const
     **-- Convert hex nibbles to character:
     D CvtHexChr       Pr          1024a   Varying
     D  PxHexStr                   2048a   Varying  Const
     **-- Get next value:
     D GetNxtVal       Pr              n
     D  PxFldVal                   1024a   Varying  Const
     D  PxRtnVal                   1024a   Varying
     **-- Send completion message:
     D SndCompMsg      Pr            10i 0
     D  PxMsgId                       7a   Const
     D  PxMsgDta                   3000a   Const  Varying  Options( *Omit )

     **-- Open file:
     D Ropen           Pr              *   ExtProc( '_Ropen' )
     D  pRFile                         *   Value  Options( *String )
     D  pMode                          *   Value  Options( *String )
     **-- Close file:
     D Rclose          Pr            10i 0 ExtProc( '_Rclose' )
     D  pRFile                         *   Value
     **-- Read first record:
     D Rreadf          Pr              *   ExtProc( '_Rreadf' )
     D  pRFile                         *   Value
     D  pBuffer                        *   Value
     D  BufLength                    10u 0 Value
     D  Options                      10i 0 Value
     **-- Read next record:
     D Rreadn          Pr              *   ExtProc( '_Rreadn' )
     D  pRFile                         *   Value
     D  pBuffer                        *   Value
     D  BufLength                    10u 0 Value
     D  Options                      10i 0 Value
     **-- Convert character to hex:
     D cvtch           Pr                  ExtProc( 'cvtch' )
     D  RcvChr                         *   Value
     D  SrcHex                         *   Value
     D  SrcLen                       10i 0 Value

     **-- List fields to user space:
     D LstFldSpc       Pr                  ExtPgm( 'QUSLFLD' )
     D  SpcNam_q                     20a   Const
     D  FmtNam                        8a   Const
     D  FilNam_q                     20a   Const
     D  RcdFmtNam                    10a   Const
     D  OvrPrc                        1a   Const
     D  Error                     32767a         Options( *NoPass: *VarSize )
     **-- Create user space:
     D CrtUsrSpc       Pr                  ExtPgm( 'QUSCRTUS' )
     D  SpcNam_q                     20a   Const
     D  ExtAtr                       10a   Const
     D  InzSiz                       10i 0 Const
     D  InzVal                        1a   Const
     D  PubAut                       10a   Const
     D  Text                         50a   Const
     **-- Optional 1:
     D  Replace                      10a   Const  Options( *NoPass )
     D  Error                     32767a          Options( *NoPass: *VarSize )
     **-- Optional 2:
     D  Domain                       10a   Const  Options( *NoPass )
     **-- Retrieve pointer to user space:
     D RtvPtrSpc       Pr                  ExtPgm( 'QUSPTRUS' )
     D  SpcNam_q                     20a   Const
     D  Pointer                        *
     D  Error                     32767a         Options( *NoPass: *VarSize )
     **-- Delete user space:
     D DltUsrSpc       Pr                  ExtPgm( 'QUSDLTUS' )
     D  SpcNam_q                     20a   Const
     D  Error                     32767a          Options( *VarSize )
     **-- Execute command:
     D ExcCmd          Pr                  ExtPgm( 'QCMDEXC' )
     D  CmdStr                     4096a   Const  Options( *VarSize )
     D  CmdLen                       15p 5 Const
     D  CmdIGC                        3a   Const  Options( *NoPass )
     **-- Send program message:
     D SndPgmMsg       Pr                  ExtPgm( 'QMHSNDPM' )
     D  MsgId                         7a   Const
     D  MsgFil_q                     20a   Const
     D  MsgDta                      512a   Const  Options( *VarSize )
     D  MsgDtaLen                    10i 0 Const
     D  MsgTyp                       10a   Const
     D  CalStkEnt                    10a   Const  Options( *VarSize )
     D  CalStkCtr                    10i 0 Const
     D  MsgKey                        4a
     D  Error                       512a          Options( *VarSize )
     **
     D  CalStkEntLen                 10i 0 Const  Options( *NoPass )
     D  CalStkEntQlf                 20a   Const  Options( *NoPass )
     D  DspWait                      10i 0 Const  Options( *NoPass )
     **
     D  CalStkEntTyp                 20a   Const  Options( *NoPass )
     D  CcsId                        10i 0 Const  Options( *NoPass )

     **-- Entry parameters:
     D ObjNam_q        Ds                  Based( pNull )  Qualified
     D  ObjNam                       10a
     D  LibNam                       10a
     **
     D Period          Ds                  Based( pNull )  Qualified
     D  NbrElm                        5i 0
     D  OfsElm1                       5i 0
     D  OfsElm2                       5i 0
     D  NbrEnd                        5i 0
     D  TimEnd                        6a
     D  DatEnd                        7a
     D  NbrBeg                        5i 0
     D  TimBeg                        6a
     D  DatBeg                        7a
     **
     D ProVer          Ds                  Based( pNull )  Qualified
     D  NbrEnt                        5i 0
     D  VerId                         8a   Dim( 5 )
     **
     D IncArg          Ds                  Qualified
     D  NbrElm                        5i 0
     D  IncCph                       32a   Varying
     D  IncIp                        32a   Varying
     **
     D SEC107          Pr
     D  PxFilNam_q                         LikeDs( ObjNam_q )
     D  PxPeriod                           LikeDs( Period )
     D  PxProVer                           LikeDs( ProVer )
     D  PxIncArg                           LikeDs( IncArg )
     D  PxOutQue_q                         LikeDs( ObjNam_q )
     **
     D SEC107          Pi
     D  PxFilNam_q                         LikeDs( ObjNam_q )
     D  PxPeriod                           LikeDs( Period )
     D  PxProVer                           LikeDs( ProVer )
     D  PxIncArg                           LikeDs( IncArg )
     D  PxOutQue_q                         LikeDs( ObjNam_q )

      /Free

        ExSr  InzPgm;

        pUsrSpc  = LstFld( USR_SPC_Q: PxFilNam_q.ObjNam: PxFilNam_q.LibNam );

        If  pUsrSpc <> *Null;

          pRFILE = Ropen( %Trim( PxFilNam_q.LibNam ) +
                          '/'               +
                          %Trim( PxFilNam_q.ObjNam )
                        : 'rr, arrseq=Y, '  +
                          'blkrcd=Y'
                        );

          If  pRFILE <> *Null;

            ExSr  GetRcd;
          EndIf;
        EndIf;

        ExSr  TrmPgm;

      //-- Get record:
        BegSr  GetRcd;

          pRIOFB  = Rreadf( pRFILE
                          : %Addr( StrBuf )
                          : %Size( StrBuf )
                          : NO_LOCK
                          );

          DoW  RIOFB.NbrBytRw > *Zero;

            ExSr  PrcRcd;

            pRIOFB  = Rreadn( pRFILE
                            : %Addr( StrBuf )
                            : %Size( StrBuf )
                            : NO_LOCK
                            );

          EndDo;

        EndSr;

      //-- Process record:
        BegSr  PrcRcd;

          RtnBuf = %SubSt( StrBuf: 1: RIOFB.NbrBytRw );

          pHdrInf = pUsrSpc + UsrSpc.OfsHdr;
          pLstEnt = pUsrSpc + UsrSpc.OfsLst;

          For  Idx = 1  To  UsrSpc.NumLstEnt;

            If  FLDL0100.DtaTyp = 'A';

              FldVal = %SubSt( RtnBuf: FLDL0100.InpBufPos: FLDL0100.Len );

              If  FLDL0100.FldNm = 'SCDATA';
                ExSr  PrcSslDta;
              Else;
                ExSr  PrcRcdDta;
              EndIf;
            EndIf;

            If  Idx < UsrSpc.NumLstEnt;
              pLstEnt = pLstEnt + UsrSpc.SizLstEnt;
            EndIf;
          EndFor;

          If  TrcRcd.TrcTyp = 'SOCKETS';
            ExSr ChkTrcRcd;
          EndIf;

        EndSr;

      //-- Process SSL data:
        BegSr  PrcSslDta;

          TrcRcd.TrcDta = *Blanks;
          Itm = *Zero;

          DoW  GetNxtVal( FldVal: RtnVal ) = *On;

            Itm += 1;
            TrcRcd.TrcDta(Itm) = CvtHexChr( RtnVal );
          EndDo;

          TrcRcd.SslPtc   = TrcRcd.TrcDta(2);
          TrcRcd.SslAlg   = TrcRcd.TrcDta(3);
          TrcRcd.LclPort  = TrcRcd.TrcDta(5);
          TrcRcd.LclIp    = TrcRcd.TrcDta(7);
          TrcRcd.RmtPort  = TrcRcd.TrcDta(9);
          TrcRcd.RmtIp    = TrcRcd.TrcDta(11);
          TrcRcd.JobNam_q = TrcRcd.TrcDta(12);
          TrcRcd.AppNam   = TrcRcd.TrcDta(13);

       EndSr;

      //-- Process record data:
        BegSr  PrcRcdDta;

          Select;
          When  FLDL0100.FldNm = 'SCTRCT';
            TrcRcd.TrcTyp = FldVal;

          When  FLDL0100.FldNm = 'SCIDEN';
            TrcRcd.TrcId = FldVal;

          When  FLDL0100.FldNm = 'SCDATE';
            TrcRcd.TrcDat = FldVal;

          When  FLDL0100.FldNm = 'SCTIME';
            TrcRcd.TrcTimMs = FldVal;

          When  FLDL0100.FldNm = 'SCTDEN';
            TrcRcd.TrcTde = FldVal;
          EndSl;

        EndSr;

      //-- Check trace record:
        BegSr  ChkTrcRcd;

          TrcRcd.TrcDts = %Date( TrcRcd.TrcDat: *ISO0 ) +
                          %Time( TrcRcd.TrcTim: *HMS0 ) +
                          %Mseconds( TrcRcd.TrcMs );

          If  TrcRcd.TrcDts >= PerBegDts  And  TrcRcd.TrcDts <= PerEndDts;

            If  PxProVer.NbrEnt = 1  And  PxProVer.VerId(1) = '*ALL'  Or
                %Lookup( TrcRcd.SslPtc
                       : PxProVer.VerId
                       : 1
                       : PxProVer.NbrEnt
                       ) > *Zero;

              If  PxIncArg.IncCph = '*ALL'  Or
                  %Scan( PxIncArg.IncCph: TrcRcd.SslAlg ) > *Zero;

                If  PxIncArg.IncIp = '*ALL'  Or
                  %Scan( PxIncArg.IncIp: TrcRcd.LclIp ) > *Zero  Or
                  %Scan( PxIncArg.IncIp: TrcRcd.RmtIp ) > *Zero;

                  ExSr WrtLstLin;
                EndIf;
              EndIf;
            EndIf;
          EndIf;

        EndSr;

      //-- Write list line:
        BegSr  WrtLstLin;

          If  PrtInf.CurLin > PrtInf.OvfLin - 1;
            Except  Header;
          EndIf;

          NbrRcds += 1;

          Except  Detail;

        EndSr;

      //-- Initialize program:
        BegSr  InzPgm;

          ExSr  InzParms;

          CmdStr = 'OVRPRTF FILE(QSYSPRT) +
                            PAGESIZE(68 198) +
                            LPI(8) +
                            CPI(15) +
                            OVRFLW(68) +
                            OUTQ(' + %Trim( PxOutQue_q.LibNam ) + '/'
                                   + %Trim( PxOutQue_q.ObjNam ) + ') ' +
                           'OVRSCOPE(*ACTGRPDFN)';

          ExcCmd( CmdStr: %Len( CmdStr ));

          Open  QSYSPRT;

          Time = Time;

          Except  Header;

          CrtUsrSpc( USR_SPC_Q
                   : *Blanks
                   : 65535
                   : x'00'
                   : '*CHANGE'
                   : *Blanks
                   : '*YES'
                   : ERRC0100
                   );

        EndSr;

      //-- Initialize parameters:
        BegSr  InzParms;

          If  PxOutQue_q.LibNam = *Blanks;
            PxOutQue_q.LibNam = '*N';
          EndIf;

          If  PxPeriod.NbrElm = 2;

            Select;
            When  PxPeriod.DatBeg = '0040000';
              PxPeriod.DatBeg = %Char( %Date(): *CYMD0 );

            When  PxPeriod.DatBeg = '0050000';
              PxPeriod.DatBeg = %Char( LO_Date: *CYMD0 );
            EndSl;

            Select;
            When  PxPeriod.DatEnd = '0040000';
              PxPeriod.DatEnd = %Char( %Date(): *CYMD0 );

            When  PxPeriod.DatEnd = '0060000';
              PxPeriod.DatEnd = %Char( HI_Date: *CYMD0 );
              PxPeriod.TimEnd = %Char( HI_Time: *HMS0 );
            EndSl;

            PerBegDts = %Date( PxPeriod.DatBeg: *CYMD0 ) +
                        %Time( PxPeriod.TimBeg: *HMS0 );

            PerEndDts = %Date( PxPeriod.DatEnd: *CYMD0 ) +
                        %Time( PxPeriod.TimEnd: *HMS0 );
          EndIf;

        EndSr;

      //-- Terminate program:
        BegSr  TrmPgm;

          If  NbrRcds = *Zero;
            Except  NoRcds;
          Else;
            Except  EndRpt;
          EndIf;

          If  pRFILE <> *Null;
            rc = Rclose( pRFILE );
          EndIf;

          If  pUsrSpc <> *Null;
            DltUsrSpc( USR_SPC_Q: ERRC0100 );
          EndIf;

          Close  QSYSPRT;

          CmdStr = 'DLTOVR FILE(QSYSPRT) +
                           LVL(*ACTGRPDFN)';

          ExcCmd( CmdStr: %Len( CmdStr ));

          SndCompMsg( 'CPF9897': 'SSL trace data report printed successfully.');

          *InLr = *On;
          Return;

        EndSr;

      /End-Free

     **-- Print file definition:
     OQSYSPRT   EF           Header         2  3
     O                       UDATE         Y      8
     O                       Time                18 '  :  :  '
     O                                          100 'SSL trace data report'
     O                                          150 'Program:'
     O                       PgmSts.PgmNam      161
     O                                          190 'Page:'
     O                       PAGE             +   1
     OQSYSPRT   EF           Header         1
     O                                           15 'Trace timestamp'
     O                                           35 'Version'
     O                                           49 'Cipher suite'
     O                                           91 'Local IP address'
     O                                          104 'Port'
     O                                          124 'Remote IP address'
     O                                          136 'Port'
     O                                          147 'Job name'
     O                                          154 'User'
     O                                          167 'Number'
     O                                          180 'Application'
     OQSYSPRT   EF           Detail         1
     O                       TrcRcd.TrcDts       26
     O                       TrcRcd.SslPtc       35
     O                       TrcRcd.SslAlg       73
     O                       TrcRcd.LclIp        99
     O                       TrcRcd.LclPort     105
     O                       TrcRcd.RmtIp       131
     O                       TrcRcd.RmtPort     137
     O                       TrcRcd.JobNam      149
     O                       TrcRcd.UsrPrf      160
     O                       TrcRcd.JobNbr      167
     O                       TrcRcd.AppNam      197
     **
     OQSYSPRT   EF           NoRcds      1
     O                                           32 '(No tracerecords found)'
     OQSYSPRT   EF           EndRpt      1
     O                                           64 '*****  End of SSL trace re-
     O                                              port *****'
     **-- List fields:
     P LstFld          B
     D                 Pi              *
     D  PxUsrSpc                     20a   Const
     D  PxFilNam                     10a   Const
     D  PxLibNam                     10a   Const
     **
     D pUsrSpc         s               *

      /Free

        LstFldSpc( PxUsrSpc
                 : 'FLDL0100'
                 : PxFilNam  + PxLibNam
                 : '*FIRST'
                 : '0'
                 : ERRC0100
                 );

        If  ERRC0100.BytAvl = *Zero;
          RtvPtrSpc( PxUsrSpc: pUsrSpc );

          Return  pUsrSpc;
        Else;

          Return  *Null;
        EndIf;

      /End-Free

     P LstFld          E
     **-- Convert hex nibbles to character:
     P CvtHexChr       B                   Export
     D                 Pi          1024a   Varying
     D  PxHexStr                   2048a   Varying  Const

     **-- Local declarations
     D ChrStr          s           1024a
     D HexStr          s           2048a

      /Free

        HexStr = PxHexStr;

        cvtch( %Addr( ChrStr ): %Addr( HexStr ): %Len( PxHexStr ));

        Return  %Subst( ChrStr: 1: %Int( %Len( PxHexStr ) / 2 ));

      /End-Free

     P CvtHexChr       E
     **-- Get next value:
     P GetNxtVal       B                   Export
     D                 Pi              n
     D  PxFldVal                   1024a   Varying  Const
     D  PxRtnVal                   1024a   Varying

     **-- Local declarations
     D ScnIdx          Ds                  Qualified
     D                                1a   Inz( x'7B' )
     D  Fld                           5s 0
     **
     D FldVal          Ds                  Qualified
     D                                1a
     D  ParLft                        1a
     D  FldLen                        5s 0
     D  ParRgt                        1a
     D                                1a
     D  FldDta                     1024a
     **
     D Ofs             s             10i 0
     D Len             s             10i 0
     D Fld             s             10i 0 Static
     D RtnVal          s           1024a   Varying

      /Free

        Fld += 1;

        ScnIdx.Fld = Fld;

        Ofs  =  %Scan( ScnIdx: PxFldVal );

        If  Ofs = *Zero;
          Fld = *Zero;

          Return  *Off;
        Else;
          FldVal = %Subst( PxFldVal: Ofs + 6 );
          PxRtnVal = %Subst( FldVal.FldDta: 1: FldVal.FldLen * 2 );

          Return  *On;
        EndIf;

      /End-Free

     P GetNxtVal       E
     **-- Send completion message:
     P SndCompMsg      B                   Export
     D                 Pi            10i 0
     D  PxMsgId                       7a   Const
     D  PxMsgDta                   3000a   Const  Varying  Options( *Omit )
     **
     D MsgKey          s              4a
     D MsgDta          s           3000a   Varying

      /Free

        If  %Addr( PxMsgDta ) = *Null;
          MsgDta = NULL;
        Else;
          MsgDta = PxMsgDta;
        EndIf;

        SndPgmMsg( PxMsgId
                 : 'QCPFMSG   *LIBL'
                 : MsgDta
                 : %Len( MsgDta )
                 : '*COMP'
                 : '*PGMBDY'
                 : 1
                 : MsgKey
                 : ERRC0100
                 );

        If  ERRC0100.BytAvl > *Zero;
          Return  -1;

        Else;
          Return   0;
        EndIf;

      /End-Free

     P SndCompMsg      E
