Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * Copyright (c) 2012 George Nachman <tmux@georgester.com> |
6 | | * |
7 | | * Permission to use, copy, modify, and distribute this software for any |
8 | | * purpose with or without fee is hereby granted, provided that the above |
9 | | * copyright notice and this permission notice appear in all copies. |
10 | | * |
11 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
16 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
17 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 | | */ |
19 | | |
20 | | #include <sys/types.h> |
21 | | |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <time.h> |
25 | | #include <unistd.h> |
26 | | |
27 | | #include "tmux.h" |
28 | | |
29 | | /* |
30 | | * Block of data to output. Each client has one "all" queue of blocks and |
31 | | * another queue for each pane (in struct client_offset). %output blocks are |
32 | | * added to both queues and other output lines (notifications) added only to |
33 | | * the client queue. |
34 | | * |
35 | | * When a client becomes writeable, data from blocks on the pane queue are sent |
36 | | * up to the maximum size (CLIENT_BUFFER_HIGH). If a block is entirely written, |
37 | | * it is removed from both pane and client queues and if this means non-%output |
38 | | * blocks are now at the head of the client queue, they are written. |
39 | | * |
40 | | * This means a %output block holds up any subsequent non-%output blocks until |
41 | | * it is written which enforces ordering even if the client cannot accept the |
42 | | * entire block in one go. |
43 | | */ |
44 | | struct control_block { |
45 | | size_t size; |
46 | | char *line; |
47 | | uint64_t t; |
48 | | |
49 | | TAILQ_ENTRY(control_block) entry; |
50 | | TAILQ_ENTRY(control_block) all_entry; |
51 | | }; |
52 | | |
53 | | /* Control client pane. */ |
54 | | struct control_pane { |
55 | | u_int pane; |
56 | | |
57 | | /* |
58 | | * Offsets into the pane data. The first (offset) is the data we have |
59 | | * written; the second (queued) the data we have queued (pointed to by |
60 | | * a block). |
61 | | */ |
62 | | struct window_pane_offset offset; |
63 | | struct window_pane_offset queued; |
64 | | |
65 | | int flags; |
66 | 0 | #define CONTROL_PANE_OFF 0x1 |
67 | 0 | #define CONTROL_PANE_PAUSED 0x2 |
68 | | |
69 | | int pending_flag; |
70 | | TAILQ_ENTRY(control_pane) pending_entry; |
71 | | |
72 | | TAILQ_HEAD(, control_block) blocks; |
73 | | |
74 | | RB_ENTRY(control_pane) entry; |
75 | | }; |
76 | | RB_HEAD(control_panes, control_pane); |
77 | | |
78 | | /* Subscription pane. */ |
79 | | struct control_sub_pane { |
80 | | u_int pane; |
81 | | u_int idx; |
82 | | char *last; |
83 | | |
84 | | RB_ENTRY(control_sub_pane) entry; |
85 | | }; |
86 | | RB_HEAD(control_sub_panes, control_sub_pane); |
87 | | |
88 | | /* Subscription window. */ |
89 | | struct control_sub_window { |
90 | | u_int window; |
91 | | u_int idx; |
92 | | char *last; |
93 | | |
94 | | RB_ENTRY(control_sub_window) entry; |
95 | | }; |
96 | | RB_HEAD(control_sub_windows, control_sub_window); |
97 | | |
98 | | /* Control client subscription. */ |
99 | | struct control_sub { |
100 | | char *name; |
101 | | char *format; |
102 | | |
103 | | enum control_sub_type type; |
104 | | u_int id; |
105 | | |
106 | | char *last; |
107 | | struct control_sub_panes panes; |
108 | | struct control_sub_windows windows; |
109 | | |
110 | | RB_ENTRY(control_sub) entry; |
111 | | }; |
112 | | RB_HEAD(control_subs, control_sub); |
113 | | |
114 | | /* Control client state. */ |
115 | | struct control_state { |
116 | | struct control_panes panes; |
117 | | |
118 | | TAILQ_HEAD(, control_pane) pending_list; |
119 | | u_int pending_count; |
120 | | |
121 | | TAILQ_HEAD(, control_block) all_blocks; |
122 | | |
123 | | struct bufferevent *read_event; |
124 | | struct bufferevent *write_event; |
125 | | |
126 | | struct control_subs subs; |
127 | | struct event subs_timer; |
128 | | }; |
129 | | |
130 | | /* Low and high watermarks. */ |
131 | 0 | #define CONTROL_BUFFER_LOW 512 |
132 | 0 | #define CONTROL_BUFFER_HIGH 8192 |
133 | | |
134 | | /* Minimum to write to each client. */ |
135 | 0 | #define CONTROL_WRITE_MINIMUM 32 |
136 | | |
137 | | /* Maximum age for clients that are not using pause mode. */ |
138 | 0 | #define CONTROL_MAXIMUM_AGE 300000 |
139 | | |
140 | | /* Flags to ignore client. */ |
141 | | #define CONTROL_IGNORE_FLAGS \ |
142 | 0 | (CLIENT_CONTROL_NOOUTPUT| \ |
143 | 0 | CLIENT_UNATTACHEDFLAGS) |
144 | | |
145 | | /* Compare client panes. */ |
146 | | static int |
147 | | control_pane_cmp(struct control_pane *cp1, struct control_pane *cp2) |
148 | 0 | { |
149 | 0 | if (cp1->pane < cp2->pane) |
150 | 0 | return (-1); |
151 | 0 | if (cp1->pane > cp2->pane) |
152 | 0 | return (1); |
153 | 0 | return (0); |
154 | 0 | } |
155 | | RB_GENERATE_STATIC(control_panes, control_pane, entry, control_pane_cmp); |
156 | | |
157 | | /* Compare client subs. */ |
158 | | static int |
159 | | control_sub_cmp(struct control_sub *csub1, struct control_sub *csub2) |
160 | 0 | { |
161 | 0 | return (strcmp(csub1->name, csub2->name)); |
162 | 0 | } |
163 | | RB_GENERATE_STATIC(control_subs, control_sub, entry, control_sub_cmp); |
164 | | |
165 | | /* Compare client subscription panes. */ |
166 | | static int |
167 | | control_sub_pane_cmp(struct control_sub_pane *csp1, |
168 | | struct control_sub_pane *csp2) |
169 | 0 | { |
170 | 0 | if (csp1->pane < csp2->pane) |
171 | 0 | return (-1); |
172 | 0 | if (csp1->pane > csp2->pane) |
173 | 0 | return (1); |
174 | 0 | if (csp1->idx < csp2->idx) |
175 | 0 | return (-1); |
176 | 0 | if (csp1->idx > csp2->idx) |
177 | 0 | return (1); |
178 | 0 | return (0); |
179 | 0 | } |
180 | | RB_GENERATE_STATIC(control_sub_panes, control_sub_pane, entry, |
181 | | control_sub_pane_cmp); |
182 | | |
183 | | /* Compare client subscription windows. */ |
184 | | static int |
185 | | control_sub_window_cmp(struct control_sub_window *csw1, |
186 | | struct control_sub_window *csw2) |
187 | 0 | { |
188 | 0 | if (csw1->window < csw2->window) |
189 | 0 | return (-1); |
190 | 0 | if (csw1->window > csw2->window) |
191 | 0 | return (1); |
192 | 0 | if (csw1->idx < csw2->idx) |
193 | 0 | return (-1); |
194 | 0 | if (csw1->idx > csw2->idx) |
195 | 0 | return (1); |
196 | 0 | return (0); |
197 | 0 | } |
198 | | RB_GENERATE_STATIC(control_sub_windows, control_sub_window, entry, |
199 | | control_sub_window_cmp); |
200 | | |
201 | | /* Free a subscription. */ |
202 | | static void |
203 | | control_free_sub(struct control_state *cs, struct control_sub *csub) |
204 | 0 | { |
205 | 0 | struct control_sub_pane *csp, *csp1; |
206 | 0 | struct control_sub_window *csw, *csw1; |
207 | |
|
208 | 0 | RB_FOREACH_SAFE(csp, control_sub_panes, &csub->panes, csp1) { |
209 | 0 | RB_REMOVE(control_sub_panes, &csub->panes, csp); |
210 | 0 | free(csp); |
211 | 0 | } |
212 | 0 | RB_FOREACH_SAFE(csw, control_sub_windows, &csub->windows, csw1) { |
213 | 0 | RB_REMOVE(control_sub_windows, &csub->windows, csw); |
214 | 0 | free(csw); |
215 | 0 | } |
216 | 0 | free(csub->last); |
217 | |
|
218 | 0 | RB_REMOVE(control_subs, &cs->subs, csub); |
219 | 0 | free(csub->name); |
220 | 0 | free(csub->format); |
221 | 0 | free(csub); |
222 | 0 | } |
223 | | |
224 | | /* Free a block. */ |
225 | | static void |
226 | | control_free_block(struct control_state *cs, struct control_block *cb) |
227 | 0 | { |
228 | 0 | free(cb->line); |
229 | 0 | TAILQ_REMOVE(&cs->all_blocks, cb, all_entry); |
230 | 0 | free(cb); |
231 | 0 | } |
232 | | |
233 | | /* Get pane offsets for this client. */ |
234 | | static struct control_pane * |
235 | | control_get_pane(struct client *c, struct window_pane *wp) |
236 | 0 | { |
237 | 0 | struct control_state *cs = c->control_state; |
238 | 0 | struct control_pane cp = { .pane = wp->id }; |
239 | |
|
240 | 0 | return (RB_FIND(control_panes, &cs->panes, &cp)); |
241 | 0 | } |
242 | | |
243 | | /* Add pane offsets for this client. */ |
244 | | static struct control_pane * |
245 | | control_add_pane(struct client *c, struct window_pane *wp) |
246 | 0 | { |
247 | 0 | struct control_state *cs = c->control_state; |
248 | 0 | struct control_pane *cp; |
249 | |
|
250 | 0 | cp = control_get_pane(c, wp); |
251 | 0 | if (cp != NULL) |
252 | 0 | return (cp); |
253 | | |
254 | 0 | cp = xcalloc(1, sizeof *cp); |
255 | 0 | cp->pane = wp->id; |
256 | 0 | RB_INSERT(control_panes, &cs->panes, cp); |
257 | |
|
258 | 0 | memcpy(&cp->offset, &wp->offset, sizeof cp->offset); |
259 | 0 | memcpy(&cp->queued, &wp->offset, sizeof cp->queued); |
260 | 0 | TAILQ_INIT(&cp->blocks); |
261 | |
|
262 | 0 | return (cp); |
263 | 0 | } |
264 | | |
265 | | /* Discard output for a pane. */ |
266 | | static void |
267 | | control_discard_pane(struct client *c, struct control_pane *cp) |
268 | 0 | { |
269 | 0 | struct control_state *cs = c->control_state; |
270 | 0 | struct control_block *cb, *cb1; |
271 | |
|
272 | 0 | TAILQ_FOREACH_SAFE(cb, &cp->blocks, entry, cb1) { |
273 | 0 | TAILQ_REMOVE(&cp->blocks, cb, entry); |
274 | 0 | control_free_block(cs, cb); |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | | /* Get actual pane for this client. */ |
279 | | static struct window_pane * |
280 | | control_window_pane(struct client *c, u_int pane) |
281 | 0 | { |
282 | 0 | struct window_pane *wp; |
283 | |
|
284 | 0 | if (c->session == NULL) |
285 | 0 | return (NULL); |
286 | 0 | if ((wp = window_pane_find_by_id(pane)) == NULL) |
287 | 0 | return (NULL); |
288 | 0 | if (winlink_find_by_window(&c->session->windows, wp->window) == NULL) |
289 | 0 | return (NULL); |
290 | 0 | return (wp); |
291 | 0 | } |
292 | | |
293 | | /* Reset control offsets. */ |
294 | | void |
295 | | control_reset_offsets(struct client *c) |
296 | 0 | { |
297 | 0 | struct control_state *cs = c->control_state; |
298 | 0 | struct control_pane *cp, *cp1; |
299 | |
|
300 | 0 | RB_FOREACH_SAFE(cp, control_panes, &cs->panes, cp1) { |
301 | 0 | RB_REMOVE(control_panes, &cs->panes, cp); |
302 | 0 | free(cp); |
303 | 0 | } |
304 | |
|
305 | 0 | TAILQ_INIT(&cs->pending_list); |
306 | 0 | cs->pending_count = 0; |
307 | 0 | } |
308 | | |
309 | | /* Get offsets for client. */ |
310 | | struct window_pane_offset * |
311 | | control_pane_offset(struct client *c, struct window_pane *wp, int *off) |
312 | 0 | { |
313 | 0 | struct control_state *cs = c->control_state; |
314 | 0 | struct control_pane *cp; |
315 | |
|
316 | 0 | if (c->flags & CLIENT_CONTROL_NOOUTPUT) { |
317 | 0 | *off = 0; |
318 | 0 | return (NULL); |
319 | 0 | } |
320 | | |
321 | 0 | cp = control_get_pane(c, wp); |
322 | 0 | if (cp == NULL || (cp->flags & CONTROL_PANE_PAUSED)) { |
323 | 0 | *off = 0; |
324 | 0 | return (NULL); |
325 | 0 | } |
326 | 0 | if (cp->flags & CONTROL_PANE_OFF) { |
327 | 0 | *off = 1; |
328 | 0 | return (NULL); |
329 | 0 | } |
330 | 0 | *off = (EVBUFFER_LENGTH(cs->write_event->output) >= CONTROL_BUFFER_LOW); |
331 | 0 | return (&cp->offset); |
332 | 0 | } |
333 | | |
334 | | /* Set pane as on. */ |
335 | | void |
336 | | control_set_pane_on(struct client *c, struct window_pane *wp) |
337 | 0 | { |
338 | 0 | struct control_pane *cp; |
339 | |
|
340 | 0 | cp = control_get_pane(c, wp); |
341 | 0 | if (cp != NULL && (cp->flags & CONTROL_PANE_OFF)) { |
342 | 0 | cp->flags &= ~CONTROL_PANE_OFF; |
343 | 0 | memcpy(&cp->offset, &wp->offset, sizeof cp->offset); |
344 | 0 | memcpy(&cp->queued, &wp->offset, sizeof cp->queued); |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | | /* Set pane as off. */ |
349 | | void |
350 | | control_set_pane_off(struct client *c, struct window_pane *wp) |
351 | 0 | { |
352 | 0 | struct control_pane *cp; |
353 | |
|
354 | 0 | cp = control_add_pane(c, wp); |
355 | 0 | cp->flags |= CONTROL_PANE_OFF; |
356 | 0 | } |
357 | | |
358 | | /* Continue a paused pane. */ |
359 | | void |
360 | | control_continue_pane(struct client *c, struct window_pane *wp) |
361 | 0 | { |
362 | 0 | struct control_pane *cp; |
363 | |
|
364 | 0 | cp = control_get_pane(c, wp); |
365 | 0 | if (cp != NULL && (cp->flags & CONTROL_PANE_PAUSED)) { |
366 | 0 | cp->flags &= ~CONTROL_PANE_PAUSED; |
367 | 0 | memcpy(&cp->offset, &wp->offset, sizeof cp->offset); |
368 | 0 | memcpy(&cp->queued, &wp->offset, sizeof cp->queued); |
369 | 0 | control_write(c, "%%continue %%%u", wp->id); |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | | /* Pause a pane. */ |
374 | | void |
375 | | control_pause_pane(struct client *c, struct window_pane *wp) |
376 | 0 | { |
377 | 0 | struct control_pane *cp; |
378 | |
|
379 | 0 | cp = control_add_pane(c, wp); |
380 | 0 | if (~cp->flags & CONTROL_PANE_PAUSED) { |
381 | 0 | cp->flags |= CONTROL_PANE_PAUSED; |
382 | 0 | control_discard_pane(c, cp); |
383 | 0 | control_write(c, "%%pause %%%u", wp->id); |
384 | 0 | } |
385 | 0 | } |
386 | | |
387 | | /* Write a line. */ |
388 | | static void printflike(2, 0) |
389 | | control_vwrite(struct client *c, const char *fmt, va_list ap) |
390 | 0 | { |
391 | 0 | struct control_state *cs = c->control_state; |
392 | 0 | char *s; |
393 | |
|
394 | 0 | xvasprintf(&s, fmt, ap); |
395 | 0 | log_debug("%s: %s: writing line: %s", __func__, c->name, s); |
396 | |
|
397 | 0 | bufferevent_write(cs->write_event, s, strlen(s)); |
398 | 0 | bufferevent_write(cs->write_event, "\n", 1); |
399 | |
|
400 | 0 | bufferevent_enable(cs->write_event, EV_WRITE); |
401 | 0 | free(s); |
402 | 0 | } |
403 | | |
404 | | /* Write a line. */ |
405 | | void |
406 | | control_write(struct client *c, const char *fmt, ...) |
407 | 0 | { |
408 | 0 | struct control_state *cs = c->control_state; |
409 | 0 | struct control_block *cb; |
410 | 0 | va_list ap; |
411 | |
|
412 | 0 | va_start(ap, fmt); |
413 | |
|
414 | 0 | if (TAILQ_EMPTY(&cs->all_blocks)) { |
415 | 0 | control_vwrite(c, fmt, ap); |
416 | 0 | va_end(ap); |
417 | 0 | return; |
418 | 0 | } |
419 | | |
420 | 0 | cb = xcalloc(1, sizeof *cb); |
421 | 0 | xvasprintf(&cb->line, fmt, ap); |
422 | 0 | TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry); |
423 | 0 | cb->t = get_timer(); |
424 | |
|
425 | 0 | log_debug("%s: %s: storing line: %s", __func__, c->name, cb->line); |
426 | 0 | bufferevent_enable(cs->write_event, EV_WRITE); |
427 | |
|
428 | 0 | va_end(ap); |
429 | 0 | } |
430 | | |
431 | | /* Check age for this pane. */ |
432 | | static int |
433 | | control_check_age(struct client *c, struct window_pane *wp, |
434 | | struct control_pane *cp) |
435 | 0 | { |
436 | 0 | struct control_block *cb; |
437 | 0 | uint64_t t, age; |
438 | |
|
439 | 0 | cb = TAILQ_FIRST(&cp->blocks); |
440 | 0 | if (cb == NULL) |
441 | 0 | return (0); |
442 | 0 | t = get_timer(); |
443 | 0 | if (cb->t >= t) |
444 | 0 | return (0); |
445 | | |
446 | 0 | age = t - cb->t; |
447 | 0 | log_debug("%s: %s: %%%u is %llu behind", __func__, c->name, wp->id, |
448 | 0 | (unsigned long long)age); |
449 | |
|
450 | 0 | if (c->flags & CLIENT_CONTROL_PAUSEAFTER) { |
451 | 0 | if (age < c->pause_age) |
452 | 0 | return (0); |
453 | 0 | cp->flags |= CONTROL_PANE_PAUSED; |
454 | 0 | control_discard_pane(c, cp); |
455 | 0 | control_write(c, "%%pause %%%u", wp->id); |
456 | 0 | } else { |
457 | 0 | if (age < CONTROL_MAXIMUM_AGE) |
458 | 0 | return (0); |
459 | 0 | c->exit_message = xstrdup("too far behind"); |
460 | 0 | c->flags |= CLIENT_EXIT; |
461 | 0 | control_discard(c); |
462 | 0 | } |
463 | 0 | return (1); |
464 | 0 | } |
465 | | |
466 | | /* Write output from a pane. */ |
467 | | void |
468 | | control_write_output(struct client *c, struct window_pane *wp) |
469 | 0 | { |
470 | 0 | struct control_state *cs = c->control_state; |
471 | 0 | struct control_pane *cp; |
472 | 0 | struct control_block *cb; |
473 | 0 | size_t new_size; |
474 | |
|
475 | 0 | if (winlink_find_by_window(&c->session->windows, wp->window) == NULL) |
476 | 0 | return; |
477 | | |
478 | 0 | if (c->flags & CONTROL_IGNORE_FLAGS) { |
479 | 0 | cp = control_get_pane(c, wp); |
480 | 0 | if (cp != NULL) |
481 | 0 | goto ignore; |
482 | 0 | return; |
483 | 0 | } |
484 | 0 | cp = control_add_pane(c, wp); |
485 | 0 | if (cp->flags & (CONTROL_PANE_OFF|CONTROL_PANE_PAUSED)) |
486 | 0 | goto ignore; |
487 | 0 | if (control_check_age(c, wp, cp)) |
488 | 0 | return; |
489 | | |
490 | 0 | window_pane_get_new_data(wp, &cp->queued, &new_size); |
491 | 0 | if (new_size == 0) |
492 | 0 | return; |
493 | 0 | window_pane_update_used_data(wp, &cp->queued, new_size); |
494 | |
|
495 | 0 | cb = xcalloc(1, sizeof *cb); |
496 | 0 | cb->size = new_size; |
497 | 0 | TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry); |
498 | 0 | cb->t = get_timer(); |
499 | |
|
500 | 0 | TAILQ_INSERT_TAIL(&cp->blocks, cb, entry); |
501 | 0 | log_debug("%s: %s: new output block of %zu for %%%u", __func__, c->name, |
502 | 0 | cb->size, wp->id); |
503 | |
|
504 | 0 | if (!cp->pending_flag) { |
505 | 0 | log_debug("%s: %s: %%%u now pending", __func__, c->name, |
506 | 0 | wp->id); |
507 | 0 | TAILQ_INSERT_TAIL(&cs->pending_list, cp, pending_entry); |
508 | 0 | cp->pending_flag = 1; |
509 | 0 | cs->pending_count++; |
510 | 0 | } |
511 | 0 | bufferevent_enable(cs->write_event, EV_WRITE); |
512 | 0 | return; |
513 | | |
514 | 0 | ignore: |
515 | 0 | log_debug("%s: %s: ignoring pane %%%u", __func__, c->name, wp->id); |
516 | 0 | window_pane_update_used_data(wp, &cp->offset, SIZE_MAX); |
517 | 0 | window_pane_update_used_data(wp, &cp->queued, SIZE_MAX); |
518 | 0 | } |
519 | | |
520 | | /* Control client error callback. */ |
521 | | static enum cmd_retval |
522 | | control_error(struct cmdq_item *item, void *data) |
523 | 0 | { |
524 | 0 | struct client *c = cmdq_get_client(item); |
525 | 0 | char *error = data; |
526 | |
|
527 | 0 | cmdq_guard(item, "begin", 1); |
528 | 0 | control_write(c, "parse error: %s", error); |
529 | 0 | cmdq_guard(item, "error", 1); |
530 | |
|
531 | 0 | free(error); |
532 | 0 | return (CMD_RETURN_NORMAL); |
533 | 0 | } |
534 | | |
535 | | /* Control client error callback. */ |
536 | | static void |
537 | | control_error_callback(__unused struct bufferevent *bufev, |
538 | | __unused short what, void *data) |
539 | 0 | { |
540 | 0 | struct client *c = data; |
541 | |
|
542 | 0 | c->flags |= CLIENT_EXIT; |
543 | 0 | } |
544 | | |
545 | | /* Control client input callback. Read lines and fire commands. */ |
546 | | static void |
547 | | control_read_callback(__unused struct bufferevent *bufev, void *data) |
548 | 0 | { |
549 | 0 | struct client *c = data; |
550 | 0 | struct control_state *cs = c->control_state; |
551 | 0 | struct evbuffer *buffer = cs->read_event->input; |
552 | 0 | char *line, *error; |
553 | 0 | struct cmdq_state *state; |
554 | 0 | enum cmd_parse_status status; |
555 | |
|
556 | 0 | for (;;) { |
557 | 0 | line = evbuffer_readln(buffer, NULL, EVBUFFER_EOL_LF); |
558 | 0 | if (line == NULL) |
559 | 0 | break; |
560 | 0 | log_debug("%s: %s: %s", __func__, c->name, line); |
561 | 0 | if (*line == '\0') { /* empty line detach */ |
562 | 0 | free(line); |
563 | 0 | c->flags |= CLIENT_EXIT; |
564 | 0 | break; |
565 | 0 | } |
566 | | |
567 | 0 | state = cmdq_new_state(NULL, NULL, CMDQ_STATE_CONTROL); |
568 | 0 | status = cmd_parse_and_append(line, NULL, c, state, &error); |
569 | 0 | if (status == CMD_PARSE_ERROR) |
570 | 0 | cmdq_append(c, cmdq_get_callback(control_error, error)); |
571 | 0 | cmdq_free_state(state); |
572 | |
|
573 | 0 | free(line); |
574 | 0 | } |
575 | 0 | } |
576 | | |
577 | | /* Does this control client have outstanding data to write? */ |
578 | | int |
579 | | control_all_done(struct client *c) |
580 | 0 | { |
581 | 0 | struct control_state *cs = c->control_state; |
582 | |
|
583 | 0 | if (!TAILQ_EMPTY(&cs->all_blocks)) |
584 | 0 | return (0); |
585 | 0 | return (EVBUFFER_LENGTH(cs->write_event->output) == 0); |
586 | 0 | } |
587 | | |
588 | | /* Flush all blocks until output. */ |
589 | | static void |
590 | | control_flush_all_blocks(struct client *c) |
591 | 0 | { |
592 | 0 | struct control_state *cs = c->control_state; |
593 | 0 | struct control_block *cb, *cb1; |
594 | |
|
595 | 0 | TAILQ_FOREACH_SAFE(cb, &cs->all_blocks, all_entry, cb1) { |
596 | 0 | if (cb->size != 0) |
597 | 0 | break; |
598 | 0 | log_debug("%s: %s: flushing line: %s", __func__, c->name, |
599 | 0 | cb->line); |
600 | |
|
601 | 0 | bufferevent_write(cs->write_event, cb->line, strlen(cb->line)); |
602 | 0 | bufferevent_write(cs->write_event, "\n", 1); |
603 | 0 | control_free_block(cs, cb); |
604 | 0 | } |
605 | 0 | } |
606 | | |
607 | | /* Append data to buffer. */ |
608 | | static struct evbuffer * |
609 | | control_append_data(struct client *c, struct control_pane *cp, uint64_t age, |
610 | | struct evbuffer *message, struct window_pane *wp, size_t size) |
611 | 0 | { |
612 | 0 | u_char *new_data; |
613 | 0 | size_t new_size; |
614 | 0 | u_int i; |
615 | |
|
616 | 0 | if (message == NULL) { |
617 | 0 | message = evbuffer_new(); |
618 | 0 | if (message == NULL) |
619 | 0 | fatalx("out of memory"); |
620 | 0 | if (c->flags & CLIENT_CONTROL_PAUSEAFTER) { |
621 | 0 | evbuffer_add_printf(message, |
622 | 0 | "%%extended-output %%%u %llu : ", wp->id, |
623 | 0 | (unsigned long long)age); |
624 | 0 | } else |
625 | 0 | evbuffer_add_printf(message, "%%output %%%u ", wp->id); |
626 | 0 | } |
627 | | |
628 | 0 | new_data = window_pane_get_new_data(wp, &cp->offset, &new_size); |
629 | 0 | if (new_size < size) |
630 | 0 | fatalx("not enough data: %zu < %zu", new_size, size); |
631 | 0 | for (i = 0; i < size; i++) { |
632 | 0 | if (new_data[i] < ' ' || new_data[i] == '\\') |
633 | 0 | evbuffer_add_printf(message, "\\%03o", new_data[i]); |
634 | 0 | else |
635 | 0 | evbuffer_add_printf(message, "%c", new_data[i]); |
636 | 0 | } |
637 | 0 | window_pane_update_used_data(wp, &cp->offset, size); |
638 | 0 | return (message); |
639 | 0 | } |
640 | | |
641 | | /* Write buffer. */ |
642 | | static void |
643 | | control_write_data(struct client *c, struct evbuffer *message) |
644 | 0 | { |
645 | 0 | struct control_state *cs = c->control_state; |
646 | |
|
647 | 0 | log_debug("%s: %s: %.*s", __func__, c->name, |
648 | 0 | (int)EVBUFFER_LENGTH(message), EVBUFFER_DATA(message)); |
649 | |
|
650 | 0 | evbuffer_add(message, "\n", 1); |
651 | 0 | bufferevent_write_buffer(cs->write_event, message); |
652 | 0 | evbuffer_free(message); |
653 | 0 | } |
654 | | |
655 | | /* Write output to client. */ |
656 | | static int |
657 | | control_write_pending(struct client *c, struct control_pane *cp, size_t limit) |
658 | 0 | { |
659 | 0 | struct control_state *cs = c->control_state; |
660 | 0 | struct window_pane *wp = NULL; |
661 | 0 | struct evbuffer *message = NULL; |
662 | 0 | size_t used = 0, size; |
663 | 0 | struct control_block *cb, *cb1; |
664 | 0 | uint64_t age, t = get_timer(); |
665 | |
|
666 | 0 | wp = control_window_pane(c, cp->pane); |
667 | 0 | if (wp == NULL || wp->fd == -1) { |
668 | 0 | TAILQ_FOREACH_SAFE(cb, &cp->blocks, entry, cb1) { |
669 | 0 | TAILQ_REMOVE(&cp->blocks, cb, entry); |
670 | 0 | control_free_block(cs, cb); |
671 | 0 | } |
672 | 0 | control_flush_all_blocks(c); |
673 | 0 | return (0); |
674 | 0 | } |
675 | | |
676 | 0 | while (used != limit && !TAILQ_EMPTY(&cp->blocks)) { |
677 | 0 | if (control_check_age(c, wp, cp)) { |
678 | 0 | if (message != NULL) |
679 | 0 | evbuffer_free(message); |
680 | 0 | message = NULL; |
681 | 0 | break; |
682 | 0 | } |
683 | | |
684 | 0 | cb = TAILQ_FIRST(&cp->blocks); |
685 | 0 | if (cb->t < t) |
686 | 0 | age = t - cb->t; |
687 | 0 | else |
688 | 0 | age = 0; |
689 | 0 | log_debug("%s: %s: output block %zu (age %llu) for %%%u " |
690 | 0 | "(used %zu/%zu)", __func__, c->name, cb->size, |
691 | 0 | (unsigned long long)age, cp->pane, used, limit); |
692 | |
|
693 | 0 | size = cb->size; |
694 | 0 | if (size > limit - used) |
695 | 0 | size = limit - used; |
696 | 0 | used += size; |
697 | |
|
698 | 0 | message = control_append_data(c, cp, age, message, wp, size); |
699 | |
|
700 | 0 | cb->size -= size; |
701 | 0 | if (cb->size == 0) { |
702 | 0 | TAILQ_REMOVE(&cp->blocks, cb, entry); |
703 | 0 | control_free_block(cs, cb); |
704 | |
|
705 | 0 | cb = TAILQ_FIRST(&cs->all_blocks); |
706 | 0 | if (cb != NULL && cb->size == 0) { |
707 | 0 | if (wp != NULL && message != NULL) { |
708 | 0 | control_write_data(c, message); |
709 | 0 | message = NULL; |
710 | 0 | } |
711 | 0 | control_flush_all_blocks(c); |
712 | 0 | } |
713 | 0 | } |
714 | 0 | } |
715 | 0 | if (message != NULL) |
716 | 0 | control_write_data(c, message); |
717 | 0 | return (!TAILQ_EMPTY(&cp->blocks)); |
718 | 0 | } |
719 | | |
720 | | /* Control client write callback. */ |
721 | | static void |
722 | | control_write_callback(__unused struct bufferevent *bufev, void *data) |
723 | 0 | { |
724 | 0 | struct client *c = data; |
725 | 0 | struct control_state *cs = c->control_state; |
726 | 0 | struct control_pane *cp, *cp1; |
727 | 0 | struct evbuffer *evb = cs->write_event->output; |
728 | 0 | size_t space, limit; |
729 | |
|
730 | 0 | control_flush_all_blocks(c); |
731 | |
|
732 | 0 | while (EVBUFFER_LENGTH(evb) < CONTROL_BUFFER_HIGH) { |
733 | 0 | if (cs->pending_count == 0) |
734 | 0 | break; |
735 | 0 | space = CONTROL_BUFFER_HIGH - EVBUFFER_LENGTH(evb); |
736 | 0 | log_debug("%s: %s: %zu bytes available, %u panes", __func__, |
737 | 0 | c->name, space, cs->pending_count); |
738 | |
|
739 | 0 | limit = (space / cs->pending_count / 3); /* 3 bytes for \xxx */ |
740 | 0 | if (limit < CONTROL_WRITE_MINIMUM) |
741 | 0 | limit = CONTROL_WRITE_MINIMUM; |
742 | |
|
743 | 0 | TAILQ_FOREACH_SAFE(cp, &cs->pending_list, pending_entry, cp1) { |
744 | 0 | if (EVBUFFER_LENGTH(evb) >= CONTROL_BUFFER_HIGH) |
745 | 0 | break; |
746 | 0 | if (control_write_pending(c, cp, limit)) |
747 | 0 | continue; |
748 | 0 | TAILQ_REMOVE(&cs->pending_list, cp, pending_entry); |
749 | 0 | cp->pending_flag = 0; |
750 | 0 | cs->pending_count--; |
751 | 0 | } |
752 | 0 | } |
753 | 0 | if (EVBUFFER_LENGTH(evb) == 0) |
754 | 0 | bufferevent_disable(cs->write_event, EV_WRITE); |
755 | 0 | } |
756 | | |
757 | | /* Initialize for control mode. */ |
758 | | void |
759 | | control_start(struct client *c) |
760 | 0 | { |
761 | 0 | struct control_state *cs; |
762 | |
|
763 | 0 | if (c->flags & CLIENT_CONTROLCONTROL) { |
764 | 0 | close(c->out_fd); |
765 | 0 | c->out_fd = -1; |
766 | 0 | } else |
767 | 0 | setblocking(c->out_fd, 0); |
768 | 0 | setblocking(c->fd, 0); |
769 | |
|
770 | 0 | cs = c->control_state = xcalloc(1, sizeof *cs); |
771 | 0 | RB_INIT(&cs->panes); |
772 | 0 | TAILQ_INIT(&cs->pending_list); |
773 | 0 | TAILQ_INIT(&cs->all_blocks); |
774 | 0 | RB_INIT(&cs->subs); |
775 | |
|
776 | 0 | cs->read_event = bufferevent_new(c->fd, control_read_callback, |
777 | 0 | control_write_callback, control_error_callback, c); |
778 | 0 | if (cs->read_event == NULL) |
779 | 0 | fatalx("out of memory"); |
780 | | |
781 | 0 | if (c->flags & CLIENT_CONTROLCONTROL) |
782 | 0 | cs->write_event = cs->read_event; |
783 | 0 | else { |
784 | 0 | cs->write_event = bufferevent_new(c->out_fd, NULL, |
785 | 0 | control_write_callback, control_error_callback, c); |
786 | 0 | if (cs->write_event == NULL) |
787 | 0 | fatalx("out of memory"); |
788 | 0 | } |
789 | 0 | bufferevent_setwatermark(cs->write_event, EV_WRITE, CONTROL_BUFFER_LOW, |
790 | 0 | 0); |
791 | |
|
792 | 0 | if (c->flags & CLIENT_CONTROLCONTROL) { |
793 | 0 | bufferevent_write(cs->write_event, "\033P1000p", 7); |
794 | 0 | bufferevent_enable(cs->write_event, EV_WRITE); |
795 | 0 | } |
796 | 0 | } |
797 | | |
798 | | /* Control client ready. */ |
799 | | void |
800 | | control_ready(struct client *c) |
801 | 0 | { |
802 | 0 | bufferevent_enable(c->control_state->read_event, EV_READ); |
803 | 0 | } |
804 | | |
805 | | /* Discard all output for a client. */ |
806 | | void |
807 | | control_discard(struct client *c) |
808 | 0 | { |
809 | 0 | struct control_state *cs = c->control_state; |
810 | 0 | struct control_pane *cp; |
811 | |
|
812 | 0 | RB_FOREACH(cp, control_panes, &cs->panes) |
813 | 0 | control_discard_pane(c, cp); |
814 | 0 | bufferevent_disable(cs->read_event, EV_READ); |
815 | 0 | } |
816 | | |
817 | | /* Stop control mode. */ |
818 | | void |
819 | | control_stop(struct client *c) |
820 | 0 | { |
821 | 0 | struct control_state *cs = c->control_state; |
822 | 0 | struct control_block *cb, *cb1; |
823 | 0 | struct control_sub *csub, *csub1; |
824 | |
|
825 | 0 | if (~c->flags & CLIENT_CONTROLCONTROL) |
826 | 0 | bufferevent_free(cs->write_event); |
827 | 0 | bufferevent_free(cs->read_event); |
828 | |
|
829 | 0 | RB_FOREACH_SAFE(csub, control_subs, &cs->subs, csub1) |
830 | 0 | control_free_sub(cs, csub); |
831 | 0 | if (evtimer_initialized(&cs->subs_timer)) |
832 | 0 | evtimer_del(&cs->subs_timer); |
833 | |
|
834 | 0 | TAILQ_FOREACH_SAFE(cb, &cs->all_blocks, all_entry, cb1) |
835 | 0 | control_free_block(cs, cb); |
836 | 0 | control_reset_offsets(c); |
837 | |
|
838 | 0 | free(cs); |
839 | 0 | } |
840 | | |
841 | | /* Check session subscription. */ |
842 | | static void |
843 | | control_check_subs_session(struct client *c, struct control_sub *csub) |
844 | 0 | { |
845 | 0 | struct session *s = c->session; |
846 | 0 | struct format_tree *ft; |
847 | 0 | char *value; |
848 | |
|
849 | 0 | ft = format_create_defaults(NULL, c, s, NULL, NULL); |
850 | 0 | value = format_expand(ft, csub->format); |
851 | 0 | format_free(ft); |
852 | |
|
853 | 0 | if (csub->last != NULL && strcmp(value, csub->last) == 0) { |
854 | 0 | free(value); |
855 | 0 | return; |
856 | 0 | } |
857 | 0 | control_write(c, |
858 | 0 | "%%subscription-changed %s $%u - - - : %s", |
859 | 0 | csub->name, s->id, value); |
860 | 0 | free(csub->last); |
861 | 0 | csub->last = value; |
862 | 0 | } |
863 | | |
864 | | /* Check pane subscription. */ |
865 | | static void |
866 | | control_check_subs_pane(struct client *c, struct control_sub *csub) |
867 | 0 | { |
868 | 0 | struct session *s = c->session; |
869 | 0 | struct window_pane *wp; |
870 | 0 | struct window *w; |
871 | 0 | struct winlink *wl; |
872 | 0 | struct format_tree *ft; |
873 | 0 | char *value; |
874 | 0 | struct control_sub_pane *csp, find; |
875 | |
|
876 | 0 | wp = window_pane_find_by_id(csub->id); |
877 | 0 | if (wp == NULL || wp->fd == -1) |
878 | 0 | return; |
879 | 0 | w = wp->window; |
880 | |
|
881 | 0 | TAILQ_FOREACH(wl, &w->winlinks, wentry) { |
882 | 0 | if (wl->session != s) |
883 | 0 | continue; |
884 | | |
885 | 0 | ft = format_create_defaults(NULL, c, s, wl, wp); |
886 | 0 | value = format_expand(ft, csub->format); |
887 | 0 | format_free(ft); |
888 | |
|
889 | 0 | find.pane = wp->id; |
890 | 0 | find.idx = wl->idx; |
891 | |
|
892 | 0 | csp = RB_FIND(control_sub_panes, &csub->panes, &find); |
893 | 0 | if (csp == NULL) { |
894 | 0 | csp = xcalloc(1, sizeof *csp); |
895 | 0 | csp->pane = wp->id; |
896 | 0 | csp->idx = wl->idx; |
897 | 0 | RB_INSERT(control_sub_panes, &csub->panes, csp); |
898 | 0 | } |
899 | |
|
900 | 0 | if (csp->last != NULL && strcmp(value, csp->last) == 0) { |
901 | 0 | free(value); |
902 | 0 | continue; |
903 | 0 | } |
904 | 0 | control_write(c, |
905 | 0 | "%%subscription-changed %s $%u @%u %u %%%u : %s", |
906 | 0 | csub->name, s->id, w->id, wl->idx, wp->id, value); |
907 | 0 | free(csp->last); |
908 | 0 | csp->last = value; |
909 | 0 | } |
910 | 0 | } |
911 | | |
912 | | /* Check all panes subscription. */ |
913 | | static void |
914 | | control_check_subs_all_panes(struct client *c, struct control_sub *csub) |
915 | 0 | { |
916 | 0 | struct session *s = c->session; |
917 | 0 | struct window_pane *wp; |
918 | 0 | struct window *w; |
919 | 0 | struct winlink *wl; |
920 | 0 | struct format_tree *ft; |
921 | 0 | char *value; |
922 | 0 | struct control_sub_pane *csp, find; |
923 | |
|
924 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
925 | 0 | w = wl->window; |
926 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
927 | 0 | ft = format_create_defaults(NULL, c, s, wl, wp); |
928 | 0 | value = format_expand(ft, csub->format); |
929 | 0 | format_free(ft); |
930 | |
|
931 | 0 | find.pane = wp->id; |
932 | 0 | find.idx = wl->idx; |
933 | |
|
934 | 0 | csp = RB_FIND(control_sub_panes, &csub->panes, &find); |
935 | 0 | if (csp == NULL) { |
936 | 0 | csp = xcalloc(1, sizeof *csp); |
937 | 0 | csp->pane = wp->id; |
938 | 0 | csp->idx = wl->idx; |
939 | 0 | RB_INSERT(control_sub_panes, &csub->panes, csp); |
940 | 0 | } |
941 | |
|
942 | 0 | if (csp->last != NULL && |
943 | 0 | strcmp(value, csp->last) == 0) { |
944 | 0 | free(value); |
945 | 0 | continue; |
946 | 0 | } |
947 | 0 | control_write(c, |
948 | 0 | "%%subscription-changed %s $%u @%u %u %%%u : %s", |
949 | 0 | csub->name, s->id, w->id, wl->idx, wp->id, value); |
950 | 0 | free(csp->last); |
951 | 0 | csp->last = value; |
952 | 0 | } |
953 | 0 | } |
954 | 0 | } |
955 | | |
956 | | /* Check window subscription. */ |
957 | | static void |
958 | | control_check_subs_window(struct client *c, struct control_sub *csub) |
959 | 0 | { |
960 | 0 | struct session *s = c->session; |
961 | 0 | struct window *w; |
962 | 0 | struct winlink *wl; |
963 | 0 | struct format_tree *ft; |
964 | 0 | char *value; |
965 | 0 | struct control_sub_window *csw, find; |
966 | |
|
967 | 0 | w = window_find_by_id(csub->id); |
968 | 0 | if (w == NULL) |
969 | 0 | return; |
970 | | |
971 | 0 | TAILQ_FOREACH(wl, &w->winlinks, wentry) { |
972 | 0 | if (wl->session != s) |
973 | 0 | continue; |
974 | | |
975 | 0 | ft = format_create_defaults(NULL, c, s, wl, NULL); |
976 | 0 | value = format_expand(ft, csub->format); |
977 | 0 | format_free(ft); |
978 | |
|
979 | 0 | find.window = w->id; |
980 | 0 | find.idx = wl->idx; |
981 | |
|
982 | 0 | csw = RB_FIND(control_sub_windows, &csub->windows, &find); |
983 | 0 | if (csw == NULL) { |
984 | 0 | csw = xcalloc(1, sizeof *csw); |
985 | 0 | csw->window = w->id; |
986 | 0 | csw->idx = wl->idx; |
987 | 0 | RB_INSERT(control_sub_windows, &csub->windows, csw); |
988 | 0 | } |
989 | |
|
990 | 0 | if (csw->last != NULL && strcmp(value, csw->last) == 0) { |
991 | 0 | free(value); |
992 | 0 | continue; |
993 | 0 | } |
994 | 0 | control_write(c, |
995 | 0 | "%%subscription-changed %s $%u @%u %u - : %s", |
996 | 0 | csub->name, s->id, w->id, wl->idx, value); |
997 | 0 | free(csw->last); |
998 | 0 | csw->last = value; |
999 | 0 | } |
1000 | 0 | } |
1001 | | |
1002 | | /* Check all windows subscription. */ |
1003 | | static void |
1004 | | control_check_subs_all_windows(struct client *c, struct control_sub *csub) |
1005 | 0 | { |
1006 | 0 | struct session *s = c->session; |
1007 | 0 | struct window *w; |
1008 | 0 | struct winlink *wl; |
1009 | 0 | struct format_tree *ft; |
1010 | 0 | char *value; |
1011 | 0 | struct control_sub_window *csw, find; |
1012 | |
|
1013 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
1014 | 0 | w = wl->window; |
1015 | |
|
1016 | 0 | ft = format_create_defaults(NULL, c, s, wl, NULL); |
1017 | 0 | value = format_expand(ft, csub->format); |
1018 | 0 | format_free(ft); |
1019 | |
|
1020 | 0 | find.window = w->id; |
1021 | 0 | find.idx = wl->idx; |
1022 | |
|
1023 | 0 | csw = RB_FIND(control_sub_windows, &csub->windows, &find); |
1024 | 0 | if (csw == NULL) { |
1025 | 0 | csw = xcalloc(1, sizeof *csw); |
1026 | 0 | csw->window = w->id; |
1027 | 0 | csw->idx = wl->idx; |
1028 | 0 | RB_INSERT(control_sub_windows, &csub->windows, csw); |
1029 | 0 | } |
1030 | |
|
1031 | 0 | if (csw->last != NULL && strcmp(value, csw->last) == 0) { |
1032 | 0 | free(value); |
1033 | 0 | continue; |
1034 | 0 | } |
1035 | 0 | control_write(c, |
1036 | 0 | "%%subscription-changed %s $%u @%u %u - : %s", |
1037 | 0 | csub->name, s->id, w->id, wl->idx, value); |
1038 | 0 | free(csw->last); |
1039 | 0 | csw->last = value; |
1040 | 0 | } |
1041 | 0 | } |
1042 | | |
1043 | | /* Check subscriptions timer. */ |
1044 | | static void |
1045 | | control_check_subs_timer(__unused int fd, __unused short events, void *data) |
1046 | 0 | { |
1047 | 0 | struct client *c = data; |
1048 | 0 | struct control_state *cs = c->control_state; |
1049 | 0 | struct control_sub *csub, *csub1; |
1050 | 0 | struct timeval tv = { .tv_sec = 1 }; |
1051 | |
|
1052 | 0 | log_debug("%s: timer fired", __func__); |
1053 | 0 | evtimer_add(&cs->subs_timer, &tv); |
1054 | |
|
1055 | 0 | RB_FOREACH_SAFE(csub, control_subs, &cs->subs, csub1) { |
1056 | 0 | switch (csub->type) { |
1057 | 0 | case CONTROL_SUB_SESSION: |
1058 | 0 | control_check_subs_session(c, csub); |
1059 | 0 | break; |
1060 | 0 | case CONTROL_SUB_PANE: |
1061 | 0 | control_check_subs_pane(c, csub); |
1062 | 0 | break; |
1063 | 0 | case CONTROL_SUB_ALL_PANES: |
1064 | 0 | control_check_subs_all_panes(c, csub); |
1065 | 0 | break; |
1066 | 0 | case CONTROL_SUB_WINDOW: |
1067 | 0 | control_check_subs_window(c, csub); |
1068 | 0 | break; |
1069 | 0 | case CONTROL_SUB_ALL_WINDOWS: |
1070 | 0 | control_check_subs_all_windows(c, csub); |
1071 | 0 | break; |
1072 | 0 | } |
1073 | 0 | } |
1074 | 0 | } |
1075 | | |
1076 | | /* Add a subscription. */ |
1077 | | void |
1078 | | control_add_sub(struct client *c, const char *name, enum control_sub_type type, |
1079 | | int id, const char *format) |
1080 | 0 | { |
1081 | 0 | struct control_state *cs = c->control_state; |
1082 | 0 | struct control_sub *csub, find; |
1083 | 0 | struct timeval tv = { .tv_sec = 1 }; |
1084 | |
|
1085 | 0 | find.name = (char *)name; |
1086 | 0 | if ((csub = RB_FIND(control_subs, &cs->subs, &find)) != NULL) |
1087 | 0 | control_free_sub(cs, csub); |
1088 | |
|
1089 | 0 | csub = xcalloc(1, sizeof *csub); |
1090 | 0 | csub->name = xstrdup(name); |
1091 | 0 | csub->type = type; |
1092 | 0 | csub->id = id; |
1093 | 0 | csub->format = xstrdup(format); |
1094 | 0 | RB_INSERT(control_subs, &cs->subs, csub); |
1095 | |
|
1096 | 0 | RB_INIT(&csub->panes); |
1097 | 0 | RB_INIT(&csub->windows); |
1098 | |
|
1099 | 0 | if (!evtimer_initialized(&cs->subs_timer)) |
1100 | 0 | evtimer_set(&cs->subs_timer, control_check_subs_timer, c); |
1101 | 0 | if (!evtimer_pending(&cs->subs_timer, NULL)) |
1102 | 0 | evtimer_add(&cs->subs_timer, &tv); |
1103 | 0 | } |
1104 | | |
1105 | | /* Remove a subscription. */ |
1106 | | void |
1107 | | control_remove_sub(struct client *c, const char *name) |
1108 | 0 | { |
1109 | 0 | struct control_state *cs = c->control_state; |
1110 | 0 | struct control_sub *csub, find; |
1111 | |
|
1112 | 0 | find.name = (char *)name; |
1113 | 0 | if ((csub = RB_FIND(control_subs, &cs->subs, &find)) != NULL) |
1114 | 0 | control_free_sub(cs, csub); |
1115 | 0 | if (RB_EMPTY(&cs->subs)) |
1116 | 0 | evtimer_del(&cs->subs_timer); |
1117 | 0 | } |