GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-confirm-before.c Lines: 0 39 0.0 %
Date: 2017-11-07 Branches: 0 20 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-confirm-before.c,v 1.35 2017/05/17 15:20:23 nicm Exp $ */
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 <sys/types.h>
20
21
#include <ctype.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "tmux.h"
26
27
/*
28
 * Asks for confirmation before executing a command.
29
 */
30
31
static enum cmd_retval	cmd_confirm_before_exec(struct cmd *,
32
			    struct cmdq_item *);
33
34
static int	cmd_confirm_before_callback(struct client *, void *,
35
		    const char *, int);
36
static void	cmd_confirm_before_free(void *);
37
38
const struct cmd_entry cmd_confirm_before_entry = {
39
	.name = "confirm-before",
40
	.alias = "confirm",
41
42
	.args = { "p:t:", 1, 1 },
43
	.usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
44
45
	.flags = 0,
46
	.exec = cmd_confirm_before_exec
47
};
48
49
struct cmd_confirm_before_data {
50
	char	*cmd;
51
};
52
53
static enum cmd_retval
54
cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
55
{
56
	struct args			*args = self->args;
57
	struct cmd_confirm_before_data	*cdata;
58
	struct client			*c;
59
	char				*cmd, *copy, *new_prompt, *ptr;
60
	const char			*prompt;
61
62
	if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
63
		return (CMD_RETURN_ERROR);
64
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);
71
		free(copy);
72
	}
73
74
	cdata = xmalloc(sizeof *cdata);
75
	cdata->cmd = xstrdup(args->argv[0]);
76
77
	status_prompt_set(c, new_prompt, NULL,
78
	    cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
79
	    PROMPT_SINGLE);
80
81
	free(new_prompt);
82
	return (CMD_RETURN_NORMAL);
83
}
84
85
static enum cmd_retval
86
cmd_confirm_before_error(struct cmdq_item *item, void *data)
87
{
88
	char	*error = data;
89
90
	cmdq_error(item, "%s", error);
91
	free(error);
92
93
	return (CMD_RETURN_NORMAL);
94
}
95
96
static int
97
cmd_confirm_before_callback(struct client *c, void *data, const char *s,
98
    __unused int done)
99
{
100
	struct cmd_confirm_before_data	*cdata = data;
101
	struct cmd_list			*cmdlist;
102
	struct cmdq_item		*new_item;
103
	char				*cause;
104
105
	if (c->flags & CLIENT_DEAD)
106
		return (0);
107
108
	if (s == NULL || *s == '\0')
109
		return (0);
110
	if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
111
		return (0);
112
113
	cmdlist = cmd_string_parse(cdata->cmd, NULL, 0, &cause);
114
	if (cmdlist == NULL) {
115
		if (cause != NULL) {
116
			new_item = cmdq_get_callback(cmd_confirm_before_error,
117
			    cause);
118
		} else
119
			new_item = NULL;
120
	} else {
121
		new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
122
		cmd_list_free(cmdlist);
123
	}
124
125
	if (new_item != NULL)
126
		cmdq_append(c, new_item);
127
128
	return (0);
129
}
130
131
static void
132
cmd_confirm_before_free(void *data)
133
{
134
	struct cmd_confirm_before_data	*cdata = data;
135
136
	free(cdata->cmd);
137
	free(cdata);
138
}