Skip to content

Commit

Permalink
Fix error reporting for client commands by adding a flag to
Browse files Browse the repository at this point in the history
cmd_find_client to tell it whether or not to show errors, sometimes it's
needed and sometimes not.
  • Loading branch information
nicm committed Mar 24, 2013
1 parent 3eae71b commit bb8457b
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
template = BREAK_PANE_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)
if ((c = cmd_find_client(ctx, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
Expand Down
2 changes: 1 addition & 1 deletion cmd-command-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
char *prompt, *ptr, *input = NULL;
size_t n;

if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (c->prompt_string != NULL)
Expand Down
2 changes: 1 addition & 1 deletion cmd-confirm-before.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ cmd_confirm_before_exec(struct cmd *self, struct cmd_ctx *ctx)
return (CMD_RETURN_ERROR);
}

if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if ((prompt = args_get(args, 'p')) != NULL)
Expand Down
2 changes: 1 addition & 1 deletion cmd-detach-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ cmd_detach_client_exec(struct cmd *self, struct cmd_ctx *ctx)
server_write_client(c, msgtype, NULL, 0);
}
} else {
c = cmd_find_client(ctx, args_get(args, 't'));
c = cmd_find_client(ctx, args_get(args, 't'), 0);
if (c == NULL)
return (CMD_RETURN_ERROR);

Expand Down
6 changes: 2 additions & 4 deletions cmd-display-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ cmd_display_message_exec(struct cmd *self, struct cmd_ctx *ctx)
time_t t;
size_t len;

if ((c = cmd_find_client(ctx, args_get(args, 'c'))) == NULL)
return (CMD_RETURN_ERROR);

if (args_has(args, 't')) {
wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp);
if (wl == NULL)
Expand All @@ -80,7 +77,8 @@ cmd_display_message_exec(struct cmd *self, struct cmd_ctx *ctx)
template = DISPLAY_MESSAGE_TEMPLATE;

ft = format_create();
format_client(ft, c);
if ((c = cmd_find_client(ctx, args_get(args, 'c'), 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wp);
Expand Down
2 changes: 1 addition & 1 deletion cmd-display-panes.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cmd_display_panes_exec(struct cmd *self, struct cmd_ctx *ctx)
struct args *args = self->args;
struct client *c;

if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

server_set_identify(c);
Expand Down
2 changes: 1 addition & 1 deletion cmd-lock-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ cmd_lock_server_exec(struct cmd *self, unused struct cmd_ctx *ctx)
return (CMD_RETURN_ERROR);
server_lock_session(s);
} else {
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);
server_lock_client(c);
}
Expand Down
2 changes: 1 addition & 1 deletion cmd-new-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx)
template = NEW_WINDOW_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)
if ((c = cmd_find_client(ctx, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
Expand Down
2 changes: 1 addition & 1 deletion cmd-pipe-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)

if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
return (CMD_RETURN_ERROR);
c = cmd_find_client(ctx, NULL);
c = cmd_find_client(ctx, NULL, 1);

/* Destroy the old pipe. */
old_fd = wp->pipe_fd;
Expand Down
2 changes: 1 addition & 1 deletion cmd-refresh-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cmd_refresh_client_exec(struct cmd *self, struct cmd_ctx *ctx)
const char *size;
u_int w, h;

if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (args_has(args, 'C')) {
Expand Down
2 changes: 1 addition & 1 deletion cmd-show-messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ cmd_show_messages_exec(struct cmd *self, struct cmd_ctx *ctx)
char *tim;
u_int i;

if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
Expand Down
19 changes: 14 additions & 5 deletions cmd-split-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct window *w;
struct window_pane *wp, *new_wp = NULL;
struct environ env;
const char *cmd, *cwd, *shell;
char *cause, *new_cause;
const char *cmd, *cwd, *shell, *prefix;
char *cause, *new_cause, *cmd1;
u_int hlimit;
int size, percentage;
enum layout_type type;
Expand Down Expand Up @@ -122,9 +122,18 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
goto error;
}
new_wp = window_add_pane(w, hlimit);
if (window_pane_spawn(
new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0)

if (*cmd != '\0') {
prefix = options_get_string(&w->options, "command-prefix");
xasprintf(&cmd1, "%s%s", prefix, cmd);
} else
cmd1 = xstrdup("");
if (window_pane_spawn(new_wp, cmd1, shell, cwd, &env, s->tio,
&cause) != 0) {
free(cmd1);
goto error;
}
free(cmd1);
layout_assign_pane(lc, new_wp);

server_redraw_window(w);
Expand All @@ -143,7 +152,7 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
template = SPLIT_WINDOW_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)
if ((c = cmd_find_client(ctx, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
Expand Down
2 changes: 1 addition & 1 deletion cmd-suspend-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cmd_suspend_client_exec(struct cmd *self, struct cmd_ctx *ctx)
struct args *args = self->args;
struct client *c;

if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

tty_stop_tty(&c->tty);
Expand Down
2 changes: 1 addition & 1 deletion cmd-switch-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
struct client *c;
struct session *s;

if ((c = cmd_find_client(ctx, args_get(args, 'c'))) == NULL)
if ((c = cmd_find_client(ctx, args_get(args, 'c'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (args_has(args, 'r')) {
Expand Down
12 changes: 8 additions & 4 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,19 @@ cmd_choose_client(struct clients *cc)

/* Find the target client or report an error and return NULL. */
struct client *
cmd_find_client(struct cmd_ctx *ctx, const char *arg)
cmd_find_client(struct cmd_ctx *ctx, const char *arg, int quiet)
{
struct client *c;
char *tmparg;
size_t arglen;

/* A NULL argument means the current client. */
if (arg == NULL)
return (cmd_current_client(ctx));
if (arg == NULL) {
c = cmd_current_client(ctx);
if (c == NULL && !quiet)
ctx->error(ctx, "no clients");
return (c);
}
tmparg = xstrdup(arg);

/* Trim a single trailing colon if any. */
Expand All @@ -537,7 +541,7 @@ cmd_find_client(struct cmd_ctx *ctx, const char *arg)
c = cmd_lookup_client(tmparg);

/* If no client found, report an error. */
if (c == NULL)
if (c == NULL && !quiet)
ctx->error(ctx, "client not found: %s", tmparg);

free(tmparg);
Expand Down

0 comments on commit bb8457b

Please sign in to comment.