2009-07-21 13:07:50 +00:00
|
|
|
/* $Id: cmd.c,v 1.106 2009-07-21 13:07:50 nicm Exp $ */
|
2007-10-03 10:18:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2008-06-03 18:13:54 +00:00
|
|
|
#include <sys/time.h>
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
#include <fnmatch.h>
|
2008-06-05 21:25:00 +00:00
|
|
|
#include <stdlib.h>
|
2007-10-03 11:26:34 +00:00
|
|
|
#include <string.h>
|
2008-06-02 18:08:17 +00:00
|
|
|
#include <unistd.h>
|
2007-10-03 10:18:32 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
const struct cmd_entry *cmd_table[] = {
|
2007-10-04 11:52:03 +00:00
|
|
|
&cmd_attach_session_entry,
|
2007-10-04 09:30:53 +00:00
|
|
|
&cmd_bind_key_entry,
|
2009-03-07 09:29:54 +00:00
|
|
|
&cmd_break_pane_entry,
|
2009-01-15 19:27:31 +00:00
|
|
|
&cmd_choose_session_entry,
|
|
|
|
&cmd_choose_window_entry,
|
2009-05-14 16:56:23 +00:00
|
|
|
&cmd_clear_history_entry,
|
2009-01-10 19:35:40 +00:00
|
|
|
&cmd_clock_mode_entry,
|
2008-06-19 20:45:21 +00:00
|
|
|
&cmd_command_prompt_entry,
|
2009-04-27 13:21:16 +00:00
|
|
|
&cmd_confirm_before_entry,
|
2009-02-03 17:21:19 +00:00
|
|
|
&cmd_copy_buffer_entry,
|
2007-11-22 18:09:43 +00:00
|
|
|
&cmd_copy_mode_entry,
|
2008-06-20 17:31:48 +00:00
|
|
|
&cmd_delete_buffer_entry,
|
2007-11-16 21:12:31 +00:00
|
|
|
&cmd_detach_client_entry,
|
2009-07-17 18:32:54 +00:00
|
|
|
&cmd_display_message_entry,
|
2009-01-14 19:41:15 +00:00
|
|
|
&cmd_down_pane_entry,
|
2009-01-18 17:20:52 +00:00
|
|
|
&cmd_find_window_entry,
|
2007-10-25 17:44:25 +00:00
|
|
|
&cmd_has_session_entry,
|
2009-07-09 18:14:18 +00:00
|
|
|
&cmd_if_shell_entry,
|
2009-01-13 06:50:10 +00:00
|
|
|
&cmd_kill_pane_entry,
|
2008-06-03 05:10:38 +00:00
|
|
|
&cmd_kill_server_entry,
|
2007-11-12 14:21:41 +00:00
|
|
|
&cmd_kill_session_entry,
|
2007-10-19 11:10:35 +00:00
|
|
|
&cmd_kill_window_entry,
|
2007-10-04 09:30:53 +00:00
|
|
|
&cmd_last_window_entry,
|
2007-10-26 13:03:59 +00:00
|
|
|
&cmd_link_window_entry,
|
2008-06-20 17:31:48 +00:00
|
|
|
&cmd_list_buffers_entry,
|
2007-10-23 09:36:19 +00:00
|
|
|
&cmd_list_clients_entry,
|
2008-06-23 22:24:16 +00:00
|
|
|
&cmd_list_commands_entry,
|
2007-10-04 09:30:53 +00:00
|
|
|
&cmd_list_keys_entry,
|
2007-10-03 21:31:07 +00:00
|
|
|
&cmd_list_sessions_entry,
|
2007-10-04 11:23:17 +00:00
|
|
|
&cmd_list_windows_entry,
|
2009-01-25 19:00:10 +00:00
|
|
|
&cmd_load_buffer_entry,
|
2009-01-11 00:48:42 +00:00
|
|
|
&cmd_lock_server_entry,
|
2008-06-25 20:33:20 +00:00
|
|
|
&cmd_move_window_entry,
|
2007-10-03 21:31:07 +00:00
|
|
|
&cmd_new_session_entry,
|
2007-10-03 23:32:26 +00:00
|
|
|
&cmd_new_window_entry,
|
2009-04-01 18:21:42 +00:00
|
|
|
&cmd_next_layout_entry,
|
2007-10-04 09:30:53 +00:00
|
|
|
&cmd_next_window_entry,
|
2007-11-23 17:52:54 +00:00
|
|
|
&cmd_paste_buffer_entry,
|
2009-04-30 21:17:06 +00:00
|
|
|
&cmd_previous_layout_entry,
|
2007-10-04 09:30:53 +00:00
|
|
|
&cmd_previous_window_entry,
|
2007-11-16 21:12:31 +00:00
|
|
|
&cmd_refresh_client_entry,
|
2007-11-09 11:02:01 +00:00
|
|
|
&cmd_rename_session_entry,
|
2007-10-04 10:39:07 +00:00
|
|
|
&cmd_rename_window_entry,
|
2009-04-30 06:01:24 +00:00
|
|
|
&cmd_resize_pane_entry,
|
2009-05-04 17:58:27 +00:00
|
|
|
&cmd_respawn_window_entry,
|
2009-04-03 17:21:46 +00:00
|
|
|
&cmd_rotate_window_entry,
|
2009-01-11 23:14:57 +00:00
|
|
|
&cmd_save_buffer_entry,
|
2007-11-21 13:11:41 +00:00
|
|
|
&cmd_scroll_mode_entry,
|
2009-05-16 11:48:47 +00:00
|
|
|
&cmd_select_layout_entry,
|
2009-01-14 19:56:55 +00:00
|
|
|
&cmd_select_pane_entry,
|
2008-06-25 20:43:14 +00:00
|
|
|
&cmd_select_prompt_entry,
|
2007-10-04 10:54:21 +00:00
|
|
|
&cmd_select_window_entry,
|
2008-06-01 20:20:25 +00:00
|
|
|
&cmd_send_keys_entry,
|
2007-10-12 13:03:58 +00:00
|
|
|
&cmd_send_prefix_entry,
|
2009-01-10 01:30:38 +00:00
|
|
|
&cmd_server_info_entry,
|
2008-06-20 08:36:20 +00:00
|
|
|
&cmd_set_buffer_entry,
|
2007-12-06 09:46:23 +00:00
|
|
|
&cmd_set_option_entry,
|
2009-01-11 00:48:42 +00:00
|
|
|
&cmd_set_password_entry,
|
2008-06-15 08:01:54 +00:00
|
|
|
&cmd_set_window_option_entry,
|
2008-06-20 08:36:20 +00:00
|
|
|
&cmd_show_buffer_entry,
|
2008-06-15 08:01:54 +00:00
|
|
|
&cmd_show_options_entry,
|
2008-06-16 06:10:02 +00:00
|
|
|
&cmd_show_window_options_entry,
|
2008-12-15 21:21:56 +00:00
|
|
|
&cmd_source_file_entry,
|
2009-01-11 23:31:46 +00:00
|
|
|
&cmd_split_window_entry,
|
2008-06-02 21:08:36 +00:00
|
|
|
&cmd_start_server_entry,
|
2009-01-18 12:09:42 +00:00
|
|
|
&cmd_suspend_client_entry,
|
2009-04-02 23:28:16 +00:00
|
|
|
&cmd_swap_pane_entry,
|
2007-10-30 11:10:33 +00:00
|
|
|
&cmd_swap_window_entry,
|
2007-11-16 21:31:03 +00:00
|
|
|
&cmd_switch_client_entry,
|
2007-10-04 09:30:53 +00:00
|
|
|
&cmd_unbind_key_entry,
|
2007-10-26 16:57:32 +00:00
|
|
|
&cmd_unlink_window_entry,
|
2009-01-14 19:41:15 +00:00
|
|
|
&cmd_up_pane_entry,
|
2007-10-03 21:31:07 +00:00
|
|
|
NULL
|
2007-10-03 11:26:34 +00:00
|
|
|
};
|
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
struct session *cmd_newest_session(void);
|
|
|
|
struct client *cmd_lookup_client(const char *);
|
|
|
|
struct session *cmd_lookup_session(const char *, int *);
|
|
|
|
struct winlink *cmd_lookup_window(struct session *, const char *, int *);
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
struct cmd *
|
|
|
|
cmd_parse(int argc, char **argv, char **cause)
|
2007-10-03 11:26:34 +00:00
|
|
|
{
|
2007-10-04 22:04:01 +00:00
|
|
|
const struct cmd_entry **entryp, *entry;
|
2007-10-03 21:31:07 +00:00
|
|
|
struct cmd *cmd;
|
2007-11-16 13:23:59 +00:00
|
|
|
char s[BUFSIZ];
|
2009-07-08 18:01:55 +00:00
|
|
|
int opt, ambiguous = 0;
|
2007-10-03 21:31:07 +00:00
|
|
|
|
|
|
|
*cause = NULL;
|
2009-06-25 15:56:39 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
xasprintf(cause, "no command");
|
2007-10-03 21:31:07 +00:00
|
|
|
return (NULL);
|
2009-06-25 15:56:39 +00:00
|
|
|
}
|
2007-10-03 21:31:07 +00:00
|
|
|
|
|
|
|
entry = NULL;
|
2007-10-04 22:04:01 +00:00
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
2007-12-06 09:46:23 +00:00
|
|
|
if ((*entryp)->alias != NULL &&
|
2007-10-12 13:51:44 +00:00
|
|
|
strcmp((*entryp)->alias, argv[0]) == 0) {
|
2009-07-08 18:01:55 +00:00
|
|
|
ambiguous = 0;
|
2007-10-04 22:04:01 +00:00
|
|
|
entry = *entryp;
|
2007-10-03 11:26:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2007-10-04 22:04:01 +00:00
|
|
|
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
|
2007-10-03 21:31:07 +00:00
|
|
|
continue;
|
2007-11-16 13:23:59 +00:00
|
|
|
if (entry != NULL)
|
2009-07-08 18:01:55 +00:00
|
|
|
ambiguous = 1;
|
2007-10-04 22:04:01 +00:00
|
|
|
entry = *entryp;
|
2008-07-19 10:07:50 +00:00
|
|
|
|
|
|
|
/* Bail now if an exact match. */
|
|
|
|
if (strcmp(entry->name, argv[0]) == 0)
|
|
|
|
break;
|
2007-10-03 11:26:34 +00:00
|
|
|
}
|
2009-07-08 18:01:55 +00:00
|
|
|
if (ambiguous)
|
|
|
|
goto ambiguous;
|
2007-10-03 21:31:07 +00:00
|
|
|
if (entry == NULL) {
|
|
|
|
xasprintf(cause, "unknown command: %s", argv[0]);
|
|
|
|
return (NULL);
|
2007-10-03 12:34:16 +00:00
|
|
|
}
|
2009-01-10 01:51:22 +00:00
|
|
|
|
2008-12-10 20:25:42 +00:00
|
|
|
optreset = 1;
|
2007-10-03 21:31:07 +00:00
|
|
|
optind = 1;
|
|
|
|
if (entry->parse == NULL) {
|
2008-12-10 20:25:42 +00:00
|
|
|
while ((opt = getopt(argc, argv, "")) != -1) {
|
2007-10-03 21:31:07 +00:00
|
|
|
switch (opt) {
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
2007-10-03 11:26:34 +00:00
|
|
|
}
|
2007-10-03 21:31:07 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 0)
|
|
|
|
goto usage;
|
2007-10-03 11:26:34 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd = xmalloc(sizeof *cmd);
|
|
|
|
cmd->entry = entry;
|
2008-06-16 07:01:41 +00:00
|
|
|
cmd->data = NULL;
|
2007-10-03 21:31:07 +00:00
|
|
|
if (entry->parse != NULL) {
|
2008-06-05 16:35:32 +00:00
|
|
|
if (entry->parse(cmd, argc, argv, cause) != 0) {
|
2007-10-03 21:31:07 +00:00
|
|
|
xfree(cmd);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (cmd);
|
2007-10-03 11:26:34 +00:00
|
|
|
|
2007-11-16 13:23:59 +00: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 21:31:07 +00:00
|
|
|
usage:
|
2008-06-02 21:08:36 +00:00
|
|
|
xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
|
2007-10-03 21:31:07 +00:00
|
|
|
return (NULL);
|
2007-10-03 11:26:34 +00:00
|
|
|
}
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2009-01-19 18:23:40 +00:00
|
|
|
int
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
|
2007-10-03 10:18:32 +00:00
|
|
|
{
|
2009-01-11 00:48:42 +00:00
|
|
|
if (server_locked) {
|
|
|
|
ctx->error(ctx, "server is locked");
|
2009-01-19 18:23:40 +00:00
|
|
|
return (-1);
|
2009-01-11 00:48:42 +00:00
|
|
|
}
|
2009-01-19 18:23:40 +00:00
|
|
|
return (cmd->entry->exec(cmd, ctx));
|
2007-10-03 10:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd_send(struct cmd *cmd, struct buffer *b)
|
2007-10-03 10:18:32 +00:00
|
|
|
{
|
2007-10-04 22:04:01 +00:00
|
|
|
const struct cmd_entry **entryp;
|
|
|
|
u_int n;
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-04 22:04:01 +00:00
|
|
|
n = 0;
|
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
|
|
|
if (*entryp == cmd->entry)
|
|
|
|
break;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (*entryp == NULL)
|
|
|
|
fatalx("command not found");
|
|
|
|
|
|
|
|
buffer_write(b, &n, sizeof n);
|
|
|
|
|
|
|
|
if (cmd->entry->send != NULL)
|
2008-06-05 16:35:32 +00:00
|
|
|
cmd->entry->send(cmd, b);
|
2007-10-03 10:18:32 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
struct cmd *
|
|
|
|
cmd_recv(struct buffer *b)
|
2007-10-03 10:18:32 +00:00
|
|
|
{
|
2007-10-04 22:04:01 +00:00
|
|
|
const struct cmd_entry **entryp;
|
2007-10-03 21:31:07 +00:00
|
|
|
struct cmd *cmd;
|
2007-10-04 22:04:01 +00:00
|
|
|
u_int m, n;
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2007-10-04 22:04:01 +00:00
|
|
|
buffer_read(b, &m, sizeof m);
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2007-10-04 22:04:01 +00:00
|
|
|
n = 0;
|
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
|
|
|
if (n == m)
|
2007-10-03 21:31:07 +00:00
|
|
|
break;
|
2007-10-04 22:04:01 +00:00
|
|
|
n++;
|
2007-10-03 21:31:07 +00:00
|
|
|
}
|
2007-10-04 22:04:01 +00:00
|
|
|
if (*entryp == NULL)
|
|
|
|
fatalx("command not found");
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd = xmalloc(sizeof *cmd);
|
2007-10-04 22:04:01 +00:00
|
|
|
cmd->entry = *entryp;
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
if (cmd->entry->recv != NULL)
|
2008-06-05 16:35:32 +00:00
|
|
|
cmd->entry->recv(cmd, b);
|
2007-10-03 21:31:07 +00:00
|
|
|
return (cmd);
|
2007-10-03 10:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd_free(struct cmd *cmd)
|
2007-10-03 10:18:32 +00:00
|
|
|
{
|
2007-10-04 09:30:53 +00:00
|
|
|
if (cmd->data != NULL && cmd->entry->free != NULL)
|
2008-06-05 16:35:32 +00:00
|
|
|
cmd->entry->free(cmd);
|
2007-10-03 21:31:07 +00:00
|
|
|
xfree(cmd);
|
2007-10-03 10:18:32 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 14:40:48 +00:00
|
|
|
size_t
|
|
|
|
cmd_print(struct cmd *cmd, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
if (cmd->entry->print == NULL) {
|
|
|
|
return (xsnprintf(buf, len, "%s", cmd->entry->name));
|
|
|
|
}
|
|
|
|
return (cmd->entry->print(cmd, buf, len));
|
|
|
|
}
|
|
|
|
|
2007-10-03 10:18:32 +00:00
|
|
|
void
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd_send_string(struct buffer *b, const char *s)
|
2007-10-03 10:18:32 +00:00
|
|
|
{
|
2007-10-03 21:31:07 +00:00
|
|
|
size_t n;
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
n = 0;
|
|
|
|
buffer_write(b, &n, sizeof n);
|
|
|
|
return;
|
|
|
|
}
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
n = strlen(s) + 1;
|
|
|
|
buffer_write(b, &n, sizeof n);
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
buffer_write(b, s, n);
|
2007-10-03 10:18:32 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
char *
|
|
|
|
cmd_recv_string(struct buffer *b)
|
2007-10-03 10:18:32 +00:00
|
|
|
{
|
2007-10-03 21:31:07 +00:00
|
|
|
char *s;
|
|
|
|
size_t n;
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
buffer_read(b, &n, sizeof n);
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
if (n == 0)
|
|
|
|
return (NULL);
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
s = xmalloc(n);
|
|
|
|
buffer_read(b, s, n);
|
|
|
|
s[n - 1] = '\0';
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
return (s);
|
2007-10-03 10:18:32 +00:00
|
|
|
}
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/*
|
|
|
|
* Figure out the current session. Use: 1) the current session, if the command
|
|
|
|
* context has one; 2) the session specified in the TMUX variable from the
|
|
|
|
* environment (as passed from the client); 3) the newest session.
|
|
|
|
*/
|
2008-06-03 16:55:09 +00:00
|
|
|
struct session *
|
2008-06-05 21:25:00 +00:00
|
|
|
cmd_current_session(struct cmd_ctx *ctx)
|
2008-06-03 16:55:09 +00:00
|
|
|
{
|
2008-06-02 18:08:17 +00:00
|
|
|
struct msg_command_data *data = ctx->msgdata;
|
2009-07-14 06:42:06 +00:00
|
|
|
struct session *s;
|
2008-06-02 18:08:17 +00:00
|
|
|
|
|
|
|
if (ctx->cursession != NULL)
|
|
|
|
return (ctx->cursession);
|
|
|
|
|
|
|
|
if (data != NULL && data->pid != -1) {
|
2009-07-14 06:42:06 +00:00
|
|
|
if (data->pid != getpid())
|
2008-06-02 18:08:17 +00:00
|
|
|
return (NULL);
|
2009-07-14 06:42:06 +00:00
|
|
|
if (data->idx > ARRAY_LENGTH(&sessions))
|
2008-06-02 18:08:17 +00:00
|
|
|
return (NULL);
|
2009-07-14 06:42:06 +00:00
|
|
|
if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL)
|
2008-06-02 18:08:17 +00:00
|
|
|
return (NULL);
|
|
|
|
return (s);
|
|
|
|
}
|
2008-06-18 22:21:51 +00:00
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
return (cmd_newest_session());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the newest session. */
|
|
|
|
struct session *
|
|
|
|
cmd_newest_session(void)
|
|
|
|
{
|
|
|
|
struct session *s, *snewest;
|
|
|
|
struct timeval *tv = NULL;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
snewest = NULL;
|
2008-06-02 18:08:17 +00:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
2009-07-14 06:42:06 +00:00
|
|
|
if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tv == NULL || timercmp(&s->tv, tv, >)) {
|
|
|
|
snewest = s;
|
2008-08-28 17:45:30 +00:00
|
|
|
tv = &s->tv;
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-14 06:42:06 +00:00
|
|
|
|
|
|
|
return (snewest);
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/* Find the target client or report an error and return NULL. */
|
2008-06-05 21:25:00 +00:00
|
|
|
struct client *
|
|
|
|
cmd_find_client(struct cmd_ctx *ctx, const char *arg)
|
|
|
|
{
|
|
|
|
struct client *c;
|
2009-07-14 06:42:06 +00:00
|
|
|
char *tmparg;
|
|
|
|
size_t arglen;
|
2008-06-18 22:21:51 +00:00
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/* A NULL argument means the current client. */
|
2008-06-27 17:10:01 +00:00
|
|
|
if (arg == NULL)
|
2009-07-14 06:42:06 +00:00
|
|
|
return (ctx->curclient);
|
|
|
|
tmparg = xstrdup(arg);
|
|
|
|
|
|
|
|
/* Trim a single trailing colon if any. */
|
|
|
|
arglen = strlen(tmparg);
|
|
|
|
if (arglen != 0 && tmparg[arglen - 1] == ':')
|
|
|
|
tmparg[arglen - 1] = '\0';
|
|
|
|
|
|
|
|
/* Find the client, if any. */
|
|
|
|
c = cmd_lookup_client(tmparg);
|
|
|
|
|
|
|
|
/* If no client found, report an error. */
|
|
|
|
if (c == NULL)
|
|
|
|
ctx->error(ctx, "client not found: %s", tmparg);
|
|
|
|
|
|
|
|
xfree(tmparg);
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup a client by device path. Either of a full match and a match without a
|
|
|
|
* leading _PATH_DEV ("/dev/") is accepted.
|
|
|
|
*/
|
|
|
|
struct client *
|
|
|
|
cmd_lookup_client(const char *name)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
const char *path;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
if ((c = ARRAY_ITEM(&clients, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
path = c->tty.path;
|
|
|
|
|
|
|
|
/* Check for exact matches. */
|
|
|
|
if (strcmp(name, path) == 0)
|
|
|
|
return (c);
|
|
|
|
|
|
|
|
/* Check without leading /dev if present. */
|
|
|
|
if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
|
|
|
|
continue;
|
|
|
|
if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup a session by name. If no session is found, NULL is returned. */
|
|
|
|
struct session *
|
|
|
|
cmd_lookup_session(const char *name, int *ambiguous)
|
|
|
|
{
|
|
|
|
struct session *s, *sfound;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
*ambiguous = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for matches. Session names must be unique so an exact match
|
|
|
|
* can't be ambigious and can just be returned.
|
|
|
|
*/
|
|
|
|
sfound = NULL;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Check for an exact match and return it if found. */
|
|
|
|
if (strcmp(name, s->name) == 0)
|
|
|
|
return (s);
|
|
|
|
|
|
|
|
/* Then check for pattern matches. */
|
|
|
|
if (strncmp(name, s->name, strlen(name)) == 0 ||
|
|
|
|
fnmatch(name, s->name, 0) == 0) {
|
|
|
|
if (sfound != NULL) {
|
|
|
|
*ambiguous = 1;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
sfound = s;
|
2008-06-27 17:10:01 +00:00
|
|
|
}
|
2008-06-16 22:03:27 +00:00
|
|
|
}
|
2009-07-14 06:42:06 +00:00
|
|
|
|
|
|
|
return (sfound);
|
2008-06-05 21:25:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/*
|
|
|
|
* Lookup a window or return -1 if not found or ambigious. First try as an index
|
|
|
|
* and if invalid, use fnmatch or leading prefix.
|
|
|
|
*/
|
|
|
|
struct winlink *
|
|
|
|
cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
|
|
|
|
{
|
|
|
|
struct winlink *wl, *wlfound;
|
|
|
|
struct window *w;
|
|
|
|
const char *errstr;
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
*ambiguous = 0;
|
|
|
|
|
|
|
|
/* First see if this is a valid window index in this session. */
|
|
|
|
idx = strtonum(name, 0, INT_MAX, &errstr);
|
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
|
|
|
|
return (wl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for exact matches, error if more than one. */
|
|
|
|
wlfound = NULL;
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
w = wl->window;
|
|
|
|
if (strcmp(name, w->name) == 0) {
|
|
|
|
if (wlfound != NULL) {
|
|
|
|
*ambiguous = 1;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
wlfound = wl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wlfound != NULL)
|
|
|
|
return (wlfound);
|
|
|
|
|
|
|
|
/* Now look for pattern matches, again error if multiple. */
|
|
|
|
wlfound = NULL;
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
w = wl->window;
|
|
|
|
if (strncmp(name, w->name, strlen(name)) == 0 ||
|
|
|
|
fnmatch(name, w->name, 0) == 0) {
|
|
|
|
if (wlfound != NULL) {
|
|
|
|
*ambiguous = 1;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
wlfound = wl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wlfound != NULL)
|
|
|
|
return (wlfound);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the target session or report an error and return NULL. */
|
2008-06-05 21:25:00 +00:00
|
|
|
struct session *
|
|
|
|
cmd_find_session(struct cmd_ctx *ctx, const char *arg)
|
|
|
|
{
|
|
|
|
struct session *s;
|
2009-07-14 06:42:06 +00:00
|
|
|
struct client *c;
|
|
|
|
char *tmparg;
|
|
|
|
size_t arglen;
|
|
|
|
int ambiguous;
|
2008-06-05 21:25:00 +00:00
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/* A NULL argument means the current session. */
|
2008-06-27 17:10:01 +00:00
|
|
|
if (arg == NULL)
|
2009-07-14 06:42:06 +00:00
|
|
|
return (cmd_current_session(ctx));
|
|
|
|
tmparg = xstrdup(arg);
|
|
|
|
|
|
|
|
/* Trim a single trailing colon if any. */
|
|
|
|
arglen = strlen(tmparg);
|
|
|
|
if (arglen != 0 && tmparg[arglen - 1] == ':')
|
|
|
|
tmparg[arglen - 1] = '\0';
|
|
|
|
|
|
|
|
/* Find the session, if any. */
|
|
|
|
s = cmd_lookup_session(tmparg, &ambiguous);
|
|
|
|
|
|
|
|
/* If it doesn't, try to match it as a client. */
|
|
|
|
if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
|
|
|
|
s = c->session;
|
|
|
|
|
|
|
|
/* If no session found, report an error. */
|
|
|
|
if (s == NULL) {
|
|
|
|
if (ambiguous)
|
|
|
|
ctx->error(ctx, "more than one session: %s", tmparg);
|
|
|
|
else
|
|
|
|
ctx->error(ctx, "session not found: %s", tmparg);
|
2008-06-16 22:03:27 +00:00
|
|
|
}
|
2009-07-14 06:42:06 +00:00
|
|
|
|
|
|
|
xfree(tmparg);
|
2008-06-05 21:25:00 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/* Find the target session and window or report an error and return NULL. */
|
2008-06-03 16:55:09 +00:00
|
|
|
struct winlink *
|
2008-06-05 21:25:00 +00:00
|
|
|
cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
|
2008-06-02 18:08:17 +00:00
|
|
|
{
|
2008-06-05 21:25:00 +00:00
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
2009-07-14 06:42:06 +00:00
|
|
|
const char *winptr;
|
|
|
|
char *sessptr = NULL;
|
|
|
|
int ambiguous = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the current session. There must always be a current session, if
|
|
|
|
* it can't be found, report an error.
|
|
|
|
*/
|
|
|
|
if ((s = cmd_current_session(ctx)) == NULL) {
|
|
|
|
ctx->error(ctx, "can't establish current session");
|
2009-07-17 15:56:46 +00:00
|
|
|
return (NULL);
|
2008-06-05 21:25:00 +00:00
|
|
|
}
|
2009-07-14 06:42:06 +00:00
|
|
|
|
|
|
|
/* A NULL argument means the current session and window. */
|
|
|
|
if (arg == NULL) {
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
return (s->curw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Time to look at the argument. If it is empty, that is an error. */
|
|
|
|
if (*arg == '\0')
|
|
|
|
goto not_found;
|
|
|
|
|
|
|
|
/* Find the separating colon. If none, assume the current session. */
|
|
|
|
winptr = strchr(arg, ':');
|
|
|
|
if (winptr == NULL)
|
|
|
|
winptr = xstrdup(arg);
|
|
|
|
else {
|
|
|
|
winptr++; /* skip : */
|
|
|
|
sessptr = xstrdup(arg);
|
|
|
|
*strchr(sessptr, ':') = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("%s: winptr=%s, sessptr=%s", __func__, winptr, sessptr);
|
|
|
|
|
|
|
|
/* Try to lookup the session if present. */
|
|
|
|
if (sessptr != NULL && *sessptr != '\0') {
|
|
|
|
if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
|
|
|
|
goto no_session;
|
|
|
|
}
|
2008-06-03 16:55:09 +00:00
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
2008-06-05 22:59:38 +00:00
|
|
|
|
2009-07-14 06:42:06 +00:00
|
|
|
/*
|
|
|
|
* Then work out the window. An empty string is the current window,
|
|
|
|
* otherwise try to look it up in the session.
|
|
|
|
*/
|
|
|
|
if (winptr == NULL || *winptr == '\0')
|
2008-06-05 22:59:38 +00:00
|
|
|
wl = s->curw;
|
2009-07-14 06:42:06 +00:00
|
|
|
else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL)
|
|
|
|
goto not_found;
|
|
|
|
|
|
|
|
if (sessptr != NULL)
|
|
|
|
xfree(sessptr);
|
2008-06-03 16:55:09 +00:00
|
|
|
return (wl);
|
2009-07-14 06:42:06 +00:00
|
|
|
|
|
|
|
no_session:
|
|
|
|
if (ambiguous)
|
|
|
|
ctx->error(ctx, "multiple sessions: %s", sessptr);
|
|
|
|
else
|
|
|
|
ctx->error(ctx, "session not found: %s", sessptr);
|
|
|
|
if (sessptr != NULL)
|
|
|
|
xfree(sessptr);
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
not_found:
|
|
|
|
if (ambiguous)
|
|
|
|
ctx->error(ctx, "multiple windows: %s", arg);
|
|
|
|
else
|
|
|
|
ctx->error(ctx, "window not found: %s", arg);
|
|
|
|
if (sessptr != NULL)
|
|
|
|
xfree(sessptr);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the target session and window index, whether or not it exists in the
|
|
|
|
* session. Return -2 on error or -1 if no window index is specified. This is
|
|
|
|
* used when parsing an argument for a window target that may not be exist (for
|
|
|
|
* example it is going to be created).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
|
|
|
const char *winptr, *errstr;
|
|
|
|
char *sessptr = NULL;
|
|
|
|
int idx, ambiguous = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the current session. There must always be a current session, if
|
|
|
|
* it can't be found, report an error.
|
|
|
|
*/
|
|
|
|
if ((s = cmd_current_session(ctx)) == NULL) {
|
|
|
|
ctx->error(ctx, "can't establish current session");
|
2009-07-15 17:45:29 +00:00
|
|
|
return (-1);
|
2009-07-14 06:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A NULL argument means the current session and "no window" (-1). */
|
|
|
|
if (arg == NULL) {
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Time to look at the argument. If it is empty, that is an error. */
|
|
|
|
if (*arg == '\0')
|
|
|
|
goto not_found;
|
|
|
|
|
|
|
|
/* Find the separating colon. If none, assume the current session. */
|
|
|
|
winptr = strchr(arg, ':');
|
|
|
|
if (winptr == NULL)
|
|
|
|
winptr = xstrdup(arg);
|
|
|
|
else {
|
|
|
|
winptr++; /* skip : */
|
|
|
|
sessptr = xstrdup(arg);
|
|
|
|
*strchr(sessptr, ':') = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("%s: winptr=%s, sessptr=%s", __func__, winptr, sessptr);
|
|
|
|
|
|
|
|
/* Try to lookup the session if present. */
|
|
|
|
if (sessptr != NULL && *sessptr != '\0') {
|
|
|
|
if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
|
|
|
|
goto no_session;
|
|
|
|
}
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Then work out the window. No : means "no window" (-1), an empty
|
|
|
|
* string is the current window, otherwise try to look it up in the
|
|
|
|
* session.
|
|
|
|
*/
|
|
|
|
if (winptr == NULL)
|
|
|
|
idx = -1;
|
|
|
|
else if (*winptr == '\0')
|
|
|
|
idx = s->curw->idx;
|
|
|
|
else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL) {
|
|
|
|
if (ambiguous)
|
|
|
|
goto not_found;
|
|
|
|
/* Don't care it doesn't exist if this is a valid index. */
|
|
|
|
idx = strtonum(winptr, 0, INT_MAX, &errstr);
|
|
|
|
if (errstr != NULL) {
|
|
|
|
ctx->error(ctx, "index %s: %s", errstr, winptr);
|
|
|
|
idx = -2;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
idx = wl->idx;
|
|
|
|
|
|
|
|
if (sessptr != NULL)
|
|
|
|
xfree(sessptr);
|
|
|
|
return (idx);
|
|
|
|
|
|
|
|
no_session:
|
|
|
|
if (ambiguous)
|
|
|
|
ctx->error(ctx, "multiple sessions: %s", sessptr);
|
|
|
|
else
|
|
|
|
ctx->error(ctx, "session not found: %s", sessptr);
|
|
|
|
if (sessptr != NULL)
|
|
|
|
xfree(sessptr);
|
|
|
|
return (-2);
|
|
|
|
|
|
|
|
not_found:
|
|
|
|
if (ambiguous)
|
|
|
|
ctx->error(ctx, "multiple windows: %s", arg);
|
|
|
|
else
|
|
|
|
ctx->error(ctx, "window not found: %s", arg);
|
|
|
|
if (sessptr != NULL)
|
|
|
|
xfree(sessptr);
|
|
|
|
return (-2);
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|