Revision: 2647
          http://tmux.svn.sourceforge.net/tmux/?rev=2647&view=rev
Author:   nicm
Date:     2011-12-09 16:37:29 +0000 (Fri, 09 Dec 2011)
Log Message:
-----------
Change the way the working directory for new processes is discovered. If
default-path isn't empty, it is used. Otherwise:

1) If tmux neww is run from the command line, the working directory of the
client is used.

2) Otherwise use some platform specific code to retrieve the current working
directory of the process in the active pane.

3) If that fails, the directory where the session was created is used.

Idea and support code, Linux, Solaris, FreeBSD bits by Romain Francois, OpenBSD
bits by me.

Modified Paths:
--------------
    trunk/cmd-new-window.c
    trunk/cmd-split-window.c
    trunk/cmd.c
    trunk/osdep-aix.c
    trunk/osdep-darwin.c
    trunk/osdep-dragonfly.c
    trunk/osdep-freebsd.c
    trunk/osdep-hpux.c
    trunk/osdep-linux.c
    trunk/osdep-netbsd.c
    trunk/osdep-openbsd.c
    trunk/osdep-sunos.c
    trunk/osdep-unknown.c
    trunk/tmux.1
    trunk/tmux.h

Modified: trunk/cmd-new-window.c
===================================================================
--- trunk/cmd-new-window.c      2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/cmd-new-window.c      2011-12-09 16:37:29 UTC (rev 2647)
@@ -98,13 +98,7 @@
                cmd = options_get_string(&s->options, "default-command");
        else
                cmd = args->argv[0];
-       cwd = options_get_string(&s->options, "default-path");
-       if (*cwd == '\0') {
-               if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
-                       cwd = ctx->cmdclient->cwd;
-               else
-                       cwd = s->cwd;
-       }
+       cwd = cmd_get_default_path(ctx);
 
        if (idx == -1)
                idx = -1 - options_get_number(&s->options, "base-index");

Modified: trunk/cmd-split-window.c
===================================================================
--- trunk/cmd-split-window.c    2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/cmd-split-window.c    2011-12-09 16:37:29 UTC (rev 2647)
@@ -77,13 +77,7 @@
                cmd = options_get_string(&s->options, "default-command");
        else
                cmd = args->argv[0];
-       cwd = options_get_string(&s->options, "default-path");
-       if (*cwd == '\0') {
-               if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
-                       cwd = ctx->cmdclient->cwd;
-               else
-                       cwd = s->cwd;
-       }
+       cwd = cmd_get_default_path(ctx);
 
        type = LAYOUT_TOPBOTTOM;
        if (args_has(args, 'h'))

Modified: trunk/cmd.c
===================================================================
--- trunk/cmd.c 2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/cmd.c 2011-12-09 16:37:29 UTC (rev 2647)
@@ -1212,3 +1212,28 @@
 
        return (buf);
 }
+
+/* Return the default path for a new pane. */
+char *
+cmd_get_default_path(struct cmd_ctx *ctx)
+{
+       char                    *cwd;
+       struct session          *s;
+       struct window_pane      *wp;
+
+       if ((s = cmd_current_session(ctx, 0)) == NULL)
+               return (NULL);
+
+       cwd = options_get_string(&s->options, "default-path");
+       if (*cwd == '\0') {
+               if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
+                       return (ctx->cmdclient->cwd);
+               if (ctx->curclient != NULL) {
+                       wp = s->curw->window->active;
+                       if ((cwd = osdep_get_cwd(wp->pid)) != NULL)
+                               return (cwd);
+               }
+               return (s->cwd);
+       }
+       return (cwd);
+}

Modified: trunk/osdep-aix.c
===================================================================
--- trunk/osdep-aix.c   2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-aix.c   2011-12-09 16:37:29 UTC (rev 2647)
@@ -28,6 +28,12 @@
        return (NULL);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-darwin.c
===================================================================
--- trunk/osdep-darwin.c        2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-darwin.c        2011-12-09 16:37:29 UTC (rev 2647)
@@ -25,6 +25,7 @@
 #include <unistd.h>
 
 char                   *osdep_get_name(int, char *);
+char                   *osdep_get_cwd(pid_t);
 struct event_base      *osdep_event_init(void);
 
 #define unused __attribute__ ((unused))
@@ -48,6 +49,12 @@
        return (strdup(kp.kp_proc.p_comm));
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-dragonfly.c
===================================================================
--- trunk/osdep-dragonfly.c     2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-dragonfly.c     2011-12-09 16:37:29 UTC (rev 2647)
@@ -31,6 +31,7 @@
 
 struct kinfo_proc      *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
 char                   *osdep_get_name(int, char *);
+char                   *osdep_get_cwd(pid_t);
 struct event_base      *osdep_event_init(void);
 
 #ifndef nitems
@@ -119,6 +120,12 @@
        return (NULL);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-freebsd.c
===================================================================
--- trunk/osdep-freebsd.c       2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-freebsd.c       2011-12-09 16:37:29 UTC (rev 2647)
@@ -29,9 +29,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <libutil.h>
 
 struct kinfo_proc      *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
 char                   *osdep_get_name(int, char *);
+char                   *osdep_get_cwd(pid_t);
 struct event_base      *osdep_event_init(void);
 
 #ifndef nitems
@@ -130,6 +132,28 @@
        return (NULL);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       static char              wd[PATH_MAX];
