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 winlink
*cmd_lookup_winlink_windowid(struct session
*, 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
)
257 cmd
= xcalloc(1, sizeof *cmd
);
262 cmd
->file
= xstrdup(file
);
269 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
270 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
272 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
274 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
277 s
[strlen(s
) - 2] = '\0';
278 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
284 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
289 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
293 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
295 used
= args_print(cmd
->args
, buf
+ off
, len
- off
- 1);
306 * Figure out the current session. Use: 1) the current session, if the command
307 * context has one; 2) the most recently used session containing the pty of the
308 * calling client, if any; 3) the session specified in the TMUX variable from
309 * the environment (as passed from the client); 4) the most recently used
310 * session from all sessions.
313 cmd_current_session(struct cmd_q
*cmdq
, int prefer_unattached
)
315 struct client
*c
= cmdq
->client
;
317 struct sessionslist ss
;
319 struct window_pane
*wp
;
323 if (c
!= NULL
&& c
->session
!= NULL
)
327 * If the name of the calling client's pty is known, build a list of
328 * the sessions that contain it and if any choose either the first or
331 path
= c
== NULL
? NULL
: c
->tty
.path
;
334 RB_FOREACH(s
, sessions
, &sessions
) {
336 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
337 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
338 if (strcmp(wp
->tty
, path
) == 0) {
350 s
= cmd_choose_session_list(&ss
);
356 return (cmd_choose_session(prefer_unattached
));
359 /* Is this session better? */
361 cmd_session_better(struct session
*s
, struct session
*best
,
362 int prefer_unattached
)
366 if (prefer_unattached
) {
367 if (!(best
->flags
& SESSION_UNATTACHED
) &&
368 (s
->flags
& SESSION_UNATTACHED
))
370 else if ((best
->flags
& SESSION_UNATTACHED
) &&
371 !(s
->flags
& SESSION_UNATTACHED
))
374 return (timercmp(&s
->activity_time
, &best
->activity_time
, >));
378 * Find the most recently used session, preferring unattached if the flag is
382 cmd_choose_session(int prefer_unattached
)
384 struct session
*s
, *best
;
387 RB_FOREACH(s
, sessions
, &sessions
) {
388 if (cmd_session_better(s
, best
, prefer_unattached
))
394 /* Find the most recently used session from a list. */
396 cmd_choose_session_list(struct sessionslist
*ss
)
398 struct session
*s
, *sbest
;
399 struct timeval
*tv
= NULL
;
403 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
404 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
407 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
409 tv
= &s
->activity_time
;
417 * Find the current client. First try the current client if set, then pick the
418 * most recently used of the clients attached to the current session if any,
419 * then of all clients.
422 cmd_current_client(struct cmd_q
*cmdq
)
429 if (cmdq
->client
!= NULL
&& cmdq
->client
->session
!= NULL
)
430 return (cmdq
->client
);
433 * No current client set. Find the current session and return the
434 * newest of its clients.
436 s
= cmd_current_session(cmdq
, 0);
437 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
439 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
440 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
446 c
= cmd_choose_client(&cc
);
452 return (cmd_choose_client(&clients
));
455 /* Choose the most recently used client from a list. */
457 cmd_choose_client(struct clients
*cc
)
459 struct client
*c
, *cbest
;
460 struct timeval
*tv
= NULL
;
464 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
465 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
467 if (c
->session
== NULL
)
470 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
472 tv
= &c
->activity_time
;
479 /* Find the target client or report an error and return NULL. */
481 cmd_find_client(struct cmd_q
*cmdq
, const char *arg
, int quiet
)
487 /* A NULL argument means the current client. */
489 c
= cmd_current_client(cmdq
);
490 if (c
== NULL
&& !quiet
)
491 cmdq_error(cmdq
, "no clients");
494 tmparg
= xstrdup(arg
);
496 /* Trim a single trailing colon if any. */
497 arglen
= strlen(tmparg
);
498 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
499 tmparg
[arglen
- 1] = '\0';
501 /* Find the client, if any. */
502 c
= cmd_lookup_client(tmparg
);
504 /* If no client found, report an error. */
505 if (c
== NULL
&& !quiet
)
506 cmdq_error(cmdq
, "client not found: %s", tmparg
);
513 * Lookup a client by device path. Either of a full match and a match without a
514 * leading _PATH_DEV ("/dev/") is accepted.
517 cmd_lookup_client(const char *name
)
523 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
524 c
= ARRAY_ITEM(&clients
, i
);
525 if (c
== NULL
|| c
->session
== NULL
|| c
->tty
.path
== NULL
)
529 /* Check for exact matches. */
530 if (strcmp(name
, path
) == 0)
533 /* Check without leading /dev if present. */
534 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
536 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
543 /* Find the target session or report an error and return NULL. */
545 cmd_lookup_session_id(const char *arg
)
552 id
= strtol(arg
+ 1, &endptr
, 10);
553 if (arg
[1] != '\0' && *endptr
== '\0')
554 return (session_find_by_id(id
));
558 /* Lookup a session by name. If no session is found, NULL is returned. */
560 cmd_lookup_session(const char *name
, int *ambiguous
)
562 struct session
*s
, *sfound
;
566 /* Look for $id first. */
567 if ((s
= cmd_lookup_session_id(name
)) != NULL
)
571 * Look for matches. First look for exact matches - session names must
572 * be unique so an exact match can't be ambigious and can just be
575 if ((s
= session_find(name
)) != NULL
)
579 * Otherwise look for partial matches, returning early if it is found to
583 RB_FOREACH(s
, sessions
, &sessions
) {
584 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
585 fnmatch(name
, s
->name
, 0) == 0) {
586 if (sfound
!= NULL
) {
597 * Lookup a window or return -1 if not found or ambigious. First try as an
598 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
599 * idx if the window index is a valid number but there is no window with that
603 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
605 struct winlink
*wl
, *wlfound
;
611 /* Try as a window id. */
612 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
615 /* First see if this is a valid window index in this session. */
616 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
617 if (errstr
== NULL
) {
618 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
622 /* Look for exact matches, error if more than one. */
624 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
625 if (strcmp(name
, wl
->window
->name
) == 0) {
626 if (wlfound
!= NULL
) {
636 /* Now look for pattern matches, again error if multiple. */
638 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
639 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
640 fnmatch(name
, wl
->window
->name
, 0) == 0) {
641 if (wlfound
!= NULL
) {
655 * Find a window index - if the window doesn't exist, check if it is a
656 * potential index and return it anyway.
659 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
665 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
670 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
677 /* Lookup pane id. An initial % means a pane id. */
679 cmd_lookup_paneid(const char *arg
)
687 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
690 return (window_pane_find_by_id(paneid
));
693 /* Lookup window id in a session. An initial @ means a window id. */
695 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
703 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
706 return (winlink_find_by_window_id(&s
->windows
, windowid
));
709 /* Lookup window id. An initial @ means a window id. */
711 cmd_lookup_windowid(const char *arg
)
719 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
722 return (window_find_by_id(windowid
));
725 /* Find session and winlink for window. */
727 cmd_window_session(struct cmd_q
*cmdq
, struct window
*w
, struct winlink
**wlp
)
730 struct sessionslist ss
;
733 /* If this window is in the current session, return that winlink. */
734 s
= cmd_current_session(cmdq
, 0);
736 wl
= winlink_find_by_window(&s
->windows
, w
);
744 /* Otherwise choose from all sessions with this window. */
746 RB_FOREACH(s
, sessions
, &sessions
) {
747 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
750 s
= cmd_choose_session_list(&ss
);
753 *wlp
= winlink_find_by_window(&s
->windows
, w
);
757 /* Find the target session or report an error and return NULL. */
759 cmd_find_session(struct cmd_q
*cmdq
, const char *arg
, int prefer_unattached
)
762 struct window_pane
*wp
;
769 /* A NULL argument means the current session. */
771 return (cmd_current_session(cmdq
, prefer_unattached
));
773 /* Lookup as pane id or window id. */
774 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
)
775 return (cmd_window_session(cmdq
, wp
->window
, NULL
));
776 if ((w
= cmd_lookup_windowid(arg
)) != NULL
)
777 return (cmd_window_session(cmdq
, w
, NULL
));
779 /* Trim a single trailing colon if any. */
780 tmparg
= xstrdup(arg
);
781 arglen
= strlen(tmparg
);
782 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
783 tmparg
[arglen
- 1] = '\0';
785 /* An empty session name is the current session. */
786 if (*tmparg
== '\0') {
788 return (cmd_current_session(cmdq
, prefer_unattached
));
791 /* Find the session, if any. */
792 s
= cmd_lookup_session(tmparg
, &ambiguous
);
794 /* If it doesn't, try to match it as a client. */
795 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
798 /* If no session found, report an error. */
801 cmdq_error(cmdq
, "more than one session: %s", tmparg
);
803 cmdq_error(cmdq
, "session not found: %s", tmparg
);
810 /* Find the target session and window or report an error and return NULL. */
812 cmd_find_window(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
816 struct window_pane
*wp
;
818 char *sessptr
= NULL
;
822 * Find the current session. There must always be a current session, if
823 * it can't be found, report an error.
825 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
826 cmdq_error(cmdq
, "can't establish current session");
830 /* A NULL argument means the current session and window. */
837 /* Lookup as pane id. */
838 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
839 s
= cmd_window_session(cmdq
, wp
->window
, &wl
);
845 /* Time to look at the argument. If it is empty, that is an error. */
849 /* Find the separating colon and split into window and session. */
850 winptr
= strchr(arg
, ':');
853 winptr
++; /* skip : */
854 sessptr
= xstrdup(arg
);
855 *strchr(sessptr
, ':') = '\0';
857 /* Try to lookup the session if present. */
858 if (*sessptr
!= '\0') {
859 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
866 * Then work out the window. An empty string is the current window,
867 * otherwise try special cases then to look it up in the session.
871 else if (winptr
[0] == '!' && winptr
[1] == '\0')
872 wl
= TAILQ_FIRST(&s
->lastw
);
873 else if (winptr
[0] == '^' && winptr
[1] == '\0')
874 wl
= RB_MIN(winlinks
, &s
->windows
);
875 else if (winptr
[0] == '$' && winptr
[1] == '\0')
876 wl
= RB_MAX(winlinks
, &s
->windows
);
877 else if (winptr
[0] == '+' || winptr
[0] == '-')
878 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
880 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
890 * No colon in the string, first try special cases, then as a window
891 * and lastly as a session.
893 if (arg
[0] == '!' && arg
[1] == '\0') {
894 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
896 } else if (arg
[0] == '+' || arg
[0] == '-') {
897 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
899 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
910 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
920 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
922 cmdq_error(cmdq
, "session not found: %s", arg
);
928 cmdq_error(cmdq
, "multiple windows: %s", arg
);
930 cmdq_error(cmdq
, "window not found: %s", arg
);
936 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
941 if (winptr
[1] != '\0')
942 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
944 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
946 if (winptr
[0] == '+')
947 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
949 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
956 * Find the target session and window index, whether or not it exists in the
957 * session. Return -2 on error or -1 if no window index is specified. This is
958 * used when parsing an argument for a window target that may not exist (for
959 * example if it is going to be created).
962 cmd_find_index(struct cmd_q
*cmdq
, const char *arg
, struct session
**sp
)
967 char *sessptr
= NULL
;
968 int idx
, ambiguous
= 0;
971 * Find the current session. There must always be a current session, if
972 * it can't be found, report an error.
974 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
975 cmdq_error(cmdq
, "can't establish current session");
979 /* A NULL argument means the current session and "no window" (-1). */
986 /* Time to look at the argument. If it is empty, that is an error. */
990 /* Find the separating colon. If none, assume the current session. */
991 winptr
= strchr(arg
, ':');
994 winptr
++; /* skip : */
995 sessptr
= xstrdup(arg
);
996 *strchr(sessptr
, ':') = '\0';
998 /* Try to lookup the session if present. */
999 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
1000 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
1007 * Then work out the window. An empty string is a new window otherwise
1008 * try to look it up in the session.
1010 if (*winptr
== '\0')
1012 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
1013 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1016 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
1017 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
1019 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
1027 * No colon in the string, first try special cases, then as a window
1028 * and lastly as a session.
1030 if (arg
[0] == '!' && arg
[1] == '\0') {
1031 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1034 } else if (arg
[0] == '+' || arg
[0] == '-') {
1035 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
1036 goto lookup_session
;
1037 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
1038 goto lookup_session
;
1048 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1058 cmdq_error(cmdq
, "multiple sessions: %s", arg
);
1060 cmdq_error(cmdq
, "session not found: %s", arg
);
1067 cmdq_error(cmdq
, "invalid index: %s", arg
);
1074 cmdq_error(cmdq
, "multiple windows: %s", arg
);
1076 cmdq_error(cmdq
, "window not found: %s", arg
);
1082 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1084 int idx
, offset
= 1;
1086 if (winptr
[1] != '\0')
1087 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1089 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1091 if (winptr
[0] == '+') {
1092 if (s
->curw
->idx
== INT_MAX
)
1093 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1095 idx
= s
->curw
->idx
+ offset
;
1097 if (s
->curw
->idx
== 0)
1098 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1100 idx
= s
->curw
->idx
- offset
;
1108 * Find the target session, window and pane number or report an error and
1109 * return NULL. The pane number is separated from the session:window by a .,
1110 * such as mysession:mywindow.0.
1113 cmd_find_pane(struct cmd_q
*cmdq
,
1114 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1118 const char *period
, *errstr
;
1119 char *winptr
, *paneptr
;
1122 /* Get the current session. */
1123 if ((s
= cmd_current_session(cmdq
, 0)) == NULL
) {
1124 cmdq_error(cmdq
, "can't establish current session");
1130 /* A NULL argument means the current session, window and pane. */
1132 *wpp
= s
->curw
->window
->active
;
1136 /* Lookup as pane id. */
1137 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1138 s
= cmd_window_session(cmdq
, (*wpp
)->window
, &wl
);
1144 /* Look for a separating period. */
1145 if ((period
= strrchr(arg
, '.')) == NULL
)
1148 /* Pull out the window part and parse it. */
1149 winptr
= xstrdup(arg
);
1150 winptr
[period
- arg
] = '\0';
1151 if (*winptr
== '\0')
1153 else if ((wl
= cmd_find_window(cmdq
, winptr
, sp
)) == NULL
)
1156 /* Find the pane section and look it up. */
1157 paneptr
= winptr
+ (period
- arg
) + 1;
1158 if (*paneptr
== '\0')
1159 *wpp
= wl
->window
->active
;
1160 else if (paneptr
[0] == '+' || paneptr
[0] == '-')
1161 *wpp
= cmd_find_pane_offset(paneptr
, wl
);
1163 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1166 *wpp
= window_pane_at_index(wl
->window
, idx
);
1175 /* Try pane string description. */
1176 if ((*wpp
= window_find_string(wl
->window
, paneptr
)) == NULL
) {
1177 cmdq_error(cmdq
, "can't find pane: %s", paneptr
);
1185 /* Try as a pane number alone. */
1186 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1190 /* Try index in the current session and window. */
1191 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1197 /* Try pane string description. */
1198 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
1201 /* Try as a window and use the active pane. */
1202 if ((wl
= cmd_find_window(cmdq
, arg
, sp
)) != NULL
)
1203 *wpp
= wl
->window
->active
;
1211 struct window_pane
*
1212 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1214 struct window
*w
= wl
->window
;
1215 struct window_pane
*wp
= w
->active
;
1218 if (paneptr
[1] != '\0')
1219 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1221 if (paneptr
[0] == '+')
1222 wp
= window_pane_next_by_number(w
, wp
, offset
);
1224 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1230 /* Replace the first %% or %idx in template by s. */
1232 cmd_template_replace(const char *template, const char *s
, int idx
)
1239 if (strchr(template, '%') == NULL
)
1240 return (xstrdup(template));
1248 while (*ptr
!= '\0') {
1249 switch (ch
= *ptr
++) {
1251 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1252 if (*ptr
!= '%' || replaced
)
1259 buf
= xrealloc(buf
, 1, len
+ 1);
1260 strlcat(buf
, s
, len
+ 1);
1263 buf
= xrealloc(buf
, 1, len
+ 2);