Micah Cowan wrote:
> Micah Cowan wrote:
>> Nicholas Marriott wrote:
>>> Hi
>>>
>>> This is a cool idea, tmux should definitely just do the right thing.
>>>
>>> We could call realpath() to get around the // issue. If we did it on path in
>>> main.c and that would clean up TMUX as well.
>> Alright.
>>
>> I'm thinking it may be worthwhile to parse TMUX properly, once, and
>> avoid all these various partial parses we're now doing. I'll submit a
>> patch for that first, and submit the revision for this patch to be
>> applied after that one.
> 
> Attached is the patch for parsing TMUX in one place.

Ugh, that patch is broken, as it tests for the results of fill_session
before it's called. Here's a fixed version, tmux-parse.1.diff, and a
revised nested.1.diff, which is meant to apply atop tmux-parse.1.diff.

-mjc
Index: sf/server-client.c
===================================================================
--- sf.orig/server-client.c	2010-02-05 08:36:25.000000000 -0800
+++ sf/server-client.c	2010-02-05 08:37:43.000000000 -0800
@@ -692,17 +692,6 @@
 	}
 	cmd_free_argv(argc, argv);
 
-	if (data->pid != -1) {
-		TAILQ_FOREACH(cmd, cmdlist, qentry) {
-			if (cmd->entry->flags & CMD_CANTNEST) {
-				server_client_msg_error(&ctx,
-				    "sessions should be nested with care. "
-				    "unset $TMUX to force");
-				goto error;
-			}
-		}
-	}
-
 	if (cmd_list_exec(cmdlist, &ctx) != 1)
 		server_write_client(c, MSG_EXIT, NULL, 0);
 	cmd_list_free(cmdlist);
Index: sf/tmux.c
===================================================================
--- sf.orig/tmux.c	2010-02-05 08:37:05.000000000 -0800
+++ sf/tmux.c	2010-02-05 08:42:07.000000000 -0800
@@ -491,6 +491,7 @@
 		cmddata.pid = envdata.pid;
 		cmddata.idx = envdata.idx;
 
+		/* Prepare command for server. */
 		cmddata.argc = argc;
 		if (cmd_pack_argv(
 		    argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
@@ -506,7 +507,7 @@
 	if (shellcmd != NULL)
 		cmdflags |= CMD_STARTSERVER;
 	else if (argc == 0)	/* new-session is the default */
-		cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON;
+		cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
 	else {
 		/*
 		 * It sucks parsing the command string twice (in client and
@@ -523,10 +524,22 @@
 				cmdflags |= CMD_STARTSERVER;
 			if (cmd->entry->flags & CMD_SENDENVIRON)
 				cmdflags |= CMD_SENDENVIRON;
+			if (cmd->entry->flags & CMD_CANTNEST)
+				cmdflags |= CMD_CANTNEST;
 		}
 		cmd_list_free(cmdlist);
 	}
 
+	/* Heuristic check for nested tmux sessions on same server. */
+	if (shellcmd == NULL
+	    && envdata.path != NULL
+	    && (cmdflags & CMD_CANTNEST)
+	    && ((path == envdata.path) || strcmp(path, envdata.path) == 0)) {
+		log_warnx("sessions should be nested with care."
+			  "unset $TMUX to force.");
+		exit(1);
+	}
+
 	if ((main_ibuf = client_init(path, cmdflags, flags)) == NULL)
 		exit(1);
 	xfree(path);
Index: sf/tmux.c
===================================================================
--- sf.orig/tmux.c	2010-02-04 19:06:51.000000000 -0800
+++ sf/tmux.c	2010-02-05 08:37:05.000000000 -0800
@@ -30,6 +30,12 @@
 
 #include "tmux.h"
 
+struct env_session_data {
+	char		*path;
+	pid_t		pid;
+	u_int		idx;
+};
+
 #if defined(DEBUG) && defined(__OpenBSD__)
 extern char	*malloc_options;
 #endif
@@ -46,7 +52,7 @@
 int		 login_shell;
 
 __dead void	 usage(void);
-void	 	 fill_session(struct msg_command_data *);
+void	 	 fill_session(struct env_session_data *);
 char 		*makesockpath(const char *);
 __dead void	 shell_exec(const char *, const char *);
 
@@ -132,9 +138,9 @@
 }
 
 void
-fill_session(struct msg_command_data *data)
+fill_session(struct env_session_data *data)
 {
-	char		*env, *ptr1, *ptr2, buf[256];
+	char		*env, *path_pid, *pid_idx, buf[256];
 	size_t		 len;
 	const char	*errstr;
 	long long	 ll;
@@ -143,19 +149,24 @@
 	if ((env = getenv("TMUX")) == NULL)
 		return;
 
-	if ((ptr2 = strrchr(env, ',')) == NULL || ptr2 == env)
+	if ((path_pid = strchr(env, ',')) == NULL || path_pid == env)
+		return;
+	if ((pid_idx = strchr(path_pid+1, ',')) == NULL)
 		return;
-	for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--)
-		;
-	if (*ptr1 != ',')
+	if ((pid_idx == path_pid+1 || pid_idx[1] == '\0'))
 		return;
-	ptr1++;
-	ptr2++;
 
-	len = ptr2 - ptr1 - 1;
+	/* path */
+	len = path_pid - env;
+	data->path = xmalloc (len + 1);
+	memcpy(data->path, env, len);
+	data->path[len] = '\0';
+
+	/* pid */
+	len = pid_idx - path_pid - 1;
 	if (len > (sizeof buf) - 1)
 		return;
-	memcpy(buf, ptr1, len);
+	memcpy(buf, path_pid+1, len);
 	buf[len] = '\0';
 
 	ll = strtonum(buf, 0, LONG_MAX, &errstr);
@@ -163,7 +174,8 @@
 		return;
 	data->pid = ll;
 
-	ll = strtonum(ptr2, 0, UINT_MAX, &errstr);
+	/* idx */
+	ll = strtonum(pid_idx+1, 0, UINT_MAX, &errstr);
 	if (errstr != NULL)
 		return;
 	data->idx = ll;
@@ -227,6 +239,7 @@
 	struct passwd		*pw;
 	struct options		*oo, *so, *wo;
 	struct keylist		*keylist;
+	struct env_session_data	 envdata;
 	struct msg_command_data	 cmddata;
 	char			*s, *shellcmd, *path, *label, *home, *cause;
 	char			 cwd[MAXPATHLEN], **var;
@@ -241,6 +254,7 @@
 
 	flags = 0;
 	shellcmd = label = path = NULL;
+	envdata.path = NULL;
 	login_shell = (**argv == '-');
 	while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) {
 		switch (opt) {
@@ -450,13 +464,11 @@
 	 * Figure out the socket path. If specified on the command-line with
 	 * -S or -L, use it, otherwise try $TMUX or assume -L default.
 	 */
+	fill_session(&envdata);
 	if (path == NULL) {
 		/* No -L. Try $TMUX, or default. */
 		if (label == NULL) {
-			if ((path = getenv("TMUX")) != NULL) {
-				path = xstrdup(path);
-				path[strcspn(path, ",")] = '\0';
-			} else
+			if ((path = envdata.path) == NULL)
 				label = xstrdup("default");
 		}
 
@@ -476,7 +488,8 @@
 		buf = NULL;
 		len = 0;
 	} else {
-		fill_session(&cmddata);
+		cmddata.pid = envdata.pid;
+		cmddata.idx = envdata.idx;
 
 		cmddata.argc = argc;
 		if (cmd_pack_argv(
------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to