changeset: 6889:8a23708d978b user: David Champion <d...@bikeshed.us> date: Sun Dec 11 18:56:59 2016 -0800 link: http://dev.mutt.org/hg/mutt/rev/8a23708d978b
Add setenv/unsetenv commands. These can be used to add and remove environment variables passed to children via mutt_system(). Commited by Kevin McCarthy with some cleanup. diffs (285 lines): diff -r df1d1e379477 -r 8a23708d978b doc/manual.xml.head --- a/doc/manual.xml.head Tue Dec 06 19:07:32 2016 -0800 +++ b/doc/manual.xml.head Sun Dec 11 18:56:59 2016 -0800 @@ -5769,6 +5769,24 @@ </sect1> +<sect1 id="setenv"> +<title>Managing the Environment</title> + +<para> +You can alter the environment that Mutt passes on to its child processes +using the <quote>setenv</quote> and <quote>unsetenv</quote> operators. +(N.B. These follow Mutt-style syntax, not shell-style!) You can also +query current environment values by prefixing a <quote>?</quote> character. +</para> + +<screen> +setenv TERM vt100 +setenv ORGANIZATION "The Mutt Development Team" +unsetenv DISPLAY +setenv ?LESS +</screen> +</sect1> + <sect1 id="query"> <title>External Address Queries</title> @@ -9895,6 +9913,23 @@ </listitem> <listitem> + <cmdsynopsis> + <command><link linkend="setenv">setenv</link></command> + <arg choice="plain"> + <replaceable class="parameter">[?]variable</replaceable> + </arg> + <arg choice="opt"> + <replaceable class="parameter">value</replaceable> + </arg> + + <command><link linkend="setenv">unsetenv</link></command> + <arg choice="plain"> + <replaceable class="parameter">variable</replaceable> + </arg> + </cmdsynopsis> +</listitem> + +<listitem> <cmdsynopsis> <command><link linkend="sidebar-whitelist">sidebar_whitelist</link></command> <arg choice="plain"> diff -r df1d1e379477 -r 8a23708d978b init.c --- a/init.c Tue Dec 06 19:07:32 2016 -0800 +++ b/init.c Sun Dec 11 18:56:59 2016 -0800 @@ -76,6 +76,8 @@ static const char* myvar_get (const char* var); static void myvar_del (const char* var); +extern char **envlist; + static void toggle_quadoption (int opt) { int n = opt/4; @@ -1696,6 +1698,133 @@ return rc; } +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; + int count = 0; + + query = 0; + unset = data & MUTT_SET_UNSET; + + if (!MoreArgs (s)) + { + strfcpy (err->data, _("too few arguments"), err->dsize); + return -1; + } + + if (*s->dptr == '?') + { + query = 1; + s->dptr++; + } + + /* get variable name */ + mutt_extract_token (tmp, s, MUTT_TOKEN_EQUAL); + len = strlen (tmp->data); + + if (query) + { + int found = 0; + while (envp && *envp) + { + if (!mutt_strncmp (tmp->data, *envp, len)) + { + if (!found) + { + mutt_endwin (NULL); + found = 1; + } + puts (*envp); + } + envp++; + } + + if (found) + { + set_option (OPTFORCEREDRAWINDEX); + set_option (OPTFORCEREDRAWPAGER); + mutt_any_key_to_continue (NULL); + return 0; + } + + snprintf (err->data, err->dsize, _("%s is unset"), tmp->data); + return -1; + } + + if (unset) + { + count = 0; + while (envp && *envp) + { + if (!mutt_strncmp (tmp->data, *envp, len) && (*envp)[len] == '=') + { + /* shuffle down */ + save = envp++; + while (*envp) + { + *save++ = *envp++; + count++; + } + *save = NULL; + safe_realloc (&envlist, sizeof(char *) * (count+1)); + return 0; + } + envp++; + count++; + } + return -1; + } + + if (*s->dptr == '=') + { + s->dptr++; + SKIPWS (s->dptr); + } + + if (!MoreArgs (s)) + { + strfcpy (err->data, _("too few arguments"), err->dsize); + 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++] = '='; + 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; + } + + return 0; +} + static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err) { int query, unset, inv, reset, r = 0; diff -r df1d1e379477 -r 8a23708d978b init.h --- a/init.h Tue Dec 06 19:07:32 2016 -0800 +++ b/init.h Sun Dec 11 18:56:59 2016 -0800 @@ -3876,6 +3876,7 @@ static int parse_unignore (BUFFER *, BUFFER *, unsigned long, BUFFER *); static int parse_source (BUFFER *, BUFFER *, unsigned long, BUFFER *); static int parse_set (BUFFER *, BUFFER *, unsigned long, BUFFER *); +static int parse_setenv (BUFFER *, BUFFER *, unsigned long, BUFFER *); static int parse_my_hdr (BUFFER *, BUFFER *, unsigned long, BUFFER *); static int parse_unmy_hdr (BUFFER *, BUFFER *, unsigned long, BUFFER *); static int parse_subscribe (BUFFER *, BUFFER *, unsigned long, BUFFER *); @@ -3953,6 +3954,7 @@ { "send-hook", mutt_parse_hook, MUTT_SENDHOOK }, { "send2-hook", mutt_parse_hook, MUTT_SEND2HOOK }, { "set", parse_set, 0 }, + { "setenv", parse_setenv, 0 }, #ifdef USE_SIDEBAR { "sidebar_whitelist",parse_list, UL &SidebarWhitelist }, { "unsidebar_whitelist",parse_unlist, UL &SidebarWhitelist }, @@ -3973,6 +3975,7 @@ { "unmy_hdr", parse_unmy_hdr, 0 }, { "unscore", mutt_parse_unscore, 0 }, { "unset", parse_set, MUTT_SET_UNSET }, + { "unsetenv", parse_setenv, MUTT_SET_UNSET }, { "unsubscribe", parse_unsubscribe, 0 }, { NULL, NULL, 0 } }; diff -r df1d1e379477 -r 8a23708d978b main.c --- a/main.c Tue Dec 06 19:07:32 2016 -0800 +++ b/main.c Sun Dec 11 18:56:59 2016 -0800 @@ -105,6 +105,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\ "); +char **envlist; + void mutt_exit (int code) { mutt_endwin (NULL); @@ -571,7 +573,7 @@ #define MUTT_RO (1<<3) /* -R */ #define MUTT_SELECT (1<<4) /* -y */ -int main (int argc, char **argv) +int main (int argc, char **argv, char **environ) { char folder[_POSIX_PATH_MAX] = ""; char *subject = NULL; @@ -619,6 +621,17 @@ memset (Options, 0, sizeof (Options)); memset (QuadOptions, 0, sizeof (QuadOptions)); + /* Init envlist */ + { + char **srcp, **dstp; + int count = 0; + for (srcp = environ; srcp && *srcp; srcp++) + count++; + envlist = safe_calloc(count+1, sizeof(char *)); + for (srcp = environ, dstp = envlist; srcp && *srcp; srcp++, dstp++) + *dstp = safe_strdup(*srcp); + } + for (optind = 1; optind < double_dash; ) { /* We're getopt'ing POSIXLY, so we'll be here every time getopt() diff -r df1d1e379477 -r 8a23708d978b system.c --- a/system.c Tue Dec 06 19:07:32 2016 -0800 +++ b/system.c Sun Dec 11 18:56:59 2016 -0800 @@ -32,6 +32,8 @@ #include <sys/wait.h> #include <unistd.h> +extern char **envlist; + int _mutt_system (const char *cmd, int flags) { int rc = -1; @@ -114,7 +116,7 @@ sigaction (SIGTSTP, &act, NULL); sigaction (SIGCONT, &act, NULL); - execl (EXECSHELL, "sh", "-c", cmd, NULL); + execle (EXECSHELL, "sh", "-c", cmd, NULL, envlist); _exit (127); /* execl error */ } else if (thepid != -1)