From ddc5744a0ab7209d684a67aa99d3351b3b10980a Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Sat, 18 Mar 2017 13:48:02 -0700 Subject: [PATCH] Pass envlist to filter children too. (closes #3922) 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 | 4 +-- init.c | 86 ++++++++++++++++++++++++++++++++++---------------------- protos.h | 2 ++ system.c | 4 +-- 4 files changed, 58 insertions(+), 38 deletions(-) diff --git a/filter.c b/filter.c index 08e4ae1ee..bb33b883e 100644 --- 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 9df285993..758b28259 100644 --- 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; } diff --git a/protos.h b/protos.h index fe548abc7..e4e16255a 100644 --- 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 *); diff --git a/system.c b/system.c index be109332c..ee0f82353 100644 --- a/system.c +++ b/system.c @@ -30,8 +30,6 @@ #include #include -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) -- 2.40.0