ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

using SYSTEM rather than QCMDEXEC or QCAPCMD

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • using SYSTEM rather than QCMDEXEC or QCAPCMD

    This was kinda fun...

    PHP Code:
          /If Defined(*Crtbndrpg)                                                                       
         
    H Dftactgrp(*No)                                                                               
          /Endif                                                                                        
         
    H Bnddir('QC2LE')                                                                              
           
    // ------------------------------------------------------- Prototypes                        
                                                                                                        
         
    d GoCmd           pr            10I 0 Extproc('system')                                        
         
    d   CmdString                     *   Value                                                    
         d                                     Options
    (*String)                                         
                                                                                                        
         
    d $GetMessage     pr                  extpgm('QMHRTVM')                                        
         
    d  WRcvVar                   65535A   Options(*VarSize)                                        
         
    d  WRcvVarLen                   10I 0 Const                                                    
         
    d  WFormat                       8A   Const                                                    
         
    d  WMsgID                        7A   Const                                                    
         
    d  WQualMsgF                    20A   Const                                                    
         
    d  WReplData                 65535A   Const Options(*VarSize)                                  
         
    d  WReplDataLen                 10I 0 Const                                                    
         
    d  WDoReplace                   10A   Const                                                    
         
    d  WUseCtrlChars                10A   Const                                                    
         
    d  WErrorCode                65535A   Options(*VarSize)                                        
         
    d  WRetrOpt                     10A   Const Options(*NoPass)                                   
         
    d  WRetrOpt                     10A   Const Options(*NoPass)                                   
         
    d  WCCSID_out                   10I 0 Const Options(*NoPass)                                   
         
    d  WCCSID_in                    10I 0 Const Options(*NoPass)                                   
                                                                                                        
           
    // program variables                                                                         
         
    d Errmsgid        s              7    Import('_EXCP_MSGID')                                    
         
    d errorcode       s              7                                                             
         d LibraryNotFound
    ...                                                                           
         
    d                 c                   'CPF2110'                                                
         
    d NullString      c                   -1                                                       
         d ObjectNotFound  c                   
    'CPF3142'                                                
         
    d ObjectInUse     c                   'CPF3156'                                                
         
    d Returncode      s             10I 0                                                          
         d Success         c                    0                                                       
         d Userid          s             10a                                                            
         d WReplaceData    s            100A   Inz
    (*Blanks)                                             
         
    d WMessage        s            100a   Inz(*blanks)                                             
         
    d WMsgFile        s             10A                                                            
         d WMsgLibrary     s             10A                                                            
         d WMsgFileLib     s             20A                                                            
         d WMaxMsgLen      s             10i 0 inz
    (%size(WMessage))                                     
                                                                                                        
         
    d ErrorDS         ds                  qualified                                                
         d    bytesProv                  10i 0 inz
    (0)                                                   
         
    d    bytesAvail                 10i 0 inz(0)                                                   
                                                                                                        
         
    d RTVM0100        ds                  qualified                                                
         d    WMsgLen                    10i 0 overlay
    (RTVM0100:9)                                      
         
    d    WMsgData                32767a   overlay(RTVM0100:25)                                     
                                                                                                        
          /
    Free                                                                                         
                                                                                                        
           
    *inlr = *on;                                                                                 
                                                                                                        
           
    returncode Gocmd('DLTF MYLIB/MYFILE');                                                     
                                                                                                        
           
    select;                                                                                      
             
    when returncode Success;          // Command was successful                              
               // ...                                                                                   
             
    when returncode NullString;       // Command string was null                             
               // ...                                                                                   
             
    when errmsgid ObjectNotFound;     // CPF3142                                             
               // ...                                                                                   
             
    when errMsgid ObjectInUse;        // CPF3156                                             
               // ...                                                                                   
             
    when errMsgid LibraryNotFound;    // CPF2110                                             
               // ...                                                                                   
             
    other;                              // Some other error                                    
               // ...                                                                                   
           
    endsl;                                                                                       
                                                                                                        
           
    // added for fun --- return CPF message text                                                 
           
    if errMsgid <> *blanks;                                                                      
            
    exsr $getTheMessageDescription;                                                             
           endif;                                                                                       
                                                                                                        
           return;                                                                                      
                                                                                                        
            
    //--------------------------------------------------------                                  
            // $GetTheMessageDescription - return message description                                   
            //--------------------------------------------------------                                  
                 
    begsr $GetTheMessageDescription;                                                       
                                                                                                        
                   
    WMsgFile 'QCPFMSG';                                                                
                   
    WMsgLibrary 'QSYS';                                                                
                   
    WMsgFileLib WMsgFile WMsgLibrary;                                                
                   
    WReplaceData userid;                                                               
                                                                                                        
                   
    $GetMessage(RTVM0100                                                                 
                                 
    : %sizeRTVM0100 )                                   // return data   
                                 
    'RTVM0100'                                          // return data   
                                 
    errMsgid                                            // msg id        
                                 
    WMsgFileLib                                         // msg file,     
                                 
    WReplaceData                                        // msg parameter 
                                 
    : %size(WReplaceData)                                 // msg parameter 
                                 
    '*YES'                                              // substitute MSG
                                 
    '*NO'                                               //  control chars
                                 
    ErrorDS                                                              
                                 
    );                                                                     
                                                                                                        
                   if (
    RTVM0100.WMsgLen > *Zeros);                                                      
                    if (
    RTVM0100.WMsgLen WMaxMsgLen);                                                 
                     
    RTVM0100.WMsgLen WMaxMsgLen;                                                     
                    endif;                                                                              
                    
    // retrieve the message with message data inserted                                  
                    
    WMessage = %subst(RTVM0100.WMsgData RTVM0100.WMsgLen);                        
                   else;                                                                                
                    
    WMessage = *Blanks;                                                                 
                   endif;                                                                               
                                                                                                        
                 
    endsr;                                                                                 
                                                                                                        
          /
    end-free 
    Attached Files
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

  • #2
    Re: using SYSTEM rather than QCMDEXEC or QCAPCMD

    I like using system and the ability to get the message id. I have a SysCall procedure that you can pass optional parms of messages to ignore. So if you have a call to delete an object you can pass the message id of 'object not found' and the call will ignore that message if it gets it and still return successfully. Cleans up application code so it doesn't have to check for expected messages that are ok to receive.

    Comment


    • #3
      Re: using SYSTEM rather than QCMDEXEC or QCAPCMD

      Why not using QCMDEXC and retrieving the message id (Position 40-46) and/or the message text (Position 91-170) from the Program Status Structure?

      Code:
      D  PGMSDS       SDS                                             Qualified
      D    MsgId                                  40    46
      D    MsgTxt                                 91   170
       *----------------------------------------------------------------------
       /Free
            CallP(E) QCMDEXC(Yourcommand: CommandLength);
            If %Error and PGMSDS.MsgId = 'CPF9801';
                //Do something;
            ElseIf %Error;
                ErrText = PGMSDS.MsgTxt;
                Dsply ErrText;
            EndIf;
      Birgitta

      Comment


      • #4
        Re: using SYSTEM rather than QCMDEXEC or QCAPCMD

        I guess I always liked that with SYSTEM I didn't need to mess with the CommandLength. Kind of a personal preference either way I think.

        Comment


        • #5
          Re: using SYSTEM rather than QCMDEXEC or QCAPCMD

          I like system too and use it regularly. But a consideration is also whether or not you like seeing all the exception messages in the job log.

          Comment


          • #6
            Re: using SYSTEM rather than QCMDEXEC or QCAPCMD

            command length isn't a problem...put your command in a varying length field like so:
            Code:
            cmdstr = 'DSPJOBLOG';
            QCMDEXC( cmdstr : %len( cmdstr ) );
            and let the program figure it out for you.
            I'm not anti-social, I just don't like people -Tommy Holden

            Comment

            Working...
            X