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