2009-07-09 15:47:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-11 09:10:57 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-07-09 15:47:49 +00: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-11 09:10:57 +00:00
|
|
|
#include <sys/wait.h>
|
2009-07-09 15:47:49 +00: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-11 09:10:57 +00:00
|
|
|
void cmd_if_shell_callback(struct job *);
|
|
|
|
void cmd_if_shell_free(void *);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_if_shell_entry = {
|
|
|
|
"if-shell", "if",
|
2011-01-04 00:42:46 +00:00
|
|
|
"", 2, 2,
|
2009-07-09 15:47:49 +00:00
|
|
|
"shell-command command",
|
2011-01-04 00:42:46 +00:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
cmd_if_shell_exec
|
2009-07-09 15:47:49 +00:00
|
|
|
};
|
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
struct cmd_if_shell_data {
|
|
|
|
char *cmd;
|
|
|
|
struct cmd_ctx ctx;
|
|
|
|
};
|
2009-07-09 15:47:49 +00:00
|
|
|
|
|
|
|
int
|
2009-10-11 09:10:57 +00:00
|
|
|
cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
|
2009-07-09 15:47:49 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2009-10-11 09:10:57 +00:00
|
|
|
struct cmd_if_shell_data *cdata;
|
|
|
|
struct job *job;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2011-01-04 00:42:46 +00:00
|
|
|
cdata->cmd = xstrdup(args->argv[1]);
|
2009-10-11 09:10:57 +00:00
|
|
|
memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
if (ctx->cmdclient != NULL)
|
|
|
|
ctx->cmdclient->references++;
|
|
|
|
if (ctx->curclient != NULL)
|
|
|
|
ctx->curclient->references++;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-11-01 23:20:37 +00:00
|
|
|
job = job_add(NULL, 0, NULL,
|
2011-01-04 00:42:46 +00:00
|
|
|
args->argv[0], cmd_if_shell_callback, cmd_if_shell_free, cdata);
|
2009-10-11 09:10:57 +00:00
|
|
|
job_run(job);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
return (1); /* don't let client exit */
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
void
|
|
|
|
cmd_if_shell_callback(struct job *job)
|
2009-07-09 15:47:49 +00:00
|
|
|
{
|
2009-10-11 09:10:57 +00:00
|
|
|
struct cmd_if_shell_data *cdata = job->data;
|
|
|
|
struct cmd_ctx *ctx = &cdata->ctx;
|
2009-07-09 15:47:49 +00:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
char *cause;
|
|
|
|
|
2009-11-01 23:20:37 +00:00
|
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
2009-10-11 09:10:57 +00:00
|
|
|
return;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
|
2009-07-09 15:47:49 +00:00
|
|
|
if (cause != NULL) {
|
|
|
|
ctx->error(ctx, "%s", cause);
|
|
|
|
xfree(cause);
|
|
|
|
}
|
2009-10-11 09:10:57 +00:00
|
|
|
return;
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd_list_exec(cmdlist, ctx) < 0) {
|
|
|
|
cmd_list_free(cmdlist);
|
2009-10-11 09:10:57 +00:00
|
|
|
return;
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-11 09:10:57 +00:00
|
|
|
cmd_if_shell_free(void *data)
|
2009-07-09 15:47:49 +00:00
|
|
|
{
|
2009-10-11 09:10:57 +00:00
|
|
|
struct cmd_if_shell_data *cdata = data;
|
|
|
|
struct cmd_ctx *ctx = &cdata->ctx;
|
2010-07-11 17:06:45 +00:00
|
|
|
struct msg_exit_data exitdata;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
if (ctx->cmdclient != NULL) {
|
|
|
|
ctx->cmdclient->references--;
|
2010-07-11 17:06:45 +00:00
|
|
|
exitdata.retcode = ctx->cmdclient->retcode;
|
2010-07-24 20:11:59 +00:00
|
|
|
ctx->cmdclient->flags |= CLIENT_EXIT;
|
2009-10-11 09:10:57 +00:00
|
|
|
}
|
|
|
|
if (ctx->curclient != NULL)
|
|
|
|
ctx->curclient->references--;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2009-10-11 09:10:57 +00:00
|
|
|
xfree(cdata->cmd);
|
|
|
|
xfree(cdata);
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|