GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-confirm-before.c Lines: 0 36 0.0 %
Date: 2016-12-06 Branches: 0 16 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-confirm-before.c,v 1.28 2015/12/14 00:31:54 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
enum cmd_retval	 cmd_confirm_before_exec(struct cmd *, struct cmd_q *);
32
33
int		 cmd_confirm_before_callback(void *, const char *);
34
void		 cmd_confirm_before_free(void *);
35
36
const struct cmd_entry cmd_confirm_before_entry = {
37
	.name = "confirm-before",
38
	.alias = "confirm",
39
40
	.args = { "p:t:", 1, 1 },
41
	.usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
42
43
	.tflag = CMD_CLIENT,
44
45
	.flags = 0,
46
	.exec = cmd_confirm_before_exec
47
};
48
49
struct cmd_confirm_before_data {
50
	char		*cmd;
51
	struct client	*client;
52
};
53
54
enum cmd_retval
55
cmd_confirm_before_exec(struct cmd *self, struct cmd_q *cmdq)
56
{
57
	struct args			*args = self->args;
58
	struct cmd_confirm_before_data	*cdata;
59
	struct client			*c = cmdq->state.c;
60
	char				*cmd, *copy, *new_prompt, *ptr;
61
	const char			*prompt;
62
63
	if ((prompt = args_get(args, 'p')) != NULL)
64
		xasprintf(&new_prompt, "%s ", prompt);
65
	else {
66
		ptr = copy = xstrdup(args->argv[0]);
67
		cmd = strsep(&ptr, " \t");
68
		xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
69
		free(copy);
70
	}
71
72
	cdata = xmalloc(sizeof *cdata);
73
	cdata->cmd = xstrdup(args->argv[0]);
74
75
	cdata->client = c;
76
	cdata->client->references++;
77
78
	status_prompt_set(c, new_prompt, NULL,
79
	    cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
80
	    PROMPT_SINGLE);
81
82
	free(new_prompt);
83
	return (CMD_RETURN_NORMAL);
84
}
85
86
int
87
cmd_confirm_before_callback(void *data, const char *s)
88
{
89
	struct cmd_confirm_before_data	*cdata = data;
90
	struct client			*c = cdata->client;
91
	struct cmd_list			*cmdlist;
92
	char				*cause;
93
94
	if (c->flags & CLIENT_DEAD)
95
		return (0);
96
97
	if (s == NULL || *s == '\0')
98
		return (0);
99
	if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
100
		return (0);
101
102
	if (cmd_string_parse(cdata->cmd, &cmdlist, NULL, 0, &cause) != 0) {
103
		if (cause != NULL) {
104
			cmdq_error(c->cmdq, "%s", cause);
105
			free(cause);
106
		}
107
		return (0);
108
	}
109
110
	cmdq_run(c->cmdq, cmdlist, NULL);
111
	cmd_list_free(cmdlist);
112
113
	return (0);
114
}
115
116
void
117
cmd_confirm_before_free(void *data)
118
{
119
	struct cmd_confirm_before_data	*cdata = data;
120
	struct client			*c = cdata->client;
121
122
	server_client_unref(c);
123
124
	free(cdata->cmd);
125
	free(cdata);
126
}