summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicm <nicm>2017-03-09 17:02:38 +0000
committernicm <nicm>2017-03-09 17:02:38 +0000
commitdbfee6a468172899759a86c7bf50169053adce45 (patch)
tree6db1e79cc83501839bb96436a430cce03c1ea730
parente8f2609ca445b09b04cb131059e5b71c3a40f451 (diff)
Move server_fill_environ into environ.c and move some other common code
into it.
-rw-r--r--cmd-respawn-pane.c9
-rw-r--r--cmd-respawn-window.c8
-rw-r--r--cmd-split-window.c14
-rw-r--r--environ.c26
-rw-r--r--job.c7
-rw-r--r--server-fn.c18
-rw-r--r--session.c8
-rw-r--r--tmux.h2
8 files changed, 40 insertions, 52 deletions
diff --git a/cmd-respawn-pane.c b/cmd-respawn-pane.c
index b95fc596..66e0d955 100644
--- a/cmd-respawn-pane.c
+++ b/cmd-respawn-pane.c
@@ -65,11 +65,6 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_ERROR);
}
- env = environ_create();
- environ_copy(global_environ, env);
- environ_copy(s->environ, env);
- server_fill_environ(s, env);
-
window_pane_reset_mode(wp);
screen_reinit(&wp->base);
input_init(wp);
@@ -82,6 +77,7 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item)
if (envent != NULL)
path = envent->value;
+ env = environ_for_session(s);
if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env,
s->tio, &cause) != 0) {
cmdq_error(item, "respawn pane failed: %s", cause);
@@ -89,9 +85,10 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item)
environ_free(env);
return (CMD_RETURN_ERROR);
}
+ environ_free(env);
+
wp->flags |= PANE_REDRAW;
server_status_window(w);
- environ_free(env);
return (CMD_RETURN_NORMAL);
}
diff --git a/cmd-respawn-window.c b/cmd-respawn-window.c
index 92ba6299..876fab6a 100644
--- a/cmd-respawn-window.c
+++ b/cmd-respawn-window.c
@@ -66,11 +66,6 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item)
}
}
- env = environ_create();
- environ_copy(global_environ, env);
- environ_copy(s->environ, env);
- server_fill_environ(s, env);
-
wp = TAILQ_FIRST(&w->panes);
TAILQ_REMOVE(&w->panes, wp, entry);
layout_free(w);
@@ -86,6 +81,7 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item)
if (envent != NULL)
path = envent->value;
+ env = environ_for_session(s);
if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env,
s->tio, &cause) != 0) {
cmdq_error(item, "respawn window failed: %s", cause);
@@ -94,6 +90,7 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item)
server_destroy_pane(wp, 0);
return (CMD_RETURN_ERROR);
}
+ environ_free(env);
layout_init(w, wp);
window_pane_reset_mode(wp);
screen_reinit(&wp->base);
@@ -103,6 +100,5 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item)
recalculate_sizes();
server_redraw_window(w);
- environ_free(env);
return (CMD_RETURN_NORMAL);
}
diff --git a/cmd-split-window.c b/cmd-split-window.c
index 064d1f5f..4d061d94 100644
--- a/cmd-split-window.c
+++ b/cmd-split-window.c
@@ -71,11 +71,6 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
server_unzoom_window(w);
- env = environ_create();
- environ_copy(global_environ, env);
- environ_copy(s->environ, env);
- server_fill_environ(s, env);
-
if (args->argc == 0) {
cmd = options_get_string(s->options, "default-command");
if (cmd != NULL && *cmd != '\0') {
@@ -148,9 +143,13 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
if (envent != NULL)
path = envent->value;
+ env = environ_for_session(s);
if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, env,
- s->tio, &cause) != 0)
+ s->tio, &cause) != 0) {
+ environ_free(env);
goto error;
+ }
+ environ_free(env);
server_redraw_window(w);
@@ -161,8 +160,6 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
} else
server_status_session(s);
- environ_free(env);
-
if (args_has(args, 'P')) {
if ((template = args_get(args, 'F')) == NULL)
template = SPLIT_WINDOW_TEMPLATE;
@@ -186,7 +183,6 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
error:
- environ_free(env);
if (new_wp != NULL) {
layout_close_pane(new_wp);
window_remove_pane(w, new_wp);
diff --git a/environ.c b/environ.c
index 4bb794dd..868324ed 100644
--- a/environ.c
+++ b/environ.c
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "tmux.h"
@@ -218,3 +219,28 @@ environ_log(struct environ *env, const char *prefix)
}
}
}
+
+/* Create initial environment for new child. */
+struct environ *
+environ_for_session(struct session *s)
+{
+ struct environ *env;
+ const char *value;
+ int idx;
+
+ env = environ_create();
+ environ_copy(global_environ, env);
+ if (s != NULL)
+ environ_copy(s->environ, env);
+
+ value = options_get_string(global_options, "default-terminal");
+ environ_set(env, "TERM", "%s", value);
+
+ if (s != NULL)
+ idx = s->id;
+ else
+ idx = -1;
+ environ_set(env, "TMUX", "%s,%ld,%d", socket_path, (long)getpid(), idx);
+
+ return (env);
+}
diff --git a/job.c b/job.c
index 8310c376..d5b8713f 100644
--- a/job.c
+++ b/job.c
@@ -53,12 +53,7 @@ job_run(const char *cmd, struct session *s, const char *cwd,
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0)
return (NULL);
- env = environ_create();
- environ_copy(global_environ, env);
- if (s != NULL)
- environ_copy(s->environ, env);
- server_fill_environ(s, env);
-
+ env = environ_for_session(s);
switch (pid = fork()) {
case -1:
environ_free(env);
diff --git a/server-fn.c b/server-fn.c
index c14b4cab..9d7702a7 100644
--- a/server-fn.c
+++ b/server-fn.c
@@ -33,24 +33,6 @@ static void server_callback_identify(int, short, void *);
static void server_destroy_session_group(struct session *);
void
-server_fill_environ(struct session *s, struct environ *env)
-{
- const char *term;
- u_int idx;
- long pid;
-
- if (s != NULL) {
- term = options_get_string(global_options, "default-terminal");
- environ_set(env, "TERM", "%s", term);
-
- idx = s->id;
- } else
- idx = (u_int)-1;
- pid = getpid();
- environ_set(env, "TMUX", "%s,%ld,%u", socket_path, pid, idx);
-}
-
-void
server_redraw_client(struct client *c)
{
c->flags |= CLIENT_REDRAW;
diff --git a/session.c b/session.c
index 5ffa2b87..cb8ed1eb 100644
--- a/session.c
+++ b/session.c
@@ -355,16 +355,12 @@ session_new(struct session *s, const char *name, int argc, char **argv,
}
wl->session = s;
- env = environ_create();
- environ_copy(global_environ, env);
- environ_copy(s->environ, env);
- server_fill_environ(s, env);
-
shell = options_get_string(s->options, "default-shell");
if (*shell == '\0' || areshell(shell))
shell = _PATH_BSHELL;
hlimit = options_get_number(s->options, "history-limit");
+ env = environ_for_session(s);
w = window_create_spawn(name, argc, argv, path, shell, cwd, env, s->tio,
s->sx, s->sy, hlimit, cause);
if (w == NULL) {
@@ -373,8 +369,8 @@ session_new(struct session *s, const char *name, int argc, char **argv,
return (NULL);
}
winlink_set_window(wl, w);
- notify_session_window("window-linked", s, w);
environ_free(env);
+ notify_session_window("window-linked", s, w);
session_group_synchronize_from(s);
return (wl);
diff --git a/tmux.h b/tmux.h
index 61ce665b..a0978a1c 100644
--- a/tmux.h
+++ b/tmux.h
@@ -1613,6 +1613,7 @@ void environ_unset(struct environ *, const char *);
void environ_update(struct options *, struct environ *, struct environ *);
void environ_push(struct environ *);
void environ_log(struct environ *, const char *);
+struct environ *environ_for_session(struct session *);
/* tty.c */
void tty_create_log(void);
@@ -1838,7 +1839,6 @@ char *server_client_get_path(struct client *, const char *);
const char *server_client_get_cwd(struct client *);
/* server-fn.c */
-void server_fill_environ(struct session *, struct environ *);
void server_redraw_client(struct client *);
void server_status_client(struct client *);
void server_redraw_session(struct session *);