Annotation of src/usr.bin/tmux/cmd.c, Revision 1.101
1.101 ! nicm 1: /* $OpenBSD: cmd.c,v 1.100 2015/04/21 22:42:27 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 *);
! 120:
1.72 nicm 121: int cmd_session_better(struct session *, struct session *, int);
1.47 nicm 122: struct session *cmd_choose_session_list(struct sessionslist *);
1.52 nicm 123: struct session *cmd_choose_session(int);
1.101 ! nicm 124: struct client *cmd_choose_client(struct client_list *);
1.5 nicm 125: struct client *cmd_lookup_client(const char *);
1.97 nicm 126: struct session *cmd_lookup_session(struct cmd_q *, const char *, int *);
1.83 nicm 127: struct session *cmd_lookup_session_id(const char *);
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:
1.83 nicm 642: /* Find the target session or report an error and return NULL. */
643: struct session *
644: cmd_lookup_session_id(const char *arg)
645: {
646: char *endptr;
647: long id;
648:
649: if (arg[0] != '$')
650: return (NULL);
651: id = strtol(arg + 1, &endptr, 10);
652: if (arg[1] != '\0' && *endptr == '\0')
653: return (session_find_by_id(id));
654: return (NULL);
655: }
656:
1.5 nicm 657: /* Lookup a session by name. If no session is found, NULL is returned. */
658: struct session *
1.97 nicm 659: cmd_lookup_session(struct cmd_q *cmdq, const char *name, int *ambiguous)
1.5 nicm 660: {
1.97 nicm 661: struct session *s, *sfound;
662: struct window *w;
663: struct window_pane *wp;
1.5 nicm 664:
665: *ambiguous = 0;
1.83 nicm 666:
667: /* Look for $id first. */
668: if ((s = cmd_lookup_session_id(name)) != NULL)
669: return (s);
1.5 nicm 670:
1.97 nicm 671: /* Try as pane or window id. */
672: if ((wp = cmd_lookup_paneid(name)) != NULL)
673: return (cmd_window_session(cmdq, wp->window, NULL));
674: if ((w = cmd_lookup_windowid(name)) != NULL)
675: return (cmd_window_session(cmdq, w, NULL));
676:
1.5 nicm 677: /*
1.28 nicm 678: * Look for matches. First look for exact matches - session names must
679: * be unique so an exact match can't be ambigious and can just be
680: * returned.
1.5 nicm 681: */
1.47 nicm 682: if ((s = session_find(name)) != NULL)
683: return (s);
1.28 nicm 684:
685: /*
686: * Otherwise look for partial matches, returning early if it is found to
687: * be ambiguous.
688: */
689: sfound = NULL;
1.47 nicm 690: RB_FOREACH(s, sessions, &sessions) {
1.5 nicm 691: if (strncmp(name, s->name, strlen(name)) == 0 ||
692: fnmatch(name, s->name, 0) == 0) {
693: if (sfound != NULL) {
694: *ambiguous = 1;
695: return (NULL);
696: }
697: sfound = s;
698: }
699: }
1.35 nicm 700: return (sfound);
1.5 nicm 701: }
702:
703: /*
1.8 nicm 704: * Lookup a window or return -1 if not found or ambigious. First try as an
705: * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41 nicm 706: * idx if the window index is a valid number but there is no window with that
1.8 nicm 707: * index.
1.5 nicm 708: */
709: struct winlink *
710: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
711: {
1.97 nicm 712: struct winlink *wl, *wlfound;
713: struct window *w;
714: struct window_pane *wp;
715: const char *errstr;
716: u_int idx;
1.5 nicm 717:
718: *ambiguous = 0;
719:
1.97 nicm 720: /* Try as pane or window id. */
1.60 nicm 721: if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
722: return (wl);
723:
1.97 nicm 724: /* Lookup as pane or window id. */
725: if ((wp = cmd_lookup_paneid(name)) != NULL) {
726: wl = winlink_find_by_window(&s->windows, wp->window);
727: if (wl != NULL)
728: return (wl);
729: }
730: if ((w = cmd_lookup_windowid(name)) != NULL) {
731: wl = winlink_find_by_window(&s->windows, w);
732: if (wl != NULL)
733: return (wl);
734: }
735:
1.5 nicm 736: /* First see if this is a valid window index in this session. */
737: idx = strtonum(name, 0, INT_MAX, &errstr);
738: if (errstr == NULL) {
739: if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
740: return (wl);
741: }
1.35 nicm 742:
1.5 nicm 743: /* Look for exact matches, error if more than one. */
744: wlfound = NULL;
745: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 746: if (strcmp(name, wl->window->name) == 0) {
1.5 nicm 747: if (wlfound != NULL) {
748: *ambiguous = 1;
749: return (NULL);
750: }
751: wlfound = wl;
1.1 nicm 752: }
753: }
1.5 nicm 754: if (wlfound != NULL)
755: return (wlfound);
756:
757: /* Now look for pattern matches, again error if multiple. */
758: wlfound = NULL;
759: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 760: if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
761: fnmatch(name, wl->window->name, 0) == 0) {
1.5 nicm 762: if (wlfound != NULL) {
763: *ambiguous = 1;
764: return (NULL);
765: }
766: wlfound = wl;
767: }
1.35 nicm 768: }
1.5 nicm 769: if (wlfound != NULL)
770: return (wlfound);
771:
772: return (NULL);
1.1 nicm 773: }
774:
1.8 nicm 775: /*
776: * Find a window index - if the window doesn't exist, check if it is a
777: * potential index and return it anyway.
778: */
779: int
780: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
781: {
782: struct winlink *wl;
783: const char *errstr;
784: u_int idx;
785:
1.100 nicm 786: idx = strtonum(name, 0, INT_MAX, &errstr);
787: if (errstr == NULL)
788: return (idx);
789:
1.8 nicm 790: if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
791: return (wl->idx);
792: if (*ambiguous)
793: return (-1);
794:
795: return (-1);
796: }
797:
1.60 nicm 798: /* Lookup pane id. An initial % means a pane id. */
1.51 nicm 799: struct window_pane *
800: cmd_lookup_paneid(const char *arg)
801: {
802: const char *errstr;
803: u_int paneid;
804:
805: if (*arg != '%')
806: return (NULL);
807:
808: paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
809: if (errstr != NULL)
810: return (NULL);
811: return (window_pane_find_by_id(paneid));
812: }
813:
1.60 nicm 814: /* Lookup window id in a session. An initial @ means a window id. */
815: struct winlink *
816: cmd_lookup_winlink_windowid(struct session *s, const char *arg)
817: {
818: const char *errstr;
819: u_int windowid;
820:
821: if (*arg != '@')
822: return (NULL);
823:
824: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
825: if (errstr != NULL)
826: return (NULL);
827: return (winlink_find_by_window_id(&s->windows, windowid));
828: }
829:
830: /* Lookup window id. An initial @ means a window id. */
831: struct window *
832: cmd_lookup_windowid(const char *arg)
833: {
834: const char *errstr;
835: u_int windowid;
836:
837: if (*arg != '@')
838: return (NULL);
839:
840: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
841: if (errstr != NULL)
842: return (NULL);
843: return (window_find_by_id(windowid));
844: }
845:
846: /* Find session and winlink for window. */
1.51 nicm 847: struct session *
1.80 nicm 848: cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
1.51 nicm 849: {
850: struct session *s;
851: struct sessionslist ss;
852: struct winlink *wl;
853:
1.60 nicm 854: /* If this window is in the current session, return that winlink. */
1.80 nicm 855: s = cmd_current_session(cmdq, 0);
1.51 nicm 856: if (s != NULL) {
1.60 nicm 857: wl = winlink_find_by_window(&s->windows, w);
1.51 nicm 858: if (wl != NULL) {
859: if (wlp != NULL)
860: *wlp = wl;
861: return (s);
862: }
863: }
864:
1.60 nicm 865: /* Otherwise choose from all sessions with this window. */
1.51 nicm 866: ARRAY_INIT(&ss);
867: RB_FOREACH(s, sessions, &sessions) {
1.60 nicm 868: if (winlink_find_by_window(&s->windows, w) != NULL)
1.51 nicm 869: ARRAY_ADD(&ss, s);
870: }
871: s = cmd_choose_session_list(&ss);
872: ARRAY_FREE(&ss);
873: if (wlp != NULL)
1.60 nicm 874: *wlp = winlink_find_by_window(&s->windows, w);
1.51 nicm 875: return (s);
876: }
877:
1.5 nicm 878: /* Find the target session or report an error and return NULL. */
1.1 nicm 879: struct session *
1.80 nicm 880: cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
1.1 nicm 881: {
1.97 nicm 882: struct session *s;
883: struct client *c;
884: char *tmparg;
885: size_t arglen;
886: int ambiguous;
1.1 nicm 887:
1.5 nicm 888: /* A NULL argument means the current session. */
1.95 nicm 889: if (arg == NULL) {
890: if ((s = cmd_current_session(cmdq, prefer_unattached)) == NULL)
891: cmdq_error(cmdq, "can't establish current session");
892: return (s);
893: }
1.5 nicm 894:
895: /* Trim a single trailing colon if any. */
1.54 nicm 896: tmparg = xstrdup(arg);
1.5 nicm 897: arglen = strlen(tmparg);
898: if (arglen != 0 && tmparg[arglen - 1] == ':')
899: tmparg[arglen - 1] = '\0';
900:
1.53 nicm 901: /* An empty session name is the current session. */
902: if (*tmparg == '\0') {
1.68 nicm 903: free(tmparg);
1.95 nicm 904: if ((s = cmd_current_session(cmdq, prefer_unattached)) == NULL)
905: cmdq_error(cmdq, "can't establish current session");
906: return (s);
1.53 nicm 907: }
908:
1.5 nicm 909: /* Find the session, if any. */
1.97 nicm 910: s = cmd_lookup_session(cmdq, tmparg, &ambiguous);
1.5 nicm 911:
912: /* If it doesn't, try to match it as a client. */
913: if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
914: s = c->session;
915:
916: /* If no session found, report an error. */
917: if (s == NULL) {
918: if (ambiguous)
1.80 nicm 919: cmdq_error(cmdq, "more than one session: %s", tmparg);
1.5 nicm 920: else
1.80 nicm 921: cmdq_error(cmdq, "session not found: %s", tmparg);
1.1 nicm 922: }
1.5 nicm 923:
1.68 nicm 924: free(tmparg);
1.1 nicm 925: return (s);
926: }
927:
1.5 nicm 928: /* Find the target session and window or report an error and return NULL. */
1.1 nicm 929: struct winlink *
1.80 nicm 930: cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.1 nicm 931: {
1.97 nicm 932: struct session *s;
933: struct winlink *wl;
934: const char *winptr;
935: char *sessptr = NULL;
936: int ambiguous = 0;
1.5 nicm 937:
938: /*
939: * Find the current session. There must always be a current session, if
940: * it can't be found, report an error.
941: */
1.80 nicm 942: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
943: cmdq_error(cmdq, "can't establish current session");
1.5 nicm 944: return (NULL);
945: }
946:
947: /* A NULL argument means the current session and window. */
948: if (arg == NULL) {
949: if (sp != NULL)
950: *sp = s;
951: return (s->curw);
952: }
953:
954: /* Time to look at the argument. If it is empty, that is an error. */
955: if (*arg == '\0')
956: goto not_found;
957:
1.8 nicm 958: /* Find the separating colon and split into window and session. */
1.5 nicm 959: winptr = strchr(arg, ':');
960: if (winptr == NULL)
1.8 nicm 961: goto no_colon;
962: winptr++; /* skip : */
963: sessptr = xstrdup(arg);
964: *strchr(sessptr, ':') = '\0';
1.5 nicm 965:
966: /* Try to lookup the session if present. */
1.8 nicm 967: if (*sessptr != '\0') {
1.97 nicm 968: if ((s = cmd_lookup_session(cmdq, sessptr, &ambiguous)) == NULL)
1.5 nicm 969: goto no_session;
970: }
971: if (sp != NULL)
972: *sp = s;
973:
974: /*
975: * Then work out the window. An empty string is the current window,
1.38 nicm 976: * otherwise try special cases then to look it up in the session.
1.5 nicm 977: */
1.8 nicm 978: if (*winptr == '\0')
1.5 nicm 979: wl = s->curw;
1.38 nicm 980: else if (winptr[0] == '!' && winptr[1] == '\0')
981: wl = TAILQ_FIRST(&s->lastw);
1.73 nicm 982: else if (winptr[0] == '^' && winptr[1] == '\0')
983: wl = RB_MIN(winlinks, &s->windows);
984: else if (winptr[0] == '$' && winptr[1] == '\0')
985: wl = RB_MAX(winlinks, &s->windows);
1.43 nicm 986: else if (winptr[0] == '+' || winptr[0] == '-')
987: wl = cmd_find_window_offset(winptr, s, &ambiguous);
988: else
1.38 nicm 989: wl = cmd_lookup_window(s, winptr, &ambiguous);
990: if (wl == NULL)
1.5 nicm 991: goto not_found;
1.35 nicm 992:
1.5 nicm 993: if (sessptr != NULL)
1.68 nicm 994: free(sessptr);
1.5 nicm 995: return (wl);
996:
1.8 nicm 997: no_colon:
1.38 nicm 998: /*
999: * No colon in the string, first try special cases, then as a window
1000: * and lastly as a session.
1001: */
1.99 nicm 1002: if (arg[0] == '=' && arg[1] == '\0') {
1003: if ((wl = cmd_mouse_window(&cmdq->item->mouse, &s)) == NULL) {
1004: cmdq_error(cmdq, "no mouse target");
1005: goto error;
1006: }
1007: } else if (arg[0] == '!' && arg[1] == '\0') {
1.38 nicm 1008: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1009: goto not_found;
1.41 nicm 1010: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 1011: if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41 nicm 1012: goto lookup_session;
1013: } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
1014: goto lookup_session;
1.8 nicm 1015:
1016: if (sp != NULL)
1017: *sp = s;
1018:
1019: return (wl);
1020:
1.41 nicm 1021: lookup_session:
1022: if (ambiguous)
1023: goto not_found;
1.97 nicm 1024: if (*arg != '\0' &&
1025: (s = cmd_lookup_session(cmdq, arg, &ambiguous)) == NULL)
1.41 nicm 1026: goto no_session;
1027:
1028: if (sp != NULL)
1029: *sp = s;
1030:
1031: return (s->curw);
1032:
1.5 nicm 1033: no_session:
1034: if (ambiguous)
1.80 nicm 1035: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 1036: else
1.80 nicm 1037: cmdq_error(cmdq, "session not found: %s", arg);
1.99 nicm 1038: goto error;
1.5 nicm 1039:
1040: not_found:
1041: if (ambiguous)
1.80 nicm 1042: cmdq_error(cmdq, "multiple windows: %s", arg);
1.5 nicm 1043: else
1.80 nicm 1044: cmdq_error(cmdq, "window not found: %s", arg);
1.99 nicm 1045: goto error;
1046:
1047: error:
1.68 nicm 1048: free(sessptr);
1.5 nicm 1049: return (NULL);
1050: }
1.1 nicm 1051:
1.43 nicm 1052: struct winlink *
1053: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
1054: {
1055: struct winlink *wl;
1056: int offset = 1;
1057:
1058: if (winptr[1] != '\0')
1059: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1060: if (offset == 0)
1061: wl = cmd_lookup_window(s, winptr, ambiguous);
1062: else {
1063: if (winptr[0] == '+')
1064: wl = winlink_next_by_number(s->curw, s, offset);
1065: else
1066: wl = winlink_previous_by_number(s->curw, s, offset);
1067: }
1068:
1069: return (wl);
1070: }
1071:
1.5 nicm 1072: /*
1073: * Find the target session and window index, whether or not it exists in the
1074: * session. Return -2 on error or -1 if no window index is specified. This is
1.8 nicm 1075: * used when parsing an argument for a window target that may not exist (for
1076: * example if it is going to be created).
1.5 nicm 1077: */
1078: int
1.80 nicm 1079: cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.5 nicm 1080: {
1081: struct session *s;
1.38 nicm 1082: struct winlink *wl;
1.8 nicm 1083: const char *winptr;
1.5 nicm 1084: char *sessptr = NULL;
1085: int idx, ambiguous = 0;
1086:
1087: /*
1088: * Find the current session. There must always be a current session, if
1089: * it can't be found, report an error.
1090: */
1.80 nicm 1091: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1092: cmdq_error(cmdq, "can't establish current session");
1.9 nicm 1093: return (-2);
1.1 nicm 1094: }
1.5 nicm 1095:
1096: /* A NULL argument means the current session and "no window" (-1). */
1097: if (arg == NULL) {
1098: if (sp != NULL)
1099: *sp = s;
1100: return (-1);
1101: }
1102:
1103: /* Time to look at the argument. If it is empty, that is an error. */
1104: if (*arg == '\0')
1105: goto not_found;
1106:
1107: /* Find the separating colon. If none, assume the current session. */
1108: winptr = strchr(arg, ':');
1109: if (winptr == NULL)
1.8 nicm 1110: goto no_colon;
1111: winptr++; /* skip : */
1112: sessptr = xstrdup(arg);
1113: *strchr(sessptr, ':') = '\0';
1.5 nicm 1114:
1115: /* Try to lookup the session if present. */
1116: if (sessptr != NULL && *sessptr != '\0') {
1.97 nicm 1117: if ((s = cmd_lookup_session(cmdq, sessptr, &ambiguous)) == NULL)
1.5 nicm 1118: goto no_session;
1119: }
1.1 nicm 1120: if (sp != NULL)
1121: *sp = s;
1122:
1.5 nicm 1123: /*
1.8 nicm 1124: * Then work out the window. An empty string is a new window otherwise
1125: * try to look it up in the session.
1.5 nicm 1126: */
1.8 nicm 1127: if (*winptr == '\0')
1.38 nicm 1128: idx = -1;
1129: else if (winptr[0] == '!' && winptr[1] == '\0') {
1130: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1131: goto not_found;
1132: idx = wl->idx;
1.41 nicm 1133: } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43 nicm 1134: if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41 nicm 1135: goto invalid_index;
1136: } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1137: goto invalid_index;
1.35 nicm 1138:
1.68 nicm 1139: free(sessptr);
1.5 nicm 1140: return (idx);
1141:
1.8 nicm 1142: no_colon:
1.38 nicm 1143: /*
1144: * No colon in the string, first try special cases, then as a window
1145: * and lastly as a session.
1146: */
1147: if (arg[0] == '!' && arg[1] == '\0') {
1148: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1149: goto not_found;
1150: idx = wl->idx;
1.41 nicm 1151: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 1152: if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41 nicm 1153: goto lookup_session;
1154: } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1155: goto lookup_session;
1.8 nicm 1156:
1157: if (sp != NULL)
1158: *sp = s;
1159:
1160: return (idx);
1161:
1.41 nicm 1162: lookup_session:
1163: if (ambiguous)
1164: goto not_found;
1.97 nicm 1165: if (*arg != '\0' &&
1166: (s = cmd_lookup_session(cmdq, arg, &ambiguous)) == NULL)
1.41 nicm 1167: goto no_session;
1168:
1169: if (sp != NULL)
1170: *sp = s;
1171:
1172: return (-1);
1173:
1.5 nicm 1174: no_session:
1175: if (ambiguous)
1.80 nicm 1176: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 1177: else
1.80 nicm 1178: cmdq_error(cmdq, "session not found: %s", arg);
1.68 nicm 1179: free(sessptr);
1.5 nicm 1180: return (-2);
1181:
1.41 nicm 1182: invalid_index:
1183: if (ambiguous)
1184: goto not_found;
1.80 nicm 1185: cmdq_error(cmdq, "invalid index: %s", arg);
1.41 nicm 1186:
1.68 nicm 1187: free(sessptr);
1.41 nicm 1188: return (-2);
1189:
1.5 nicm 1190: not_found:
1191: if (ambiguous)
1.80 nicm 1192: cmdq_error(cmdq, "multiple windows: %s", arg);
1.1 nicm 1193: else
1.80 nicm 1194: cmdq_error(cmdq, "window not found: %s", arg);
1.68 nicm 1195: free(sessptr);
1.5 nicm 1196: return (-2);
1.12 nicm 1197: }
1198:
1.43 nicm 1199: int
1200: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1201: {
1202: int idx, offset = 1;
1203:
1204: if (winptr[1] != '\0')
1205: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1206: if (offset == 0)
1207: idx = cmd_lookup_index(s, winptr, ambiguous);
1208: else {
1209: if (winptr[0] == '+') {
1210: if (s->curw->idx == INT_MAX)
1211: idx = cmd_lookup_index(s, winptr, ambiguous);
1212: else
1213: idx = s->curw->idx + offset;
1214: } else {
1215: if (s->curw->idx == 0)
1216: idx = cmd_lookup_index(s, winptr, ambiguous);
1217: else
1218: idx = s->curw->idx - offset;
1219: }
1220: }
1221:
1222: return (idx);
1223: }
1224:
1.12 nicm 1225: /*
1226: * Find the target session, window and pane number or report an error and
1227: * return NULL. The pane number is separated from the session:window by a .,
1228: * such as mysession:mywindow.0.
1229: */
1230: struct winlink *
1.80 nicm 1231: cmd_find_pane(struct cmd_q *cmdq,
1.12 nicm 1232: const char *arg, struct session **sp, struct window_pane **wpp)
1233: {
1.55 nicm 1234: struct session *s;
1235: struct winlink *wl;
1236: const char *period, *errstr;
1237: char *winptr, *paneptr;
1238: u_int idx;
1.12 nicm 1239:
1240: /* Get the current session. */
1.80 nicm 1241: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1242: cmdq_error(cmdq, "can't establish current session");
1.12 nicm 1243: return (NULL);
1244: }
1245: if (sp != NULL)
1246: *sp = s;
1247:
1248: /* A NULL argument means the current session, window and pane. */
1249: if (arg == NULL) {
1250: *wpp = s->curw->window->active;
1251: return (s->curw);
1.51 nicm 1252: }
1253:
1254: /* Lookup as pane id. */
1255: if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1.80 nicm 1256: s = cmd_window_session(cmdq, (*wpp)->window, &wl);
1.51 nicm 1257: if (sp != NULL)
1258: *sp = s;
1259: return (wl);
1.12 nicm 1260: }
1261:
1262: /* Look for a separating period. */
1263: if ((period = strrchr(arg, '.')) == NULL)
1264: goto no_period;
1265:
1266: /* Pull out the window part and parse it. */
1267: winptr = xstrdup(arg);
1268: winptr[period - arg] = '\0';
1269: if (*winptr == '\0')
1270: wl = s->curw;
1.80 nicm 1271: else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
1.12 nicm 1272: goto error;
1273:
1274: /* Find the pane section and look it up. */
1275: paneptr = winptr + (period - arg) + 1;
1276: if (*paneptr == '\0')
1277: *wpp = wl->window->active;
1.43 nicm 1278: else if (paneptr[0] == '+' || paneptr[0] == '-')
1279: *wpp = cmd_find_pane_offset(paneptr, wl);
1.96 nicm 1280: else if (paneptr[0] == '!' && paneptr[1] == '\0') {
1281: if (wl->window->last == NULL) {
1282: cmdq_error(cmdq, "no last pane");
1283: goto error;
1284: }
1285: *wpp = wl->window->last;
1286: } else {
1.12 nicm 1287: idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36 nicm 1288: if (errstr != NULL)
1289: goto lookup_string;
1.12 nicm 1290: *wpp = window_pane_at_index(wl->window, idx);
1.36 nicm 1291: if (*wpp == NULL)
1292: goto lookup_string;
1.12 nicm 1293: }
1294:
1.68 nicm 1295: free(winptr);
1.12 nicm 1296: return (wl);
1297:
1.36 nicm 1298: lookup_string:
1299: /* Try pane string description. */
1.55 nicm 1300: if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1.80 nicm 1301: cmdq_error(cmdq, "can't find pane: %s", paneptr);
1.36 nicm 1302: goto error;
1303: }
1304:
1.68 nicm 1305: free(winptr);
1.39 nicm 1306: return (wl);
1.36 nicm 1307:
1.12 nicm 1308: no_period:
1.99 nicm 1309: /* Check mouse event. */
1310: if (arg[0] == '=' && arg[1] == '\0') {
1311: *wpp = cmd_mouse_pane(&cmdq->item->mouse, &s, &wl);
1312: if (*wpp == NULL) {
1313: cmdq_error(cmdq, "no mouse target");
1314: return (NULL);
1315: }
1316: if (sp != NULL)
1317: *sp = s;
1318: return (wl);
1319: }
1320:
1.12 nicm 1321: /* Try as a pane number alone. */
1322: idx = strtonum(arg, 0, INT_MAX, &errstr);
1323: if (errstr != NULL)
1324: goto lookup_window;
1325:
1326: /* Try index in the current session and window. */
1327: if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1328: goto lookup_window;
1.35 nicm 1329:
1.12 nicm 1330: return (s->curw);
1331:
1332: lookup_window:
1.36 nicm 1333: /* Try pane string description. */
1.55 nicm 1334: if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1.36 nicm 1335: return (s->curw);
1336:
1.12 nicm 1337: /* Try as a window and use the active pane. */
1.80 nicm 1338: if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
1.12 nicm 1339: *wpp = wl->window->active;
1340: return (wl);
1.35 nicm 1341:
1.12 nicm 1342: error:
1.68 nicm 1343: free(winptr);
1.12 nicm 1344: return (NULL);
1.43 nicm 1345: }
1346:
1347: struct window_pane *
1348: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1349: {
1350: struct window *w = wl->window;
1351: struct window_pane *wp = w->active;
1352: u_int offset = 1;
1353:
1354: if (paneptr[1] != '\0')
1355: offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1356: if (offset > 0) {
1357: if (paneptr[0] == '+')
1358: wp = window_pane_next_by_number(w, wp, offset);
1359: else
1360: wp = window_pane_previous_by_number(w, wp, offset);
1361: }
1362:
1363: return (wp);
1.15 nicm 1364: }
1365:
1366: /* Replace the first %% or %idx in template by s. */
1367: char *
1.75 nicm 1368: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 1369: {
1.75 nicm 1370: char ch, *buf;
1371: const char *ptr;
1.84 nicm 1372: int replaced;
1373: size_t len;
1.15 nicm 1374:
1.76 nicm 1375: if (strchr(template, '%') == NULL)
1.15 nicm 1376: return (xstrdup(template));
1377:
1378: buf = xmalloc(1);
1379: *buf = '\0';
1380: len = 0;
1381: replaced = 0;
1382:
1383: ptr = template;
1384: while (*ptr != '\0') {
1385: switch (ch = *ptr++) {
1386: case '%':
1387: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1388: if (*ptr != '%' || replaced)
1389: break;
1390: replaced = 1;
1391: }
1392: ptr++;
1393:
1394: len += strlen(s);
1.98 nicm 1395: buf = xrealloc(buf, len + 1);
1.15 nicm 1396: strlcat(buf, s, len + 1);
1397: continue;
1398: }
1.98 nicm 1399: buf = xrealloc(buf, len + 2);
1.15 nicm 1400: buf[len++] = ch;
1401: buf[len] = '\0';
1402: }
1403:
1404: return (buf);
1.1 nicm 1405: }