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
|
|
|
*/
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2009-10-12 02:29:03 +02:00
|
|
|
void cmd_if_shell_callback(struct job *);
|
2013-02-23 23:25:58 +01:00
|
|
|
void cmd_if_shell_done(struct cmd_q *);
|
2009-10-12 02:29:03 +02:00
|
|
|
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",
|
2013-02-23 23:42:49 +01:00
|
|
|
"bt:", 2, 3,
|
|
|
|
"[-b] " CMD_TARGET_PANE_USAGE " 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;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cmdq;
|
|
|
|
int bflag;
|
|
|
|
int started;
|
2009-10-12 02:29:03 +02:00
|
|
|
};
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
enum cmd_retval
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
|
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;
|
2013-02-23 23:42:49 +01:00
|
|
|
char *shellcmd;
|
2013-02-23 20:24:42 +01:00
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
|
|
|
struct window_pane *wp;
|
|
|
|
struct format_tree *ft;
|
|
|
|
|
2013-02-23 23:42:49 +01:00
|
|
|
wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
|
2013-02-23 20:24:42 +01:00
|
|
|
if (wl == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
|
|
|
|
ft = format_create();
|
|
|
|
format_session(ft, s);
|
|
|
|
format_winlink(ft, s, wl);
|
|
|
|
format_window_pane(ft, wp);
|
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
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;
|
2013-02-23 23:25:58 +01:00
|
|
|
cdata->bflag = args_has(args, 'b');
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cdata->started = 0;
|
|
|
|
cdata->cmdq = cmdq;
|
|
|
|
cmdq->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);
|
2013-02-23 20:24:42 +01:00
|
|
|
free(shellcmd);
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cdata->bflag)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
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;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
|
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
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cmdq->dead)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
2011-10-23 17:00:41 +02:00
|
|
|
cmd = cdata->cmd_else;
|
2013-02-23 23:25:58 +01:00
|
|
|
else
|
2011-10-23 17:00:41 +02:00
|
|
|
cmd = cdata->cmd_if;
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cmd == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
|
2009-07-09 20:14:18 +02:00
|
|
|
if (cause != NULL) {
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_error(cmdq, "%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
|
|
|
}
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cdata->started = 1;
|
|
|
|
|
|
|
|
cmdq1 = cmdq_new(cmdq->client);
|
|
|
|
cmdq1->emptyfn = cmd_if_shell_done;
|
|
|
|
cmdq1->data = cdata;
|
|
|
|
|
|
|
|
cmdq_run(cmdq1, cmdlist);
|
2009-07-09 20:14:18 +02:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
}
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
void
|
|
|
|
cmd_if_shell_done(struct cmd_q *cmdq1)
|
|
|
|
{
|
|
|
|
struct cmd_if_shell_data *cdata = cmdq1->data;
|
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
|
|
|
|
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
|
|
|
|
|
|
|
cmdq_free(cmdq1);
|
|
|
|
|
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
|
|
|
free(cdata);
|
|
|
|
}
|
|
|
|
|
2009-07-09 20:14:18 +02:00
|
|
|
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;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
|
|
|
|
|
|
|
if (cdata->started)
|
|
|
|
return;
|
2009-07-09 20:14:18 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
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
|
|
|
}
|