Pass envlist to filter children too. (closes #3922)
authorKevin McCarthy <kevin@8t8.us>
Sat, 18 Mar 2017 20:48:02 +0000 (13:48 -0700)
committerRichard Russon <rich@flatcap.org>
Tue, 21 Mar 2017 16:11:38 +0000 (16:11 +0000)
The new setenv patch neglected to pass the envlist for filters too.

Unfortunately, the filter code was already set up to pass COLUMNS to
children, so it needed to be changed to add this to the envlist
instead.

Factor out mutt_envlist_set() from the parse_setenv() function, which
the filter code can then use to set COLUMNS after forking.

filter.c
init.c
protos.h
system.c

index 08e4ae1eefa97338c641d63a9f7183d4194c87cb..bb33b883ef206dcd7bc44cc4494e194baccceece 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -121,10 +121,10 @@ mutt_create_filter_fd (const char *cmd, FILE **in, FILE **out, FILE **err,
     if (MuttIndexWindow && (MuttIndexWindow->cols > 0))
     {
       snprintf (columns, sizeof (columns), "%d", MuttIndexWindow->cols);
-      setenv ("COLUMNS", columns, 1);
+      mutt_envlist_set ("COLUMNS", columns);
     }
 
-    execl (EXECSHELL, "sh", "-c", cmd, NULL);
+    execle (EXECSHELL, "sh", "-c", cmd, NULL, mutt_envlist ());
     _exit (127);
   }
   else if (thepid == -1)
diff --git a/init.c b/init.c
index 9df285993087ed747cd5f1122017b91fa9b19dd4..758b282597d8d900fe2a8c76d62a15c37a845ed4 100644 (file)
--- a/init.c
+++ b/init.c
@@ -2023,11 +2023,57 @@ static bool valid_show_multipart_alternative(const char *val)
           (val == NULL) || (*val == 0));
 }
 
+char **mutt_envlist (void)
+{
+  return envlist;
+}
+
+/* Helper function for parse_setenv().
+ * It's broken out because some other parts of mutt (filter.c) need
+ * to set/overwrite environment variables in envlist before execing.
+ */
+void mutt_envlist_set (const char *name, const char *value)
+{
+  char **envp = envlist;
+  char work[LONG_STRING];
+  int count, len;
+
+  len = mutt_strlen (name);
+
+  /* Look for current slot to overwrite */
+  count = 0;
+  while (envp && *envp)
+  {
+    if (!mutt_strncmp (name, *envp, len) && (*envp)[len] == '=')
+    {
+      FREE (envp);     /* __FREE_CHECKED__ */
+      break;
+    }
+    envp++;
+    count++;
+  }
+
+  /* Format var=value string */
+  snprintf (work, sizeof(work), "%s=%s", NONULL (name), NONULL (value));
+
+  /* If slot found, overwrite */
+  if (*envp)
+    *envp = safe_strdup (work);
+
+  /* If not found, add new slot */
+  else
+  {
+    *envp = safe_strdup (work);
+    count++;
+    safe_realloc (&envlist, sizeof(char *) * (count + 1));
+    envlist[count] = NULL;
+  }
+}
+
 static int parse_setenv(BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
 {
   int query, unset, len;
-  char work[LONG_STRING];
-  char **save, **envp = envlist;
+  char *name, **save, **envp = envlist;
   int count = 0;
 
   query = 0;
@@ -2102,6 +2148,8 @@ static int parse_setenv(BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
     return -1;
   }
 
+  /* set variable */
+
   if (*s->dptr == '=')
   {
     s->dptr++;
@@ -2114,38 +2162,10 @@ static int parse_setenv(BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
     return -1;
   }
 
-  /* Look for current slot to overwrite */
-  count = 0;
-  while (envp && *envp)
-  {
-    if (!mutt_strncmp (tmp->data, *envp, len) && (*envp)[len] == '=')
-    {
-      FREE (envp);     /* __FREE_CHECKED__ */
-      break;
-    }
-    envp++;
-    count++;
-  }
-
-  /* Format var=value string */
-  strfcpy (work, tmp->data, sizeof(work));
-  len = strlen (work);
-  work[len++] = '=';
+  name = safe_strdup (tmp->data);
   mutt_extract_token (tmp, s, 0);
-  strfcpy (&work[len], tmp->data, sizeof(work)-len);
-
-  /* If slot found, overwrite */
-  if (*envp)
-    *envp = safe_strdup (work);
-
-  /* If not found, add new slot */
-  else
-  {
-    *envp = safe_strdup (work);
-    count++;
-    safe_realloc (&envlist, sizeof(char *) * (count + 1));
-    envlist[count] = NULL;
-  }
+  mutt_envlist_set (name, tmp->data);
+  FREE (&name);
 
   return 0;
 }
index fe548abc7a640e0495c6b3207ca848896058bbce..e4e16255a593a0c05a4f23e0ecc6c421787c330b 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -179,6 +179,8 @@ void mutt_draw_statusline (int cols, const char *buf, int buflen);
 void mutt_edit_content_type (HEADER *, BODY *, FILE *);
 void mutt_edit_file (const char *, const char *);
 void mutt_edit_headers (const char *, const char *, HEADER *, char *, size_t);
+char **mutt_envlist (void);
+void mutt_envlist_set (const char *name, const char *value);
 int mutt_filter_unprintable (char **);
 int mutt_label_message (HEADER *);
 void mutt_make_label_hash (CONTEXT *);
index be109332c499843853f4db994f07cecafe0ca500..ee0f82353645fcb0640ae7156829358b9cbf192b 100644 (file)
--- a/system.c
+++ b/system.c
@@ -30,8 +30,6 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-extern char **envlist;
-
 int _mutt_system (const char *cmd, int flags)
 {
   int rc = -1;
@@ -114,7 +112,7 @@ int _mutt_system (const char *cmd, int flags)
     sigaction (SIGTSTP, &act, NULL);
     sigaction (SIGCONT, &act, NULL);
 
-    execle (EXECSHELL, "sh", "-c", cmd, NULL, envlist);
+    execle (EXECSHELL, "sh", "-c", cmd, NULL, mutt_envlist ());
     _exit (127); /* execl error */
   }
   else if (thepid != -1)