From dbfee6a468172899759a86c7bf50169053adce45 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 9 Mar 2017 17:02:38 +0000 Subject: [PATCH 1/2] Move server_fill_environ into environ.c and move some other common code into it. --- cmd-respawn-pane.c | 9 +++------ cmd-respawn-window.c | 8 ++------ cmd-split-window.c | 14 +++++--------- environ.c | 26 ++++++++++++++++++++++++++ job.c | 7 +------ server-fn.c | 18 ------------------ session.c | 8 ++------ tmux.h | 2 +- 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 #include +#include #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 @@ -32,24 +32,6 @@ static struct session *server_next_session(struct session *); 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) { 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 *); From bce1dee0341b3624d927d393fd0f21771660d5ed Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 9 Mar 2017 17:06:35 +0000 Subject: [PATCH 2/2] Move the client identify (display-panes) code into server-client.c. --- cmd-display-panes.c | 2 +- server-client.c | 49 ++++++++++++++++++++++++++++++++++++++++++--- server-fn.c | 41 ------------------------------------- tmux.h | 4 ++-- 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/cmd-display-panes.c b/cmd-display-panes.c index e670c1b9..6a2fbfd8 100644 --- a/cmd-display-panes.c +++ b/cmd-display-panes.c @@ -61,7 +61,7 @@ cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item) else c->identify_callback_data = xstrdup("select-pane -t '%%'"); - server_set_identify(c); + server_client_set_identify(c); return (CMD_RETURN_NORMAL); } diff --git a/server-client.c b/server-client.c index 4ea79e3d..c983f32f 100644 --- a/server-client.c +++ b/server-client.c @@ -49,6 +49,49 @@ static void server_client_dispatch_command(struct client *, struct imsg *); static void server_client_dispatch_identify(struct client *, struct imsg *); static void server_client_dispatch_shell(struct client *); +/* Idenfity mode callback. */ +static void +server_client_callback_identify(__unused int fd, __unused short events, void *data) +{ + server_client_clear_identify(data, NULL); +} + +/* Set identify mode on client. */ +void +server_client_set_identify(struct client *c) +{ + struct timeval tv; + int delay; + + delay = options_get_number(c->session->options, "display-panes-time"); + tv.tv_sec = delay / 1000; + tv.tv_usec = (delay % 1000) * 1000L; + + if (event_initialized(&c->identify_timer)) + evtimer_del(&c->identify_timer); + evtimer_set(&c->identify_timer, server_client_callback_identify, c); + evtimer_add(&c->identify_timer, &tv); + + c->flags |= CLIENT_IDENTIFY; + c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR); + server_redraw_client(c); +} + +/* Clear identify mode on client. */ +void +server_client_clear_identify(struct client *c, struct window_pane *wp) +{ + if (~c->flags & CLIENT_IDENTIFY) + return; + c->flags &= ~CLIENT_IDENTIFY; + + if (c->identify_callback != NULL) + c->identify_callback(c, wp); + + c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR); + server_redraw_client(c); +} + /* Check if this client is inside this server. */ int server_client_check_nested(struct client *c) @@ -192,7 +235,7 @@ server_client_lost(struct client *c) c->flags |= CLIENT_DEAD; - server_clear_identify(c, NULL); + server_client_clear_identify(c, NULL); status_prompt_clear(c); status_message_clear(c); @@ -760,14 +803,14 @@ server_client_handle_key(struct client *c, key_code key) wp = window_pane_at_index(w, key - '0'); if (wp != NULL && !window_pane_visible(wp)) wp = NULL; - server_clear_identify(c, wp); + server_client_clear_identify(c, wp); return; } /* Handle status line. */ if (!(c->flags & CLIENT_READONLY)) { status_message_clear(c); - server_clear_identify(c, NULL); + server_client_clear_identify(c, NULL); } if (c->prompt_string != NULL) { if (c->flags & CLIENT_READONLY) diff --git a/server-fn.c b/server-fn.c index 9d7702a7..53bcb6b5 100644 --- a/server-fn.c +++ b/server-fn.c @@ -29,7 +29,6 @@ #include "tmux.h" static struct session *server_next_session(struct session *); -static void server_callback_identify(int, short, void *); static void server_destroy_session_group(struct session *); void @@ -405,46 +404,6 @@ server_check_unattached(void) } } -void -server_set_identify(struct client *c) -{ - struct timeval tv; - int delay; - - delay = options_get_number(c->session->options, "display-panes-time"); - tv.tv_sec = delay / 1000; - tv.tv_usec = (delay % 1000) * 1000L; - - if (event_initialized(&c->identify_timer)) - evtimer_del(&c->identify_timer); - evtimer_set(&c->identify_timer, server_callback_identify, c); - evtimer_add(&c->identify_timer, &tv); - - c->flags |= CLIENT_IDENTIFY; - c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR); - server_redraw_client(c); -} - -void -server_clear_identify(struct client *c, struct window_pane *wp) -{ - if (~c->flags & CLIENT_IDENTIFY) - return; - c->flags &= ~CLIENT_IDENTIFY; - - if (c->identify_callback != NULL) - c->identify_callback(c, wp); - - c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR); - server_redraw_client(c); -} - -static void -server_callback_identify(__unused int fd, __unused short events, void *data) -{ - server_clear_identify(data, NULL); -} - /* Set stdin callback. */ int server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int, diff --git a/tmux.h b/tmux.h index a0978a1c..fa7ef636 100644 --- a/tmux.h +++ b/tmux.h @@ -1819,6 +1819,8 @@ void server_update_socket(void); void server_add_accept(int); /* server-client.c */ +void server_client_set_identify(struct client *); +void server_client_clear_identify(struct client *, struct window_pane *); void server_client_set_key_table(struct client *, const char *); const char *server_client_get_key_table(struct client *); int server_client_is_default_key_table(struct client *); @@ -1858,8 +1860,6 @@ void server_unlink_window(struct session *, struct winlink *); void server_destroy_pane(struct window_pane *, int); void server_destroy_session(struct session *); void server_check_unattached(void); -void server_set_identify(struct client *); -void server_clear_identify(struct client *, struct window_pane *); int server_set_stdin_callback(struct client *, void (*)(struct client *, int, void *), void *, char **); void server_unzoom_window(struct window *);