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
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-07-09 15:47:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2011-10-27 22:40:15 +00:00
|
|
|
* Executes a tmux command if a shell command returns true or false.
|
2009-07-09 15:47:49 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_if_shell_error(struct cmdq_item *, void *);
|
|
|
|
static void cmd_if_shell_callback(struct job *);
|
|
|
|
static void cmd_if_shell_free(void *);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_if_shell_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "if-shell",
|
|
|
|
.alias = "if",
|
|
|
|
|
|
|
|
.args = { "bFt:", 2, 3 },
|
|
|
|
.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
|
|
|
|
"[command]",
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.tflag = CMD_PANE_CANFAIL,
|
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = 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 {
|
2016-10-09 08:06:51 +00:00
|
|
|
char *file;
|
|
|
|
u_int line;
|
|
|
|
|
2015-04-21 15:18:06 +00:00
|
|
|
char *cmd_if;
|
|
|
|
char *cmd_else;
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
struct client *client;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2015-04-21 15:18:06 +00:00
|
|
|
struct mouse_event mouse;
|
2009-10-11 09:10:57 +00:00
|
|
|
};
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
|
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;
|
2014-12-02 23:39:02 +00:00
|
|
|
char *shellcmd, *cmd, *cause;
|
|
|
|
struct cmd_list *cmdlist;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *new_item;
|
|
|
|
struct session *s = item->state.tflag.s;
|
|
|
|
struct winlink *wl = item->state.tflag.wl;
|
|
|
|
struct window_pane *wp = item->state.tflag.wp;
|
2013-03-24 09:33:35 +00:00
|
|
|
struct format_tree *ft;
|
2015-10-31 08:13:58 +00:00
|
|
|
const char *cwd;
|
2013-03-24 09:33:35 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (item->client != NULL && item->client->session == NULL)
|
|
|
|
cwd = item->client->cwd;
|
2015-12-13 14:32:38 +00:00
|
|
|
else if (s != NULL)
|
|
|
|
cwd = s->cwd;
|
|
|
|
else
|
|
|
|
cwd = NULL;
|
2016-03-01 12:06:07 +00:00
|
|
|
|
2017-02-03 11:57:27 +00:00
|
|
|
ft = format_create(item, FORMAT_NONE, 0);
|
2016-10-16 19:04:05 +00:00
|
|
|
format_defaults(ft, item->state.c, s, wl, wp);
|
2013-03-24 09:33:35 +00:00
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2014-12-02 23:39:02 +00: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 22:02:54 +00:00
|
|
|
free(shellcmd);
|
2014-12-02 23:39:02 +00:00
|
|
|
if (cmd == NULL)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
2017-01-15 22:00:56 +00:00
|
|
|
cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2014-12-02 23:39:02 +00:00
|
|
|
if (cause != NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "%s", cause);
|
2014-12-02 23:39:02 +00:00
|
|
|
free(cause);
|
|
|
|
}
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0);
|
|
|
|
cmdq_insert_after(item, new_item);
|
2014-12-02 23:39:02 +00:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-09 08:06:51 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
|
|
|
if (self->file != NULL) {
|
|
|
|
cdata->file = xstrdup(self->file);
|
|
|
|
cdata->line = self->line;
|
|
|
|
}
|
2015-04-21 15:18:06 +00:00
|
|
|
|
2011-10-18 08:57:01 +00:00
|
|
|
cdata->cmd_if = xstrdup(args->argv[1]);
|
2011-10-27 22:40:15 +00:00
|
|
|
if (args->argc == 3)
|
|
|
|
cdata->cmd_else = xstrdup(args->argv[2]);
|
2011-10-18 08:57:01 +00:00
|
|
|
else
|
|
|
|
cdata->cmd_else = NULL;
|
2015-04-21 15:18:06 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
cdata->client = item->client;
|
2016-10-16 17:55:14 +00:00
|
|
|
cdata->client->references++;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if (!args_has(args, 'b'))
|
2016-10-16 19:04:05 +00:00
|
|
|
cdata->item = item;
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
cdata->item = NULL;
|
|
|
|
memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2015-04-24 22:19:36 +00:00
|
|
|
job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
|
|
|
|
cdata);
|
2013-03-24 09:33:35 +00:00
|
|
|
free(shellcmd);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if (args_has(args, 'b'))
|
2013-03-24 09:54:10 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_if_shell_error(struct cmdq_item *item, void *data)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
|
|
|
char *error = data;
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "%s", error);
|
2016-10-16 17:55:14 +00:00
|
|
|
free(error);
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2009-10-11 09:10:57 +00:00
|
|
|
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;
|
2016-10-16 17:55:14 +00:00
|
|
|
struct client *c = cdata->client;
|
2009-07-09 15:47:49 +00:00
|
|
|
struct cmd_list *cmdlist;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *new_item;
|
2016-10-16 17:55:14 +00:00
|
|
|
char *cause, *cmd, *file = cdata->file;
|
|
|
|
u_int line = cdata->line;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
2011-10-18 08:57:01 +00:00
|
|
|
cmd = cdata->cmd_else;
|
2013-03-24 09:54:10 +00:00
|
|
|
else
|
2011-10-18 08:57:01 +00:00
|
|
|
cmd = cdata->cmd_if;
|
2013-03-24 09:54:10 +00:00
|
|
|
if (cmd == NULL)
|
2016-10-16 17:55:14 +00:00
|
|
|
goto out;
|
|
|
|
|
2017-01-15 22:00:56 +00:00
|
|
|
cmdlist = cmd_string_parse(cmd, file, line, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2016-10-16 17:55:14 +00:00
|
|
|
if (cause != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_callback(cmd_if_shell_error, cause);
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = NULL;
|
2016-10-16 17:55:14 +00:00
|
|
|
} else {
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
|
2016-10-16 17:55:14 +00:00
|
|
|
cmd_list_free(cmdlist);
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (new_item != NULL) {
|
|
|
|
if (cdata->item == NULL)
|
|
|
|
cmdq_append(c, new_item);
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_insert_after(cdata->item, new_item);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
2016-10-09 08:06:51 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
out:
|
2016-10-16 19:04:05 +00:00
|
|
|
if (cdata->item != NULL)
|
|
|
|
cdata->item->flags &= ~CMDQ_WAITING;
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static 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;
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
server_client_unref(cdata->client);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
2016-10-09 08:06:51 +00:00
|
|
|
|
|
|
|
free(cdata->file);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata);
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|