From 0bc4603ee530d558f38fe8e7cfc8e3c84007cb0f 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. --- usr.bin/tmux/cmd-if-shell.c | 17 +++++++++++++---- usr.bin/tmux/cmd-run-shell.c | 17 +++++++++++++---- usr.bin/tmux/job.c | 7 +++++-- usr.bin/tmux/status.c | 4 ++-- usr.bin/tmux/tmux.h | 4 ++-- usr.bin/tmux/window-copy.c | 4 ++-- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/usr.bin/tmux/cmd-if-shell.c b/usr.bin/tmux/cmd-if-shell.c index 3456449e548..507ecb5a4a0 100644 --- a/usr.bin/tmux/cmd-if-shell.c +++ b/usr.bin/tmux/cmd-if-shell.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-if-shell.c,v 1.31 2015/04/21 21:31:02 nicm Exp $ */ +/* $OpenBSD: cmd-if-shell.c,v 1.32 2015/04/24 22:19:36 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -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/usr.bin/tmux/cmd-run-shell.c b/usr.bin/tmux/cmd-run-shell.c index 2189686d352..6fac94f519d 100644 --- a/usr.bin/tmux/cmd-run-shell.c +++ b/usr.bin/tmux/cmd-run-shell.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-run-shell.c,v 1.27 2015/02/05 10:29:43 nicm Exp $ */ +/* $OpenBSD: cmd-run-shell.c,v 1.28 2015/04/24 22:19:36 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -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/usr.bin/tmux/job.c b/usr.bin/tmux/job.c index 088d7e4ea3f..7995b6dea54 100644 --- a/usr.bin/tmux/job.c +++ b/usr.bin/tmux/job.c @@ -1,4 +1,4 @@ -/* $OpenBSD: job.c,v 1.34 2014/10/20 23:27:14 nicm Exp $ */ +/* $OpenBSD: job.c,v 1.35 2015/04/24 22:19:36 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -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/usr.bin/tmux/status.c b/usr.bin/tmux/status.c index b39a47c0a6a..8ef0bdc0139 100644 --- a/usr.bin/tmux/status.c +++ b/usr.bin/tmux/status.c @@ -1,4 +1,4 @@ -/* $OpenBSD: status.c,v 1.125 2015/04/19 21:34:21 nicm Exp $ */ +/* $OpenBSD: status.c,v 1.126 2015/04/24 22:19:36 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -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/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 9d86f7d8de2..81b146c61ef 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.497 2015/04/22 15:32:33 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.498 2015/04/24 22:19:36 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -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/usr.bin/tmux/window-copy.c b/usr.bin/tmux/window-copy.c index 4f304cf0b92..4adead2819e 100644 --- a/usr.bin/tmux/window-copy.c +++ b/usr.bin/tmux/window-copy.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window-copy.c,v 1.129 2015/04/21 15:16:06 nicm Exp $ */ +/* $OpenBSD: window-copy.c,v 1.130 2015/04/24 22:19:36 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -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); -- 2.20.1