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
|
|
|
*/
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type cmd_if_shell_args_parse(struct args *, u_int,
|
|
|
|
char **);
|
|
|
|
static enum cmd_retval cmd_if_shell_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
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",
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
.args = { "bFt:", 2, 3, cmd_if_shell_args_parse },
|
2015-12-13 21:53:57 +00:00
|
|
|
.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
|
|
|
|
"[command]",
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
|
|
|
.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 {
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmd_list *cmd_if;
|
|
|
|
struct cmd_list *cmd_else;
|
2015-04-21 15:18:06 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
struct client *client;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2009-10-11 09:10:57 +00:00
|
|
|
};
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type
|
|
|
|
cmd_if_shell_args_parse(__unused struct args *args, u_int idx,
|
|
|
|
__unused char **cause)
|
|
|
|
{
|
|
|
|
if (idx == 1 || idx == 2)
|
|
|
|
return (ARGS_PARSE_COMMANDS_OR_STRING);
|
|
|
|
return (ARGS_PARSE_STRING);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2009-10-11 09:10:57 +00:00
|
|
|
struct cmd_if_shell_data *cdata;
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmdq_item *new_item;
|
|
|
|
char *shellcmd;
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct session *s = target->s;
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmd_list *cmdlist = NULL;
|
2021-08-20 19:50:16 +00:00
|
|
|
u_int count = args_count(args);
|
2016-03-01 12:06:07 +00:00
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
shellcmd = format_single_from_target(item, args_string(args, 0));
|
2014-12-02 23:39:02 +00:00
|
|
|
if (args_has(args, 'F')) {
|
|
|
|
if (*shellcmd != '0' && *shellcmd != '\0')
|
2021-09-09 13:38:32 +00:00
|
|
|
cmdlist = args_make_commands_now(self, item, 1, 0);
|
2021-08-20 19:50:16 +00:00
|
|
|
else if (count == 3)
|
2021-09-09 13:38:32 +00:00
|
|
|
cmdlist = args_make_commands_now(self, item, 2, 0);
|
2021-08-23 12:33:55 +00:00
|
|
|
else {
|
|
|
|
free(shellcmd);
|
2014-12-02 23:39:02 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2021-08-23 12:33:55 +00:00
|
|
|
free(shellcmd);
|
|
|
|
if (cmdlist == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
|
|
|
|
cmdq_insert_after(item, new_item);
|
2014-12-02 23:39:02 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-09 08:06:51 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2015-04-21 15:18:06 +00:00
|
|
|
|
2021-09-09 13:38:32 +00:00
|
|
|
cdata->cmd_if = args_make_commands_now(self, item, 1, 0);
|
2021-08-23 12:33:55 +00:00
|
|
|
if (cdata->cmd_if == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
if (count == 3) {
|
2021-09-09 13:38:32 +00:00
|
|
|
cdata->cmd_else = args_make_commands_now(self, item, 2, 0);
|
2021-08-23 12:33:55 +00:00
|
|
|
if (cdata->cmd_else == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2015-04-21 15:18:06 +00:00
|
|
|
|
2019-07-17 17:49:23 +00:00
|
|
|
if (!args_has(args, 'b'))
|
2020-04-13 10:59:58 +00:00
|
|
|
cdata->client = cmdq_get_client(item);
|
2019-07-17 17:49:23 +00:00
|
|
|
else
|
2020-04-13 20:51:57 +00:00
|
|
|
cdata->client = tc;
|
2017-04-22 06:27:15 +00:00
|
|
|
if (cdata->client != NULL)
|
|
|
|
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;
|
2019-05-23 11:13:30 +00:00
|
|
|
|
2021-03-02 10:56:45 +00:00
|
|
|
if (job_run(shellcmd, 0, NULL, s,
|
2020-04-13 10:59:58 +00:00
|
|
|
server_client_get_cwd(cmdq_get_client(item), s), NULL,
|
2020-03-19 13:43:18 +00:00
|
|
|
cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
|
|
|
|
-1) == NULL) {
|
2018-08-27 11:03:34 +00:00
|
|
|
cmdq_error(item, "failed to run command: %s", shellcmd);
|
|
|
|
free(shellcmd);
|
|
|
|
free(cdata);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
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-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
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct cmd_if_shell_data *cdata = job_get_data(job);
|
2016-10-16 17:55:14 +00:00
|
|
|
struct client *c = cdata->client;
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmdq_item *item = cdata->item, *new_item;
|
|
|
|
struct cmd_list *cmdlist;
|
2018-08-23 15:45:05 +00:00
|
|
|
int status;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2018-08-23 15:45:05 +00:00
|
|
|
status = job_get_status(job);
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
2021-08-23 12:33:55 +00:00
|
|
|
cmdlist = cdata->cmd_else;
|
2013-03-24 09:54:10 +00:00
|
|
|
else
|
2021-08-23 12:33:55 +00:00
|
|
|
cmdlist = cdata->cmd_if;
|
|
|
|
if (cmdlist == NULL)
|
2016-10-16 17:55:14 +00:00
|
|
|
goto out;
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
if (item == NULL) {
|
|
|
|
new_item = cmdq_get_command(cmdlist, NULL);
|
|
|
|
cmdq_append(c, new_item);
|
|
|
|
} else {
|
|
|
|
new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
|
|
|
|
cmdq_insert_after(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)
|
2019-06-18 11:08:42 +00:00
|
|
|
cmdq_continue(cdata->item);
|
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
|
|
|
|
2017-04-22 06:27:15 +00:00
|
|
|
if (cdata->client != NULL)
|
|
|
|
server_client_unref(cdata->client);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
if (cdata->cmd_else != NULL)
|
|
|
|
cmd_list_free(cdata->cmd_else);
|
|
|
|
cmd_list_free(cdata->cmd_if);
|
2019-05-23 11:13:30 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata);
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|