Annotation of src/usr.bin/tmux/cmd-confirm-before.c, Revision 1.10
1.10 ! nicm 1: /* $OpenBSD: cmd-confirm-before.c,v 1.9 2009/11/13 19:53:29 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.4 nicm 90: status_prompt_set(cdata->c, buf,
91: cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
92: PROMPT_SINGLE);
1.1 nicm 93:
94: xfree(buf);
95: return (1);
96: }
97:
98: int
99: cmd_confirm_before_callback(void *data, const char *s)
100: {
101: struct cmd_confirm_before_data *cdata = data;
102: struct client *c = cdata->c;
103: struct cmd_list *cmdlist;
104: struct cmd_ctx ctx;
105: char *cause;
106:
1.7 nicm 107: if (s == NULL || *s == '\0')
108: return (0);
109: if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
1.4 nicm 110: return (0);
1.1 nicm 111:
112: if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
113: if (cause != NULL) {
114: *cause = toupper((u_char) *cause);
1.3 nicm 115: status_message_set(c, "%s", cause);
1.1 nicm 116: xfree(cause);
117: }
1.4 nicm 118: return (0);
1.1 nicm 119: }
120:
121: ctx.msgdata = NULL;
122: ctx.curclient = c;
123:
124: ctx.error = key_bindings_error;
125: ctx.print = key_bindings_print;
126: ctx.info = key_bindings_info;
127:
128: ctx.cmdclient = NULL;
129:
130: cmd_list_exec(cmdlist, &ctx);
131: cmd_list_free(cmdlist);
132:
1.4 nicm 133: return (0);
134: }
135:
136: void
137: cmd_confirm_before_free(void *data)
138: {
139: struct cmd_confirm_before_data *cdata = data;
140:
1.1 nicm 141: if (cdata->cmd != NULL)
142: xfree(cdata->cmd);
143: xfree(cdata);
144: }