Add -s option to the shown-environment command which will alter the output format to Bourne shell commands, allowing import of the environment into current shell with:
eval `tmux showenv -s` --- cmd-show-environment.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- tmux.1 | 7 +++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/cmd-show-environment.c b/cmd-show-environment.c index ffe98bc..31058af 100644 --- a/cmd-show-environment.c +++ b/cmd-show-environment.c @@ -31,14 +31,61 @@ enum cmd_retval cmd_show_environment_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_show_environment_entry = { "show-environment", "showenv", - "gt:", 0, 1, - "[-g] " CMD_TARGET_SESSION_USAGE " [name]", + "gst:", 0, 1, + "[-gs] " CMD_TARGET_SESSION_USAGE " [name]", 0, NULL, NULL, cmd_show_environment_exec }; +static void +sh_environment( struct cmd_q *cmdq, struct environ_entry *envent ) +{ + char *buf; + char *old; + char *val; + size_t len; + size_t bufsize = 128; + + val = buf = xmalloc(bufsize); + + if (envent->value != NULL) { + old = envent->value; + *(val++) = '\''; + len = 1; + + while (*old) { + /* Ensure buffer has enough space + * including closing quote and NUL terminator + * even if current character needs to be quoted. + * 6 = 3(inner quote)+1(current char)+1(final quote)+1(NUL) + */ + if(bufsize < len + 6) { + bufsize *= 2; + buf = xrealloc(buf, 1, bufsize); + val = buf+len; + } + + /* Escape single quotes by converting to '\'' */ + if (*old == '\'') { + *(val++) = '\''; + *(val++) = '\\'; + *(val++) = '\''; + len += 3; + } + *(val++) = *(old++); + ++len; + } + *(val++) = '\''; + } + + *val = 0; + cmdq_print(cmdq, "%s=%s", envent->name, buf); + + free(buf); +} + enum cmd_retval cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) { @@ -61,6 +108,10 @@ cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "unknown variable: %s", args->argv[0]); return (CMD_RETURN_ERROR); } + if (args_has(self->args, 's')) { + sh_environment( cmdq, envent ); + return (CMD_RETURN_NORMAL); + } if (envent->value != NULL) cmdq_print(cmdq, "%s=%s", envent->name, envent->value); else @@ -69,6 +120,10 @@ cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) } RB_FOREACH(envent, environ, env) { + if (args_has(self->args, 's')) { + sh_environment( cmdq, envent ); + continue; + } if (envent->value != NULL) cmdq_print(cmdq, "%s=%s", envent->name, envent->value); else diff --git a/tmux.1 b/tmux.1 index ce4b9b8..0b5a557 100644 --- a/tmux.1 +++ b/tmux.1 @@ -3207,7 +3207,7 @@ flag unsets a variable. indicates the variable is to be removed from the environment before starting a new process. .It Xo Ic show-environment -.Op Fl g +.Op Fl gs .Op Fl t Ar target-session .Op Ar variable .Xc @@ -3221,6 +3221,11 @@ If is omitted, all variables are shown. Variables removed from the environment are prefixed with .Ql - . +If the +.Fl s +option is used, the output will be in the form of Bourne shell +variable assignments allowing it to be imported into the current shell +using eval. .El .Sh STATUS LINE .Nm -- 1.7.10.4 ------------------------------------------------------------------------------ Get 100% visibility into Java/.NET code with AppDynamics Lite! It's a free troubleshooting tool designed for production. Get down to code-level detail for bottlenecks, with <2% overhead. Download for free and get started troubleshooting in minutes. http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk _______________________________________________ tmux-users mailing list tmux-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/tmux-users