diff options
author | Nicholas Marriott <nicm@openbsd.org> | 2011-10-18 08:57:01 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@openbsd.org> | 2011-10-18 08:57:01 +0000 |
commit | 6821ccc8824588652db3d5805cf2e2fd143e8d42 (patch) | |
tree | df85e1de237f35151ec4ad836186b8d422997873 /cmd-if-shell.c | |
parent | 49ef0b4af31ed67a38d2e70e6ac60d48b0730519 (diff) | |
download | rtmux-6821ccc8824588652db3d5805cf2e2fd143e8d42.tar.gz rtmux-6821ccc8824588652db3d5805cf2e2fd143e8d42.tar.bz2 rtmux-6821ccc8824588652db3d5805cf2e2fd143e8d42.zip |
Add an else clause for if-shell, from "arno-" on SourceForge.
Diffstat (limited to 'cmd-if-shell.c')
-rw-r--r-- | cmd-if-shell.c | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/cmd-if-shell.c b/cmd-if-shell.c index f2b015d2..9bd86f01 100644 --- a/cmd-if-shell.c +++ b/cmd-if-shell.c @@ -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); } |