diff options
author | Nicholas Marriott <nicm@openbsd.org> | 2012-07-11 07:10:15 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@openbsd.org> | 2012-07-11 07:10:15 +0000 |
commit | ede8312d59c5d08990f83f38682c26434823525b (patch) | |
tree | bdf66e80ca1d71548a554804f4ab6656a828a821 | |
parent | df912e3540968a2a0b266e523ecc08bb2dc0ca20 (diff) |
Make command exec functions return an enum rather than -1/0/1 values and
add a new value to mean "leave client running but don't attach" to fix
problems with using some commands in a command sequence. Most of the
work by Thomas Adam, problem reported by "jspenguin" on SF bug 3535531.
75 files changed, 489 insertions, 464 deletions
@@ -81,7 +81,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) size_t len; struct cmd_list *cmdlist; struct cmd_ctx ctx; - int retval; + enum cmd_retval retval; if ((f = fopen(path, "rb")) == NULL) { cfg_add_cause(causes, "%s: %s", path, strerror(errno)); @@ -90,7 +90,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) n = 0; line = NULL; - retval = 0; + retval = CMD_RETURN_NORMAL; while ((buf = fgetln(f, &len))) { if (buf[len - 1] == '\n') len--; @@ -145,8 +145,18 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) ctx.info = cfg_print; cfg_cause = NULL; - if (cmd_list_exec(cmdlist, &ctx) == 1) - retval = 1; + switch (cmd_list_exec(cmdlist, &ctx)) { + case CMD_RETURN_YIELD: + if (retval != CMD_RETURN_ATTACH) + retval = CMD_RETURN_YIELD; + break; + case CMD_RETURN_ATTACH: + retval = CMD_RETURN_ATTACH; + break; + case CMD_RETURN_ERROR: + case CMD_RETURN_NORMAL: + break; + } cmd_list_free(cmdlist); if (cfg_cause != NULL) { cfg_add_cause( diff --git a/cmd-attach-session.c b/cmd-attach-session.c index 42ef89ce..30451389 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -26,7 +26,7 @@ * Attach existing session to the current terminal. */ -int cmd_attach_session_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_attach_session_entry = { "attach-session", "attach", @@ -38,7 +38,7 @@ const struct cmd_entry cmd_attach_session_entry = { cmd_attach_session_exec }; -int +enum cmd_retval cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -50,14 +50,14 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) if (RB_EMPTY(&sessions)) { ctx->error(ctx, "no sessions"); - return (-1); + return (CMD_RETURN_ERROR); } if ((s = cmd_find_session(ctx, args_get(args, 't'), 1)) == NULL) - return (-1); + return (CMD_RETURN_ERROR); if (ctx->cmdclient == NULL && ctx->curclient == NULL) - return (0); + return (CMD_RETURN_NORMAL); if (ctx->cmdclient == NULL) { if (args_has(self->args, 'd')) { @@ -84,7 +84,7 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) if (server_client_open(ctx->cmdclient, s, &cause) != 0) { ctx->error(ctx, "open terminal failed: %s", cause); free(cause); - return (-1); + return (CMD_RETURN_ERROR); } if (args_has(self->args, 'r')) @@ -107,5 +107,5 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) recalculate_sizes(); server_update_socket(); - return (1); /* 1 means don't tell command client to exit */ + return (CMD_RETURN_ATTACH); } diff --git a/cmd-bind-key.c b/cmd-bind-key.c index 980e7ec9..909f4839 100644 --- a/cmd-bind-key.c +++ b/cmd-bind-key.c @@ -27,10 +27,10 @@ * Bind a key to a command, this recurses through cmd_*. */ -int cmd_bind_key_check(struct args *); -int cmd_bind_key_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_bind_key_check(struct args *); +enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmd_ctx *); -int cmd_bind_key_table(struct cmd *, struct cmd_ctx *, int); +enum cmd_retval cmd_bind_key_table(struct cmd *, struct cmd_ctx *, int); const struct cmd_entry cmd_bind_key_entry = { "bind-key", "bind", @@ -42,20 +42,20 @@ const struct cmd_entry cmd_bind_key_entry = { cmd_bind_key_exec }; -int +enum cmd_retval cmd_bind_key_check(struct args *args) { if (args_has(args, 't')) { if (args->argc != 2) - return (-1); + return (CMD_RETURN_ERROR); } else { if (args->argc < 2) - return (-1); + return (CMD_RETURN_ERROR); } - return (0); + return (CMD_RETURN_NORMAL); } -int +enum cmd_retval cmd_bind_key_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -66,7 +66,7 @@ cmd_bind_key_exec(struct cmd *self, struct cmd_ctx *ctx) key = key_string_lookup_string(args->argv[0]); if (key == KEYC_NONE) { ctx->error(ctx, "unknown key: %s", args->argv[0]); - return (-1); + return (CMD_RETURN_ERROR); } if (args_has(args, 't')) @@ -76,16 +76,16 @@ cmd_bind_key_exec(struct cmd *self, struct cmd_ctx *ctx) if (cmdlist == NULL) { ctx->error(ctx, "%s", cause); free(cause); - return (-1); + return (CMD_RETURN_ERROR); } if (!args_has(args, 'n')) key |= KEYC_PREFIX; key_bindings_add(key, args_has(args, 'r'), cmdlist); - return (0); + return (CMD_RETURN_NORMAL); } -int +enum cmd_retval cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key) { struct args *args = self->args; @@ -97,25 +97,25 @@ cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key) tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { ctx->error(ctx, "unknown key table: %s", tablename); - return (-1); + return (CMD_RETURN_ERROR); } cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); if (cmd == MODEKEY_NONE) { ctx->error(ctx, "unknown command: %s", args->argv[1]); - return (-1); + return (CMD_RETURN_ERROR); } mtmp.key = key; mtmp.mode = !!args_has(args, 'c'); if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { mbind->cmd = cmd; - return (0); + return (CMD_RETURN_NORMAL); } mbind = xmalloc(sizeof *mbind); mbind->key = mtmp.key; mbind->mode = mtmp.mode; mbind->cmd = cmd; RB_INSERT(mode_key_tree, mtab->tree, mbind); - return (0); + return (CMD_RETURN_NORMAL); } diff --git a/cmd-break-pane.c b/cmd-break-pane.c index bcb6e6b9..5ddb8362 100644 --- a/cmd-break-pane.c +++ b/cmd-break-pane.c @@ -26,7 +26,7 @@ * Break pane off into a window. */ -int cmd_break_pane_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_break_pane_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_break_pane_entry = { "break-pane", "breakp", @@ -38,7 +38,7 @@ const struct cmd_entry cmd_break_pane_entry = { cmd_break_pane_exec }; -int +enum cmd_retval cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -55,11 +55,11 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) char *cp; if ((wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp)) == NULL) - return (-1); + return (CMD_RETURN_ERROR); if (window_count_panes(wl->window) == 1) { ctx->error(ctx, "can't break with only one pane"); - return (-1); + return (CMD_RETURN_ERROR); } w = wl->window; @@ -110,5 +110,5 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) format_free(ft); } - return (0); + return (CMD_RETURN_NORMAL); } diff --git a/cmd-capture-pane.c b/cmd-capture-pane.c index 236a51ca..f38fcb76 100644 --- a/cmd-capture-pane.c +++ b/cmd-capture-pane.c @@ -27,7 +27,7 @@ * Write the entire contents of a pane to a buffer. */ -int cmd_capture_pane_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_capture_pane_entry = { "capture-pane", "capturep", @@ -39,7 +39,7 @@ const struct cmd_entry cmd_capture_pane_entry = { cmd_capture_pane_exec }; -int +enum cmd_retval cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -52,7 +52,7 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx) size_t len, linelen; if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL) - return (-1); + return (CMD_RETURN_ERROR); s = &wp->base; gd = s->grid; @@ -103,7 +103,7 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx) if (!args_has(args, 'b')) { paste_add(&global_buffers, buf, len, limit); - return (0); + return (CMD_RETURN_NORMAL); } buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); @@ -111,14 +111,14 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx) ctx->error(ctx, "buffer %s", cause); free(buf); free(cause); - return (-1); + return (CMD_RETURN_ERROR); } if (paste_replace(&global_buffers, buffer, buf, len) != 0) { ctx->error(ctx, "no buffer %d", buffer); free(buf); - return (-1); + return (CMD_RETURN_ERROR); } - return (0); + return (CMD_RETURN_NORMAL); } diff --git a/cmd-choose-buffer.c b/cmd-choose-buffer.c index 6c62cfa3..3deedfa9 100644 --- a/cmd-choose-buffer.c +++ b/cmd-choose-buffer.c @@ -27,7 +27,7 @@ * Enter choice mode to choose a buffer. */ -int cmd_choose_buffer_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_choose_buffer_exec(struct cmd *, struct cmd_ctx *); void cmd_choose_buffer_callback(struct window_choose_data *); void cmd_choose_buffer_free(struct window_choose_data *); @@ -42,7 +42,7 @@ const struct cmd_entry cmd_choose_buffer_entry = { cmd_choose_buffer_exec }; -int +enum cmd_retval cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -55,20 +55,20 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) if (ctx->curclient == NULL) { ctx->error(ctx, "must be run interactively"); - return (-1); + return (CMD_RETURN_ERROR); } if ((template = args_get(args, 'F')) == NULL) template = DEFAULT_BUFFER_LIST_TEMPLATE; if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL) - return (-1); + return (CMD_RETURN_ERROR); if (paste_get_top(&global_buffers) == NULL) - return (0); + return (CMD_RETURN_NORMAL); if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) - return (0); + return (CMD_RETURN_NORMAL); if (args->argc != 0) action = xstrdup(args->argv[0]); @@ -96,7 +96,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) window_choose_ready(wl->window->active, 0, cmd_choose_buffer_callback, cmd_choose_buffer_free); - return (0); + return (CMD_RETURN_NORMAL); } void diff --git a/cmd-choose-client.c b/cmd-choose-client.c index a20fec42..4059cbba 100644 --- a/cmd-choose-client.c +++ b/cmd-choose-client.c @@ -27,7 +27,7 @@ * Enter choice mode to choose a client. */ -int cmd_choose_client_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_choose_client_exec(struct cmd *, struct cmd_ctx *); void cmd_choose_client_callback(struct window_choose_data *); void cmd_choose_client_free(struct window_choose_data *); @@ -47,7 +47,7 @@ struct cmd_choose_client_data { char *template; }; -int +enum cmd_retval cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -60,14 +60,14 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx) if (ctx->curclient == NULL) { ctx->error(ctx, "must be run interactively"); - return (-1); + return (CMD_RETURN_ERROR); } if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL) - return (-1); + return (CMD_RETURN_ERROR); if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) - return (0); + return (CMD_RETURN_NORMAL); if ((template = args_get(args, 'F')) == NULL) template = DEFAULT_CLIENT_TEMPLATE; @@ -104,7 +104,7 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx) window_choose_ready(wl->window->active, cur, cmd_choose_client_callback, cmd_choose_client_free); - return (0); + return (CMD_RETURN_NORMAL); } void diff --git a/cmd-choose-tree.c b/cmd-choose-tree.c index 0389bea3..166684f8 100644 --- a/cmd-choose-tree.c +++ b/cmd-choose-tree.c @@ -70,7 +70,7 @@ const struct cmd_entry cmd_choose_window_entry = { cmd_choose_tree_exec }; -int +enum cmd_retval cmd_choose_tree_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -89,17 +89,17 @@ cmd_choose_tree_exec(struct cmd *self, struct cmd_ctx *ctx) if (ctx->curclient == NULL) { ctx->error(ctx, "must be run interactively"); - return (-1); + return (CMD_RETURN_ERROR); } s = ctx->curclient->session; tty = &ctx->curclient->tty; if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL) - return (-1); + return (CMD_RETURN_ERROR); if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) - return (0); + return (CMD_RETURN_NORMAL); /* Sort out which command this is. */ wflag = sflag = 0; @@ -221,7 +221,7 @@ windows_only: window_choose_ready(wl->window->active, cur_win, cmd_choose_tree_callback, cmd_choose_tree_free); - return (0); + return (CMD_RETURN_NORMAL); } void @@ -248,4 +248,3 @@ cmd_choose_tree_free(struct window_choose_data *cdata) free(cdata); } - diff --git a/cmd-clear-history.c b/cmd-clear-history.c index cf3da229..8a88e46c 100644 --- a/cmd-clear-history.c +++ b/cmd-clear-history.c @@ -24,7 +24,7 @@ * Clear pane history. */ -int cmd_clear_history_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_clear_history_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_clear_history_entry = { "clear-history", "clearhist", @@ -36,7 +36,7 @@ const struct cmd_entry cmd_clear_history_entry = { cmd_clear_history_exec }; -int +enum cmd_retval cmd_clear_history_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -44,11 +44,11 @@ cmd_clear_history_exec(struct cmd *self, struct cmd_ctx *ctx) struct grid *gd; if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL) - return (-1); + return (CMD_RETURN_ERROR); gd = wp->base.grid; grid_move_lines(gd, 0, gd->hsize, gd->sy); gd->hsize = 0; - return (0); + return (CMD_RETURN_NORMAL); } diff --git a/cmd-clock-mode.c b/cmd-clock-mode.c index 0dc151d1..f1d9eceb 100644 --- a/cmd-clock-mode.c +++ b/cmd-clock-mode.c @@ -24,7 +24,7 @@ * Enter clock mode. */ -int cmd_clock_mode_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_clock_mode_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_clock_mode_entry = { "clock-mode", NULL, @@ -36,16 +36,16 @@ const struct cmd_entry cmd_clock_mode_entry = { cmd_clock_mode_exec }; -int +enum cmd_retval cmd_clock_mode_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct window_pane *wp; if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL) - return (-1); + return (CMD_RETURN_ERROR); window_pane_set_mode(wp, &window_clock_mode); - return (0); + return (CMD_RETURN_NORMAL); } diff --git a/cmd-command-prompt.c b/cmd-command-prompt.c index 3105744f..6c3df4ef 100644 --- a/cmd-command-prompt.c +++ b/cmd-command-prompt.c @@ -31,7 +31,7 @@ void cmd_command_prompt_key_binding(struct cmd *, int); int cmd_command_prompt_check(struct args *); -int cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *); +enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *); int cmd_command_prompt_callback(void *, const char *); void cmd_command_prompt_free(void *); @@ -84,7 +84,7 @@ cmd_command_prompt_key_binding(struct cmd *self, int key) } } -int +enum cmd_retval cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -95,10 +95,10 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx) size_t n; if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL) - return (-1); + return (CMD_RETURN_ERROR); if (c->prompt_string != NULL) - retur |