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",
|
|
|
|
|
|
|
|
.args = { "p:t:", 1, 1 },
|
|
|
|
.usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
|
|
|
|
|
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 {
|
2017-05-17 15:20:23 +00:00
|
|
|
char *cmd;
|
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;
|
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
|
|
|
|
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);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(new_prompt);
|
2013-03-24 09:54:10 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
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;
|
2020-04-13 18:59:41 +00:00
|
|
|
char *error;
|
|
|
|
enum cmd_parse_status status;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (c->flags & CLIENT_DEAD)
|
|
|
|
return (0);
|
|
|
|
|
2009-08-13 23:44:18 +00:00
|
|
|
if (s == NULL || *s == '\0')
|
|
|
|
return (0);
|
2019-05-23 11:13:30 +00:00
|
|
|
if (tolower((u_char)s[0]) != 'y' || s[1] != '\0')
|
2009-07-17 06:13:27 +00:00
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-04-13 18:59:41 +00:00
|
|
|
status = cmd_parse_and_append(cdata->cmd, NULL, c, NULL, &error);
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|