-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.c
200 lines (164 loc) · 4.24 KB
/
hooks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* $OpenBSD$ */
/*
* Copyright (c) 2012 Thomas Adam <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
struct hooks {
RB_HEAD(hooks_tree, hook) tree;
struct hooks *parent;
};
static int hooks_cmp(struct hook *, struct hook *);
RB_GENERATE_STATIC(hooks_tree, hook, entry, hooks_cmp);
static struct hook *hooks_find1(struct hooks *, const char *);
static void hooks_free1(struct hooks *, struct hook *);
static int
hooks_cmp(struct hook *hook1, struct hook *hook2)
{
return (strcmp(hook1->name, hook2->name));
}
struct hooks *
hooks_get(struct session *s)
{
if (s != NULL)
return (s->hooks);
return (global_hooks);
}
struct hooks *
hooks_create(struct hooks *parent)
{
struct hooks *hooks;
hooks = xcalloc(1, sizeof *hooks);
RB_INIT(&hooks->tree);
hooks->parent = parent;
return (hooks);
}
static void
hooks_free1(struct hooks *hooks, struct hook *hook)
{
RB_REMOVE(hooks_tree, &hooks->tree, hook);
cmd_list_free(hook->cmdlist);
free((char *)hook->name);
free(hook);
}
void
hooks_free(struct hooks *hooks)
{
struct hook *hook, *hook1;
RB_FOREACH_SAFE(hook, hooks_tree, &hooks->tree, hook1)
hooks_free1(hooks, hook);
free(hooks);
}
struct hook *
hooks_first(struct hooks *hooks)
{
return (RB_MIN(hooks_tree, &hooks->tree));
}
struct hook *
hooks_next(struct hook *hook)
{
return (RB_NEXT(hooks_tree, &hooks->tree, hook));
}
void
hooks_add(struct hooks *hooks, const char *name, struct cmd_list *cmdlist)
{
struct hook *hook;
if ((hook = hooks_find1(hooks, name)) != NULL)
hooks_free1(hooks, hook);
hook = xcalloc(1, sizeof *hook);
hook->name = xstrdup(name);
hook->cmdlist = cmdlist;
hook->cmdlist->references++;
RB_INSERT(hooks_tree, &hooks->tree, hook);
}
void
hooks_remove(struct hooks *hooks, const char *name)
{
struct hook *hook;
if ((hook = hooks_find1(hooks, name)) != NULL)
hooks_free1(hooks, hook);
}
static struct hook *
hooks_find1(struct hooks *hooks, const char *name)
{
struct hook hook;
hook.name = name;
return (RB_FIND(hooks_tree, &hooks->tree, &hook));
}
struct hook *
hooks_find(struct hooks *hooks, const char *name)
{
struct hook hook0, *hook;
hook0.name = name;
hook = RB_FIND(hooks_tree, &hooks->tree, &hook0);
while (hook == NULL) {
hooks = hooks->parent;
if (hooks == NULL)
break;
hook = RB_FIND(hooks_tree, &hooks->tree, &hook0);
}
return (hook);
}
void
hooks_run(struct hooks *hooks, struct client *c, struct cmd_find_state *fs,
const char *fmt, ...)
{
struct hook *hook;
va_list ap;
char *name;
struct cmdq_item *new_item;
va_start(ap, fmt);
xvasprintf(&name, fmt, ap);
va_end(ap);
hook = hooks_find(hooks, name);
if (hook == NULL) {
free(name);
return;
}
log_debug("running hook %s", name);
new_item = cmdq_get_command(hook->cmdlist, fs, NULL, CMDQ_NOHOOKS);
cmdq_format(new_item, "hook", "%s", name);
cmdq_append(c, new_item);
free(name);
}
void
hooks_insert(struct hooks *hooks, struct cmdq_item *item,
struct cmd_find_state *fs, const char *fmt, ...)
{
struct hook *hook;
va_list ap;
char *name;
struct cmdq_item *new_item;
if (item->flags & CMDQ_NOHOOKS)
return;
va_start(ap, fmt);
xvasprintf(&name, fmt, ap);
va_end(ap);
hook = hooks_find(hooks, name);
if (hook == NULL) {
free(name);
return;
}
log_debug("running hook %s (parent %p)", name, item);
new_item = cmdq_get_command(hook->cmdlist, fs, NULL, CMDQ_NOHOOKS);
cmdq_format(new_item, "hook", "%s", name);
if (item != NULL)
cmdq_insert_after(item, new_item);
else
cmdq_append(NULL, new_item);
free(name);
}