From 583b4ab72b7bf66fda8ab63a08fe435483de5e5a Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 24 Apr 2015 22:19:36 +0000 Subject: [PATCH] Set working directory for run-shell and if-shell. --- cmd-if-shell.c | 15 ++++++++++++--- cmd-run-shell.c | 15 ++++++++++++--- job.c | 5 ++++- status.c | 2 +- tmux.h | 2 +- window-copy.c | 2 +- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/cmd-if-shell.c b/cmd-if-shell.c index cdd2135c..a307bd2f 100644 --- a/cmd-if-shell.c +++ b/cmd-if-shell.c @@ -66,16 +66,24 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) struct winlink *wl = NULL; struct window_pane *wp = NULL; struct format_tree *ft; + int cwd; - if (args_has(args, 't')) + if (args_has(args, 't')) { wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); - else { + cwd = wp->cwd; + } else { c = cmd_find_client(cmdq, NULL, 1); if (c != NULL && c->session != NULL) { s = c->session; wl = s->curw; wp = wl->window->active; } + if (cmdq->client != NULL && cmdq->client->session == NULL) + cwd = cmdq->client->cwd; + else if (s != NULL) + cwd = s->cwd; + else + cwd = -1; } ft = format_create(); @@ -118,7 +126,8 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) cmdq->references++; cdata->references = 1; - job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata); + job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, + cdata); free(shellcmd); if (cdata->bflag) diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 5d6d178b..134cbeba 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -80,16 +80,24 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) struct winlink *wl = NULL; struct window_pane *wp = NULL; struct format_tree *ft; + int cwd; - if (args_has(args, 't')) + if (args_has(args, 't')) { wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); - else { + cwd = wp->cwd; + } else { c = cmd_find_client(cmdq, NULL, 1); if (c != NULL && c->session != NULL) { s = c->session; wl = s->curw; wp = wl->window->active; } + if (cmdq->client != NULL && cmdq->client->session == NULL) + cwd = cmdq->client->cwd; + else if (s != NULL) + cwd = s->cwd; + else + cwd = -1; } ft = format_create(); @@ -105,7 +113,8 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) cdata->cmdq = cmdq; cmdq->references++; - job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata); + job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free, + cdata); if (cdata->bflag) return (CMD_RETURN_NORMAL); diff --git a/job.c b/job.c index c04a70b0..5675eec3 100644 --- a/job.c +++ b/job.c @@ -41,7 +41,7 @@ struct joblist all_jobs = LIST_HEAD_INITIALIZER(all_jobs); /* Start a job running, if it isn't already. */ struct job * -job_run(const char *cmd, struct session *s, +job_run(const char *cmd, struct session *s, int cwd, void (*callbackfn)(struct job *), void (*freefn)(void *), void *data) { struct job *job; @@ -67,6 +67,9 @@ job_run(const char *cmd, struct session *s, case 0: /* child */ clear_signals(1); + if (cwd != -1 && fchdir(cwd) != 0) + chdir("/"); + environ_push(&env); environ_free(&env); diff --git a/status.c b/status.c index ae5d99ab..fd0292c7 100644 --- a/status.c +++ b/status.c @@ -515,7 +515,7 @@ status_find_job(struct client *c, char **iptr) /* If not found at all, start the job and add to the tree. */ if (so == NULL) { - job_run(cmd, NULL, status_job_callback, status_job_free, c); + job_run(cmd, NULL, -1, status_job_callback, status_job_free, c); c->references++; so = xmalloc(sizeof *so); diff --git a/tmux.h b/tmux.h index a47218e5..9a6e4c83 100644 --- a/tmux.h +++ b/tmux.h @@ -1613,7 +1613,7 @@ int options_table_find(const char *, const struct options_table_entry **, /* job.c */ extern struct joblist all_jobs; -struct job *job_run(const char *, struct session *, +struct job *job_run(const char *, struct session *, int, void (*)(struct job *), void (*)(void *), void *); void job_free(struct job *); void job_died(struct job *, int); diff --git a/window-copy.c b/window-copy.c index eb00131e..7d5bae4c 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1465,7 +1465,7 @@ window_copy_copy_pipe(struct window_pane *wp, struct session *sess, format_defaults(ft, NULL, sess, NULL, wp); expanded = format_expand(ft, arg); - job = job_run(expanded, sess, NULL, NULL, NULL); + job = job_run(expanded, sess, -1, NULL, NULL, NULL); bufferevent_write(job->event, buf, len); free(expanded);