2009-07-09 17:47:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-11 11:10:57 +02:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-07-09 17:47:49 +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-11 11:10:57 +02:00
|
|
|
#include <sys/wait.h>
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2012-07-10 13:53:01 +02:00
|
|
|
#include <stdlib.h>
|
2009-07-09 17:47:49 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2011-10-28 00:40:15 +02:00
|
|
|
* Executes a tmux command if a shell command returns true or false.
|
2009-07-09 17:47:49 +02:00
|
|
|
*/
|
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2009-10-11 11:10:57 +02:00
|
|
|
void cmd_if_shell_callback(struct job *);
|
2013-03-24 10:54:10 +01:00
|
|
|
void cmd_if_shell_done(struct cmd_q *);
|
2009-10-11 11:10:57 +02:00
|
|
|
void cmd_if_shell_free(void *);
|
2009-07-09 17:47:49 +02:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_if_shell_entry = {
|
2015-12-13 22:53:57 +01:00
|
|
|
.name = "if-shell",
|
|
|
|
.alias = "if",
|
|
|
|
|
|
|
|
.args = { "bFt:", 2, 3 },
|
|
|
|
.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
|
|
|
|
"[command]",
|
|
|
|
|
2015-12-14 01:31:54 +01:00
|
|
|
.tflag = CMD_PANE_CANFAIL,
|
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 22:53:57 +01:00
|
|
|
.exec = cmd_if_shell_exec
|
2009-07-09 17:47:49 +02:00
|
|
|
};
|
|
|
|
|
2009-10-11 11:10:57 +02:00
|
|
|
struct cmd_if_shell_data {
|
2015-04-21 17:18:06 +02:00
|
|
|
char *cmd_if;
|
|
|
|
char *cmd_else;
|
|
|
|
|
|
|
|
struct cmd_q *cmdq;
|
|
|
|
struct mouse_event mouse;
|
|
|
|
|
|
|
|
int bflag;
|
2015-04-21 23:31:02 +02:00
|
|
|
int references;
|
2009-10-11 11:10:57 +02:00
|
|
|
};
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2012-07-11 09:10:15 +02:00
|
|
|
enum cmd_retval
|
2013-03-24 10:54:10 +01:00
|
|
|
cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-07-09 17:47:49 +02:00
|
|
|
{
|
2011-01-04 01:42:46 +01:00
|
|
|
struct args *args = self->args;
|
2009-10-11 11:10:57 +02:00
|
|
|
struct cmd_if_shell_data *cdata;
|
2014-12-03 00:39:02 +01:00
|
|
|
char *shellcmd, *cmd, *cause;
|
|
|
|
struct cmd_list *cmdlist;
|
2015-12-13 15:32:38 +01:00
|
|
|
struct session *s = cmdq->state.tflag.s;
|
|
|
|
struct winlink *wl = cmdq->state.tflag.wl;
|
|
|
|
struct window_pane *wp = cmdq->state.tflag.wp;
|
2013-03-24 10:33:35 +01:00
|
|
|
struct format_tree *ft;
|
2015-10-31 09:13:58 +01:00
|
|
|
const char *cwd;
|
2013-03-24 10:33:35 +01:00
|
|
|
|
2015-12-13 15:32:38 +01:00
|
|
|
cwd = wp->cwd;
|
2013-03-24 10:33:35 +01:00
|
|
|
|
2015-12-13 15:32:38 +01:00
|
|
|
if (cmdq->client != NULL && cmdq->client->session == NULL)
|
|
|
|
cwd = cmdq->client->cwd;
|
|
|
|
else if (s != NULL)
|
|
|
|
cwd = s->cwd;
|
|
|
|
else
|
|
|
|
cwd = NULL;
|
2015-12-11 13:27:36 +01:00
|
|
|
ft = format_create(cmdq, 0);
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults(ft, NULL, s, wl, wp);
|
2013-03-24 10:33:35 +01:00
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2014-12-03 00:39:02 +01:00
|
|
|
if (args_has(args, 'F')) {
|
|
|
|
cmd = NULL;
|
|
|
|
if (*shellcmd != '0' && *shellcmd != '\0')
|
|
|
|
cmd = args->argv[1];
|
|
|
|
else if (args->argc == 3)
|
|
|
|
cmd = args->argv[2];
|
2015-11-20 23:02:54 +01:00
|
|
|
free(shellcmd);
|
2014-12-03 00:39:02 +01:00
|
|
|
if (cmd == NULL)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
|
|
|
|
if (cause != NULL) {
|
|
|
|
cmdq_error(cmdq, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
}
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2015-04-21 17:18:06 +02:00
|
|
|
cmdq_run(cmdq, cmdlist, &cmdq->item->mouse);
|
2014-12-03 00:39:02 +01:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2009-10-11 11:10:57 +02:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2015-04-21 17:18:06 +02:00
|
|
|
|
2011-10-18 10:57:01 +02:00
|
|
|
cdata->cmd_if = xstrdup(args->argv[1]);
|
2011-10-28 00:40:15 +02:00
|
|
|
if (args->argc == 3)
|
|
|
|
cdata->cmd_else = xstrdup(args->argv[2]);
|
2011-10-18 10:57:01 +02:00
|
|
|
else
|
|
|
|
cdata->cmd_else = NULL;
|
2015-04-21 17:18:06 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cdata->bflag = args_has(args, 'b');
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cdata->cmdq = cmdq;
|
2015-04-21 17:18:06 +02:00
|
|
|
memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse);
|
2013-03-24 10:54:10 +01:00
|
|
|
cmdq->references++;
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2015-04-21 23:31:02 +02:00
|
|
|
cdata->references = 1;
|
2015-04-25 00:19:36 +02:00
|
|
|
job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
|
|
|
|
cdata);
|
2013-03-24 10:33:35 +01:00
|
|
|
free(shellcmd);
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
if (cdata->bflag)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-07-09 17:47:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-11 11:10:57 +02:00
|
|
|
void
|
|
|
|
cmd_if_shell_callback(struct job *job)
|
2009-07-09 17:47:49 +02:00
|
|
|
{
|
2009-10-11 11:10:57 +02:00
|
|
|
struct cmd_if_shell_data *cdata = job->data;
|
2013-03-24 10:54:10 +01:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
|
2009-07-09 17:47:49 +02:00
|
|
|
struct cmd_list *cmdlist;
|
2011-10-28 00:40:15 +02:00
|
|
|
char *cause, *cmd;
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2015-09-17 00:24:54 +02:00
|
|
|
if (cmdq->flags & CMD_Q_DEAD)
|
2013-03-24 10:54:10 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
2011-10-18 10:57:01 +02:00
|
|
|
cmd = cdata->cmd_else;
|
2013-03-24 10:54:10 +01:00
|
|
|
else
|
2011-10-18 10:57:01 +02:00
|
|
|
cmd = cdata->cmd_if;
|
2013-03-24 10:54:10 +01:00
|
|
|
if (cmd == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
|
2009-07-09 17:47:49 +02:00
|
|
|
if (cause != NULL) {
|
2013-03-24 10:54:10 +01:00
|
|
|
cmdq_error(cmdq, "%s", cause);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(cause);
|
2009-07-09 17:47:49 +02:00
|
|
|
}
|
2009-10-11 11:10:57 +02:00
|
|
|
return;
|
2009-07-09 17:47:49 +02:00
|
|
|
}
|
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cmdq1 = cmdq_new(cmdq->client);
|
|
|
|
cmdq1->emptyfn = cmd_if_shell_done;
|
|
|
|
cmdq1->data = cdata;
|
|
|
|
|
2015-04-21 23:31:02 +02:00
|
|
|
cdata->references++;
|
2015-04-21 17:18:06 +02:00
|
|
|
cmdq_run(cmdq1, cmdlist, &cdata->mouse);
|
2009-07-09 17:47:49 +02:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
}
|
|
|
|
|
2013-03-24 10:54:10 +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;
|
|
|
|
|
2013-10-10 14:04:38 +02:00
|
|
|
if (cmdq1->client_exit >= 0)
|
|
|
|
cmdq->client_exit = cmdq1->client_exit;
|
2015-04-21 23:31:02 +02:00
|
|
|
cmdq_free(cmdq1);
|
|
|
|
|
|
|
|
if (--cdata->references != 0)
|
|
|
|
return;
|
2013-10-10 14:04:38 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
|
|
|
|
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
|
|
|
free(cdata);
|
|
|
|
}
|
|
|
|
|
2009-07-09 17:47:49 +02:00
|
|
|
void
|
2009-10-11 11:10:57 +02:00
|
|
|
cmd_if_shell_free(void *data)
|
2009-07-09 17:47:49 +02:00
|
|
|
{
|
2009-10-11 11:10:57 +02:00
|
|
|
struct cmd_if_shell_data *cdata = data;
|
2013-03-24 10:54:10 +01:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
|
|
|
|
2015-04-21 23:31:02 +02:00
|
|
|
if (--cdata->references != 0)
|
2013-03-24 10:54:10 +01:00
|
|
|
return;
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
2009-07-09 17:47:49 +02:00
|
|
|
|
2012-07-10 13:53:01 +02:00
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
|
|
|
free(cdata);
|
2009-07-09 17:47:49 +02:00
|
|
|
}
|