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.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
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-11 08:40:58 +00:00
|
|
|
.args = { "bp:t:", 1, 1 },
|
|
|
|
.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
|
|
|
char *cmd;
|
|
|
|
struct cmdq_item *item;
|
|
|
|
struct cmd_parse_input pi;
|
2009-07-17 06:13:27 +00:00
|
|
|
};
|
|
|
|
|
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);
|
2011-07-08 06:37:57 +00:00
|
|
|
char *cmd, *copy, *new_prompt, *ptr;
|
|
|
|
const char *prompt;
|
2021-08-11 08:40:58 +00:00
|
|
|
int wait = !args_has(args, 'b');
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-07-08 06:37:57 +00:00
|
|
|
if ((prompt = args_get(args, 'p')) != NULL)
|
|
|
|
xasprintf(&new_prompt, "%s ", prompt);
|
|
|
|
else {
|
|
|
|
ptr = copy = xstrdup(args->argv[0]);
|
|
|
|
cmd = strsep(&ptr, " \t");
|
|
|
|
xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(copy);
|
2011-07-08 06:37:57 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2011-01-04 00:42:46 +00:00
|
|
|
cdata->cmd = xstrdup(args->argv[0]);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2021-08-11 08:40:58 +00:00
|
|
|
cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
|
|
|
|
if (wait)
|
|
|
|
cdata->pi.item = item;
|
|
|
|
cdata->pi.c = tc;
|
|
|
|
cmd_find_copy_state(&cdata->pi.fs, target);
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
cdata->item = item;
|
|
|
|
|
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-11 08:40:58 +00:00
|
|
|
const char *cmd = cdata->cmd;
|
2020-04-13 18:59:41 +00:00
|
|
|
char *error;
|
2021-08-11 08:40:58 +00:00
|
|
|
struct cmdq_item *item = cdata->item;
|
2020-04-13 18:59:41 +00:00
|
|
|
enum cmd_parse_status status;
|
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-11 08:40:58 +00:00
|
|
|
if (item != NULL) {
|
|
|
|
status = cmd_parse_and_insert(cmd, &cdata->pi, item,
|
|
|
|
cmdq_get_state(item), &error);
|
|
|
|
} else
|
|
|
|
status = cmd_parse_and_append(cmd, &cdata->pi, c, NULL, &error);
|
2020-04-13 18:59:41 +00:00
|
|
|
if (status == CMD_PARSE_ERROR) {
|
|
|
|
cmdq_append(c, cmdq_get_error(error));
|
|
|
|
free(error);
|
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;
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|