Annotation of src/usr.bin/tmux/cmd.c, Revision 1.99
1.99 ! nicm 1: /* $OpenBSD: cmd.c,v 1.98 2014/10/08 17:35:58 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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: #include <sys/time.h>
21:
1.5 nicm 22: #include <fnmatch.h>
23: #include <paths.h>
1.61 nicm 24: #include <pwd.h>
1.1 nicm 25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #include "tmux.h"
30:
31: const struct cmd_entry *cmd_table[] = {
32: &cmd_attach_session_entry,
33: &cmd_bind_key_entry,
34: &cmd_break_pane_entry,
1.34 nicm 35: &cmd_capture_pane_entry,
1.42 nicm 36: &cmd_choose_buffer_entry,
1.15 nicm 37: &cmd_choose_client_entry,
1.1 nicm 38: &cmd_choose_session_entry,
1.67 nicm 39: &cmd_choose_tree_entry,
1.1 nicm 40: &cmd_choose_window_entry,
41: &cmd_clear_history_entry,
42: &cmd_clock_mode_entry,
43: &cmd_command_prompt_entry,
44: &cmd_confirm_before_entry,
45: &cmd_copy_mode_entry,
46: &cmd_delete_buffer_entry,
47: &cmd_detach_client_entry,
1.7 nicm 48: &cmd_display_message_entry,
1.16 nicm 49: &cmd_display_panes_entry,
1.1 nicm 50: &cmd_find_window_entry,
51: &cmd_has_session_entry,
1.4 nicm 52: &cmd_if_shell_entry,
1.37 nicm 53: &cmd_join_pane_entry,
1.1 nicm 54: &cmd_kill_pane_entry,
55: &cmd_kill_server_entry,
56: &cmd_kill_session_entry,
57: &cmd_kill_window_entry,
1.45 nicm 58: &cmd_last_pane_entry,
1.1 nicm 59: &cmd_last_window_entry,
60: &cmd_link_window_entry,
61: &cmd_list_buffers_entry,
62: &cmd_list_clients_entry,
63: &cmd_list_commands_entry,
64: &cmd_list_keys_entry,
1.23 nicm 65: &cmd_list_panes_entry,
1.1 nicm 66: &cmd_list_sessions_entry,
67: &cmd_list_windows_entry,
68: &cmd_load_buffer_entry,
1.19 nicm 69: &cmd_lock_client_entry,
1.1 nicm 70: &cmd_lock_server_entry,
1.19 nicm 71: &cmd_lock_session_entry,
1.63 nicm 72: &cmd_move_pane_entry,
1.1 nicm 73: &cmd_move_window_entry,
74: &cmd_new_session_entry,
75: &cmd_new_window_entry,
76: &cmd_next_layout_entry,
77: &cmd_next_window_entry,
78: &cmd_paste_buffer_entry,
1.24 nicm 79: &cmd_pipe_pane_entry,
1.1 nicm 80: &cmd_previous_layout_entry,
81: &cmd_previous_window_entry,
82: &cmd_refresh_client_entry,
83: &cmd_rename_session_entry,
84: &cmd_rename_window_entry,
85: &cmd_resize_pane_entry,
1.56 nicm 86: &cmd_respawn_pane_entry,
1.1 nicm 87: &cmd_respawn_window_entry,
88: &cmd_rotate_window_entry,
1.17 nicm 89: &cmd_run_shell_entry,
1.1 nicm 90: &cmd_save_buffer_entry,
91: &cmd_select_layout_entry,
92: &cmd_select_pane_entry,
93: &cmd_select_window_entry,
94: &cmd_send_keys_entry,
95: &cmd_send_prefix_entry,
96: &cmd_server_info_entry,
97: &cmd_set_buffer_entry,
1.13 nicm 98: &cmd_set_environment_entry,
1.1 nicm 99: &cmd_set_option_entry,
100: &cmd_set_window_option_entry,
101: &cmd_show_buffer_entry,
1.13 nicm 102: &cmd_show_environment_entry,
1.32 nicm 103: &cmd_show_messages_entry,
1.1 nicm 104: &cmd_show_options_entry,
105: &cmd_show_window_options_entry,
106: &cmd_source_file_entry,
107: &cmd_split_window_entry,
108: &cmd_start_server_entry,
109: &cmd_suspend_client_entry,
110: &cmd_swap_pane_entry,
111: &cmd_swap_window_entry,
112: &cmd_switch_client_entry,
113: &cmd_unbind_key_entry,
114: &cmd_unlink_window_entry,
1.82 nicm 115: &cmd_wait_for_entry,
1.1 nicm 116: NULL
117: };
118:
1.72 nicm 119: int cmd_session_better(struct session *, struct session *, int);
1.47 nicm 120: struct session *cmd_choose_session_list(struct sessionslist *);
1.52 nicm 121: struct session *cmd_choose_session(int);
1.31 nicm 122: struct client *cmd_choose_client(struct clients *);
1.5 nicm 123: struct client *cmd_lookup_client(const char *);
1.97 nicm 124: struct session *cmd_lookup_session(struct cmd_q *, const char *, int *);
1.83 nicm 125: struct session *cmd_lookup_session_id(const char *);
1.5 nicm 126: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8 nicm 127: int cmd_lookup_index(struct session *, const char *, int *);
1.60 nicm 128: struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
1.80 nicm 129: struct session *cmd_window_session(struct cmd_q *, struct window *,
130: struct winlink **);
1.43 nicm 131: struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
132: int cmd_find_index_offset(const char *, struct session *, int *);
1.51 nicm 133: struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
1.5 nicm 134:
1.10 nicm 135: int
136: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
137: {
138: size_t arglen;
139: int i;
1.91 nicm 140:
141: if (argc == 0)
142: return (0);
1.10 nicm 143:
144: *buf = '\0';
145: for (i = 0; i < argc; i++) {
146: if (strlcpy(buf, argv[i], len) >= len)
147: return (-1);
148: arglen = strlen(argv[i]) + 1;
149: buf += arglen;
150: len -= arglen;
151: }
152:
153: return (0);
154: }
155:
156: int
157: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
158: {
159: int i;
160: size_t arglen;
161:
162: if (argc == 0)
163: return (0);
164: *argv = xcalloc(argc, sizeof **argv);
165:
166: buf[len - 1] = '\0';
167: for (i = 0; i < argc; i++) {
168: if (len == 0) {
169: cmd_free_argv(argc, *argv);
170: return (-1);
171: }
172:
173: arglen = strlen(buf) + 1;
174: (*argv)[i] = xstrdup(buf);
175: buf += arglen;
176: len -= arglen;
177: }
178:
179: return (0);
1.46 nicm 180: }
181:
182: char **
1.93 nicm 183: cmd_copy_argv(int argc, char **argv)
1.46 nicm 184: {
185: char **new_argv;
186: int i;
187:
188: if (argc == 0)
189: return (NULL);
1.94 nicm 190: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 191: for (i = 0; i < argc; i++) {
192: if (argv[i] != NULL)
193: new_argv[i] = xstrdup(argv[i]);
194: }
195: return (new_argv);
1.10 nicm 196: }
197:
198: void
199: cmd_free_argv(int argc, char **argv)
200: {
201: int i;
202:
203: if (argc == 0)
1.35 nicm 204: return;
1.68 nicm 205: for (i = 0; i < argc; i++)
206: free(argv[i]);
207: free(argv);
1.94 nicm 208: }
209:
210: char *
211: cmd_stringify_argv(int argc, char **argv)
212: {
213: char *buf;
214: int i;
215: size_t len;
216:
217: if (argc == 0)
218: return (xstrdup(""));
219:
220: len = 0;
221: buf = NULL;
222:
223: for (i = 0; i < argc; i++) {
224: len += strlen(argv[i]) + 1;
1.98 nicm 225: buf = xrealloc(buf, len);
1.94 nicm 226:
227: if (i == 0)
228: *buf = '\0';
229: else
230: strlcat(buf, " ", len);
231: strlcat(buf, argv[i], len);
232: }
233: return (buf);
1.10 nicm 234: }
235:
1.1 nicm 236: struct cmd *
1.80 nicm 237: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 238: {
239: const struct cmd_entry **entryp, *entry;
1.27 deraadt 240: struct cmd *cmd;
1.49 nicm 241: struct args *args;
1.1 nicm 242: char s[BUFSIZ];
1.49 nicm 243: int ambiguous = 0;
1.1 nicm 244:
245: *cause = NULL;
1.2 nicm 246: if (argc == 0) {
247: xasprintf(cause, "no command");
1.1 nicm 248: return (NULL);
1.2 nicm 249: }
1.1 nicm 250:
251: entry = NULL;
252: for (entryp = cmd_table; *entryp != NULL; entryp++) {
253: if ((*entryp)->alias != NULL &&
254: strcmp((*entryp)->alias, argv[0]) == 0) {
1.3 nicm 255: ambiguous = 0;
1.1 nicm 256: entry = *entryp;
257: break;
258: }
259:
260: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
261: continue;
262: if (entry != NULL)
1.3 nicm 263: ambiguous = 1;
1.1 nicm 264: entry = *entryp;
265:
266: /* Bail now if an exact match. */
267: if (strcmp(entry->name, argv[0]) == 0)
268: break;
269: }
1.3 nicm 270: if (ambiguous)
271: goto ambiguous;
1.1 nicm 272: if (entry == NULL) {
273: xasprintf(cause, "unknown command: %s", argv[0]);
274: return (NULL);
275: }
276:
1.49 nicm 277: args = args_parse(entry->args_template, argc, argv);
278: if (args == NULL)
279: goto usage;
280: if (entry->args_lower != -1 && args->argc < entry->args_lower)
281: goto usage;
282: if (entry->args_upper != -1 && args->argc > entry->args_upper)
283: goto usage;
1.1 nicm 284:
1.80 nicm 285: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 286: cmd->entry = entry;
1.49 nicm 287: cmd->args = args;
1.80 nicm 288:
289: if (file != NULL)
290: cmd->file = xstrdup(file);
291: cmd->line = line;
292:
1.1 nicm 293: return (cmd);
294:
295: ambiguous:
296: *s = '\0';
297: for (entryp = cmd_table; *entryp != NULL; entryp++) {
298: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
299: continue;
300: if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
301: break;
302: if (strlcat(s, ", ", sizeof s) >= sizeof s)
303: break;
304: }
305: s[strlen(s) - 2] = '\0';
306: xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
307: return (NULL);
308:
309: usage:
1.49 nicm 310: if (args != NULL)
311: args_free(args);
1.1 nicm 312: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
313: return (NULL);
314: }
315:
316: size_t
317: cmd_print(struct cmd *cmd, char *buf, size_t len)
318: {
1.49 nicm 319: size_t off, used;
320:
321: off = xsnprintf(buf, len, "%s ", cmd->entry->name);
1.85 nicm 322: if (off + 1 < len) {
323: used = args_print(cmd->args, buf + off, len - off - 1);
1.49 nicm 324: if (used == 0)
1.64 nicm 325: off--;
326: else
1.49 nicm 327: off += used;
1.64 nicm 328: buf[off] = '\0';
1.49 nicm 329: }
330: return (off);
1.1 nicm 331: }
332:
1.5 nicm 333: /*
334: * Figure out the current session. Use: 1) the current session, if the command
1.31 nicm 335: * context has one; 2) the most recently used session containing the pty of the
336: * calling client, if any; 3) the session specified in the TMUX variable from
337: * the environment (as passed from the client); 4) the most recently used
338: * session from all sessions.
1.5 nicm 339: */
1.1 nicm 340: struct session *
1.80 nicm 341: cmd_current_session(struct cmd_q *cmdq, int prefer_unattached)
1.1 nicm 342: {
1.80 nicm 343: struct client *c = cmdq->client;
1.5 nicm 344: struct session *s;
1.47 nicm 345: struct sessionslist ss;
1.11 nicm 346: struct winlink *wl;
347: struct window_pane *wp;
1.80 nicm 348: const char *path;
1.11 nicm 349: int found;
1.1 nicm 350:
1.99 ! nicm 351: /* Try the queue session. */
1.80 nicm 352: if (c != NULL && c->session != NULL)
353: return (c->session);
1.1 nicm 354:
1.11 nicm 355: /*
1.81 nicm 356: * If the name of the calling client's pty is known, build a list of
357: * the sessions that contain it and if any choose either the first or
358: * the newest.
1.11 nicm 359: */
1.80 nicm 360: path = c == NULL ? NULL : c->tty.path;
361: if (path != NULL) {
1.11 nicm 362: ARRAY_INIT(&ss);
1.47 nicm 363: RB_FOREACH(s, sessions, &sessions) {
1.11 nicm 364: found = 0;
365: RB_FOREACH(wl, winlinks, &s->windows) {
366: TAILQ_FOREACH(wp, &wl->window->panes, entry) {
1.80 nicm 367: if (strcmp(wp->tty, path) == 0) {
1.11 nicm 368: found = 1;
369: break;
370: }
371: }
372: if (found)
373: break;
374: }
375: if (found)
376: ARRAY_ADD(&ss, s);
377: }
378:
1.47 nicm 379: s = cmd_choose_session_list(&ss);
1.11 nicm 380: ARRAY_FREE(&ss);
1.47 nicm 381: if (s != NULL)
382: return (s);
383: }
384:
1.52 nicm 385: return (cmd_choose_session(prefer_unattached));
1.47 nicm 386: }
387:
1.72 nicm 388: /* Is this session better? */
389: int
390: cmd_session_better(struct session *s, struct session *best,
391: int prefer_unattached)
392: {
393: if (best == NULL)
1.74 nicm 394: return (1);
1.72 nicm 395: if (prefer_unattached) {
396: if (!(best->flags & SESSION_UNATTACHED) &&
397: (s->flags & SESSION_UNATTACHED))
1.74 nicm 398: return (1);
1.72 nicm 399: else if ((best->flags & SESSION_UNATTACHED) &&
400: !(s->flags & SESSION_UNATTACHED))
1.74 nicm 401: return (0);
1.72 nicm 402: }
403: return (timercmp(&s->activity_time, &best->activity_time, >));
404: }
405:
1.52 nicm 406: /*
407: * Find the most recently used session, preferring unattached if the flag is
408: * set.
409: */
1.47 nicm 410: struct session *
1.52 nicm 411: cmd_choose_session(int prefer_unattached)
1.47 nicm 412: {
1.72 nicm 413: struct session *s, *best;
1.47 nicm 414:
1.72 nicm 415: best = NULL;
1.47 nicm 416: RB_FOREACH(s, sessions, &sessions) {
1.72 nicm 417: if (cmd_session_better(s, best, prefer_unattached))
418: best = s;
1.47 nicm 419: }
1.72 nicm 420: return (best);
1.5 nicm 421: }
422:
1.31 nicm 423: /* Find the most recently used session from a list. */
1.5 nicm 424: struct session *
1.47 nicm 425: cmd_choose_session_list(struct sessionslist *ss)
1.5 nicm 426: {
1.31 nicm 427: struct session *s, *sbest;
1.5 nicm 428: struct timeval *tv = NULL;
429: u_int i;
430:
1.31 nicm 431: sbest = NULL;
1.11 nicm 432: for (i = 0; i < ARRAY_LENGTH(ss); i++) {
433: if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5 nicm 434: continue;
435:
1.31 nicm 436: if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
437: sbest = s;
438: tv = &s->activity_time;
1.1 nicm 439: }
440: }
1.5 nicm 441:
1.31 nicm 442: return (sbest);
1.1 nicm 443: }
444:
1.30 nicm 445: /*
446: * Find the current client. First try the current client if set, then pick the
1.31 nicm 447: * most recently used of the clients attached to the current session if any,
448: * then of all clients.
1.30 nicm 449: */
450: struct client *
1.80 nicm 451: cmd_current_client(struct cmd_q *cmdq)
1.30 nicm 452: {
453: struct session *s;
454: struct client *c;
455: struct clients cc;
456: u_int i;
457:
1.80 nicm 458: if (cmdq->client != NULL && cmdq->client->session != NULL)
459: return (cmdq->client);
1.30 nicm 460:
461: /*
462: * No current client set. Find the current session and return the
463: * newest of its clients.
464: */
1.80 nicm 465: s = cmd_current_session(cmdq, 0);
1.30 nicm 466: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
467: ARRAY_INIT(&cc);
468: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
469: if ((c = ARRAY_ITEM(&clients, i)) == NULL)
470: continue;
471: if (s == c->session)
472: ARRAY_ADD(&cc, c);
473: }
474:
1.31 nicm 475: c = cmd_choose_client(&cc);
1.30 nicm 476: ARRAY_FREE(&cc);
477: if (c != NULL)
478: return (c);
479: }
480:
1.31 nicm 481: return (cmd_choose_client(&clients));
1.30 nicm 482: }
483:
1.31 nicm 484: /* Choose the most recently used client from a list. */
1.20 nicm 485: struct client *
1.31 nicm 486: cmd_choose_client(struct clients *cc)
1.20 nicm 487: {
1.31 nicm 488: struct client *c, *cbest;
1.20 nicm 489: struct timeval *tv = NULL;
490: u_int i;
491:
1.31 nicm 492: cbest = NULL;
1.30 nicm 493: for (i = 0; i < ARRAY_LENGTH(cc); i++) {
494: if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20 nicm 495: continue;
496: if (c->session == NULL)
497: continue;
498:
1.31 nicm 499: if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
500: cbest = c;
501: tv = &c->activity_time;
1.20 nicm 502: }
503: }
504:
1.31 nicm 505: return (cbest);
1.20 nicm 506: }
507:
1.99 ! nicm 508: /* Adjust current mouse position for a pane. */
! 509: int
! 510: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
! 511: u_int *yp, int last)
! 512: {
! 513: u_int x, y;
! 514:
! 515: if (last) {
! 516: x = m->lx;
! 517: y = m->ly;
! 518: } else {
! 519: x = m->x;
! 520: y = m->y;
! 521: }
! 522:
! 523: if (m->statusat == 0 && y > 0)
! 524: y--;
! 525: else if (m->statusat > 0 && y >= (u_int)m->statusat)
! 526: y = m->statusat - 1;
! 527:
! 528: if (x < wp->xoff || x >= wp->xoff + wp->sx)
! 529: return (-1);
! 530: if (y < wp->yoff || y >= wp->yoff + wp->sy)
! 531: return (-1);
! 532:
! 533: *xp = x - wp->xoff;
! 534: *yp = y - wp->yoff;
! 535: return (0);
! 536: }
! 537:
! 538: /* Get current mouse window if any. */
! 539: struct winlink *
! 540: cmd_mouse_window(struct mouse_event *m, struct session **sp)
! 541: {
! 542: struct session *s;
! 543: struct window *w;
! 544:
! 545: if (!m->valid || m->s == -1 || m->w == -1)
! 546: return (NULL);
! 547: if ((s = session_find_by_id(m->s)) == NULL)
! 548: return (NULL);
! 549: if ((w = window_find_by_id(m->w)) == NULL)
! 550: return (NULL);
! 551:
! 552: if (sp != NULL)
! 553: *sp = s;
! 554: return (winlink_find_by_window(&s->windows, w));
! 555: }
! 556:
! 557: /* Get current mouse pane if any. */
! 558: struct window_pane *
! 559: cmd_mouse_pane(struct mouse_event *m, struct session **sp, struct winlink **wlp)
! 560: {
! 561: struct winlink *wl;
! 562: struct window_pane *wp;
! 563:
! 564: if ((wl = cmd_mouse_window(m, sp)) == NULL)
! 565: return (NULL);
! 566: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
! 567: return (NULL);
! 568: if (!window_has_pane(wl->window, wp))
! 569: return (NULL);
! 570:
! 571: if (wlp != NULL)
! 572: *wlp = wl;
! 573: return (wp);
! 574: }
! 575:
1.5 nicm 576: /* Find the target client or report an error and return NULL. */
1.1 nicm 577: struct client *
1.80 nicm 578: cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet)
1.1 nicm 579: {
1.30 nicm 580: struct client *c;
1.5 nicm 581: char *tmparg;
582: size_t arglen;
1.1 nicm 583:
1.5 nicm 584: /* A NULL argument means the current client. */
1.79 nicm 585: if (arg == NULL) {
1.80 nicm 586: c = cmd_current_client(cmdq);
1.79 nicm 587: if (c == NULL && !quiet)
1.80 nicm 588: cmdq_error(cmdq, "no clients");
1.79 nicm 589: return (c);
590: }
1.5 nicm 591: tmparg = xstrdup(arg);
592:
593: /* Trim a single trailing colon if any. */
594: arglen = strlen(tmparg);
595: if (arglen != 0 && tmparg[arglen - 1] == ':')
596: tmparg[arglen - 1] = '\0';
597:
598: /* Find the client, if any. */
599: c = cmd_lookup_client(tmparg);
600:
601: /* If no client found, report an error. */
1.79 nicm 602: if (c == NULL && !quiet)
1.80 nicm 603: cmdq_error(cmdq, "client not found: %s", tmparg);
1.5 nicm 604:
1.68 nicm 605: free(tmparg);
1.5 nicm 606: return (c);
607: }
608:
609: /*
610: * Lookup a client by device path. Either of a full match and a match without a
611: * leading _PATH_DEV ("/dev/") is accepted.
612: */
613: struct client *
614: cmd_lookup_client(const char *name)
615: {
616: struct client *c;
617: const char *path;
618: u_int i;
619:
620: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25 nicm 621: c = ARRAY_ITEM(&clients, i);
1.81 nicm 622: if (c == NULL || c->session == NULL || c->tty.path == NULL)
1.5 nicm 623: continue;
624: path = c->tty.path;
625:
626: /* Check for exact matches. */
627: if (strcmp(name, path) == 0)
628: return (c);
629:
630: /* Check without leading /dev if present. */
631: if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
632: continue;
633: if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
634: return (c);
635: }
636:
637: return (NULL);
638: }
639:
1.83 nicm 640: /* Find the target session or report an error and return NULL. */
641: struct session *
642: cmd_lookup_session_id(const char *arg)
643: {
644: char *endptr;
645: long id;
646:
647: if (arg[0] != '$')
648: return (NULL);
649: id = strtol(arg + 1, &endptr, 10);
650: if (arg[1] != '\0' && *endptr == '\0')
651: return (session_find_by_id(id));
652: return (NULL);
653: }
654:
1.5 nicm 655: /* Lookup a session by name. If no session is found, NULL is returned. */
656: struct session *
1.97 nicm 657: cmd_lookup_session(struct cmd_q *cmdq, const char *name, int *ambiguous)
1.5 nicm 658: {
1.97 nicm 659: struct session *s, *sfound;
660: struct window *w;
661: struct window_pane *wp;
1.5 nicm 662:
663: *ambiguous = 0;
1.83 nicm 664:
665: /* Look for $id first. */
666: if ((s = cmd_lookup_session_id(name)) != NULL)
667: return (s);
1.5 nicm 668:
1.97 nicm 669: /* Try as pane or window id. */
670: if ((wp = cmd_lookup_paneid(name)) != NULL)
671: return (cmd_window_session(cmdq, wp->window, NULL));
672: if ((w = cmd_lookup_windowid(name)) != NULL)
673: return (cmd_window_session(cmdq, w, NULL));
674:
1.5 nicm 675: /*
1.28 nicm 676: * Look for matches. First look for exact matches - session names must
677: * be unique so an exact match can't be ambigious and can just be
678: * returned.
1.5 nicm 679: */
1.47 nicm 680: if ((s = session_find(name)) != NULL)
681: return (s);
1.28 nicm 682:
683: /*
684: * Otherwise look for partial matches, returning early if it is found to
685: * be ambiguous.
686: */
687: sfound = NULL;
1.47 nicm 688: RB_FOREACH(s, sessions, &sessions) {
1.5 nicm 689: if (strncmp(name, s->name, strlen(name)) == 0 ||
690: fnmatch(name, s->name, 0) == 0) {
691: if (sfound != NULL) {
692: *ambiguous = 1;
693: return (NULL);
694: }
695: sfound = s;
696: }
697: }
1.35 nicm 698: return (sfound);
1.5 nicm 699: }
700:
701: /*
1.8 nicm 702: * Lookup a window or return -1 if not found or ambigious. First try as an
703: * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41 nicm 704: * idx if the window index is a valid number but there is no window with that
1.8 nicm 705: * index.
1.5 nicm 706: */
707: struct winlink *
708: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
709: {
1.97 nicm 710: struct winlink *wl, *wlfound;
711: struct window *w;
712: struct window_pane *wp;
713: const char *errstr;
714: u_int idx;
1.5 nicm 715:
716: *ambiguous = 0;
717:
1.97 nicm 718: /* Try as pane or window id. */
1.60 nicm 719: if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
720: return (wl);
721:
1.97 nicm 722: /* Lookup as pane or window id. */
723: if ((wp = cmd_lookup_paneid(name)) != NULL) {
724: wl = winlink_find_by_window(&s->windows, wp->window);
725: if (wl != NULL)
726: return (wl);
727: }
728: if ((w = cmd_lookup_windowid(name)) != NULL) {
729: wl = winlink_find_by_window(&s->windows, w);
730: if (wl != NULL)
731: return (wl);
732: }
733:
1.5 nicm 734: /* First see if this is a valid window index in this session. */
735: idx = strtonum(name, 0, INT_MAX, &errstr);
736: if (errstr == NULL) {
737: if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
738: return (wl);
739: }
1.35 nicm 740:
1.5 nicm 741: /* Look for exact matches, error if more than one. */
742: wlfound = NULL;
743: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 744: if (strcmp(name, wl->window->name) == 0) {
1.5 nicm 745: if (wlfound != NULL) {
746: *ambiguous = 1;
747: return (NULL);
748: }
749: wlfound = wl;
1.1 nicm 750: }
751: }
1.5 nicm 752: if (wlfound != NULL)
753: return (wlfound);
754:
755: /* Now look for pattern matches, again error if multiple. */
756: wlfound = NULL;
757: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 758: if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
759: fnmatch(name, wl->window->name, 0) == 0) {
1.5 nicm 760: if (wlfound != NULL) {
761: *ambiguous = 1;
762: return (NULL);
763: }
764: wlfound = wl;
765: }
1.35 nicm 766: }
1.5 nicm 767: if (wlfound != NULL)
768: return (wlfound);
769:
770: return (NULL);
1.1 nicm 771: }
772:
1.8 nicm 773: /*
774: * Find a window index - if the window doesn't exist, check if it is a
775: * potential index and return it anyway.
776: */
777: int
778: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
779: {
780: struct winlink *wl;
781: const char *errstr;
782: u_int idx;
783:
784: if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
785: return (wl->idx);
786: if (*ambiguous)
787: return (-1);
788:
789: idx = strtonum(name, 0, INT_MAX, &errstr);
790: if (errstr == NULL)
791: return (idx);
792:
793: return (-1);
794: }
795:
1.60 nicm 796: /* Lookup pane id. An initial % means a pane id. */
1.51 nicm 797: struct window_pane *
798: cmd_lookup_paneid(const char *arg)
799: {
800: const char *errstr;
801: u_int paneid;
802:
803: if (*arg != '%')
804: return (NULL);
805:
806: paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
807: if (errstr != NULL)
808: return (NULL);
809: return (window_pane_find_by_id(paneid));
810: }
811:
1.60 nicm 812: /* Lookup window id in a session. An initial @ means a window id. */
813: struct winlink *
814: cmd_lookup_winlink_windowid(struct session *s, const char *arg)
815: {
816: const char *errstr;
817: u_int windowid;
818:
819: if (*arg != '@')
820: return (NULL);
821:
822: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
823: if (errstr != NULL)
824: return (NULL);
825: return (winlink_find_by_window_id(&s->windows, windowid));
826: }
827:
828: /* Lookup window id. An initial @ means a window id. */
829: struct window *
830: cmd_lookup_windowid(const char *arg)
831: {
832: const char *errstr;
833: u_int windowid;
834:
835: if (*arg != '@')
836: return (NULL);
837:
838: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
839: if (errstr != NULL)
840: return (NULL);
841: return (window_find_by_id(windowid));
842: }
843:
844: /* Find session and winlink for window. */
1.51 nicm 845: struct session *
1.80 nicm 846: cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
1.51 nicm 847: {
848: struct session *s;
849: struct sessionslist ss;
850: struct winlink *wl;
851:
1.60 nicm 852: /* If this window is in the current session, return that winlink. */
1.80 nicm 853: s = cmd_current_session(cmdq, 0);
1.51 nicm 854: if (s != NULL) {
1.60 nicm 855: wl = winlink_find_by_window(&s->windows, w);
1.51 nicm 856: if (wl != NULL) {
857: if (wlp != NULL)
858: *wlp = wl;
859: return (s);
860: }
861: }
862:
1.60 nicm 863: /* Otherwise choose from all sessions with this window. */
1.51 nicm 864: ARRAY_INIT(&ss);
865: RB_FOREACH(s, sessions, &sessions) {
1.60 nicm 866: if (winlink_find_by_window(&s->windows, w) != NULL)
1.51 nicm 867: ARRAY_ADD(&ss, s);
868: }
869: s = cmd_choose_session_list(&ss);
870: ARRAY_FREE(&ss);
871: if (wlp != NULL)
1.60 nicm 872: *wlp = winlink_find_by_window(&s->windows, w);
1.51 nicm 873: return (s);
874: }
875:
1.5 nicm 876: /* Find the target session or report an error and return NULL. */
1.1 nicm 877: struct session *
1.80 nicm 878: cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
1.1 nicm 879: {
1.97 nicm 880: struct session *s;
881: struct client *c;
882: char *tmparg;
883: size_t arglen;
884: int ambiguous;
1.1 nicm 885:
1.5 nicm 886: /* A NULL argument means the current session. */
1.95 nicm 887: if (arg == NULL) {
888: if ((s = cmd_current_session(cmdq, prefer_unattached)) == NULL)
889: cmdq_error(cmdq, "can't establish current session");
890: return (s);
891: }
1.5 nicm 892:
893: /* Trim a single trailing colon if any. */
1.54 nicm 894: tmparg = xstrdup(arg);
1.5 nicm 895: arglen = strlen(tmparg);
896: if (arglen != 0 && tmparg[arglen - 1] == ':')
897: tmparg[arglen - 1] = '\0';
898:
1.53 nicm 899: /* An empty session name is the current session. */
900: if (*tmparg == '\0') {
1.68 nicm 901: free(tmparg);
1.95 nicm 902: if ((s = cmd_current_session(cmdq, prefer_unattached)) == NULL)
903: cmdq_error(cmdq, "can't establish current session");
904: return (s);
1.53 nicm 905: }
906:
1.5 nicm 907: /* Find the session, if any. */
1.97 nicm 908: s = cmd_lookup_session(cmdq, tmparg, &ambiguous);
1.5 nicm 909:
910: /* If it doesn't, try to match it as a client. */
911: if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
912: s = c->session;
913:
914: /* If no session found, report an error. */
915: if (s == NULL) {
916: if (ambiguous)
1.80 nicm 917: cmdq_error(cmdq, "more than one session: %s", tmparg);
1.5 nicm 918: else
1.80 nicm 919: cmdq_error(cmdq, "session not found: %s", tmparg);
1.1 nicm 920: }
1.5 nicm 921:
1.68 nicm 922: free(tmparg);
1.1 nicm 923: return (s);
924: }
925:
1.5 nicm 926: /* Find the target session and window or report an error and return NULL. */
1.1 nicm 927: struct winlink *
1.80 nicm 928: cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.1 nicm 929: {
1.97 nicm 930: struct session *s;
931: struct winlink *wl;
932: const char *winptr;
933: char *sessptr = NULL;
934: int ambiguous = 0;
1.5 nicm 935:
936: /*
937: * Find the current session. There must always be a current session, if
938: * it can't be found, report an error.
939: */
1.80 nicm 940: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
941: cmdq_error(cmdq, "can't establish current session");
1.5 nicm 942: return (NULL);
943: }
944:
945: /* A NULL argument means the current session and window. */
946: if (arg == NULL) {
947: if (sp != NULL)
948: *sp = s;
949: return (s->curw);
950: }
951:
952: /* Time to look at the argument. If it is empty, that is an error. */
953: if (*arg == '\0')
954: goto not_found;
955:
1.8 nicm 956: /* Find the separating colon and split into window and session. */
1.5 nicm 957: winptr = strchr(arg, ':');
958: if (winptr == NULL)
1.8 nicm 959: goto no_colon;
960: winptr++; /* skip : */
961: sessptr = xstrdup(arg);
962: *strchr(sessptr, ':') = '\0';
1.5 nicm 963:
964: /* Try to lookup the session if present. */
1.8 nicm 965: if (*sessptr != '\0') {
1.97 nicm 966: if ((s = cmd_lookup_session(cmdq, sessptr, &ambiguous)) == NULL)
1.5 nicm 967: goto no_session;
968: }
969: if (sp != NULL)
970: *sp = s;
971:
972: /*
973: * Then work out the window. An empty string is the current window,
1.38 nicm 974: * otherwise try special cases then to look it up in the session.
1.5 nicm 975: */
1.8 nicm 976: if (*winptr == '\0')
1.5 nicm 977: wl = s->curw;
1.38 nicm 978: else if (winptr[0] == '!' && winptr[1] == '\0')
979: wl = TAILQ_FIRST(&s->lastw);
1.73 nicm 980: else if (winptr[0] == '^' && winptr[1] == '\0')
981: wl = RB_MIN(winlinks, &s->windows);
982: else if (winptr[0] == '$' && winptr[1] == '\0')
983: wl = RB_MAX(winlinks, &s->windows);
1.43 nicm 984: else if (winptr[0] == '+' || winptr[0] == '-')
985: wl = cmd_find_window_offset(winptr, s, &ambiguous);
986: else
1.38 nicm 987: wl = cmd_lookup_window(s, winptr, &ambiguous);
988: if (wl == NULL)
1.5 nicm 989: goto not_found;
1.35 nicm 990:
1.5 nicm 991: if (sessptr != NULL)
1.68 nicm 992: free(sessptr);
1.5 nicm 993: return (wl);
994:
1.8 nicm 995: no_colon:
1.38 nicm 996: /*
997: * No colon in the string, first try special cases, then as a window
998: * and lastly as a session.
999: */
1.99 ! nicm 1000: if (arg[0] == '=' && arg[1] == '\0') {
! 1001: if ((wl = cmd_mouse_window(&cmdq->item->mouse, &s)) == NULL) {
! 1002: cmdq_error(cmdq, "no mouse target");
! 1003: goto error;
! 1004: }
! 1005: } else if (arg[0] == '!' && arg[1] == '\0') {
1.38 nicm 1006: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1007: goto not_found;
1.41 nicm 1008: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 1009: if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41 nicm 1010: goto lookup_session;
1011: } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
1012: goto lookup_session;
1.8 nicm 1013:
1014: if (sp != NULL)
1015: *sp = s;
1016:
1017: return (wl);
1018:
1.41 nicm 1019: lookup_session:
1020: if (ambiguous)
1021: goto not_found;
1.97 nicm 1022: if (*arg != '\0' &&
1023: (s = cmd_lookup_session(cmdq, arg, &ambiguous)) == NULL)
1.41 nicm 1024: goto no_session;
1025:
1026: if (sp != NULL)
1027: *sp = s;
1028:
1029: return (s->curw);
1030:
1.5 nicm 1031: no_session:
1032: if (ambiguous)
1.80 nicm 1033: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 1034: else
1.80 nicm 1035: cmdq_error(cmdq, "session not found: %s", arg);
1.99 ! nicm 1036: goto error;
1.5 nicm 1037:
1038: not_found:
1039: if (ambiguous)
1.80 nicm 1040: cmdq_error(cmdq, "multiple windows: %s", arg);
1.5 nicm 1041: else
1.80 nicm 1042: cmdq_error(cmdq, "window not found: %s", arg);
1.99 ! nicm 1043: goto error;
! 1044:
! 1045: error:
1.68 nicm 1046: free(sessptr);
1.5 nicm 1047: return (NULL);
1048: }
1.1 nicm 1049:
1.43 nicm 1050: struct winlink *
1051: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
1052: {
1053: struct winlink *wl;
1054: int offset = 1;
1055:
1056: if (winptr[1] != '\0')
1057: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1058: if (offset == 0)
1059: wl = cmd_lookup_window(s, winptr, ambiguous);
1060: else {
1061: if (winptr[0] == '+')
1062: wl = winlink_next_by_number(s->curw, s, offset);
1063: else
1064: wl = winlink_previous_by_number(s->curw, s, offset);
1065: }
1066:
1067: return (wl);
1068: }
1069:
1.5 nicm 1070: /*
1071: * Find the target session and window index, whether or not it exists in the
1072: * session. Return -2 on error or -1 if no window index is specified. This is
1.8 nicm 1073: * used when parsing an argument for a window target that may not exist (for
1074: * example if it is going to be created).
1.5 nicm 1075: */
1076: int
1.80 nicm 1077: cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.5 nicm 1078: {
1079: struct session *s;
1.38 nicm 1080: struct winlink *wl;
1.8 nicm 1081: const char *winptr;
1.5 nicm 1082: char *sessptr = NULL;
1083: int idx, ambiguous = 0;
1084:
1085: /*
1086: * Find the current session. There must always be a current session, if
1087: * it can't be found, report an error.
1088: */
1.80 nicm 1089: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1090: cmdq_error(cmdq, "can't establish current session");
1.9 nicm 1091: return (-2);
1.1 nicm 1092: }
1.5 nicm 1093:
1094: /* A NULL argument means the current session and "no window" (-1). */
1095: if (arg == NULL) {
1096: if (sp != NULL)
1097: *sp = s;
1098: return (-1);
1099: }
1100:
1101: /* Time to look at the argument. If it is empty, that is an error. */
1102: if (*arg == '\0')
1103: goto not_found;
1104:
1105: /* Find the separating colon. If none, assume the current session. */
1106: winptr = strchr(arg, ':');
1107: if (winptr == NULL)
1.8 nicm 1108: goto no_colon;
1109: winptr++; /* skip : */
1110: sessptr = xstrdup(arg);
1111: *strchr(sessptr, ':') = '\0';
1.5 nicm 1112:
1113: /* Try to lookup the session if present. */
1114: if (sessptr != NULL && *sessptr != '\0') {
1.97 nicm 1115: if ((s = cmd_lookup_session(cmdq, sessptr, &ambiguous)) == NULL)
1.5 nicm 1116: goto no_session;
1117: }
1.1 nicm 1118: if (sp != NULL)
1119: *sp = s;
1120:
1.5 nicm 1121: /*
1.8 nicm 1122: * Then work out the window. An empty string is a new window otherwise
1123: * try to look it up in the session.
1.5 nicm 1124: */
1.8 nicm 1125: if (*winptr == '\0')
1.38 nicm 1126: idx = -1;
1127: else if (winptr[0] == '!' && winptr[1] == '\0') {
1128: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1129: goto not_found;
1130: idx = wl->idx;
1.41 nicm 1131: } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43 nicm 1132: if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41 nicm 1133: goto invalid_index;
1134: } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1135: goto invalid_index;
1.35 nicm 1136:
1.68 nicm 1137: free(sessptr);
1.5 nicm 1138: return (idx);
1139:
1.8 nicm 1140: no_colon:
1.38 nicm 1141: /*
1142: * No colon in the string, first try special cases, then as a window
1143: * and lastly as a session.
1144: */
1145: if (arg[0] == '!' && arg[1] == '\0') {
1146: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1147: goto not_found;
1148: idx = wl->idx;
1.41 nicm 1149: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 1150: if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41 nicm 1151: goto lookup_session;
1152: } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1153: goto lookup_session;
1.8 nicm 1154:
1155: if (sp != NULL)
1156: *sp = s;
1157:
1158: return (idx);
1159:
1.41 nicm 1160: lookup_session:
1161: if (ambiguous)
1162: goto not_found;
1.97 nicm 1163: if (*arg != '\0' &&
1164: (s = cmd_lookup_session(cmdq, arg, &ambiguous)) == NULL)
1.41 nicm 1165: goto no_session;
1166:
1167: if (sp != NULL)
1168: *sp = s;
1169:
1170: return (-1);
1171:
1.5 nicm 1172: no_session:
1173: if (ambiguous)
1.80 nicm 1174: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 1175: else
1.80 nicm 1176: cmdq_error(cmdq, "session not found: %s", arg);
1.68 nicm 1177: free(sessptr);
1.5 nicm 1178: return (-2);
1179:
1.41 nicm 1180: invalid_index:
1181: if (ambiguous)
1182: goto not_found;
1.80 nicm 1183: cmdq_error(cmdq, "invalid index: %s", arg);
1.41 nicm 1184:
1.68 nicm 1185: free(sessptr);
1.41 nicm 1186: return (-2);
1187:
1.5 nicm 1188: not_found:
1189: if (ambiguous)
1.80 nicm 1190: cmdq_error(cmdq, "multiple windows: %s", arg);
1.1 nicm 1191: else
1.80 nicm 1192: cmdq_error(cmdq, "window not found: %s", arg);
1.68 nicm 1193: free(sessptr);
1.5 nicm 1194: return (-2);
1.12 nicm 1195: }
1196:
1.43 nicm 1197: int
1198: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1199: {
1200: int idx, offset = 1;
1201:
1202: if (winptr[1] != '\0')
1203: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1204: if (offset == 0)
1205: idx = cmd_lookup_index(s, winptr, ambiguous);
1206: else {
1207: if (winptr[0] == '+') {
1208: if (s->curw->idx == INT_MAX)
1209: idx = cmd_lookup_index(s, winptr, ambiguous);
1210: else
1211: idx = s->curw->idx + offset;
1212: } else {
1213: if (s->curw->idx == 0)
1214: idx = cmd_lookup_index(s, winptr, ambiguous);
1215: else
1216: idx = s->curw->idx - offset;
1217: }
1218: }
1219:
1220: return (idx);
1221: }
1222:
1.12 nicm 1223: /*
1224: * Find the target session, window and pane number or report an error and
1225: * return NULL. The pane number is separated from the session:window by a .,
1226: * such as mysession:mywindow.0.
1227: */
1228: struct winlink *
1.80 nicm 1229: cmd_find_pane(struct cmd_q *cmdq,
1.12 nicm 1230: const char *arg, struct session **sp, struct window_pane **wpp)
1231: {
1.55 nicm 1232: struct session *s;
1233: struct winlink *wl;
1234: const char *period, *errstr;
1235: char *winptr, *paneptr;
1236: u_int idx;
1.12 nicm 1237:
1238: /* Get the current session. */
1.80 nicm 1239: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1240: cmdq_error(cmdq, "can't establish current session");
1.12 nicm 1241: return (NULL);
1242: }
1243: if (sp != NULL)
1244: *sp = s;
1245:
1246: /* A NULL argument means the current session, window and pane. */
1247: if (arg == NULL) {
1248: *wpp = s->curw->window->active;
1249: return (s->curw);
1.51 nicm 1250: }
1251:
1252: /* Lookup as pane id. */
1253: if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1.80 nicm 1254: s = cmd_window_session(cmdq, (*wpp)->window, &wl);
1.51 nicm 1255: if (sp != NULL)
1256: *sp = s;
1257: return (wl);
1.12 nicm 1258: }
1259:
1260: /* Look for a separating period. */
1261: if ((period = strrchr(arg, '.')) == NULL)
1262: goto no_period;
1263:
1264: /* Pull out the window part and parse it. */
1265: winptr = xstrdup(arg);
1266: winptr[period - arg] = '\0';
1267: if (*winptr == '\0')
1268: wl = s->curw;
1.80 nicm 1269: else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
1.12 nicm 1270: goto error;
1271:
1272: /* Find the pane section and look it up. */
1273: paneptr = winptr + (period - arg) + 1;
1274: if (*paneptr == '\0')
1275: *wpp = wl->window->active;
1.43 nicm 1276: else if (paneptr[0] == '+' || paneptr[0] == '-')
1277: *wpp = cmd_find_pane_offset(paneptr, wl);
1.96 nicm 1278: else if (paneptr[0] == '!' && paneptr[1] == '\0') {
1279: if (wl->window->last == NULL) {
1280: cmdq_error(cmdq, "no last pane");
1281: goto error;
1282: }
1283: *wpp = wl->window->last;
1284: } else {
1.12 nicm 1285: idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36 nicm 1286: if (errstr != NULL)
1287: goto lookup_string;
1.12 nicm 1288: *wpp = window_pane_at_index(wl->window, idx);
1.36 nicm 1289: if (*wpp == NULL)
1290: goto lookup_string;
1.12 nicm 1291: }
1292:
1.68 nicm 1293: free(winptr);
1.12 nicm 1294: return (wl);
1295:
1.36 nicm 1296: lookup_string:
1297: /* Try pane string description. */
1.55 nicm 1298: if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1.80 nicm 1299: cmdq_error(cmdq, "can't find pane: %s", paneptr);
1.36 nicm 1300: goto error;
1301: }
1302:
1.68 nicm 1303: free(winptr);
1.39 nicm 1304: return (wl);
1.36 nicm 1305:
1.12 nicm 1306: no_period:
1.99 ! nicm 1307: /* Check mouse event. */
! 1308: if (arg[0] == '=' && arg[1] == '\0') {
! 1309: *wpp = cmd_mouse_pane(&cmdq->item->mouse, &s, &wl);
! 1310: if (*wpp == NULL) {
! 1311: cmdq_error(cmdq, "no mouse target");
! 1312: return (NULL);
! 1313: }
! 1314: if (sp != NULL)
! 1315: *sp = s;
! 1316: return (wl);
! 1317: }
! 1318:
1.12 nicm 1319: /* Try as a pane number alone. */
1320: idx = strtonum(arg, 0, INT_MAX, &errstr);
1321: if (errstr != NULL)
1322: goto lookup_window;
1323:
1324: /* Try index in the current session and window. */
1325: if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1326: goto lookup_window;
1.35 nicm 1327:
1.12 nicm 1328: return (s->curw);
1329:
1330: lookup_window:
1.36 nicm 1331: /* Try pane string description. */
1.55 nicm 1332: if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1.36 nicm 1333: return (s->curw);
1334:
1.12 nicm 1335: /* Try as a window and use the active pane. */
1.80 nicm 1336: if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
1.12 nicm 1337: *wpp = wl->window->active;
1338: return (wl);
1.35 nicm 1339:
1.12 nicm 1340: error:
1.68 nicm 1341: free(winptr);
1.12 nicm 1342: return (NULL);
1.43 nicm 1343: }
1344:
1345: struct window_pane *
1346: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1347: {
1348: struct window *w = wl->window;
1349: struct window_pane *wp = w->active;
1350: u_int offset = 1;
1351:
1352: if (paneptr[1] != '\0')
1353: offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1354: if (offset > 0) {
1355: if (paneptr[0] == '+')
1356: wp = window_pane_next_by_number(w, wp, offset);
1357: else
1358: wp = window_pane_previous_by_number(w, wp, offset);
1359: }
1360:
1361: return (wp);
1.15 nicm 1362: }
1363:
1364: /* Replace the first %% or %idx in template by s. */
1365: char *
1.75 nicm 1366: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 1367: {
1.75 nicm 1368: char ch, *buf;
1369: const char *ptr;
1.84 nicm 1370: int replaced;
1371: size_t len;
1.15 nicm 1372:
1.76 nicm 1373: if (strchr(template, '%') == NULL)
1.15 nicm 1374: return (xstrdup(template));
1375:
1376: buf = xmalloc(1);
1377: *buf = '\0';
1378: len = 0;
1379: replaced = 0;
1380:
1381: ptr = template;
1382: while (*ptr != '\0') {
1383: switch (ch = *ptr++) {
1384: case '%':
1385: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1386: if (*ptr != '%' || replaced)
1387: break;
1388: replaced = 1;
1389: }
1390: ptr++;
1391:
1392: len += strlen(s);
1.98 nicm 1393: buf = xrealloc(buf, len + 1);
1.15 nicm 1394: strlcat(buf, s, len + 1);
1395: continue;
1396: }
1.98 nicm 1397: buf = xrealloc(buf, len + 2);
1.15 nicm 1398: buf[len++] = ch;
1399: buf[len] = '\0';
1400: }
1401:
1402: return (buf);
1.1 nicm 1403: }