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