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>
30 const struct cmd_entry
*cmd_table
[] = {
31 &cmd_attach_session_entry
,
33 &cmd_break_pane_entry
,
34 &cmd_capture_pane_entry
,
35 &cmd_choose_client_entry
,
36 &cmd_choose_session_entry
,
37 &cmd_choose_window_entry
,
38 &cmd_clear_history_entry
,
39 &cmd_clock_mode_entry
,
40 &cmd_command_prompt_entry
,
41 &cmd_confirm_before_entry
,
42 &cmd_copy_buffer_entry
,
44 &cmd_delete_buffer_entry
,
45 &cmd_detach_client_entry
,
46 &cmd_display_message_entry
,
47 &cmd_display_panes_entry
,
48 &cmd_find_window_entry
,
49 &cmd_has_session_entry
,
53 &cmd_kill_server_entry
,
54 &cmd_kill_session_entry
,
55 &cmd_kill_window_entry
,
56 &cmd_last_window_entry
,
57 &cmd_link_window_entry
,
58 &cmd_list_buffers_entry
,
59 &cmd_list_clients_entry
,
60 &cmd_list_commands_entry
,
62 &cmd_list_panes_entry
,
63 &cmd_list_sessions_entry
,
64 &cmd_list_windows_entry
,
65 &cmd_load_buffer_entry
,
66 &cmd_lock_client_entry
,
67 &cmd_lock_server_entry
,
68 &cmd_lock_session_entry
,
69 &cmd_move_window_entry
,
70 &cmd_new_session_entry
,
71 &cmd_new_window_entry
,
72 &cmd_next_layout_entry
,
73 &cmd_next_window_entry
,
74 &cmd_paste_buffer_entry
,
76 &cmd_previous_layout_entry
,
77 &cmd_previous_window_entry
,
78 &cmd_refresh_client_entry
,
79 &cmd_rename_session_entry
,
80 &cmd_rename_window_entry
,
81 &cmd_resize_pane_entry
,
82 &cmd_respawn_window_entry
,
83 &cmd_rotate_window_entry
,
85 &cmd_save_buffer_entry
,
86 &cmd_select_layout_entry
,
87 &cmd_select_pane_entry
,
88 &cmd_select_prompt_entry
,
89 &cmd_select_window_entry
,
91 &cmd_send_prefix_entry
,
92 &cmd_server_info_entry
,
93 &cmd_set_buffer_entry
,
94 &cmd_set_environment_entry
,
95 &cmd_set_option_entry
,
96 &cmd_set_window_option_entry
,
97 &cmd_show_buffer_entry
,
98 &cmd_show_environment_entry
,
99 &cmd_show_messages_entry
,
100 &cmd_show_options_entry
,
101 &cmd_show_window_options_entry
,
102 &cmd_source_file_entry
,
103 &cmd_split_window_entry
,
104 &cmd_start_server_entry
,
105 &cmd_suspend_client_entry
,
106 &cmd_swap_pane_entry
,
107 &cmd_swap_window_entry
,
108 &cmd_switch_client_entry
,
109 &cmd_unbind_key_entry
,
110 &cmd_unlink_window_entry
,
114 struct session
*cmd_choose_session(struct sessions
*);
115 struct client
*cmd_choose_client(struct clients
*);
116 struct client
*cmd_lookup_client(const char *);
117 struct session
*cmd_lookup_session(const char *, int *);
118 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
119 int cmd_lookup_index(struct session
*, const char *, int *);
122 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
128 for (i
= 0; i
< argc
; i
++) {
129 if (strlcpy(buf
, argv
[i
], len
) >= len
)
131 arglen
= strlen(argv
[i
]) + 1;
140 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
147 *argv
= xcalloc(argc
, sizeof **argv
);
150 for (i
= 0; i
< argc
; i
++) {
152 cmd_free_argv(argc
, *argv
);
156 arglen
= strlen(buf
) + 1;
157 (*argv
)[i
] = xstrdup(buf
);
166 cmd_free_argv(int argc
, char **argv
)
172 for (i
= 0; i
< argc
; i
++) {
180 cmd_parse(int argc
, char **argv
, char **cause
)
182 const struct cmd_entry
**entryp
, *entry
;
185 int opt
, ambiguous
= 0;
189 xasprintf(cause
, "no command");
194 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
195 if ((*entryp
)->alias
!= NULL
&&
196 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
202 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
208 /* Bail now if an exact match. */
209 if (strcmp(entry
->name
, argv
[0]) == 0)
215 xasprintf(cause
, "unknown command: %s", argv
[0]);
221 if (entry
->parse
== NULL
) {
222 while ((opt
= getopt(argc
, argv
, "")) != -1) {
234 cmd
= xmalloc(sizeof *cmd
);
237 if (entry
->parse
!= NULL
) {
238 if (entry
->parse(cmd
, argc
, argv
, cause
) != 0) {
247 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
248 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
250 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
252 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
255 s
[strlen(s
) - 2] = '\0';
256 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
260 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
265 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
267 return (cmd
->entry
->exec(cmd
, ctx
));
271 cmd_free(struct cmd
*cmd
)
273 if (cmd
->data
!= NULL
&& cmd
->entry
->free
!= NULL
)
274 cmd
->entry
->free(cmd
);
279 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
281 if (cmd
->entry
->print
== NULL
)
282 return (xsnprintf(buf
, len
, "%s", cmd
->entry
->name
));
283 return (cmd
->entry
->print(cmd
, buf
, len
));
287 * Figure out the current session. Use: 1) the current session, if the command
288 * context has one; 2) the most recently used session containing the pty of the
289 * calling client, if any; 3) the session specified in the TMUX variable from
290 * the environment (as passed from the client); 4) the most recently used
291 * session from all sessions.
294 cmd_current_session(struct cmd_ctx
*ctx
)
296 struct msg_command_data
*data
= ctx
->msgdata
;
297 struct client
*c
= ctx
->cmdclient
;
301 struct window_pane
*wp
;
305 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
306 return (ctx
->curclient
->session
);
309 * If the name of the calling client's pty is know, build a list of the
310 * sessions that contain it and if any choose either the first or the
313 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
315 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
316 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
319 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
320 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
321 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
333 s
= cmd_choose_session(&ss
);
339 /* Use the session from the TMUX environment variable. */
340 if (data
!= NULL
&& data
->pid
!= -1) {
341 if (data
->pid
!= getpid())
343 if (data
->idx
> ARRAY_LENGTH(&sessions
))
345 if ((s
= ARRAY_ITEM(&sessions
, data
->idx
)) == NULL
)
350 return (cmd_choose_session(&sessions
));
353 /* Find the most recently used session from a list. */
355 cmd_choose_session(struct sessions
*ss
)
357 struct session
*s
, *sbest
;
358 struct timeval
*tv
= NULL
;
362 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
363 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
366 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
368 tv
= &s
->activity_time
;
376 * Find the current client. First try the current client if set, then pick the
377 * most recently used of the clients attached to the current session if any,
378 * then of all clients.
381 cmd_current_client(struct cmd_ctx
*ctx
)
388 if (ctx
->curclient
!= NULL
)
389 return (ctx
->curclient
);
392 * No current client set. Find the current session and return the
393 * newest of its clients.
395 s
= cmd_current_session(ctx
);
396 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
398 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
399 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
405 c
= cmd_choose_client(&cc
);
411 return (cmd_choose_client(&clients
));
414 /* Choose the most recently used client from a list. */
416 cmd_choose_client(struct clients
*cc
)
418 struct client
*c
, *cbest
;
419 struct timeval
*tv
= NULL
;
423 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
424 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
426 if (c
->session
== NULL
)
429 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
431 tv
= &c
->activity_time
;
438 /* Find the target client or report an error and return NULL. */
440 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
446 /* A NULL argument means the current client. */
448 return (cmd_current_client(ctx
));
449 tmparg
= xstrdup(arg
);
451 /* Trim a single trailing colon if any. */
452 arglen
= strlen(tmparg
);
453 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
454 tmparg
[arglen
- 1] = '\0';
456 /* Find the client, if any. */
457 c
= cmd_lookup_client(tmparg
);
459 /* If no client found, report an error. */
461 ctx
->error(ctx
, "client not found: %s", tmparg
);
468 * Lookup a client by device path. Either of a full match and a match without a
469 * leading _PATH_DEV ("/dev/") is accepted.
472 cmd_lookup_client(const char *name
)
478 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
479 c
= ARRAY_ITEM(&clients
, i
);
480 if (c
== NULL
|| c
->session
== NULL
)
484 /* Check for exact matches. */
485 if (strcmp(name
, path
) == 0)
488 /* Check without leading /dev if present. */
489 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
491 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
498 /* Lookup a session by name. If no session is found, NULL is returned. */
500 cmd_lookup_session(const char *name
, int *ambiguous
)
502 struct session
*s
, *sfound
;
508 * Look for matches. First look for exact matches - session names must
509 * be unique so an exact match can't be ambigious and can just be
512 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
513 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
515 if (strcmp(name
, s
->name
) == 0)
520 * Otherwise look for partial matches, returning early if it is found to
524 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
525 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
527 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
528 fnmatch(name
, s
->name
, 0) == 0) {
529 if (sfound
!= NULL
) {
540 * Lookup a window or return -1 if not found or ambigious. First try as an
541 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
542 * idx if the window index is a valid number but there is now window with that
546 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
548 struct winlink
*wl
, *wlfound
;
554 /* First see if this is a valid window index in this session. */
555 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
556 if (errstr
== NULL
) {
557 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
561 /* Look for exact matches, error if more than one. */
563 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
564 if (strcmp(name
, wl
->window
->name
) == 0) {
565 if (wlfound
!= NULL
) {
575 /* Now look for pattern matches, again error if multiple. */
577 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
578 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
579 fnmatch(name
, wl
->window
->name
, 0) == 0) {
580 if (wlfound
!= NULL
) {
594 * Find a window index - if the window doesn't exist, check if it is a
595 * potential index and return it anyway.
598 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
604 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
609 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
616 /* Find the target session or report an error and return NULL. */
618 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
)
626 /* A NULL argument means the current session. */
628 return (cmd_current_session(ctx
));
629 tmparg
= xstrdup(arg
);
631 /* Trim a single trailing colon if any. */
632 arglen
= strlen(tmparg
);
633 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
634 tmparg
[arglen
- 1] = '\0';
636 /* Find the session, if any. */
637 s
= cmd_lookup_session(tmparg
, &ambiguous
);
639 /* If it doesn't, try to match it as a client. */
640 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
643 /* If no session found, report an error. */
646 ctx
->error(ctx
, "more than one session: %s", tmparg
);
648 ctx
->error(ctx
, "session not found: %s", tmparg
);
655 /* Find the target session and window or report an error and return NULL. */
657 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
662 char *sessptr
= NULL
;
666 * Find the current session. There must always be a current session, if
667 * it can't be found, report an error.
669 if ((s
= cmd_current_session(ctx
)) == NULL
) {
670 ctx
->error(ctx
, "can't establish current session");
674 /* A NULL argument means the current session and window. */
681 /* Time to look at the argument. If it is empty, that is an error. */
685 /* Find the separating colon and split into window and session. */
686 winptr
= strchr(arg
, ':');
689 winptr
++; /* skip : */
690 sessptr
= xstrdup(arg
);
691 *strchr(sessptr
, ':') = '\0';
693 /* Try to lookup the session if present. */
694 if (*sessptr
!= '\0') {
695 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
702 * Then work out the window. An empty string is the current window,
703 * otherwise try special cases then to look it up in the session.
707 else if (winptr
[0] == '!' && winptr
[1] == '\0')
708 wl
= TAILQ_FIRST(&s
->lastw
);
709 else if (winptr
[0] == '+' && winptr
[1] == '\0')
710 wl
= winlink_next(s
->curw
);
711 else if (winptr
[0] == '-' && winptr
[1] == '\0')
712 wl
= winlink_previous(s
->curw
);
714 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
724 * No colon in the string, first try special cases, then as a window
725 * and lastly as a session.
727 if (arg
[0] == '!' && arg
[1] == '\0') {
728 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
730 } else if (arg
[0] == '+' && arg
[1] == '\0') {
731 if ((wl
= winlink_next(s
->curw
)) == NULL
)
733 } else if (arg
[0] == '-' && arg
[1] == '\0') {
734 if ((wl
= winlink_previous(s
->curw
)) == NULL
)
736 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
) {
739 if ((s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
751 ctx
->error(ctx
, "multiple sessions: %s", arg
);
753 ctx
->error(ctx
, "session not found: %s", arg
);
760 ctx
->error(ctx
, "multiple windows: %s", arg
);
762 ctx
->error(ctx
, "window not found: %s", arg
);
769 * Find the target session and window index, whether or not it exists in the
770 * session. Return -2 on error or -1 if no window index is specified. This is
771 * used when parsing an argument for a window target that may not exist (for
772 * example if it is going to be created).
775 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
780 char *sessptr
= NULL
;
781 int idx
, ambiguous
= 0;
784 * Find the current session. There must always be a current session, if
785 * it can't be found, report an error.
787 if ((s
= cmd_current_session(ctx
)) == NULL
) {
788 ctx
->error(ctx
, "can't establish current session");
792 /* A NULL argument means the current session and "no window" (-1). */
799 /* Time to look at the argument. If it is empty, that is an error. */
803 /* Find the separating colon. If none, assume the current session. */
804 winptr
= strchr(arg
, ':');
807 winptr
++; /* skip : */
808 sessptr
= xstrdup(arg
);
809 *strchr(sessptr
, ':') = '\0';
811 /* Try to lookup the session if present. */
812 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
813 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
820 * Then work out the window. An empty string is a new window otherwise
821 * try to look it up in the session.
825 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
826 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
829 } else if (winptr
[0] == '+' && winptr
[1] == '\0') {
830 if (s
->curw
->idx
== INT_MAX
)
832 idx
= s
->curw
->idx
+ 1;
833 } else if (winptr
[0] == '-' && winptr
[1] == '\0') {
834 if (s
->curw
->idx
== 0)
836 idx
= s
->curw
->idx
- 1;
837 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1) {
840 ctx
->error(ctx
, "invalid index: %s", arg
);
850 * No colon in the string, first try special cases, then as a window
851 * and lastly as a session.
853 if (arg
[0] == '!' && arg
[1] == '\0') {
854 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
857 } else if (arg
[0] == '+' && arg
[1] == '\0') {
858 if (s
->curw
->idx
== INT_MAX
)
860 idx
= s
->curw
->idx
+ 1;
861 } else if (arg
[0] == '-' && arg
[1] == '\0') {
862 if (s
->curw
->idx
== 0)
864 idx
= s
->curw
->idx
- 1;
865 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1) {
868 if ((s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
880 ctx
->error(ctx
, "multiple sessions: %s", arg
);
882 ctx
->error(ctx
, "session not found: %s", arg
);
889 ctx
->error(ctx
, "multiple windows: %s", arg
);
891 ctx
->error(ctx
, "window not found: %s", arg
);
898 * Find the target session, window and pane number or report an error and
899 * return NULL. The pane number is separated from the session:window by a .,
900 * such as mysession:mywindow.0.
903 cmd_find_pane(struct cmd_ctx
*ctx
,
904 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
908 struct layout_cell
*lc
;
909 const char *period
, *errstr
;
910 char *winptr
, *paneptr
;
913 /* Get the current session. */
914 if ((s
= cmd_current_session(ctx
)) == NULL
) {
915 ctx
->error(ctx
, "can't establish current session");
921 /* A NULL argument means the current session, window and pane. */
923 *wpp
= s
->curw
->window
->active
;
927 /* Look for a separating period. */
928 if ((period
= strrchr(arg
, '.')) == NULL
)
931 /* Pull out the window part and parse it. */
932 winptr
= xstrdup(arg
);
933 winptr
[period
- arg
] = '\0';
936 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
939 /* Find the pane section and look it up. */
940 paneptr
= winptr
+ (period
- arg
) + 1;
941 if (*paneptr
== '\0')
942 *wpp
= wl
->window
->active
;
944 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
947 *wpp
= window_pane_at_index(wl
->window
, idx
);
956 /* Try as next or previous pane. */
957 if (paneptr
[0] == '+' && paneptr
[1] == '\0') {
958 *wpp
= TAILQ_NEXT(wl
->window
->active
, entry
);
960 *wpp
= TAILQ_FIRST(&wl
->window
->panes
);
963 if (paneptr
[0] == '-' && paneptr
[1] == '\0') {
964 *wpp
= TAILQ_PREV(wl
->window
->active
, window_panes
, entry
);
966 *wpp
= TAILQ_LAST(&wl
->window
->panes
, window_panes
);
970 /* Try pane string description. */
971 if ((lc
= layout_find_string(wl
->window
, paneptr
)) == NULL
) {
972 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
981 /* Try as a pane number alone. */
982 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
986 /* Try index in the current session and window. */
987 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
993 /* Try pane string description. */
994 if ((lc
= layout_find_string(s
->curw
->window
, arg
)) != NULL
) {
999 /* Try as a window and use the active pane. */
1000 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
1001 *wpp
= wl
->window
->active
;
1009 /* Replace the first %% or %idx in template by s. */
1011 cmd_template_replace(char *template, const char *s
, int idx
)
1018 if (strstr(template, "%") == NULL
)
1019 return (xstrdup(template));
1027 while (*ptr
!= '\0') {
1028 switch (ch
= *ptr
++) {
1030 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1031 if (*ptr
!= '%' || replaced
)
1038 buf
= xrealloc(buf
, 1, len
+ 1);
1039 strlcat(buf
, s
, len
+ 1);
1042 buf
= xrealloc(buf
, 1, len
+ 2);