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