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 session
*cmd_lookup_session_id(const char *);
127 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
128 int cmd_lookup_index(struct session
*, const char *, int *);
129 struct window_pane
*cmd_lookup_paneid(const char *);
130 struct winlink
*cmd_lookup_winlink_windowid(struct session
*, const char *);
131 struct window
*cmd_lookup_windowid(const char *);
132 struct session
*cmd_window_session(struct cmd_q
*, struct window
*,
134 struct winlink
*cmd_find_window_offset(const char *, struct session
*, int *);
135 int cmd_find_index_offset(const char *, struct session
*, int *);
136 struct window_pane
*cmd_find_pane_offset(const char *, struct winlink
*);
139 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
145 for (i
= 0; i
< argc
; i
++) {
146 if (strlcpy(buf
, argv
[i
], len
) >= len
)
148 arglen
= strlen(argv
[i
]) + 1;
157 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
164 *argv
= xcalloc(argc
, sizeof **argv
);
167 for (i
= 0; i
< argc
; i
++) {
169 cmd_free_argv(argc
, *argv
);
173 arglen
= strlen(buf
) + 1;
174 (*argv
)[i
] = xstrdup(buf
);
183 cmd_copy_argv(int argc
, char *const *argv
)
190 new_argv
= xcalloc(argc
, sizeof *new_argv
);
191 for (i
= 0; i
< argc
; i
++) {
193 new_argv
[i
] = xstrdup(argv
[i
]);
199 cmd_free_argv(int argc
, char **argv
)
205 for (i
= 0; i
< argc
; i
++)
211 cmd_parse(int argc
, char **argv
, const char *file
, u_int line
, char **cause
)
213 const struct cmd_entry
**entryp
, *entry
;
221 xasprintf(cause
, "no command");
226 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
227 if ((*entryp
)->alias
!= NULL
&&
228 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
234 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
240 /* Bail now if an exact match. */
241 if (strcmp(entry
->name
, argv
[0]) == 0)
247 xasprintf(cause
, "unknown command: %s", argv
[0]);
251 args
= args_parse(entry
->args_template
, argc
, argv
);
254 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
256 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
258 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
261 cmd
= xcalloc(1, sizeof *cmd
);
266 cmd
->file
= xstrdup(file
);
273 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
274 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
276 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
278 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
281 s
[strlen(s
) - 2] = '\0';
282 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
288 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
293 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
297 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
299 used
= args_print(cmd
->args
, buf
+ off
, len
- off
- 1);
310 * Figure out the current session. Use: 1) the current session, if the command
311 * context has one; 2) the most recently used session containing the pty of the
312 * calling client, if any; 3) the session specified in the TMUX variable from
313 * the environment (as passed from the client); 4) the most recently used
314 * session from all sessions.
317 cmd_current_session(struct cmd_q
*cmdq
, int prefer_unattached
)
319 struct msg_command_data
*data
= cmdq
->msgdata
;
320 struct client
*c
= cmdq
->client
;
322 struct sessionslist ss
;
324 struct window_pane
*wp
;
328 if (c
!= NULL
&& c
->session
!= NULL
)
332 * If the name of the calling client's pty is known, build a list of
333 * the sessions that contain it and if any choose either the first or
336 path
= c
== NULL
? NULL
: c
->tty
.path
;
339 RB_FOREACH(s
, sessions
, &sessions
) {
341 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
342 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
343 if (strcmp(wp
->tty
, path
) == 0) {
355 s
= cmd_choose_session_list(&ss
);
361 /* Use the session from the TMUX environment variable. */
362 if (data
!= NULL
&& data
->pid
== getpid() && data
->session_id
!= -1) {
363 s
= session_find_by_id(data
->session_id
);
368 return (cmd_choose_session(prefer_unattached
));
371 /* Is this session better? */
373 cmd_session_better(struct session
*s
, struct session
*best
,
374 int prefer_unattached
)
378 if (prefer_unattached
) {
379 if (!(best
->flags
& SESSION_UNATTACHED
) &&
380 (s
->flags
& SESSION_UNATTACHED
))
382 else if ((best
->flags
& SESSION_UNATTACHED
) &&
383 !(s
->flags
& SESSION_UNATTACHED
))
386 return (timercmp(&s
->activity_time
, &best
->activity_time
, >));
390 * Find the most recently used session, preferring unattached if the flag is
394 cmd_choose_session(int prefer_unattached
)
396 struct session
*s
, *best
;
399 RB_FOREACH(s
, sessions
, &sessions
) {
400 if (cmd_session_better(s
, best
, prefer_unattached
))
406 /* Find the most recently used session from a list. */
408 cmd_choose_session_list(struct sessionslist
*ss
)
410 struct session
*s
, *sbest
;
411 struct timeval
*tv
= NULL
;
415 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
416 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
419 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
421 tv
= &s
->activity_time
;
429 * Find the current client. First try the current client if set, then pick the
430 * most recently used of the clients attached to the current session if any,
431 * then of all clients.
434 cmd_current_client(struct cmd_q
*cmdq
)
441 if (cmdq
->client
!= NULL
&& cmdq
->client
->session
!= NULL
)
442 return (cmdq
->client
);
445 * No current client set. Find the current session and return the
446 * newest of its clients.
448 s
= cmd_current_session(cmdq
, 0);
449 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
451 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
452 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
458 c
= cmd_choose_client(&cc
);
464 return (cmd_choose_client(&clients
));
467 /* Choose the most recently used client from a list. */
469 cmd_choose_client(struct clients
*cc
)
471 struct client
*c
, *cbest
;
472 struct timeval
*tv
= NULL
;
476 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
477 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
479 if (c
->session
== NULL
)
482 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
484 tv
= &c
->activity_time
;
491 /* Find the target client or report an error and return NULL. */
493 cmd_find_client(struct cmd_q
*cmdq
, const char *arg
, int quiet
)
499 /* A NULL argument means the current client. */
501 c
= cmd_current_client(cmdq
);
502 if (c
== NULL
&& !quiet
)
503 cmdq_error(cmdq
, "no clients");
506 tmparg
= xstrdup(arg
);
508 /* Trim a single trailing colon if any. */
509 arglen
= strlen(tmparg
);
510 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
511 tmparg
[arglen
- 1] = '\0';
513 /* Find the client, if any. */
514 c
= cmd_lookup_client(tmparg
);
516 /* If no client found, report an error. */
517 if (c
== NULL
&& !quiet
)
518 cmdq_error(cmdq
, "client not found: %s", tmparg
);
525 * Lookup a client by device path. Either of a full match and a match without a
526 * leading _PATH_DEV ("/dev/") is accepted.
529 cmd_lookup_client(const char *name
)
535 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
536 c
= ARRAY_ITEM(&clients
, i
);
537 if (c
== NULL
|| c
->session
== NULL
|| c
->tty
.path
== NULL
)
541 /* Check for exact matches. */
542 if (strcmp(name
, path
) == 0)
545 /* Check without leading /dev if present. */
546 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
548 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
555 /* Find the target session or report an error and return NULL. */
557 cmd_lookup_session_id(const char *arg
)
564 id
= strtol(arg
+ 1, &endptr
, 10);
565 if (arg
[1] != '\0' && *endptr
== '\0')
566 return (session_find_by_id(id
));
570 /* Lookup a session by name. If no session is found, NULL is returned. */
572 cmd_lookup_session(const char *name
, int *ambiguous
)
574 struct session
*s
, *sfound
;
578 /* Look for $id first. */
579 if ((s
= cmd_lookup_session_id(name
)) != NULL
)
583 * Look for matches. First look for exact matches - session names must
584 * be unique so an exact match can't be ambigious and can just be
587 if ((s
= session_find(name
)) != NULL
)
591 * Otherwise look for partial matches, returning early if it is found to
595 RB_FOREACH(s
, sessions
, &sessions
) {
596 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
597 fnmatch(name
, s
->name
, 0) == 0) {
598 if (sfound
!= NULL
) {
609 * Lookup a window or return -1 if not found or ambigious. First try as an
610 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
611 * idx if the window index is a valid number but there is no window with that
615 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
617 struct winlink
*wl
, *wlfound
;
623 /* Try as a window id. */
624 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
627 /* First see if this is a valid window index in this session. */
628 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
629 if (errstr
== NULL
) {
630 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
634 /* Look for exact matches, error if more than one. */
636 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
637 if (strcmp(name
, wl
->window
->name
) == 0) {
638 if (wlfound
!= NULL
) {
648 /* Now look for pattern matches, again error if multiple. */
650 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
651 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
652 fnmatch(name
, wl
->window
->name
, 0) == 0) {
653 if (wlfound
!= NULL
) {
667 * Find a window index - if the window doesn't exist, check if it is a
668 * potential index and return it anyway.
671 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
677 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
682 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
689 /* Lookup pane id. An initial % means a pane id. */
691 cmd_lookup_paneid(const char *arg
)
699 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
702 return (window_pane_find_by_id(paneid
));
705 /* Lookup window id in a session. An initial @ means a window id. */
707 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
715 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
718 return (winlink_find_by_window_id(&s
->windows
, windowid
));
721 /* Lookup window id. An initial @ means a window id. */
723 cmd_lookup_windowid(const char *arg
)
731 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
734 return (window_find_by_id(windowid
));
737 /* Find session and winlink for window. */
739 cmd_window_session(struct cmd_q
*cmdq
, struct window
*w
, struct winlink
**wlp
)
742 struct sessionslist ss
;
745 /* If this window is in the current session, return that winlink. */
746 s
= cmd_current_session(cmdq
, 0);
748 wl
= winlink_find_by_window(&s
->windows
, w
);
756 /* Otherwise choose from all sessions with this window. */
758 RB_FOREACH(s
, sessions
, &sessions
) {
759 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
762 s
= cmd_choose_session_list(&ss
);
765 *wlp
= winlink_find_by_window(&s
->windows
, w
);
769 /* Find the target session or report an error and return NULL. */
771 cmd_find_session(struct cmd_q
*cmdq
, const char *arg
, int prefer_unattached
)
774 struct window_pane
*wp
;
781 /* A NULL argument means the current session. */
783 return (cmd_current_session(cmdq
, prefer_unattached
));
785 /* Lookup as pane id or window id. */
786 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
)
787 return (cmd_window_session(cmdq
, wp
->window
, NULL
));
788 if ((w
= cmd_lookup_windowid(arg
)) != NULL
)
789 return (cmd_window_session(cmdq
, w
, NULL
));
791 /* Trim a single trailing colon if any. */
792 tmparg
= xstrdup(arg
);
793 arglen
= strlen(tmparg
);
794 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
795 tmparg
[arglen
- 1] = '\0';
797 /* An empty session name is the current session. */
798 if (*tmparg
== '\0') {
800 return (cmd_current_session(cmdq
, prefer_unattached
));
803 /* Find the session, if any. */
804 s
= cmd_lookup_session(tmparg
, &ambiguous
);
806 /* If it doesn't, try to match it as a client. */
807 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
810 /* If no session found, report an error. */
813 cmdq_error(cmdq
, "more than one session: %s", tmparg
);
815 cmdq_error(cmdq
, "session not found: %s", tmparg
);
822 /* Find the target session and window or report an error and return NULL. */
824 cmd_find_window(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
828 struct window_pane
*wp
;
830 char *sessptr
= NULL
;
834 * Find the current session. There must always be a current session, if
835 * it can't be found, report an error.
837 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
838 cmdq_error(cmdq
, "can't establish current session");
842 /* A NULL argument means the current session and window. */
849 /* Lookup as pane id. */
850 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
851 s
= cmd_window_session(cmdq
, wp
->window
, &wl
);
857 /* Time to look at the argument. If it is empty, that is an error. */
861 /* Find the separating colon and split into window and session. */
862 winptr
= strchr(arg
, ':');
865 winptr
++; /* skip : */
866 sessptr
= xstrdup(arg
);
867 *strchr(sessptr
, ':') = '\0';
869 /* Try to lookup the session if present. */
870 if (*sessptr
!= '\0') {
871 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
878 * Then work out the window. An empty string is the current window,
879 * otherwise try special cases then to look it up in the session.
883 else if (winptr
[0] == '!' && winptr
[1] == '\0')
884 wl
= TAILQ_FIRST(&s
->lastw
);
885 else if (winptr
[0] == '^' && winptr
[1] == '\0')
886 wl
= RB_MIN(winlinks
, &s
->windows
);
887 else if (winptr
[0] == '$' && winptr
[1] == '\0')
888 wl
= RB_MAX(winlinks
, &s
->windows
);
889 else if (winptr
[0] == '+' || winptr
[0] == '-')
890 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
892 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
902 * No colon in the string, first try special cases, then as a window
903 * and lastly as a session.
905 if (arg
[0] == '!' && arg
[1] == '\0') {
906 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
908 } else if (arg
[0] == '+' || arg
[0] == '-') {
909 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
911 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
922 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
932 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
934 cmdq_error(cmdq
, "session not found: %s", arg
);
940 cmdq_error(cmdq
, "multiple windows: %s", arg
);
942 cmdq_error(cmdq
, "window not found: %s", arg
);
948 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
953 if (winptr
[1] != '\0')
954 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
956 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
958 if (winptr
[0] == '+')
959 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
961 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
968 * Find the target session and window index, whether or not it exists in the
969 * session. Return -2 on error or -1 if no window index is specified. This is
970 * used when parsing an argument for a window target that may not exist (for
971 * example if it is going to be created).
974 cmd_find_index(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
979 char *sessptr
= NULL
;
980 int idx
, ambiguous
= 0;
983 * Find the current session. There must always be a current session, if
984 * it can't be found, report an error.
986 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
987 cmdq_error(cmdq
, "can't establish current session");
991 /* A NULL argument means the current session and "no window" (-1). */
998 /* Time to look at the argument. If it is empty, that is an error. */
1002 /* Find the separating colon. If none, assume the current session. */
1003 winptr
= strchr(arg
, ':');
1006 winptr
++; /* skip : */
1007 sessptr
= xstrdup(arg
);
1008 *strchr(sessptr
, ':') = '\0';
1010 /* Try to lookup the session if present. */
1011 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
1012 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
1019 * Then work out the window. An empty string is a new window otherwise
1020 * try to look it up in the session.
1022 if (*winptr
== '\0')
1024 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
1025 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1028 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
1029 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
1031 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
1039 * No colon in the string, first try special cases, then as a window
1040 * and lastly as a session.
1042 if (arg
[0] == '!' && arg
[1] == '\0') {
1043 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1046 } else if (arg
[0] == '+' || arg
[0] == '-') {
1047 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
1048 goto lookup_session
;
1049 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
1050 goto lookup_session
;
1060 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1070 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
1072 cmdq_error(cmdq
, "session not found: %s", arg
);
1079 cmdq_error(cmdq
, "invalid index: %s", arg
);
1086 cmdq_error(cmdq
, "multiple windows: %s", arg
);
1088 cmdq_error(cmdq
, "window not found: %s", arg
);
1094 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1096 int idx
, offset
= 1;
1098 if (winptr
[1] != '\0')
1099 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1101 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1103 if (winptr
[0] == '+') {
1104 if (s
->curw
->idx
== INT_MAX
)
1105 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1107 idx
= s
->curw
->idx
+ offset
;
1109 if (s
->curw
->idx
== 0)
1110 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1112 idx
= s
->curw
->idx
- offset
;
1120 * Find the target session, window and pane number or report an error and
1121 * return NULL. The pane number is separated from the session:window by a .,
1122 * such as mysession:mywindow.0.
1125 cmd_find_pane(struct cmd_q
*cmdq
,
1126 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1130 const char *period
, *errstr
;
1131 char *winptr
, *paneptr
;
1134 /* Get the current session. */
1135 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
1136 cmdq_error(cmdq
, "can't establish current session");
1142 /* A NULL argument means the current session, window and pane. */
1144 *wpp
= s
->curw
->window
->active
;
1148 /* Lookup as pane id. */
1149 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1150 s
= cmd_window_session(cmdq
, (*wpp
)->window
, &wl
);
1156 /* Look for a separating period. */
1157 if ((period
= strrchr(arg
, '.')) == NULL
)
1160 /* Pull out the window part and parse it. */
1161 winptr
= xstrdup(arg
);
1162 winptr
[period
- arg
] = '\0';
1163 if (*winptr
== '\0')
1165 else if ((wl
= cmd_find_window(cmdq
, winptr
, sp
)) == NULL
)
1168 /* Find the pane section and look it up. */
1169 paneptr
= winptr
+ (period
- arg
) + 1;
1170 if (*paneptr
== '\0')
1171 *wpp
= wl
->window
->active
;
1172 else if (paneptr
[0] == '+' || paneptr
[0] == '-')
1173 *wpp
= cmd_find_pane_offset(paneptr
, wl
);
1175 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1178 *wpp
= window_pane_at_index(wl
->window
, idx
);
1187 /* Try pane string description. */
1188 if ((*wpp
= window_find_string(wl
->window
, paneptr
)) == NULL
) {
1189 cmdq_error(cmdq
, "can't find pane: %s", paneptr
);
1197 /* Try as a pane number alone. */
1198 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1202 /* Try index in the current session and window. */
1203 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1209 /* Try pane string description. */
1210 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
1213 /* Try as a window and use the active pane. */
1214 if ((wl
= cmd_find_window(cmdq
, arg
, sp
)) != NULL
)
1215 *wpp
= wl
->window
->active
;
1223 struct window_pane
*
1224 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1226 struct window
*w
= wl
->window
;
1227 struct window_pane
*wp
= w
->active
;
1230 if (paneptr
[1] != '\0')
1231 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1233 if (paneptr
[0] == '+')
1234 wp
= window_pane_next_by_number(w
, wp
, offset
);
1236 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1242 /* Replace the first %% or %idx in template by s. */
1244 cmd_template_replace(const char *template, const char *s
, int idx
)
1251 if (strchr(template, '%') == NULL
)
1252 return (xstrdup(template));
1260 while (*ptr
!= '\0') {
1261 switch (ch
= *ptr
++) {
1263 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1264 if (*ptr
!= '%' || replaced
)
1271 buf
= xrealloc(buf
, 1, len
+ 1);
1272 strlcat(buf
, s
, len
+ 1);
1275 buf
= xrealloc(buf
, 1, len
+ 2);
1284 * Return the default path for a new pane, using the given path or the
1285 * default-path option if it is NULL. Several special values are accepted: the
1286 * empty string or relative path for the current pane's working directory, ~
1287 * for the user's home, - for the session working directory, . for the tmux
1288 * server's working directory. The default on failure is the session's working
1292 cmd_get_default_path(struct cmd_q
*cmdq
, const char *cwd
)
1294 struct client
*c
= cmdq
->client
;
1296 struct environ_entry
*envent
;
1298 char tmp
[MAXPATHLEN
];
1302 static char path
[MAXPATHLEN
];
1304 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
)
1308 cwd
= options_get_string(&s
->options
, "default-path");
1311 if (strcmp(cwd
, "$HOME") == 0 || strncmp(cwd
, "$HOME/", 6) == 0) {
1312 /* User's home directory - $HOME. */
1315 } else if (cwd
[0] == '~' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1316 /* User's home directory - ~. */
1318 } else if (cwd
[0] == '-' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1319 /* Session working directory. */
1322 } else if (cwd
[0] == '.' && (cwd
[1] == '\0' || cwd
[1] == '/')) {
1323 /* Server working directory. */
1324 if (getcwd(tmp
, sizeof tmp
) != NULL
) {
1329 } else if (*cwd
== '/') {
1330 /* Absolute path. */
1333 /* Empty or relative path. */
1334 if (c
!= NULL
&& c
->session
== NULL
&& c
->cwd
!= NULL
)
1336 else if (s
->curw
!= NULL
)
1337 root
= get_proc_cwd(s
->curw
->window
->active
->fd
);
1348 envent
= environ_find(&global_environ
, "HOME");
1349 if (envent
!= NULL
&& *envent
->value
!= '\0')
1350 root
= envent
->value
;
1351 else if ((pw
= getpwuid(getuid())) != NULL
)
1357 if (root
[skip
] == '\0') {
1358 strlcpy(path
, root
, sizeof path
);
1361 n
= snprintf(path
, sizeof path
, "%s/%s", root
, cwd
+ skip
);
1362 if (n
> 0 && (size_t)n
< sizeof path
)