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
,
49 &cmd_find_window_entry
,
50 &cmd_has_session_entry
,
54 &cmd_kill_server_entry
,
55 &cmd_kill_session_entry
,
56 &cmd_kill_window_entry
,
57 &cmd_last_window_entry
,
58 &cmd_link_window_entry
,
59 &cmd_list_buffers_entry
,
60 &cmd_list_clients_entry
,
61 &cmd_list_commands_entry
,
63 &cmd_list_panes_entry
,
64 &cmd_list_sessions_entry
,
65 &cmd_list_windows_entry
,
66 &cmd_load_buffer_entry
,
67 &cmd_lock_client_entry
,
68 &cmd_lock_server_entry
,
69 &cmd_lock_session_entry
,
70 &cmd_move_window_entry
,
71 &cmd_new_session_entry
,
72 &cmd_new_window_entry
,
73 &cmd_next_layout_entry
,
74 &cmd_next_window_entry
,
75 &cmd_paste_buffer_entry
,
77 &cmd_previous_layout_entry
,
78 &cmd_previous_window_entry
,
79 &cmd_refresh_client_entry
,
80 &cmd_rename_session_entry
,
81 &cmd_rename_window_entry
,
82 &cmd_resize_pane_entry
,
83 &cmd_respawn_window_entry
,
84 &cmd_rotate_window_entry
,
86 &cmd_save_buffer_entry
,
87 &cmd_select_layout_entry
,
88 &cmd_select_pane_entry
,
89 &cmd_select_prompt_entry
,
90 &cmd_select_window_entry
,
92 &cmd_send_prefix_entry
,
93 &cmd_server_info_entry
,
94 &cmd_set_buffer_entry
,
95 &cmd_set_environment_entry
,
96 &cmd_set_option_entry
,
97 &cmd_set_window_option_entry
,
98 &cmd_show_buffer_entry
,
99 &cmd_show_environment_entry
,
100 &cmd_show_messages_entry
,
101 &cmd_show_options_entry
,
102 &cmd_show_window_options_entry
,
103 &cmd_source_file_entry
,
104 &cmd_split_window_entry
,
105 &cmd_start_server_entry
,
106 &cmd_suspend_client_entry
,
107 &cmd_swap_pane_entry
,
108 &cmd_swap_window_entry
,
109 &cmd_switch_client_entry
,
110 &cmd_unbind_key_entry
,
111 &cmd_unlink_window_entry
,
116 struct session
*cmd_choose_session(struct sessions
*);
117 struct client
*cmd_choose_client(struct clients
*);
118 struct client
*cmd_lookup_client(const char *);
119 struct session
*cmd_lookup_session(const char *, int *);
120 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
121 int cmd_lookup_index(struct session
*, const char *, int *);
124 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
130 for (i
= 0; i
< argc
; i
++) {
131 if (strlcpy(buf
, argv
[i
], len
) >= len
)
133 arglen
= strlen(argv
[i
]) + 1;
142 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
149 *argv
= xcalloc(argc
, sizeof **argv
);
152 for (i
= 0; i
< argc
; i
++) {
154 cmd_free_argv(argc
, *argv
);
158 arglen
= strlen(buf
) + 1;
159 (*argv
)[i
] = xstrdup(buf
);
168 cmd_free_argv(int argc
, char **argv
)
174 for (i
= 0; i
< argc
; i
++) {
182 cmd_parse(int argc
, char **argv
, char **cause
)
184 const struct cmd_entry
**entryp
, *entry
;
187 int opt
, ambiguous
= 0;
191 xasprintf(cause
, "no command");
196 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
197 if ((*entryp
)->alias
!= NULL
&&
198 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
204 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
210 /* Bail now if an exact match. */
211 if (strcmp(entry
->name
, argv
[0]) == 0)
217 xasprintf(cause
, "unknown command: %s", argv
[0]);
223 if (entry
->parse
== NULL
) {
224 while ((opt
= getopt(argc
, argv
, "")) != -1) {
236 cmd
= xmalloc(sizeof *cmd
);
239 if (entry
->parse
!= NULL
) {
240 if (entry
->parse(cmd
, argc
, argv
, cause
) != 0) {
249 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
250 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
252 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
254 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
257 s
[strlen(s
) - 2] = '\0';
258 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
262 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
267 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
269 return (cmd
->entry
->exec(cmd
, ctx
));
273 cmd_free(struct cmd
*cmd
)
275 if (cmd
->data
!= NULL
&& cmd
->entry
->free
!= NULL
)
276 cmd
->entry
->free(cmd
);
281 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
283 if (cmd
->entry
->print
== NULL
)
284 return (xsnprintf(buf
, len
, "%s", cmd
->entry
->name
));
285 return (cmd
->entry
->print(cmd
, buf
, len
));
289 * Figure out the current session. Use: 1) the current session, if the command
290 * context has one; 2) the most recently used session containing the pty of the
291 * calling client, if any; 3) the session specified in the TMUX variable from
292 * the environment (as passed from the client); 4) the most recently used
293 * session from all sessions.
296 cmd_current_session(struct cmd_ctx
*ctx
)
298 struct msg_command_data
*data
= ctx
->msgdata
;
299 struct client
*c
= ctx
->cmdclient
;
303 struct window_pane
*wp
;
307 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
308 return (ctx
->curclient
->session
);
311 * If the name of the calling client's pty is know, build a list of the
312 * sessions that contain it and if any choose either the first or the
315 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
317 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
318 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
321 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
322 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
323 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
335 s
= cmd_choose_session(&ss
);
341 /* Use the session from the TMUX environment variable. */
342 if (data
!= NULL
&& data
->pid
!= -1) {
343 if (data
->pid
!= getpid())
345 if (data
->idx
> ARRAY_LENGTH(&sessions
))
347 if ((s
= ARRAY_ITEM(&sessions
, data
->idx
)) == NULL
)
352 return (cmd_choose_session(&sessions
));
355 /* Find the most recently used session from a list. */
357 cmd_choose_session(struct sessions
*ss
)
359 struct session
*s
, *sbest
;
360 struct timeval
*tv
= NULL
;
364 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
365 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
368 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
370 tv
= &s
->activity_time
;
378 * Find the current client. First try the current client if set, then pick the
379 * most recently used of the clients attached to the current session if any,
380 * then of all clients.
383 cmd_current_client(struct cmd_ctx
*ctx
)
390 if (ctx
->curclient
!= NULL
)
391 return (ctx
->curclient
);
394 * No current client set. Find the current session and return the
395 * newest of its clients.
397 s
= cmd_current_session(ctx
);
398 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
400 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
401 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
407 c
= cmd_choose_client(&cc
);
413 return (cmd_choose_client(&clients
));
416 /* Choose the most recently used client from a list. */
418 cmd_choose_client(struct clients
*cc
)
420 struct client
*c
, *cbest
;
421 struct timeval
*tv
= NULL
;
425 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
426 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
428 if (c
->session
== NULL
)
431 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
433 tv
= &c
->activity_time
;
440 /* Find the target client or report an error and return NULL. */
442 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
448 /* A NULL argument means the current client. */
450 return (cmd_current_client(ctx
));
451 tmparg
= xstrdup(arg
);
453 /* Trim a single trailing colon if any. */
454 arglen
= strlen(tmparg
);
455 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
456 tmparg
[arglen
- 1] = '\0';
458 /* Find the client, if any. */
459 c
= cmd_lookup_client(tmparg
);
461 /* If no client found, report an error. */
463 ctx
->error(ctx
, "client not found: %s", tmparg
);
470 * Lookup a client by device path. Either of a full match and a match without a
471 * leading _PATH_DEV ("/dev/") is accepted.
474 cmd_lookup_client(const char *name
)
480 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
481 c
= ARRAY_ITEM(&clients
, i
);
482 if (c
== NULL
|| c
->session
== NULL
)
486 /* Check for exact matches. */
487 if (strcmp(name
, path
) == 0)
490 /* Check without leading /dev if present. */
491 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
493 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
500 /* Lookup a session by name. If no session is found, NULL is returned. */
502 cmd_lookup_session(const char *name
, int *ambiguous
)
504 struct session
*s
, *sfound
;
510 * Look for matches. First look for exact matches - session names must
511 * be unique so an exact match can't be ambigious and can just be
514 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
515 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
517 if (strcmp(name
, s
->name
) == 0)
522 * Otherwise look for partial matches, returning early if it is found to
526 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
527 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
529 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
530 fnmatch(name
, s
->name
, 0) == 0) {
531 if (sfound
!= NULL
) {
542 * Lookup a window or return -1 if not found or ambigious. First try as an
543 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
544 * idx if the window index is a valid number but there is now window with that
548 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
550 struct winlink
*wl
, *wlfound
;
556 /* First see if this is a valid window index in this session. */
557 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
558 if (errstr
== NULL
) {
559 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
563 /* Look for exact matches, error if more than one. */
565 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
566 if (strcmp(name
, wl
->window
->name
) == 0) {
567 if (wlfound
!= NULL
) {
577 /* Now look for pattern matches, again error if multiple. */
579 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
580 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
581 fnmatch(name
, wl
->window
->name
, 0) == 0) {
582 if (wlfound
!= NULL
) {
596 * Find a window index - if the window doesn't exist, check if it is a
597 * potential index and return it anyway.
600 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
606 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
611 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
618 /* Find the target session or report an error and return NULL. */
620 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
)
628 /* A NULL argument means the current session. */
630 return (cmd_current_session(ctx
));
631 tmparg
= xstrdup(arg
);
633 /* Trim a single trailing colon if any. */
634 arglen
= strlen(tmparg
);
635 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
636 tmparg
[arglen
- 1] = '\0';
638 /* Find the session, if any. */
639 s
= cmd_lookup_session(tmparg
, &ambiguous
);
641 /* If it doesn't, try to match it as a client. */
642 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
645 /* If no session found, report an error. */
648 ctx
->error(ctx
, "more than one session: %s", tmparg
);
650 ctx
->error(ctx
, "session not found: %s", tmparg
);
657 /* Find the target session and window or report an error and return NULL. */
659 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
664 char *sessptr
= NULL
;
668 * Find the current session. There must always be a current session, if
669 * it can't be found, report an error.
671 if ((s
= cmd_current_session(ctx
)) == NULL
) {
672 ctx
->error(ctx
, "can't establish current session");
676 /* A NULL argument means the current session and window. */
683 /* Time to look at the argument. If it is empty, that is an error. */
687 /* Find the separating colon and split into window and session. */
688 winptr
= strchr(arg
, ':');
691 winptr
++; /* skip : */
692 sessptr
= xstrdup(arg
);
693 *strchr(sessptr
, ':') = '\0';
695 /* Try to lookup the session if present. */
696 if (*sessptr
!= '\0') {
697 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
704 * Then work out the window. An empty string is the current window,
705 * otherwise try to look it up in the session.
709 else if ((wl
= cmd_lookup_window(s
, winptr
, &ambiguous
)) == NULL
)
717 /* No colon in the string, first try as a window then as a session. */
718 if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
) {
721 if ((s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
733 ctx
->error(ctx
, "multiple sessions: %s", arg
);
735 ctx
->error(ctx
, "session not found: %s", arg
);
742 ctx
->error(ctx
, "multiple windows: %s", arg
);
744 ctx
->error(ctx
, "window not found: %s", arg
);
751 * Find the target session and window index, whether or not it exists in the
752 * session. Return -2 on error or -1 if no window index is specified. This is
753 * used when parsing an argument for a window target that may not exist (for
754 * example if it is going to be created).
757 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
761 char *sessptr
= NULL
;
762 int idx
, ambiguous
= 0;
765 * Find the current session. There must always be a current session, if
766 * it can't be found, report an error.
768 if ((s
= cmd_current_session(ctx
)) == NULL
) {
769 ctx
->error(ctx
, "can't establish current session");
773 /* A NULL argument means the current session and "no window" (-1). */
780 /* Time to look at the argument. If it is empty, that is an error. */
784 /* Find the separating colon. If none, assume the current session. */
785 winptr
= strchr(arg
, ':');
788 winptr
++; /* skip : */
789 sessptr
= xstrdup(arg
);
790 *strchr(sessptr
, ':') = '\0';
792 /* Try to lookup the session if present. */
793 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
794 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
801 * Then work out the window. An empty string is a new window otherwise
802 * try to look it up in the session.
806 else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1) {
809 ctx
->error(ctx
, "invalid index: %s", arg
);
818 /* No colon in the string, first try as a window then as a session. */
819 if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1) {
822 if ((s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
834 ctx
->error(ctx
, "multiple sessions: %s", arg
);
836 ctx
->error(ctx
, "session not found: %s", arg
);
843 ctx
->error(ctx
, "multiple windows: %s", arg
);
845 ctx
->error(ctx
, "window not found: %s", arg
);
852 * Find the target session, window and pane number or report an error and
853 * return NULL. The pane number is separated from the session:window by a .,
854 * such as mysession:mywindow.0.
857 cmd_find_pane(struct cmd_ctx
*ctx
,
858 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
862 struct layout_cell
*lc
;
863 const char *period
, *errstr
;
864 char *winptr
, *paneptr
;
867 /* Get the current session. */
868 if ((s
= cmd_current_session(ctx
)) == NULL
) {
869 ctx
->error(ctx
, "can't establish current session");
875 /* A NULL argument means the current session, window and pane. */
877 *wpp
= s
->curw
->window
->active
;
881 /* Look for a separating period. */
882 if ((period
= strrchr(arg
, '.')) == NULL
)
885 /* Pull out the window part and parse it. */
886 winptr
= xstrdup(arg
);
887 winptr
[period
- arg
] = '\0';
890 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
893 /* Find the pane section and look it up. */
894 paneptr
= winptr
+ (period
- arg
) + 1;
895 if (*paneptr
== '\0')
896 *wpp
= wl
->window
->active
;
898 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
901 *wpp
= window_pane_at_index(wl
->window
, idx
);
910 /* Try pane string description. */
911 if ((lc
= layout_find_string(s
->curw
->window
, paneptr
)) == NULL
) {
912 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
921 /* Try as a pane number alone. */
922 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
926 /* Try index in the current session and window. */
927 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
933 /* Try pane string description. */
934 if ((lc
= layout_find_string(s
->curw
->window
, arg
)) != NULL
) {
939 /* Try as a window and use the active pane. */
940 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
941 *wpp
= wl
->window
->active
;
949 /* Replace the first %% or %idx in template by s. */
951 cmd_template_replace(char *template, const char *s
, int idx
)
958 if (strstr(template, "%") == NULL
)
959 return (xstrdup(template));
967 while (*ptr
!= '\0') {
968 switch (ch
= *ptr
++) {
970 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
971 if (*ptr
!= '%' || replaced
)
978 buf
= xrealloc(buf
, 1, len
+ 1);
979 strlcat(buf
, s
, len
+ 1);
982 buf
= xrealloc(buf
, 1, len
+ 2);