GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-set-hook.c Lines: 0 44 0.0 %
Date: 2017-11-07 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-set-hook.c,v 1.11 2017/04/22 10:22:39 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
static enum cmd_retval cmd_set_hook_exec(struct cmd *, struct cmdq_item *);
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
	.target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
40
41
	.flags = CMD_AFTERHOOK,
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
	.target = { 't', CMD_FIND_SESSION, 0 },
53
54
	.flags = CMD_AFTERHOOK,
55
	.exec = cmd_set_hook_exec
56
};
57
58
static enum cmd_retval
59
cmd_set_hook_exec(struct cmd *self, struct cmdq_item *item)
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 (item->target.s == NULL) {
72
			target = args_get(args, 't');
73
			if (target != NULL)
74
				cmdq_error(item, "no such session: %s", target);
75
			else
76
				cmdq_error(item, "no current session");
77
			return (CMD_RETURN_ERROR);
78
		}
79
		hooks = item->target.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(item, "%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(item, "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(item, "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(item, "no command to set hook: %s", name);
116
		return (CMD_RETURN_ERROR);
117
	}
118
	cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
119
	if (cmdlist == NULL) {
120
		if (cause != NULL) {
121
			cmdq_error(item, "%s", cause);
122
			free(cause);
123
		}
124
		return (CMD_RETURN_ERROR);
125
	}
126
	hooks_add(hooks, name, cmdlist);
127
	cmd_list_free(cmdlist);
128
129
	return (CMD_RETURN_NORMAL);
130
}