Make server_client_get_cwd used (almost) everywhere we need to work out

the cwd, and do not fall back to "." as it is pretty useless. GitHub
issue 1331.
This commit is contained in:
nicm 2018-05-24 09:42:49 +00:00
parent 8f5903d7c3
commit b9a6162d2f
9 changed files with 19 additions and 36 deletions

View File

@ -73,14 +73,6 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
struct session *s = item->target.s; struct session *s = item->target.s;
struct winlink *wl = item->target.wl; struct winlink *wl = item->target.wl;
struct window_pane *wp = item->target.wp; struct window_pane *wp = item->target.wp;
const char *cwd;
if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
else if (s != NULL)
cwd = s->cwd;
else
cwd = NULL;
shellcmd = format_single(item, args->argv[0], c, s, wl, wp); shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
if (args_has(args, 'F')) { if (args_has(args, 'F')) {
@ -128,8 +120,8 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
cdata->item = NULL; cdata->item = NULL;
memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse); memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse);
job_run(shellcmd, s, cwd, NULL, cmd_if_shell_callback, job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL,
cmd_if_shell_free, cdata, 0); cmd_if_shell_callback, cmd_if_shell_free, cdata, 0);
free(shellcmd); free(shellcmd);
if (args_has(args, 'b')) if (args_has(args, 'b'))

View File

@ -156,10 +156,8 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
/* Get the new session working directory. */ /* Get the new session working directory. */
if ((tmp = args_get(args, 'c')) != NULL) if ((tmp = args_get(args, 'c')) != NULL)
cwd = format_single(item, tmp, c, NULL, NULL, NULL); cwd = format_single(item, tmp, c, NULL, NULL, NULL);
else if (c != NULL && c->session == NULL && c->cwd != NULL)
cwd = xstrdup(c->cwd);
else else
cwd = xstrdup("."); cwd = xstrdup(server_client_get_cwd(c, NULL));
/* /*
* If this is a new client, check for nesting and save the termios * If this is a new client, check for nesting and save the termios

View File

@ -95,10 +95,8 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
if ((tmp = args_get(args, 'c')) != NULL) if ((tmp = args_get(args, 'c')) != NULL)
cwd = format_single(item, tmp, c, s, NULL, NULL); cwd = format_single(item, tmp, c, s, NULL, NULL);
else if (item->client != NULL && item->client->session == NULL)
cwd = xstrdup(item->client->cwd);
else else
cwd = xstrdup(s->cwd); cwd = xstrdup(server_client_get_cwd(item->client, s));
if ((tmp = args_get(args, 'n')) != NULL) if ((tmp = args_get(args, 'n')) != NULL)
name = format_single(item, tmp, c, s, NULL, NULL); name = format_single(item, tmp, c, s, NULL, NULL);

View File

@ -90,14 +90,6 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
struct session *s = item->target.s; struct session *s = item->target.s;
struct winlink *wl = item->target.wl; struct winlink *wl = item->target.wl;
struct window_pane *wp = item->target.wp; struct window_pane *wp = item->target.wp;
const char *cwd;
if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
else if (s != NULL)
cwd = s->cwd;
else
cwd = NULL;
cdata = xcalloc(1, sizeof *cdata); cdata = xcalloc(1, sizeof *cdata);
cdata->cmd = format_single(item, args->argv[0], c, s, wl, wp); cdata->cmd = format_single(item, args->argv[0], c, s, wl, wp);
@ -110,8 +102,8 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
if (!args_has(args, 'b')) if (!args_has(args, 'b'))
cdata->item = item; cdata->item = item;
job_run(cdata->cmd, s, cwd, NULL, cmd_run_shell_callback, job_run(cdata->cmd, s, server_client_get_cwd(item->client, s), NULL,
cmd_run_shell_free, cdata, 0); cmd_run_shell_callback, cmd_run_shell_free, cdata, 0);
if (args_has(args, 'b')) if (args_has(args, 'b'))
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);

View File

@ -61,7 +61,7 @@ cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
if (*path == '/') if (*path == '/')
pattern = xstrdup(path); pattern = xstrdup(path);
else { else {
utf8_stravis(&tmp, server_client_get_cwd(c), VIS_GLOB); utf8_stravis(&tmp, server_client_get_cwd(c, NULL), VIS_GLOB);
xasprintf(&pattern, "%s/%s", tmp, path); xasprintf(&pattern, "%s/%s", tmp, path);
free(tmp); free(tmp);
} }

View File

@ -88,10 +88,8 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
if ((tmp = args_get(args, 'c')) != NULL) if ((tmp = args_get(args, 'c')) != NULL)
cwd = format_single(item, tmp, c, s, NULL, NULL); cwd = format_single(item, tmp, c, s, NULL, NULL);
else if (item->client != NULL && item->client->session == NULL)
cwd = xstrdup(item->client->cwd);
else else
cwd = xstrdup(s->cwd); cwd = xstrdup(server_client_get_cwd(item->client, s));
type = LAYOUT_TOPBOTTOM; type = LAYOUT_TOPBOTTOM;
if (args_has(args, 'h')) if (args_has(args, 'h'))

View File

@ -1847,15 +1847,19 @@ server_client_add_message(struct client *c, const char *fmt, ...)
/* Get client working directory. */ /* Get client working directory. */
const char * const char *
server_client_get_cwd(struct client *c) server_client_get_cwd(struct client *c, struct session *s)
{ {
struct session *s; const char *home;
if (c != NULL && c->session == NULL && c->cwd != NULL) if (c != NULL && c->session == NULL && c->cwd != NULL)
return (c->cwd); return (c->cwd);
if (s != NULL && s->cwd != NULL)
return (s->cwd);
if (c != NULL && (s = c->session) != NULL && s->cwd != NULL) if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
return (s->cwd); return (s->cwd);
return ("."); if ((home = find_home()) != NULL)
return (home);
return ("/");
} }
/* Resolve an absolute path or relative to client working directory. */ /* Resolve an absolute path or relative to client working directory. */
@ -1867,7 +1871,7 @@ server_client_get_path(struct client *c, const char *file)
if (*file == '/') if (*file == '/')
path = xstrdup(file); path = xstrdup(file);
else else
xasprintf(&path, "%s/%s", server_client_get_cwd(c), file); xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
if (realpath(path, resolved) == NULL) if (realpath(path, resolved) == NULL)
return (path); return (path);
free(path); free(path);

