This commit was manufactured by cvs2svn to create tag 'TMUX_1_2'.
[tmux.git] / cmd-if-shell.c
blob0fa5ba387dde0bb2d7df8e87a2428041f4e4b2eb
1 /* $Id: cmd-if-shell.c,v 1.8 2009-11-14 17:56:39 tcunha Exp $ */
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_exec(struct cmd *, struct cmd_ctx *);
33 void cmd_if_shell_callback(struct job *);
34 void cmd_if_shell_free(void *);
36 const struct cmd_entry cmd_if_shell_entry = {
37 "if-shell", "if",
38 "shell-command command",
39 CMD_ARG2, "",
40 cmd_target_init,
41 cmd_target_parse,
42 cmd_if_shell_exec,
43 cmd_target_free,
44 cmd_target_print
47 struct cmd_if_shell_data {
48 char *cmd;
49 struct cmd_ctx ctx;
52 int
53 cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
55 struct cmd_target_data *data = self->data;
56 struct cmd_if_shell_data *cdata;
57 struct job *job;
59 cdata = xmalloc(sizeof *cdata);
60 cdata->cmd = xstrdup(data->arg2);
61 memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
63 if (ctx->cmdclient != NULL)
64 ctx->cmdclient->references++;
65 if (ctx->curclient != NULL)
66 ctx->curclient->references++;
68 job = job_add(NULL, 0, NULL,
69 data->arg, cmd_if_shell_callback, cmd_if_shell_free, cdata);
70 job_run(job);
72 return (1); /* don't let client exit */
75 void
76 cmd_if_shell_callback(struct job *job)
78 struct cmd_if_shell_data *cdata = job->data;
79 struct cmd_ctx *ctx = &cdata->ctx;
80 struct cmd_list *cmdlist;
81 char *cause;
83 if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
84 return;
86 if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
87 if (cause != NULL) {
88 ctx->error(ctx, "%s", cause);
89 xfree(cause);
91 return;
94 if (cmd_list_exec(cmdlist, ctx) < 0) {
95 cmd_list_free(cmdlist);
96 return;
99 cmd_list_free(cmdlist);
102 void
103 cmd_if_shell_free(void *data)
105 struct cmd_if_shell_data *cdata = data;
106 struct cmd_ctx *ctx = &cdata->ctx;
108 if (ctx->cmdclient != NULL) {
109 ctx->cmdclient->references--;
110 server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
112 if (ctx->curclient != NULL)
113 ctx->curclient->references--;
115 xfree(cdata->cmd);
116 xfree(cdata);