GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-set-hook.c Lines: 0 42 0.0 %
Date: 2016-12-06 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-set-hook.c,v 1.6 2016/05/12 13:21:56 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2012 Thomas Adam <thomas@xteddy.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 <stdlib.h>
22
#include <string.h>
23
24
#include "tmux.h"
25
26
/*
27
 * Set or show global or session hooks.
28
 */
29
30
enum cmd_retval cmd_set_hook_exec(struct cmd *, struct cmd_q *);
31
32
const struct cmd_entry cmd_set_hook_entry = {
33
	.name = "set-hook",
34
	.alias = NULL,
35
36
	.args = { "gt:u", 1, 2 },
37
	.usage = "[-gu] " CMD_TARGET_SESSION_USAGE " hook-name [command]",
38
39
	.tflag = CMD_SESSION_CANFAIL,
40
41
	.flags = 0,
42
	.exec = cmd_set_hook_exec
43
};
44
45
const struct cmd_entry cmd_show_hooks_entry = {
46
	.name = "show-hooks",
47
	.alias = NULL,
48
49
	.args = { "gt:", 0, 1 },
50
	.usage = "[-g] " CMD_TARGET_SESSION_USAGE,
51
52
	.tflag = CMD_SESSION,
53
54
	.flags = 0,
55
	.exec = cmd_set_hook_exec
56
};
57
58
enum cmd_retval
59
cmd_set_hook_exec(struct cmd *self, struct cmd_q *cmdq)
60
{
61
	struct args	*args = self->args;
62
	struct cmd_list	*cmdlist;
63
	struct hooks	*hooks;
64
	struct hook	*hook;
65
	char		*cause, *tmp;
66
	const char	*name, *cmd, *target;
67
68
	if (args_has(args, 'g'))
69
		hooks = global_hooks;
70
	else {
71
		if (cmdq->state.tflag.s == NULL) {
72
			target = args_get(args, 't');
73
			if (target != NULL)
74
				cmdq_error(cmdq, "no such session: %s", target);
75
			else
76
				cmdq_error(cmdq, "no current session");
77
			return (CMD_RETURN_ERROR);
78
		}
79
		hooks = cmdq->state.tflag.s->hooks;
80
	}
81
82
	if (self->entry == &cmd_show_hooks_entry) {
83
		hook = hooks_first(hooks);
84
		while (hook != NULL) {
85
			tmp = cmd_list_print(hook->cmdlist);
86
			cmdq_print(cmdq, "%s -> %s", hook->name, tmp);
87
			free(tmp);
88
89
			hook = hooks_next(hook);
90
		}
91
		return (CMD_RETURN_NORMAL);
92
	}
93
94
	name = args->argv[0];
95
	if (*name == '\0') {
96
		cmdq_error(cmdq, "invalid hook name");
97
		return (CMD_RETURN_ERROR);
98
	}
99
	if (args->argc < 2)
100
		cmd = NULL;
101
	else
102
		cmd = args->argv[1];
103
104
	if (args_has(args, 'u')) {
105
		if (cmd != NULL) {
106
			cmdq_error(cmdq, "command passed to unset hook: %s",
107
			    name);
108
			return (CMD_RETURN_ERROR);
109
		}
110
		hooks_remove(hooks, name);
111
		return (CMD_RETURN_NORMAL);
112
	}
113
114
	if (cmd == NULL) {
115
		cmdq_error(cmdq, "no command to set hook: %s", name);
116
		return (CMD_RETURN_ERROR);
117
	}
118
	if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
119
		if (cause != NULL) {
120
			cmdq_error(cmdq, "%s", cause);
121
			free(cause);
122
		}
123
		return (CMD_RETURN_ERROR);
124
	}
125
	hooks_add(hooks, name, cmdlist);
126
	cmd_list_free(cmdlist);
127
128
	return (CMD_RETURN_NORMAL);
129
}