f42530
commit 98fe4dcef136eaaa323f1729c11f38b2b19b3a42
f42530
Author: Michal Domonkos <mdomonko@redhat.com>
f42530
Date:   Mon Jan 13 17:04:41 2020 +0100
f42530
f42530
    Adapt env parser to newer module(1) versions. BZ 1618803
f42530
    
f42530
    With module(1) version 4.x and later (tcl-based), the command
f42530
    
f42530
      MODULE_CMD sh load <collection>
f42530
    
f42530
    prints a newline after each export line, breaking our parsing logic in
f42530
    get_env_vars() which just tokenizes the output by semicolons and does
f42530
    not anticipate newlines.  As a result, we would end up with
f42530
    "\nKEY=VALUE" pairs and pass them as such to putenv(3) which then has no
f42530
    effect.
f42530
    
f42530
    The fix is easy; just strip the leading newline from the KEY=VALUE pairs
f42530
    if present.  This ensures we stay compatible with the older module(1)
f42530
    versions as well.
f42530
    
f42530
    A simple reproducer follows:
f42530
    
f42530
      1) Make scl(1) run in "modulefile" mode by creating a modulefile in
f42530
         /etc/scl/modulefiles corresponding to a collection, for example:
f42530
    
f42530
         $ /usr/share/Modules/bin/createmodule.sh /opt/rh/eap7/enable \
f42530
           > /etc/scl/modulefiles/eap7
f42530
    
f42530
      2) Run a simple scl(1) command to detect the presence of an env var
f42530
         that we know should be set in the target collection, for example:
f42530
    
f42530
         $ scl enable eap7 'echo $LOADEDMODULES'
f42530
    
f42530
         Previously, there would be no output.  With this commit, the output
f42530
         should be "eap7".
f42530
f42530
diff --git a/src/scllib.c b/src/scllib.c
f42530
index 3c32d65..a182194 100644
f42530
--- a/src/scllib.c
f42530
+++ b/src/scllib.c
f42530
@@ -52,7 +52,7 @@ static scl_rc get_env_vars(const char *colname, char ***_vars)
f42530
     char *argv[] = {MODULE_CMD, MODULE_CMD, "sh", "add", "", NULL};
f42530
     char *output = NULL;
f42530
     int i = 0;
f42530
-    char **parts, **vars;
f42530
+    char **parts, *part, **vars;
f42530
     scl_rc ret = EOK;
f42530
 
f42530
     ret = initialize_env();
f42530
@@ -73,16 +73,21 @@ static scl_rc get_env_vars(const char *colname, char ***_vars)
f42530
      * Expected format of string stored in variable output is following:
f42530
      * var1=value1 ;export value1 ; var2=value2 ;export value2;
f42530
      * var3=value\ with\ spaces
f42530
+     * NOTE: Newer (tcl-based) versions of MODULE_CMD put a newline after each
f42530
+     * export command so we need to take that into account.
f42530
      */
f42530
 
f42530
     vars = parts = split(output, ';');
f42530
 
f42530
     /* Filter out strings without "=" i. e. strings with export. */
f42530
-    while (*parts !=  NULL) {
f42530
-        if (strchr(*parts, '=')) {
f42530
-            strip_trailing_chars(*parts, ' ');
f42530
-            unescape_string(*parts);
f42530
-            vars[i++] = xstrdup(*parts);
f42530
+    while (*parts != NULL) {
f42530
+        part = *parts;
f42530
+        if (part[0] == '\n')
f42530
+            part++;
f42530
+        if (strchr(part, '=')) {
f42530
+            strip_trailing_chars(part, ' ');
f42530
+            unescape_string(part);
f42530
+            vars[i++] = xstrdup(part);
f42530
         }
f42530
         parts++;
f42530
     }