星期三, 11月 08, 2023

2008-02-15 如何取得現有執行環境的環境變數(ENVVER)? (Command: RTVENVVAR with API Qp0zGetEnv, Qp0zGetSysEnv)


如何取得現有執行環境的環境變數(ENVVER)? (Command: RTVENVVAR with API Qp0zGetEnv, Qp0zGetSysEnv)

如何取得現有執行環境的環境變數(ENVVER)? (Command: RTVENVVAR with API Qp0zGetEnv, Qp0zGetSysEnv)

在現有商業運作模式下有許多應用程式可以搬到 AS/400 上執行,例如 Java, PHP, MySQL, OPENSSH... 等均需要有 Qshell 的環境,
而 Qshell 的執行環境介面與 Unix 是類似的介面,所以 AS/400 也才有環境變數的需求,系統提供 ADDENVVAR, RMVENVVAR, WRKENVVAR,
三個指令,但缺少擷取環境變數的指令,所以底下是透過 API Qp0zGetEnv, Qp0zGetSysEnv 擷取環境變數的設定值。


File   : QRPGLESRC
Member : RTVENVVAR
Type   : RPGLE
Usage  : CRTBNDRPG PGM(RTVENVVAR) TGTRLS(V5R1M0)
OS     : V5R1 以後

     H Debug
     H OPTION(*NODEBUGIO : *SRCSTMT) DFTACTGRP(*NO) BNDDIR('QC2LE')
      **************************************************************************
      **  This program returns the environment variable's value.
      **  It is stuffed into the CL variable passed on the 2nd parm.
      **   CPP for RTVENVVAR CL command.
      **************************************************************************

      ** Template definition used for 2nd parameter
     D CL_RTNVAR_T     DS                  based(pNothing_T) QUALIFIED
     D  nLen                          5I 0
     D  Data                      32766A

     D RtvEnvVar       PR
     D  envvar                      256A
     D  rtnVar                             LIKEDS(CL_RTNVAR_T)
     D  envlvl                        4A

     D RtvEnvVar       PI
     D  envvar                      256A
     D  rtnVar                             LIKEDS(CL_RTNVAR_T)
     D  envlvl                        4A

      * Get job level env
     D Qp0zGetEnv      PR              *   ExtProc('Qp0zGetEnv')
     D  envvar                         *   VALUE OPTIONS(*STRING)
     D  nCCSID                       10I 0

      * Get sys level env
     D Qp0zGetSysEnv   PR            10I 0 ExtProc('Qp0zGetSysEnv')
     D  envVarName                     *   VALUE OPTIONS(*STRING)
     D  rtnBuffer                 65535A   OPTIONS(*VARSIZE)
     D  bufLen                       10I 0
     D  nCCSID                       10I 0
     D  reserved                       *   OPTIONS(*OMIT)

     D  rtnBuffer      S            512A
     D  pRtnBuffer     S               *   Inz
     D  pEnv           S               *   Inz
     D  bufLen         S             10I 0
     D  nCCSID         S             10I 0 Inz(0)
     D  nRtn           S             10I 0 Inz(0)

     C                   eval      *INLR = *ON
     C                   if        envlvl = '*JOB'
      ** Retrieve a pointer to the environment variable's value
     C                   eval      pEnv = Qp0zGetEnv(%TRIMR(ENVVAR):nCCSID)

      **  If nothing came back, then the ENVVAR is bad, so return nothing.
     C                   if        pEnv = *NULL
     C                   return
     C                   endif

      **  Copy the environment variable to the return variable,
      **  being careful not to overstep the variable's length.
     C                   eval      %subst(rtnVar.Data:1:rtnVar.nLen) =
     C                                %str(pEnv)
     C                   else
     C                   eval      bufLen = %len(rtnBuffer)
     C                   eval      nRtn = Qp0zGetSysEnv(%TRIMR(ENVVAR):
     C                                                  rtnBuffer  :
     C                                                  bufLen     :
     C                                                  nCCSID :
     C                                                  *OMIT)
     C                   if        nRtn <> 0
     C                   dump
     C                   return
     C                   endif

     C                   eval      rtnVar.nLen = bufLen
     C                   eval      pRtnBuffer = %addr(rtnBuffer)
     C                   eval      %subst(rtnVar.Data:1:bufLen) =
     C                                %str(pRtnBuffer)
     C                   endif

     C                   return



File   : QCMDSRC
Member : RTVENVVAR
Type   : CMD
Usage  : CRTCMD CMD(your-lib/RTVENVVAR) PGM(your-lib/RTVENVVAR) ALLOW(*IPGM *BPGM) 

 RTVENVVAR:  CMD        PROMPT('Retrieve Environment Variable')

             /* Command processing program is RTVENVVAR   */

             PARM       KWD(ENVVAR) TYPE(*CHAR) LEN(256) MIN(1) +
                          EXPR(*YES) INLPMTLEN(17) +
                          PROMPT('Environment variable')
             PARM       KWD(RTNVAL) TYPE(*CHAR) LEN(1) RTNVAL(*YES) +
                          VARY(*YES) CHOICE('Environment var return +
                          value') PROMPT('CL Var. for return value')
             PARM       KWD(LEVEL) TYPE(*CHAR) LEN(4) RSTD(*YES) +
                          DFT(*JOB) SPCVAL((*JOB) (*SYS)) +
                          EXPR(*YES) PMTCTL(*PMTRQS) PROMPT('Level')


File   : QCLSRC
Member : RTVENVVARC
Type   : CLP
Usage  : 測試程式 
         CRTCLPGM RTVENVVARC
         CALL RTVENVVARC 

/* Before you run the program run following command to set sample   */
/* environment variable for testing:                                */
/*  ADDENVVAR ENVVAR(JOBCLASSPATH) VALUE('.:/java')                 */
/*  ADDENVVAR ENVVAR(SYSCLASSPATH) VALUE('.:/system') LEVEL(*SYS)   */

/* After  you run the program run following command to reset ENVVAR */
/*  RMVENVVAR ENVVAR(JOBCLASSPATH)                                  */
/*  RMVENVVAR ENVVAR(SYSCLASSPATH) LEVEL(*SYS)                      */

/* You can browse spooled file QPPGMDMP under current job for job   */
/* and system level value same as we set above                      */

             PGM

             DCL &SYSPATH *CHAR 32
             DCL &JOBPATH *CHAR 32

             RTVENVVAR  ENVVAR(JOBCLASSPATH) RTNVAL(&JOBPATH)

             RTVENVVAR  ENVVAR(SYSCLASSPATH) RTNVAL(&SYSPATH) +
                          LEVEL(*SYS)
             DMPCLPGM

             ENDPGM


                        




沒有留言: