Add window-status-separator option, from Thomas Adam.
[tmux-openbsd.git] / cmd.c
blob5d8e72092b532e465cfdb5fdc1a27824a57ac5b1
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_session_entry,
39 &cmd_choose_window_entry,
40 &cmd_clear_history_entry,
41 &cmd_clock_mode_entry,
42 &cmd_command_prompt_entry,
43 &cmd_confirm_before_entry,
44 &cmd_copy_mode_entry,
45 &cmd_delete_buffer_entry,
46 &cmd_detach_client_entry,
47 &cmd_display_message_entry,
48 &cmd_display_panes_entry,
49 &cmd_find_window_entry,
50 &cmd_has_session_entry,
51 &cmd_if_shell_entry,
52 &cmd_join_pane_entry,
53 &cmd_kill_pane_entry,
54 &cmd_kill_server_entry,
55 &cmd_kill_session_entry,
56 &cmd_kill_window_entry,
57 &cmd_last_pane_entry,
58 &cmd_last_window_entry,
59 &cmd_link_window_entry,
60 &cmd_list_buffers_entry,
61 &cmd_list_clients_entry,
62 &cmd_list_commands_entry,
63 &cmd_list_keys_entry,
64 &cmd_list_panes_entry,
65 &cmd_list_sessions_entry,
66 &cmd_list_windows_entry,
67 &cmd_load_buffer_entry,
68 &cmd_lock_client_entry,
69 &cmd_lock_server_entry,
70 &cmd_lock_session_entry,
71 &cmd_move_pane_entry,
72 &cmd_move_window_entry,
73 &cmd_new_session_entry,
74 &cmd_new_window_entry,
75 &cmd_next_layout_entry,
76 &cmd_next_window_entry,
77 &cmd_paste_buffer_entry,
78 &cmd_pipe_pane_entry,
79 &cmd_previous_layout_entry,
80 &cmd_previous_window_entry,
81 &cmd_refresh_client_entry,
82 &cmd_rename_session_entry,
83 &cmd_rename_window_entry,
84 &cmd_resize_pane_entry,
85 &cmd_respawn_pane_entry,
86 &cmd_respawn_window_entry,
87 &cmd_rotate_window_entry,
88 &cmd_run_shell_entry,
89 &cmd_save_buffer_entry,
90 &cmd_select_layout_entry,
91 &cmd_select_pane_entry,
92 &cmd_select_window_entry,
93 &cmd_send_keys_entry,
94 &cmd_send_prefix_entry,
95 &cmd_server_info_entry,
96 &cmd_set_buffer_entry,
97 &cmd_set_environment_entry,
98 &cmd_set_option_entry,
99 &cmd_set_window_option_entry,
100 &cmd_show_buffer_entry,
101 &cmd_show_environment_entry,
102 &cmd_show_messages_entry,
103 &cmd_show_options_entry,
104 &cmd_show_window_options_entry,
105 &cmd_source_file_entry,
106 &cmd_split_window_entry,
107 &cmd_start_server_entry,
108 &cmd_suspend_client_entry,
109 &cmd_swap_pane_entry,
110 &cmd_swap_window_entry,
111 &cmd_switch_client_entry,
112 &cmd_unbind_key_entry,
113 &cmd_unlink_window_entry,
114 NULL
117 struct session *cmd_choose_session_list(struct sessionslist *);
118 struct session *cmd_choose_session(int);
119 struct client *cmd_choose_client(struct clients *);
120 struct client *cmd_lookup_client(const char *);
121 struct session *cmd_lookup_session(const char *, int *);
122 struct winlink *cmd_lookup_window(struct session *, const char *, int *);
123 int cmd_lookup_index(struct session *, const char *, int *);
124 struct window_pane *cmd_lookup_paneid(const char *);
125 struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
126 struct window *cmd_lookup_windowid(const char *);
127 struct session *cmd_window_session(struct cmd_ctx *,
128 struct window *, struct winlink **);
129 struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
130 int cmd_find_index_offset(const char *, struct session *, int *);
131 struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
134 cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
136 size_t arglen;
137 int i;
139 *buf = '\0';
140 for (i = 0; i < argc; i++) {
141 if (strlcpy(buf, argv[i], len) >= len)
142 return (-1);
143 arglen = strlen(argv[i]) + 1;
144 buf += arglen;
145 len -= arglen;
148 return (0);
152 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
154 int i;
155 size_t arglen;
157 if (argc == 0)
158 return (0);
159 *argv = xcalloc(argc, sizeof **argv);
161 buf[len - 1] = '\0';
162 for (i = 0; i < argc; i++) {
163 if (len == 0) {
164 cmd_free_argv(argc, *argv);
165 return (-1);
168 arglen = strlen(buf) + 1;
169 (*argv)[i] = xstrdup(buf);
170 buf += arglen;
171 len -= arglen;
174 return (0);
177 char **
178 cmd_copy_argv(int argc, char *const *argv)
180 char **new_argv;
181 int i;
183 if (argc == 0)
184 return (NULL);
185 new_argv = xcalloc(argc, sizeof *new_argv);
186 for (i = 0; i < argc; i++) {
187 if (argv[i] != NULL)
188 new_argv[i] = xstrdup(argv[i]);
190 return (new_argv);
193 void
194 cmd_free_argv(int argc, char **argv)
196 int i;
198 if (argc == 0)
199 return;
200 for (i = 0; i < argc; i++) {
201 if (argv[i] != NULL)
202 xfree(argv[i]);
204 xfree(argv);
207 struct cmd *
208 cmd_parse(int argc, char **argv, char **cause)
210 const struct cmd_entry **entryp, *entry;
211 struct cmd *cmd;
212 struct args *args;
213 char s[BUFSIZ];
214 int ambiguous = 0;
216 *cause = NULL;
217 if (argc == 0) {
218 xasprintf(cause, "no command");
219 return (NULL);
222 entry = NULL;
223 for (entryp = cmd_table; *entryp != NULL; entryp++) {
224 if ((*entryp)->alias != NULL &&
225 strcmp((*entryp)->alias, argv[0]) == 0) {
226 ambiguous = 0;
227 entry = *entryp;
228 break;
231 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
232 continue;
233 if (entry != NULL)
234 ambiguous = 1;
235 entry = *entryp;
237 /* Bail now if an exact match. */
238 if (strcmp(entry->name, argv[0]) == 0)
239 break;
241 if (ambiguous)
242 goto ambiguous;
243 if (entry == NULL) {
244 xasprintf(cause, "unknown command: %s", argv[0]);
245 return (NULL);
248 args = args_parse(entry->args_template, argc, argv);
249 if (args == NULL)
250 goto usage;
251 if (entry->args_lower != -1 && args->argc < entry->args_lower)
252 goto usage;
253 if (entry->args_upper != -1 && args->argc > entry->args_upper)
254 goto usage;
255 if (entry->check != NULL && entry->check(args) != 0)
256 goto usage;
258 cmd = xmalloc(sizeof *cmd);
259 cmd->entry = entry;
260 cmd->args = args;
261 return (cmd);
263 ambiguous:
264 *s = '\0';
265 for (entryp = cmd_table; *entryp != NULL; entryp++) {
266 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
267 continue;
268 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
269 break;
270 if (strlcat(s, ", ", sizeof s) >= sizeof s)
271 break;
273 s[strlen(s) - 2] = '\0';
274 xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
275 return (NULL);
277 usage:
278 if (args != NULL)
279 args_free(args);
280 xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
281 return (NULL);
285 cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
287 return (cmd->entry->exec(cmd, ctx));
290 void
291 cmd_free(struct cmd *cmd)
293 if (cmd->args != NULL)
294 args_free(cmd->args);
295 xfree(cmd);
298 size_t
299 cmd_print(struct cmd *cmd, char *buf, size_t len)
301 size_t off, used;
303 off = xsnprintf(buf, len, "%s ", cmd->entry->name);
304 if (off < len) {
305 used = args_print(cmd->args, buf + off, len - off);
306 if (used == 0)
307 off--;
308 else
309 off += used;
310 buf[off] = '\0';
312 return (off);
316 * Figure out the current session. Use: 1) the current session, if the command
317 * context has one; 2) the most recently used session containing the pty of the
318 * calling client, if any; 3) the session specified in the TMUX variable from
319 * the environment (as passed from the client); 4) the most recently used
320 * session from all sessions.
322 struct session *
323 cmd_current_session(struct cmd_ctx *ctx, int prefer_unattached)
325 struct msg_command_data *data = ctx->msgdata;
326 struct client *c = ctx->cmdclient;
327 struct session *s;
328 struct sessionslist ss;
329 struct winlink *wl;
330 struct window_pane *wp;
331 int found;
333 if (ctx->curclient != NULL && ctx->curclient->session != NULL)
334 return (ctx->curclient->session);
337 * If the name of the calling client's pty is know, build a list of the
338 * sessions that contain it and if any choose either the first or the
339 * newest.
341 if (c != NULL && c->tty.path != NULL) {
342 ARRAY_INIT(&ss);
343 RB_FOREACH(s, sessions, &sessions) {
344 found = 0;
345 RB_FOREACH(wl, winlinks, &s->windows) {
346 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
347 if (strcmp(wp->tty, c->tty.path) == 0) {
348 found = 1;
349 break;
352 if (found)
353 break;
355 if (found)
356 ARRAY_ADD(&ss, s);
359 s = cmd_choose_session_list(&ss);
360 ARRAY_FREE(&ss);
361 if (s != NULL)
362 return (s);
365 /* Use the session from the TMUX environment variable. */
366 if (data != NULL && data->pid == getpid() && data->idx != -1) {
367 s = session_find_by_index(data->idx);
368 if (s != NULL)
369 return (s);
372 return (cmd_choose_session(prefer_unattached));
376 * Find the most recently used session, preferring unattached if the flag is
377 * set.
379 struct session *
380 cmd_choose_session(int prefer_unattached)
382 struct session *s, *sbest;
383 struct timeval *tv = NULL;
385 sbest = NULL;
386 RB_FOREACH(s, sessions, &sessions) {
387 if (tv == NULL || timercmp(&s->activity_time, tv, >) ||
388 (prefer_unattached &&
389 !(sbest->flags & SESSION_UNATTACHED) &&
390 (s->flags & SESSION_UNATTACHED))) {
391 sbest = s;
392 tv = &s->activity_time;
396 return (sbest);
399 /* Find the most recently used session from a list. */
400 struct session *
401 cmd_choose_session_list(struct sessionslist *ss)
403 struct session *s, *sbest;
404 struct timeval *tv = NULL;
405 u_int i;
407 sbest = NULL;
408 for (i = 0; i < ARRAY_LENGTH(ss); i++) {
409 if ((s = ARRAY_ITEM(ss, i)) == NULL)
410 continue;
412 if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
413 sbest = s;
414 tv = &s->activity_time;
418 return (sbest);
422 * Find the current client. First try the current client if set, then pick the
423 * most recently used of the clients attached to the current session if any,
424 * then of all clients.
426 struct client *
427 cmd_current_client(struct cmd_ctx *ctx)
429 struct session *s;
430 struct client *c;
431 struct clients cc;
432 u_int i;
434 if (ctx->curclient != NULL)
435 return (ctx->curclient);
438 * No current client set. Find the current session and return the
439 * newest of its clients.
441 s = cmd_current_session(ctx, 0);
442 if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
443 ARRAY_INIT(&cc);
444 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
445 if ((c = ARRAY_ITEM(&clients, i)) == NULL)
446 continue;
447 if (s == c->session)
448 ARRAY_ADD(&cc, c);
451 c = cmd_choose_client(&cc);
452 ARRAY_FREE(&cc);
453 if (c != NULL)
454 return (c);
457 return (cmd_choose_client(&clients));
460 /* Choose the most recently used client from a list. */
461 struct client *
462 cmd_choose_client(struct clients *cc)
464 struct client *c, *cbest;
465 struct timeval *tv = NULL;
466 u_int i;
468 cbest = NULL;
469 for (i = 0; i < ARRAY_LENGTH(cc); i++) {
470 if ((c = ARRAY_ITEM(cc, i)) == NULL)
471 continue;
472 if (c->session == NULL)
473 continue;
475 if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
476 cbest = c;
477 tv = &c->activity_time;
481 return (cbest);
484 /* Find the target client or report an error and return NULL. */
485 struct client *
486 cmd_find_client(struct cmd_ctx *ctx, const char *arg)
488 struct client *c;
489 char *tmparg;
490 size_t arglen;
492 /* A NULL argument means the current client. */
493 if (arg == NULL)
494 return (cmd_current_client(ctx));
495 tmparg = xstrdup(arg);
497 /* Trim a single trailing colon if any. */
498 arglen = strlen(tmparg);
499 if (arglen != 0 && tmparg[arglen - 1] == ':')
500 tmparg[arglen - 1] = '\0';
502 /* Find the client, if any. */
503 c = cmd_lookup_client(tmparg);
505 /* If no client found, report an error. */
506 if (c == NULL)
507 ctx->error(ctx, "client not found: %s", tmparg);
509 xfree(tmparg);
510 return (c);
514 * Lookup a client by device path. Either of a full match and a match without a
515 * leading _PATH_DEV ("/dev/") is accepted.
517 struct client *
518 cmd_lookup_client(const char *name)
520 struct client *c;
521 const char *path;
522 u_int i;
524 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
525 c = ARRAY_ITEM(&clients, i);
526 if (c == NULL || c->session == NULL)
527 continue;
528 path = c->tty.path;
530 /* Check for exact matches. */
531 if (strcmp(name, path) == 0)
532 return (c);
534 /* Check without leading /dev if present. */
535 if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
536 continue;
537 if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
538 return (c);
541 return (NULL);
544 /* Lookup a session by name. If no session is found, NULL is returned. */
545 struct session *
546 cmd_lookup_session(const char *name, int *ambiguous)
548 struct session *s, *sfound;
550 *ambiguous = 0;
553 * Look for matches. First look for exact matches - session names must
554 * be unique so an exact match can't be ambigious and can just be
555 * returned.
557 if ((s = session_find(name)) != NULL)
558 return (s);
561 * Otherwise look for partial matches, returning early if it is found to
562 * be ambiguous.
564 sfound = NULL;
565 RB_FOREACH(s, sessions, &sessions) {
566 if (strncmp(name, s->name, strlen(name)) == 0 ||
567 fnmatch(name, s->name, 0) == 0) {
568 if (sfound != NULL) {
569 *ambiguous = 1;
570 return (NULL);
572 sfound = s;
575 return (sfound);
579 * Lookup a window or return -1 if not found or ambigious. First try as an
580 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
581 * idx if the window index is a valid number but there is no window with that
582 * index.
584 struct winlink *
585 cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
587 struct winlink *wl, *wlfound;
588 const char *errstr;
589 u_int idx;
591 *ambiguous = 0;
593 /* Try as a window id. */
594 if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
595 return (wl);
597 /* First see if this is a valid window index in this session. */
598 idx = strtonum(name, 0, INT_MAX, &errstr);
599 if (errstr == NULL) {
600 if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
601 return (wl);
604 /* Look for exact matches, error if more than one. */
605 wlfound = NULL;
606 RB_FOREACH(wl, winlinks, &s->windows) {
607 if (strcmp(name, wl->window->name) == 0) {
608 if (wlfound != NULL) {
609 *ambiguous = 1;
610 return (NULL);
612 wlfound = wl;
615 if (wlfound != NULL)
616 return (wlfound);
618 /* Now look for pattern matches, again error if multiple. */
619 wlfound = NULL;
620 RB_FOREACH(wl, winlinks, &s->windows) {
621 if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
622 fnmatch(name, wl->window->name, 0) == 0) {
623 if (wlfound != NULL) {
624 *ambiguous = 1;
625 return (NULL);
627 wlfound = wl;
630 if (wlfound != NULL)
631 return (wlfound);
633 return (NULL);
637 * Find a window index - if the window doesn't exist, check if it is a
638 * potential index and return it anyway.
641 cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
643 struct winlink *wl;
644 const char *errstr;
645 u_int idx;
647 if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
648 return (wl->idx);
649 if (*ambiguous)
650 return (-1);
652 idx = strtonum(name, 0, INT_MAX, &errstr);
653 if (errstr == NULL)
654 return (idx);
656 return (-1);
659 /* Lookup pane id. An initial % means a pane id. */
660 struct window_pane *
661 cmd_lookup_paneid(const char *arg)
663 const char *errstr;
664 u_int paneid;
666 if (*arg != '%')
667 return (NULL);
669 paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
670 if (errstr != NULL)
671 return (NULL);
672 return (window_pane_find_by_id(paneid));
675 /* Lookup window id in a session. An initial @ means a window id. */
676 struct winlink *
677 cmd_lookup_winlink_windowid(struct session *s, const char *arg)
679 const char *errstr;
680 u_int windowid;
682 if (*arg != '@')
683 return (NULL);
685 windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
686 if (errstr != NULL)
687 return (NULL);
688 return (winlink_find_by_window_id(&s->windows, windowid));
691 /* Lookup window id. An initial @ means a window id. */
692 struct window *
693 cmd_lookup_windowid(const char *arg)
695 const char *errstr;
696 u_int windowid;
698 if (*arg != '@')
699 return (NULL);
701 windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
702 if (errstr != NULL)
703 return (NULL);
704 return (window_find_by_id(windowid));
707 /* Find session and winlink for window. */
708 struct session *
709 cmd_window_session(struct cmd_ctx *ctx, struct window *w, struct winlink **wlp)
711 struct session *s;
712 struct sessionslist ss;
713 struct winlink *wl;
715 /* If this window is in the current session, return that winlink. */
716 s = cmd_current_session(ctx, 0);
717 if (s != NULL) {
718 wl = winlink_find_by_window(&s->windows, w);
719 if (wl != NULL) {
720 if (wlp != NULL)
721 *wlp = wl;
722 return (s);
726 /* Otherwise choose from all sessions with this window. */
727 ARRAY_INIT(&ss);
728 RB_FOREACH(s, sessions, &sessions) {
729 if (winlink_find_by_window(&s->windows, w) != NULL)
730 ARRAY_ADD(&ss, s);
732 s = cmd_choose_session_list(&ss);
733 ARRAY_FREE(&ss);
734 if (wlp != NULL)
735 *wlp = winlink_find_by_window(&s->windows, w);
736 return (s);
739 /* Find the target session or report an error and return NULL. */
740 struct session *
741 cmd_find_session(struct cmd_ctx *ctx, const char *arg, int prefer_unattached)
743 struct session *s;
744 struct window_pane *wp;
745 struct window *w;
746 struct client *c;
747 char *tmparg;
748 size_t arglen;
749 int ambiguous;
751 /* A NULL argument means the current session. */
752 if (arg == NULL)
753 return (cmd_current_session(ctx, prefer_unattached));
755 /* Lookup as pane id or window id. */
756 if ((wp = cmd_lookup_paneid(arg)) != NULL)
757 return (cmd_window_session(ctx, wp->window, NULL));
758 if ((w = cmd_lookup_windowid(arg)) != NULL)
759 return (cmd_window_session(ctx, w, NULL));
761 /* Trim a single trailing colon if any. */
762 tmparg = xstrdup(arg);
763 arglen = strlen(tmparg);
764 if (arglen != 0 && tmparg[arglen - 1] == ':')
765 tmparg[arglen - 1] = '\0';
767 /* An empty session name is the current session. */
768 if (*tmparg == '\0') {
769 xfree(tmparg);
770 return (cmd_current_session(ctx, prefer_unattached));
773 /* Find the session, if any. */
774 s = cmd_lookup_session(tmparg, &ambiguous);
776 /* If it doesn't, try to match it as a client. */
777 if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
778 s = c->session;
780 /* If no session found, report an error. */
781 if (s == NULL) {
782 if (ambiguous)
783 ctx->error(ctx, "more than one session: %s", tmparg);
784 else
785 ctx->error(ctx, "session not found: %s", tmparg);
788 xfree(tmparg);
789 return (s);
792 /* Find the target session and window or report an error and return NULL. */
793 struct winlink *
794 cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
796 struct session *s;
797 struct winlink *wl;
798 struct window_pane *wp;
799 const char *winptr;
800 char *sessptr = NULL;
801 int ambiguous = 0;
804 * Find the current session. There must always be a current session, if
805 * it can't be found, report an error.
807 if ((s = cmd_current_session(ctx, 0)) == NULL) {
808 ctx->error(ctx, "can't establish current session");
809 return (NULL);
812 /* A NULL argument means the current session and window. */
813 if (arg == NULL) {
814 if (sp != NULL)
815 *sp = s;
816 return (s->curw);
819 /* Lookup as pane id. */
820 if ((wp = cmd_lookup_paneid(arg)) != NULL) {
821 s = cmd_window_session(ctx, wp->window, &wl);
822 if (sp != NULL)
823 *sp = s;
824 return (wl);
827 /* Time to look at the argument. If it is empty, that is an error. */
828 if (*arg == '\0')
829 goto not_found;
831 /* Find the separating colon and split into window and session. */
832 winptr = strchr(arg, ':');
833 if (winptr == NULL)
834 goto no_colon;
835 winptr++; /* skip : */
836 sessptr = xstrdup(arg);
837 *strchr(sessptr, ':') = '\0';
839 /* Try to lookup the session if present. */
840 if (*sessptr != '\0') {
841 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
842 goto no_session;
844 if (sp != NULL)
845 *sp = s;
848 * Then work out the window. An empty string is the current window,
849 * otherwise try special cases then to look it up in the session.
851 if (*winptr == '\0')
852 wl = s->curw;
853 else if (winptr[0] == '!' && winptr[1] == '\0')
854 wl = TAILQ_FIRST(&s->lastw);
855 else if (winptr[0] == '+' || winptr[0] == '-')
856 wl = cmd_find_window_offset(winptr, s, &ambiguous);
857 else
858 wl = cmd_lookup_window(s, winptr, &ambiguous);
859 if (wl == NULL)
860 goto not_found;
862 if (sessptr != NULL)
863 xfree(sessptr);
864 return (wl);
866 no_colon:
868 * No colon in the string, first try special cases, then as a window
869 * and lastly as a session.
871 if (arg[0] == '!' && arg[1] == '\0') {
872 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
873 goto not_found;
874 } else if (arg[0] == '+' || arg[0] == '-') {
875 if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
876 goto lookup_session;
877 } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
878 goto lookup_session;
880 if (sp != NULL)
881 *sp = s;
883 return (wl);
885 lookup_session:
886 if (ambiguous)
887 goto not_found;
888 if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
889 goto no_session;
891 if (sp != NULL)
892 *sp = s;
894 return (s->curw);
896 no_session:
897 if (ambiguous)
898 ctx->error(ctx, "multiple sessions: %s", arg);
899 else
900 ctx->error(ctx, "session not found: %s", arg);
901 if (sessptr != NULL)
902 xfree(sessptr);
903 return (NULL);
905 not_found:
906 if (ambiguous)
907 ctx->error(ctx, "multiple windows: %s", arg);
908 else
909 ctx->error(ctx, "window not found: %s", arg);
910 if (sessptr != NULL)
911 xfree(sessptr);
912 return (NULL);
915 struct winlink *
916 cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
918 struct winlink *wl;
919 int offset = 1;
921 if (winptr[1] != '\0')
922 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
923 if (offset == 0)
924 wl = cmd_lookup_window(s, winptr, ambiguous);
925 else {
926 if (winptr[0] == '+')
927 wl = winlink_next_by_number(s->curw, s, offset);
928 else
929 wl = winlink_previous_by_number(s->curw, s, offset);
932 return (wl);
936 * Find the target session and window index, whether or not it exists in the
937 * session. Return -2 on error or -1 if no window index is specified. This is
938 * used when parsing an argument for a window target that may not exist (for
939 * example if it is going to be created).
942 cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
944 struct session *s;
945 struct winlink *wl;
946 const char *winptr;
947 char *sessptr = NULL;
948 int idx, ambiguous = 0;
951 * Find the current session. There must always be a current session, if
952 * it can't be found, report an error.
954 if ((s = cmd_current_session(ctx, 0)) == NULL) {
955 ctx->error(ctx, "can't establish current session");
956 return (-2);
959 /* A NULL argument means the current session and "no window" (-1). */
960 if (arg == NULL) {
961 if (sp != NULL)
962 *sp = s;
963 return (-1);
966 /* Time to look at the argument. If it is empty, that is an error. */
967 if (*arg == '\0')
968 goto not_found;
970 /* Find the separating colon. If none, assume the current session. */
971 winptr = strchr(arg, ':');
972 if (winptr == NULL)
973 goto no_colon;
974 winptr++; /* skip : */
975 sessptr = xstrdup(arg);
976 *strchr(sessptr, ':') = '\0';
978 /* Try to lookup the session if present. */
979 if (sessptr != NULL && *sessptr != '\0') {
980 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
981 goto no_session;
983 if (sp != NULL)
984 *sp = s;
987 * Then work out the window. An empty string is a new window otherwise
988 * try to look it up in the session.
990 if (*winptr == '\0')
991 idx = -1;
992 else if (winptr[0] == '!' && winptr[1] == '\0') {
993 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
994 goto not_found;
995 idx = wl->idx;
996 } else if (winptr[0] == '+' || winptr[0] == '-') {
997 if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
998 goto invalid_index;
999 } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1000 goto invalid_index;
1002 if (sessptr != NULL)
1003 xfree(sessptr);
1004 return (idx);
1006 no_colon:
1008 * No colon in the string, first try special cases, then as a window
1009 * and lastly as a session.
1011 if (arg[0] == '!' && arg[1] == '\0') {
1012 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1013 goto not_found;
1014 idx = wl->idx;
1015 } else if (arg[0] == '+' || arg[0] == '-') {
1016 if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1017 goto lookup_session;
1018 } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1019 goto lookup_session;
1021 if (sp != NULL)
1022 *sp = s;
1024 return (idx);
1026 lookup_session:
1027 if (ambiguous)
1028 goto not_found;
1029 if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1030 goto no_session;
1032 if (sp != NULL)
1033 *sp = s;
1035 return (-1);
1037 no_session:
1038 if (ambiguous)
1039 ctx->error(ctx, "multiple sessions: %s", arg);
1040 else
1041 ctx->error(ctx, "session not found: %s", arg);
1042 if (sessptr != NULL)
1043 xfree(sessptr);
1044 return (-2);
1046 invalid_index:
1047 if (ambiguous)
1048 goto not_found;
1049 ctx->error(ctx, "invalid index: %s", arg);
1051 if (sessptr != NULL)
1052 xfree(sessptr);
1053 return (-2);
1055 not_found:
1056 if (ambiguous)
1057 ctx->error(ctx, "multiple windows: %s", arg);
1058 else
1059 ctx->error(ctx, "window not found: %s", arg);
1060 if (sessptr != NULL)
1061 xfree(sessptr);
1062 return (-2);
1066 cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1068 int idx, offset = 1;
1070 if (winptr[1] != '\0')
1071 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1072 if (offset == 0)
1073 idx = cmd_lookup_index(s, winptr, ambiguous);
1074 else {
1075 if (winptr[0] == '+') {
1076 if (s->curw->idx == INT_MAX)
1077 idx = cmd_lookup_index(s, winptr, ambiguous);
1078 else
1079 idx = s->curw->idx + offset;
1080 } else {
1081 if (s->curw->idx == 0)
1082 idx = cmd_lookup_index(s, winptr, ambiguous);
1083 else
1084 idx = s->curw->idx - offset;
1088 return (idx);
1092 * Find the target session, window and pane number or report an error and
1093 * return NULL. The pane number is separated from the session:window by a .,
1094 * such as mysession:mywindow.0.
1096 struct winlink *
1097 cmd_find_pane(struct cmd_ctx *ctx,
1098 const char *arg, struct session **sp, struct window_pane **wpp)
1100 struct session *s;
1101 struct winlink *wl;
1102 const char *period, *errstr;
1103 char *winptr, *paneptr;
1104 u_int idx;
1106 /* Get the current session. */
1107 if ((s = cmd_current_session(ctx, 0)) == NULL) {
1108 ctx->error(ctx, "can't establish current session");
1109 return (NULL);
1111 if (sp != NULL)
1112 *sp = s;
1114 /* A NULL argument means the current session, window and pane. */
1115 if (arg == NULL) {
1116 *wpp = s->curw->window->active;
1117 return (s->curw);
1120 /* Lookup as pane id. */
1121 if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1122 s = cmd_window_session(ctx, (*wpp)->window, &wl);
1123 if (sp != NULL)
1124 *sp = s;
1125 return (wl);
1128 /* Look for a separating period. */
1129 if ((period = strrchr(arg, '.')) == NULL)
1130 goto no_period;
1132 /* Pull out the window part and parse it. */
1133 winptr = xstrdup(arg);
1134 winptr[period - arg] = '\0';
1135 if (*winptr == '\0')
1136 wl = s->curw;
1137 else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
1138 goto error;
1140 /* Find the pane section and look it up. */
1141 paneptr = winptr + (period - arg) + 1;
1142 if (*paneptr == '\0')
1143 *wpp = wl->window->active;
1144 else if (paneptr[0] == '+' || paneptr[0] == '-')
1145 *wpp = cmd_find_pane_offset(paneptr, wl);
1146 else {
1147 idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1148 if (errstr != NULL)
1149 goto lookup_string;
1150 *wpp = window_pane_at_index(wl->window, idx);
1151 if (*wpp == NULL)
1152 goto lookup_string;
1155 xfree(winptr);
1156 return (wl);
1158 lookup_string:
1159 /* Try pane string description. */
1160 if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1161 ctx->error(ctx, "can't find pane: %s", paneptr);
1162 goto error;
1165 xfree(winptr);
1166 return (wl);
1168 no_period:
1169 /* Try as a pane number alone. */
1170 idx = strtonum(arg, 0, INT_MAX, &errstr);
1171 if (errstr != NULL)
1172 goto lookup_window;
1174 /* Try index in the current session and window. */
1175 if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1176 goto lookup_window;
1178 return (s->curw);
1180 lookup_window:
1181 /* Try pane string description. */
1182 if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1183 return (s->curw);
1185 /* Try as a window and use the active pane. */
1186 if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
1187 *wpp = wl->window->active;
1188 return (wl);
1190 error:
1191 xfree(winptr);
1192 return (NULL);
1195 struct window_pane *
1196 cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1198 struct window *w = wl->window;
1199 struct window_pane *wp = w->active;
1200 u_int offset = 1;
1202 if (paneptr[1] != '\0')
1203 offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1204 if (offset > 0) {
1205 if (paneptr[0] == '+')
1206 wp = window_pane_next_by_number(w, wp, offset);
1207 else
1208 wp = window_pane_previous_by_number(w, wp, offset);
1211 return (wp);
1214 /* Replace the first %% or %idx in template by s. */
1215 char *
1216 cmd_template_replace(char *template, const char *s, int idx)
1218 char ch;
1219 char *buf, *ptr;
1220 int replaced;
1221 size_t len;
1223 if (strstr(template, "%") == NULL)
1224 return (xstrdup(template));
1226 buf = xmalloc(1);
1227 *buf = '\0';
1228 len = 0;
1229 replaced = 0;
1231 ptr = template;
1232 while (*ptr != '\0') {
1233 switch (ch = *ptr++) {
1234 case '%':
1235 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1236 if (*ptr != '%' || replaced)
1237 break;
1238 replaced = 1;
1240 ptr++;
1242 len += strlen(s);
1243 buf = xrealloc(buf, 1, len + 1);
1244 strlcat(buf, s, len + 1);
1245 continue;
1247 buf = xrealloc(buf, 1, len + 2);
1248 buf[len++] = ch;
1249 buf[len] = '\0';
1252 return (buf);
1256 * Return the default path for a new pane, using the given path or the
1257 * default-path option if it is NULL. Several special values are accepted: the
1258 * empty string or relative path for the current pane's working directory, ~
1259 * for the user's home, - for the session working directory, . for the tmux
1260 * server's working directory. The default on failure is the session's working
1261 * directory.
1263 const char *
1264 cmd_get_default_path(struct cmd_ctx *ctx, const char *cwd)
1266 struct session *s;
1267 struct environ_entry *envent;
1268 const char *root;
1269 char tmp[MAXPATHLEN];
1270 struct passwd *pw;
1271 int n;
1272 size_t skip;
1273 static char path[MAXPATHLEN];
1275 if ((s = cmd_current_session(ctx, 0)) == NULL)
1276 return (NULL);
1278 if (cwd == NULL)
1279 cwd = options_get_string(&s->options, "default-path");
1281 skip = 1;
1282 if (strcmp(cwd, "$HOME") == 0 || strncmp(cwd, "$HOME/", 6) == 0) {
1283 /* User's home directory - $HOME. */
1284 skip = 5;
1285 goto find_home;
1286 } else if (cwd[0] == '~' && (cwd[1] == '\0' || cwd[1] == '/')) {
1287 /* User's home directory - ~. */
1288 goto find_home;
1289 } else if (cwd[0] == '-' && (cwd[1] == '\0' || cwd[1] == '/')) {
1290 /* Session working directory. */
1291 root = s->cwd;
1292 goto complete_path;
1293 } else if (cwd[0] == '.' && (cwd[1] == '\0' || cwd[1] == '/')){
1294 /* Server working directory. */
1295 if (getcwd(tmp, sizeof tmp) != NULL) {
1296 root = tmp;
1297 goto complete_path;
1299 return (s->cwd);
1300 } else if (*cwd == '/') {
1301 /* Absolute path. */
1302 return (cwd);
1303 } else {
1304 /* Empty or relative path. */
1305 if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
1306 root = ctx->cmdclient->cwd;
1307 else if (ctx->curclient != NULL)
1308 root = get_proc_cwd(s->curw->window->active->pid);
1309 else
1310 return (s->cwd);
1311 skip = 0;
1312 if (root != NULL)
1313 goto complete_path;
1316 return (s->cwd);
1318 find_home:
1319 envent = environ_find(&global_environ, "HOME");
1320 if (envent != NULL && *envent->value != '\0')
1321 root = envent->value;
1322 else if ((pw = getpwuid(getuid())) != NULL)
1323 root = pw->pw_dir;
1324 else
1325 return (s->cwd);
1327 complete_path:
1328 if (root[skip] == '\0')
1329 return (root);
1330 n = snprintf(path, sizeof path, "%s/%s", root, cwd + skip);
1331 if (n > 0 && (size_t)n < sizeof path)
1332 return (path);
1333 return (s->cwd);