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_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
,
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
,
54 &cmd_kill_server_entry
,
55 &cmd_kill_session_entry
,
56 &cmd_kill_window_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
,
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_window_entry
,
72 &cmd_new_session_entry
,
73 &cmd_new_window_entry
,
74 &cmd_next_layout_entry
,
75 &cmd_next_window_entry
,
76 &cmd_paste_buffer_entry
,
78 &cmd_previous_layout_entry
,
79 &cmd_previous_window_entry
,
80 &cmd_refresh_client_entry
,
81 &cmd_rename_session_entry
,
82 &cmd_rename_window_entry
,
83 &cmd_resize_pane_entry
,
84 &cmd_respawn_pane_entry
,
85 &cmd_respawn_window_entry
,
86 &cmd_rotate_window_entry
,
88 &cmd_save_buffer_entry
,
89 &cmd_select_layout_entry
,
90 &cmd_select_pane_entry
,
91 &cmd_select_window_entry
,
93 &cmd_send_prefix_entry
,
94 &cmd_server_info_entry
,
95 &cmd_set_buffer_entry
,
96 &cmd_set_environment_entry
,
97 &cmd_set_option_entry
,
98 &cmd_set_window_option_entry
,
99 &cmd_show_buffer_entry
,
100 &cmd_show_environment_entry
,
101 &cmd_show_messages_entry
,
102 &cmd_show_options_entry
,
103 &cmd_show_window_options_entry
,
104 &cmd_source_file_entry
,
105 &cmd_split_window_entry
,
106 &cmd_start_server_entry
,
107 &cmd_suspend_client_entry
,
108 &cmd_swap_pane_entry
,
109 &cmd_swap_window_entry
,
110 &cmd_switch_client_entry
,
111 &cmd_unbind_key_entry
,
112 &cmd_unlink_window_entry
,
116 struct session
*cmd_choose_session_list(struct sessionslist
*);
117 struct session
*cmd_choose_session(int);
118 struct client
*cmd_choose_client(struct clients
*);
119 struct client
*cmd_lookup_client(const char *);
120 struct session
*cmd_lookup_session(const char *, int *);
121 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
122 int cmd_lookup_index(struct session
*, const char *, int *);
123 struct window_pane
*cmd_lookup_paneid(const char *);
124 struct winlink
*cmd_lookup_winlink_windowid(struct session
*, const char *);
125 struct window
*cmd_lookup_windowid(const char *);
126 struct session
*cmd_window_session(struct cmd_ctx
*,
127 struct window
*, struct winlink
**);
128 struct winlink
*cmd_find_window_offset(const char *, struct session
*, int *);
129 int cmd_find_index_offset(const char *, struct session
*, int *);
130 struct window_pane
*cmd_find_pane_offset(const char *, struct winlink
*);
133 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
139 for (i
= 0; i
< argc
; i
++) {
140 if (strlcpy(buf
, argv
[i
], len
) >= len
)
142 arglen
= strlen(argv
[i
]) + 1;
151 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
158 *argv
= xcalloc(argc
, sizeof **argv
);
161 for (i
= 0; i
< argc
; i
++) {
163 cmd_free_argv(argc
, *argv
);
167 arglen
= strlen(buf
) + 1;
168 (*argv
)[i
] = xstrdup(buf
);
177 cmd_copy_argv(int argc
, char *const *argv
)
184 new_argv
= xcalloc(argc
, sizeof *new_argv
);
185 for (i
= 0; i
< argc
; i
++) {
187 new_argv
[i
] = xstrdup(argv
[i
]);
193 cmd_free_argv(int argc
, char **argv
)
199 for (i
= 0; i
< argc
; i
++) {
207 cmd_parse(int argc
, char **argv
, char **cause
)
209 const struct cmd_entry
**entryp
, *entry
;
217 xasprintf(cause
, "no command");
222 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
223 if ((*entryp
)->alias
!= NULL
&&
224 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
230 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
236 /* Bail now if an exact match. */
237 if (strcmp(entry
->name
, argv
[0]) == 0)
243 xasprintf(cause
, "unknown command: %s", argv
[0]);
247 args
= args_parse(entry
->args_template
, argc
, argv
);
250 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
252 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
254 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
257 cmd
= xmalloc(sizeof *cmd
);
264 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
265 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
267 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
269 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
272 s
[strlen(s
) - 2] = '\0';
273 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
279 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
284 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
286 return (cmd
->entry
->exec(cmd
, ctx
));
290 cmd_free(struct cmd
*cmd
)
292 if (cmd
->args
!= NULL
)
293 args_free(cmd
->args
);
298 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
302 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
304 used
= args_print(cmd
->args
, buf
+ off
, len
- 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.
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
;
328 struct sessionslist ss
;
330 struct window_pane
*wp
;
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
341 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
343 RB_FOREACH(s
, sessions
, &sessions
) {
345 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
346 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
347 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
359 s
= cmd_choose_session_list(&ss
);
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
);
372 return (cmd_choose_session(prefer_unattached
));
376 * Find the most recently used session, preferring unattached if the flag is
380 cmd_choose_session(int prefer_unattached
)
382 struct session
*s
, *sbest
;
383 struct timeval
*tv
= 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
))) {
392 tv
= &s
->activity_time
;
399 /* Find the most recently used session from a list. */
401 cmd_choose_session_list(struct sessionslist
*ss
)
403 struct session
*s
, *sbest
;
404 struct timeval
*tv
= NULL
;
408 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
409 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
412 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
414 tv
= &s
->activity_time
;
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.
427 cmd_current_client(struct cmd_ctx
*ctx
)
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
)) {
444 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
445 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
451 c
= cmd_choose_client(&cc
);
457 return (cmd_choose_client(&clients
));
460 /* Choose the most recently used client from a list. */
462 cmd_choose_client(struct clients
*cc
)
464 struct client
*c
, *cbest
;
465 struct timeval
*tv
= NULL
;
469 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
470 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
472 if (c
->session
== NULL
)
475 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
477 tv
= &c
->activity_time
;
484 /* Find the target client or report an error and return NULL. */
486 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
492 /* A NULL argument means the current client. */
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. */
507 ctx
->error(ctx
, "client not found: %s", tmparg
);
514 * Lookup a client by device path. Either of a full match and a match without a
515 * leading _PATH_DEV ("/dev/") is accepted.
518 cmd_lookup_client(const char *name
)
524 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
525 c
= ARRAY_ITEM(&clients
, i
);
526 if (c
== NULL
|| c
->session
== NULL
)
530 /* Check for exact matches. */
531 if (strcmp(name
, path
) == 0)
534 /* Check without leading /dev if present. */
535 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
537 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
544 /* Lookup a session by name. If no session is found, NULL is returned. */
546 cmd_lookup_session(const char *name
, int *ambiguous
)
548 struct session
*s
, *sfound
;
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
557 if ((s
= session_find(name
)) != NULL
)
561 * Otherwise look for partial matches, returning early if it is found to
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
) {
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
585 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
587 struct winlink
*wl
, *wlfound
;
593 /* Try as a window id. */
594 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
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
)
604 /* Look for exact matches, error if more than one. */
606 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
607 if (strcmp(name
, wl
->window
->name
) == 0) {
608 if (wlfound
!= NULL
) {
618 /* Now look for pattern matches, again error if multiple. */
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
) {
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
)
647 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
652 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
659 /* Lookup pane id. An initial % means a pane id. */
661 cmd_lookup_paneid(const char *arg
)
669 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
672 return (window_pane_find_by_id(paneid
));
675 /* Lookup window id in a session. An initial @ means a window id. */
677 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
685 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
688 return (winlink_find_by_window_id(&s
->windows
, windowid
));
691 /* Lookup window id. An initial @ means a window id. */
693 cmd_lookup_windowid(const char *arg
)
701 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
704 return (window_find_by_id(windowid
));
707 /* Find session and winlink for window. */
709 cmd_window_session(struct cmd_ctx
*ctx
, struct window
*w
, struct winlink
**wlp
)
712 struct sessionslist ss
;
715 /* If this window is in the current session, return that winlink. */
716 s
= cmd_current_session(ctx
, 0);
718 wl
= winlink_find_by_window(&s
->windows
, w
);
726 /* Otherwise choose from all sessions with this window. */
728 RB_FOREACH(s
, sessions
, &sessions
) {
729 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
732 s
= cmd_choose_session_list(&ss
);
735 *wlp
= winlink_find_by_window(&s
->windows
, w
);
739 /* Find the target session or report an error and return NULL. */
741 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
, int prefer_unattached
)
744 struct window_pane
*wp
;
751 /* A NULL argument means the current session. */
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') {
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
)
780 /* If no session found, report an error. */
783 ctx
->error(ctx
, "more than one session: %s", tmparg
);
785 ctx
->error(ctx
, "session not found: %s", tmparg
);
792 /* Find the target session and window or report an error and return NULL. */
794 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
798 struct window_pane
*wp
;
800 char *sessptr
= NULL
;
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");
812 /* A NULL argument means the current session and window. */
819 /* Lookup as pane id. */
820 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
821 s
= cmd_window_session(ctx
, wp
->window
, &wl
);
827 /* Time to look at the argument. If it is empty, that is an error. */
831 /* Find the separating colon and split into window and session. */
832 winptr
= strchr(arg
, ':');
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
)
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.
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
);
858 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
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
)
874 } else if (arg
[0] == '+' || arg
[0] == '-') {
875 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
877 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
888 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
898 ctx
->error(ctx
, "multiple sessions: %s", arg
);
900 ctx
->error(ctx
, "session not found: %s", arg
);
907 ctx
->error(ctx
, "multiple windows: %s", arg
);
909 ctx
->error(ctx
, "window not found: %s", arg
);
916 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
921 if (winptr
[1] != '\0')
922 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
924 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
926 if (winptr
[0] == '+')
927 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
929 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
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
)
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");
959 /* A NULL argument means the current session and "no window" (-1). */
966 /* Time to look at the argument. If it is empty, that is an error. */
970 /* Find the separating colon. If none, assume the current session. */
971 winptr
= strchr(arg
, ':');
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
)
987 * Then work out the window. An empty string is a new window otherwise
988 * try to look it up in the session.
992 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
993 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
996 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
997 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
999 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
1002 if (sessptr
!= NULL
)
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
)
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
;
1029 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1039 ctx
->error(ctx
, "multiple sessions: %s", arg
);
1041 ctx
->error(ctx
, "session not found: %s", arg
);
1042 if (sessptr
!= NULL
)
1049 ctx
->error(ctx
, "invalid index: %s", arg
);
1051 if (sessptr
!= NULL
)
1057 ctx
->error(ctx
, "multiple windows: %s", arg
);
1059 ctx
->error(ctx
, "window not found: %s", arg
);
1060 if (sessptr
!= NULL
)
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
);
1073 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1075 if (winptr
[0] == '+') {
1076 if (s
->curw
->idx
== INT_MAX
)
1077 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1079 idx
= s
->curw
->idx
+ offset
;
1081 if (s
->curw
->idx
== 0)
1082 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1084 idx
= s
->curw
->idx
- offset
;
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.
1097 cmd_find_pane(struct cmd_ctx
*ctx
,
1098 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1102 const char *period
, *errstr
;
1103 char *winptr
, *paneptr
;
1106 /* Get the current session. */
1107 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
1108 ctx
->error(ctx
, "can't establish current session");
1114 /* A NULL argument means the current session, window and pane. */
1116 *wpp
= s
->curw
->window
->active
;
1120 /* Lookup as pane id. */
1121 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1122 s
= cmd_window_session(ctx
, (*wpp
)->window
, &wl
);
1128 /* Look for a separating period. */
1129 if ((period
= strrchr(arg
, '.')) == NULL
)
1132 /* Pull out the window part and parse it. */
1133 winptr
= xstrdup(arg
);
1134 winptr
[period
- arg
] = '\0';
1135 if (*winptr
== '\0')
1137 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
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
);
1147 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1150 *wpp
= window_pane_at_index(wl
->window
, idx
);
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
);
1169 /* Try as a pane number alone. */
1170 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1174 /* Try index in the current session and window. */
1175 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1181 /* Try pane string description. */
1182 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
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
;
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
;
1202 if (paneptr
[1] != '\0')
1203 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1205 if (paneptr
[0] == '+')
1206 wp
= window_pane_next_by_number(w
, wp
, offset
);
1208 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1214 /* Replace the first %% or %idx in template by s. */
1216 cmd_template_replace(char *template, const char *s
, int idx
)
1223 if (strstr(template, "%") == NULL
)
1224 return (xstrdup(template));
1232 while (*ptr
!= '\0') {
1233 switch (ch
= *ptr
++) {
1235 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1236 if (*ptr
!= '%' || replaced
)
1243 buf
= xrealloc(buf
, 1, len
+ 1);
1244 strlcat(buf
, s
, len
+ 1);
1247 buf
= xrealloc(buf
, 1, len
+ 2);
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
1264 cmd_get_default_path(struct cmd_ctx
*ctx
, const char *cwd
)
1267 struct environ_entry
*envent
;
1269 char tmp
[MAXPATHLEN
];
1273 static char path
[MAXPATHLEN
];
1275 if ((s
= cmd_current_session(ctx
, 0)) == NULL
)
1279 cwd
= options_get_string(&s
->options
, "default-path");
1282 if (strcmp(cwd
, "$HOME") == 0 || strncmp(cwd
, "$HOME/", 6) == 0) {
1283 /* User's home directory - $HOME. */
1286 } else if (cwd
[0] == '~' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1287 /* User's home directory - ~. */
1289 } else if (cwd
[0] == '-' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1290 /* Session working directory. */
1293 } else if (cwd
[0] == '.' && (cwd
[1] == '\0' || cwd
[1] == '/')){
1294 /* Server working directory. */
1295 if (getcwd(tmp
, sizeof tmp
) != NULL
) {
1300 } else if (*cwd
== '/') {
1301 /* Absolute path. */
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
);
1318 envent
= environ_find(&global_environ
, "HOME");
1319 if (envent
!= NULL
&& *envent
->value
!= '\0')
1320 root
= envent
->value
;
1321 else if ((pw
= getpwuid(getuid())) != NULL
)
1327 if (root
[skip
] == '\0')
1329 n
= snprintf(path
, sizeof path
, "%s/%s", root
, cwd
+ skip
);
1330 if (n
> 0 && (size_t)n
< sizeof path
)