Coverage Report

Created: 2026-06-12 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/notify.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2012 George Nachman <tmux@georgester.com>
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
struct notify_entry {
27
  const char    *name;
28
  struct cmd_find_state  fs;
29
  struct format_tree  *formats;
30
31
  struct client   *client;
32
  struct session    *session;
33
  struct window   *window;
34
  int      pane;
35
  const char    *pbname;
36
};
37
38
static struct cmdq_item *
39
notify_insert_one_hook(struct cmdq_item *item, struct notify_entry *ne,
40
    struct cmd_list *cmdlist, struct cmdq_state *state)
41
0
{
42
0
  struct cmdq_item  *new_item;
43
0
  char      *s;
44
45
0
  if (cmdlist == NULL)
46
0
    return (item);
47
0
  if (log_get_level() != 0) {
48
0
    s = cmd_list_print(cmdlist, 0);
49
0
    log_debug("%s: hook %s is: %s", __func__, ne->name, s);
50
0
    free(s);
51
0
  }
52
0
  new_item = cmdq_get_command(cmdlist, state);
53
0
  return (cmdq_insert_after(item, new_item));
54
0
}
55
56
static void
57
notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne)
58
5.18k
{
59
5.18k
  struct cmd_find_state    fs;
60
5.18k
  struct options      *oo;
61
5.18k
  struct cmdq_state   *state;
62
5.18k
  struct options_entry    *o;
63
5.18k
  struct options_array_item *a;
64
5.18k
  struct cmd_list     *cmdlist;
65
5.18k
  const char      *value;
66
5.18k
  struct cmd_parse_result   *pr;
67
68
5.18k
  log_debug("%s: inserting hook %s", __func__, ne->name);
69
70
5.18k
  cmd_find_clear_state(&fs, 0);
71
5.18k
  if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs))
72
5.18k
    cmd_find_from_nothing(&fs, 0);
73
0
  else
74
0
    cmd_find_copy_state(&fs, &ne->fs);
75
76
5.18k
  if (fs.s == NULL)
77
5.18k
    oo = global_s_options;
78
0
  else
79
0
    oo = fs.s->options;
80
5.18k
  o = options_get(oo, ne->name);
81
5.18k
  if (o == NULL && fs.wp != NULL) {
82
0
    oo = fs.wp->options;
83
0
    o = options_get(oo, ne->name);
84
0
  }
85
5.18k
  if (o == NULL && fs.wl != NULL) {
86
0
    oo = fs.wl->window->options;
87
0
    o = options_get(oo, ne->name);
88
0
  }
89
5.18k
  if (o == NULL) {
90
5.18k
    log_debug("%s: hook %s not found", __func__, ne->name);
91
5.18k
    return;
92
5.18k
  }
93
94
0
  state = cmdq_new_state(&fs, NULL, CMDQ_STATE_NOHOOKS);
95
0
  cmdq_add_formats(state, ne->formats);
96
97
0
  if (*ne->name == '@') {
98
0
    value = options_get_string(oo, ne->name);
99
0
    pr = cmd_parse_from_string(value, NULL);
100
0
    switch (pr->status) {
101
0
    case CMD_PARSE_ERROR:
102
0
      log_debug("%s: can't parse hook %s: %s", __func__,
103
0
          ne->name, pr->error);
104
0
      free(pr->error);
105
0
      break;
106
0
    case CMD_PARSE_SUCCESS:
107
0
      notify_insert_one_hook(item, ne, pr->cmdlist, state);
108
0
      break;
109
0
    }
110
0
  } else {
111
0
    a = options_array_first(o);
112
0
    while (a != NULL) {
113
0
      cmdlist = options_array_item_value(a)->cmdlist;
114
0
      item = notify_insert_one_hook(item, ne, cmdlist, state);
115
0
      a = options_array_next(a);
116
0
    }
117
0
  }
118
119
0
  cmdq_free_state(state);
