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>
31 const struct cmd_entry
*cmd_table
[] = {
32 &cmd_attach_session_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
,
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
,
56 &cmd_kill_server_entry
,
57 &cmd_kill_session_entry
,
58 &cmd_kill_window_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
,
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
,
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
,
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
,
91 &cmd_save_buffer_entry
,
92 &cmd_select_layout_entry
,
93 &cmd_select_pane_entry
,
94 &cmd_select_window_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
,
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
*,
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
)
144 for (i
= 0; i
< argc
; i
++) {
145 if (strlcpy(buf
, argv
[i
], len
) >= len
)
147 arglen
= strlen(argv
[i
]) + 1;
156 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
163 *argv
= xcalloc(argc
, sizeof **argv
);
166 for (i
= 0; i
< argc
; i
++) {
168 cmd_free_argv(argc
, *argv
);
172 arglen
= strlen(buf
) + 1;
173 (*argv
)[i
] = xstrdup(buf
);
182 cmd_copy_argv(int argc
, char *const *argv
)
189 new_argv
= xcalloc(argc
, sizeof *new_argv
);
190 for (i
= 0; i
< argc
; i
++) {
192 new_argv
[i
] = xstrdup(argv
[i
]);
198 cmd_free_argv(int argc
, char **argv
)
204 for (i
= 0; i
< argc
; i
++)
210 cmd_parse(int argc
, char **argv
, const char *file
, u_int line
, char **cause
)
212 const struct cmd_entry
**entryp
, *entry
;
220 xasprintf(cause
, "no command");
225 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
226 if ((*entryp
)->alias
!= NULL
&&
227 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
233 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
239 /* Bail now if an exact match. */
240 if (strcmp(entry
->name
, argv
[0]) == 0)
246 xasprintf(cause
, "unknown command: %s", argv
[0]);
250 args
= args_parse(entry
->args_template
, argc
, argv
);
253 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
255 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
257 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
260 cmd
= xcalloc(1, sizeof *cmd
);
265 cmd
->file
= xstrdup(file
);
272 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
273 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
275 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
277 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
280 s
[strlen(s
) - 2] = '\0';
281 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
287 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
292 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
296 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
298 used
= args_print(cmd
->args
, buf
+ off
, len
- 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.
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
;
321 struct sessionslist ss
;
323 struct window_pane
*wp
;
327 if (c
!= NULL
&& c
->session
!= NULL
)
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
335 path
= c
== NULL
? NULL
: c
->tty
.path
;
338 RB_FOREACH(s
, sessions
, &sessions
) {
340 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
341 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
342 if (strcmp(wp
->tty
, path
) == 0) {
354 s
= cmd_choose_session_list(&ss
);
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
);
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
)
377 if (prefer_unattached
) {
378 if (!(best
->flags
& SESSION_UNATTACHED
) &&
379 (s
->flags
& SESSION_UNATTACHED
))
381 else if ((best
->flags
& SESSION_UNATTACHED
) &&
382 !(s
->flags
& SESSION_UNATTACHED
))
385 return (timercmp(&s
->activity_time
, &best
->activity_time
, >));
389 * Find the most recently used session, preferring unattached if the flag is
393 cmd_choose_session(int prefer_unattached
)
395 struct session
*s
, *best
;
398 RB_FOREACH(s
, sessions
, &sessions
) {
399 if (cmd_session_better(s
, best
, prefer_unattached
))
405 /* Find the most recently used session from a list. */
407 cmd_choose_session_list(struct sessionslist
*ss
)
409 struct session
*s
, *sbest
;
410 struct timeval
*tv
= NULL
;
414 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
415 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
418 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
420 tv
= &s
->activity_time
;
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.
433 cmd_current_client(struct cmd_q
*cmdq
)
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
)) {
450 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
451 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
457 c
= cmd_choose_client(&cc
);
463 return (cmd_choose_client(&clients
));
466 /* Choose the most recently used client from a list. */
468 cmd_choose_client(struct clients
*cc
)
470 struct client
*c
, *cbest
;
471 struct timeval
*tv
= NULL
;
475 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
476 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
478 if (c
->session
== NULL
)
481 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
483 tv
= &c
->activity_time
;
490 /* Find the target client or report an error and return NULL. */
492 cmd_find_client(struct cmd_q
*cmdq
, const char *arg
, int quiet
)
498 /* A NULL argument means the current client. */
500 c
= cmd_current_client(cmdq
);
501 if (c
== NULL
&& !quiet
)
502 cmdq_error(cmdq
, "no clients");
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
);
524 * Lookup a client by device path. Either of a full match and a match without a
525 * leading _PATH_DEV ("/dev/") is accepted.
528 cmd_lookup_client(const char *name
)
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
)
540 /* Check for exact matches. */
541 if (strcmp(name
, path
) == 0)
544 /* Check without leading /dev if present. */
545 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
547 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
554 /* Lookup a session by name. If no session is found, NULL is returned. */
556 cmd_lookup_session(const char *name
, int *ambiguous
)
558 struct session
*s
, *sfound
;
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
567 if ((s
= session_find(name
)) != NULL
)
571 * Otherwise look for partial matches, returning early if it is found to
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
) {
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
595 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
597 struct winlink
*wl
, *wlfound
;
603 /* Try as a window id. */
604 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
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
)
614 /* Look for exact matches, error if more than one. */
616 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
617 if (strcmp(name
, wl
->window
->name
) == 0) {
618 if (wlfound
!= NULL
) {
628 /* Now look for pattern matches, again error if multiple. */
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
) {
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
)
657 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
662 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
669 /* Lookup pane id. An initial % means a pane id. */
671 cmd_lookup_paneid(const char *arg
)
679 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
682 return (window_pane_find_by_id(paneid
));
685 /* Lookup window id in a session. An initial @ means a window id. */
687 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
695 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
698 return (winlink_find_by_window_id(&s
->windows
, windowid
));
701 /* Lookup window id. An initial @ means a window id. */
703 cmd_lookup_windowid(const char *arg
)
711 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
714 return (window_find_by_id(windowid
));
717 /* Find session and winlink for window. */
719 cmd_window_session(struct cmd_q
*cmdq
, struct window
*w
, struct winlink
**wlp
)
722 struct sessionslist ss
;
725 /* If this window is in the current session, return that winlink. */
726 s
= cmd_current_session(cmdq
, 0);
728 wl
= winlink_find_by_window(&s
->windows
, w
);
736 /* Otherwise choose from all sessions with this window. */
738 RB_FOREACH(s
, sessions
, &sessions
) {
739 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
742 s
= cmd_choose_session_list(&ss
);
745 *wlp
= winlink_find_by_window(&s
->windows
, w
);
749 /* Find the target session or report an error and return NULL. */
751 cmd_find_session(struct cmd_q
*cmdq
, const char *arg
, int prefer_unattached
)
754 struct window_pane
*wp
;
761 /* A NULL argument means the current session. */
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') {
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
)
790 /* If no session found, report an error. */
793 cmdq_error(cmdq
, "more than one session: %s", tmparg
);
795 cmdq_error(cmdq
, "session not found: %s", tmparg
);
802 /* Find the target session and window or report an error and return NULL. */
804 cmd_find_window(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
808 struct window_pane
*wp
;
810 char *sessptr
= NULL
;
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");
822 /* A NULL argument means the current session and window. */
829 /* Lookup as pane id. */
830 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
831 s
= cmd_window_session(cmdq
, wp
->window
, &wl
);
837 /* Time to look at the argument. If it is empty, that is an error. */
841 /* Find the separating colon and split into window and session. */
842 winptr
= strchr(arg
, ':');
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
)
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.
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
);
872 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
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
)
888 } else if (arg
[0] == '+' || arg
[0] == '-') {
889 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
891 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
902 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
912 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
914 cmdq_error(cmdq
, "session not found: %s", arg
);
920 cmdq_error(cmdq
, "multiple windows: %s", arg
);
922 cmdq_error(cmdq
, "window not found: %s", arg
);
928 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
933 if (winptr
[1] != '\0')
934 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
936 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
938 if (winptr
[0] == '+')
939 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
941 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
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
)
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");
971 /* A NULL argument means the current session and "no window" (-1). */
978 /* Time to look at the argument. If it is empty, that is an error. */
982 /* Find the separating colon. If none, assume the current session. */
983 winptr
= strchr(arg
, ':');
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
)
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')
1004 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
1005 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1008 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
1009 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
1011 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
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
)
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
;
1040 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1050 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
1052 cmdq_error(cmdq
, "session not found: %s", arg
);
1059 cmdq_error(cmdq
, "invalid index: %s", arg
);
1066 cmdq_error(cmdq
, "multiple windows: %s", arg
);
1068 cmdq_error(cmdq
, "window not found: %s", arg
);
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
);
1081 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1083 if (winptr
[0] == '+') {
1084 if (s
->curw
->idx
== INT_MAX
)
1085 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1087 idx
= s
->curw
->idx
+ offset
;
1089 if (s
->curw
->idx
== 0)
1090 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1092 idx
= s
->curw
->idx
- offset
;
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.
1105 cmd_find_pane(struct cmd_q
*cmdq
,
1106 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1110 const char *period
, *errstr
;
1111 char *winptr
, *paneptr
;
1114 /* Get the current session. */
1115 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
1116 cmdq_error(cmdq
, "can't establish current session");
1122 /* A NULL argument means the current session, window and pane. */
1124 *wpp
= s
->curw
->window
->active
;
1128 /* Lookup as pane id. */
1129 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1130 s
= cmd_window_session(cmdq
, (*wpp
)->window
, &wl
);
1136 /* Look for a separating period. */
1137 if ((period
= strrchr(arg
, '.')) == NULL
)
1140 /* Pull out the window part and parse it. */
1141 winptr
= xstrdup(arg
);
1142 winptr
[period
- arg
] = '\0';
1143 if (*winptr
== '\0')
1145 else if ((wl
= cmd_find_window(cmdq
, winptr
, sp
)) == NULL
)
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
);
1155 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1158 *wpp
= window_pane_at_index(wl
->window
, idx
);
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
);
1177 /* Try as a pane number alone. */
1178 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1182 /* Try index in the current session and window. */
1183 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1189 /* Try pane string description. */
1190 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
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
;
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
;
1210 if (paneptr
[1] != '\0')
1211 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1213 if (paneptr
[0] == '+')
1214 wp
= window_pane_next_by_number(w
, wp
, offset
);
1216 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1222 /* Replace the first %% or %idx in template by s. */
1224 cmd_template_replace(const char *template, const char *s
, int idx
)
1231 if (strchr(template, '%') == NULL
)
1232 return (xstrdup(template));
1240 while (*ptr
!= '\0') {
1241 switch (ch
= *ptr
++) {
1243 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1244 if (*ptr
!= '%' || replaced
)
1251 buf
= xrealloc(buf
, 1, len
+ 1);
1252 strlcat(buf
, s
, len
+ 1);
1255 buf
= xrealloc(buf
, 1, len
+ 2);
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
1272 cmd_get_default_path(struct cmd_q
*cmdq
, const char *cwd
)
1274 struct client
*c
= cmdq
->client
;
1276 struct environ_entry
*envent
;
1278 char tmp
[MAXPATHLEN
];
1282 static char path
[MAXPATHLEN
];
1284 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
)
1288 cwd
= options_get_string(&s
->options
, "default-path");
1291 if (strcmp(cwd
, "$HOME") == 0 || strncmp(cwd
, "$HOME/", 6) == 0) {
1292 /* User's home directory - $HOME. */
1295 } else if (cwd
[0] == '~' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1296 /* User's home directory - ~. */
1298 } else if (cwd
[0] == '-' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1299 /* Session working directory. */
1302 } else if (cwd
[0] == '.' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1303 /* Server working directory. */
1304 if (getcwd(tmp
, sizeof tmp
) != NULL
) {
1309 } else if (*cwd
== '/') {
1310 /* Absolute path. */
1313 /* Empty or relative path. */
1314 if (c
!= NULL
&& c
->session
== NULL
&& c
->cwd
!= NULL
)
1316 else if (s
->curw
!= NULL
)
1317 root
= get_proc_cwd(s
->curw
->window
->active
->fd
);
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
)
1337 if (root
[skip
] == '\0') {
1338 strlcpy(path
, root
, sizeof path
);
1341 n
= snprintf(path
, sizeof path
, "%s/%s", root
, cwd
+ skip
);
1342 if (n
> 0 && (size_t)n
< sizeof path
)