2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-10-20 23:27:14 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <ctype.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asks for confirmation before executing a command.
|
|
|
|
*/
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type cmd_confirm_before_args_parse(struct args *,
|
|
|
|
u_int, char **);
|
|
|
|
static enum cmd_retval cmd_confirm_before_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-05-17 15:20:23 +00:00
|
|
|
static int cmd_confirm_before_callback(struct client *, void *,
|
|
|
|
const char *, int);
|
2016-10-16 19:04:05 +00:00
|
|
|
static void cmd_confirm_before_free(void *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_confirm_before_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "confirm-before",
|
|
|
|
.alias = "confirm",
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
.args = { "bp:t:", 1, 1, cmd_confirm_before_args_parse },
|
2021-08-11 08:40:58 +00:00
|
|
|
.usage = "[-b] [-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
.flags = CMD_CLIENT_TFLAG,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_confirm_before_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2009-07-17 06:13:27 +00:00
|
|
|
struct cmd_confirm_before_data {
|
2021-08-11 08:40:58 +00:00
|
|
|
struct cmdq_item *item;
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmd_list *cmdlist;
|
2009-07-17 06:13:27 +00:00
|
|
|
};
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type
|
|
|
|
cmd_confirm_before_args_parse(__unused struct args *args, __unused u_int idx,
|
|
|
|
__unused char **cause)
|
|
|
|
{
|
|
|
|
return (ARGS_PARSE_COMMANDS_OR_STRING);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2009-06-01 22:58:49 +00:00
|
|
|
struct cmd_confirm_before_data *cdata;
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2020-05-16 16:16:07 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2021-08-23 12:33:55 +00:00
|
|
|
char *new_prompt;
|
|
|
|
const char *prompt, *cmd;
|
2021-08-11 08:40:58 +00:00
|
|
|
int wait = !args_has(args, 'b');
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2021-09-09 13:38:32 +00:00
|
|
|
cdata->cmdlist = args_make_commands_now(self, item, 0, 0);
|
2021-08-23 12:33:55 +00:00
|
|
|
if (cdata->cmdlist == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
cdata->item = item;
|
2021-08-20 19:50:16 +00:00
|
|
|
|
2011-07-08 06:37:57 +00:00
|
|
|
if ((prompt = args_get(args, 'p')) != NULL)
|
|
|
|
xasprintf(&new_prompt, "%s ", prompt);
|
|
|
|
else {
|
2021-08-23 12:33:55 +00:00
|
|
|
cmd = cmd_get_entry(cmd_list_first(cdata->cmdlist))->name;
|
2011-07-08 06:37:57 +00:00
|
|
|
xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-05-16 16:16:07 +00:00
|
|
|
status_prompt_set(tc, target, new_prompt, NULL,
|
|
|
|
cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
|
2021-06-10 07:50:03 +00:00
|
|
|
PROMPT_SINGLE, PROMPT_TYPE_COMMAND);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(new_prompt);
|
2021-08-13 06:50:42 +00:00
|
|
|
|
2021-08-11 08:40:58 +00:00
|
|
|
if (!wait)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static int
|
2017-05-17 15:20:23 +00:00
|
|
|
cmd_confirm_before_callback(struct client *c, void *data, const char *s,
|
|
|
|
__unused int done)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
struct cmd_confirm_before_data *cdata = data;
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmdq_item *item = cdata->item, *new_item;
|
2021-08-13 06:50:42 +00:00
|
|
|
int retcode = 1;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (c->flags & CLIENT_DEAD)
|
2021-08-13 06:50:42 +00:00
|
|
|
goto out;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2009-08-13 23:44:18 +00:00
|
|
|
if (s == NULL || *s == '\0')
|
2021-08-11 08:40:58 +00:00
|
|
|
goto out;
|
2019-05-23 11:13:30 +00:00
|
|
|
if (tolower((u_char)s[0]) != 'y' || s[1] != '\0')
|
2021-08-11 08:40:58 +00:00
|
|
|
goto out;
|
2021-08-13 06:50:42 +00:00
|
|
|
retcode = 0;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
if (item == NULL) {
|
|
|
|
new_item = cmdq_get_command(cdata->cmdlist, NULL);
|
|
|
|
cmdq_append(c, new_item);
|
|
|
|
} else {
|
|
|
|
new_item = cmdq_get_command(cdata->cmdlist,
|
|
|
|
cmdq_get_state(item));
|
|
|
|
cmdq_insert_after(item, new_item);
|
2019-05-23 11:13:30 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2021-08-11 08:40:58 +00:00
|
|
|
out:
|
2021-08-13 06:50:42 +00:00
|
|
|
if (item != NULL) {
|
|
|
|
if (cmdq_get_client(item) != NULL &&
|
|
|
|
cmdq_get_client(item)->session == NULL)
|
|
|
|
cmdq_get_client(item)->retval = retcode;
|
|
|
|
cmdq_continue(item);
|
|
|
|
}
|
2009-07-17 06:13:27 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2009-07-17 06:13:27 +00:00
|
|
|
cmd_confirm_before_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_confirm_before_data *cdata = data;
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
cmd_list_free(cdata->cmdlist);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|