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",
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.flags = 0,
|
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;
|
2017-04-22 10:22:39 +00:00
|
|
|
struct client *c;
|
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
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
|
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
|
|
|
|
|
|
|
status_prompt_set(c, new_prompt, NULL,
|
2011-07-08 06:37:57 +00:00
|
|
|
cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
|
|
|
|
PROMPT_SINGLE);
|
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;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *new_item;
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_result *pr;
|
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
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
pr = cmd_parse_from_string(cdata->cmd, NULL);
|
|
|
|
switch (pr->status) {
|
|
|
|
case CMD_PARSE_EMPTY:
|
|
|
|
new_item = NULL;
|
|
|
|
break;
|
|
|
|
case CMD_PARSE_ERROR:
|
|
|
|
new_item = cmdq_get_error(pr->error);
|
|
|
|
free(pr->error);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_append(c, new_item);
|
2019-05-23 11:13:30 +00:00
|
|
|
break;
|
|
|
|
case CMD_PARSE_SUCCESS:
|
2020-04-13 15:55:51 +00:00
|
|
|
new_item = cmdq_get_command(pr->cmdlist, NULL);
|
2019-05-23 11:13:30 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
cmdq_append(c, new_item);
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
}
|