diff options
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/cmd-if-shell.c | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/usr.bin/tmux/cmd-if-shell.c b/usr.bin/tmux/cmd-if-shell.c index fbae0302a2b..45a5f9d8cc9 100644 --- a/usr.bin/tmux/cmd-if-shell.c +++ b/usr.bin/tmux/cmd-if-shell.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-if-shell.c,v 1.12 2011/05/25 17:50:52 nicm Exp $ */ +/* $OpenBSD: cmd-if-shell.c,v 1.13 2011/10/18 08:57:01 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> @@ -28,6 +28,7 @@ * Executes a tmux command if a shell command returns true. */ +int cmd_if_shell_check(struct args *); int cmd_if_shell_exec(struct cmd *, struct cmd_ctx *); void cmd_if_shell_callback(struct job *); @@ -35,20 +36,31 @@ void cmd_if_shell_free(void *); const struct cmd_entry cmd_if_shell_entry = { "if-shell", "if", - "", 2, 2, - "shell-command command", + "", 2, 4, + "shell-command command [else command]", 0, NULL, - NULL, + cmd_if_shell_check, cmd_if_shell_exec }; struct cmd_if_shell_data { - char *cmd; + char *cmd_if; + char *cmd_else; struct cmd_ctx ctx; }; int +cmd_if_shell_check(struct args *args) +{ + if (args->argc == 3) + return (-1); + if (args->argc == 4 && strcmp(args->argv[2], "else") != 0) + return (-1); + return (0); +} + +int cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; @@ -56,7 +68,11 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx) const char *shellcmd = args->argv[0]; cdata = xmalloc(sizeof *cdata); - cdata->cmd = xstrdup(args->argv[1]); + cdata->cmd_if = xstrdup(args->argv[1]); + if (args->argc == 4) + cdata->cmd_else = xstrdup(args->argv[3]); + else + cdata->cmd_else = NULL; memcpy(&cdata->ctx, ctx, sizeof cdata->ctx); if (ctx->cmdclient != NULL) @@ -75,12 +91,16 @@ cmd_if_shell_callback(struct job *job) struct cmd_if_shell_data *cdata = job->data; struct cmd_ctx *ctx = &cdata->ctx; struct cmd_list *cmdlist; + char *cmd; char *cause; - if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) - return; - - if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) { + if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) { + cmd = cdata->cmd_else; + if (cmd == NULL) + return; + } else + cmd = cdata->cmd_if; + if (cmd_string_parse(cmd, &cmdlist, &cause) != 0) { if (cause != NULL) { ctx->error(ctx, "%s", cause); xfree(cause); @@ -107,6 +127,8 @@ cmd_if_shell_free(void *data) if (ctx->curclient != NULL) ctx->curclient->references--; - xfree(cdata->cmd); + if (cdata->cmd_else != NULL) + xfree(cdata->cmd_else); + xfree(cdata->cmd_if); xfree(cdata); } |