Add client formats, from Ben Boeckel.
[tmux-openbsd.git] / cmd-if-shell.c
blob9bd86f01f06d1953bbf4ecb905579d5d74c177e8
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
21 #include <sys/wait.h>
23 #include <string.h>
25 #include "tmux.h"
28 * Executes a tmux command if a shell command returns true.
31 int cmd_if_shell_check(struct args *);
32 int cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
34 void cmd_if_shell_callback(struct job *);
35 void cmd_if_shell_free(void *);
37 const struct cmd_entry cmd_if_shell_entry = {
38 "if-shell", "if",
39 "", 2, 4,
40 "shell-command command [else command]",
42 NULL,
43 cmd_if_shell_check,
44 cmd_if_shell_exec
47 struct cmd_if_shell_data {
48 char *cmd_if;
49 char *cmd_else;
50 struct cmd_ctx ctx;
53 int
54 cmd_if_shell_check(struct args *args)
56 if (args->argc == 3)
57 return (-1);
58 if (args->argc == 4 && strcmp(args->argv[2], "else") != 0)
59 return (-1);
60 return (0);
63 int
64 cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
66 struct args *args = self->args;
67 struct cmd_if_shell_data *cdata;
68 const char *shellcmd = args->argv[0];
70 cdata = xmalloc(sizeof *cdata);
71 cdata->cmd_if = xstrdup(args->argv[1]);
72 if (args->argc == 4)
73 cdata->cmd_else = xstrdup(args->argv[3]);
74 else
75 cdata->cmd_else = NULL;
76 memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
78 if (ctx->cmdclient != NULL)
79 ctx->cmdclient->references++;
80 if (ctx->curclient != NULL)
81 ctx->curclient->references++;
83 job_run(shellcmd, cmd_if_shell_callback, cmd_if_shell_free, cdata);
85 return (1); /* don't let client exit */
88 void
89 cmd_if_shell_callback(struct job *job)
91 struct cmd_if_shell_data *cdata = job->data;
92 struct cmd_ctx *ctx = &cdata->ctx;
93 struct cmd_list *cmdlist;
94 char *cmd;
95 char *cause;
97 if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) {
98 cmd = cdata->cmd_else;
99 if (cmd == NULL)
100 return;
101 } else
102 cmd = cdata->cmd_if;
103 if (cmd_string_parse(cmd, &cmdlist, &cause) != 0) {
104 if (cause != NULL) {
105 ctx->error(ctx, "%s", cause);
106 xfree(cause);
108 return;
111 cmd_list_exec(cmdlist, ctx);
112 cmd_list_free(cmdlist);
115 void
116 cmd_if_shell_free(void *data)
118 struct cmd_if_shell_data *cdata = data;
119 struct cmd_ctx *ctx = &cdata->ctx;
120 struct msg_exit_data exitdata;
122 if (ctx->cmdclient != NULL) {
123 ctx->cmdclient->references--;
124 exitdata.retcode = ctx->cmdclient->retcode;
125 ctx->cmdclient->flags |= CLIENT_EXIT;
127 if (ctx->curclient != NULL)
128 ctx->curclient->references--;
130 if (cdata->cmd_else != NULL)
131 xfree(cdata->cmd_else);
132 xfree(cdata->cmd_if);
133 xfree(cdata);