120
0
}
121
122
static enum cmd_retval
123
notify_callback(struct cmdq_item *item, void *data)
124
5.18k
{
125
5.18k
  struct notify_entry *ne = data;
126
127
5.18k
  log_debug("%s: %s", __func__, ne->name);
128
129
5.18k
  if (strcmp(ne->name, "pane-mode-changed") == 0)
130
0
    control_notify_pane_mode_changed(ne->pane);
131
5.18k
  if (strcmp(ne->name, "window-layout-changed") == 0)
132
0
    control_notify_window_layout_changed(ne->window);
133
5.18k
  if (strcmp(ne->name, "window-pane-changed") == 0)
134
0
    control_notify_window_pane_changed(ne->window);
135
5.18k
  if (strcmp(ne->name, "window-unlinked") == 0)
136
0
    control_notify_window_unlinked(ne->session, ne->window);
137
5.18k
  if (strcmp(ne->name, "window-linked") == 0)
138
0
    control_notify_window_linked(ne->session, ne->window);
139
5.18k
  if (strcmp(ne->name, "window-renamed") == 0)
140
1.07k
    control_notify_window_renamed(ne->window);
141
5.18k
  if (strcmp(ne->name, "client-session-changed") == 0)
142
0
    control_notify_client_session_changed(ne->client);
143
5.18k
  if (strcmp(ne->name, "client-detached") == 0)
144
0
    control_notify_client_detached(ne->client);
145
5.18k
  if (strcmp(ne->name, "session-renamed") == 0)
146
0
    control_notify_session_renamed(ne->session);
147
5.18k
  if (strcmp(ne->name, "session-created") == 0)
148
0
    control_notify_session_created(ne->session);
149
5.18k
  if (strcmp(ne->name, "session-closed") == 0)
150
0
    control_notify_session_closed(ne->session);
151
5.18k
  if (strcmp(ne->name, "session-window-changed") == 0)
152
0
    control_notify_session_window_changed(ne->session);
153
5.18k
  if (strcmp(ne->name, "paste-buffer-changed") == 0)
154
783
    control_notify_paste_buffer_changed(ne->pbname);
155
5.18k
  if (strcmp(ne->name, "paste-buffer-deleted") == 0)
156
733
    control_notify_paste_buffer_deleted(ne->pbname);
157
158
5.18k
  notify_insert_hook(item, ne);
159
160
5.18k
  if (ne->client != NULL)
161
0
    server_client_unref(ne->client);
162
5.18k
  if (ne->session != NULL)
163
0
    session_remove_ref(ne->session, __func__);
164
5.18k
  if (ne->window != NULL)
165
1.07k
    window_remove_ref(ne->window, __func__);
166
167
5.18k
  if (ne->fs.s != NULL)
168
0
    session_remove_ref(ne->fs.s, __func__);
169
170
5.18k
  format_free(ne->formats);
171
5.18k
  free((void *)ne->name);
172
5.18k
  free((void *)ne->pbname);
173
5.18k
  free(ne);
174
175
5.18k
  return (CMD_RETURN_NORMAL);
176
5.18k
}
177
178
static void
179
notify_add(const char *name, struct cmd_find_state *fs, struct client *c,
180
    struct session *s, struct window *w, struct window_pane *wp,
181
    const char *pbname)
