2011-05-29 17:13:36 +02:00
|
|
|
/* $Id$ */
|
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
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2009-07-09 20:14:18 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2011-10-31 14:55:10 +01:00
|
|
|
* Executes a tmux command if a shell command returns true or false.
|
2009-07-09 20:14:18 +02:00
|
|
|
*/
|
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
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",
|
2011-10-31 14:55:10 +01:00
|
|
|
"", 2, 3,
|
|
|
|
"shell-command command [command]",
|
2011-01-07 15:45:34 +01:00
|
|
|
0,
|
|
|
|
NULL,
|
2011-10-31 14:55:10 +01:00
|
|
|
NULL,
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_if_shell_exec
|
2009-07-09 20:14:18 +02:00
|
|
|
};
|
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_if_shell_data {
|
2011-10-23 17:00:41 +02:00
|
|
|
char *cmd_if;
|
|
|
|
char *cmd_else;
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_ctx ctx;
|
|
|
|
};
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
enum cmd_retval
|
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
|
|
|
{
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args = self->args;
|
2009-10-12 02:29:03 +02:00
|
|
|
struct cmd_if_shell_data *cdata;
|
2011-02-15 16:20:03 +01:00
|
|
|
const char *shellcmd = args->argv[0];
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2011-10-23 17:00:41 +02:00
|
|
|
cdata->cmd_if = xstrdup(args->argv[1]);
|
2011-10-31 14:55:10 +01:00
|
|
|
if (args->argc == 3)
|
|
|
|
cdata->cmd_else = xstrdup(args->argv[2]);
|
2011-10-23 17:00:41 +02:00
|
|
|
else
|
|
|
|
cdata->cmd_else = NULL;
|
2009-10-12 02:29:03 +02:00
|
|
|
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
|
|
|
|
2011-02-15 16:20:03 +01:00
|
|
|
job_run(shellcmd, cmd_if_shell_callback, cmd_if_shell_free, cdata);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
return (CMD_RETURN_YIELD); /* 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;
|
2011-10-31 14:55:10 +01:00
|
|
|
char *cause, *cmd;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2011-10-23 17:00:41 +02:00
|
|
|
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) {
|
2009-07-09 20:14:18 +02:00
|
|
|
if (cause != NULL) {
|
|
|
|
ctx->error(ctx, "%s", cause);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cause);
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|
2009-10-12 02:29:03 +02:00
|
|
|
return;
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 22:12:07 +02:00
|
|
|
cmd_list_exec(cmdlist, ctx);
|
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;
|
2010-07-17 16:36:41 +02:00
|
|
|
struct msg_exit_data exitdata;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
if (ctx->cmdclient != NULL) {
|
|
|
|
ctx->cmdclient->references--;
|
2010-07-17 16:36:41 +02:00
|
|
|
exitdata.retcode = ctx->cmdclient->retcode;
|
2010-08-09 23:44:25 +02:00
|
|
|
ctx->cmdclient->flags |= CLIENT_EXIT;
|
2009-10-12 02:29:03 +02:00
|
|
|
}
|
|
|
|
if (ctx->curclient != NULL)
|
|
|
|
ctx->curclient->references--;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
|
|
|
free(cdata);
|
2009-07-09 20:14:18 +02:00
|
|
|
}
|