Line | Count | Source |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.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 <fnmatch.h> |
22 | | #include <limits.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <unistd.h> |
26 | | |
27 | | #include "tmux.h" |
28 | | |
29 | | static int cmd_find_session_better(struct session *, struct session *, |
30 | | int); |
31 | | static struct session *cmd_find_best_session(struct session **, u_int, int); |
32 | | static int cmd_find_best_session_with_window(struct cmd_find_state *); |
33 | | static int cmd_find_best_winlink_with_window(struct cmd_find_state *); |
34 | | |
35 | | static const char *cmd_find_map_table(const char *[][2], const char *); |
36 | | |
37 | | static void cmd_find_log_state(const char *, struct cmd_find_state *); |
38 | | static int cmd_find_get_session(struct cmd_find_state *, const char *); |
39 | | static int cmd_find_get_window(struct cmd_find_state *, const char *, int); |
40 | | static int cmd_find_get_window_with_session(struct cmd_find_state *, |
41 | | const char *); |
42 | | static int cmd_find_get_pane(struct cmd_find_state *, const char *, int); |
43 | | static int cmd_find_get_pane_with_session(struct cmd_find_state *, |
44 | | const char *); |
45 | | static int cmd_find_get_pane_with_window(struct cmd_find_state *, |
46 | | const char *); |
47 | | |
48 | | static const char *cmd_find_session_table[][2] = { |
49 | | { NULL, NULL } |
50 | | }; |
51 | | static const char *cmd_find_window_table[][2] = { |
52 | | { "{start}", "^" }, |
53 | | { "{last}", "!" }, |
54 | | { "{end}", "$" }, |
55 | | { "{next}", "+" }, |
56 | | { "{previous}", "-" }, |
57 | | { NULL, NULL } |
58 | | }; |
59 | | static const char *cmd_find_pane_table[][2] = { |
60 | | { "{last}", "!" }, |
61 | | { "{next}", "+" }, |
62 | | { "{previous}", "-" }, |
63 | | { "{top}", "top" }, |
64 | | { "{bottom}", "bottom" }, |
65 | | { "{left}", "left" }, |
66 | | { "{right}", "right" }, |
67 | | { "{top-left}", "top-left" }, |
68 | | { "{top-right}", "top-right" }, |
69 | | { "{bottom-left}", "bottom-left" }, |
70 | | { "{bottom-right}", "bottom-right" }, |
71 | | { "{up-of}", "{up-of}" }, |
72 | | { "{down-of}", "{down-of}" }, |
73 | | { "{left-of}", "{left-of}" }, |
74 | | { "{right-of}", "{right-of}" }, |
75 | | { NULL, NULL } |
76 | | }; |
77 | | |
78 | | /* Find pane containing client if any. */ |
79 | | static struct window_pane * |
80 | | cmd_find_inside_pane(struct client *c) |
81 | 0 | { |
82 | 0 | struct window_pane *wp; |
83 | 0 | struct environ_entry *envent; |
84 | |
|
85 | 0 | if (c == NULL) |
86 | 0 | return (NULL); |
87 | | |
88 | 0 | RB_FOREACH(wp, window_pane_tree, &all_window_panes) { |
89 | 0 | if (wp->fd != -1 && strcmp(wp->tty, c->ttyname) == 0) |
90 | 0 | break; |
91 | 0 | } |
92 | 0 | if (wp == NULL) { |
93 | 0 | envent = environ_find(c->environ, "TMUX_PANE"); |
94 | 0 | if (envent != NULL) |
95 | 0 | wp = window_pane_find_by_id_str(envent->value); |
96 | 0 | } |
97 | 0 | if (wp != NULL) |
98 | 0 | log_debug("%s: got pane %%%u (%s)", __func__, wp->id, wp->tty); |
99 | 0 | return (wp); |
100 | 0 | } |
101 | | |
102 | | /* Is this client better? */ |
103 | | static int |
104 | | cmd_find_client_better(struct client *c, struct client *than) |
105 | 0 | { |
106 | 0 | if (than == NULL) |
107 | 0 | return (1); |
108 | 0 | return (timercmp(&c->activity_time, &than->activity_time, >)); |
109 | 0 | } |
110 | | |
111 | | /* Find best client for session. */ |
112 | | struct client * |
113 | | cmd_find_best_client(struct session *s) |
114 | 0 | { |
115 | 0 | struct client *c_loop, *c; |
116 | |
|
117 | 0 | if (s->attached == 0) |
118 | 0 | s = NULL; |
119 | |
|
120 | 0 | c = NULL; |
121 | 0 | TAILQ_FOREACH(c_loop, &clients, entry) { |
122 | 0 | if (c_loop->session == NULL) |
123 | 0 | continue; |
124 | 0 | if (s != NULL && c_loop->session != s) |
125 | 0 | continue; |
126 | 0 | if (cmd_find_client_better(c_loop, c)) |
127 | 0 | c = c_loop; |
128 | 0 | } |
129 | 0 | return (c); |
130 | 0 | } |
131 | | |
132 | | /* Is this session better? */ |
133 | | static int |
134 | | cmd_find_session_better(struct session *s, struct session *than, int flags) |
135 | 0 | { |
136 | 0 | int attached; |
137 | |
|
138 | 0 | if (than == NULL) |
139 | 0 | return (1); |
140 | 0 | if (flags & CMD_FIND_PREFER_UNATTACHED) { |
141 | 0 | attached = (than->attached != 0); |
142 | 0 | if (attached && s->attached == 0) |
143 | 0 | return (1); |
144 | 0 | else if (!attached && s->attached != 0) |
145 | 0 | return (0); |
146 | 0 | } |
147 | 0 | return (timercmp(&s->activity_time, &than->activity_time, >)); |
148 | 0 | } |
149 | | |
150 | | /* Can this session be usefully targeted? */ |
151 | | static int |
152 | | cmd_find_session_valid(struct session *s) |
153 | 0 | { |
154 | 0 | if (!session_alive(s) || |
155 | 0 | s->curw == NULL || |
156 | 0 | s->curw->window == NULL || |
157 | 0 | s->curw->window->active == NULL) |
158 | 0 | return (0); |
159 | 0 | return (1); |
160 | 0 | } |
161 | | |
162 | | /* Find best session from a list, or all if list is NULL. */ |
163 | | static struct session * |
164 | | cmd_find_best_session(struct session **slist, u_int ssize, int flags) |
165 | 19.4k | { |
166 | 19.4k | struct session *s_loop, *s; |
167 | 19.4k | u_int i; |
168 | | |
169 | 19.4k | log_debug("%s: %u sessions to try", __func__, ssize); |
170 | | |
171 | 19.4k | s = NULL; |
172 | 19.4k | if (slist != NULL) { |
173 | 0 | for (i = 0; i < ssize; i++) { |
174 | 0 | if (!cmd_find_session_valid(slist[i])) |
175 | 0 | continue; |
176 | 0 | if (cmd_find_session_better(slist[i], s, flags)) |
177 | 0 | s = slist[i]; |
178 | 0 | } |
179 | 19.4k | } else { |
180 | 19.4k | RB_FOREACH(s_loop, sessions, &sessions) { |
181 | 0 | if (!cmd_find_session_valid(s_loop)) |
182 | 0 | continue; |
183 | 0 | if (cmd_find_session_better(s_loop, s, flags)) |
184 | 0 | s = s_loop; |
185 | 0 | } |
186 | 19.4k | } |
187 | 19.4k | return (s); |
188 | 19.4k | } |
189 | | |
190 | | /* Find best session and winlink for window. */ |
191 | | static int |
192 | | cmd_find_best_session_with_window(struct cmd_find_state *fs) |
193 | 4.68k | { |
194 | 4.68k | struct session **slist = NULL; |
195 | 4.68k | u_int ssize; |
196 | 4.68k | struct session *s; |
197 | | |
198 | 4.68k | log_debug("%s: window is @%u", __func__, fs->w->id); |
199 | | |
200 | 4.68k | ssize = 0; |
201 | 4.68k | RB_FOREACH(s, sessions, &sessions) { |
202 | 0 | if (!session_has(s, fs->w)) |
203 | 0 | continue; |
204 | 0 | slist = xreallocarray(slist, ssize + 1, sizeof *slist); |
205 | 0 | slist[ssize++] = s; |
206 | 0 | } |
207 | 4.68k | if (ssize == 0) |
208 | 4.68k | goto fail; |
209 | 0 | fs->s = cmd_find_best_session(slist, ssize, fs->flags); |
210 | 0 | if (fs->s == NULL) |
211 | 0 | goto fail; |
212 | 0 | free(slist); |
213 | 0 | return (cmd_find_best_winlink_with_window(fs)); |
214 | | |
215 | 4.68k | fail: |
216 | 4.68k | free(slist); |
217 | 4.68k | return (-1); |
218 | 0 | } |
219 | | |
220 | | /* |
221 | | * Find the best winlink for a window (the current if it contains the window, |
222 | | * otherwise the first). |
223 | | */ |
224 | | static int |
225 | | cmd_find_best_winlink_with_window(struct cmd_find_state *fs) |
226 | 0 | { |
227 | 0 | struct winlink *wl, *wl_loop; |
228 | |
|
229 | 0 | log_debug("%s: window is @%u", __func__, fs->w->id); |
230 | |
|
231 | 0 | wl = NULL; |
232 | 0 | if (fs->s->curw != NULL && fs->s->curw->window == fs->w) |
233 | 0 | wl = fs->s->curw; |
234 | 0 | else { |
235 | 0 | RB_FOREACH(wl_loop, winlinks, &fs->s->windows) { |
236 | 0 | if (wl_loop->window == fs->w) { |
237 | 0 | wl = wl_loop; |
238 | 0 | break; |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | 0 | if (wl == NULL) |
243 | 0 | return (-1); |
244 | 0 | fs->wl = wl; |
245 | 0 | fs->idx = fs->wl->idx; |
246 | 0 | return (0); |
247 | 0 | } |
248 | | |
249 | | /* Maps string in table. */ |
250 | | static const char * |
251 | | cmd_find_map_table(const char *table[][2], const char *s) |
252 | 0 | { |
253 | 0 | u_int i; |
254 | |
|
255 | 0 | for (i = 0; table[i][0] != NULL; i++) { |
256 | 0 | if (strcmp(s, table[i][0]) == 0) |
257 | 0 | return (table[i][1]); |
258 | 0 | } |
259 | 0 | return (s); |
260 | 0 | } |
261 | | |
262 | | /* Find session from string. Fills in s. */ |
263 | | static int |
264 | | cmd_find_get_session(struct cmd_find_state *fs, const char *session) |
265 | 0 | { |
266 | 0 | struct session *s, *s_loop; |
267 | 0 | struct client *c; |
268 | |
|
269 | 0 | log_debug("%s: %s", __func__, session); |
270 | | |
271 | | /* Check for session ids starting with $. */ |
272 | 0 | if (*session == '$') { |
273 | 0 | fs->s = session_find_by_id_str(session); |
274 | 0 | if (fs->s == NULL) |
275 | 0 | return (-1); |
276 | 0 | return (0); |
277 | 0 | } |
278 | | |
279 | | /* Look for exactly this session. */ |
280 | 0 | fs->s = session_find(session); |
281 | 0 | if (fs->s != NULL) |
282 | 0 | return (0); |
283 | | |
284 | | /* Look for as a client. */ |
285 | 0 | c = cmd_find_client(NULL, session, 1); |
286 | 0 | if (c != NULL && c->session != NULL) { |
287 | 0 | fs->s = c->session; |
288 | 0 | return (0); |
289 | 0 | } |
290 | | |
291 | | /* Stop now if exact only. */ |
292 | 0 | if (fs->flags & CMD_FIND_EXACT_SESSION) |
293 | 0 | return (-1); |
294 | | |
295 | | /* Otherwise look for prefix. */ |
296 | 0 | s = NULL; |
297 | 0 | RB_FOREACH(s_loop, sessions, &sessions) { |
298 | 0 | if (strncmp(session, s_loop->name, strlen(session)) == 0) { |
299 | 0 | if (s != NULL) |
300 | 0 | return (-1); |
301 | 0 | s = s_loop; |
302 | 0 | } |
303 | 0 | } |
304 | 0 | if (s != NULL) { |
305 | 0 | fs->s = s; |
306 | 0 | return (0); |
307 | 0 | } |
308 | | |
309 | | /* Then as a pattern. */ |
310 | 0 | s = NULL; |
311 | 0 | RB_FOREACH(s_loop, sessions, &sessions) { |
312 | 0 | if (fnmatch(session, s_loop->name, 0) == 0) { |
313 | 0 | if (s != NULL) |
314 | 0 | return (-1); |
315 | 0 | s = s_loop; |
316 | 0 | } |
317 | 0 | } |
318 | 0 | if (s != NULL) { |
319 | 0 | fs->s = s; |
320 | 0 | return (0); |
321 | 0 | } |
322 | | |
323 | 0 | return (-1); |
324 | 0 | } |
325 | | |
326 | | /* Find window from string. Fills in s, wl, w. */ |
327 | | static int |
328 | | cmd_find_get_window(struct cmd_find_state *fs, const char *window, int only) |
329 | 0 | { |
330 | 0 | log_debug("%s: %s", __func__, window); |
331 | | |
332 | | /* Check for window ids starting with @. */ |
333 | 0 | if (*window == '@') { |
334 | 0 | fs->w = window_find_by_id_str(window); |
335 | 0 | if (fs->w == NULL) |
336 | 0 | return (-1); |
337 | 0 | return (cmd_find_best_session_with_window(fs)); |
338 | 0 | } |
339 | | |
340 | | /* Not a window id, so use the current session. */ |
341 | 0 | fs->s = fs->current->s; |
342 | | |
343 | | /* We now only need to find the winlink in this session. */ |
344 | 0 | if (cmd_find_get_window_with_session(fs, window) == 0) |
345 | 0 | return (0); |
346 | | |
347 | | /* Otherwise try as a session itself. */ |
348 | 0 | if (!only && cmd_find_get_session(fs, window) == 0) { |
349 | 0 | fs->wl = fs->s->curw; |
350 | 0 | fs->w = fs->wl->window; |
351 | 0 | if (~fs->flags & CMD_FIND_WINDOW_INDEX) |
352 | 0 | fs->idx = fs->wl->idx; |
353 | 0 | return (0); |
354 | 0 | } |
355 | | |
356 | 0 | return (-1); |
357 | 0 | } |
358 | | |
359 | | /* |
360 | | * Find window from string, assuming it is in given session. Needs s, fills in |
361 | | * wl and w. |
362 | | */ |
363 | | static int |
364 | | cmd_find_get_window_with_session(struct cmd_find_state *fs, const char *window) |
365 | 0 | { |
366 | 0 | struct winlink *wl; |
367 | 0 | const char *errstr; |
368 | 0 | int idx, n, exact; |
369 | 0 | struct session *s; |
370 | |
|
371 | 0 | log_debug("%s: %s", __func__, window); |
372 | 0 | exact = (fs->flags & CMD_FIND_EXACT_WINDOW); |
373 | | |
374 | | /* |
375 | | * Start with the current window as the default. So if only an index is |
376 | | * found, the window will be the current. |
377 | | */ |
378 | 0 | fs->wl = fs->s->curw; |
379 | 0 | fs->w = fs->wl->window; |
380 | | |
381 | | /* Check for window ids starting with @. */ |
382 | 0 | if (*window == '@') { |
383 | 0 | fs->w = window_find_by_id_str(window); |
384 | 0 | if (fs->w == NULL || !session_has(fs->s, fs->w)) |
385 | 0 | return (-1); |
386 | 0 | return (cmd_find_best_winlink_with_window(fs)); |
387 | 0 | } |
388 | | |
389 | | /* Try as an offset. */ |
390 | 0 | if (!exact && (window[0] == '+' || window[0] == '-')) { |
391 | 0 | if (window[1] != '\0') |
392 | 0 | n = strtonum(window + 1, 1, INT_MAX, NULL); |
393 | 0 | else |
394 | 0 | n = 1; |
395 | 0 | s = fs->s; |
396 | 0 | if (fs->flags & CMD_FIND_WINDOW_INDEX) { |
397 | 0 | if (window[0] == '+') { |
398 | 0 | if (INT_MAX - s->curw->idx < n) |
399 | 0 | return (-1); |
400 | 0 | fs->idx = s->curw->idx + n; |
401 | 0 | } else { |
402 | 0 | if (n > s->curw->idx) |
403 | 0 | return (-1); |
404 | 0 | fs->idx = s->curw->idx - n; |
405 | 0 | } |
406 | 0 | return (0); |
407 | 0 | } |
408 | 0 | if (window[0] == '+') |
409 | 0 | fs->wl = winlink_next_by_number(s->curw, s, n); |
410 | 0 | else |
411 | 0 | fs->wl = winlink_previous_by_number(s->curw, s, n); |
412 | 0 | if (fs->wl != NULL) { |
413 | 0 | fs->idx = fs->wl->idx; |
414 | 0 | fs->w = fs->wl->window; |
415 | 0 | return (0); |
416 | 0 | } |
417 | 0 | } |
418 | | |
419 | | /* Try special characters. */ |
420 | 0 | if (!exact) { |
421 | 0 | if (strcmp(window, "!") == 0) { |
422 | 0 | fs->wl = TAILQ_FIRST(&fs->s->lastw); |
423 | 0 | if (fs->wl == NULL) |
424 | 0 | return (-1); |
425 | 0 | fs->idx = fs->wl->idx; |
426 | 0 | fs->w = fs->wl->window; |
427 | 0 | return (0); |
428 | 0 | } else if (strcmp(window, "^") == 0) { |
429 | 0 | fs->wl = RB_MIN(winlinks, &fs->s->windows); |
430 | 0 | if (fs->wl == NULL) |
431 | 0 | return (-1); |
432 | 0 | fs->idx = fs->wl->idx; |
433 | 0 | fs->w = fs->wl->window; |
434 | 0 | return (0); |
435 | 0 | } else if (strcmp(window, "$") == 0) { |
436 | 0 | fs->wl = RB_MAX(winlinks, &fs->s->windows); |
437 | 0 | if (fs->wl == NULL) |
438 | 0 | return (-1); |
439 | 0 | fs->idx = fs->wl->idx; |
440 | 0 | fs->w = fs->wl->window; |
441 | 0 | return (0); |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | /* First see if this is a valid window index in this session. */ |
446 | 0 | if (window[0] != '+' && window[0] != '-') { |
447 | 0 | idx = strtonum(window, 0, INT_MAX, &errstr); |
448 | 0 | if (errstr == NULL) { |
449 | 0 | fs->wl = winlink_find_by_index(&fs->s->windows, idx); |
450 | 0 | if (fs->wl != NULL) { |
451 | 0 | fs->idx = fs->wl->idx; |
452 | 0 | fs->w = fs->wl->window; |
453 | 0 | return (0); |
454 | 0 | } |
455 | 0 | if (fs->flags & CMD_FIND_WINDOW_INDEX) { |
456 | 0 | fs->idx = idx; |
457 | 0 | return (0); |
458 | 0 | } |
459 | 0 | } |
460 | 0 | } |
461 | | |
462 | | /* Look for exact matches, error if more than one. */ |
463 | 0 | fs->wl = NULL; |
464 | 0 | RB_FOREACH(wl, winlinks, &fs->s->windows) { |
465 | 0 | if (strcmp(window, wl->window->name) == 0) { |
466 | 0 | if (fs->wl != NULL) |
467 | 0 | return (-1); |
468 | 0 | fs->wl = wl; |
469 | 0 | } |
470 | 0 | } |
471 | 0 | if (fs->wl != NULL) { |
472 | 0 | fs->idx = fs->wl->idx; |
473 | 0 | fs->w = fs->wl->window; |
474 | 0 | return (0); |
475 | 0 | } |
476 | | |
477 | | /* Stop now if exact only. */ |
478 | 0 | if (exact) |
479 | 0 | return (-1); |
480 | | |
481 | | /* Try as the start of a window name, error if multiple. */ |
482 | 0 | fs->wl = NULL; |
483 | 0 | RB_FOREACH(wl, winlinks, &fs->s->windows) { |
484 | 0 | if (strncmp(window, wl->window->name, strlen(window)) == 0) { |
485 | 0 | if (fs->wl != NULL) |
486 | 0 | return (-1); |
487 | 0 | fs->wl = wl; |
488 | 0 | } |
489 | 0 | } |
490 | 0 | if (fs->wl != NULL) { |
491 | 0 | fs->idx = fs->wl->idx; |
492 | 0 | fs->w = fs->wl->window; |
493 | 0 | return (0); |
494 | 0 | } |
495 | | |
496 | | /* Now look for pattern matches, again error if multiple. */ |
497 | 0 | fs->wl = NULL; |
498 | 0 | RB_FOREACH(wl, winlinks, &fs->s->windows) { |
499 | 0 | if (fnmatch(window, wl->window->name, 0) == 0) { |
500 | 0 | if (fs->wl != NULL) |
501 | 0 | return (-1); |
502 | 0 | fs->wl = wl; |
503 | 0 | } |
504 | 0 | } |
505 | 0 | if (fs->wl != NULL) { |
506 | 0 | fs->idx = fs->wl->idx; |
507 | 0 | fs->w = fs->wl->window; |
508 | 0 | return (0); |
509 | 0 | } |
510 | | |
511 | 0 | return (-1); |
512 | 0 | } |
513 | | |
514 | | /* Find pane from string. Fills in s, wl, w, wp. */ |
515 | | static int |
516 | | cmd_find_get_pane(struct cmd_find_state *fs, const char *pane, int only) |
517 | 0 | { |
518 | 0 | log_debug("%s: %s", __func__, pane); |
519 | | |
520 | | /* Check for pane ids starting with %. */ |
521 | 0 | if (*pane == '%') { |
522 | 0 | fs->wp = window_pane_find_by_id_str(pane); |
523 | 0 | if (fs->wp == NULL) |
524 | 0 | return (-1); |
525 | 0 | fs->w = fs->wp->window; |
526 | 0 | return (cmd_find_best_session_with_window(fs)); |
527 | 0 | } |
528 | | |
529 | | /* Not a pane id, so try the current session and window. */ |
530 | 0 | fs->s = fs->current->s; |
531 | 0 | fs->wl = fs->current->wl; |
532 | 0 | fs->idx = fs->current->idx; |
533 | 0 | fs->w = fs->current->w; |
534 | | |
535 | | /* We now only need to find the pane in this window. */ |
536 | 0 | if (cmd_find_get_pane_with_window(fs, pane) == 0) |
537 | 0 | return (0); |
538 | | |
539 | | /* Otherwise try as a window itself (this will also try as session). */ |
540 | 0 | if (!only && cmd_find_get_window(fs, pane, 0) == 0) { |
541 | 0 | fs->wp = fs->w->active; |
542 | 0 | return (0); |
543 | 0 | } |
544 | | |
545 | 0 | return (-1); |
546 | 0 | } |
547 | | |
548 | | /* |
549 | | * Find pane from string, assuming it is in given session. Needs s, fills in wl |
550 | | * and w and wp. |
551 | | */ |
552 | | static int |
553 | | cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane) |
554 | 0 | { |
555 | 0 | log_debug("%s: %s", __func__, pane); |
556 | | |
557 | | /* Check for pane ids starting with %. */ |
558 | 0 | if (*pane == '%') { |
559 | 0 | fs->wp = window_pane_find_by_id_str(pane); |
560 | 0 | if (fs->wp == NULL) |
561 | 0 | return (-1); |
562 | 0 | fs->w = fs->wp->window; |
563 | 0 | return (cmd_find_best_winlink_with_window(fs)); |
564 | 0 | } |
565 | | |
566 | | /* Otherwise use the current window. */ |
567 | 0 | fs->wl = fs->s->curw; |
568 | 0 | fs->idx = fs->wl->idx; |
569 | 0 | fs->w = fs->wl->window; |
570 | | |
571 | | /* Now we just need to look up the pane. */ |
572 | 0 | return (cmd_find_get_pane_with_window(fs, pane)); |
573 | 0 | } |
574 | | |
575 | | /* |
576 | | * Find pane from string, assuming it is in the given window. Needs w, fills in |
577 | | * wp. |
578 | | */ |
579 | | static int |
580 | | cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane) |
581 | 0 | { |
582 | 0 | const char *errstr; |
583 | 0 | int idx; |
584 | 0 | struct window_pane *wp; |
585 | 0 | u_int n; |
586 | |
|
587 | 0 | log_debug("%s: %s", __func__, pane); |
588 | | |
589 | | /* Check for pane ids starting with %. */ |
590 | 0 | if (*pane == '%') { |
591 | 0 | fs->wp = window_pane_find_by_id_str(pane); |
592 | 0 | if (fs->wp == NULL) |
593 | 0 | return (-1); |
594 | 0 | if (fs->wp->window != fs->w) |
595 | 0 | return (-1); |
596 | 0 | return (0); |
597 | 0 | } |
598 | | |
599 | | /* Try special characters. */ |
600 | 0 | if (strcmp(pane, "!") == 0) { |
601 | 0 | fs->wp = TAILQ_FIRST(&fs->w->last_panes); |
602 | 0 | if (fs->wp == NULL) |
603 | 0 | return (-1); |
604 | 0 | return (0); |
605 | 0 | } else if (strcmp(pane, "{up-of}") == 0) { |
606 | 0 | fs->wp = window_pane_find_up(fs->w->active); |
607 | 0 | if (fs->wp == NULL) |
608 | 0 | return (-1); |
609 | 0 | return (0); |
610 | 0 | } else if (strcmp(pane, "{down-of}") == 0) { |
611 | 0 | fs->wp = window_pane_find_down(fs->w->active); |
612 | 0 | if (fs->wp == NULL) |
613 | 0 | return (-1); |
614 | 0 | return (0); |
615 | 0 | } else if (strcmp(pane, "{left-of}") == 0) { |
616 | 0 | fs->wp = window_pane_find_left(fs->w->active); |
617 | 0 | if (fs->wp == NULL) |
618 | 0 | return (-1); |
619 | 0 | return (0); |
620 | 0 | } else if (strcmp(pane, "{right-of}") == 0) { |
621 | 0 | fs->wp = window_pane_find_right(fs->w->active); |
622 | 0 | if (fs->wp == NULL) |
623 | 0 | return (-1); |
624 | 0 | return (0); |
625 | 0 | } |
626 | | |
627 | | /* Try as an offset. */ |
628 | 0 | if (pane[0] == '+' || pane[0] == '-') { |
629 | 0 | if (pane[1] != '\0') |
630 | 0 | n = strtonum(pane + 1, 1, INT_MAX, NULL); |
631 | 0 | else |
632 | 0 | n = 1; |
633 | 0 | wp = fs->w->active; |
634 | 0 | if (pane[0] == '+') |
635 | 0 | fs->wp = window_pane_next_by_number(fs->w, wp, n); |
636 | 0 | else |
637 | 0 | fs->wp = window_pane_previous_by_number(fs->w, wp, n); |
638 | 0 | if (fs->wp != NULL) |
639 | 0 | return (0); |
640 | 0 | } |
641 | | |
642 | | /* Get pane by index. */ |
643 | 0 | idx = strtonum(pane, 0, INT_MAX, &errstr); |
644 | 0 | if (errstr == NULL) { |
645 | 0 | fs->wp = window_pane_at_index(fs->w, idx); |
646 | 0 | if (fs->wp != NULL) |
647 | 0 | return (0); |
648 | 0 | } |
649 | | |
650 | | /* Try as a description. */ |
651 | 0 | fs->wp = window_find_string(fs->w, pane); |
652 | 0 | if (fs->wp != NULL) |
653 | 0 | return (0); |
654 | | |
655 | 0 | return (-1); |
656 | 0 | } |
657 | | |
658 | | /* Clear state. */ |
659 | | void |
660 | | cmd_find_clear_state(struct cmd_find_state *fs, int flags) |
661 | 62.7k | { |
662 | 62.7k | memset(fs, 0, sizeof *fs); |
663 | | |
664 | 62.7k | fs->flags = flags; |
665 | | |
666 | 62.7k | fs->idx = -1; |
667 | 62.7k | } |
668 | | |
669 | | /* Check if state is empty. */ |
670 | | int |
671 | | cmd_find_empty_state(struct cmd_find_state *fs) |
672 | 6.38k | { |
673 | 6.38k | if (fs->s == NULL && fs->wl == NULL && fs->w == NULL && fs->wp == NULL) |
674 | 6.38k | return (1); |
675 | 0 | return (0); |
676 | 6.38k | } |
677 | | |
678 | | /* Check if a state if valid. */ |
679 | | int |
680 | | cmd_find_valid_state(struct cmd_find_state *fs) |
681 | 13.0k | { |
682 | 13.0k | struct winlink *wl; |
683 | | |
684 | 13.0k | if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL) |
685 | 13.0k | return (0); |
686 | | |
687 | 0 | if (!session_alive(fs->s)) |
688 | 0 | return (0); |
689 | | |
690 | 0 | RB_FOREACH(wl, winlinks, &fs->s->windows) { |
691 | 0 | if (wl->window == fs->w && wl == fs->wl) |
692 | 0 | break; |
693 | 0 | } |
694 | 0 | if (wl == NULL) |
695 | 0 | return (0); |
696 | | |
697 | 0 | if (fs->w != fs->wl->window) |
698 | 0 | return (0); |
699 | | |
700 | 0 | return (window_has_pane(fs->w, fs->wp)); |
701 | 0 | } |
702 | | |
703 | | /* Copy a state. */ |
704 | | void |
705 | | cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src) |
706 | 6.38k | { |
707 | 6.38k | dst->s = src->s; |
708 | 6.38k | dst->wl = src->wl; |
709 | 6.38k | dst->idx = src->idx; |
710 | 6.38k | dst->w = src->w; |
711 | 6.38k | dst->wp = src->wp; |
712 | 6.38k | } |
713 | | |
714 | | /* Log the result. */ |
715 | | static void |
716 | | cmd_find_log_state(const char *prefix, struct cmd_find_state *fs) |
717 | 0 | { |
718 | 0 | if (fs->s != NULL) |
719 | 0 | log_debug("%s: s=$%u %s", prefix, fs->s->id, fs->s->name); |
720 | 0 | else |
721 | 0 | log_debug("%s: s=none", prefix); |
722 | 0 | if (fs->wl != NULL) { |
723 | 0 | log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx, |
724 | 0 | fs->wl->window == fs->w, fs->w->id, fs->w->name); |
725 | 0 | } else |
726 | 0 | log_debug("%s: wl=none", prefix); |
727 | 0 | if (fs->wp != NULL) |
728 | 0 | log_debug("%s: wp=%%%u", prefix, fs->wp->id); |
729 | 0 | else |
730 | 0 | log_debug("%s: wp=none", prefix); |
731 | 0 | if (fs->idx != -1) |
732 | 0 | log_debug("%s: idx=%d", prefix, fs->idx); |
733 | 0 | else |
734 | 0 | log_debug("%s: idx=none", prefix); |
735 | 0 | } |
736 | | |
737 | | /* Find state from a session. */ |
738 | | void |
739 | | cmd_find_from_session(struct cmd_find_state *fs, struct session *s, int flags) |
740 | 0 | { |
741 | 0 | cmd_find_clear_state(fs, flags); |
742 | |
|
743 | 0 | fs->s = s; |
744 | 0 | fs->wl = fs->s->curw; |
745 | 0 | fs->w = fs->wl->window; |
746 | 0 | fs->wp = fs->w->active; |
747 | |
|
748 | 0 | cmd_find_log_state(__func__, fs); |
749 | 0 | } |
750 | | |
751 | | /* Find state from a winlink. */ |
752 | | void |
753 | | cmd_find_from_winlink(struct cmd_find_state *fs, struct winlink *wl, int flags) |
754 | 0 | { |
755 | 0 | cmd_find_clear_state(fs, flags); |
756 | |
|
757 | 0 | fs->s = wl->session; |
758 | 0 | fs->wl = wl; |
759 | 0 | fs->w = wl->window; |
760 | 0 | fs->wp = wl->window->active; |
761 | |
|
762 | 0 | cmd_find_log_state(__func__, fs); |
763 | 0 | } |
764 | | |
765 | | /* Find state from a session and window. */ |
766 | | int |
767 | | cmd_find_from_session_window(struct cmd_find_state *fs, struct session *s, |
768 | | struct window *w, int flags) |
769 | 0 | { |
770 | 0 | cmd_find_clear_state(fs, flags); |
771 | |
|
772 | 0 | fs->s = s; |
773 | 0 | fs->w = w; |
774 | 0 | if (cmd_find_best_winlink_with_window(fs) != 0) { |
775 | 0 | cmd_find_clear_state(fs, flags); |
776 | 0 | return (-1); |
777 | 0 | } |
778 | 0 | fs->wp = fs->w->active; |
779 | |
|
780 | 0 | cmd_find_log_state(__func__, fs); |
781 | 0 | return (0); |
782 | 0 | } |
783 | | |
784 | | /* Find state from a window. */ |
785 | | int |
786 | | cmd_find_from_window(struct cmd_find_state *fs, struct window *w, int flags) |
787 | 4.68k | { |
788 | 4.68k | cmd_find_clear_state(fs, flags); |
789 | | |
790 | 4.68k | fs->w = w; |
791 | 4.68k | if (cmd_find_best_session_with_window(fs) != 0) { |
792 | 4.68k | cmd_find_clear_state(fs, flags); |
793 | 4.68k | return (-1); |
794 | 4.68k | } |
795 | 0 | if (cmd_find_best_winlink_with_window(fs) != 0) { |
796 | 0 | cmd_find_clear_state(fs, flags); |
797 | 0 | return (-1); |
798 | 0 | } |
799 | 0 | fs->wp = fs->w->active; |
800 | |
|
801 | 0 | cmd_find_log_state(__func__, fs); |
802 | 0 | return (0); |
803 | 0 | } |
804 | | |
805 | | /* Find state from a winlink and pane. */ |
806 | | void |
807 | | cmd_find_from_winlink_pane(struct cmd_find_state *fs, struct winlink *wl, |
808 | | struct window_pane *wp, int flags) |
809 | 0 | { |
810 | 0 | cmd_find_clear_state(fs, flags); |
811 | |
|
812 | 0 | fs->s = wl->session; |
813 | 0 | fs->wl = wl; |
814 | 0 | fs->idx = fs->wl->idx; |
815 | 0 | fs->w = fs->wl->window; |
816 | 0 | fs->wp = wp; |
817 | |
|
818 | 0 | cmd_find_log_state(__func__, fs); |
819 | 0 | } |
820 | | |
821 | | /* Find state from a pane. */ |
822 | | int |
823 | | cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp, int flags) |
824 | 3.25k | { |
825 | 3.25k | if (cmd_find_from_window(fs, wp->window, flags) != 0) |
826 | 3.25k | return (-1); |
827 | 0 | fs->wp = wp; |
828 | |
|
829 | 0 | cmd_find_log_state(__func__, fs); |
830 | 0 | return (0); |
831 | 3.25k | } |
832 | | |
833 | | /* Find state from nothing. */ |
834 | | int |
835 | | cmd_find_from_nothing(struct cmd_find_state *fs, int flags) |
836 | 19.4k | { |
837 | 19.4k | cmd_find_clear_state(fs, flags); |
838 | | |
839 | 19.4k | fs->s = cmd_find_best_session(NULL, 0, flags); |
840 | 19.4k | if (fs->s == NULL) { |
841 | 19.4k | cmd_find_clear_state(fs, flags); |
842 | 19.4k | return (-1); |
843 | 19.4k | } |
844 | 0 | fs->wl = fs->s->curw; |
845 | 0 | fs->idx = fs->wl->idx; |
846 | 0 | fs->w = fs->wl->window; |
847 | 0 | fs->wp = fs->w->active; |
848 | |
|
849 | 0 | cmd_find_log_state(__func__, fs); |
850 | 0 | return (0); |
851 | 19.4k | } |
852 | | |
853 | | /* Find state from mouse. */ |
854 | | int |
855 | | cmd_find_from_mouse(struct cmd_find_state *fs, struct mouse_event *m, int flags) |
856 | 0 | { |
857 | 0 | cmd_find_clear_state(fs, flags); |
858 | |
|
859 | 0 | if (!m->valid) |
860 | 0 | return (-1); |
861 | | |
862 | 0 | fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl); |
863 | 0 | if (fs->wp == NULL) { |
864 | 0 | cmd_find_clear_state(fs, flags); |
865 | 0 | return (-1); |
866 | 0 | } |
867 | 0 | fs->w = fs->wl->window; |
868 | |
|
869 | 0 | cmd_find_log_state(__func__, fs); |
870 | 0 | return (0); |
871 | 0 | } |
872 | | |
873 | | /* Find state from client. */ |
874 | | int |
875 | | cmd_find_from_client(struct cmd_find_state *fs, struct client *c, int flags) |
876 | 13.0k | { |
877 | 13.0k | struct window_pane *wp; |
878 | | |
879 | | /* If no client, treat as from nothing. */ |
880 | 13.0k | if (c == NULL) |
881 | 13.0k | return (cmd_find_from_nothing(fs, flags)); |
882 | | |
883 | | /* If this is an attached client, all done. */ |
884 | 0 | if (c->session != NULL) { |
885 | 0 | cmd_find_clear_state(fs, flags); |
886 | |
|
887 | 0 | fs->wp = server_client_get_pane(c); |
888 | 0 | if (fs->wp == NULL) { |
889 | 0 | cmd_find_from_session(fs, c->session, flags); |
890 | 0 | return (0); |
891 | 0 | } |
892 | 0 | fs->s = c->session; |
893 | 0 | fs->wl = fs->s->curw; |
894 | 0 | fs->w = fs->wl->window; |
895 | |
|
896 | 0 | cmd_find_log_state(__func__, fs); |
897 | 0 | return (0); |
898 | 0 | } |
899 | 0 | cmd_find_clear_state(fs, flags); |
900 | | |
901 | | /* |
902 | | * If this is an unattached client running in a pane, we can use that |
903 | | * to limit the list of sessions to those containing that pane. |
904 | | */ |
905 | 0 | wp = cmd_find_inside_pane(c); |
906 | 0 | if (wp == NULL) |
907 | 0 | goto unknown_pane; |
908 | | |
909 | | /* |
910 | | * Don't have a session, or it doesn't have this pane. Try all |
911 | | * sessions. |
912 | | */ |
913 | 0 | fs->w = wp->window; |
914 | 0 | if (cmd_find_best_session_with_window(fs) != 0) { |
915 | | /* |
916 | | * The window may have been destroyed but the pane |
917 | | * still on all_window_panes due to something else |
918 | | * holding a reference. |
919 | | */ |
920 | 0 | goto unknown_pane; |
921 | 0 | } |
922 | 0 | fs->wl = fs->s->curw; |
923 | 0 | fs->w = fs->wl->window; |
924 | 0 | fs->wp = fs->w->active; /* use active pane */ |
925 | |
|
926 | 0 | cmd_find_log_state(__func__, fs); |
927 | 0 | return (0); |
928 | | |
929 | 0 | unknown_pane: |
930 | | /* We can't find the pane so need to guess. */ |
931 | 0 | return (cmd_find_from_nothing(fs, flags)); |
932 | 0 | } |
933 | | |
934 | | /* |
935 | | * Split target into pieces and resolve for the given type. Fills in the given |
936 | | * state. Returns 0 on success or -1 on error. |
937 | | */ |
938 | | int |
939 | | cmd_find_target(struct cmd_find_state *fs, struct cmdq_item *item, |
940 | | const char *target, enum cmd_find_type type, int flags) |
941 | 0 | { |
942 | 0 | struct mouse_event *m; |
943 | 0 | struct client *c; |
944 | 0 | struct cmd_find_state current; |
945 | 0 | char *colon, *period, *copy = NULL, tmp[256]; |
946 | 0 | const char *session, *window, *pane, *s; |
947 | 0 | int window_only = 0, pane_only = 0; |
948 | | |
949 | | /* Can fail flag implies quiet. */ |
950 | 0 | if (flags & CMD_FIND_CANFAIL) |
951 | 0 | flags |= CMD_FIND_QUIET; |
952 | | |
953 | | /* Log the arguments. */ |
954 | 0 | if (type == CMD_FIND_PANE) |
955 | 0 | s = "pane"; |
956 | 0 | else if (type == CMD_FIND_WINDOW) |
957 | 0 | s = "window"; |
958 | 0 | else if (type == CMD_FIND_SESSION) |
959 | 0 | s = "session"; |
960 | 0 | else |
961 | 0 | s = "unknown"; |
962 | 0 | *tmp = '\0'; |
963 | 0 | if (flags & CMD_FIND_PREFER_UNATTACHED) |
964 | 0 | strlcat(tmp, "PREFER_UNATTACHED,", sizeof tmp); |
965 | 0 | if (flags & CMD_FIND_QUIET) |
966 | 0 | strlcat(tmp, "QUIET,", sizeof tmp); |
967 | 0 | if (flags & CMD_FIND_WINDOW_INDEX) |
968 | 0 | strlcat(tmp, "WINDOW_INDEX,", sizeof tmp); |
969 | 0 | if (flags & CMD_FIND_DEFAULT_MARKED) |
970 | 0 | strlcat(tmp, "DEFAULT_MARKED,", sizeof tmp); |
971 | 0 | if (flags & CMD_FIND_EXACT_SESSION) |
972 | 0 | strlcat(tmp, "EXACT_SESSION,", sizeof tmp); |
973 | 0 | if (flags & CMD_FIND_EXACT_WINDOW) |
974 | 0 | strlcat(tmp, "EXACT_WINDOW,", sizeof tmp); |
975 | 0 | if (flags & CMD_FIND_CANFAIL) |
976 | 0 | strlcat(tmp, "CANFAIL,", sizeof tmp); |
977 | 0 | if (*tmp != '\0') |
978 | 0 | tmp[strlen(tmp) - 1] = '\0'; |
979 | 0 | else |
980 | 0 | strlcat(tmp, "NONE", sizeof tmp); |
981 | 0 | log_debug("%s: target %s, type %s, item %p, flags %s", __func__, |
982 | 0 | target == NULL ? "none" : target, s, item, tmp); |
983 | | |
984 | | /* Clear new state. */ |
985 | 0 | cmd_find_clear_state(fs, flags); |
986 | | |
987 | | /* Find current state. */ |
988 | 0 | if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) { |
989 | 0 | fs->current = &marked_pane; |
990 | 0 | log_debug("%s: current is marked pane", __func__); |
991 | 0 | } else if (cmd_find_valid_state(cmdq_get_current(item))) { |
992 | 0 | fs->current = cmdq_get_current(item); |
993 | 0 | log_debug("%s: current is from queue", __func__); |
994 | 0 | } else if (cmd_find_from_client(¤t, cmdq_get_client(item), |
995 | 0 | flags) == 0) { |
996 | 0 | fs->current = ¤t; |
997 | 0 | log_debug("%s: current is from client", __func__); |
998 | 0 | } else { |
999 | 0 | if (~flags & CMD_FIND_QUIET) |
1000 | 0 | cmdq_error(item, "no current target"); |
1001 | 0 | goto error; |
1002 | 0 | } |
1003 | 0 | if (!cmd_find_valid_state(fs->current)) |
1004 | 0 | fatalx("invalid current find state"); |
1005 | | |
1006 | | /* An empty or NULL target is the current. */ |
1007 | 0 | if (target == NULL || *target == '\0') |
1008 | 0 | goto current; |
1009 | | |
1010 | 0 | if (strcmp(target, "@") == 0 || |
1011 | 0 | strcmp(target, "{active}") == 0 || |
1012 | 0 | strcmp(target, "{current}") == 0) { |
1013 | 0 | c = cmdq_get_client(item); |
1014 | 0 | if (c == NULL || c->session == NULL) { |
1015 | 0 | cmdq_error(item, "no current client"); |
1016 | 0 | goto error; |
1017 | 0 | } |
1018 | 0 | fs->wl = c->session->curw; |
1019 | 0 | fs->wp = c->session->curw->window->active; |
1020 | 0 | fs->w = c->session->curw->window; |
1021 | 0 | goto found; |
1022 | 0 | } |
1023 | | |
1024 | | /* Mouse target is a plain = or {mouse}. */ |
1025 | 0 | if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) { |
1026 | 0 | m = &cmdq_get_event(item)->m; |
1027 | 0 | switch (type) { |
1028 | 0 | case CMD_FIND_PANE: |
1029 | 0 | fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl); |
1030 | 0 | if (fs->wp != NULL) { |
1031 | 0 | fs->w = fs->wl->window; |
1032 | 0 | break; |
1033 | 0 | } |
1034 | | /* FALLTHROUGH */ |
1035 | 0 | case CMD_FIND_WINDOW: |
1036 | 0 | case CMD_FIND_SESSION: |
1037 | 0 | fs->wl = cmd_mouse_window(m, &fs->s); |
1038 | 0 | if (fs->wl == NULL && fs->s != NULL) |
1039 | 0 | fs->wl = fs->s->curw; |
1040 | 0 | if (fs->wl != NULL) { |
1041 | 0 | fs->w = fs->wl->window; |
1042 | 0 | fs->wp = fs->w->active; |
1043 | 0 | } |
1044 | 0 | break; |
1045 | 0 | } |
1046 | 0 | if (fs->wp == NULL) { |
1047 | 0 | if (~flags & CMD_FIND_QUIET) |
1048 | 0 | cmdq_error(item, "no mouse target"); |
1049 | 0 | goto error; |
1050 | 0 | } |
1051 | 0 | goto found; |
1052 | 0 | } |
1053 | | |
1054 | | /* Marked target is a plain ~ or {marked}. */ |
1055 | 0 | if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) { |
1056 | 0 | if (!server_check_marked()) { |
1057 | 0 | if (~flags & CMD_FIND_QUIET) |
1058 | 0 | cmdq_error(item, "no marked target"); |
1059 | 0 | goto error; |
1060 | 0 | } |
1061 | 0 | cmd_find_copy_state(fs, &marked_pane); |
1062 | 0 | goto found; |
1063 | 0 | } |
1064 | | |
1065 | | /* Find separators if they exist. */ |
1066 | 0 | copy = xstrdup(target); |
1067 | 0 | colon = strchr(copy, ':'); |
1068 | 0 | if (colon != NULL) |
1069 | 0 | *colon++ = '\0'; |
1070 | 0 | if (colon == NULL) |
1071 | 0 | period = strchr(copy, '.'); |
1072 | 0 | else |
1073 | 0 | period = strchr(colon, '.'); |
1074 | 0 | if (period != NULL) |
1075 | 0 | *period++ = '\0'; |
1076 | | |
1077 | | /* Set session, window and pane parts. */ |
1078 | 0 | session = window = pane = NULL; |
1079 | 0 | if (colon != NULL && period != NULL) { |
1080 | 0 | session = copy; |
1081 | 0 | window = colon; |
1082 | 0 | window_only = 1; |
1083 | 0 | pane = period; |
1084 | 0 | pane_only = 1; |
1085 | 0 | } else if (colon != NULL && period == NULL) { |
1086 | 0 | session = copy; |
1087 | 0 | window = colon; |
1088 | 0 | window_only = 1; |
1089 | 0 | } else if (colon == NULL && period != NULL) { |
1090 | 0 | window = copy; |
1091 | 0 | pane = period; |
1092 | 0 | pane_only = 1; |
1093 | 0 | } else { |
1094 | 0 | if (*copy == '$') |
1095 | 0 | session = copy; |
1096 | 0 | else if (*copy == '@') |
1097 | 0 | window = copy; |
1098 | 0 | else if (*copy == '%') |
1099 | 0 | pane = copy; |
1100 | 0 | else { |
1101 | 0 | switch (type) { |
1102 | 0 | case CMD_FIND_SESSION: |
1103 | 0 | session = copy; |
1104 | 0 | break; |
1105 | 0 | case CMD_FIND_WINDOW: |
1106 | 0 | window = copy; |
1107 | 0 | break; |
1108 | 0 | case CMD_FIND_PANE: |
1109 | 0 | pane = copy; |
1110 | 0 | break; |
1111 | 0 | } |
1112 | 0 | } |
1113 | 0 | } |
1114 | | |
1115 | | /* Set exact match flags. */ |
1116 | 0 | if (session != NULL && *session == '=') { |
1117 | 0 | session++; |
1118 | 0 | fs->flags |= CMD_FIND_EXACT_SESSION; |
1119 | 0 | } |
1120 | 0 | if (window != NULL && *window == '=') { |
1121 | 0 | window++; |
1122 | 0 | fs->flags |= CMD_FIND_EXACT_WINDOW; |
1123 | 0 | } |
1124 | | |
1125 | | /* Empty is the same as NULL. */ |
1126 | 0 | if (session != NULL && *session == '\0') |
1127 | 0 | session = NULL; |
1128 | 0 | if (window != NULL && *window == '\0') |
1129 | 0 | window = NULL; |
1130 | 0 | if (pane != NULL && *pane == '\0') |
1131 | 0 | pane = NULL; |
1132 | | |
1133 | | /* Map though conversion table. */ |
1134 | 0 | if (session != NULL) |
1135 | 0 | session = cmd_find_map_table(cmd_find_session_table, session); |
1136 | 0 | if (window != NULL) |
1137 | 0 | window = cmd_find_map_table(cmd_find_window_table, window); |
1138 | 0 | if (pane != NULL) |
1139 | 0 | pane = cmd_find_map_table(cmd_find_pane_table, pane); |
1140 | |
|
1141 | 0 | if (session != NULL || window != NULL || pane != NULL) { |
1142 | 0 | log_debug("%s: target %s is %s%s%s%s%s%s", |
1143 | 0 | __func__, target, |
1144 | 0 | session == NULL ? "" : "session ", |
1145 | 0 | session == NULL ? "" : session, |
1146 | 0 | window == NULL ? "" : "window ", |
1147 | 0 | window == NULL ? "" : window, |
1148 | 0 | pane == NULL ? "" : "pane ", |
1149 | 0 | pane == NULL ? "" : pane); |
1150 | 0 | } |
1151 | | |
1152 | | /* No pane is allowed if want an index. */ |
1153 | 0 | if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) { |
1154 | 0 | if (~flags & CMD_FIND_QUIET) |
1155 | 0 | cmdq_error(item, "can't specify pane here"); |
1156 | 0 | goto error; |
1157 | 0 | } |
1158 | | |
1159 | | /* If the session isn't NULL, look it up. */ |
1160 | 0 | if (session != NULL) { |
1161 | | /* This will fill in session. */ |
1162 | 0 | if (cmd_find_get_session(fs, session) != 0) |
1163 | 0 | goto no_session; |
1164 | | |
1165 | | /* If window and pane are NULL, use that session's current. */ |
1166 | 0 | if (window == NULL && pane == NULL) { |
1167 | 0 | fs->wl = fs->s->curw; |
1168 | 0 | fs->idx = -1; |
1169 | 0 | fs->w = fs->wl->window; |
1170 | 0 | fs->wp = fs->w->active; |
1171 | 0 | goto found; |
1172 | 0 | } |
1173 | | |
1174 | | /* If window is present but pane not, find window in session. */ |
1175 | 0 | if (window != NULL && pane == NULL) { |
1176 | | /* This will fill in winlink and window. */ |
1177 | 0 | if (cmd_find_get_window_with_session(fs, window) != 0) |
1178 | 0 | goto no_window; |
1179 | 0 | if (fs->wl != NULL) /* can be NULL if index only */ |
1180 | 0 | fs->wp = fs->wl->window->active; |
1181 | 0 | goto found; |
1182 | 0 | } |
1183 | | |
1184 | | /* If pane is present but window not, find pane. */ |
1185 | 0 | if (window == NULL && pane != NULL) { |
1186 | | /* This will fill in winlink and window and pane. */ |
1187 | 0 | if (cmd_find_get_pane_with_session(fs, pane) != 0) |
1188 | 0 | goto no_pane; |
1189 | 0 | goto found; |
1190 | 0 | } |
1191 | | |
1192 | | /* |
1193 | | * If window and pane are present, find both in session. This |
1194 | | * will fill in winlink and window. |
1195 | | */ |
1196 | 0 | if (cmd_find_get_window_with_session(fs, window) != 0) |
1197 | 0 | goto no_window; |
1198 | | /* This will fill in pane. */ |
1199 | 0 | if (cmd_find_get_pane_with_window(fs, pane) != 0) |
1200 | 0 | goto no_pane; |
1201 | 0 | goto found; |
1202 | 0 | } |
1203 | | |
1204 | | /* No session. If window and pane, try them. */ |
1205 | 0 | if (window != NULL && pane != NULL) { |
1206 | | /* This will fill in session, winlink and window. */ |
1207 | 0 | if (cmd_find_get_window(fs, window, window_only) != 0) |
1208 | 0 | goto no_window; |
1209 | | /* This will fill in pane. */ |
1210 | 0 | if (cmd_find_get_pane_with_window(fs, pane) != 0) |
1211 | 0 | goto no_pane; |
1212 | 0 | goto found; |
1213 | 0 | } |
1214 | | |
1215 | | /* If just window is present, try it. */ |
1216 | 0 | if (window != NULL && pane == NULL) { |
1217 | | /* This will fill in session, winlink and window. */ |
1218 | 0 | if (cmd_find_get_window(fs, window, window_only) != 0) |
1219 | 0 | goto no_window; |
1220 | 0 | if (fs->wl != NULL) /* can be NULL if index only */ |
1221 | 0 | fs->wp = fs->wl->window->active; |
1222 | 0 | goto found; |
1223 | 0 | } |
1224 | | |
1225 | | /* If just pane is present, try it. */ |
1226 | 0 | if (window == NULL && pane != NULL) { |
1227 | | /* This will fill in session, winlink, window and pane. */ |
1228 | 0 | if (cmd_find_get_pane(fs, pane, pane_only) != 0) |
1229 | 0 | goto no_pane; |
1230 | 0 | goto found; |
1231 | 0 | } |
1232 | | |
1233 | 0 | current: |
1234 | | /* Use the current session. */ |
1235 | 0 | cmd_find_copy_state(fs, fs->current); |
1236 | 0 | if (flags & CMD_FIND_WINDOW_INDEX) |
1237 | 0 | fs->idx = -1; |
1238 | 0 | goto found; |
1239 | | |
1240 | 0 | error: |
1241 | 0 | fs->current = NULL; |
1242 | 0 | log_debug("%s: error", __func__); |
1243 | |
|
1244 | 0 | free(copy); |
1245 | 0 | if (flags & CMD_FIND_CANFAIL) |
1246 | 0 | return (0); |
1247 | 0 | return (-1); |
1248 | | |
1249 | 0 | found: |
1250 | 0 | fs->current = NULL; |
1251 | 0 | cmd_find_log_state(__func__, fs); |
1252 | |
|
1253 | 0 | free(copy); |
1254 | 0 | return (0); |
1255 | | |
1256 | 0 | no_session: |
1257 | 0 | if (~flags & CMD_FIND_QUIET) |
1258 | 0 | cmdq_error(item, "can't find session: %s", session); |
1259 | 0 | goto error; |
1260 | | |
1261 | 0 | no_window: |
1262 | 0 | if (~flags & CMD_FIND_QUIET) |
1263 | 0 | cmdq_error(item, "can't find window: %s", window); |
1264 | 0 | goto error; |
1265 | | |
1266 | 0 | no_pane: |
1267 | 0 | if (~flags & CMD_FIND_QUIET) |
1268 | 0 | cmdq_error(item, "can't find pane: %s", pane); |
1269 | 0 | goto error; |
1270 | 0 | } |
1271 | | |
1272 | | /* Find the current client. */ |
1273 | | static struct client * |
1274 | | cmd_find_current_client(struct cmdq_item *item, int quiet) |
1275 | 0 | { |
1276 | 0 | struct client *c = NULL, *found; |
1277 | 0 | struct session *s; |
1278 | 0 | struct window_pane *wp; |
1279 | 0 | struct cmd_find_state fs; |
1280 | |
|
1281 | 0 | if (item != NULL) |
1282 | 0 | c = cmdq_get_client(item); |
1283 | 0 | if (c != NULL && c->session != NULL) |
1284 | 0 | return (c); |
1285 | | |
1286 | 0 | found = NULL; |
1287 | 0 | if (c != NULL && (wp = cmd_find_inside_pane(c)) != NULL) { |
1288 | 0 | cmd_find_clear_state(&fs, CMD_FIND_QUIET); |
1289 | 0 | fs.w = wp->window; |
1290 | 0 | if (cmd_find_best_session_with_window(&fs) == 0) |
1291 | 0 | found = cmd_find_best_client(fs.s); |
1292 | 0 | } else { |
1293 | 0 | s = cmd_find_best_session(NULL, 0, CMD_FIND_QUIET); |
1294 | 0 | if (s != NULL) |
1295 | 0 | found = cmd_find_best_client(s); |
1296 | 0 | } |
1297 | 0 | if (found == NULL && item != NULL && !quiet) |
1298 | 0 | cmdq_error(item, "no current client"); |
1299 | 0 | log_debug("%s: no target, return %p", __func__, found); |
1300 | 0 | return (found); |
1301 | 0 | } |
1302 | | |
1303 | | /* Find the target client or report an error and return NULL. */ |
1304 | | struct client * |
1305 | | cmd_find_client(struct cmdq_item *item, const char *target, int quiet) |
1306 | 0 | { |
1307 | 0 | struct client *c; |
1308 | 0 | char *copy; |
1309 | 0 | size_t size; |
1310 | | |
1311 | | /* A NULL argument means the current client. */ |
1312 | 0 | if (target == NULL) |
1313 | 0 | return (cmd_find_current_client(item, quiet)); |
1314 | 0 | copy = xstrdup(target); |
1315 | | |
1316 | | /* Trim a single trailing colon if any. */ |
1317 | 0 | size = strlen(copy); |
1318 | 0 | if (size != 0 && copy[size - 1] == ':') |
1319 | 0 | copy[size - 1] = '\0'; |
1320 | | |
1321 | | /* Check name and path of each client. */ |
1322 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
1323 | 0 | if (c->session == NULL) |
1324 | 0 | continue; |
1325 | 0 | if (strcmp(copy, c->name) == 0) |
1326 | 0 | break; |
1327 | | |
1328 | 0 | if (*c->ttyname == '\0') |
1329 | 0 | continue; |
1330 | 0 | if (strcmp(copy, c->ttyname) == 0) |
1331 | 0 | break; |
1332 | 0 | if (strncmp(c->ttyname, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0) |
1333 | 0 | continue; |
1334 | 0 | if (strcmp(copy, c->ttyname + (sizeof _PATH_DEV) - 1) == 0) |
1335 | 0 | break; |
1336 | 0 | } |
1337 | | |
1338 | | /* If no client found, report an error. */ |
1339 | 0 | if (c == NULL && !quiet) |
1340 | 0 | cmdq_error(item, "can't find client: %s", copy); |
1341 | |
|
1342 | 0 | free(copy); |
1343 | 0 | log_debug("%s: target %s, return %p", __func__, target, c); |
1344 | 0 | return (c); |
1345 | 0 | } |