Replace the various identical error callbacks with a single one in cmd-queue.c.

This commit is contained in:
nicm 2019-05-20 11:46:06 +00:00
parent 8db89f8efb
commit e128c7fcd8
8 changed files with 47 additions and 89 deletions

View File

@ -129,17 +129,6 @@ cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_command_prompt_error(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
static int
cmd_command_prompt_callback(struct client *c, void *data, const char *s,
int done)
@ -177,11 +166,11 @@ cmd_command_prompt_callback(struct client *c, void *data, const char *s,
cmdlist = cmd_string_parse(new_template, NULL, 0, &cause);
if (cmdlist == NULL) {
if (cause != NULL) {
new_item = cmdq_get_callback(cmd_command_prompt_error,
cause);
} else
if (cause != NULL)
new_item = cmdq_get_error(cause);
else
new_item = NULL;
free(cause);
} else {
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
cmd_list_free(cmdlist);

View File

@ -82,17 +82,6 @@ cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_confirm_before_error(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
static int
cmd_confirm_before_callback(struct client *c, void *data, const char *s,
__unused int done)
@ -112,11 +101,11 @@ cmd_confirm_before_callback(struct client *c, void *data, const char *s,
cmdlist = cmd_string_parse(cdata->cmd, NULL, 0, &cause);
if (cmdlist == NULL) {
if (cause != NULL) {
new_item = cmdq_get_callback(cmd_confirm_before_error,
cause);
} else
if (cause != NULL)
new_item = cmdq_get_error(cause);
else
new_item = NULL;
free(cause);
} else {
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
cmd_list_free(cmdlist);

View File

@ -182,17 +182,6 @@ cmd_display_panes_draw(struct client *c, struct screen_redraw_ctx *ctx)
}
}
static enum cmd_retval
cmd_display_panes_error(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
static void
cmd_display_panes_free(struct client *c)
{
@ -226,11 +215,13 @@ cmd_display_panes_key(struct client *c, struct key_event *event)
cmd = cmd_template_replace(cdata->command, expanded, 1);
cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
if (cmdlist == NULL && cause != NULL)
new_item = cmdq_get_callback(cmd_display_panes_error, cause);
else if (cmdlist == NULL)
new_item = NULL;
else {
if (cmdlist == NULL) {
if (cause != NULL)
new_item = cmdq_get_error(cause);
else
new_item = NULL;
free(cause);
} else {
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
cmd_list_free(cmdlist);
}

View File

@ -329,6 +329,25 @@ cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
return (item);
}
/* Generic error callback. */
static enum cmd_retval
cmdq_error_callback(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
/* Get an error callback for the command queue. */
struct cmdq_item *
cmdq_get_error(const char *error)
{
return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
}
/* Fire callback on callback queue. */
static enum cmd_retval
cmdq_fire_callback(struct cmdq_item *item)

25
menu.c
View File

@ -191,17 +191,6 @@ menu_free_cb(struct client *c)
free(md);
}
static enum cmd_retval
menu_error_cb(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
static int
menu_key_cb(struct client *c, struct key_event *event)
{
@ -284,12 +273,14 @@ chosen:
return (1);
}
cmdlist = cmd_string_parse(item->command, NULL, 0, &cause);
if (cmdlist == NULL && cause != NULL)
new_item = cmdq_get_callback(menu_error_cb, cause);
else if (cmdlist == NULL)
new_item = NULL;
else {
new_item = cmdq_get_command(cmdlist, &md->fs, NULL, 0);
if (cmdlist == NULL) {
if (cause != NULL)
new_item = cmdq_get_error(cause);
else
new_item = NULL;
free(cause);
} else {
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
cmd_list_free(cmdlist);
}
if (new_item != NULL) {

View File

@ -1768,18 +1768,6 @@ server_client_command_done(struct cmdq_item *item, __unused void *data)
return (CMD_RETURN_NORMAL);
}
/* Show an error message. */
static enum cmd_retval
server_client_command_error(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
/* Handle command message. */
static void
server_client_dispatch_command(struct client *c, struct imsg *imsg)
@ -1827,7 +1815,8 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
return;
error:
cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
cmdq_append(c, cmdq_get_error(cause));
free(cause);
if (cmdlist != NULL)
cmd_list_free(cmdlist);

View File

@ -144,18 +144,6 @@ fail:
return (-1);
}
/* Server error callback. */
static enum cmd_retval
server_start_error(struct cmdq_item *item, void *data)
{
char *error = data;
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_NORMAL);
}
/* Fork new server. */
int
server_start(struct tmuxproc *client, struct event_base *base, int lockfd,
@ -217,7 +205,8 @@ server_start(struct tmuxproc *client, struct event_base *base, int lockfd,
}
if (cause != NULL) {
cmdq_append(c, cmdq_get_callback(server_start_error, cause));
cmdq_append(c, cmdq_get_error(cause));
free(cause);
c->flags |= CLIENT_EXIT;
}

1
tmux.h
View File

@ -1983,6 +1983,7 @@ struct cmdq_item *cmdq_get_command(struct cmd_list *, struct cmd_find_state *,
struct mouse_event *, int);
#define cmdq_get_callback(cb, data) cmdq_get_callback1(#cb, cb, data)
struct cmdq_item *cmdq_get_callback1(const char *, cmdq_cb, void *);
struct cmdq_item *cmdq_get_error(const char *);
void cmdq_insert_after(struct cmdq_item *, struct cmdq_item *);
void cmdq_append(struct client *, struct cmdq_item *);
void cmdq_insert_hook(struct session *, struct cmdq_item *,