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_q
*, struct window
*,
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_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
143 for (i
= 0; i
< argc
; i
++) {
144 if (strlcpy(buf
, argv
[i
], len
) >= len
)
146 arglen
= strlen(argv
[i
]) + 1;
155 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
162 *argv
= xcalloc(argc
, sizeof **argv
);
165 for (i
= 0; i
< argc
; i
++) {
167 cmd_free_argv(argc
, *argv
);
171 arglen
= strlen(buf
) + 1;
172 (*argv
)[i
] = xstrdup(buf
);
181 cmd_copy_argv(int argc
, char *const *argv
)
188 new_argv
= xcalloc(argc
, sizeof *new_argv
);
189 for (i
= 0; i
< argc
; i
++) {
191 new_argv
[i
] = xstrdup(argv
[i
]);
197 cmd_free_argv(int argc
, char **argv
)
203 for (i
= 0; i
< argc
; i
++)
209 cmd_parse(int argc
, char **argv
, const char *file
, u_int line
, char **cause
)
211 const struct cmd_entry
**entryp
, *entry
;
219 xasprintf(cause
, "no command");
224 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
225 if ((*entryp
)->alias
!= NULL
&&
226 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
232 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
238 /* Bail now if an exact match. */
239 if (strcmp(entry
->name
, argv
[0]) == 0)
245 xasprintf(cause
, "unknown command: %s", argv
[0]);
249 args
= args_parse(entry
->args_template
, argc
, argv
);
252 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
254 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
256 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
259 cmd
= xcalloc(1, sizeof *cmd
);
264 cmd
->file
= xstrdup(file
);
271 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
272 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
274 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
276 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
279 s
[strlen(s
) - 2] = '\0';
280 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
286 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
291 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
295 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
297 used
= args_print(cmd
->args
, buf
+ off
, len
- off
);
308 * Figure out the current session. Use: 1) the current session, if the command
309 * context has one; 2) the most recently used session containing the pty of the
310 * calling client, if any; 3) the session specified in the TMUX variable from
311 * the environment (as passed from the client); 4) the most recently used
312 * session from all sessions.
315 cmd_current_session(struct cmd_q
*cmdq
, int prefer_unattached
)
317 struct msg_command_data
*data
= cmdq
->msgdata
;
318 struct client
*c
= cmdq
->client
;
320 struct sessionslist ss
;
322 struct window_pane
*wp
;
326 if (c
!= NULL
&& c
->session
!= NULL
)
330 * If the name of the calling client's pty is know, build a list of the
331 * sessions that contain it and if any choose either the first or the
334 path
= c
== NULL
? NULL
: c
->tty
.path
;
337 RB_FOREACH(s
, sessions
, &sessions
) {
339 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
340 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
341 if (strcmp(wp
->tty
, path
) == 0) {
353 s
= cmd_choose_session_list(&ss
);
359 /* Use the session from the TMUX environment variable. */
360 if (data
!= NULL
&& data
->pid
== getpid() && data
->idx
!= -1) {
361 s
= session_find_by_index(data
->idx
);
366 return (cmd_choose_session(prefer_unattached
));
369 /* Is this session better? */
371 cmd_session_better(struct session
*s
, struct session
*best
,
372 int prefer_unattached
)
376 if (prefer_unattached
) {
377 if (!(best
->flags
& SESSION_UNATTACHED
) &&
378 (s
->flags
& SESSION_UNATTACHED
))
380 else if ((best
->flags
& SESSION_UNATTACHED
) &&
381 !(s
->flags
& SESSION_UNATTACHED
))
384 return (timercmp(&s
->activity_time
, &best
->activity_time
, >));
388 * Find the most recently used session, preferring unattached if the flag is
392 cmd_choose_session(int prefer_unattached
)
394 struct session
*s
, *best
;
397 RB_FOREACH(s
, sessions
, &sessions
) {
398 if (cmd_session_better(s
, best
, prefer_unattached
))
404 /* Find the most recently used session from a list. */
406 cmd_choose_session_list(struct sessionslist
*ss
)
408 struct session
*s
, *sbest
;
409 struct timeval
*tv
= NULL
;
413 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
414 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
417 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
419 tv
= &s
->activity_time
;
427 * Find the current client. First try the current client if set, then pick the
428 * most recently used of the clients attached to the current session if any,
429 * then of all clients.
432 cmd_current_client(struct cmd_q
*cmdq
)
439 if (cmdq
->client
!= NULL
&& cmdq
->client
->session
!= NULL
)
440 return (cmdq
->client
);
443 * No current client set. Find the current session and return the
444 * newest of its clients.
446 s
= cmd_current_session(cmdq
, 0);
447 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
449 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
450 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
456 c
= cmd_choose_client(&cc
);
462 return (cmd_choose_client(&clients
));
465 /* Choose the most recently used client from a list. */
467 cmd_choose_client(struct clients
*cc
)
469 struct client
*c
, *cbest
;
470 struct timeval
*tv
= NULL
;
474 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
475 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
477 if (c
->session
== NULL
)
480 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
482 tv
= &c
->activity_time
;
489 /* Find the target client or report an error and return NULL. */
491 cmd_find_client(struct cmd_q
*cmdq
, const char *arg
, int quiet
)
497 /* A NULL argument means the current client. */
499 c
= cmd_current_client(cmdq
);
500 if (c
== NULL
&& !quiet
)
501 cmdq_error(cmdq
, "no clients");
504 tmparg
= xstrdup(arg
);
506 /* Trim a single trailing colon if any. */
507 arglen
= strlen(tmparg
);
508 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
509 tmparg
[arglen
- 1] = '\0';
511 /* Find the client, if any. */
512 c
= cmd_lookup_client(tmparg
);
514 /* If no client found, report an error. */
515 if (c
== NULL
&& !quiet
)
516 cmdq_error(cmdq
, "client not found: %s", tmparg
);
523 * Lookup a client by device path. Either of a full match and a match without a
524 * leading _PATH_DEV ("/dev/") is accepted.
527 cmd_lookup_client(const char *name
)
533 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
534 c
= ARRAY_ITEM(&clients
, i
);
535 if (c
== NULL
|| c
->session
== NULL
)
539 /* Check for exact matches. */
540 if (strcmp(name
, path
) == 0)
543 /* Check without leading /dev if present. */
544 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
546 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
553 /* Lookup a session by name. If no session is found, NULL is returned. */
555 cmd_lookup_session(const char *name
, int *ambiguous
)
557 struct session
*s
, *sfound
;
562 * Look for matches. First look for exact matches - session names must
563 * be unique so an exact match can't be ambigious and can just be
566 if ((s
= session_find(name
)) != NULL
)
570 * Otherwise look for partial matches, returning early if it is found to
574 RB_FOREACH(s
, sessions
, &sessions
) {
575 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
576 fnmatch(name
, s
->name
, 0) == 0) {
577 if (sfound
!= NULL
) {
588 * Lookup a window or return -1 if not found or ambigious. First try as an
589 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
590 * idx if the window index is a valid number but there is no window with that
594 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
596 struct winlink
*wl
, *wlfound
;
602 /* Try as a window id. */
603 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
606 /* First see if this is a valid window index in this session. */
607 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
608 if (errstr
== NULL
) {
609 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
613 /* Look for exact matches, error if more than one. */
615 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
616 if (strcmp(name
, wl
->window
->name
) == 0) {
617 if (wlfound
!= NULL
) {
627 /* Now look for pattern matches, again error if multiple. */
629 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
630 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
631 fnmatch(name
, wl
->window
->name
, 0) == 0) {
632 if (wlfound
!= NULL
) {
646 * Find a window index - if the window doesn't exist, check if it is a
647 * potential index and return it anyway.
650 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
656 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
661 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
668 /* Lookup pane id. An initial % means a pane id. */
670 cmd_lookup_paneid(const char *arg
)
678 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
681 return (window_pane_find_by_id(paneid
));
684 /* Lookup window id in a session. An initial @ means a window id. */
686 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
694 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
697 return (winlink_find_by_window_id(&s
->windows
, windowid
));
700 /* Lookup window id. An initial @ means a window id. */
702 cmd_lookup_windowid(const char *arg
)
710 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
713 return (window_find_by_id(windowid
));
716 /* Find session and winlink for window. */
718 cmd_window_session(struct cmd_q
*cmdq
, struct window
*w
, struct winlink
**wlp
)
721 struct sessionslist ss
;
724 /* If this window is in the current session, return that winlink. */
725 s
= cmd_current_session(cmdq
, 0);
727 wl
= winlink_find_by_window(&s
->windows
, w
);
735 /* Otherwise choose from all sessions with this window. */
737 RB_FOREACH(s
, sessions
, &sessions
) {
738 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
741 s
= cmd_choose_session_list(&ss
);
744 *wlp
= winlink_find_by_window(&s
->windows
, w
);
748 /* Find the target session or report an error and return NULL. */
750 cmd_find_session(struct cmd_q
*cmdq
, const char *arg
, int prefer_unattached
)
753 struct window_pane
*wp
;
760 /* A NULL argument means the current session. */
762 return (cmd_current_session(cmdq
, prefer_unattached
));
764 /* Lookup as pane id or window id. */
765 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
)
766 return (cmd_window_session(cmdq
, wp
->window
, NULL
));
767 if ((w
= cmd_lookup_windowid(arg
)) != NULL
)
768 return (cmd_window_session(cmdq
, w
, NULL
));
770 /* Trim a single trailing colon if any. */
771 tmparg
= xstrdup(arg
);
772 arglen
= strlen(tmparg
);
773 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
774 tmparg
[arglen
- 1] = '\0';
776 /* An empty session name is the current session. */
777 if (*tmparg
== '\0') {
779 return (cmd_current_session(cmdq
, prefer_unattached
));
782 /* Find the session, if any. */
783 s
= cmd_lookup_session(tmparg
, &ambiguous
);
785 /* If it doesn't, try to match it as a client. */
786 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
789 /* If no session found, report an error. */
792 cmdq_error(cmdq
, "more than one session: %s", tmparg
);
794 cmdq_error(cmdq
, "session not found: %s", tmparg
);
801 /* Find the target session and window or report an error and return NULL. */
803 cmd_find_window(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
807 struct window_pane
*wp
;
809 char *sessptr
= NULL
;
813 * Find the current session. There must always be a current session, if
814 * it can't be found, report an error.
816 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
817 cmdq_error(cmdq
, "can't establish current session");
821 /* A NULL argument means the current session and window. */
828 /* Lookup as pane id. */
829 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
830 s
= cmd_window_session(cmdq
, wp
->window
, &wl
);
836 /* Time to look at the argument. If it is empty, that is an error. */
840 /* Find the separating colon and split into window and session. */
841 winptr
= strchr(arg
, ':');
844 winptr
++; /* skip : */
845 sessptr
= xstrdup(arg
);
846 *strchr(sessptr
, ':') = '\0';
848 /* Try to lookup the session if present. */
849 if (*sessptr
!= '\0') {
850 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
857 * Then work out the window. An empty string is the current window,
858 * otherwise try special cases then to look it up in the session.
862 else if (winptr
[0] == '!' && winptr
[1] == '\0')
863 wl
= TAILQ_FIRST(&s
->lastw
);
864 else if (winptr
[0] == '^' && winptr
[1] == '\0')
865 wl
= RB_MIN(winlinks
, &s
->windows
);
866 else if (winptr
[0] == '$' && winptr
[1] == '\0')
867 wl
= RB_MAX(winlinks
, &s
->windows
);
868 else if (winptr
[0] == '+' || winptr
[0] == '-')
869 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
871 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
881 * No colon in the string, first try special cases, then as a window
882 * and lastly as a session.
884 if (arg
[0] == '!' && arg
[1] == '\0') {
885 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
887 } else if (arg
[0] == '+' || arg
[0] == '-') {
888 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
890 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
901 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
911 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
913 cmdq_error(cmdq
, "session not found: %s", arg
);
919 cmdq_error(cmdq
, "multiple windows: %s", arg
);
921 cmdq_error(cmdq
, "window not found: %s", arg
);
927 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
932 if (winptr
[1] != '\0')
933 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
935 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
937 if (winptr
[0] == '+')
938 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
940 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
947 * Find the target session and window index, whether or not it exists in the
948 * session. Return -2 on error or -1 if no window index is specified. This is
949 * used when parsing an argument for a window target that may not exist (for
950 * example if it is going to be created).
953 cmd_find_index(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
958 char *sessptr
= NULL
;
959 int idx
, ambiguous
= 0;
962 * Find the current session. There must always be a current session, if
963 * it can't be found, report an error.
965 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
966 cmdq_error(cmdq
, "can't establish current session");
970 /* A NULL argument means the current session and "no window" (-1). */
977 /* Time to look at the argument. If it is empty, that is an error. */
981 /* Find the separating colon. If none, assume the current session. */
982 winptr
= strchr(arg
, ':');
985 winptr
++; /* skip : */
986 sessptr
= xstrdup(arg
);
987 *strchr(sessptr
, ':') = '\0';
989 /* Try to lookup the session if present. */
990 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
991 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
998 * Then work out the window. An empty string is a new window otherwise
999 * try to look it up in the session.
1001 if (*winptr
== '\0')
1003 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
1004 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1007 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
1008 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
1010 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
1018 * No colon in the string, first try special cases, then as a window
1019 * and lastly as a session.
1021 if (arg
[0] == '!' && arg
[1] == '\0') {
1022 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1025 } else if (arg
[0] == '+' || arg
[0] == '-') {
1026 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
1027 goto lookup_session
;
1028 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
1029 goto lookup_session
;
1039 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1049 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
1051 cmdq_error(cmdq
, "session not found: %s", arg
);
1058 cmdq_error(cmdq
, "invalid index: %s", arg
);
1065 cmdq_error(cmdq
, "multiple windows: %s", arg
);
1067 cmdq_error(cmdq
, "window not found: %s", arg
);
1073 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1075 int idx
, offset
= 1;
1077 if (winptr
[1] != '\0')
1078 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1080 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1082 if (winptr
[0] == '+') {
1083 if (s
->curw
->idx
== INT_MAX
)
1084 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1086 idx
= s
->curw
->idx
+ offset
;
1088 if (s
->curw
->idx
== 0)
1089 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1091 idx
= s
->curw
->idx
- offset
;
1099 * Find the target session, window and pane number or report an error and
1100 * return NULL. The pane number is separated from the session:window by a .,
1101 * such as mysession:mywindow.0.
1104 cmd_find_pane(struct cmd_q
*cmdq
,
1105 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1109 const char *period
, *errstr
;
1110 char *winptr
, *paneptr
;
1113 /* Get the current session. */
1114 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
1115 cmdq_error(cmdq
, "can't establish current session");
1121 /* A NULL argument means the current session, window and pane. */
1123 *wpp
= s
->curw
->window
->active
;
1127 /* Lookup as pane id. */
1128 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1129 s
= cmd_window_session(cmdq
, (*wpp
)->window
, &wl
);
1135 /* Look for a separating period. */
1136 if ((period
= strrchr(arg
, '.')) == NULL
)
1139 /* Pull out the window part and parse it. */
1140 winptr
= xstrdup(arg
);
1141 winptr
[period
- arg
] = '\0';
1142 if (*winptr
== '\0')
1144 else if ((wl
= cmd_find_window(cmdq
, winptr
, sp
)) == NULL
)
1147 /* Find the pane section and look it up. */
1148 paneptr
= winptr
+ (period
- arg
) + 1;
1149 if (*paneptr
== '\0')
1150 *wpp
= wl
->window
->active
;
1151 else if (paneptr
[0] == '+' || paneptr
[0] == '-')
1152 *wpp
= cmd_find_pane_offset(paneptr
, wl
);
1154 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1157 *wpp
= window_pane_at_index(wl
->window
, idx
);
1166 /* Try pane string description. */
1167 if ((*wpp
= window_find_string(wl
->window
, paneptr
)) == NULL
) {
1168 cmdq_error(cmdq
, "can't find pane: %s", paneptr
);
1176 /* Try as a pane number alone. */
1177 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1181 /* Try index in the current session and window. */
1182 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1188 /* Try pane string description. */
1189 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
1192 /* Try as a window and use the active pane. */
1193 if ((wl
= cmd_find_window(cmdq
, arg
, sp
)) != NULL
)
1194 *wpp
= wl
->window
->active
;
1202 struct window_pane
*
1203 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1205 struct window
*w
= wl
->window
;
1206 struct window_pane
*wp
= w
->active
;
1209 if (paneptr
[1] != '\0')
1210 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1212 if (paneptr
[0] == '+')
1213 wp
= window_pane_next_by_number(w
, wp
, offset
);
1215 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1221 /* Replace the first %% or %idx in template by s. */
1223 cmd_template_replace(const char *template, const char *s
, int idx
)
1230 if (strchr(template, '%') == NULL
)
1231 return (xstrdup(template));
1239 while (*ptr
!= '\0') {
1240 switch (ch
= *ptr
++) {
1242 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1243 if (*ptr
!= '%' || replaced
)
1250 buf
= xrealloc(buf
, 1, len
+ 1);
1251 strlcat(buf
, s
, len
+ 1);
1254 buf
= xrealloc(buf
, 1, len
+ 2);
1263 * Return the default path for a new pane, using the given path or the
1264 * default-path option if it is NULL. Several special values are accepted: the
1265 * empty string or relative path for the current pane's working directory, ~
1266 * for the user's home, - for the session working directory, . for the tmux
1267 * server's working directory. The default on failure is the session's working
1271 cmd_get_default_path(struct cmd_q
*cmdq
, const char *cwd
)
1273 struct client
*c
= cmdq
->client
;
1275 struct environ_entry
*envent
;
1277 char tmp
[MAXPATHLEN
];
1281 static char path
[MAXPATHLEN
];
1283 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
)
1287 cwd
= options_get_string(&s
->options
, "default-path");
1290 if (strcmp(cwd
, "$HOME") == 0 || strncmp(cwd
, "$HOME/", 6) == 0) {
1291 /* User's home directory - $HOME. */
1294 } else if (cwd
[0] == '~' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1295 /* User's home directory - ~. */
1297 } else if (cwd
[0] == '-' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1298 /* Session working directory. */
1301 } else if (cwd
[0] == '.' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1302 /* Server working directory. */
1303 if (getcwd(tmp
, sizeof tmp
) != NULL
) {
1308 } else if (*cwd
== '/') {
1309 /* Absolute path. */
1312 /* Empty or relative path. */
1313 if (c
!= NULL
&& c
->session
== NULL
&& c
->cwd
!= NULL
)
1315 else if (s
->curw
!= NULL
)
1316 root
= get_proc_cwd(s
->curw
->window
->active
->fd
);
1327 envent
= environ_find(&global_environ
, "HOME");
1328 if (envent
!= NULL
&& *envent
->value
!= '\0')
1329 root
= envent
->value
;
1330 else if ((pw
= getpwuid(getuid())) != NULL
)
1336 if (root
[skip
] == '\0') {
1337 strlcpy(path
, root
, sizeof path
);
1340 n
= snprintf(path
, sizeof path
, "%s/%s", root
, cwd
+ skip
);
1341 if (n
> 0 && (size_t)n
< sizeof path
)