Annotation of src/usr.bin/tmux/cmd-confirm-before.c, Revision 1.11
1.11 ! nicm 1: /* $OpenBSD: cmd-confirm-before.c,v 1.10 2011/01/04 00:42:46 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: */
18:
19: #include <ctype.h>
20: #include <string.h>
21:
22: #include "tmux.h"
23:
24: /*
25: * Asks for confirmation before executing a command.
26: */
27:
1.10 nicm 28: void cmd_confirm_before_key_binding(struct cmd *, int);
1.1 nicm 29: int cmd_confirm_before_exec(struct cmd *, struct cmd_ctx *);
30:
31: int cmd_confirm_before_callback(void *, const char *);
1.4 nicm 32: void cmd_confirm_before_free(void *);
1.1 nicm 33:
34: const struct cmd_entry cmd_confirm_before_entry = {
35: "confirm-before", "confirm",
1.10 nicm 36: "t:", 1, 1,
1.1 nicm 37: CMD_TARGET_CLIENT_USAGE " command",
1.10 nicm 38: 0,
39: cmd_confirm_before_key_binding,
40: NULL,
41: cmd_confirm_before_exec
1.1 nicm 42: };
43:
1.4 nicm 44: struct cmd_confirm_before_data {
45: struct client *c;
46: char *cmd;
47: };
48:
1.1 nicm 49: void
1.10 nicm 50: cmd_confirm_before_key_binding(struct cmd *self, int key)
1.1 nicm 51: {
52: switch (key) {
53: case '&':
1.10 nicm 54: self->args = args_create(1, "kill-window");
1.1 nicm 55: break;
56: case 'x':
1.10 nicm 57: self->args = args_create(1, "kill-pane");
58: break;
59: default:
60: self->args = args_create(0);
1.1 nicm 61: break;
62: }
63: }
64:
65: int
1.5 nicm 66: cmd_confirm_before_exec(struct cmd *self, struct cmd_ctx *ctx)
1.1 nicm 67: {
1.10 nicm 68: struct args *args = self->args;
1.1 nicm 69: struct cmd_confirm_before_data *cdata;
70: struct client *c;
71: char *buf, *cmd, *ptr;
72:
73: if (ctx->curclient == NULL) {
74: ctx->error(ctx, "must be run interactively");
75: return (-1);
76: }
77:
1.10 nicm 78: if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
1.1 nicm 79: return (-1);
80:
1.10 nicm 81: ptr = xstrdup(args->argv[0]);
1.1 nicm 82: if ((cmd = strtok(ptr, " \t")) == NULL)
83: cmd = ptr;
84: xasprintf(&buf, "Confirm '%s'? (y/n) ", cmd);
85: xfree(ptr);
86:
87: cdata = xmalloc(sizeof *cdata);
1.10 nicm 88: cdata->cmd = xstrdup(args->argv[0]);
1.1 nicm 89: cdata->c = c;
1.11 ! nicm 90: status_prompt_set(cdata->c, buf, NULL, cmd_confirm_before_callback,
! 91: cmd_confirm_before_free, cdata, PROMPT_SINGLE);
1.1 nicm 92:
93: xfree(buf);
94: return (1);
95: }
96:
97: int
98: cmd_confirm_before_callback(void *data, const char *s)
99: {
100: struct cmd_confirm_before_data *cdata = data;
101: struct client *c = cdata->c;
102: struct cmd_list *cmdlist;
103: struct cmd_ctx ctx;
104: char *cause;
105:
1.7 nicm 106: if (s == NULL || *s == '\0')
107: return (0);
108: if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
1.4 nicm 109: return (0);
1.1 nicm 110:
111: if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
112: if (cause != NULL) {
113: *cause = toupper((u_char) *cause);
1.3 nicm 114: status_message_set(c, "%s", cause);
1.1 nicm 115: xfree(cause);
116: }
1.4 nicm 117: return (0);
1.1 nicm 118: }
119:
120: ctx.msgdata = NULL;
121: ctx.curclient = c;
122:
123: ctx.error = key_bindings_error;
124: ctx.print = key_bindings_print;
125: ctx.info = key_bindings_info;
126:
127: ctx.cmdclient = NULL;
128:
129: cmd_list_exec(cmdlist, &ctx);
130: cmd_list_free(cmdlist);
131:
1.4 nicm 132: return (0);
133: }
134:
135: void
136: cmd_confirm_before_free(void *data)
137: {
138: struct cmd_confirm_before_data *cdata = data;
139:
1.1 nicm 140: if (cdata->cmd != NULL)
141: xfree(cdata->cmd);
142: xfree(cdata);
143: }