2014-11-08 13:27:43 +01:00
|
|
|
/* $OpenBSD$ */
|
2007-10-03 12:18:32 +02:00
|
|
|
|
|
|
|
/*
|
2016-01-19 16:59:12 +01:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2007-10-03 12:18:32 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2008-06-03 20:13:54 +02:00
|
|
|
#include <sys/time.h>
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2009-07-14 08:42:06 +02:00
|
|
|
#include <fnmatch.h>
|
2012-02-02 02:55:34 +01:00
|
|
|
#include <pwd.h>
|
2008-06-05 23:25:00 +02:00
|
|
|
#include <stdlib.h>
|
2007-10-03 13:26:34 +02:00
|
|
|
#include <string.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
#include <unistd.h>
|
2007-10-03 12:18:32 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2015-09-01 11:48:34 +02:00
|
|
|
extern const struct cmd_entry cmd_attach_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_bind_key_entry;
|
|
|
|
extern const struct cmd_entry cmd_break_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_capture_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_choose_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_choose_client_entry;
|
|
|
|
extern const struct cmd_entry cmd_choose_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_choose_tree_entry;
|
|
|
|
extern const struct cmd_entry cmd_choose_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_clear_history_entry;
|
|
|
|
extern const struct cmd_entry cmd_clock_mode_entry;
|
|
|
|
extern const struct cmd_entry cmd_command_prompt_entry;
|
|
|
|
extern const struct cmd_entry cmd_confirm_before_entry;
|
|
|
|
extern const struct cmd_entry cmd_copy_mode_entry;
|
|
|
|
extern const struct cmd_entry cmd_delete_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_detach_client_entry;
|
|
|
|
extern const struct cmd_entry cmd_display_message_entry;
|
|
|
|
extern const struct cmd_entry cmd_display_panes_entry;
|
|
|
|
extern const struct cmd_entry cmd_down_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_find_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_has_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_if_shell_entry;
|
|
|
|
extern const struct cmd_entry cmd_join_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_kill_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_kill_server_entry;
|
|
|
|
extern const struct cmd_entry cmd_kill_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_kill_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_last_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_last_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_link_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_buffers_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_clients_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_commands_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_keys_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_panes_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_sessions_entry;
|
|
|
|
extern const struct cmd_entry cmd_list_windows_entry;
|
|
|
|
extern const struct cmd_entry cmd_load_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_lock_client_entry;
|
|
|
|
extern const struct cmd_entry cmd_lock_server_entry;
|
|
|
|
extern const struct cmd_entry cmd_lock_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_move_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_move_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_new_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_new_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_next_layout_entry;
|
|
|
|
extern const struct cmd_entry cmd_next_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_paste_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_pipe_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_previous_layout_entry;
|
|
|
|
extern const struct cmd_entry cmd_previous_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_refresh_client_entry;
|
|
|
|
extern const struct cmd_entry cmd_rename_session_entry;
|
|
|
|
extern const struct cmd_entry cmd_rename_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_resize_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_respawn_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_respawn_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_rotate_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_run_shell_entry;
|
|
|
|
extern const struct cmd_entry cmd_save_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_select_layout_entry;
|
|
|
|
extern const struct cmd_entry cmd_select_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_select_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_send_keys_entry;
|
|
|
|
extern const struct cmd_entry cmd_send_prefix_entry;
|
|
|
|
extern const struct cmd_entry cmd_server_info_entry;
|
|
|
|
extern const struct cmd_entry cmd_set_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_set_environment_entry;
|
2015-12-08 02:10:31 +01:00
|
|
|
extern const struct cmd_entry cmd_set_hook_entry;
|
2015-09-01 11:48:34 +02:00
|
|
|
extern const struct cmd_entry cmd_set_option_entry;
|
|
|
|
extern const struct cmd_entry cmd_set_window_option_entry;
|
|
|
|
extern const struct cmd_entry cmd_show_buffer_entry;
|
|
|
|
extern const struct cmd_entry cmd_show_environment_entry;
|
2015-12-08 02:10:31 +01:00
|
|
|
extern const struct cmd_entry cmd_show_hooks_entry;
|
2015-09-01 11:48:34 +02:00
|
|
|
extern const struct cmd_entry cmd_show_messages_entry;
|
|
|
|
extern const struct cmd_entry cmd_show_options_entry;
|
|
|
|
extern const struct cmd_entry cmd_show_window_options_entry;
|
|
|
|
extern const struct cmd_entry cmd_source_file_entry;
|
|
|
|
extern const struct cmd_entry cmd_split_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_start_server_entry;
|
|
|
|
extern const struct cmd_entry cmd_suspend_client_entry;
|
|
|
|
extern const struct cmd_entry cmd_swap_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_swap_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_switch_client_entry;
|
|
|
|
extern const struct cmd_entry cmd_unbind_key_entry;
|
|
|
|
extern const struct cmd_entry cmd_unlink_window_entry;
|
|
|
|
extern const struct cmd_entry cmd_up_pane_entry;
|
|
|
|
extern const struct cmd_entry cmd_wait_for_entry;
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
const struct cmd_entry *cmd_table[] = {
|
2007-10-04 13:52:03 +02:00
|
|
|
&cmd_attach_session_entry,
|
2007-10-04 11:30:53 +02:00
|
|
|
&cmd_bind_key_entry,
|
2009-03-07 10:29:54 +01:00
|
|
|
&cmd_break_pane_entry,
|
2009-12-02 16:10:44 +01:00
|
|
|
&cmd_capture_pane_entry,
|
2010-06-23 01:35:20 +02:00
|
|
|
&cmd_choose_buffer_entry,
|
2009-08-25 15:53:39 +02:00
|
|
|
&cmd_choose_client_entry,
|
2009-01-15 20:27:31 +01:00
|
|
|
&cmd_choose_session_entry,
|
2012-07-11 19:13:55 +02:00
|
|
|
&cmd_choose_tree_entry,
|
2009-01-15 20:27:31 +01:00
|
|
|
&cmd_choose_window_entry,
|
2009-05-14 18:56:23 +02:00
|
|
|
&cmd_clear_history_entry,
|
2009-01-10 20:35:40 +01:00
|
|
|
&cmd_clock_mode_entry,
|
2008-06-19 22:45:21 +02:00
|
|
|
&cmd_command_prompt_entry,
|
2009-04-27 15:21:16 +02:00
|
|
|
&cmd_confirm_before_entry,
|
2007-11-22 19:09:43 +01:00
|
|
|
&cmd_copy_mode_entry,
|
2008-06-20 19:31:48 +02:00
|
|
|
&cmd_delete_buffer_entry,
|
2007-11-16 22:12:31 +01:00
|
|
|
&cmd_detach_client_entry,
|
2009-07-17 20:32:54 +02:00
|
|
|
&cmd_display_message_entry,
|
2009-09-01 00:30:15 +02:00
|
|
|
&cmd_display_panes_entry,
|
2009-01-18 18:20:52 +01:00
|
|
|
&cmd_find_window_entry,
|
2007-10-25 19:44:25 +02:00
|
|
|
&cmd_has_session_entry,
|
2009-07-09 20:14:18 +02:00
|
|
|
&cmd_if_shell_entry,
|
2010-01-08 17:31:35 +01:00
|
|
|
&cmd_join_pane_entry,
|
2009-01-13 07:50:10 +01:00
|
|
|
&cmd_kill_pane_entry,
|
2008-06-03 07:10:38 +02:00
|
|
|
&cmd_kill_server_entry,
|
2007-11-12 15:21:41 +01:00
|
|
|
&cmd_kill_session_entry,
|
2007-10-19 13:10:35 +02:00
|
|
|
&cmd_kill_window_entry,
|
2010-10-24 03:34:30 +02:00
|
|
|
&cmd_last_pane_entry,
|
2007-10-04 11:30:53 +02:00
|
|
|
&cmd_last_window_entry,
|
2007-10-26 15:03:59 +02:00
|
|
|
&cmd_link_window_entry,
|
2008-06-20 19:31:48 +02:00
|
|
|
&cmd_list_buffers_entry,
|
2007-10-23 11:36:19 +02:00
|
|
|
&cmd_list_clients_entry,
|
2008-06-24 00:24:16 +02:00
|
|
|
&cmd_list_commands_entry,
|
2007-10-04 11:30:53 +02:00
|
|
|
&cmd_list_keys_entry,
|
2009-10-12 02:08:12 +02:00
|
|
|
&cmd_list_panes_entry,
|
2007-10-03 23:31:07 +02:00
|
|
|
&cmd_list_sessions_entry,
|
2007-10-04 13:23:17 +02:00
|
|
|
&cmd_list_windows_entry,
|
2009-01-25 20:00:10 +01:00
|
|
|
&cmd_load_buffer_entry,
|
2009-09-25 19:51:39 +02:00
|
|
|
&cmd_lock_client_entry,
|
2009-01-11 01:48:42 +01:00
|
|
|
&cmd_lock_server_entry,
|
2009-09-25 19:51:39 +02:00
|
|
|
&cmd_lock_session_entry,
|
2012-03-03 10:19:40 +01:00
|
|
|
&cmd_move_pane_entry,
|
2008-06-25 22:33:20 +02:00
|
|
|
&cmd_move_window_entry,
|
2007-10-03 23:31:07 +02:00
|
|
|
&cmd_new_session_entry,
|
2007-10-04 01:32:26 +02:00
|
|
|
&cmd_new_window_entry,
|
2009-04-01 20:21:42 +02:00
|
|
|
&cmd_next_layout_entry,
|
2007-10-04 11:30:53 +02:00
|
|
|
&cmd_next_window_entry,
|
2007-11-23 18:52:54 +01:00
|
|
|
&cmd_paste_buffer_entry,
|
2009-10-12 02:35:08 +02:00
|
|
|
&cmd_pipe_pane_entry,
|
2009-04-30 23:17:06 +02:00
|
|
|
&cmd_previous_layout_entry,
|
2007-10-04 11:30:53 +02:00
|
|
|
&cmd_previous_window_entry,
|
2007-11-16 22:12:31 +01:00
|
|
|
&cmd_refresh_client_entry,
|
2007-11-09 12:02:01 +01:00
|
|
|
&cmd_rename_session_entry,
|
2007-10-04 12:39:07 +02:00
|
|
|
&cmd_rename_window_entry,
|
2009-04-30 08:01:24 +02:00
|
|
|
&cmd_resize_pane_entry,
|
2011-06-23 21:23:12 +02:00
|
|
|
&cmd_respawn_pane_entry,
|
2009-05-04 19:58:27 +02:00
|
|
|
&cmd_respawn_window_entry,
|
2009-04-03 19:21:46 +02:00
|
|
|
&cmd_rotate_window_entry,
|
2009-09-21 00:20:10 +02:00
|
|
|
&cmd_run_shell_entry,
|
2009-01-12 00:14:57 +01:00
|
|
|
&cmd_save_buffer_entry,
|
2009-05-16 13:48:47 +02:00
|
|
|
&cmd_select_layout_entry,
|
2009-01-14 20:56:55 +01:00
|
|
|
&cmd_select_pane_entry,
|
2007-10-04 12:54:21 +02:00
|
|
|
&cmd_select_window_entry,
|
2008-06-01 22:20:25 +02:00
|
|
|
&cmd_send_keys_entry,
|
2007-10-12 15:03:58 +02:00
|
|
|
&cmd_send_prefix_entry,
|
2009-01-10 02:30:38 +01:00
|
|
|
&cmd_server_info_entry,
|
2008-06-20 10:36:20 +02:00
|
|
|
&cmd_set_buffer_entry,
|
2009-08-09 19:48:55 +02:00
|
|
|
&cmd_set_environment_entry,
|
2015-12-08 02:10:31 +01:00
|
|
|
&cmd_set_hook_entry,
|
2007-12-06 10:46:23 +01:00
|
|
|
&cmd_set_option_entry,
|
2008-06-15 10:01:54 +02:00
|
|
|
&cmd_set_window_option_entry,
|
2008-06-20 10:36:20 +02:00
|
|
|
&cmd_show_buffer_entry,
|
2009-08-09 19:48:55 +02:00
|
|
|
&cmd_show_environment_entry,
|
2015-12-08 02:10:31 +01:00
|
|
|
&cmd_show_hooks_entry,
|
2009-11-19 23:20:04 +01:00
|
|
|
&cmd_show_messages_entry,
|
2008-06-15 10:01:54 +02:00
|
|
|
&cmd_show_options_entry,
|
2008-06-16 08:10:02 +02:00
|
|
|
&cmd_show_window_options_entry,
|
2008-12-15 22:21:56 +01:00
|
|
|
&cmd_source_file_entry,
|
2009-01-12 00:31:46 +01:00
|
|
|
&cmd_split_window_entry,
|
2008-06-02 23:08:36 +02:00
|
|
|
&cmd_start_server_entry,
|
2009-01-18 13:09:42 +01:00
|
|
|
&cmd_suspend_client_entry,
|
2009-04-03 01:28:16 +02:00
|
|
|
&cmd_swap_pane_entry,
|
2007-10-30 12:10:33 +01:00
|
|
|
&cmd_swap_window_entry,
|
2007-11-16 22:31:03 +01:00
|
|
|
&cmd_switch_client_entry,
|
2007-10-04 11:30:53 +02:00
|
|
|
&cmd_unbind_key_entry,
|
2007-10-26 18:57:32 +02:00
|
|
|
&cmd_unlink_window_entry,
|
2013-03-06 10:57:26 +01:00
|
|
|
&cmd_wait_for_entry,
|
2007-10-03 23:31:07 +02:00
|
|
|
NULL
|
2007-10-03 13:26:34 +02:00
|
|
|
};
|
|
|
|
|
2009-07-29 00:12:16 +02:00
|
|
|
int
|
|
|
|
cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t arglen;
|
|
|
|
int i;
|
|
|
|
|
2014-02-26 22:42:59 +01:00
|
|
|
if (argc == 0)
|
|
|
|
return (0);
|
|
|
|
|
2009-07-29 00:12:16 +02:00
|
|
|
*buf = '\0';
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (strlcpy(buf, argv[i], len) >= len)
|
|
|
|
return (-1);
|
|
|
|
arglen = strlen(argv[i]) + 1;
|
|
|
|
buf += arglen;
|
|
|
|
len -= arglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t arglen;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return (0);
|
|
|
|
*argv = xcalloc(argc, sizeof **argv);
|
|
|
|
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (len == 0) {
|
|
|
|
cmd_free_argv(argc, *argv);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
arglen = strlen(buf) + 1;
|
|
|
|
(*argv)[i] = xstrdup(buf);
|
|
|
|
buf += arglen;
|
|
|
|
len -= arglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-12-06 22:48:56 +01:00
|
|
|
char **
|
2014-05-09 11:11:24 +02:00
|
|
|
cmd_copy_argv(int argc, char **argv)
|
2010-12-06 22:48:56 +01:00
|
|
|
{
|
|
|
|
char **new_argv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return (NULL);
|
2014-05-13 10:08:32 +02:00
|
|
|
new_argv = xcalloc(argc + 1, sizeof *new_argv);
|
2010-12-06 22:48:56 +01:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] != NULL)
|
|
|
|
new_argv[i] = xstrdup(argv[i]);
|
|
|
|
}
|
|
|
|
return (new_argv);
|
|
|
|
}
|
|
|
|
|
2009-07-29 00:12:16 +02:00
|
|
|
void
|
|
|
|
cmd_free_argv(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc == 0)
|
2009-12-04 23:14:47 +01:00
|
|
|
return;
|
2012-07-11 21:34:16 +02:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
free(argv[i]);
|
|
|
|
free(argv);
|
2009-07-29 00:12:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:08:32 +02:00
|
|
|
char *
|
|
|
|
cmd_stringify_argv(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int i;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return (xstrdup(""));
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
buf = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
len += strlen(argv[i]) + 1;
|
2014-10-08 19:35:58 +02:00
|
|
|
buf = xrealloc(buf, len);
|
2014-05-13 10:08:32 +02:00
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
*buf = '\0';
|
|
|
|
else
|
|
|
|
strlcat(buf, " ", len);
|
|
|
|
strlcat(buf, argv[i], len);
|
|
|
|
}
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
struct cmd *
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
|
2007-10-03 13:26:34 +02:00
|
|
|
{
|
2007-10-05 00:04:01 +02:00
|
|
|
const struct cmd_entry **entryp, *entry;
|
2009-10-29 00:12:38 +01:00
|
|
|
struct cmd *cmd;
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args;
|
2007-11-16 14:23:59 +01:00
|
|
|
char s[BUFSIZ];
|
2011-01-07 15:45:34 +01:00
|
|
|
int ambiguous = 0;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
*cause = NULL;
|
2009-06-25 17:56:39 +02:00
|
|
|
if (argc == 0) {
|
|
|
|
xasprintf(cause, "no command");
|
2007-10-03 23:31:07 +02:00
|
|
|
return (NULL);
|
2009-06-25 17:56:39 +02:00
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
entry = NULL;
|
2007-10-05 00:04:01 +02:00
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
2007-12-06 10:46:23 +01:00
|
|
|
if ((*entryp)->alias != NULL &&
|
2007-10-12 15:51:44 +02:00
|
|
|
strcmp((*entryp)->alias, argv[0]) == 0) {
|
2009-07-08 20:01:55 +02:00
|
|
|
ambiguous = 0;
|
2007-10-05 00:04:01 +02:00
|
|
|
entry = *entryp;
|
2007-10-03 13:26:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2007-10-05 00:04:01 +02:00
|
|
|
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
|
2007-10-03 23:31:07 +02:00
|
|
|
continue;
|
2007-11-16 14:23:59 +01:00
|
|
|
if (entry != NULL)
|
2009-07-08 20:01:55 +02:00
|
|
|
ambiguous = 1;
|
2007-10-05 00:04:01 +02:00
|
|
|
entry = *entryp;
|
2008-07-19 12:07:50 +02:00
|
|
|
|
|
|
|
/* Bail now if an exact match. */
|
|
|
|
if (strcmp(entry->name, argv[0]) == 0)
|
|
|
|
break;
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2009-07-08 20:01:55 +02:00
|
|
|
if (ambiguous)
|
|
|
|
goto ambiguous;
|
2007-10-03 23:31:07 +02:00
|
|
|
if (entry == NULL) {
|
|
|
|
xasprintf(cause, "unknown command: %s", argv[0]);
|
|
|
|
return (NULL);
|
2007-10-03 14:34:16 +02:00
|
|
|
}
|
2009-01-10 02:51:22 +01:00
|
|
|
|
2015-12-13 22:53:57 +01:00
|
|
|
args = args_parse(entry->args.template, argc, argv);
|
2011-01-07 15:45:34 +01:00
|
|
|
if (args == NULL)
|
|
|
|
goto usage;
|
2015-12-13 22:53:57 +01:00
|
|
|
if (entry->args.lower != -1 && args->argc < entry->args.lower)
|
2011-01-07 15:45:34 +01:00
|
|
|
goto usage;
|
2015-12-13 22:53:57 +01:00
|
|
|
if (entry->args.upper != -1 && args->argc > entry->args.upper)
|
2011-01-07 15:45:34 +01:00
|
|
|
goto usage;
|
2007-10-03 13:26:34 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd = xcalloc(1, sizeof *cmd);
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd->entry = entry;
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd->args = args;
|
2013-02-23 23:25:58 +01:00
|
|
|
|
|
|
|
if (file != NULL)
|
|
|
|
cmd->file = xstrdup(file);
|
|
|
|
cmd->line = line;
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
return (cmd);
|
2007-10-03 13:26:34 +02:00
|
|
|
|
2007-11-16 14:23:59 +01:00
|
|
|
ambiguous:
|
|
|
|
*s = '\0';
|
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
|
|
|
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
|
|
|
|
continue;
|
|
|
|
if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
|
|
|
|
break;
|
|
|
|
if (strlcat(s, ", ", sizeof s) >= sizeof s)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s[strlen(s) - 2] = '\0';
|
|
|
|
xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
|
|
|
|
return (NULL);
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
usage:
|
2011-01-07 15:45:34 +01:00
|
|
|
if (args != NULL)
|
|
|
|
args_free(args);
|
2008-06-02 23:08:36 +02:00
|
|
|
xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
|
2007-10-03 23:31:07 +02:00
|
|
|
return (NULL);
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2015-12-13 15:32:38 +01:00
|
|
|
static int
|
2016-01-19 17:01:30 +01:00
|
|
|
cmd_prepare_state_flag(char c, const char *target, enum cmd_entry_flag flag,
|
|
|
|
struct cmd_q *cmdq, struct cmd_q *parent)
|
2015-12-13 15:32:38 +01:00
|
|
|
{
|
2016-01-19 17:01:30 +01:00
|
|
|
int targetflags, error;
|
|
|
|
struct cmd_find_state *fs = NULL;
|
|
|
|
struct cmd_find_state *current = NULL;
|
|
|
|
struct cmd_find_state tmp;
|
|
|
|
|
|
|
|
if (flag == CMD_NONE ||
|
|
|
|
flag == CMD_CLIENT ||
|
|
|
|
flag == CMD_CLIENT_CANFAIL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (c == 't')
|
|
|
|
fs = &cmdq->state.tflag;
|
|
|
|
else if (c == 's')
|
|
|
|
fs = &cmdq->state.sflag;
|
2015-12-13 15:32:38 +01:00
|
|
|
|
2015-12-14 01:31:54 +01:00
|
|
|
if (flag == CMD_SESSION_WITHPANE) {
|
|
|
|
if (target != NULL && target[strcspn(target, ":.")] != '\0')
|
|
|
|
flag = CMD_PANE;
|
|
|
|
else
|
|
|
|
flag = CMD_SESSION;
|
2015-12-13 15:32:38 +01:00
|
|
|
}
|
|
|
|
|
2016-01-19 17:01:30 +01:00
|
|
|
targetflags = 0;
|
2015-12-14 01:31:54 +01:00
|
|
|
switch (flag) {
|
|
|
|
case CMD_SESSION:
|
|
|
|
case CMD_SESSION_CANFAIL:
|
|
|
|
case CMD_SESSION_PREFERUNATTACHED:
|
|
|
|
if (flag == CMD_SESSION_CANFAIL)
|
|
|
|
targetflags |= CMD_FIND_QUIET;
|
|
|
|
if (flag == CMD_SESSION_PREFERUNATTACHED)
|
|
|
|
targetflags |= CMD_FIND_PREFER_UNATTACHED;
|
2015-12-13 15:32:38 +01:00
|
|
|
break;
|
2015-12-14 01:31:54 +01:00
|
|
|
case CMD_MOVEW_R:
|
|
|
|
flag = CMD_WINDOW_INDEX;
|
2015-12-13 16:32:12 +01:00
|
|
|
/* FALLTHROUGH */
|
2015-12-14 01:31:54 +01:00
|
|
|
case CMD_WINDOW:
|
|
|
|
case CMD_WINDOW_CANFAIL:
|
|
|
|
case CMD_WINDOW_MARKED:
|
|
|
|
case CMD_WINDOW_INDEX:
|
|
|
|
if (flag == CMD_WINDOW_CANFAIL)
|
|
|
|
targetflags |= CMD_FIND_QUIET;
|
|
|
|
if (flag == CMD_WINDOW_MARKED)
|
|
|
|
targetflags |= CMD_FIND_DEFAULT_MARKED;
|
|
|
|
if (flag == CMD_WINDOW_INDEX)
|
|
|
|
targetflags |= CMD_FIND_WINDOW_INDEX;
|
2015-12-13 15:32:38 +01:00
|
|
|
break;
|
2015-12-14 01:31:54 +01:00
|
|
|
case CMD_PANE:
|
|
|
|
case CMD_PANE_CANFAIL:
|
|
|
|
case CMD_PANE_MARKED:
|
|
|
|
if (flag == CMD_PANE_CANFAIL)
|
|
|
|
targetflags |= CMD_FIND_QUIET;
|
|
|
|
if (flag == CMD_PANE_MARKED)
|
|
|
|
targetflags |= CMD_FIND_DEFAULT_MARKED;
|
2016-01-19 17:01:30 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatalx("unknown %cflag %d", c, flag);
|
|
|
|
}
|
2015-12-14 01:31:54 +01:00
|
|
|
|
2016-01-19 17:01:30 +01:00
|
|
|
log_debug("%s: flag %c %d %#x", __func__, c, flag, targetflags);
|
|
|
|
if (parent != NULL) {
|
|
|
|
if (c == 't')
|
|
|
|
current = &parent->state.tflag;
|
|
|
|
else if (c == 's')
|
|
|
|
current = &parent->state.sflag;
|
|
|
|
} else {
|
|
|
|
error = cmd_find_current(&tmp, cmdq, targetflags);
|
|
|
|
if (error != 0 && ~targetflags & CMD_FIND_QUIET)
|
|
|
|
return (-1);
|
|
|
|
current = &tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (flag) {
|
|
|
|
case CMD_NONE:
|
|
|
|
case CMD_CLIENT:
|
|
|
|
case CMD_CLIENT_CANFAIL:
|
|
|
|
return (0);
|
|
|
|
case CMD_SESSION:
|
|
|
|
case CMD_SESSION_CANFAIL:
|
|
|
|
case CMD_SESSION_PREFERUNATTACHED:
|
|
|
|
case CMD_SESSION_WITHPANE:
|
|
|
|
error = cmd_find_target(fs, current, cmdq, target,
|
|
|
|
CMD_FIND_SESSION, targetflags);
|
|
|
|
if (error != 0 && ~targetflags & CMD_FIND_QUIET)
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
case CMD_MOVEW_R:
|
|
|
|
error = cmd_find_target(fs, current, cmdq, target,
|
|
|
|
CMD_FIND_SESSION, CMD_FIND_QUIET);
|
|
|
|
if (error == 0)
|
|
|
|
break;
|
|
|
|
flag = CMD_WINDOW_INDEX;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CMD_WINDOW:
|
|
|
|
case CMD_WINDOW_CANFAIL:
|
|
|
|
case CMD_WINDOW_MARKED:
|
|
|
|
case CMD_WINDOW_INDEX:
|
|
|
|
error = cmd_find_target(fs, current, cmdq, target,
|
|
|
|
CMD_FIND_WINDOW, targetflags);
|
|
|
|
if (error != 0 && ~targetflags & CMD_FIND_QUIET)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
break;
|
2016-01-19 17:01:30 +01:00
|
|
|
case CMD_PANE:
|
|
|
|
case CMD_PANE_CANFAIL:
|
|
|
|
case CMD_PANE_MARKED:
|
|
|
|
error = cmd_find_target(fs, current, cmdq, target,
|
|
|
|
CMD_FIND_PANE, targetflags);
|
|
|
|
if (error != 0 && ~targetflags & CMD_FIND_QUIET)
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatalx("unknown %cflag %d", c, flag);
|
2015-12-13 16:32:12 +01:00
|
|
|
}
|
2015-12-13 15:32:38 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-01-19 17:01:30 +01:00
|
|
|
cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq, struct cmd_q *parent)
|
2015-12-13 15:32:38 +01:00
|
|
|
{
|
2015-12-14 01:31:54 +01:00
|
|
|
const struct cmd_entry *entry = cmd->entry;
|
|
|
|
struct cmd_state *state = &cmdq->state;
|
|
|
|
char *tmp;
|
|
|
|
enum cmd_entry_flag flag;
|
|
|
|
const char *s;
|
|
|
|
int error;
|
2015-12-13 15:32:38 +01:00
|
|
|
|
|
|
|
tmp = cmd_print(cmd);
|
2015-12-14 01:31:54 +01:00
|
|
|
log_debug("preparing state for %s (client %p)", tmp, cmdq->client);
|
2015-12-13 15:32:38 +01:00
|
|
|
free(tmp);
|
|
|
|
|
2015-12-15 00:30:58 +01:00
|
|
|
state->c = NULL;
|
|
|
|
cmd_find_clear_state(&state->tflag, NULL, 0);
|
|
|
|
cmd_find_clear_state(&state->sflag, NULL, 0);
|
2015-12-13 15:32:38 +01:00
|
|
|
|
2015-12-14 01:31:54 +01:00
|
|
|
flag = cmd->entry->cflag;
|
|
|
|
if (flag == CMD_NONE) {
|
|
|
|
flag = cmd->entry->tflag;
|
|
|
|
if (flag == CMD_CLIENT || flag == CMD_CLIENT_CANFAIL)
|
|
|
|
s = args_get(cmd->args, 't');
|
2015-12-13 22:17:37 +01:00
|
|
|
else
|
2015-12-14 01:31:54 +01:00
|
|
|
s = NULL;
|
|
|
|
} else
|
|
|
|
s = args_get(cmd->args, 'c');
|
|
|
|
switch (flag) {
|
|
|
|
case CMD_CLIENT:
|
|
|
|
state->c = cmd_find_client(cmdq, s, 0);
|
|
|
|
if (state->c == NULL)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
default:
|
2015-12-14 01:31:54 +01:00
|
|
|
state->c = cmd_find_client(cmdq, s, 1);
|
|
|
|
break;
|
2015-12-13 15:32:38 +01:00
|
|
|
}
|
|
|
|
|
2015-12-14 01:31:54 +01:00
|
|
|
s = args_get(cmd->args, 't');
|
|
|
|
log_debug("preparing -t state: target %s", s == NULL ? "none" : s);
|
|
|
|
|
2016-01-19 17:01:30 +01:00
|
|
|
error = cmd_prepare_state_flag('t', s, entry->tflag, cmdq, parent);
|
2015-12-14 01:31:54 +01:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
s = args_get(cmd->args, 's');
|
|
|
|
log_debug("preparing -s state: target %s", s == NULL ? "none" : s);
|
|
|
|
|
2016-01-19 17:01:30 +01:00
|
|
|
error = cmd_prepare_state_flag('s', s, entry->sflag, cmdq, parent);
|
2015-12-14 01:31:54 +01:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
return (0);
|
2015-12-13 15:32:38 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 16:06:43 +01:00
|
|
|
char *
|
|
|
|
cmd_print(struct cmd *cmd)
|
2009-01-18 15:40:48 +01:00
|
|
|
{
|
2015-11-27 16:06:43 +01:00
|
|
|
char *out, *s;
|
2011-01-07 15:45:34 +01:00
|
|
|
|
2015-11-27 16:06:43 +01:00
|
|
|
s = args_print(cmd->args);
|
|
|
|
if (*s != '\0')
|
|
|
|
xasprintf(&out, "%s %s", cmd->entry->name, s);
|
|
|
|
else
|
|
|
|
out = xstrdup(cmd->entry->name);
|
|
|
|
free(s);
|
|
|
|
|
|
|
|
return (out);
|
2009-01-18 15:40:48 +01:00
|
|
|
}
|
|
|
|
|
2015-04-19 23:34:21 +02:00
|
|
|
/* Adjust current mouse position for a pane. */
|
|
|
|
int
|
|
|
|
cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
|
|
|
|
u_int *yp, int last)
|
|
|
|
{
|
|
|
|
u_int x, y;
|
|
|
|
|
|
|
|
if (last) {
|
|
|
|
x = m->lx;
|
|
|
|
y = m->ly;
|
|
|
|
} else {
|
|
|
|
x = m->x;
|
|
|
|
y = m->y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->statusat == 0 && y > 0)
|
|
|
|
y--;
|
|
|
|
else if (m->statusat > 0 && y >= (u_int)m->statusat)
|
|
|
|
y = m->statusat - 1;
|
|
|
|
|
|
|
|
if (x < wp->xoff || x >= wp->xoff + wp->sx)
|
|
|
|
return (-1);
|
|
|
|
if (y < wp->yoff || y >= wp->yoff + wp->sy)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
*xp = x - wp->xoff;
|
|
|
|
*yp = y - wp->yoff;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get current mouse window if any. */
|
|
|
|
struct winlink *
|
|
|
|
cmd_mouse_window(struct mouse_event *m, struct session **sp)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct window *w;
|
|
|
|
|
|
|
|
if (!m->valid || m->s == -1 || m->w == -1)
|
|
|
|
return (NULL);
|
|
|
|
if ((s = session_find_by_id(m->s)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if ((w = window_find_by_id(m->w)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
return (winlink_find_by_window(&s->windows, w));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get current mouse pane if any. */
|
|
|
|
struct window_pane *
|
2015-04-27 18:25:57 +02:00
|
|
|
cmd_mouse_pane(struct mouse_event *m, struct session **sp,
|
|
|
|
struct winlink **wlp)
|
2015-04-19 23:34:21 +02:00
|
|
|
{
|
|
|
|
struct winlink *wl;
|
|
|
|
struct window_pane *wp;
|
|
|
|
|
|
|
|
if ((wl = cmd_mouse_window(m, sp)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if ((wp = window_pane_find_by_id(m->wp)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (!window_has_pane(wl->window, wp))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (wlp != NULL)
|
|
|
|
*wlp = wl;
|
|
|
|
return (wp);
|
|
|
|
}
|
|
|
|
|
2009-08-25 15:53:39 +02:00
|
|
|
/* Replace the first %% or %idx in template by s. */
|
|
|
|
char *
|
2013-02-13 11:19:43 +01:00
|
|
|
cmd_template_replace(const char *template, const char *s, int idx)
|
2009-08-25 15:53:39 +02:00
|
|
|
{
|
2013-03-25 13:00:30 +01:00
|
|
|
char ch, *buf;
|
2013-02-13 11:41:12 +01:00
|
|
|
const char *ptr;
|
|
|
|
int replaced;
|
|
|
|
size_t len;
|
2009-08-25 15:53:39 +02:00
|
|
|
|
2013-02-13 12:01:26 +01:00
|
|
|
if (strchr(template, '%') == NULL)
|
2009-08-25 15:53:39 +02:00
|
|
|
return (xstrdup(template));
|
|
|
|
|
|
|
|
buf = xmalloc(1);
|
|
|
|
*buf = '\0';
|
|
|
|
len = 0;
|
|
|
|
replaced = 0;
|
|
|
|
|
|
|
|
ptr = template;
|
|
|
|
while (*ptr != '\0') {
|
|
|
|
switch (ch = *ptr++) {
|
|
|
|
case '%':
|
|
|
|
if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
|
|
|
|
if (*ptr != '%' || replaced)
|
|
|
|
break;
|
|
|
|
replaced = 1;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
len += strlen(s);
|
2014-10-08 19:35:58 +02:00
|
|
|
buf = xrealloc(buf, len + 1);
|
2009-08-25 15:53:39 +02:00
|
|
|
strlcat(buf, s, len + 1);
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-08 19:35:58 +02:00
|
|
|
buf = xrealloc(buf, len + 2);
|
2009-08-25 15:53:39 +02:00
|
|
|
buf[len++] = ch;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (buf);
|
|
|
|
}
|