From 65b9aeb33707e9663ac594b872ab33b9a78d7daa Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Wed, 4 Mar 2009 17:24:07 +0000 Subject: [PATCH] Put socket path in $TMUX. --- TODO | 1 - client-fn.c | 32 +++++++++++++++++++++----------- cmd-respawn-window.c | 12 ++++-------- cmd-split-window.c | 13 +++++-------- server-fn.c | 19 ++++++++++++++++++- session.c | 10 +++------- tmux.h | 11 +++-------- 7 files changed, 54 insertions(+), 44 deletions(-) diff --git a/TODO b/TODO index 37d2f0d2..90cad5b2 100644 --- a/TODO +++ b/TODO @@ -81,7 +81,6 @@ - attach should have a flag to create session if it doesn't exist - swap-pane-up, swap-pane-down (maybe move-pane-*) - move-pane (to window) (maybe break-pane?) -- $TMUX should contain socket path - 88 colour support; new grid cell flag, and 256<->88 88<->16 translation tables - some fix for SF feature request 2527847 - now remain-by-default has gone cannot control it per-session diff --git a/client-fn.c b/client-fn.c index 3171f223..2e3ca11b 100644 --- a/client-fn.c +++ b/client-fn.c @@ -1,4 +1,4 @@ -/* $Id: client-fn.c,v 1.5 2009-01-10 14:43:43 nicm Exp $ */ +/* $Id: client-fn.c,v 1.6 2009-03-04 17:24:07 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -26,29 +26,39 @@ void client_fill_session(struct msg_command_data *data) { - char *env, *ptr, buf[256]; + char *env, *ptr1, *ptr2, buf[256]; + size_t len; const char *errstr; long long ll; data->pid = -1; if ((env = getenv("TMUX")) == NULL) return; - if ((ptr = strchr(env, ',')) == NULL) - return; - if ((size_t) (ptr - env) > sizeof buf) - return; - memcpy(buf, env, ptr - env); - buf[ptr - env] = '\0'; - ll = strtonum(ptr + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) + if ((ptr2 = strrchr(env, ',')) == NULL || ptr2 == env) return; - data->idx = ll; + for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--) + ; + if (*ptr1 != ',') + return; + ptr1++; + ptr2++; + + len = ptr2 - ptr1 - 1; + if (len > (sizeof buf) - 1) + return; + memcpy(buf, ptr1, len); + buf[len] = '\0'; ll = strtonum(buf, 0, LONG_MAX, &errstr); if (errstr != NULL) return; data->pid = ll; + + ll = strtonum(ptr2, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return; + data->idx = ll; } void diff --git a/cmd-respawn-window.c b/cmd-respawn-window.c index 8dcbfaea..bf3bfa6c 100644 --- a/cmd-respawn-window.c +++ b/cmd-respawn-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-respawn-window.c,v 1.13 2009-01-23 16:59:14 nicm Exp $ */ +/* $Id: cmd-respawn-window.c,v 1.14 2009-03-04 17:24:07 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -49,9 +49,8 @@ cmd_respawn_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct window *w; struct window_pane *wp; struct session *s; - const char *env[] = CHILD_ENVIRON; - char buf[256], *cause; - u_int i; + const char **env; + char *cause; if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL) return (-1); @@ -67,10 +66,7 @@ cmd_respawn_window_exec(struct cmd *self, struct cmd_ctx *ctx) } } - if (session_index(s, &i) != 0) - fatalx("session not found"); - xsnprintf(buf, sizeof buf, "TMUX=%ld,%u", (long) getpid(), i); - env[0] = buf; + env = server_fill_environ(s); wp = TAILQ_FIRST(&w->panes); TAILQ_REMOVE(&w->panes, wp, entry); diff --git a/cmd-split-window.c b/cmd-split-window.c index 7471446c..53bae77f 100644 --- a/cmd-split-window.c +++ b/cmd-split-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-split-window.c,v 1.8 2009-01-23 16:59:14 nicm Exp $ */ +/* $Id: cmd-split-window.c,v 1.9 2009-03-04 17:24:07 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -139,18 +139,15 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct winlink *wl; struct window *w; struct window_pane *wp; - const char *env[] = CHILD_ENVIRON; - char buf[256], *cmd, *cwd, *cause; - u_int i, hlimit, lines; + const char **env; + char *cmd, *cwd, *cause; + u_int hlimit, lines; if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL) return (-1); w = wl->window; - if (session_index(s, &i) != 0) - fatalx("session not found"); - xsnprintf(buf, sizeof buf, "TMUX=%ld,%u", (long) getpid(), i); - env[0] = buf; + env = server_fill_environ(s); cmd = data->cmd; if (cmd == NULL) diff --git a/server-fn.c b/server-fn.c index a1e0c714..c2bb00f1 100644 --- a/server-fn.c +++ b/server-fn.c @@ -1,4 +1,4 @@ -/* $Id: server-fn.c,v 1.55 2009-02-27 16:01:31 nicm Exp $ */ +/* $Id: server-fn.c,v 1.56 2009-03-04 17:24:07 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -26,6 +26,23 @@ int server_lock_callback(void *, const char *); +const char ** +server_fill_environ(struct session *s) +{ + static const char *env[] = { NULL /* TMUX= */, "TERM=screen", NULL }; + static char *tmuxvar[MAXPATHLEN + 256]; + u_int idx; + + if (session_index(s, &idx) != 0) + fatalx("session not found"); + + xsnprintf(tmuxvar, sizeof tmuxvar, + "TMUX=%s,%ld,%u", socket_path, (long) getpid(), idx); + env[0] = tmuxvar; + + return (env); +} + void server_write_client( struct client *c, enum hdrtype type, const void *buf, size_t len) diff --git a/session.c b/session.c index d2984bc1..41e0d69b 100644 --- a/session.c +++ b/session.c @@ -1,4 +1,4 @@ -/* $Id: session.c,v 1.52 2009-01-23 16:59:14 nicm Exp $ */ +/* $Id: session.c,v 1.53 2009-03-04 17:24:07 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -200,14 +200,10 @@ session_new(struct session *s, const char *name, const char *cmd, const char *cwd, int idx, char **cause) { struct window *w; - const char *env[] = CHILD_ENVIRON; - char buf[256]; + const char **env; u_int i, hlimit; - if (session_index(s, &i) != 0) - fatalx("session not found"); - xsnprintf(buf, sizeof buf, "TMUX=%ld,%u", (long) getpid(), i); - env[0] = buf; + env = server_fill_environ(s); hlimit = options_get_number(&s->options, "history-limit"); w = window_create(name, cmd, cwd, env, s->sx, s->sy, hlimit, cause); diff --git a/tmux.h b/tmux.h index 9d8e5266..c992eb8b 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.276 2009-03-02 18:05:40 nicm Exp $ */ +/* $Id: tmux.h,v 1.277 2009-03-04 17:24:07 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -19,7 +19,7 @@ #ifndef TMUX_H #define TMUX_H -#define PROTOCOL_VERSION -11 +#define PROTOCOL_VERSION -12 /* Shut up gcc warnings about empty if bodies. */ #define RB_AUGMENT(x) do {} while (0) @@ -117,9 +117,6 @@ extern const char *__progname; /* Default prompt history length. */ #define PROMPT_HISTORY 100 -/* Default environment. */ -#define CHILD_ENVIRON { NULL /* TMUX= */, "TERM=screen", NULL } - /* Minimum pane size. */ #define PANE_MINIMUM 4 /* includes separator line */ @@ -1313,9 +1310,7 @@ int server_start(const char *); int server_msg_dispatch(struct client *); /* server-fn.c */ -struct session *server_extract_session( - struct msg_command_data *, char *, char **); -void server_write(struct client *, enum hdrtype, const void *, size_t); +const char **server_fill_environ(struct session *); void server_write_client( struct client *, enum hdrtype, const void *, size_t); void server_write_session(