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 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]",
|
|
|
|
|
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 {
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_input input;
|
2016-10-09 08:06:51 +00:00
|
|
|
|
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;
|
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
|
|
|
{
|
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);
|
2020-04-13 15:55:51 +00:00
|
|
|
struct cmdq_state *state = cmdq_get_state(item);
|
2009-10-11 09:10:57 +00:00
|
|
|
struct cmd_if_shell_data *cdata;
|
2020-04-13 18:59:41 +00:00
|
|
|
char *shellcmd, *cmd, *error;
|
2020-04-13 08:26:27 +00:00
|
|
|
const char *file;
|
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;
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_input pi;
|
2020-04-13 18:59:41 +00:00
|
|
|
enum cmd_parse_status status;
|
2016-03-01 12:06:07 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
shellcmd = format_single_from_target(item, args->argv[0]);
|
2014-12-02 23:39:02 +00:00
|
|
|
if (args_has(args, 'F')) {
|
|
|
|
if (*shellcmd != '0' && *shellcmd != '\0')
|
|
|
|
cmd = args->argv[1];
|
|
|
|
else if (args->argc == 3)
|
|
|
|
cmd = args->argv[2];
|
2019-05-23 11:13:30 +00:00
|
|
|
else
|
|
|
|
cmd = NULL;
|
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);
|
2019-05-23 11:13:30 +00:00
|
|
|
|
|
|
|
memset(&pi, 0, sizeof pi);
|
2020-04-13 08:26:27 +00:00
|
|
|
cmd_get_source(self, &pi.file, &pi.line);
|
2019-05-23 11:13:30 +00:00
|
|
|
pi.item = item;
|
2020-04-13 20:51:57 +00:00
|
|
|
pi.c = tc;
|
2020-04-13 10:59:58 +00:00
|
|
|
cmd_find_copy_state(&pi.fs, target);
|
2019-05-23 11:13:30 +00:00
|
|
|
|
2020-04-13 18:59:41 +00:00
|
|
|
status = cmd_parse_and_insert(cmd, &pi, item, state, &error);
|
|
|
|
if (status == CMD_PARSE_ERROR) {
|
|
|
|
cmdq_error(item, "%s", error);
|
|
|
|
free(error);
|
2014-12-02 23:39:02 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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;
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
cdata->item = NULL;
|
2019-05-23 11:13:30 +00:00
|
|
|
|
|
|
|
memset(&cdata->input, 0, sizeof cdata->input);
|
2020-04-13 08:26:27 +00:00
|
|
|
cmd_get_source(self, &file, &cdata->input.line);
|
|
|
|
if (file != NULL)
|
|
|
|
cdata->input.file = xstrdup(file);
|
2020-04-13 20:51:57 +00:00
|
|
|
cdata->input.c = tc;
|
2019-05-23 11:13:30 +00:00
|
|
|
if (cdata->input.c != NULL)
|
|
|
|
cdata->input.c->references++;
|
2020-04-13 10:59:58 +00:00
|
|
|
cmd_find_copy_state(&cdata->input.fs, target);
|
2009-07-09 15:47:49 +00:00
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
if (job_run(shellcmd, s,
|
|
|
|
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;
|
2019-05-25 07:15:53 +00:00
|
|
|
struct cmdq_item *new_item = NULL;
|
2020-04-13 15:55:51 +00:00
|
|
|
struct cmdq_state *new_state = NULL;
|
2019-05-23 11:13:30 +00:00
|
|
|
char *cmd;
|
2018-08-23 15:45:05 +00:00
|
|
|
int status;
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_result *pr;
|
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)
|
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;
|
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
pr = cmd_parse_from_string(cmd, &cdata->input);
|
|
|
|
switch (pr->status) {
|
|
|
|
case CMD_PARSE_EMPTY:
|
|
|
|
break;
|
|
|
|
case CMD_PARSE_ERROR:
|
2019-05-25 07:15:53 +00:00
|
|
|
if (cdata->item != NULL)
|
|
|
|
cmdq_error(cdata->item, "%s", pr->error);
|
2019-05-23 11:13:30 +00:00
|
|
|
free(pr->error);
|
|
|
|
break;
|
|
|
|
case CMD_PARSE_SUCCESS:
|
2020-04-13 15:55:51 +00:00
|
|
|
if (cdata->item == NULL)
|
|
|
|
new_state = cmdq_new_state(NULL, NULL, 0);
|
|
|
|
else
|
|
|
|
new_state = cmdq_get_state(cdata->item);
|
|
|
|
new_item = cmdq_get_command(pr->cmdlist, new_state);
|
|
|
|
if (cdata->item == NULL)
|
|
|
|
cmdq_free_state(new_state);
|
2019-05-23 11:13:30 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
break;
|
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)
|
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
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd_else);
|
|
|
|
free(cdata->cmd_if);
|
2016-10-09 08:06:51 +00:00
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
if (cdata->input.c != NULL)
|
|
|
|
server_client_unref(cdata->input.c);
|
|
|
|
free((void *)cdata->input.file);
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata);
|
2009-07-09 15:47:49 +00:00
|
|
|
}
|