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