2009-11-02 22:38:27 +01:00
|
|
|
/* $Id: cmd-if-shell.c,v 1.7 2009-11-02 21:38:26 tcunha Exp $ */
|
2009-07-09 20:14:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-12 02:29:03 +02:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-07-09 20:14:18 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2009-10-12 02:29:03 +02:00
|
|
|
#include <sys/wait.h>
|
2009-07-09 20:14:18 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Executes a tmux command if a shell command returns true.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
|
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
void cmd_if_shell_callback(struct job *);
|
|
|
|
void cmd_if_shell_free(void *);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_if_shell_entry = {
|
|
|
|
"if-shell", "if",
|
|
|
|
"shell-command command",
|
2009-10-12 02:29:03 +02:00
|
|
|
CMD_ARG2, 0,
|
|
|
|
cmd_target_init,
|
|
|
|
cmd_target_parse,
|
2009-07-09 20:14:18 +02:00
|
|
|
cmd_if_shell_exec,
|
2009-10-12 02:29:03 +02:00
|
|
|
cmd_target_free,
|
|
|
|
cmd_target_print
|
2009-07-09 20:14:18 +02:00
|
|
|
};
|
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_if_shell_data {
|
|
|
|
char *cmd;
|
|
|
|
struct cmd_ctx ctx;
|
|
|
|
};
|
2009-07-09 20:14:18 +02:00
|
|
|
|
|
|
|
int
|
2009-10-12 02:29:03 +02:00
|
|
|
cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
|
2009-07-09 20:14:18 +02:00
|
|
|
{
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_target_data *data = self->data;
|
|
|
|
struct cmd_if_shell_data *cdata;
|
|
|
|
struct job *job;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->cmd = xstrdup(data->arg2);
|
|
|
|
memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
if (ctx->cmdclient != NULL)
|
|
|
|
ctx->cmdclient->references++;
|
|
|
|
if (ctx->curclient != NULL)
|
|
|
|
ctx->curclient->references++;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-11-02 22:38:27 +01:00
|
|
|
job = job_add(NULL, 0, NULL,
|
2009-10-12 02:29:03 +02:00
|
|
|
data->arg, cmd_if_shell_callback, cmd_if_shell_free, cdata);
|
|
|
|
job_run(job);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
return (1); /* don't let client exit */
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
void
|
|
|
|
cmd_if_shell_callback(struct job *job)
|
2009-07-09 20:14:18 +02:00
|
|
|
{
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_if_shell_data *cdata = job->data;
|
|
|
|
struct cmd_ctx *ctx = &cdata->ctx;
|
2009-07-09 20:14:18 +02:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
char *cause;
|
|
|
|
|
2009-11-02 22:38:27 +01:00
|
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
2009-10-12 02:29:03 +02:00
|
|
|
return;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
|
2009-07-09 20:14:18 +02:00
|
|
|
if (cause != NULL) {
|
|
|
|
ctx->error(ctx, "%s", cause);
|
|
|
|
xfree(cause);
|
|
|
|
}
|
2009-10-12 02:29:03 +02:00
|
|
|
return;
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd_list_exec(cmdlist, ctx) < 0) {
|
|
|
|
cmd_list_free(cmdlist);
|
2009-10-12 02:29:03 +02:00
|
|
|
return;
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-12 02:29:03 +02:00
|
|
|
cmd_if_shell_free(void *data)
|
2009-07-09 20:14:18 +02:00
|
|
|
{
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_if_shell_data *cdata = data;
|
|
|
|
struct cmd_ctx *ctx = &cdata->ctx;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
if (ctx->cmdclient != NULL) {
|
|
|
|
ctx->cmdclient->references--;
|
|
|
|
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
|
|
|
|
}
|
|
|
|
if (ctx->curclient != NULL)
|
|
|
|
ctx->curclient->references--;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
xfree(cdata->cmd);
|
|
|
|
xfree(cdata);
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|