File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / tmux / cmd-confirm-before.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Jun 14 12:22:44 2017 UTC (7 years ago) by misho
Branches: tmux, MAIN
CVS tags: v2_4p0, v2_4, HEAD
tmux 2.4

    1: /* $OpenBSD$ */
    2: 
    3: /*
    4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
    5:  *
    6:  * Permission to use, copy, modify, and distribute this software for any
    7:  * purpose with or without fee is hereby granted, provided that the above
    8:  * copyright notice and this permission notice appear in all copies.
    9:  *
   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
   15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
   16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   17:  */
   18: 
   19: #include <sys/types.h>
   20: 
   21: #include <ctype.h>
   22: #include <stdlib.h>
   23: #include <string.h>
   24: 
   25: #include "tmux.h"
   26: 
   27: /*
   28:  * Asks for confirmation before executing a command.
   29:  */
   30: 
   31: static enum cmd_retval	cmd_confirm_before_exec(struct cmd *,
   32: 			    struct cmdq_item *);
   33: 
   34: static int	cmd_confirm_before_callback(void *, const char *, int);
   35: static void	cmd_confirm_before_free(void *);
   36: 
   37: const struct cmd_entry cmd_confirm_before_entry = {
   38: 	.name = "confirm-before",
   39: 	.alias = "confirm",
   40: 
   41: 	.args = { "p:t:", 1, 1 },
   42: 	.usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
   43: 
   44: 	.tflag = CMD_CLIENT,
   45: 
   46: 	.flags = 0,
   47: 	.exec = cmd_confirm_before_exec
   48: };
   49: 
   50: struct cmd_confirm_before_data {
   51: 	char		*cmd;
   52: 	struct client	*client;
   53: };
   54: 
   55: static enum cmd_retval
   56: cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
   57: {
   58: 	struct args			*args = self->args;
   59: 	struct cmd_confirm_before_data	*cdata;
   60: 	struct client			*c = item->state.c;
   61: 	char				*cmd, *copy, *new_prompt, *ptr;
   62: 	const char			*prompt;
   63: 
   64: 	if ((prompt = args_get(args, 'p')) != NULL)
   65: 		xasprintf(&new_prompt, "%s ", prompt);
   66: 	else {
   67: 		ptr = copy = xstrdup(args->argv[0]);
   68: 		cmd = strsep(&ptr, " \t");
   69: 		xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
   70: 		free(copy);
   71: 	}
   72: 
   73: 	cdata = xmalloc(sizeof *cdata);
   74: 	cdata->cmd = xstrdup(args->argv[0]);
   75: 
   76: 	cdata->client = c;
   77: 	cdata->client->references++;
   78: 
   79: 	status_prompt_set(c, new_prompt, NULL,
   80: 	    cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
   81: 	    PROMPT_SINGLE);
   82: 
   83: 	free(new_prompt);
   84: 	return (CMD_RETURN_NORMAL);
   85: }
   86: 
   87: static enum cmd_retval
   88: cmd_confirm_before_error(struct cmdq_item *item, void *data)
   89: {
   90: 	char	*error = data;
   91: 
   92: 	cmdq_error(item, "%s", error);
   93: 	free(error);
   94: 
   95: 	return (CMD_RETURN_NORMAL);
   96: }
   97: 
   98: static int
   99: cmd_confirm_before_callback(void *data, const char *s, __unused int done)
  100: {
  101: 	struct cmd_confirm_before_data	*cdata = data;
  102: 	struct client			*c = cdata->client;
  103: 	struct cmd_list			*cmdlist;
  104: 	struct cmdq_item		*new_item;
  105: 	char				*cause;
  106: 
  107: 	if (c->flags & CLIENT_DEAD)
  108: 		return (0);
  109: 
  110: 	if (s == NULL || *s == '\0')
  111: 		return (0);
  112: 	if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
  113: 		return (0);
  114: 
  115: 	cmdlist = cmd_string_parse(cdata->cmd, NULL, 0, &cause);
  116: 	if (cmdlist == NULL) {
  117: 		if (cause != NULL) {
  118: 			new_item = cmdq_get_callback(cmd_confirm_before_error,
  119: 			    cause);
  120: 		} else
  121: 			new_item = NULL;
  122: 	} else {
  123: 		new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
  124: 		cmd_list_free(cmdlist);
  125: 	}
  126: 
  127: 	if (new_item != NULL)
  128: 		cmdq_append(c, new_item);
  129: 
  130: 	return (0);
  131: }
  132: 
  133: static void
  134: cmd_confirm_before_free(void *data)
  135: {
  136: 	struct cmd_confirm_before_data	*cdata = data;
  137: 	struct client			*c = cdata->client;
  138: 
  139: 	server_client_unref(c);
  140: 
  141: 	free(cdata->cmd);
  142: 	free(cdata);
  143: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>