Blame SOURCES/BZ-1618803-adapt-env-parser-to-new-module-output.patch

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