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-01-06 11:57:03 +00:00
|
|
|
static int cmd_confirm_before_callback(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
|
|
|
.tflag = CMD_CLIENT,
|
|
|
|
|
|
|
|
.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 {
|
|
|
|
char *cmd;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct client *client;
|
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
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct cmd_confirm_before_data *cdata;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->state.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
|
|
|
|
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
|
|
|
|
|
|
|
cdata->client = c;
|
|
|
|
cdata->client->references++;
|
|
|
|
|
|
|
|
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-16 17:55:14 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_confirm_before_error(struct cmdq_item *item, void *data)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
|
|
|
char *error = data;
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "%s", error);
|
2016-10-16 17:55:14 +00:00
|
|
|
free(error);
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static int
|
2017-01-06 11:57:03 +00:00
|
|
|
cmd_confirm_before_callback(void *data, const char *s, __unused int done)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
struct cmd_confirm_before_data *cdata = data;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct client *c = cdata->client;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct cmd_list *cmdlist;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *new_item;
|
2009-06-01 22:58:49 +00:00
|
|
|
char *cause;
|
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
2017-01-15 22:00:56 +00:00
|
|
|
cmdlist = cmd_string_parse(cdata->cmd, NULL, 0, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2009-06-01 22:58:49 +00:00
|
|
|
if (cause != NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_callback(cmd_confirm_before_error,
|
2016-10-16 17:55:14 +00:00
|
|
|
cause);
|
|
|
|
} else
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = NULL;
|
2016-10-16 17:55:14 +00:00
|
|
|
} else {
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
|
2016-10-16 17:55:14 +00:00
|
|
|
cmd_list_free(cmdlist);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (new_item != NULL)
|
|
|
|
cmdq_append(c, new_item);
|
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;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct client *c = cdata->client;
|
|
|
|
|
2015-06-05 18:06:30 +00:00
|
|
|
server_client_unref(c);
|
2009-07-17 06:13:27 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|