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