2011-05-29 15:13:36 +00:00
|
|
|
/* $Id$ */
|
2009-07-09 18:14:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-12 00:29:03 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-07-09 18:14:18 +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-12 00:29:03 +00:00
|
|
|
#include <sys/wait.h>
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2012-07-11 19:34:16 +00:00
|
|
|
#include <stdlib.h>
|
2009-07-09 18:14:18 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2011-10-31 13:55:10 +00:00
|
|
|
* Executes a tmux command if a shell command returns true or false.
|
2009-07-09 18:14:18 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2009-10-12 00:29:03 +00:00
|
|
|
void cmd_if_shell_callback(struct job *);
|
2013-02-23 22:25:58 +00:00
|
|
|
void cmd_if_shell_done(struct cmd_q *);
|
2009-10-12 00:29:03 +00:00
|
|
|
void cmd_if_shell_free(void *);
|
2009-07-09 18:14:18 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_if_shell_entry = {
|
|
|
|
"if-shell", "if",
|
2013-02-23 22:42:49 +00:00
|
|
|
"bt:", 2, 3,
|
|
|
|
"[-b] " CMD_TARGET_PANE_USAGE " shell-command command [command]",
|
2011-01-07 14:45:34 +00:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
cmd_if_shell_exec
|
2009-07-09 18:14:18 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 00:29:03 +00:00
|
|
|
struct cmd_if_shell_data {
|
2011-10-23 15:00:41 +00:00
|
|
|
char *cmd_if;
|
|
|
|
char *cmd_else;
|
2013-02-23 22:25:58 +00:00
|
|
|
struct cmd_q *cmdq;
|
|
|
|
int bflag;
|
|
|
|
int started;
|
2009-10-12 00:29:03 +00:00
|
|
|
};
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2012-07-11 19:37:32 +00:00
|
|
|
enum cmd_retval
|
2013-02-23 22:25:58 +00:00
|
|
|
cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-07-09 18:14:18 +00:00
|
|
|
{
|
2011-01-07 14:45:34 +00:00
|
|
|
struct args *args = self->args;
|
2009-10-12 00:29:03 +00:00
|
|
|
struct cmd_if_shell_data *cdata;
|
2013-02-23 22:42:49 +00:00
|
|
|
char *shellcmd;
|
2013-03-25 16:04:07 +00:00
|
|
|
struct client *c;
|
2013-03-11 09:43:56 +00:00
|
|
|
struct session *s = NULL;
|
|
|
|
struct winlink *wl = NULL;
|
|
|
|
struct window_pane *wp = NULL;
|
2013-02-23 19:24:42 +00:00
|
|
|
struct format_tree *ft;
|
|
|
|
|
2013-03-11 09:43:56 +00:00
|
|
|
if (args_has(args, 't'))
|
|
|
|
wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
|
2013-03-25 16:04:07 +00:00
|
|
|
else {
|
|
|
|
c = cmd_find_client(cmdq, NULL, 1);
|
|
|
|
if (c != NULL && c->session != NULL) {
|
|
|
|
s = c->session;
|
|
|
|
wl = s->curw;
|
|
|
|
wp = wl->window->active;
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 19:24:42 +00:00
|
|
|
|
|
|
|
ft = format_create();
|
2013-03-11 09:43:56 +00:00
|
|
|
if (s != NULL)
|
|
|
|
format_session(ft, s);
|
|
|
|
if (s != NULL && wl != NULL)
|
|
|
|
format_winlink(ft, s, wl);
|
|
|
|
if (wp != NULL)
|
|
|
|
format_window_pane(ft, wp);
|
2013-02-23 19:24:42 +00:00
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2009-10-12 00:29:03 +00:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2011-10-23 15:00:41 +00:00
|
|
|
cdata->cmd_if = xstrdup(args->argv[1]);
|
2011-10-31 13:55:10 +00:00
|
|
|
if (args->argc == 3)
|
|
|
|
cdata->cmd_else = xstrdup(args->argv[2]);
|
2011-10-23 15:00:41 +00:00
|
|
|
else
|
|
|
|
cdata->cmd_else = NULL;
|
2013-02-23 22:25:58 +00:00
|
|
|
cdata->bflag = args_has(args, 'b');
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
cdata->started = 0;
|
|
|
|
cdata->cmdq = cmdq;
|
|
|
|
cmdq->references++;
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2013-03-21 14:24:33 +00:00
|
|
|
job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata);
|
2013-02-23 19:24:42 +00:00
|
|
|
free(shellcmd);
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
if (cdata->bflag)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-07-09 18:14:18 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 00:29:03 +00:00
|
|
|
void
|
|
|
|
cmd_if_shell_callback(struct job *job)
|
2009-07-09 18:14:18 +00:00
|
|
|
{
|
2009-10-12 00:29:03 +00:00
|
|
|
struct cmd_if_shell_data *cdata = job->data;
|
2013-02-23 22:25:58 +00:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
|
2009-07-09 18:14:18 +00:00
|
|
|
struct cmd_list *cmdlist;
|
2011-10-31 13:55:10 +00:00
|
|
|
char *cause, *cmd;
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
if (cmdq->dead)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
2011-10-23 15:00:41 +00:00
|
|
|
cmd = cdata->cmd_else;
|
2013-02-23 22:25:58 +00:00
|
|
|
else
|
2011-10-23 15:00:41 +00:00
|
|
|
cmd = cdata->cmd_if;
|
2013-02-23 22:25:58 +00:00
|
|
|
if (cmd == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
|
2009-07-09 18:14:18 +00:00
|
|
|
if (cause != NULL) {
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdq_error(cmdq, "%s", cause);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cause);
|
2009-07-09 18:14:18 +00:00
|
|
|
}
|
2009-10-12 00:29:03 +00:00
|
|
|
return;
|
2009-07-09 18:14:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 22:25:58 +00: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 18:14:18 +00:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
}
|
|
|
|
|
2013-02-23 22:25:58 +00: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 18:14:18 +00:00
|
|
|
void
|
2009-10-12 00:29:03 +00:00
|
|
|
cmd_if_shell_free(void *data)
|
2009-07-09 18:14:18 +00:00
|
|
|
{
|
2009-10-12 00:29:03 +00:00
|
|
|
struct cmd_if_shell_data *cdata = data;
|
2013-02-23 22:25:58 +00:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
|
|
|
|
|
|
|
if (cdata->started)
|
|
|
|
return;
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
2009-07-09 18:14:18 +00:00
|
|
|
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
|
|
|
free(cdata);
|
2009-07-09 18:14:18 +00:00
|
|
|
}
|