2014-11-08 13:27:43 +01:00
|
|
|
/* $OpenBSD$ */
|
2009-04-27 15:21:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-21 01:27:14 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2009-04-27 15:21:16 +02:00
|
|
|
#include <ctype.h>
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2009-04-27 15:21:16 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asks for confirmation before executing a command.
|
|
|
|
*/
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
enum cmd_retval cmd_confirm_before_exec(struct cmd *, struct cmd_q *);
|
2009-04-28 20:29:44 +02:00
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
int cmd_confirm_before_callback(void *, const char *);
|
|
|
|
void cmd_confirm_before_free(void *);
|
2009-04-27 15:21:16 +02:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_confirm_before_entry = {
|
|
|
|
"confirm-before", "confirm",
|
2011-07-08 10:42:03 +02:00
|
|
|
"p:t:", 1, 1,
|
|
|
|
"[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
|
2011-01-07 15:45:34 +01:00
|
|
|
0,
|
|
|
|
cmd_confirm_before_exec
|
2009-04-27 15:21:16 +02:00
|
|
|
};
|
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
struct cmd_confirm_before_data {
|
|
|
|
char *cmd;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct client *client;
|
2009-07-17 11:26:21 +02:00
|
|
|
};
|
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
enum cmd_retval
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd_confirm_before_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-04-27 15:21:16 +02:00
|
|
|
{
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args = self->args;
|
2009-04-28 20:29:44 +02:00
|
|
|
struct cmd_confirm_before_data *cdata;
|
|
|
|
struct client *c;
|
2011-07-08 10:42:03 +02:00
|
|
|
char *cmd, *copy, *new_prompt, *ptr;
|
|
|
|
const char *prompt;
|
2009-04-27 15:21:16 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
|
2012-07-11 21:37:32 +02:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-04-28 20:29:44 +02:00
|
|
|
|
2011-07-08 10:42:03 +02: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-11 21:34:16 +02:00
|
|
|
free(copy);
|
2011-07-08 10:42:03 +02:00
|
|
|
}
|
2009-04-27 15:21:16 +02:00
|
|
|
|
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2011-01-07 15:45:34 +01:00
|
|
|
cdata->cmd = xstrdup(args->argv[0]);
|
2013-02-23 23:25:58 +01:00
|
|
|
|
|
|
|
cdata->client = c;
|
|
|
|
cdata->client->references++;
|
|
|
|
|
|
|
|
status_prompt_set(c, new_prompt, NULL,
|
2011-07-08 10:42:03 +02:00
|
|
|
cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
|
|
|
|
PROMPT_SINGLE);
|
2009-04-27 15:21:16 +02:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(new_prompt);
|
2013-02-23 23:25:58 +01:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-04-27 15:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_confirm_before_callback(void *data, const char *s)
|
|
|
|
{
|
2009-04-28 20:29:44 +02:00
|
|
|
struct cmd_confirm_before_data *cdata = data;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct client *c = cdata->client;
|
2009-04-27 15:21:16 +02:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
char *cause;
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (c->flags & CLIENT_DEAD)
|
|
|
|
return (0);
|
|
|
|
|
2009-08-16 21:29:24 +02:00
|
|
|
if (s == NULL || *s == '\0')
|
|
|
|
return (0);
|
|
|
|
if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
|
2009-07-17 11:26:21 +02:00
|
|
|
return (0);
|
2009-04-27 15:21:16 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cmd_string_parse(cdata->cmd, &cmdlist, NULL, 0, &cause) != 0) {
|
2009-04-27 15:21:16 +02:00
|
|
|
if (cause != NULL) {
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_error(c->cmdq, "%s", cause);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cause);
|
2009-04-27 15:21:16 +02:00
|
|
|
}
|
2009-07-17 11:26:21 +02:00
|
|
|
return (0);
|
2009-04-27 15:21:16 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_run(c->cmdq, cmdlist);
|
2009-04-27 15:21:16 +02:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_confirm_before_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_confirm_before_data *cdata = data;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct client *c = cdata->client;
|
|
|
|
|
|
|
|
c->references--;
|
2009-07-17 11:26:21 +02:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-04-27 15:21:16 +02:00
|
|
|
}
|