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

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