Problem 2208

Summary: /control/getEnv will not import empty-string environment variables
Product: Geant4 Reporter: Michael Kelsey <kelsey>
Component: intercomsAssignee: asai
Status: RESOLVED FIXED    
Severity: minor    
Priority: P4    
Version: 10.5   
Hardware: All   
OS: All   

Description Michael Kelsey 2019-12-04 20:41:10 CET
Geant4 provides the ability to import shell environment variables into aliaes, and use them subsequently in macro commands, via the /control/getEnv`= command.

In both sh/Bash and csh, it is possible to define an envvar and assign it an empty-string value.  This "empty" envvar is visible to the G4 executable and the UI manager, for example:

  % setenv EMPTYVAR
  % echo $EMPTYVAR

  % myG4App
  [...]
  PreInit> /control/getEnv NOTANAME
  <NOTANAME> is not defined as a shell variable. Command ignored.
  PreInit> /control/getEnv G4INSTALL
  PreInit> /control/getEnv PWD
  PreInit> /control/getEnv EMPTYVAR
  PreInit> 

Notice that no error message is returned for EMPTYVAR.  However, when I look at what aliases I have defined, it's not there:

  PreInit> /control/listAlias
    G4INSTALL : /Applications/GEANT4/geant4.10.05-seq/share/Geant4-10.5.1/geant4make
    PWD : /Users/kelsey/cdms/SimProdMacros

If I turn on verbosity, and try again, I see

  PreInit> /control/verbose 1
  PreInit> /control/getEnv EMPTYVAR 
  /control/getEnv EMPTYVAR
  /control/alias EMPTYVAR 

Notice that the embedded /control/alias command doesn't appear to have a second argument, but no error message is printed.  Contrast this with doing the same thing by hand:

  PreInit> /control/alias FOO
  /control/alias FOO
  Parameter is wrong type and/or is not omittable (index 1)
  PreInit> /control/alias FOO ""
  /control/alias FOO ""
  PreInit> /control/listAlias
  /control/listAlias
    FOO : 
    G4INSTALL : /Applications/GEANT4/geant4.10.05-seq/share/Geant4-10.5.1/geant4make
    PWD : /Users/kelsey/cdms/SimProdMacros

In this case, where I used a quoted empty-string, the alias was defined properly, and shows up in the list as existing, but not having a value.

I believe there's a bug in the implementation of /control/getEnv, such that an empty value is not properly passed to define the alias.  To fix this, I believe the code for /control/getEnv (G4UIcontrolMessenger.cc, lines 419-423) should be modified slightly:

      G4String st = "/control/alias ";
      st += newValue;
      st += " \"";
      st += getenv(newValue);
      st += "\"";
      UI->ApplyCommand(st);

This will incorporate the required '""' literal into the command.  Alternatively, SetAlias() could be called directly instead of passing through ApplyCommand():

     G4String st = newValue+" ";
     st += getenv(newValue);
     UI->SetAlias(st.c_str());

The function itself will accept an alias name alone, with no second token(s) in the string, where the corresponding macro command does not.
Comment 1 asai 2020-02-19 19:39:57 CET
Thank you for reporting.
The fix will be included in the next release.
Makoto