2
tmux.h
View File

@ -1906,7 +1906,7 @@ void server_client_push_stderr(struct client *);
void printflike(2, 3) server_client_add_message(struct client *, const char *, void printflike(2, 3) server_client_add_message(struct client *, const char *,
...); ...);
char *server_client_get_path(struct client *, const char *); char *server_client_get_path(struct client *, const char *);
const char *server_client_get_cwd(struct client *); const char *server_client_get_cwd(struct client *, struct session *);
/* server-fn.c */ /* server-fn.c */
void server_redraw_client(struct client *); void server_redraw_client(struct client *);

View File

@ -918,7 +918,8 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv,
cmd = cmd_stringify_argv(wp->argc, wp->argv); cmd = cmd_stringify_argv(wp->argc, wp->argv);
log_debug("%s: shell=%s", __func__, wp->shell); log_debug("%s: shell=%s", __func__, wp->shell);
log_debug("%s: command=%s", __func__, cmd); log_debug("%s: cmd=%s", __func__, cmd);
log_debug("%s: cwd=%s", __func__, cwd);
for (i = 0; i < wp->argc; i++) for (i = 0; i < wp->argc; i++)
log_debug("%s: argv[%d]=%s", __func__, i, wp->argv[i]); log_debug("%s: argv[%d]=%s", __func__, i, wp->argv[i]);
environ_log(env, "%s: environment ", __func__); environ_log(env, "%s: environment ", __func__);