LOAD.ENV.COMMON

From Pickwiki
Jump to navigationJump to search
                          subroutine LOAD.ENV.COMMON( flag )
* Load the ENV.COMMON, if it is not already loaded
* ========================================================================================
* 27.Mar.2009 james: Actually call unix 'env' now in case we LOGTO (see below)
* 06.May.2007 jim: Cleanup; fix comments; etc. Interesting use of 'isnull' !
* 30-Dec-2006 manoj: Created
* ========================================================================================
*
*  flag :  EC.RELOAD or EC.CLEAR (or no flag to load only if necessary)
*
* Should this be called from STD.SETUP ? (see also 'GET.REALUSER')
*
* ========================================================================================

  $include RMS.BP RMS.COMMON   ;*  For accessing 'USER.ID', etc; if necessary
  $include RMS.BP ENV.COMMON

  * if isnull(EC$IS.LOADED) then EC$IS.LOADED = @False
  if unassigned(EC$IS.LOADED) then EC$IS.LOADED = @False
 
  begin case
    case flag = EC.CLEAR    ; gosub [[CleanEnvCommon]]
    case flag = EC.RELOAD   ; gosub [[LoadEnvCommon]]
    case not( EC$IS.LOADED) ; gosub [[LoadEnvCommon]]
  end case

return

[[LoadEnvCommon]]:
  EC$[[SUDO_USER]] = ""
  
  *  This does NOT actually do a unix 'env' !? Looks like it uses an old one; because
  *  when you LOGTO it does NOT pick up the new PWD ?:
  *execute "ENV" capturing envOutput
  *
  *  sooooo use THIS instead:
  
  execute \SH -c "env"\ capturing envOutput
  
  env[[LineCount]] = dcount (envOutput, @FM)
  for i = 1 to env[[LineCount]]
    envLine = envOutput<i>
    if (envLine <> "") then
      envId   = envLine["=",1,1]
      envData = ereplace (envLine, envId:"=", "")
      
      begin case
        case envId = "HOSTNAME"  ; EC$HOSTNAME  = trim(envData)
        case envId = "USER"      ; EC$USER      = trim(envData)
        case envId = "[[SUDO_USER]]" ; EC$[[SUDO_USER]] = trim(envData)
        case envId = "UVHOME"    ; EC$UVHOME    = trim(envData)
        case envId = "PWD"       ; EC$PWD       = trim(envData)
        case envId = "HOME"      ; EC$HOME      = trim(envData)
      end case
    end
  next i

  *  Don't be fooled if we are 'sudo'ed; get the real user:

  if EC$[[SUDO_USER]] # "" then
    EC$REALUSER = EC$[[SUDO_USER]]
  end else
    EC$REALUSER = EC$USER
  end

  EC$IS.LOADED = @True

return

[[CleanEnvCommon]]:
  EC$HOSTNAME   = ""
  EC$USER       = ""
  EC$[[SUDO_USER]]  = ""
  EC$UVHOME     = ""
  EC$PWD        = ""
  EC$HOME       = ""
  EC$REALUSER   = ""
  EC$IS.LOADED  = @False
return