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
,
119 int cmd_session_better(struct session
*, struct session
*, int);
120 struct session
*cmd_choose_session_list(struct sessionslist
*);
121 struct session
*cmd_choose_session(int);
122 struct client
*cmd_choose_client(struct clients
*);
123 struct client
*cmd_lookup_client(const char *);
124 struct session
*cmd_lookup_session(const char *, int *);
125 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
126 int cmd_lookup_index(struct session
*, const char *, int *);
127 struct window_pane
*cmd_lookup_paneid(const char *);
128 struct winlink
*cmd_lookup_winlink_windowid(struct session
*, const char *);
129 struct window
*cmd_lookup_windowid(const char *);
130 struct session
*cmd_window_session(struct cmd_ctx
*,
131 struct window
*, struct winlink
**);
132 struct winlink
*cmd_find_window_offset(const char *, struct session
*, int *);
133 int cmd_find_index_offset(const char *, struct session
*, int *);
134 struct window_pane
*cmd_find_pane_offset(const char *, struct winlink
*);
137 cmd_get_ctx(struct client
*cmdclient
, struct client
*curclient
)
141 ctx
= xcalloc(1, sizeof *ctx
);
144 ctx
->cmdclient
= cmdclient
;
145 ctx
->curclient
= curclient
;
152 cmd_free_ctx(struct cmd_ctx
*ctx
)
154 if (ctx
->cmdclient
!= NULL
)
155 ctx
->cmdclient
->references
--;
156 if (ctx
->curclient
!= NULL
)
157 ctx
->curclient
->references
--;
158 if (--ctx
->references
== 0)
163 cmd_ref_ctx(struct cmd_ctx
*ctx
)
166 if (ctx
->cmdclient
!= NULL
)
167 ctx
->cmdclient
->references
++;
168 if (ctx
->curclient
!= NULL
)
169 ctx
->curclient
->references
++;
173 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
179 for (i
= 0; i
< argc
; i
++) {
180 if (strlcpy(buf
, argv
[i
], len
) >= len
)
182 arglen
= strlen(argv
[i
]) + 1;
191 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
198 *argv
= xcalloc(argc
, sizeof **argv
);
201 for (i
= 0; i
< argc
; i
++) {
203 cmd_free_argv(argc
, *argv
);
207 arglen
= strlen(buf
) + 1;
208 (*argv
)[i
] = xstrdup(buf
);
217 cmd_copy_argv(int argc
, char *const *argv
)
224 new_argv
= xcalloc(argc
, sizeof *new_argv
);
225 for (i
= 0; i
< argc
; i
++) {
227 new_argv
[i
] = xstrdup(argv
[i
]);
233 cmd_free_argv(int argc
, char **argv
)
239 for (i
= 0; i
< argc
; i
++)
245 cmd_parse(int argc
, char **argv
, char **cause
)
247 const struct cmd_entry
**entryp
, *entry
;
255 xasprintf(cause
, "no command");
260 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
261 if ((*entryp
)->alias
!= NULL
&&
262 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
268 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
274 /* Bail now if an exact match. */
275 if (strcmp(entry
->name
, argv
[0]) == 0)
281 xasprintf(cause
, "unknown command: %s", argv
[0]);
285 args
= args_parse(entry
->args_template
, argc
, argv
);
288 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
290 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
292 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
295 cmd
= xmalloc(sizeof *cmd
);
302 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
303 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
305 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
307 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
310 s
[strlen(s
) - 2] = '\0';
311 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
317 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
322 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
326 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
328 used
= args_print(cmd
->args
, buf
+ off
, len
- off
);
339 * Figure out the current session. Use: 1) the current session, if the command
340 * context has one; 2) the most recently used session containing the pty of the
341 * calling client, if any; 3) the session specified in the TMUX variable from
342 * the environment (as passed from the client); 4) the most recently used
343 * session from all sessions.
346 cmd_current_session(struct cmd_ctx
*ctx
, int prefer_unattached
)
348 struct msg_command_data
*data
= ctx
->msgdata
;
349 struct client
*c
= ctx
->cmdclient
;
351 struct sessionslist ss
;
353 struct window_pane
*wp
;
356 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
357 return (ctx
->curclient
->session
);
360 * If the name of the calling client's pty is know, build a list of the
361 * sessions that contain it and if any choose either the first or the
364 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
366 RB_FOREACH(s
, sessions
, &sessions
) {
368 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
369 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
370 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
382 s
= cmd_choose_session_list(&ss
);
388 /* Use the session from the TMUX environment variable. */
389 if (data
!= NULL
&& data
->pid
== getpid() && data
->idx
!= -1) {
390 s
= session_find_by_index(data
->idx
);
395 return (cmd_choose_session(prefer_unattached
));
398 /* Is this session better? */
400 cmd_session_better(struct session
*s
, struct session
*best
,
401 int prefer_unattached
)
405 if (prefer_unattached
) {
406 if (!(best
->flags
& SESSION_UNATTACHED
) &&
407 (s
->flags
& SESSION_UNATTACHED
))
409 else if ((best
->flags
& SESSION_UNATTACHED
) &&
410 !(s
->flags
& SESSION_UNATTACHED
))
413 return (timercmp(&s
->activity_time
, &best
->activity_time
, >));
417 * Find the most recently used session, preferring unattached if the flag is
421 cmd_choose_session(int prefer_unattached
)
423 struct session
*s
, *best
;
426 RB_FOREACH(s
, sessions
, &sessions
) {
427 if (cmd_session_better(s
, best
, prefer_unattached
))
433 /* Find the most recently used session from a list. */
435 cmd_choose_session_list(struct sessionslist
*ss
)
437 struct session
*s
, *sbest
;
438 struct timeval
*tv
= NULL
;
442 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
443 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
446 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
448 tv
= &s
->activity_time
;
456 * Find the current client. First try the current client if set, then pick the
457 * most recently used of the clients attached to the current session if any,
458 * then of all clients.
461 cmd_current_client(struct cmd_ctx
*ctx
)
468 if (ctx
->curclient
!= NULL
)
469 return (ctx
->curclient
);
472 * No current client set. Find the current session and return the
473 * newest of its clients.
475 s
= cmd_current_session(ctx
, 0);
476 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
478 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
479 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
485 c
= cmd_choose_client(&cc
);
491 return (cmd_choose_client(&clients
));
494 /* Choose the most recently used client from a list. */
496 cmd_choose_client(struct clients
*cc
)
498 struct client
*c
, *cbest
;
499 struct timeval
*tv
= NULL
;
503 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
504 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
506 if (c
->session
== NULL
)
509 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
511 tv
= &c
->activity_time
;
518 /* Find the target client or report an error and return NULL. */
520 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
, int quiet
)
526 /* A NULL argument means the current client. */
528 c
= cmd_current_client(ctx
);
529 if (c
== NULL
&& !quiet
)
530 ctx
->error(ctx
, "no clients");
533 tmparg
= xstrdup(arg
);
535 /* Trim a single trailing colon if any. */
536 arglen
= strlen(tmparg
);
537 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
538 tmparg
[arglen
- 1] = '\0';
540 /* Find the client, if any. */
541 c
= cmd_lookup_client(tmparg
);
543 /* If no client found, report an error. */
544 if (c
== NULL
&& !quiet
)
545 ctx
->error(ctx
, "client not found: %s", tmparg
);
552 * Lookup a client by device path. Either of a full match and a match without a
553 * leading _PATH_DEV ("/dev/") is accepted.
556 cmd_lookup_client(const char *name
)
562 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
563 c
= ARRAY_ITEM(&clients
, i
);
564 if (c
== NULL
|| c
->session
== NULL
)
568 /* Check for exact matches. */
569 if (strcmp(name
, path
) == 0)
572 /* Check without leading /dev if present. */
573 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
575 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
582 /* Lookup a session by name. If no session is found, NULL is returned. */
584 cmd_lookup_session(const char *name
, int *ambiguous
)
586 struct session
*s
, *sfound
;
591 * Look for matches. First look for exact matches - session names must
592 * be unique so an exact match can't be ambigious and can just be
595 if ((s
= session_find(name
)) != NULL
)
599 * Otherwise look for partial matches, returning early if it is found to
603 RB_FOREACH(s
, sessions
, &sessions
) {
604 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
605 fnmatch(name
, s
->name
, 0) == 0) {
606 if (sfound
!= NULL
) {
617 * Lookup a window or return -1 if not found or ambigious. First try as an
618 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
619 * idx if the window index is a valid number but there is no window with that
623 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
625 struct winlink
*wl
, *wlfound
;
631 /* Try as a window id. */
632 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
635 /* First see if this is a valid window index in this session. */
636 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
637 if (errstr
== NULL
) {
638 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
642 /* Look for exact matches, error if more than one. */
644 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
645 if (strcmp(name
, wl
->window
->name
) == 0) {
646 if (wlfound
!= NULL
) {
656 /* Now look for pattern matches, again error if multiple. */
658 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
659 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
660 fnmatch(name
, wl
->window
->name
, 0) == 0) {
661 if (wlfound
!= NULL
) {
675 * Find a window index - if the window doesn't exist, check if it is a
676 * potential index and return it anyway.
679 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
685 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
690 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
697 /* Lookup pane id. An initial % means a pane id. */
699 cmd_lookup_paneid(const char *arg
)
707 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
710 return (window_pane_find_by_id(paneid
));
713 /* Lookup window id in a session. An initial @ means a window id. */
715 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
723 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
726 return (winlink_find_by_window_id(&s
->windows
, windowid
));
729 /* Lookup window id. An initial @ means a window id. */
731 cmd_lookup_windowid(const char *arg
)
739 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
742 return (window_find_by_id(windowid
));
745 /* Find session and winlink for window. */
747 cmd_window_session(struct cmd_ctx
*ctx
, struct window
*w
, struct winlink
**wlp
)
750 struct sessionslist ss
;
753 /* If this window is in the current session, return that winlink. */
754 s
= cmd_current_session(ctx
, 0);
756 wl
= winlink_find_by_window(&s
->windows
, w
);
764 /* Otherwise choose from all sessions with this window. */
766 RB_FOREACH(s
, sessions
, &sessions
) {
767 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
770 s
= cmd_choose_session_list(&ss
);
773 *wlp
= winlink_find_by_window(&s
->windows
, w
);
777 /* Find the target session or report an error and return NULL. */
779 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
, int prefer_unattached
)
782 struct window_pane
*wp
;
789 /* A NULL argument means the current session. */
791 return (cmd_current_session(ctx
, prefer_unattached
));
793 /* Lookup as pane id or window id. */
794 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
)
795 return (cmd_window_session(ctx
, wp
->window
, NULL
));
796 if ((w
= cmd_lookup_windowid(arg
)) != NULL
)
797 return (cmd_window_session(ctx
, w
, NULL
));
799 /* Trim a single trailing colon if any. */
800 tmparg
= xstrdup(arg
);
801 arglen
= strlen(tmparg
);
802 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
803 tmparg
[arglen
- 1] = '\0';
805 /* An empty session name is the current session. */
806 if (*tmparg
== '\0') {
808 return (cmd_current_session(ctx
, prefer_unattached
));
811 /* Find the session, if any. */
812 s
= cmd_lookup_session(tmparg
, &ambiguous
);
814 /* If it doesn't, try to match it as a client. */
815 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
818 /* If no session found, report an error. */
821 ctx
->error(ctx
, "more than one session: %s", tmparg
);
823 ctx
->error(ctx
, "session not found: %s", tmparg
);
830 /* Find the target session and window or report an error and return NULL. */
832 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
836 struct window_pane
*wp
;
838 char *sessptr
= NULL
;
842 * Find the current session. There must always be a current session, if
843 * it can't be found, report an error.
845 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
846 ctx
->error(ctx
, "can't establish current session");
850 /* A NULL argument means the current session and window. */
857 /* Lookup as pane id. */
858 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
859 s
= cmd_window_session(ctx
, wp
->window
, &wl
);
865 /* Time to look at the argument. If it is empty, that is an error. */
869 /* Find the separating colon and split into window and session. */
870 winptr
= strchr(arg
, ':');
873 winptr
++; /* skip : */
874 sessptr
= xstrdup(arg
);
875 *strchr(sessptr
, ':') = '\0';
877 /* Try to lookup the session if present. */
878 if (*sessptr
!= '\0') {
879 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
886 * Then work out the window. An empty string is the current window,
887 * otherwise try special cases then to look it up in the session.
891 else if (winptr
[0] == '!' && winptr
[1] == '\0')
892 wl
= TAILQ_FIRST(&s
->lastw
);
893 else if (winptr
[0] == '^' && winptr
[1] == '\0')
894 wl
= RB_MIN(winlinks
, &s
->windows
);
895 else if (winptr
[0] == '$' && winptr
[1] == '\0')
896 wl
= RB_MAX(winlinks
, &s
->windows
);
897 else if (winptr
[0] == '+' || winptr
[0] == '-')
898 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
900 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
910 * No colon in the string, first try special cases, then as a window
911 * and lastly as a session.
913 if (arg
[0] == '!' && arg
[1] == '\0') {
914 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
916 } else if (arg
[0] == '+' || arg
[0] == '-') {
917 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
919 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
930 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
940 ctx
->error(ctx
, "multiple sessions: %s", arg
);
942 ctx
->error(ctx
, "session not found: %s", arg
);
948 ctx
->error(ctx
, "multiple windows: %s", arg
);
950 ctx
->error(ctx
, "window not found: %s", arg
);
956 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
961 if (winptr
[1] != '\0')
962 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
964 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
966 if (winptr
[0] == '+')
967 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
969 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
976 * Find the target session and window index, whether or not it exists in the
977 * session. Return -2 on error or -1 if no window index is specified. This is
978 * used when parsing an argument for a window target that may not exist (for
979 * example if it is going to be created).
982 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
987 char *sessptr
= NULL
;
988 int idx
, ambiguous
= 0;
991 * Find the current session. There must always be a current session, if
992 * it can't be found, report an error.
994 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
995 ctx
->error(ctx
, "can't establish current session");
999 /* A NULL argument means the current session and "no window" (-1). */
1006 /* Time to look at the argument. If it is empty, that is an error. */
1010 /* Find the separating colon. If none, assume the current session. */
1011 winptr
= strchr(arg
, ':');
1014 winptr
++; /* skip : */
1015 sessptr
= xstrdup(arg
);
1016 *strchr(sessptr
, ':') = '\0';
1018 /* Try to lookup the session if present. */
1019 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
1020 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
1027 * Then work out the window. An empty string is a new window otherwise
1028 * try to look it up in the session.
1030 if (*winptr
== '\0')
1032 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
1033 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1036 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
1037 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
1039 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
1047 * No colon in the string, first try special cases, then as a window
1048 * and lastly as a session.
1050 if (arg
[0] == '!' && arg
[1] == '\0') {
1051 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1054 } else if (arg
[0] == '+' || arg
[0] == '-') {
1055 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
1056 goto lookup_session
;
1057 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
1058 goto lookup_session
;
1068 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1078 ctx
->error(ctx
, "multiple sessions: %s", arg
);
1080 ctx
->error(ctx
, "session not found: %s", arg
);
1087 ctx
->error(ctx
, "invalid index: %s", arg
);
1094 ctx
->error(ctx
, "multiple windows: %s", arg
);
1096 ctx
->error(ctx
, "window not found: %s", arg
);
1102 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1104 int idx
, offset
= 1;
1106 if (winptr
[1] != '\0')
1107 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1109 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1111 if (winptr
[0] == '+') {
1112 if (s
->curw
->idx
== INT_MAX
)
1113 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1115 idx
= s
->curw
->idx
+ offset
;
1117 if (s
->curw
->idx
== 0)
1118 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1120 idx
= s
->curw
->idx
- offset
;
1128 * Find the target session, window and pane number or report an error and
1129 * return NULL. The pane number is separated from the session:window by a .,
1130 * such as mysession:mywindow.0.
1133 cmd_find_pane(struct cmd_ctx
*ctx
,
1134 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1138 const char *period
, *errstr
;
1139 char *winptr
, *paneptr
;
1142 /* Get the current session. */
1143 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
1144 ctx
->error(ctx
, "can't establish current session");
1150 /* A NULL argument means the current session, window and pane. */
1152 *wpp
= s
->curw
->window
->active
;
1156 /* Lookup as pane id. */
1157 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1158 s
= cmd_window_session(ctx
, (*wpp
)->window
, &wl
);
1164 /* Look for a separating period. */
1165 if ((period
= strrchr(arg
, '.')) == NULL
)
1168 /* Pull out the window part and parse it. */
1169 winptr
= xstrdup(arg
);
1170 winptr
[period
- arg
] = '\0';
1171 if (*winptr
== '\0')
1173 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
1176 /* Find the pane section and look it up. */
1177 paneptr
= winptr
+ (period
- arg
) + 1;
1178 if (*paneptr
== '\0')
1179 *wpp
= wl
->window
->active
;
1180 else if (paneptr
[0] == '+' || paneptr
[0] == '-')
1181 *wpp
= cmd_find_pane_offset(paneptr
, wl
);
1183 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1186 *wpp
= window_pane_at_index(wl
->window
, idx
);
1195 /* Try pane string description. */
1196 if ((*wpp
= window_find_string(wl
->window
, paneptr
)) == NULL
) {
1197 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
1205 /* Try as a pane number alone. */
1206 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1210 /* Try index in the current session and window. */
1211 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1217 /* Try pane string description. */
1218 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
1221 /* Try as a window and use the active pane. */
1222 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
1223 *wpp
= wl
->window
->active
;
1231 struct window_pane
*
1232 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1234 struct window
*w
= wl
->window
;
1235 struct window_pane
*wp
= w
->active
;
1238 if (paneptr
[1] != '\0')
1239 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1241 if (paneptr
[0] == '+')
1242 wp
= window_pane_next_by_number(w
, wp
, offset
);
1244 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1250 /* Replace the first %% or %idx in template by s. */
1252 cmd_template_replace(const char *template, const char *s
, int idx
)
1259 if (strchr(template, '%') == NULL
)
1260 return (xstrdup(template));
1268 while (*ptr
!= '\0') {
1269 switch (ch
= *ptr
++) {
1271 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1272 if (*ptr
!= '%' || replaced
)
1279 buf
= xrealloc(buf
, 1, len
+ 1);
1280 strlcat(buf
, s
, len
+ 1);
1283 buf
= xrealloc(buf
, 1, len
+ 2);
1292 * Return the default path for a new pane, using the given path or the
1293 * default-path option if it is NULL. Several special values are accepted: the
1294 * empty string or relative path for the current pane's working directory, ~
1295 * for the user's home, - for the session working directory, . for the tmux
1296 * server's working directory. The default on failure is the session's working
1300 cmd_get_default_path(struct cmd_ctx
*ctx
, const char *cwd
)
1303 struct environ_entry
*envent
;
1305 char tmp
[MAXPATHLEN
];
1309 static char path
[MAXPATHLEN
];
1311 if ((s
= cmd_current_session(ctx
, 0)) == NULL
)
1315 cwd
= options_get_string(&s
->options
, "default-path");
1318 if (strcmp(cwd
, "$HOME") == 0 || strncmp(cwd
, "$HOME/", 6) == 0) {
1319 /* User's home directory - $HOME. */
1322 } else if (cwd
[0] == '~' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1323 /* User's home directory - ~. */
1325 } else if (cwd
[0] == '-' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1326 /* Session working directory. */
1329 } else if (cwd
[0] == '.' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1330 /* Server working directory. */
1331 if (getcwd(tmp
, sizeof tmp
) != NULL
) {
1336 } else if (*cwd
== '/') {
1337 /* Absolute path. */
1340 /* Empty or relative path. */
1341 if (ctx
->cmdclient
!= NULL
&& ctx
->cmdclient
->cwd
!= NULL
)
1342 root
= ctx
->cmdclient
->cwd
;
1343 else if (ctx
->curclient
!= NULL
&& s
->curw
!= NULL
)
1344 root
= get_proc_cwd(s
->curw
->window
->active
->fd
);
1355 envent
= environ_find(&global_environ
, "HOME");
1356 if (envent
!= NULL
&& *envent
->value
!= '\0')
1357 root
= envent
->value
;
1358 else if ((pw
= getpwuid(getuid())) != NULL
)
1364 if (root
[skip
] == '\0') {
1365 strlcpy(path
, root
, sizeof path
);
1368 n
= snprintf(path
, sizeof path
, "%s/%s", root
, cwd
+ skip
);
1369 if (n
> 0 && (size_t)n
< sizeof path
)