Annotation of src/usr.bin/tmux/cmd-confirm-before.c, Revision 1.49
1.49 ! nicm 1: /* $OpenBSD: cmd-confirm-before.c,v 1.48 2021/08/21 10:22:38 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:
1.48 nicm 42: .args = { "bp:t:", 1, 1, NULL },
1.44 nicm 43: .usage = "[-b] [-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
1.27 nicm 44:
1.41 nicm 45: .flags = CMD_CLIENT_TFLAG,
1.27 nicm 46: .exec = cmd_confirm_before_exec
1.1 nicm 47: };
48:
1.4 nicm 49: struct cmd_confirm_before_data {
1.44 nicm 50: struct cmdq_item *item;
1.49 ! nicm 51: struct cmd_list *cmdlist;
1.4 nicm 52: };
1.1 nicm 53:
1.29 nicm 54: static enum cmd_retval
1.31 nicm 55: cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 56: {
1.38 nicm 57: struct args *args = cmd_get_args(self);
1.1 nicm 58: struct cmd_confirm_before_data *cdata;
1.41 nicm 59: struct client *tc = cmdq_get_target_client(item);
1.42 nicm 60: struct cmd_find_state *target = cmdq_get_target(item);
1.49 ! nicm 61: char *new_prompt;
! 62: const char *prompt, *cmd;
1.44 nicm 63: int wait = !args_has(args, 'b');
1.34 nicm 64:
1.47 nicm 65: cdata = xcalloc(1, sizeof *cdata);
1.49 ! nicm 66: cdata->cmdlist = args_make_commands_now(self, item, 0);
! 67: if (cdata->cmdlist == NULL)
! 68: return (CMD_RETURN_ERROR);
! 69:
! 70: if (wait)
! 71: cdata->item = item;
1.47 nicm 72:
1.12 nicm 73: if ((prompt = args_get(args, 'p')) != NULL)
74: xasprintf(&new_prompt, "%s ", prompt);
75: else {
1.49 ! nicm 76: cmd = cmd_get_entry(cmd_list_first(cdata->cmdlist))->name;
1.12 nicm 77: xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
78: }
1.19 nicm 79:
1.42 nicm 80: status_prompt_set(tc, target, new_prompt, NULL,
81: cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
1.43 nicm 82: PROMPT_SINGLE, PROMPT_TYPE_COMMAND);
1.45 nicm 83: free(new_prompt);
1.1 nicm 84:
1.44 nicm 85: if (!wait)
86: return (CMD_RETURN_NORMAL);
87: return (CMD_RETURN_WAIT);
1.1 nicm 88: }
89:
1.29 nicm 90: static int
1.35 nicm 91: cmd_confirm_before_callback(struct client *c, void *data, const char *s,
92: __unused int done)
1.1 nicm 93: {
94: struct cmd_confirm_before_data *cdata = data;
1.49 ! nicm 95: struct cmdq_item *item = cdata->item, *new_item;
1.45 nicm 96: int retcode = 1;
1.1 nicm 97:
1.19 nicm 98: if (c->flags & CLIENT_DEAD)
1.45 nicm 99: goto out;
1.19 nicm 100:
1.7 nicm 101: if (s == NULL || *s == '\0')
1.44 nicm 102: goto out;
1.37 nicm 103: if (tolower((u_char)s[0]) != 'y' || s[1] != '\0')
1.44 nicm 104: goto out;
1.45 nicm 105: retcode = 0;
1.1 nicm 106:
1.49 ! nicm 107: if (item == NULL) {
! 108: new_item = cmdq_get_command(cdata->cmdlist, NULL);
! 109: cmdq_append(c, new_item);
! 110: } else {
! 111: new_item = cmdq_get_command(cdata->cmdlist,
! 112: cmdq_get_state(item));
! 113: cmdq_insert_after(item, new_item);
1.1 nicm 114: }
115:
1.44 nicm 116: out:
1.45 nicm 117: if (item != NULL) {
118: if (cmdq_get_client(item) != NULL &&
119: cmdq_get_client(item)->session == NULL)
120: cmdq_get_client(item)->retval = retcode;
121: cmdq_continue(item);
122: }
1.4 nicm 123: return (0);
124: }
125:
1.29 nicm 126: static void
1.4 nicm 127: cmd_confirm_before_free(void *data)
128: {
129: struct cmd_confirm_before_data *cdata = data;
130:
1.49 ! nicm 131: cmd_list_free(cdata->cmdlist);
1.13 nicm 132: free(cdata);
1.1 nicm 133: }