2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2009-07-13 19:47:46 +02:00
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <paths.h>
|
2012-01-31 16:52:21 +01:00
|
|
|
#include <pwd.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2009-06-02 00:58:49 +02:00
|
|
|
const struct cmd_entry *cmd_table[] = {
|
|
|
|
&cmd_attach_session_entry,
|
|
|
|
&cmd_bind_key_entry,
|
|
|
|
&cmd_break_pane_entry,
|
2009-12-01 19:42:38 +01:00
|
|
|
&cmd_capture_pane_entry,
|
2010-06-21 23:44:09 +02:00
|
|
|
&cmd_choose_buffer_entry,
|
2009-08-25 14:18:51 +02:00
|
|
|
&cmd_choose_client_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_choose_session_entry,
|
2012-07-08 18:04:38 +02:00
|
|
|
&cmd_choose_tree_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_choose_window_entry,
|
|
|
|
&cmd_clear_history_entry,
|
|
|
|
&cmd_clock_mode_entry,
|
|
|
|
&cmd_command_prompt_entry,
|
|
|
|
&cmd_confirm_before_entry,
|
|
|
|
&cmd_copy_mode_entry,
|
|
|
|
&cmd_delete_buffer_entry,
|
|
|
|
&cmd_detach_client_entry,
|
2009-07-17 20:45:08 +02:00
|
|
|
&cmd_display_message_entry,
|
2009-08-31 22:46:19 +02:00
|
|
|
&cmd_display_panes_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_find_window_entry,
|
|
|
|
&cmd_has_session_entry,
|
2009-07-09 17:47:49 +02:00
|
|
|
&cmd_if_shell_entry,
|
2010-01-07 21:52:18 +01:00
|
|
|
&cmd_join_pane_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_kill_pane_entry,
|
|
|
|
&cmd_kill_server_entry,
|
|
|
|
&cmd_kill_session_entry,
|
|
|
|
&cmd_kill_window_entry,
|
2010-10-23 15:04:34 +02:00
|
|
|
&cmd_last_pane_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_last_window_entry,
|
|
|
|
&cmd_link_window_entry,
|
|
|
|
&cmd_list_buffers_entry,
|
|
|
|
&cmd_list_clients_entry,
|
|
|
|
&cmd_list_commands_entry,
|
|
|
|
&cmd_list_keys_entry,
|
2009-10-10 19:19:38 +02:00
|
|
|
&cmd_list_panes_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_list_sessions_entry,
|
|
|
|
&cmd_list_windows_entry,
|
|
|
|
&cmd_load_buffer_entry,
|
2009-09-24 16:17:09 +02:00
|
|
|
&cmd_lock_client_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_lock_server_entry,
|
2009-09-24 16:17:09 +02:00
|
|
|
&cmd_lock_session_entry,
|
2012-03-03 09:31:18 +01:00
|
|
|
&cmd_move_pane_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_move_window_entry,
|
|
|
|
&cmd_new_session_entry,
|
|
|
|
&cmd_new_window_entry,
|
|
|
|
&cmd_next_layout_entry,
|
|
|
|
&cmd_next_window_entry,
|
|
|
|
&cmd_paste_buffer_entry,
|
2009-10-11 12:04:27 +02:00
|
|
|
&cmd_pipe_pane_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_previous_layout_entry,
|
|
|
|
&cmd_previous_window_entry,
|
|
|
|
&cmd_refresh_client_entry,
|
|
|
|
&cmd_rename_session_entry,
|
|
|
|
&cmd_rename_window_entry,
|
|
|
|
&cmd_resize_pane_entry,
|
2011-06-05 13:19:03 +02:00
|
|
|
&cmd_respawn_pane_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_respawn_window_entry,
|
|
|
|
&cmd_rotate_window_entry,
|
2009-09-20 21:15:01 +02:00
|
|
|
&cmd_run_shell_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_save_buffer_entry,
|
|
|
|
&cmd_select_layout_entry,
|
|
|
|
&cmd_select_pane_entry,
|
|
|
|
&cmd_select_window_entry,
|
|
|
|
&cmd_send_keys_entry,
|
|
|
|
&cmd_send_prefix_entry,
|
|
|
|
&cmd_server_info_entry,
|
|
|
|
&cmd_set_buffer_entry,
|
2009-08-08 23:52:43 +02:00
|
|
|
&cmd_set_environment_entry,
|
2015-12-08 02:10:31 +01:00
|
|
|
&cmd_set_hook_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_set_option_entry,
|
|
|
|
&cmd_set_window_option_entry,
|
|
|
|
&cmd_show_buffer_entry,
|
2009-08-08 23:52:43 +02:00
|
|
|
&cmd_show_environment_entry,
|
2015-12-08 02:10:31 +01:00
|
|
|
&cmd_show_hooks_entry,
|
2009-11-18 14:16:33 +01:00
|
|
|
&cmd_show_messages_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
&cmd_show_options_entry,
|
|
|
|
&cmd_show_window_options_entry,
|
|
|
|
&cmd_source_file_entry,
|
|
|
|
&cmd_split_window_entry,
|
|
|
|
&cmd_start_server_entry,
|
|
|
|
&cmd_suspend_client_entry,
|
|
|
|
&cmd_swap_pane_entry,
|
|
|
|
&cmd_swap_window_entry,
|
|
|
|
&cmd_switch_client_entry,
|
|
|
|
&cmd_unbind_key_entry,
|
|
|
|
&cmd_unlink_window_entry,
|
2013-03-25 11:09:05 +01:00
|
|
|
&cmd_wait_for_entry,
|
2009-06-02 00:58:49 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-12-13 19:27:47 +01:00
|
|
|
static void cmd_clear_state(struct cmd_state *);
|
|
|
|
static int cmd_set_state_flag(struct cmd *, struct cmd_q *, char);
|
2015-12-13 15:32:38 +01:00
|
|
|
|
2009-07-26 14:58:44 +02:00
|
|
|
int
|
|
|
|
cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t arglen;
|
|
|
|
int i;
|
|
|
|
|
2014-03-31 23:32:31 +02:00
|
|
|
if (argc == 0)
|
|
|
|
return (0);
|
|
|
|
|
2009-07-26 14:58:44 +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-10-29 22:11:57 +02:00
|
|
|
char **
|
2014-05-09 11:11:24 +02:00
|
|
|
cmd_copy_argv(int argc, char **argv)
|
2010-10-29 22:11:57 +02: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-10-29 22:11:57 +02:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] != NULL)
|
|
|
|
new_argv[i] = xstrdup(argv[i]);
|
|
|
|
}
|
|
|
|
return (new_argv);
|
|
|
|
}
|
|
|
|
|
2009-07-26 14:58:44 +02:00
|
|
|
void
|
|
|
|
cmd_free_argv(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc == 0)
|
2009-12-03 23:50:09 +01:00
|
|
|
return;
|
2012-07-10 13:53:01 +02:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
free(argv[i]);
|
|
|
|
free(argv);
|
2009-07-26 14:58:44 +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);
|
|
|
|
}
|
|
|
|
|
2009-06-02 00:58:49 +02:00
|
|
|
struct cmd *
|
2013-03-24 10:54:10 +01:00
|
|
|
cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
|
|
|
const struct cmd_entry **entryp, *entry;
|
2009-10-26 22:42:04 +01:00
|
|
|
struct cmd *cmd;
|
2011-01-04 01:42:46 +01:00
|
|
|
struct args *args;
|
2009-06-02 00:58:49 +02:00
|
|
|
char s[BUFSIZ];
|
2011-01-04 01:42:46 +01:00
|
|
|
int ambiguous = 0;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
*cause = NULL;
|
2009-06-05 01:34:32 +02:00
|
|
|
if (argc == 0) {
|
|
|
|
xasprintf(cause, "no command");
|
2009-06-02 00:58:49 +02:00
|
|
|
return (NULL);
|
2009-06-05 01:34:32 +02:00
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
entry = NULL;
|
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
|
|
|
if ((*entryp)->alias != NULL &&
|
|
|
|
strcmp((*entryp)->alias, argv[0]) == 0) {
|
2009-07-07 23:23:22 +02:00
|
|
|
ambiguous = 0;
|
2009-06-02 00:58:49 +02:00
|
|
|
entry = *entryp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
|
|
|
|
continue;
|
|
|
|
if (entry != NULL)
|
2009-07-07 23:23:22 +02:00
|
|
|
ambiguous = 1;
|
2009-06-02 00:58:49 +02:00
|
|
|
entry = *entryp;
|
|
|
|
|
|
|
|
/* Bail now if an exact match. */
|
|
|
|
if (strcmp(entry->name, argv[0]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-07-07 23:23:22 +02:00
|
|
|
if (ambiguous)
|
|
|
|
goto ambiguous;
|
2009-06-02 00:58:49 +02:00
|
|
|
if (entry == NULL) {
|
|
|
|
xasprintf(cause, "unknown command: %s", argv[0]);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2011-01-04 01:42:46 +01:00
|
|
|
args = args_parse(entry->args_template, argc, argv);
|
|
|
|
if (args == NULL)
|
|
|
|
goto usage;
|
|
|
|
if (entry->args_lower != -1 && args->argc < entry->args_lower)
|
|
|
|
goto usage;
|
|
|
|
if (entry->args_upper != -1 && args->argc > entry->args_upper)
|
|
|
|
goto usage;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cmd = xcalloc(1, sizeof *cmd);
|
2009-06-02 00:58:49 +02:00
|
|
|
cmd->entry = entry;
|
2011-01-04 01:42:46 +01:00
|
|
|
cmd->args = args;
|
2013-03-24 10:54:10 +01:00
|
|
|
|
|
|
|
if (file != NULL)
|
|
|
|
cmd->file = xstrdup(file);
|
|
|
|
cmd->line = line;
|
|
|
|
|
2009-06-02 00:58:49 +02:00
|
|
|
return (cmd);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
usage:
|
2011-01-04 01:42:46 +01:00
|
|
|
if (args != NULL)
|
|
|
|
args_free(args);
|
2009-06-02 00:58:49 +02:00
|
|
|
xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2015-12-13 15:32:38 +01:00
|
|
|
static void
|
|
|
|
cmd_clear_state(struct cmd_state *state)
|
|
|
|
{
|
|
|
|
state->c = NULL;
|
|
|
|
|
|
|
|
state->tflag.s = NULL;
|
|
|
|
state->tflag.wl = NULL;
|
|
|
|
state->tflag.wp = NULL;
|
|
|
|
state->tflag.idx = -1;
|
|
|
|
|
|
|
|
state->sflag.s = NULL;
|
|
|
|
state->sflag.wl = NULL;
|
|
|
|
state->sflag.wp = NULL;
|
|
|
|
state->sflag.idx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c)
|
|
|
|
{
|
|
|
|
struct cmd_state *state = &cmdq->state;
|
2015-12-13 18:58:26 +01:00
|
|
|
struct cmd_find_state *fsf = NULL;
|
2015-12-13 15:32:38 +01:00
|
|
|
const char *flag;
|
|
|
|
int flags = cmd->entry->flags, everything = 0;
|
2015-12-13 17:44:35 +01:00
|
|
|
int allflags = 0, targetflags, error;
|
2015-12-13 15:32:38 +01:00
|
|
|
struct session *s;
|
|
|
|
struct window *w;
|
|
|
|
struct winlink *wl;
|
|
|
|
struct window_pane *wp;
|
|
|
|
|
2015-12-13 16:32:12 +01:00
|
|
|
/* Set up state for either -t or -s. */
|
2015-12-13 15:32:38 +01:00
|
|
|
if (c == 't') {
|
2015-12-13 18:58:26 +01:00
|
|
|
fsf = &cmdq->state.tflag;
|
2015-12-13 15:32:38 +01:00
|
|
|
allflags = CMD_ALL_T;
|
|
|
|
} else if (c == 's') {
|
2015-12-13 18:58:26 +01:00
|
|
|
fsf = &cmdq->state.sflag;
|
2015-12-13 15:32:38 +01:00
|
|
|
allflags = CMD_ALL_S;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the command wants something and no argument is present, use the
|
|
|
|
* base command instead.
|
|
|
|
*/
|
|
|
|
flag = args_get(cmd->args, c);
|
|
|
|
if (flag == NULL) {
|
|
|
|
if ((flags & allflags) == 0)
|
|
|
|
return (0); /* doesn't care about flag */
|
|
|
|
cmd = cmdq->cmd;
|
|
|
|
everything = 1;
|
|
|
|
flag = args_get(cmd->args, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If no flag and the current command is allowed to fail, just skip to
|
|
|
|
* fill in as much we can, otherwise continue and fail later if needed.
|
|
|
|
*/
|
|
|
|
if (flag == NULL && (flags & CMD_CANFAIL))
|
|
|
|
goto complete_everything;
|
|
|
|
|
|
|
|
/* Fill in state using command (current or base) flags. */
|
2015-12-13 16:32:12 +01:00
|
|
|
if (flags & CMD_PREFERUNATTACHED)
|
|
|
|
targetflags = CMD_FIND_PREFER_UNATTACHED;
|
|
|
|
else
|
|
|
|
targetflags = 0;
|
2015-12-13 15:32:38 +01:00
|
|
|
switch (cmd->entry->flags & allflags) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CMD_SESSION_T|CMD_PANE_T:
|
|
|
|
case CMD_SESSION_S|CMD_PANE_S:
|
|
|
|
if (flag != NULL && flag[strcspn(flag, ":.")] != '\0') {
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_PANE,
|
2015-12-13 16:32:12 +01:00
|
|
|
targetflags);
|
2015-12-13 17:44:35 +01:00
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
} else {
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag,
|
2015-12-13 17:44:35 +01:00
|
|
|
CMD_FIND_SESSION, targetflags);
|
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (flag == NULL) {
|
2015-12-13 18:58:26 +01:00
|
|
|
fsf->wl = fsf->s->curw;
|
|
|
|
fsf->wp = fsf->s->curw->window->active;
|
2015-12-13 15:32:38 +01:00
|
|
|
} else {
|
2015-12-13 18:58:26 +01:00
|
|
|
s = fsf->s;
|
2015-12-13 15:32:38 +01:00
|
|
|
if ((w = window_find_by_id_str(flag)) != NULL)
|
|
|
|
wp = w->active;
|
|
|
|
else {
|
|
|
|
wp = window_pane_find_by_id_str(flag);
|
|
|
|
if (wp != NULL)
|
|
|
|
w = wp->window;
|
|
|
|
}
|
|
|
|
wl = winlink_find_by_window(&s->windows, w);
|
|
|
|
if (wl != NULL) {
|
2015-12-13 18:58:26 +01:00
|
|
|
fsf->wl = wl;
|
|
|
|
fsf->wp = wp;
|
2015-12-13 15:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CMD_MOVEW_R|CMD_INDEX_T:
|
|
|
|
case CMD_MOVEW_R|CMD_INDEX_S:
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_SESSION,
|
|
|
|
targetflags|CMD_FIND_QUIET);
|
|
|
|
if (error != 0) {
|
|
|
|
error = cmd_find_target(fsf, cmdq, flag,
|
2015-12-13 17:44:35 +01:00
|
|
|
CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX);
|
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CMD_SESSION_T:
|
|
|
|
case CMD_SESSION_S:
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_SESSION,
|
2015-12-13 17:44:35 +01:00
|
|
|
targetflags);
|
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
case CMD_WINDOW_MARKED_T:
|
|
|
|
case CMD_WINDOW_MARKED_S:
|
2015-12-13 16:32:12 +01:00
|
|
|
targetflags |= CMD_FIND_DEFAULT_MARKED;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CMD_WINDOW_T:
|
|
|
|
case CMD_WINDOW_S:
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_WINDOW,
|
2015-12-13 17:44:35 +01:00
|
|
|
targetflags);
|
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
case CMD_PANE_MARKED_T:
|
2015-12-13 16:00:37 +01:00
|
|
|
case CMD_PANE_MARKED_S:
|
2015-12-13 16:32:12 +01:00
|
|
|
targetflags |= CMD_FIND_DEFAULT_MARKED;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CMD_PANE_T:
|
|
|
|
case CMD_PANE_S:
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_PANE,
|
2015-12-13 17:44:35 +01:00
|
|
|
targetflags);
|
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
case CMD_INDEX_T:
|
|
|
|
case CMD_INDEX_S:
|
2015-12-13 18:58:26 +01:00
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_WINDOW,
|
2015-12-13 16:32:12 +01:00
|
|
|
CMD_FIND_WINDOW_INDEX);
|
2015-12-13 17:44:35 +01:00
|
|
|
if (error != 0)
|
2015-12-13 15:32:38 +01:00
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatalx("too many -%c for %s", c, cmd->entry->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is still the current command, it wants what it asked for and
|
|
|
|
* nothing more. If it's the base command, fill in as much as possible
|
|
|
|
* because the current command may have different flags.
|
|
|
|
*/
|
|
|
|
if (!everything)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
complete_everything:
|
2015-12-13 18:58:26 +01:00
|
|
|
if (fsf->s == NULL) {
|
2015-12-13 15:32:38 +01:00
|
|
|
if (state->c != NULL)
|
2015-12-13 18:58:26 +01:00
|
|
|
fsf->s = state->c->session;
|
|
|
|
if (fsf->s == NULL) {
|
|
|
|
error = cmd_find_target(fsf, cmdq, NULL,
|
2015-12-13 17:44:35 +01:00
|
|
|
CMD_FIND_SESSION, CMD_FIND_QUIET);
|
2015-12-13 18:58:26 +01:00
|
|
|
if (error != 0)
|
|
|
|
fsf->s = NULL;
|
2015-12-13 16:32:12 +01:00
|
|
|
}
|
2015-12-13 18:58:26 +01:00
|
|
|
if (fsf->s == NULL) {
|
2015-12-13 15:32:38 +01:00
|
|
|
if (flags & CMD_CANFAIL)
|
|
|
|
return (0);
|
|
|
|
cmdq_error(cmdq, "no current session");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 18:58:26 +01:00
|
|
|
if (fsf->wl == NULL) {
|
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_WINDOW, 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (-1);
|
2015-12-13 16:32:12 +01:00
|
|
|
}
|
2015-12-13 18:58:26 +01:00
|
|
|
if (fsf->wp == NULL) {
|
|
|
|
error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_PANE, 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (-1);
|
2015-12-13 16:32:12 +01:00
|
|
|
}
|
2015-12-13 15:32:38 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
struct cmd_state *state = &cmdq->state;
|
|
|
|
struct args *args = cmd->args;
|
|
|
|
char *tmp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
tmp = cmd_print(cmd);
|
2015-12-13 16:00:37 +01:00
|
|
|
log_debug("preparing state for: %s (client %p)", tmp, cmdq->client);
|
2015-12-13 15:32:38 +01:00
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
/* Start with an empty state. */
|
|
|
|
cmd_clear_state(state);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the command wants a client and provides -c or -t, use it. If not,
|
|
|
|
* try the base command instead via cmd_get_state_client. No client is
|
|
|
|
* allowed if no flags, otherwise it must be available.
|
|
|
|
*/
|
|
|
|
switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
|
|
|
|
case 0:
|
2015-12-13 19:27:47 +01:00
|
|
|
state->c = cmd_find_client(cmdq, NULL, 1);
|
2015-12-13 15:32:38 +01:00
|
|
|
break;
|
|
|
|
case CMD_CLIENT_C:
|
2015-12-13 19:27:47 +01:00
|
|
|
state->c = cmd_find_client(cmdq, args_get(args, 'c'), 0);
|
2015-12-13 15:32:38 +01:00
|
|
|
if (state->c == NULL)
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
case CMD_CLIENT_T:
|
2015-12-13 19:27:47 +01:00
|
|
|
state->c = cmd_find_client(cmdq, args_get(args, 't'), 0);
|
2015-12-13 15:32:38 +01:00
|
|
|
if (state->c == NULL)
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatalx("both -c and -t for %s", cmd->entry->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = cmd_set_state_flag(cmd, cmdq, 't');
|
|
|
|
if (error == 0)
|
|
|
|
error = cmd_set_state_flag(cmd, cmdq, 's');
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2015-11-27 16:06:43 +01:00
|
|
|
char *
|
|
|
|
cmd_print(struct cmd *cmd)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2015-11-27 16:06:43 +01:00
|
|
|
char *out, *s;
|
2011-01-04 01:42:46 +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-06-02 00:58:49 +02: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 14:18:51 +02:00
|
|
|
/* Replace the first %% or %idx in template by s. */
|
|
|
|
char *
|
2013-03-21 17:22:48 +01:00
|
|
|
cmd_template_replace(const char *template, const char *s, int idx)
|
2009-08-25 14:18:51 +02:00
|
|
|
{
|
2013-03-21 17:22:48 +01:00
|
|
|
char ch, *buf;
|
|
|
|
const char *ptr;
|
2013-03-25 12:53:54 +01:00
|
|
|
int replaced;
|
|
|
|
size_t len;
|
2009-08-25 14:18:51 +02:00
|
|
|
|
2013-03-21 17:22:48 +01:00
|
|
|
if (strchr(template, '%') == NULL)
|
2009-08-25 14:18:51 +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 14:18:51 +02:00
|
|
|
strlcat(buf, s, len + 1);
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-08 19:35:58 +02:00
|
|
|
buf = xrealloc(buf, len + 2);
|
2009-08-25 14:18:51 +02:00
|
|
|
buf[len++] = ch;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (buf);
|
|
|
|
}
|