Add a wait-for command which blocks a client on a named channel until it
[tmux-openbsd.git] / cmd.c
blob33ce730fdad2f3a00788a12c7ddc2e52d91ab59b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/time.h>
22 #include <fnmatch.h>
23 #include <paths.h>
24 #include <pwd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "tmux.h"
31 const struct cmd_entry *cmd_table[] = {
32 &cmd_attach_session_entry,
33 &cmd_bind_key_entry,
34 &cmd_break_pane_entry,
35 &cmd_capture_pane_entry,
36 &cmd_choose_buffer_entry,
37 &cmd_choose_client_entry,
38 &cmd_choose_list_entry,
39 &cmd_choose_session_entry,
40 &cmd_choose_tree_entry,
41 &cmd_choose_window_entry,
42 &cmd_clear_history_entry,
43 &cmd_clock_mode_entry,
44 &cmd_command_prompt_entry,
45 &cmd_confirm_before_entry,
46 &cmd_copy_mode_entry,
47 &cmd_delete_buffer_entry,
48 &cmd_detach_client_entry,
49 &cmd_display_message_entry,
50 &cmd_display_panes_entry,
51 &cmd_find_window_entry,
52 &cmd_has_session_entry,
53 &cmd_if_shell_entry,
54 &cmd_join_pane_entry,
55 &cmd_kill_pane_entry,
56 &cmd_kill_server_entry,
57 &cmd_kill_session_entry,
58 &cmd_kill_window_entry,
59 &cmd_last_pane_entry,
60 &cmd_last_window_entry,
61 &cmd_link_window_entry,
62 &cmd_list_buffers_entry,
63 &cmd_list_clients_entry,
64 &cmd_list_commands_entry,
65 &cmd_list_keys_entry,
66 &cmd_list_panes_entry,
67 &cmd_list_sessions_entry,
68 &cmd_list_windows_entry,
69 &cmd_load_buffer_entry,
70 &cmd_lock_client_entry,
71 &cmd_lock_server_entry,
72 &cmd_lock_session_entry,
73 &cmd_move_pane_entry,
74 &cmd_move_window_entry,
75 &cmd_new_session_entry,
76 &cmd_new_window_entry,
77 &cmd_next_layout_entry,
78 &cmd_next_window_entry,
79 &cmd_paste_buffer_entry,
80 &cmd_pipe_pane_entry,
81 &cmd_previous_layout_entry,
82 &cmd_previous_window_entry,
83 &cmd_refresh_client_entry,
84 &cmd_rename_session_entry,
85 &cmd_rename_window_entry,
86 &cmd_resize_pane_entry,
87 &cmd_respawn_pane_entry,
88 &cmd_respawn_window_entry,
89 &cmd_rotate_window_entry,
90 &cmd_run_shell_entry,
91 &cmd_save_buffer_entry,
92 &cmd_select_layout_entry,
93 &cmd_select_pane_entry,
94 &cmd_select_window_entry,
95 &cmd_send_keys_entry,
96 &cmd_send_prefix_entry,
97 &cmd_server_info_entry,
98 &cmd_set_buffer_entry,
99 &cmd_set_environment_entry,
100 &cmd_set_option_entry,
101 &cmd_set_window_option_entry,
102 &cmd_show_buffer_entry,
103 &cmd_show_environment_entry,
104 &cmd_show_messages_entry,
105 &cmd_show_options_entry,
106 &cmd_show_window_options_entry,
107 &cmd_source_file_entry,
108 &cmd_split_window_entry,
109 &cmd_start_server_entry,
110 &cmd_suspend_client_entry,
111 &cmd_swap_pane_entry,
112 &cmd_swap_window_entry,
113 &cmd_switch_client_entry,
114 &cmd_unbind_key_entry,
115 &cmd_unlink_window_entry,
116 &cmd_wait_for_entry,
117 NULL
120 int cmd_session_better(struct session *, struct session *, int);
121 struct session *cmd_choose_session_list(struct sessionslist *);
122 struct session *cmd_choose_session(int);
123 struct client *cmd_choose_client(struct clients *);
124 struct client *cmd_lookup_client(const char *);
125 struct session *cmd_lookup_session(const char *, int *);
126 struct winlink *cmd_lookup_window(struct session *, const char *, int *);
127 int cmd_lookup_index(struct session *, const char *, int *);
128 struct window_pane *cmd_lookup_paneid(const char *);
129 struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
130 struct window *cmd_lookup_windowid(const char *);
131 struct session *cmd_window_session(struct cmd_q *, struct window *,
132 struct winlink **);
133 struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
134 int cmd_find_index_offset(const char *, struct session *, int *);
135 struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
138 cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
140 size_t arglen;
141 int i;
143 *buf = '\0';
144 for (i = 0; i < argc; i++) {
145 if (strlcpy(buf, argv[i], len) >= len)
146 return (-1);
147 arglen = strlen(argv[i]) + 1;
148 buf += arglen;
149 len -= arglen;
152 return (0);
156 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
158 int i;
159 size_t arglen;
161 if (argc == 0)
162 return (0);
163 *argv = xcalloc(argc, sizeof **argv);
165 buf[len - 1] = '\0';
166 for (i = 0; i < argc; i++) {
167 if (len == 0) {
168 cmd_free_argv(argc, *argv);
169 return (-1);
172 arglen = strlen(buf) + 1;
173 (*argv)[i] = xstrdup(buf);
174 buf += arglen;
175 len -= arglen;
178 return (0);
181 char **
182 cmd_copy_argv(int argc, char *const *argv)
184 char **new_argv;
185 int i;
187 if (argc == 0)
188 return (NULL);
189 new_argv = xcalloc(argc, sizeof *new_argv);
190 for (i = 0; i < argc; i++) {
191 if (argv[i] != NULL)
192 new_argv[i] = xstrdup(argv[i]);
194 return (new_argv);
197 void
198 cmd_free_argv(int argc, char **argv)
200 int i;
202 if (argc == 0)
203 return;
204 for (i = 0; i < argc; i++)
205 free(argv[i]);
206 free(argv);
209 struct cmd *
210 cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
212 const struct cmd_entry **entryp, *entry;
213 struct cmd *cmd;
214 struct args *args;
215 char s[BUFSIZ];
216 int ambiguous = 0;
218 *cause = NULL;
219 if (argc == 0) {
220 xasprintf(cause, "no command");
221 return (NULL);
224 entry = NULL;
225 for (entryp = cmd_table; *entryp != NULL; entryp++) {
226 if ((*entryp)->alias != NULL &&
227 strcmp((*entryp)->alias, argv[0]) == 0) {
228 ambiguous = 0;
229 entry = *entryp;
230 break;
233 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
234 continue;
235 if (entry != NULL)
236 ambiguous = 1;
237 entry = *entryp;
239 /* Bail now if an exact match. */
240 if (strcmp(entry->name, argv[0]) == 0)
241 break;
243 if (ambiguous)
244 goto ambiguous;
245 if (entry == NULL) {
246 xasprintf(cause, "unknown command: %s", argv[0]);
247 return (NULL);
250 args = args_parse(entry->args_template, argc, argv);
251 if (args == NULL)
252 goto usage;
253 if (entry->args_lower != -1 && args->argc < entry->args_lower)
254 goto usage;
255 if (entry->args_upper != -1 && args->argc > entry->args_upper)
256 goto usage;
257 if (entry->check != NULL && entry->check(args) != 0)
258 goto usage;
260 cmd = xcalloc(1, sizeof *cmd);
261 cmd->entry = entry;
262 cmd->args = args;
264 if (file != NULL)
265 cmd->file = xstrdup(file);
266 cmd->line = line;
268 return (cmd);
270 ambiguous:
271 *s = '\0';
272 for (entryp = cmd_table; *entryp != NULL; entryp++) {
273 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
274 continue;
275 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
276 break;
277 if (strlcat(s, ", ", sizeof s) >= sizeof s)
278 break;
280 s[strlen(s) - 2] = '\0';
281 xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
282 return (NULL);
284 usage:
285 if (args != NULL)
286 args_free(args);
287 xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
288 return (NULL);
291 size_t
292 cmd_print(struct cmd *cmd, char *buf, size_t len)
294 size_t off, used;
296 off = xsnprintf(buf, len, "%s ", cmd->entry->name);
297 if (off < len) {
298 used = args_print(cmd->args, buf + off, len - off);
299 if (used == 0)
300 off--;
301 else
302 off += used;
303 buf[off] = '\0';
305 return (off);
309 * Figure out the current session. Use: 1) the current session, if the command
310 * context has one; 2) the most recently used session containing the pty of the
311 * calling client, if any; 3) the session specified in the TMUX variable from
312 * the environment (as passed from the client); 4) the most recently used
313 * session from all sessions.
315 struct session *
316 cmd_current_session(struct cmd_q *cmdq, int prefer_unattached)
318 struct msg_command_data *data = cmdq->msgdata;
319 struct client *c = cmdq->client;
320 struct session *s;
321 struct sessionslist ss;
322 struct winlink *wl;
323 struct window_pane *wp;
324 const char *path;
325 int found;
327 if (c != NULL && c->session != NULL)
328 return (c->session);
331 * If the name of the calling client's pty is known, build a list of
332 * the sessions that contain it and if any choose either the first or
333 * the newest.
335 path = c == NULL ? NULL : c->tty.path;
336 if (path != NULL) {
337 ARRAY_INIT(&ss);
338 RB_FOREACH(s, sessions, &sessions) {
339 found = 0;
340 RB_FOREACH(wl, winlinks, &s->windows) {
341 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
342 if (strcmp(wp->tty, path) == 0) {
343 found = 1;
344 break;
347 if (found)
348 break;
350 if (found)
351 ARRAY_ADD(&ss, s);
354 s = cmd_choose_session_list(&ss);
355 ARRAY_FREE(&ss);
356 if (s != NULL)
357 return (s);
360 /* Use the session from the TMUX environment variable. */
361 if (data != NULL && data->pid == getpid() && data->idx != -1) {
362 s = session_find_by_index(data->idx);
363 if (s != NULL)
364 return (s);
367 return (cmd_choose_session(prefer_unattached));
370 /* Is this session better? */
372 cmd_session_better(struct session *s, struct session *best,
373 int prefer_unattached)
375 if (best == NULL)
376 return (1);
377 if (prefer_unattached) {
378 if (!(best->flags & SESSION_UNATTACHED) &&
379 (s->flags & SESSION_UNATTACHED))
380 return (1);
381 else if ((best->flags & SESSION_UNATTACHED) &&
382 !(s->flags & SESSION_UNATTACHED))
383 return (0);
385 return (timercmp(&s->activity_time, &best->activity_time, >));
389 * Find the most recently used session, preferring unattached if the flag is
390 * set.
392 struct session *
393 cmd_choose_session(int prefer_unattached)
395 struct session *s, *best;
397 best = NULL;
398 RB_FOREACH(s, sessions, &sessions) {
399 if (cmd_session_better(s, best, prefer_unattached))
400 best = s;
402 return (best);
405 /* Find the most recently used session from a list. */
406 struct session *
407 cmd_choose_session_list(struct sessionslist *ss)
409 struct session *s, *sbest;
410 struct timeval *tv = NULL;
411 u_int i;
413 sbest = NULL;
414 for (i = 0; i < ARRAY_LENGTH(ss); i++) {
415 if ((s = ARRAY_ITEM(ss, i)) == NULL)
416 continue;
418 if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
419 sbest = s;
420 tv = &s->activity_time;
424 return (sbest);
428 * Find the current client. First try the current client if set, then pick the
429 * most recently used of the clients attached to the current session if any,
430 * then of all clients.
432 struct client *
433 cmd_current_client(struct cmd_q *cmdq)
435 struct session *s;
436 struct client *c;
437 struct clients cc;
438 u_int i;
440 if (cmdq->client != NULL && cmdq->client->session != NULL)
441 return (cmdq->client);
444 * No current client set. Find the current session and return the
445 * newest of its clients.
447 s = cmd_current_session(cmdq, 0);
448 if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
449 ARRAY_INIT(&cc);
450 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
451 if ((c = ARRAY_ITEM(&clients, i)) == NULL)
452 continue;
453 if (s == c->session)
454 ARRAY_ADD(&cc, c);
457 c = cmd_choose_client(&cc);
458 ARRAY_FREE(&cc);
459 if (c != NULL)
460 return (c);
463 return (cmd_choose_client(&clients));
466 /* Choose the most recently used client from a list. */
467 struct client *
468 cmd_choose_client(struct clients *cc)
470 struct client *c, *cbest;
471 struct timeval *tv = NULL;
472 u_int i;
474 cbest = NULL;
475 for (i = 0; i < ARRAY_LENGTH(cc); i++) {
476 if ((c = ARRAY_ITEM(cc, i)) == NULL)
477 continue;
478 if (c->session == NULL)
479 continue;
481 if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
482 cbest = c;
483 tv = &c->activity_time;
487 return (cbest);
490 /* Find the target client or report an error and return NULL. */
491 struct client *
492 cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet)
494 struct client *c;
495 char *tmparg;
496 size_t arglen;
498 /* A NULL argument means the current client. */
499 if (arg == NULL) {
500 c = cmd_current_client(cmdq);
501 if (c == NULL && !quiet)
502 cmdq_error(cmdq, "no clients");
503 return (c);
505 tmparg = xstrdup(arg);
507 /* Trim a single trailing colon if any. */
508 arglen = strlen(tmparg);
509 if (arglen != 0 && tmparg[arglen - 1] == ':')
510 tmparg[arglen - 1] = '\0';
512 /* Find the client, if any. */
513 c = cmd_lookup_client(tmparg);
515 /* If no client found, report an error. */
516 if (c == NULL && !quiet)
517 cmdq_error(cmdq, "client not found: %s", tmparg);
519 free(tmparg);
520 return (c);
524 * Lookup a client by device path. Either of a full match and a match without a
525 * leading _PATH_DEV ("/dev/") is accepted.
527 struct client *
528 cmd_lookup_client(const char *name)
530 struct client *c;
531 const char *path;
532 u_int i;
534 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
535 c = ARRAY_ITEM(&clients, i);
536 if (c == NULL || c->session == NULL || c->tty.path == NULL)
537 continue;
538 path = c->tty.path;
540 /* Check for exact matches. */
541 if (strcmp(name, path) == 0)
542 return (c);
544 /* Check without leading /dev if present. */
545 if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
546 continue;
547 if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
548 return (c);
551 return (NULL);
554 /* Lookup a session by name. If no session is found, NULL is returned. */
555 struct session *
556 cmd_lookup_session(const char *name, int *ambiguous)
558 struct session *s, *sfound;
560 *ambiguous = 0;
563 * Look for matches. First look for exact matches - session names must
564 * be unique so an exact match can't be ambigious and can just be
565 * returned.
567 if ((s = session_find(name)) != NULL)
568 return (s);
571 * Otherwise look for partial matches, returning early if it is found to
572 * be ambiguous.
574 sfound = NULL;
575 RB_FOREACH(s, sessions, &sessions) {
576 if (strncmp(name, s->name, strlen(name)) == 0 ||
577 fnmatch(name, s->name, 0) == 0) {
578 if (sfound != NULL) {
579 *ambiguous = 1;
580 return (NULL);
582 sfound = s;
585 return (sfound);
589 * Lookup a window or return -1 if not found or ambigious. First try as an
590 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
591 * idx if the window index is a valid number but there is no window with that
592 * index.
594 struct winlink *
595 cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
597 struct winlink *wl, *wlfound;
598 const char *errstr;
599 u_int idx;
601 *ambiguous = 0;
603 /* Try as a window id. */
604 if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
605 return (wl);
607 /* First see if this is a valid window index in this session. */
608 idx = strtonum(name, 0, INT_MAX, &errstr);
609 if (errstr == NULL) {
610 if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
611 return (wl);
614 /* Look for exact matches, error if more than one. */
615 wlfound = NULL;
616 RB_FOREACH(wl, winlinks, &s->windows) {
617 if (strcmp(name, wl->window->name) == 0) {
618 if (wlfound != NULL) {
619 *ambiguous = 1;
620 return (NULL);
622 wlfound = wl;
625 if (wlfound != NULL)
626 return (wlfound);
628 /* Now look for pattern matches, again error if multiple. */
629 wlfound = NULL;
630 RB_FOREACH(wl, winlinks, &s->windows) {
631 if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
632 fnmatch(name, wl->window->name, 0) == 0) {
633 if (wlfound != NULL) {
634 *ambiguous = 1;
635 return (NULL);
637 wlfound = wl;
640 if (wlfound != NULL)
641 return (wlfound);
643 return (NULL);
647 * Find a window index - if the window doesn't exist, check if it is a
648 * potential index and return it anyway.
651 cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
653 struct winlink *wl;
654 const char *errstr;
655 u_int idx;
657 if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
658 return (wl->idx);
659 if (*ambiguous)
660 return (-1);
662 idx = strtonum(name, 0, INT_MAX, &errstr);
663 if (errstr == NULL)
664 return (idx);
666 return (-1);
669 /* Lookup pane id. An initial % means a pane id. */
670 struct window_pane *
671 cmd_lookup_paneid(const char *arg)
673 const char *errstr;
674 u_int paneid;
676 if (*arg != '%')
677 return (NULL);
679 paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
680 if (errstr != NULL)
681 return (NULL);
682 return (window_pane_find_by_id(paneid));
685 /* Lookup window id in a session. An initial @ means a window id. */
686 struct winlink *
687 cmd_lookup_winlink_windowid(struct session *s, const char *arg)
689 const char *errstr;
690 u_int windowid;
692 if (*arg != '@')
693 return (NULL);
695 windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
696 if (errstr != NULL)
697 return (NULL);
698 return (winlink_find_by_window_id(&s->windows, windowid));
701 /* Lookup window id. An initial @ means a window id. */
702 struct window *
703 cmd_lookup_windowid(const char *arg)
705 const char *errstr;
706 u_int windowid;
708 if (*arg != '@')
709 return (NULL);
711 windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
712 if (errstr != NULL)
713 return (NULL);
714 return (window_find_by_id(windowid));
717 /* Find session and winlink for window. */
718 struct session *
719 cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
721 struct session *s;
722 struct sessionslist ss;
723 struct winlink *wl;
725 /* If this window is in the current session, return that winlink. */
726 s = cmd_current_session(cmdq, 0);
727 if (s != NULL) {
728 wl = winlink_find_by_window(&s->windows, w);
729 if (wl != NULL) {
730 if (wlp != NULL)
731 *wlp = wl;
732 return (s);
736 /* Otherwise choose from all sessions with this window. */
737 ARRAY_INIT(&ss);
738 RB_FOREACH(s, sessions, &sessions) {
739 if (winlink_find_by_window(&s->windows, w) != NULL)
740 ARRAY_ADD(&ss, s);
742 s = cmd_choose_session_list(&ss);
743 ARRAY_FREE(&ss);
744 if (wlp != NULL)
745 *wlp = winlink_find_by_window(&s->windows, w);
746 return (s);
749 /* Find the target session or report an error and return NULL. */
750 struct session *
751 cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
753 struct session *s;
754 struct window_pane *wp;
755 struct window *w;
756 struct client *c;
757 char *tmparg;
758 size_t arglen;
759 int ambiguous;
761 /* A NULL argument means the current session. */
762 if (arg == NULL)
763 return (cmd_current_session(cmdq, prefer_unattached));
765 /* Lookup as pane id or window id. */
766 if ((wp = cmd_lookup_paneid(arg)) != NULL)
767 return (cmd_window_session(cmdq, wp->window, NULL));
768 if ((w = cmd_lookup_windowid(arg)) != NULL)
769 return (cmd_window_session(cmdq, w, NULL));
771 /* Trim a single trailing colon if any. */
772 tmparg = xstrdup(arg);
773 arglen = strlen(tmparg);
774 if (arglen != 0 && tmparg[arglen - 1] == ':')
775 tmparg[arglen - 1] = '\0';
777 /* An empty session name is the current session. */
778 if (*tmparg == '\0') {
779 free(tmparg);
780 return (cmd_current_session(cmdq, prefer_unattached));
783 /* Find the session, if any. */
784 s = cmd_lookup_session(tmparg, &ambiguous);
786 /* If it doesn't, try to match it as a client. */
787 if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
788 s = c->session;
790 /* If no session found, report an error. */
791 if (s == NULL) {
792 if (ambiguous)
793 cmdq_error(cmdq, "more than one session: %s", tmparg);
794 else
795 cmdq_error(cmdq, "session not found: %s", tmparg);
798 free(tmparg);
799 return (s);
802 /* Find the target session and window or report an error and return NULL. */
803 struct winlink *
804 cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
806 struct session *s;
807 struct winlink *wl;
808 struct window_pane *wp;
809 const char *winptr;
810 char *sessptr = NULL;
811 int ambiguous = 0;
814 * Find the current session. There must always be a current session, if
815 * it can't be found, report an error.
817 if ((s = cmd_current_session(cmdq, 0)) == NULL) {
818 cmdq_error(cmdq, "can't establish current session");
819 return (NULL);
822 /* A NULL argument means the current session and window. */
823 if (arg == NULL) {
824 if (sp != NULL)
825 *sp = s;
826 return (s->curw);
829 /* Lookup as pane id. */
830 if ((wp = cmd_lookup_paneid(arg)) != NULL) {
831 s = cmd_window_session(cmdq, wp->window, &wl);
832 if (sp != NULL)
833 *sp = s;
834 return (wl);
837 /* Time to look at the argument. If it is empty, that is an error. */
838 if (*arg == '\0')
839 goto not_found;
841 /* Find the separating colon and split into window and session. */
842 winptr = strchr(arg, ':');
843 if (winptr == NULL)
844 goto no_colon;
845 winptr++; /* skip : */
846 sessptr = xstrdup(arg);
847 *strchr(sessptr, ':') = '\0';
849 /* Try to lookup the session if present. */
850 if (*sessptr != '\0') {
851 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
852 goto no_session;
854 if (sp != NULL)
855 *sp = s;
858 * Then work out the window. An empty string is the current window,
859 * otherwise try special cases then to look it up in the session.
861 if (*winptr == '\0')
862 wl = s->curw;
863 else if (winptr[0] == '!' && winptr[1] == '\0')
864 wl = TAILQ_FIRST(&s->lastw);
865 else if (winptr[0] == '^' && winptr[1] == '\0')
866 wl = RB_MIN(winlinks, &s->windows);
867 else if (winptr[0] == '$' && winptr[1] == '\0')
868 wl = RB_MAX(winlinks, &s->windows);
869 else if (winptr[0] == '+' || winptr[0] == '-')
870 wl = cmd_find_window_offset(winptr, s, &ambiguous);
871 else
872 wl = cmd_lookup_window(s, winptr, &ambiguous);
873 if (wl == NULL)
874 goto not_found;
876 if (sessptr != NULL)
877 free(sessptr);
878 return (wl);
880 no_colon:
882 * No colon in the string, first try special cases, then as a window
883 * and lastly as a session.
885 if (arg[0] == '!' && arg[1] == '\0') {
886 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
887 goto not_found;
888 } else if (arg[0] == '+' || arg[0] == '-') {
889 if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
890 goto lookup_session;
891 } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
892 goto lookup_session;
894 if (sp != NULL)
895 *sp = s;
897 return (wl);
899 lookup_session:
900 if (ambiguous)
901 goto not_found;
902 if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
903 goto no_session;
905 if (sp != NULL)
906 *sp = s;
908 return (s->curw);
910 no_session:
911 if (ambiguous)
912 cmdq_error(cmdq, "multiple sessions: %s", arg);
913 else
914 cmdq_error(cmdq, "session not found: %s", arg);
915 free(sessptr);
916 return (NULL);
918 not_found:
919 if (ambiguous)
920 cmdq_error(cmdq, "multiple windows: %s", arg);
921 else
922 cmdq_error(cmdq, "window not found: %s", arg);
923 free(sessptr);
924 return (NULL);
927 struct winlink *
928 cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
930 struct winlink *wl;
931 int offset = 1;
933 if (winptr[1] != '\0')
934 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
935 if (offset == 0)
936 wl = cmd_lookup_window(s, winptr, ambiguous);
937 else {
938 if (winptr[0] == '+')
939 wl = winlink_next_by_number(s->curw, s, offset);
940 else
941 wl = winlink_previous_by_number(s->curw, s, offset);
944 return (wl);
948 * Find the target session and window index, whether or not it exists in the
949 * session. Return -2 on error or -1 if no window index is specified. This is
950 * used when parsing an argument for a window target that may not exist (for
951 * example if it is going to be created).
954 cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
956 struct session *s;
957 struct winlink *wl;
958 const char *winptr;
959 char *sessptr = NULL;
960 int idx, ambiguous = 0;
963 * Find the current session. There must always be a current session, if
964 * it can't be found, report an error.
966 if ((s = cmd_current_session(cmdq, 0)) == NULL) {
967 cmdq_error(cmdq, "can't establish current session");
968 return (-2);
971 /* A NULL argument means the current session and "no window" (-1). */
972 if (arg == NULL) {
973 if (sp != NULL)
974 *sp = s;
975 return (-1);
978 /* Time to look at the argument. If it is empty, that is an error. */
979 if (*arg == '\0')
980 goto not_found;
982 /* Find the separating colon. If none, assume the current session. */
983 winptr = strchr(arg, ':');
984 if (winptr == NULL)
985 goto no_colon;
986 winptr++; /* skip : */
987 sessptr = xstrdup(arg);
988 *strchr(sessptr, ':') = '\0';
990 /* Try to lookup the session if present. */
991 if (sessptr != NULL && *sessptr != '\0') {
992 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
993 goto no_session;
995 if (sp != NULL)
996 *sp = s;
999 * Then work out the window. An empty string is a new window otherwise
1000 * try to look it up in the session.
1002 if (*winptr == '\0')
1003 idx = -1;
1004 else if (winptr[0] == '!' && winptr[1] == '\0') {
1005 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1006 goto not_found;
1007 idx = wl->idx;
1008 } else if (winptr[0] == '+' || winptr[0] == '-') {
1009 if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1010 goto invalid_index;
1011 } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1012 goto invalid_index;
1014 free(sessptr);
1015 return (idx);
1017 no_colon:
1019 * No colon in the string, first try special cases, then as a window
1020 * and lastly as a session.
1022 if (arg[0] == '!' && arg[1] == '\0') {
1023 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1024 goto not_found;
1025 idx = wl->idx;
1026 } else if (arg[0] == '+' || arg[0] == '-') {
1027 if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1028 goto lookup_session;
1029 } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1030 goto lookup_session;
1032 if (sp != NULL)
1033 *sp = s;
1035 return (idx);
1037 lookup_session:
1038 if (ambiguous)
1039 goto not_found;
1040 if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1041 goto no_session;
1043 if (sp != NULL)
1044 *sp = s;
1046 return (-1);
1048 no_session:
1049 if (ambiguous)
1050 cmdq_error(cmdq, "multiple sessions: %s", arg);
1051 else
1052 cmdq_error(cmdq, "session not found: %s", arg);
1053 free(sessptr);
1054 return (-2);
1056 invalid_index:
1057 if (ambiguous)
1058 goto not_found;
1059 cmdq_error(cmdq, "invalid index: %s", arg);
1061 free(sessptr);
1062 return (-2);
1064 not_found:
1065 if (ambiguous)
1066 cmdq_error(cmdq, "multiple windows: %s", arg);
1067 else
1068 cmdq_error(cmdq, "window not found: %s", arg);
1069 free(sessptr);
1070 return (-2);
1074 cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1076 int idx, offset = 1;
1078 if (winptr[1] != '\0')
1079 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1080 if (offset == 0)
1081 idx = cmd_lookup_index(s, winptr, ambiguous);
1082 else {
1083 if (winptr[0] == '+') {
1084 if (s->curw->idx == INT_MAX)
1085 idx = cmd_lookup_index(s, winptr, ambiguous);
1086 else
1087 idx = s->curw->idx + offset;
1088 } else {
1089 if (s->curw->idx == 0)
1090 idx = cmd_lookup_index(s, winptr, ambiguous);
1091 else
1092 idx = s->curw->idx - offset;
1096 return (idx);
1100 * Find the target session, window and pane number or report an error and
1101 * return NULL. The pane number is separated from the session:window by a .,
1102 * such as mysession:mywindow.0.
1104 struct winlink *
1105 cmd_find_pane(struct cmd_q *cmdq,
1106 const char *arg, struct session **sp, struct window_pane **wpp)
1108 struct session *s;
1109 struct winlink *wl;
1110 const char *period, *errstr;
1111 char *winptr, *paneptr;
1112 u_int idx;
1114 /* Get the current session. */
1115 if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1116 cmdq_error(cmdq, "can't establish current session");
1117 return (NULL);
1119 if (sp != NULL)
1120 *sp = s;
1122 /* A NULL argument means the current session, window and pane. */
1123 if (arg == NULL) {
1124 *wpp = s->curw->window->active;
1125 return (s->curw);
1128 /* Lookup as pane id. */
1129 if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1130 s = cmd_window_session(cmdq, (*wpp)->window, &wl);
1131 if (sp != NULL)
1132 *sp = s;
1133 return (wl);
1136 /* Look for a separating period. */
1137 if ((period = strrchr(arg, '.')) == NULL)
1138 goto no_period;
1140 /* Pull out the window part and parse it. */
1141 winptr = xstrdup(arg);
1142 winptr[period - arg] = '\0';
1143 if (*winptr == '\0')
1144 wl = s->curw;
1145 else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
1146 goto error;
1148 /* Find the pane section and look it up. */
1149 paneptr = winptr + (period - arg) + 1;
1150 if (*paneptr == '\0')
1151 *wpp = wl->window->active;
1152 else if (paneptr[0] == '+' || paneptr[0] == '-')
1153 *wpp = cmd_find_pane_offset(paneptr, wl);
1154 else {
1155 idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1156 if (errstr != NULL)
1157 goto lookup_string;
1158 *wpp = window_pane_at_index(wl->window, idx);
1159 if (*wpp == NULL)
1160 goto lookup_string;
1163 free(winptr);
1164 return (wl);
1166 lookup_string:
1167 /* Try pane string description. */
1168 if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1169 cmdq_error(cmdq, "can't find pane: %s", paneptr);
1170 goto error;
1173 free(winptr);
1174 return (wl);
1176 no_period:
1177 /* Try as a pane number alone. */
1178 idx = strtonum(arg, 0, INT_MAX, &errstr);
1179 if (errstr != NULL)
1180 goto lookup_window;
1182 /* Try index in the current session and window. */
1183 if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1184 goto lookup_window;
1186 return (s->curw);
1188 lookup_window:
1189 /* Try pane string description. */
1190 if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1191 return (s->curw);
1193 /* Try as a window and use the active pane. */
1194 if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
1195 *wpp = wl->window->active;
1196 return (wl);
1198 error:
1199 free(winptr);
1200 return (NULL);
1203 struct window_pane *
1204 cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1206 struct window *w = wl->window;
1207 struct window_pane *wp = w->active;
1208 u_int offset = 1;
1210 if (paneptr[1] != '\0')
1211 offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1212 if (offset > 0) {
1213 if (paneptr[0] == '+')
1214 wp = window_pane_next_by_number(w, wp, offset);
1215 else
1216 wp = window_pane_previous_by_number(w, wp, offset);
1219 return (wp);
1222 /* Replace the first %% or %idx in template by s. */
1223 char *
1224 cmd_template_replace(const char *template, const char *s, int idx)
1226 char ch, *buf;
1227 const char *ptr;
1228 int replaced;
1229 size_t len;
1231 if (strchr(template, '%') == NULL)
1232 return (xstrdup(template));
1234 buf = xmalloc(1);
1235 *buf = '\0';
1236 len = 0;
1237 replaced = 0;
1239 ptr = template;
1240 while (*ptr != '\0') {
1241 switch (ch = *ptr++) {
1242 case '%':
1243 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1244 if (*ptr != '%' || replaced)
1245 break;
1246 replaced = 1;
1248 ptr++;
1250 len += strlen(s);
1251 buf = xrealloc(buf, 1, len + 1);
1252 strlcat(buf, s, len + 1);
1253 continue;
1255 buf = xrealloc(buf, 1, len + 2);
1256 buf[len++] = ch;
1257 buf[len] = '\0';
1260 return (buf);
1264 * Return the default path for a new pane, using the given path or the
1265 * default-path option if it is NULL. Several special values are accepted: the
1266 * empty string or relative path for the current pane's working directory, ~
1267 * for the user's home, - for the session working directory, . for the tmux
1268 * server's working directory. The default on failure is the session's working
1269 * directory.
1271 const char *
1272 cmd_get_default_path(struct cmd_q *cmdq, const char *cwd)
1274 struct client *c = cmdq->client;
1275 struct session *s;
1276 struct environ_entry *envent;
1277 const char *root;
1278 char tmp[MAXPATHLEN];
1279 struct passwd *pw;
1280 int n;
1281 size_t skip;
1282 static char path[MAXPATHLEN];
1284 if ((s = cmd_current_session(cmdq, 0)) == NULL)
1285 return (NULL);
1287 if (cwd == NULL)
1288 cwd = options_get_string(&s->options, "default-path");
1290 skip = 1;
1291 if (strcmp(cwd, "$HOME") == 0 || strncmp(cwd, "$HOME/", 6) == 0) {
1292 /* User's home directory - $HOME. */
1293 skip = 5;
1294 goto find_home;
1295 } else if (cwd[0] == '~' && (cwd[1] == '\0' || cwd[1] == '/')) {
1296 /* User's home directory - ~. */
1297 goto find_home;
1298 } else if (cwd[0] == '-' && (cwd[1] == '\0' || cwd[1] == '/')) {
1299 /* Session working directory. */
1300 root = s->cwd;
1301 goto complete_path;
1302 } else if (cwd[0] == '.' && (cwd[1] == '\0' || cwd[1] == '/')) {
1303 /* Server working directory. */
1304 if (getcwd(tmp, sizeof tmp) != NULL) {
1305 root = tmp;
1306 goto complete_path;
1308 return (s->cwd);
1309 } else if (*cwd == '/') {
1310 /* Absolute path. */
1311 return (cwd);
1312 } else {
1313 /* Empty or relative path. */
1314 if (c != NULL && c->session == NULL && c->cwd != NULL)
1315 root = c->cwd;
1316 else if (s->curw != NULL)
1317 root = get_proc_cwd(s->curw->window->active->fd);
1318 else
1319 return (s->cwd);
1320 skip = 0;
1321 if (root != NULL)
1322 goto complete_path;
1325 return (s->cwd);
1327 find_home:
1328 envent = environ_find(&global_environ, "HOME");
1329 if (envent != NULL && *envent->value != '\0')
1330 root = envent->value;
1331 else if ((pw = getpwuid(getuid())) != NULL)
1332 root = pw->pw_dir;
1333 else
1334 return (s->cwd);
1336 complete_path:
1337 if (root[skip] == '\0') {
1338 strlcpy(path, root, sizeof path);
1339 return (path);
1341 n = snprintf(path, sizeof path, "%s/%s", root, cwd + skip);
1342 if (n > 0 && (size_t)n < sizeof path)
1343 return (path);
1344 return (s->cwd);