mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-03 17:24:18 +00:00 
			
		
		
		
	Move server_fill_environ into environ.c and move some other common code
into it.
This commit is contained in:
		@@ -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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								environ.c
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								job.c
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								server-fn.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								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)
 | 
			
		||||
{
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								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 *);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user