182
5.18k
{
183
5.18k
  struct notify_entry *ne;
184
5.18k
  struct cmdq_item  *item;
185
186
5.18k
  item = cmdq_running(NULL);
187
5.18k
  if (item != NULL && (cmdq_get_flags(item) & CMDQ_STATE_NOHOOKS))
188
0
    return;
189
190
5.18k
  ne = xcalloc(1, sizeof *ne);
191
5.18k
  ne->name = xstrdup(name);
192
193
5.18k
  ne->client = c;
194
5.18k
  ne->session = s;
195
5.18k
  ne->window = w;
196
5.18k
  ne->pane = (wp != NULL ? (int)wp->id : -1);
197
5.18k
  ne->pbname = (pbname != NULL ? xstrdup(pbname) : NULL);
198
199
5.18k
  ne->formats = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
200
5.18k
  format_add(ne->formats, "hook", "%s", name);
201
5.18k
  if (c != NULL)
202
0
    format_add(ne->formats, "hook_client", "%s", c->name);
203
5.18k
  if (s != NULL) {
204
0
    format_add(ne->formats, "hook_session", "$%u", s->id);
205
0
    format_add(ne->formats, "hook_session_name", "%s", s->name);
206
0
  }
207
5.18k
  if (w != NULL) {
208
1.07k
    format_add(ne->formats, "hook_window", "@%u", w->id);
209
1.07k
    format_add(ne->formats, "hook_window_name", "%s", w->name);
210
1.07k
  }
211
5.18k
  if (wp != NULL) {
212
2.59k
    format_add(ne->formats, "hook_pane", "%%%d", wp->id);
213
2.59k
    format_add(ne->formats, "hook_window", "@%u", wp->window->id);
214
2.59k
    format_add(ne->formats, "hook_window_name", "%s",
215
2.59k
        wp->window->name);
216
2.59k
  }
217
5.18k
  format_log_debug(ne->formats, __func__);
218
219
5.18k
  if (c != NULL)
220
0
    c->references++;
221
5.18k
  if (s != NULL)
222
0
    session_add_ref(s, __func__);
223
5.18k
  if (w != NULL)
224
1.07k
    window_add_ref(w, __func__);
225
226
5.18k
  cmd_find_copy_state(&ne->fs, fs);
227
5.18k
  if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */
228
0
    session_add_ref(ne->fs.s, __func__);
229
230
5.18k
  cmdq_append(NULL, cmdq_get_callback(notify_callback, ne));
231
5.18k
}
232
233
void
234
notify_hook(struct cmdq_item *item, const char *name)
235
0
{
236
0
  struct cmd_find_state *target = cmdq_get_target(item);
237
0
  struct notify_entry  ne;
238
239
0
  memset(&ne, 0, sizeof ne);
240
241
0
  ne.name = name;
242
0
  cmd_find_copy_state(&ne.fs, target);
243
244
0
  ne.client = cmdq_get_client(item);
245
0
  ne.session = target->s;
246
0
  ne.window = target->w;
247
0
  ne.pane = (target->wp != NULL ? (int)target->wp->id : -1);
248
249
0
  ne.formats = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
250
0
  format_add(ne.formats, "hook", "%s", name);
251
0
  format_log_debug(ne.formats, __func__);
252
253
0
  notify_insert_hook(item, &ne);
254
0
  format_free(ne.formats);
255
0
}
256
257
void
258
notify_client(const char *name, struct client *c)
259
0
{
260
0
  struct cmd_find_state fs;
261
262
0
  cmd_find_from_client(&fs, c, 0);
263
0
  notify_add(name, &fs, c, NULL, NULL, NULL, NULL);
264
0
}
265
266
void
267
notify_session(const char *name, struct session *s)
268
0
{
269
0
  struct cmd_find_state fs;
270
271
0
  if (session_alive(s))
272
0
    cmd_find_from_session(&fs, s, 0);
273
0
  else
274
0
    cmd_find_from_nothing(&fs, 0);
275
0
  notify_add(name, &fs, NULL, s, NULL, NULL, NULL);
276
0
}
277
278
void
279
notify_winlink(const char *name, struct winlink *wl)
280
0
{
281
0
  struct cmd_find_state fs;
282
283
0
  cmd_find_from_winlink(&fs, wl, 0);
284
0
  notify_add(name, &fs, NULL, wl->session, wl->window, NULL, NULL);
285
0
}
286
287
void
288
notify_session_window(const char *name, struct session *s, struct window *w)
289
0
{
290
0
  struct cmd_find_state fs;
291
292
0
  cmd_find_from_session_window(&fs, s, w, 0);
293
0
  notify_add(name, &fs, NULL, s, w, NULL, NULL);
294
0
}
295
296
void
297
notify_window(const char *name, struct window *w)
298
1.07k
{
299
1.07k
  struct cmd_find_state fs;
300
301
1.07k
  cmd_find_from_window(&fs, w, 0);
302
1.07k
  notify_add(name, &fs, NULL, NULL, w, NULL, NULL);
303
1.07k
}
304
305
void
306
notify_pane(const char *name, struct window_pane *wp)
307
2.59k
{
308
2.59k
  struct cmd_find_state fs;
309
310
2.59k
  cmd_find_from_pane(&fs, wp, 0);
311
2.59k
  notify_add(name, &fs, NULL, NULL, NULL, wp, NULL);
312
2.59k
}
313
314
void
315
notify_paste_buffer(const char *pbname, int deleted)
316
1.51k
{
317
1.51k
    struct cmd_find_state fs;
318
319
1.51k
  cmd_find_clear_state(&fs, 0);
320
1.51k
  if (deleted) {
321
733
    notify_add("paste-buffer-deleted", &fs, NULL, NULL, NULL, NULL,
322
733
        pbname);
323
783
  } else {
324
783
    notify_add("paste-buffer-changed", &fs, NULL, NULL, NULL, NULL,
325
783
        pbname);
326
783
  }
327
1.51k
}