Skip to content

Commit

Permalink
Make command exec functions return an enum rather than -1/0/1 values and
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicm committed Jul 11, 2012
1 parent df912e3 commit ede8312
Show file tree
Hide file tree
Showing 75 changed files with 489 additions and 464 deletions.
18 changes: 14 additions & 4 deletions cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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--;
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand All @@ -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')) {
Expand All @@ -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'))
Expand All @@ -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);
}
32 changes: 16 additions & 16 deletions cmd-bind-key.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand All @@ -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'))
Expand All @@ -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;
Expand All @@ -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);
}
10 changes: 5 additions & 5 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -110,5 +110,5 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx)

format_free(ft);
}
return (0);
return (CMD_RETURN_NORMAL);
}
14 changes: 7 additions & 7 deletions cmd-capture-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -103,22 +103,22 @@ 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);
if (cause != NULL) {
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);
}
14 changes: 7 additions & 7 deletions cmd-choose-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand All @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions cmd-choose-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit ede8312

Please sign in to comment.