Sync OpenBSD patchset 963:

Add an else clause for if-shell, from "arno-" on SourceForge.
pull/1/head
Tiago Cunha 2011-10-23 15:00:41 +00:00
parent 96146a8c6f
commit 9b48c77493
1 changed files with 32 additions and 10 deletions

View File

@ -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,19 +36,30 @@ 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)
{
@ -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);
}