Annotation of src/usr.bin/tmux/cmd-confirm-before.c, Revision 1.40
1.40 ! nicm 1: /* $OpenBSD: cmd-confirm-before.c,v 1.39 2020/04/13 15:55:51 nicm Exp $ */
1.1 nicm 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: */
1.22 nicm 18:
19: #include <sys/types.h>
1.1 nicm 20:
21: #include <ctype.h>
1.13 nicm 22: #include <stdlib.h>
1.1 nicm 23: #include <string.h>
24:
25: #include "tmux.h"
26:
27: /*
28: * Asks for confirmation before executing a command.
29: */
30:
1.31 nicm 31: static enum cmd_retval cmd_confirm_before_exec(struct cmd *,
32: struct cmdq_item *);
1.1 nicm 33:
1.35 nicm 34: static int cmd_confirm_before_callback(struct client *, void *,
35: const char *, int);
1.31 nicm 36: static void cmd_confirm_before_free(void *);
1.1 nicm 37:
38: const struct cmd_entry cmd_confirm_before_entry = {
1.27 nicm 39: .name = "confirm-before",
40: .alias = "confirm",
41:
42: .args = { "p:t:", 1, 1 },
43: .usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
44:
1.28 nicm 45: .flags = 0,
1.27 nicm 46: .exec = cmd_confirm_before_exec
1.1 nicm 47: };
48:
1.4 nicm 49: struct cmd_confirm_before_data {
1.35 nicm 50: char *cmd;
1.4 nicm 51: };
1.1 nicm 52:
1.29 nicm 53: static enum cmd_retval
1.31 nicm 54: cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 55: {
1.38 nicm 56: struct args *args = cmd_get_args(self);
1.1 nicm 57: struct cmd_confirm_before_data *cdata;
1.34 nicm 58: struct client *c;
1.12 nicm 59: char *cmd, *copy, *new_prompt, *ptr;
60: const char *prompt;
1.34 nicm 61:
62: if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
63: return (CMD_RETURN_ERROR);
1.1 nicm 64:
1.12 nicm 65: if ((prompt = args_get(args, 'p')) != NULL)
66: xasprintf(&new_prompt, "%s ", prompt);
67: else {
68: ptr = copy = xstrdup(args->argv[0]);
69: cmd = strsep(&ptr, " \t");
70: xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
1.13 nicm 71: free(copy);
1.12 nicm 72: }
1.1 nicm 73:
74: cdata = xmalloc(sizeof *cdata);
1.10 nicm 75: cdata->cmd = xstrdup(args->argv[0]);
1.19 nicm 76:
77: status_prompt_set(c, new_prompt, NULL,
1.12 nicm 78: cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
79: PROMPT_SINGLE);
1.1 nicm 80:
1.13 nicm 81: free(new_prompt);
1.19 nicm 82: return (CMD_RETURN_NORMAL);
1.1 nicm 83: }
84:
1.29 nicm 85: static int
1.35 nicm 86: cmd_confirm_before_callback(struct client *c, void *data, const char *s,
87: __unused int done)
1.1 nicm 88: {
89: struct cmd_confirm_before_data *cdata = data;
1.40 ! nicm 90: char *error;
! 91: enum cmd_parse_status status;
1.1 nicm 92:
1.19 nicm 93: if (c->flags & CLIENT_DEAD)
94: return (0);
95:
1.7 nicm 96: if (s == NULL || *s == '\0')
97: return (0);
1.37 nicm 98: if (tolower((u_char)s[0]) != 'y' || s[1] != '\0')
1.4 nicm 99: return (0);
1.1 nicm 100:
1.40 ! nicm 101: status = cmd_parse_and_append(cdata->cmd, NULL, c, NULL, &error);
! 102: if (status == CMD_PARSE_ERROR) {
! 103: cmdq_append(c, cmdq_get_error(error));
! 104: free(error);
1.1 nicm 105: }
106:
1.4 nicm 107: return (0);
108: }
109:
1.29 nicm 110: static void
1.4 nicm 111: cmd_confirm_before_free(void *data)
112: {
113: struct cmd_confirm_before_data *cdata = data;
114:
1.13 nicm 115: free(cdata->cmd);
116: free(cdata);
1.1 nicm 117: }