+       struct kinfo_file       *info = NULL;
+       int                      nrecords, i;
+
+       if ((info = kinfo_getfile(pid, &nrecords)) == NULL)
+               return (NULL);
+
+       for (i = 0; i < nrecords; i++) {
+               if (info[i].kf_fd == KF_FD_TYPE_CWD) {
+                       strlcpy(wd, info[i].kf_path, sizeof wd);
+                       free(info);
+                       return (wd);
+               }
+       }
+
+       free(info);
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-hpux.c
===================================================================
--- trunk/osdep-hpux.c  2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-hpux.c  2011-12-09 16:37:29 UTC (rev 2647)
@@ -28,6 +28,12 @@
        return (NULL);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-linux.c
===================================================================
--- trunk/osdep-linux.c 2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-linux.c 2011-12-09 16:37:29 UTC (rev 2647)
@@ -60,6 +60,23 @@
        return (buf);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       static char      target[MAXPATHLEN + 1];
+       char            *path;
+       ssize_t          n;
+
+       xasprintf(&path, "/proc/%d/cwd", pid);
+       n = readlink(path, target, MAXPATHLEN);
+       xfree(path);
+       if (n > 0) {
+               target[n] = '\0';
+               return (target);
+       }
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-netbsd.c
===================================================================
--- trunk/osdep-netbsd.c        2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-netbsd.c        2011-12-09 16:37:29 UTC (rev 2647)
@@ -34,6 +34,7 @@
 
 struct kinfo_proc2     *cmp_procs(struct kinfo_proc2 *, struct kinfo_proc2 *);
 char                   *osdep_get_name(int, char *);
+char                   *osdep_get_cwd(pid_t);
 struct event_base      *osdep_event_init(void);
 
 struct kinfo_proc2 *
@@ -123,6 +124,12 @@
        return (NULL);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-openbsd.c
===================================================================
--- trunk/osdep-openbsd.c       2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-openbsd.c       2011-12-09 16:37:29 UTC (rev 2647)
@@ -37,6 +37,7 @@
 
 struct kinfo_proc      *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
 char                   *osdep_get_name(int, char *);
+char                   *osdep_get_cwd(pid_t);
 struct event_base      *osdep_event_init(void);
 
 struct kinfo_proc *
@@ -133,6 +134,18 @@
        return (NULL);
 }
 
+char*
+osdep_get_cwd(pid_t pid)
+{
+       int             name[] = { CTL_KERN, KERN_PROC_CWD, (int)pid };
+       static char     path[MAXPATHLEN];
+       size_t          pathlen = sizeof path;
+
+       if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0)
+               return (NULL);
+       return (path);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-sunos.c
===================================================================
--- trunk/osdep-sunos.c 2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-sunos.c 2011-12-09 16:37:29 UTC (rev 2647)
@@ -64,6 +64,23 @@
        return (xstrdup(p.pr_fname));
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       static char      target[MAXPATHLEN + 1];
+       char            *path;
+       ssize_t          n;
+
+       xasprintf(&path, "/proc/%u/path/cwd", (u_int) pid);
+       n = readlink(path, target, MAXPATHLEN);
+       xfree(path);
+       if (n > 0) {
+               target[n] = '\0';
+               return (target);
+       }
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/osdep-unknown.c
===================================================================
--- trunk/osdep-unknown.c       2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/osdep-unknown.c       2011-12-09 16:37:29 UTC (rev 2647)
@@ -28,6 +28,12 @@
        return (NULL);
 }
 
+char *
+osdep_get_cwd(pid_t pid)
+{
+       return (NULL);
+}
+
 struct event_base *
 osdep_event_init(void)
 {

Modified: trunk/tmux.1
===================================================================
--- trunk/tmux.1        2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/tmux.1        2011-12-09 16:37:29 UTC (rev 2647)
@@ -1845,10 +1845,10 @@
 .Ic default-shell
 option.
 .It Ic default-path Ar path
-Set the default working directory for processes created from keys, or
-interactively from the prompt.
-The default is empty, which means to use the working directory of the shell
-from which the server was started if it is available or the user's home if not.
+Set the default working directory for new panes.
+If empty (the default), the working directory is determined from the process
+running in the active pane, from the command line environment or from the
+working directory where the session was created.
 .It Ic default-shell Ar path
 Specify the default shell.
 This is used as the login shell for new windows when the

Modified: trunk/tmux.h
===================================================================
--- trunk/tmux.h        2011-12-06 18:50:26 UTC (rev 2646)
+++ trunk/tmux.h        2011-12-09 16:37:29 UTC (rev 2647)
@@ -1558,6 +1558,7 @@
 struct winlink *cmd_find_pane(struct cmd_ctx *,
                     const char *, struct session **, struct window_pane **);
 char           *cmd_template_replace(char *, const char *, int);
+char           *cmd_get_default_path(struct cmd_ctx *ctx);
 extern const struct cmd_entry *cmd_table[];
 extern const struct cmd_entry cmd_attach_session_entry;
 extern const struct cmd_entry cmd_bind_key_entry;
@@ -2075,6 +2076,7 @@
 
 /* osdep-*.c */
 char           *osdep_get_name(int, char *);
+char           *osdep_get_cwd(pid_t);
 struct event_base *osdep_event_init(void);
 
 /* log.c */

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of 
discussion for anyone considering optimizing the pricing and packaging model 
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
tmux-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tmux-cvs

Reply via email to