2009-06-01 22:58:49 +00: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 17:47:46 +00:00
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <paths.h>
|
2012-01-31 15:52:21 +00:00
|
|
|
#include <pwd.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
const struct cmd_entry *cmd_table[] = {
|
|
|
|
&cmd_attach_session_entry,
|
|
|
|
&cmd_bind_key_entry,
|
|
|
|
&cmd_break_pane_entry,
|
2009-12-01 18:42:38 +00:00
|
|
|
&cmd_capture_pane_entry,
|
2010-06-21 21:44:09 +00:00
|
|
|
&cmd_choose_buffer_entry,
|
2009-08-25 12:18:51 +00:00
|
|
|
&cmd_choose_client_entry,
|
2012-09-03 09:57:57 +00:00
|
|
|
&cmd_choose_list_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_choose_session_entry,
|
2012-07-08 16:04:38 +00:00
|
|
|
&cmd_choose_tree_entry,
|
2009-06-01 22:58:49 +00: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 18:45:08 +00:00
|
|
|
&cmd_display_message_entry,
|
2009-08-31 20:46:19 +00:00
|
|
|
&cmd_display_panes_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_find_window_entry,
|
|
|
|
&cmd_has_session_entry,
|
2009-07-09 15:47:49 +00:00
|
|
|
&cmd_if_shell_entry,
|
2010-01-07 20:52:18 +00:00
|
|
|
&cmd_join_pane_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_kill_pane_entry,
|
|
|
|
&cmd_kill_server_entry,
|
|
|
|
&cmd_kill_session_entry,
|
|
|
|
&cmd_kill_window_entry,
|
2010-10-23 13:04:34 +00:00
|
|
|
&cmd_last_pane_entry,
|
2009-06-01 22:58:49 +00: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 17:19:38 +00:00
|
|
|
&cmd_list_panes_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_list_sessions_entry,
|
|
|
|
&cmd_list_windows_entry,
|
|
|
|
&cmd_load_buffer_entry,
|
2009-09-24 14:17:09 +00:00
|
|
|
&cmd_lock_client_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_lock_server_entry,
|
2009-09-24 14:17:09 +00:00
|
|
|
&cmd_lock_session_entry,
|
2012-03-03 08:31:18 +00:00
|
|
|
&cmd_move_pane_entry,
|
2009-06-01 22:58:49 +00: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 10:04:27 +00:00
|
|
|
&cmd_pipe_pane_entry,
|
2009-06-01 22:58:49 +00: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 11:19:03 +00:00
|
|
|
&cmd_respawn_pane_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_respawn_window_entry,
|
|
|
|
&cmd_rotate_window_entry,
|
2009-09-20 19:15:01 +00:00
|
|
|
&cmd_run_shell_entry,
|
2009-06-01 22:58:49 +00: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 21:52:43 +00:00
|
|
|
&cmd_set_environment_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
&cmd_set_option_entry,
|
|
|
|
&cmd_set_window_option_entry,
|
|
|
|
&cmd_show_buffer_entry,
|
2009-08-08 21:52:43 +00:00
|
|
|
&cmd_show_environment_entry,
|
2009-11-18 13:16:33 +00:00
|
|
|
&cmd_show_messages_entry,
|
2009-06-01 22:58:49 +00: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 10:09:05 +00:00
|
|
|
&cmd_wait_for_entry,
|
2009-06-01 22:58:49 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-11-27 09:20:03 +00:00
|
|
|
int cmd_session_better(struct session *, struct session *, int);
|
2010-12-21 22:37:59 +00:00
|
|
|
struct session *cmd_choose_session_list(struct sessionslist *);
|
2011-04-05 19:37:01 +00:00
|
|
|
struct session *cmd_choose_session(int);
|
2009-11-03 22:40:40 +00:00
|
|
|
struct client *cmd_choose_client(struct clients *);
|
2009-07-13 17:47:46 +00:00
|
|
|
struct client *cmd_lookup_client(const char *);
|
|
|
|
struct session *cmd_lookup_session(const char *, int *);
|
2013-03-25 10:11:45 +00:00
|
|
|
struct session *cmd_lookup_session_id(const char *);
|
2009-07-13 17:47:46 +00:00
|
|
|
struct winlink *cmd_lookup_window(struct session *, const char *, int *);
|
2009-07-22 21:23:29 +00:00
|
|
|
int cmd_lookup_index(struct session *, const char *, int *);
|
2011-03-27 20:27:26 +00:00
|
|
|
struct window_pane *cmd_lookup_paneid(const char *);
|
2012-01-30 09:39:34 +00:00
|
|
|
struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
|
|
|
|
struct window *cmd_lookup_windowid(const char *);
|
2013-03-24 09:54:10 +00:00
|
|
|
struct session *cmd_window_session(struct cmd_q *, struct window *,
|
|
|
|
struct winlink **);
|
2010-07-14 18:37:49 +00:00
|
|
|
struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
|
|
|
|
int cmd_find_index_offset(const char *, struct session *, int *);
|
2011-03-27 20:27:26 +00:00
|
|
|
struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
|
2009-07-13 17:47:46 +00:00
|
|
|
|
2009-07-26 12:58:44 +00:00
|
|
|
int
|
|
|
|
cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t arglen;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*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 20:11:57 +00:00
|
|
|
char **
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_copy_argv(int argc, char *const *argv)
|
2010-10-29 20:11:57 +00:00
|
|
|
{
|
|
|
|
char **new_argv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return (NULL);
|
|
|
|
new_argv = xcalloc(argc, sizeof *new_argv);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] != NULL)
|
|
|
|
new_argv[i] = xstrdup(argv[i]);
|
|
|
|
}
|
|
|
|
return (new_argv);
|
|
|
|
}
|
|
|
|
|
2009-07-26 12:58:44 +00:00
|
|
|
void
|
|
|
|
cmd_free_argv(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc == 0)
|
2009-12-03 22:50:09 +00:00
|
|
|
return;
|
2012-07-10 11:53:01 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
free(argv[i]);
|
|
|
|
free(argv);
|
2009-07-26 12:58:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
struct cmd *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
const struct cmd_entry **entryp, *entry;
|
2009-10-26 21:42:04 +00:00
|
|
|
struct cmd *cmd;
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args;
|
2009-06-01 22:58:49 +00:00
|
|
|
char s[BUFSIZ];
|
2011-01-04 00:42:46 +00:00
|
|
|
int ambiguous = 0;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
*cause = NULL;
|
2009-06-04 23:34:32 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
xasprintf(cause, "no command");
|
2009-06-01 22:58:49 +00:00
|
|
|
return (NULL);
|
2009-06-04 23:34:32 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
entry = NULL;
|
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
|
|
|
if ((*entryp)->alias != NULL &&
|
|
|
|
strcmp((*entryp)->alias, argv[0]) == 0) {
|
2009-07-07 21:23:22 +00:00
|
|
|
ambiguous = 0;
|
2009-06-01 22:58:49 +00:00
|
|
|
entry = *entryp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
|
|
|
|
continue;
|
|
|
|
if (entry != NULL)
|
2009-07-07 21:23:22 +00:00
|
|
|
ambiguous = 1;
|
2009-06-01 22:58:49 +00:00
|
|
|
entry = *entryp;
|
|
|
|
|
|
|
|
/* Bail now if an exact match. */
|
|
|
|
if (strcmp(entry->name, argv[0]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-07-07 21:23:22 +00:00
|
|
|
if (ambiguous)
|
|
|
|
goto ambiguous;
|
2009-06-01 22:58:49 +00:00
|
|
|
if (entry == NULL) {
|
|
|
|
xasprintf(cause, "unknown command: %s", argv[0]);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2011-01-04 00:42:46 +00: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;
|
|
|
|
if (entry->check != NULL && entry->check(args) != 0)
|
|
|
|
goto usage;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd = xcalloc(1, sizeof *cmd);
|
2009-06-01 22:58:49 +00:00
|
|
|
cmd->entry = entry;
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd->args = args;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
if (file != NULL)
|
|
|
|
cmd->file = xstrdup(file);
|
|
|
|
cmd->line = line;
|
|
|
|
|
2009-06-01 22:58:49 +00: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 00:42:46 +00:00
|
|
|
if (args != NULL)
|
|
|
|
args_free(args);
|
2009-06-01 22:58:49 +00:00
|
|
|
xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
cmd_print(struct cmd *cmd, char *buf, size_t len)
|
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
size_t off, used;
|
|
|
|
|
|
|
|
off = xsnprintf(buf, len, "%s ", cmd->entry->name);
|
2013-05-15 15:34:09 +00:00
|
|
|
if (off + 1 < len) {
|
|
|
|
used = args_print(cmd->args, buf + off, len - off - 1);
|
2011-01-04 00:42:46 +00:00
|
|
|
if (used == 0)
|
2012-04-22 05:24:05 +00:00
|
|
|
off--;
|
|
|
|
else
|
2011-01-04 00:42:46 +00:00
|
|
|
off += used;
|
2012-04-22 05:24:05 +00:00
|
|
|
buf[off] = '\0';
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
return (off);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/*
|
|
|
|
* Figure out the current session. Use: 1) the current session, if the command
|
2009-11-03 22:40:40 +00:00
|
|
|
* context has one; 2) the most recently used session containing the pty of the
|
|
|
|
* calling client, if any; 3) the session specified in the TMUX variable from
|
|
|
|
* the environment (as passed from the client); 4) the most recently used
|
|
|
|
* session from all sessions.
|
2009-07-13 17:47:46 +00:00
|
|
|
*/
|
2009-06-01 22:58:49 +00:00
|
|
|
struct session *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_current_session(struct cmd_q *cmdq, int prefer_unattached)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2013-03-24 09:54:10 +00:00
|
|
|
struct msg_command_data *data = cmdq->msgdata;
|
|
|
|
struct client *c = cmdq->client;
|
2009-07-13 17:47:46 +00:00
|
|
|
struct session *s;
|
2010-12-21 22:37:59 +00:00
|
|
|
struct sessionslist ss;
|
2009-07-29 17:03:16 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct window_pane *wp;
|
2013-03-24 09:54:10 +00:00
|
|
|
const char *path;
|
2009-07-29 17:03:16 +00:00
|
|
|
int found;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (c != NULL && c->session != NULL)
|
|
|
|
return (c->session);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-29 17:03:16 +00:00
|
|
|
/*
|
2013-03-25 10:09:05 +00:00
|
|
|
* If the name of the calling client's pty is known, build a list of
|
|
|
|
* the sessions that contain it and if any choose either the first or
|
|
|
|
* the newest.
|
2009-07-29 17:03:16 +00:00
|
|
|
*/
|
2013-03-24 09:54:10 +00:00
|
|
|
path = c == NULL ? NULL : c->tty.path;
|
|
|
|
if (path != NULL) {
|
2009-07-29 17:03:16 +00:00
|
|
|
ARRAY_INIT(&ss);
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2009-07-29 17:03:16 +00:00
|
|
|
found = 0;
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
TAILQ_FOREACH(wp, &wl->window->panes, entry) {
|
2013-03-24 09:54:10 +00:00
|
|
|
if (strcmp(wp->tty, path) == 0) {
|
2009-07-29 17:03:16 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
ARRAY_ADD(&ss, s);
|
|
|
|
}
|
|
|
|
|
2010-12-21 22:37:59 +00:00
|
|
|
s = cmd_choose_session_list(&ss);
|
2009-07-29 17:03:16 +00:00
|
|
|
ARRAY_FREE(&ss);
|
|
|
|
if (s != NULL)
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the session from the TMUX environment variable. */
|
2013-03-25 10:11:45 +00:00
|
|
|
if (data != NULL && data->pid == getpid() && data->session_id != -1) {
|
|
|
|
s = session_find_by_id(data->session_id);
|
2010-12-21 22:37:59 +00:00
|
|
|
if (s != NULL)
|
|
|
|
return (s);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-04-05 19:37:01 +00:00
|
|
|
return (cmd_choose_session(prefer_unattached));
|
2010-12-21 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 09:20:03 +00:00
|
|
|
/* Is this session better? */
|
|
|
|
int
|
|
|
|
cmd_session_better(struct session *s, struct session *best,
|
|
|
|
int prefer_unattached)
|
|
|
|
{
|
|
|
|
if (best == NULL)
|
2013-01-18 02:10:29 +00:00
|
|
|
return (1);
|
2012-11-27 09:20:03 +00:00
|
|
|
if (prefer_unattached) {
|
|
|
|
if (!(best->flags & SESSION_UNATTACHED) &&
|
|
|
|
(s->flags & SESSION_UNATTACHED))
|
2013-01-18 02:10:29 +00:00
|
|
|
return (1);
|
2012-11-27 09:20:03 +00:00
|
|
|
else if ((best->flags & SESSION_UNATTACHED) &&
|
|
|
|
!(s->flags & SESSION_UNATTACHED))
|
2013-01-18 02:10:29 +00:00
|
|
|
return (0);
|
2012-11-27 09:20:03 +00:00
|
|
|
}
|
|
|
|
return (timercmp(&s->activity_time, &best->activity_time, >));
|
|
|
|
}
|
|
|
|
|
2011-04-05 19:37:01 +00:00
|
|
|
/*
|
|
|
|
* Find the most recently used session, preferring unattached if the flag is
|
|
|
|
* set.
|
|
|
|
*/
|
2010-12-21 22:37:59 +00:00
|
|
|
struct session *
|
2011-04-05 19:37:01 +00:00
|
|
|
cmd_choose_session(int prefer_unattached)
|
2010-12-21 22:37:59 +00:00
|
|
|
{
|
2012-11-27 09:20:03 +00:00
|
|
|
struct session *s, *best;
|
2010-12-21 22:37:59 +00:00
|
|
|
|
2012-11-27 09:20:03 +00:00
|
|
|
best = NULL;
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2012-11-27 09:20:03 +00:00
|
|
|
if (cmd_session_better(s, best, prefer_unattached))
|
|
|
|
best = s;
|
2010-12-21 22:37:59 +00:00
|
|
|
}
|
2012-11-27 09:20:03 +00:00
|
|
|
return (best);
|
2009-07-13 17:47:46 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
/* Find the most recently used session from a list. */
|
2009-07-13 17:47:46 +00:00
|
|
|
struct session *
|
2010-12-21 22:37:59 +00:00
|
|
|
cmd_choose_session_list(struct sessionslist *ss)
|
2009-07-13 17:47:46 +00:00
|
|
|
{
|
2009-11-03 22:40:40 +00:00
|
|
|
struct session *s, *sbest;
|
2009-07-13 17:47:46 +00:00
|
|
|
struct timeval *tv = NULL;
|
|
|
|
u_int i;
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
sbest = NULL;
|
2009-07-29 17:03:16 +00:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(ss); i++) {
|
|
|
|
if ((s = ARRAY_ITEM(ss, i)) == NULL)
|
2009-07-13 17:47:46 +00:00
|
|
|
continue;
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
|
|
|
|
sbest = s;
|
|
|
|
tv = &s->activity_time;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-13 17:47:46 +00:00
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
return (sbest);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 20:59:22 +00:00
|
|
|
/*
|
|
|
|
* Find the current client. First try the current client if set, then pick the
|
2009-11-03 22:40:40 +00:00
|
|
|
* most recently used of the clients attached to the current session if any,
|
|
|
|
* then of all clients.
|
2009-11-03 20:59:22 +00:00
|
|
|
*/
|
|
|
|
struct client *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_current_client(struct cmd_q *cmdq)
|
2009-11-03 20:59:22 +00:00
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct client *c;
|
|
|
|
struct clients cc;
|
|
|
|
u_int i;
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (cmdq->client != NULL && cmdq->client->session != NULL)
|
|
|
|
return (cmdq->client);
|
2009-11-03 20:59:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No current client set. Find the current session and return the
|
|
|
|
* newest of its clients.
|
|
|
|
*/
|
2013-03-24 09:54:10 +00:00
|
|
|
s = cmd_current_session(cmdq, 0);
|
2009-11-03 20:59:22 +00:00
|
|
|
if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
|
|
|
|
ARRAY_INIT(&cc);
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
if ((c = ARRAY_ITEM(&clients, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
if (s == c->session)
|
|
|
|
ARRAY_ADD(&cc, c);
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
c = cmd_choose_client(&cc);
|
2009-11-03 20:59:22 +00:00
|
|
|
ARRAY_FREE(&cc);
|
|
|
|
if (c != NULL)
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
return (cmd_choose_client(&clients));
|
2009-11-03 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
/* Choose the most recently used client from a list. */
|
2009-10-05 18:30:54 +00:00
|
|
|
struct client *
|
2009-11-03 22:40:40 +00:00
|
|
|
cmd_choose_client(struct clients *cc)
|
2009-10-05 18:30:54 +00:00
|
|
|
{
|
2009-11-03 22:40:40 +00:00
|
|
|
struct client *c, *cbest;
|
2009-10-05 18:30:54 +00:00
|
|
|
struct timeval *tv = NULL;
|
|
|
|
u_int i;
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
cbest = NULL;
|
2009-11-03 20:59:22 +00:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(cc); i++) {
|
|
|
|
if ((c = ARRAY_ITEM(cc, i)) == NULL)
|
2009-10-05 18:30:54 +00:00
|
|
|
continue;
|
|
|
|
if (c->session == NULL)
|
|
|
|
continue;
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
|
|
|
|
cbest = c;
|
|
|
|
tv = &c->activity_time;
|
2009-10-05 18:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:40:40 +00:00
|
|
|
return (cbest);
|
2009-10-05 18:30:54 +00:00
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* Find the target client or report an error and return NULL. */
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-11-03 20:59:22 +00:00
|
|
|
struct client *c;
|
2009-07-13 17:47:46 +00:00
|
|
|
char *tmparg;
|
|
|
|
size_t arglen;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* A NULL argument means the current client. */
|
2013-03-24 09:27:19 +00:00
|
|
|
if (arg == NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
c = cmd_current_client(cmdq);
|
2013-03-24 09:27:19 +00:00
|
|
|
if (c == NULL && !quiet)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "no clients");
|
2013-03-24 09:27:19 +00:00
|
|
|
return (c);
|
|
|
|
}
|
2009-07-13 17:47:46 +00:00
|
|
|
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. */
|
2013-03-24 09:27:19 +00:00
|
|
|
if (c == NULL && !quiet)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "client not found: %s", tmparg);
|
2009-07-13 17:47:46 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(tmparg);
|
2009-07-13 17:47:46 +00:00
|
|
|
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++) {
|
2009-10-14 09:29:10 +00:00
|
|
|
c = ARRAY_ITEM(&clients, i);
|
2013-03-25 10:09:05 +00:00
|
|
|
if (c == NULL || c->session == NULL || c->tty.path == NULL)
|
2009-07-13 17:47:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-25 10:11:45 +00:00
|
|
|
/* Find the target session or report an error and return NULL. */
|
|
|
|
struct session *
|
|
|
|
cmd_lookup_session_id(const char *arg)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
long id;
|
|
|
|
|
|
|
|
if (arg[0] != '$')
|
|
|
|
return (NULL);
|
|
|
|
id = strtol(arg + 1, &endptr, 10);
|
|
|
|
if (arg[1] != '\0' && *endptr == '\0')
|
|
|
|
return (session_find_by_id(id));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
*ambiguous = 0;
|
|
|
|
|
2013-03-25 10:11:45 +00:00
|
|
|
/* Look for $id first. */
|
|
|
|
if ((s = cmd_lookup_session_id(name)) != NULL)
|
|
|
|
return (s);
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/*
|
2009-11-02 16:24:29 +00:00
|
|
|
* Look for matches. First look for exact matches - session names must
|
|
|
|
* be unique so an exact match can't be ambigious and can just be
|
|
|
|
* returned.
|
2009-07-13 17:47:46 +00:00
|
|
|
*/
|
2010-12-21 22:37:59 +00:00
|
|
|
if ((s = session_find(name)) != NULL)
|
|
|
|
return (s);
|
2009-11-02 16:24:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise look for partial matches, returning early if it is found to
|
|
|
|
* be ambiguous.
|
|
|
|
*/
|
|
|
|
sfound = NULL;
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2009-07-13 17:47:46 +00:00
|
|
|
if (strncmp(name, s->name, strlen(name)) == 0 ||
|
|
|
|
fnmatch(name, s->name, 0) == 0) {
|
|
|
|
if (sfound != NULL) {
|
|
|
|
*ambiguous = 1;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
sfound = s;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
return (sfound);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/*
|
2009-07-22 21:23:29 +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. Return NULL but fill in
|
2010-06-21 01:46:36 +00:00
|
|
|
* idx if the window index is a valid number but there is no window with that
|
2009-07-22 21:23:29 +00:00
|
|
|
* index.
|
2009-07-13 17:47:46 +00:00
|
|
|
*/
|
|
|
|
struct winlink *
|
|
|
|
cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
|
|
|
|
{
|
|
|
|
struct winlink *wl, *wlfound;
|
|
|
|
const char *errstr;
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
*ambiguous = 0;
|
|
|
|
|
2012-01-30 09:39:34 +00:00
|
|
|
/* Try as a window id. */
|
|
|
|
if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
|
|
|
|
return (wl);
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* Look for exact matches, error if more than one. */
|
|
|
|
wlfound = NULL;
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
2009-07-22 21:23:29 +00:00
|
|
|
if (strcmp(name, wl->window->name) == 0) {
|
2009-07-13 17:47:46 +00:00
|
|
|
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) {
|
2009-07-22 21:23:29 +00:00
|
|
|
if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
|
|
|
|
fnmatch(name, wl->window->name, 0) == 0) {
|
2009-07-13 17:47:46 +00:00
|
|
|
if (wlfound != NULL) {
|
|
|
|
*ambiguous = 1;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
wlfound = wl;
|
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
}
|
2009-07-13 17:47:46 +00:00
|
|
|
if (wlfound != NULL)
|
|
|
|
return (wlfound);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2009-07-22 21:23:29 +00:00
|
|
|
/*
|
|
|
|
* Find a window index - if the window doesn't exist, check if it is a
|
|
|
|
* potential index and return it anyway.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
|
|
|
|
{
|
|
|
|
struct winlink *wl;
|
|
|
|
const char *errstr;
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
|
|
|
|
return (wl->idx);
|
|
|
|
if (*ambiguous)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
idx = strtonum(name, 0, INT_MAX, &errstr);
|
|
|
|
if (errstr == NULL)
|
|
|
|
return (idx);
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-01-30 09:39:34 +00:00
|
|
|
/* Lookup pane id. An initial % means a pane id. */
|
2011-03-27 20:27:26 +00:00
|
|
|
struct window_pane *
|
|
|
|
cmd_lookup_paneid(const char *arg)
|
|
|
|
{
|
|
|
|
const char *errstr;
|
|
|
|
u_int paneid;
|
|
|
|
|
|
|
|
if (*arg != '%')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (window_pane_find_by_id(paneid));
|
|
|
|
}
|
|
|
|
|
2012-01-30 09:39:34 +00:00
|
|
|
/* Lookup window id in a session. An initial @ means a window id. */
|
|
|
|
struct winlink *
|
|
|
|
cmd_lookup_winlink_windowid(struct session *s, const char *arg)
|
|
|
|
{
|
|
|
|
const char *errstr;
|
|
|
|
u_int windowid;
|
|
|
|
|
|
|
|
if (*arg != '@')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (winlink_find_by_window_id(&s->windows, windowid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup window id. An initial @ means a window id. */
|
|
|
|
struct window *
|
|
|
|
cmd_lookup_windowid(const char *arg)
|
|
|
|
{
|
|
|
|
const char *errstr;
|
|
|
|
u_int windowid;
|
|
|
|
|
|
|
|
if (*arg != '@')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (window_find_by_id(windowid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find session and winlink for window. */
|
2011-03-27 20:27:26 +00:00
|
|
|
struct session *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
|
2011-03-27 20:27:26 +00:00
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct sessionslist ss;
|
|
|
|
struct winlink *wl;
|
|
|
|
|
2012-01-30 09:39:34 +00:00
|
|
|
/* If this window is in the current session, return that winlink. */
|
2013-03-24 09:54:10 +00:00
|
|
|
s = cmd_current_session(cmdq, 0);
|
2011-03-27 20:27:26 +00:00
|
|
|
if (s != NULL) {
|
2012-01-30 09:39:34 +00:00
|
|
|
wl = winlink_find_by_window(&s->windows, w);
|
2011-03-27 20:27:26 +00:00
|
|
|
if (wl != NULL) {
|
|
|
|
if (wlp != NULL)
|
|
|
|
*wlp = wl;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 09:39:34 +00:00
|
|
|
/* Otherwise choose from all sessions with this window. */
|
2011-03-27 20:27:26 +00:00
|
|
|
ARRAY_INIT(&ss);
|
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2012-01-30 09:39:34 +00:00
|
|
|
if (winlink_find_by_window(&s->windows, w) != NULL)
|
2011-03-27 20:27:26 +00:00
|
|
|
ARRAY_ADD(&ss, s);
|
|
|
|
}
|
|
|
|
s = cmd_choose_session_list(&ss);
|
|
|
|
ARRAY_FREE(&ss);
|
|
|
|
if (wlp != NULL)
|
2012-01-30 09:39:34 +00:00
|
|
|
*wlp = winlink_find_by_window(&s->windows, w);
|
2011-03-27 20:27:26 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* Find the target session or report an error and return NULL. */
|
2009-06-01 22:58:49 +00:00
|
|
|
struct session *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2011-03-27 20:27:26 +00:00
|
|
|
struct session *s;
|
|
|
|
struct window_pane *wp;
|
2012-01-30 09:39:34 +00:00
|
|
|
struct window *w;
|
2011-03-27 20:27:26 +00:00
|
|
|
struct client *c;
|
|
|
|
char *tmparg;
|
|
|
|
size_t arglen;
|
|
|
|
int ambiguous;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* A NULL argument means the current session. */
|
2009-06-01 22:58:49 +00:00
|
|
|
if (arg == NULL)
|
2013-03-24 09:54:10 +00:00
|
|
|
return (cmd_current_session(cmdq, prefer_unattached));
|
2009-07-13 17:47:46 +00:00
|
|
|
|
2012-01-30 09:39:34 +00:00
|
|
|
/* Lookup as pane id or window id. */
|
2011-03-27 20:27:26 +00:00
|
|
|
if ((wp = cmd_lookup_paneid(arg)) != NULL)
|
2013-03-24 09:54:10 +00:00
|
|
|
return (cmd_window_session(cmdq, wp->window, NULL));
|
2012-01-30 09:39:34 +00:00
|
|
|
if ((w = cmd_lookup_windowid(arg)) != NULL)
|
2013-03-24 09:54:10 +00:00
|
|
|
return (cmd_window_session(cmdq, w, NULL));
|
2011-03-27 20:27:26 +00:00
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* Trim a single trailing colon if any. */
|
2011-05-08 20:35:58 +00:00
|
|
|
tmparg = xstrdup(arg);
|
2009-07-13 17:47:46 +00:00
|
|
|
arglen = strlen(tmparg);
|
|
|
|
if (arglen != 0 && tmparg[arglen - 1] == ':')
|
|
|
|
tmparg[arglen - 1] = '\0';
|
|
|
|
|
2011-04-06 21:51:31 +00:00
|
|
|
/* An empty session name is the current session. */
|
|
|
|
if (*tmparg == '\0') {
|
2012-07-10 11:53:01 +00:00
|
|
|
free(tmparg);
|
2013-03-24 09:54:10 +00:00
|
|
|
return (cmd_current_session(cmdq, prefer_unattached));
|
2011-04-06 21:51:31 +00:00
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* 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)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "more than one session: %s", tmparg);
|
2009-07-13 17:47:46 +00:00
|
|
|
else
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "session not found: %s", tmparg);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2009-07-13 17:47:46 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(tmparg);
|
2009-06-01 22:58:49 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* Find the target session and window or report an error and return NULL. */
|
2009-06-01 22:58:49 +00:00
|
|
|
struct winlink *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2011-03-27 20:27:26 +00:00
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
|
|
|
struct window_pane *wp;
|
|
|
|
const char *winptr;
|
|
|
|
char *sessptr = NULL;
|
|
|
|
int ambiguous = 0;
|
2009-07-13 17:47:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the current session. There must always be a current session, if
|
|
|
|
* it can't be found, report an error.
|
|
|
|
*/
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((s = cmd_current_session(cmdq, 0)) == NULL) {
|
|
|
|
cmdq_error(cmdq, "can't establish current session");
|
2009-06-01 22:58:49 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2009-07-13 17:47:46 +00:00
|
|
|
|
|
|
|
/* A NULL argument means the current session and window. */
|
|
|
|
if (arg == NULL) {
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
return (s->curw);
|
|
|
|
}
|
|
|
|
|
2011-03-27 20:27:26 +00:00
|
|
|
/* Lookup as pane id. */
|
|
|
|
if ((wp = cmd_lookup_paneid(arg)) != NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
s = cmd_window_session(cmdq, wp->window, &wl);
|
2011-03-27 20:27:26 +00:00
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
return (wl);
|
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/* Time to look at the argument. If it is empty, that is an error. */
|
|
|
|
if (*arg == '\0')
|
|
|
|
goto not_found;
|
|
|
|
|
2009-07-22 21:23:29 +00:00
|
|
|
/* Find the separating colon and split into window and session. */
|
2009-07-13 17:47:46 +00:00
|
|
|
winptr = strchr(arg, ':');
|
|
|
|
if (winptr == NULL)
|
2009-07-22 21:23:29 +00:00
|
|
|
goto no_colon;
|
|
|
|
winptr++; /* skip : */
|
|
|
|
sessptr = xstrdup(arg);
|
|
|
|
*strchr(sessptr, ':') = '\0';
|
2009-07-13 17:47:46 +00:00
|
|
|
|
|
|
|
/* Try to lookup the session if present. */
|
2009-07-22 21:23:29 +00:00
|
|
|
if (*sessptr != '\0') {
|
|
|
|
if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
|
2009-07-13 17:47:46 +00:00
|
|
|
goto no_session;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/*
|
|
|
|
* Then work out the window. An empty string is the current window,
|
2010-01-19 21:27:47 +00:00
|
|
|
* otherwise try special cases then to look it up in the session.
|
2009-07-13 17:47:46 +00:00
|
|
|
*/
|
2009-07-22 21:23:29 +00:00
|
|
|
if (*winptr == '\0')
|
2009-06-01 22:58:49 +00:00
|
|
|
wl = s->curw;
|
2010-01-19 21:27:47 +00:00
|
|
|
else if (winptr[0] == '!' && winptr[1] == '\0')
|
|
|
|
wl = TAILQ_FIRST(&s->lastw);
|
2012-12-24 12:38:57 +00:00
|
|
|
else if (winptr[0] == '^' && winptr[1] == '\0')
|
|
|
|
wl = RB_MIN(winlinks, &s->windows);
|
|
|
|
else if (winptr[0] == '$' && winptr[1] == '\0')
|
|
|
|
wl = RB_MAX(winlinks, &s->windows);
|
2010-07-14 18:37:49 +00:00
|
|
|
else if (winptr[0] == '+' || winptr[0] == '-')
|
|
|
|
wl = cmd_find_window_offset(winptr, s, &ambiguous);
|
|
|
|
else
|
2010-01-19 21:27:47 +00:00
|
|
|
wl = cmd_lookup_window(s, winptr, &ambiguous);
|
|
|
|
if (wl == NULL)
|
2009-07-13 17:47:46 +00:00
|
|
|
goto not_found;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
if (sessptr != NULL)
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2009-06-01 22:58:49 +00:00
|
|
|
return (wl);
|
2009-07-13 17:47:46 +00:00
|
|
|
|
2009-07-22 21:23:29 +00:00
|
|
|
no_colon:
|
2010-01-19 21:27:47 +00:00
|
|
|
/*
|
|
|
|
* No colon in the string, first try special cases, then as a window
|
|
|
|
* and lastly as a session.
|
|
|
|
*/
|
|
|
|
if (arg[0] == '!' && arg[1] == '\0') {
|
|
|
|
if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
|
|
|
|
goto not_found;
|
2010-06-21 01:46:36 +00:00
|
|
|
} else if (arg[0] == '+' || arg[0] == '-') {
|
2010-07-14 18:37:49 +00:00
|
|
|
if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
|
2010-06-21 01:46:36 +00:00
|
|
|
goto lookup_session;
|
|
|
|
} else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
|
|
|
|
goto lookup_session;
|
2009-07-22 21:23:29 +00:00
|
|
|
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
return (wl);
|
|
|
|
|
2010-06-21 01:46:36 +00:00
|
|
|
lookup_session:
|
|
|
|
if (ambiguous)
|
|
|
|
goto not_found;
|
2011-04-06 21:51:31 +00:00
|
|
|
if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
|
2010-06-21 01:46:36 +00:00
|
|
|
goto no_session;
|
|
|
|
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
return (s->curw);
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
no_session:
|
|
|
|
if (ambiguous)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "multiple sessions: %s", arg);
|
2009-07-13 17:47:46 +00:00
|
|
|
else
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "session not found: %s", arg);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2009-07-13 17:47:46 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
not_found:
|
|
|
|
if (ambiguous)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "multiple windows: %s", arg);
|
2009-07-13 17:47:46 +00:00
|
|
|
else
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "window not found: %s", arg);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2009-07-13 17:47:46 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2010-07-14 18:37:49 +00:00
|
|
|
struct winlink *
|
|
|
|
cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
|
|
|
|
{
|
|
|
|
struct winlink *wl;
|
|
|
|
int offset = 1;
|
|
|
|
|
|
|
|
if (winptr[1] != '\0')
|
|
|
|
offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
|
|
|
|
if (offset == 0)
|
|
|
|
wl = cmd_lookup_window(s, winptr, ambiguous);
|
|
|
|
else {
|
|
|
|
if (winptr[0] == '+')
|
|
|
|
wl = winlink_next_by_number(s->curw, s, offset);
|
|
|
|
else
|
|
|
|
wl = winlink_previous_by_number(s->curw, s, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (wl);
|
|
|
|
}
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2009-07-22 21:23:29 +00:00
|
|
|
* used when parsing an argument for a window target that may not exist (for
|
|
|
|
* example if it is going to be created).
|
2009-07-13 17:47:46 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
|
2009-07-13 17:47:46 +00:00
|
|
|
{
|
|
|
|
struct session *s;
|
2010-01-19 21:27:47 +00:00
|
|
|
struct winlink *wl;
|
2009-07-22 21:23:29 +00:00
|
|
|
const char *winptr;
|
2009-07-13 17:47:46 +00:00
|
|
|
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.
|
|
|
|
*/
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((s = cmd_current_session(cmdq, 0)) == NULL) {
|
|
|
|
cmdq_error(cmdq, "can't establish current session");
|
2009-07-24 18:46:40 +00:00
|
|
|
return (-2);
|
2009-07-13 17:47:46 +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)
|
2009-07-22 21:23:29 +00:00
|
|
|
goto no_colon;
|
|
|
|
winptr++; /* skip : */
|
|
|
|
sessptr = xstrdup(arg);
|
|
|
|
*strchr(sessptr, ':') = '\0';
|
2009-07-13 17:47:46 +00:00
|
|
|
|
|
|
|
/* Try to lookup the session if present. */
|
|
|
|
if (sessptr != NULL && *sessptr != '\0') {
|
2009-07-22 21:23:29 +00:00
|
|
|
if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
|
2009-07-13 17:47:46 +00:00
|
|
|
goto no_session;
|
|
|
|
}
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
/*
|
2009-07-22 21:23:29 +00:00
|
|
|
* Then work out the window. An empty string is a new window otherwise
|
|
|
|
* try to look it up in the session.
|
2009-07-13 17:47:46 +00:00
|
|
|
*/
|
2009-07-22 21:23:29 +00:00
|
|
|
if (*winptr == '\0')
|
2010-01-19 21:27:47 +00:00
|
|
|
idx = -1;
|
|
|
|
else if (winptr[0] == '!' && winptr[1] == '\0') {
|
|
|
|
if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
|
|
|
|
goto not_found;
|
|
|
|
idx = wl->idx;
|
2010-06-21 01:46:36 +00:00
|
|
|
} else if (winptr[0] == '+' || winptr[0] == '-') {
|
2010-07-14 18:37:49 +00:00
|
|
|
if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
|
2010-06-21 01:46:36 +00:00
|
|
|
goto invalid_index;
|
|
|
|
} else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
|
|
|
|
goto invalid_index;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2009-07-13 17:47:46 +00:00
|
|
|
return (idx);
|
|
|
|
|
2009-07-22 21:23:29 +00:00
|
|
|
no_colon:
|
2010-01-19 21:27:47 +00:00
|
|
|
/*
|
|
|
|
* No colon in the string, first try special cases, then as a window
|
|
|
|
* and lastly as a session.
|
|
|
|
*/
|
|
|
|
if (arg[0] == '!' && arg[1] == '\0') {
|
|
|
|
if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
|
|
|
|
goto not_found;
|
|
|
|
idx = wl->idx;
|
2010-06-21 01:46:36 +00:00
|
|
|
} else if (arg[0] == '+' || arg[0] == '-') {
|
2010-07-14 18:37:49 +00:00
|
|
|
if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
|
2010-06-21 01:46:36 +00:00
|
|
|
goto lookup_session;
|
|
|
|
} else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
|
|
|
|
goto lookup_session;
|
2009-07-22 21:23:29 +00:00
|
|
|
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
return (idx);
|
|
|
|
|
2010-06-21 01:46:36 +00:00
|
|
|
lookup_session:
|
|
|
|
if (ambiguous)
|
|
|
|
goto not_found;
|
2011-04-06 21:51:31 +00:00
|
|
|
if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
|
2010-06-21 01:46:36 +00:00
|
|
|
goto no_session;
|
|
|
|
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
no_session:
|
|
|
|
if (ambiguous)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "multiple sessions: %s", arg);
|
2009-07-13 17:47:46 +00:00
|
|
|
else
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "session not found: %s", arg);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2009-07-13 17:47:46 +00:00
|
|
|
return (-2);
|
|
|
|
|
2010-06-21 01:46:36 +00:00
|
|
|
invalid_index:
|
|
|
|
if (ambiguous)
|
|
|
|
goto not_found;
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "invalid index: %s", arg);
|
2010-06-21 01:46:36 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2010-06-21 01:46:36 +00:00
|
|
|
return (-2);
|
|
|
|
|
2009-07-13 17:47:46 +00:00
|
|
|
not_found:
|
|
|
|
if (ambiguous)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "multiple windows: %s", arg);
|
2009-07-13 17:47:46 +00:00
|
|
|
else
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "window not found: %s", arg);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(sessptr);
|
2009-07-13 17:47:46 +00:00
|
|
|
return (-2);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2009-07-30 13:45:56 +00:00
|
|
|
|
2010-07-14 18:37:49 +00:00
|
|
|
int
|
|
|
|
cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
|
|
|
|
{
|
|
|
|
int idx, offset = 1;
|
|
|
|
|
|
|
|
if (winptr[1] != '\0')
|
|
|
|
offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
|
|
|
|
if (offset == 0)
|
|
|
|
idx = cmd_lookup_index(s, winptr, ambiguous);
|
|
|
|
else {
|
|
|
|
if (winptr[0] == '+') {
|
|
|
|
if (s->curw->idx == INT_MAX)
|
|
|
|
idx = cmd_lookup_index(s, winptr, ambiguous);
|
|
|
|
else
|
|
|
|
idx = s->curw->idx + offset;
|
|
|
|
} else {
|
|
|
|
if (s->curw->idx == 0)
|
|
|
|
idx = cmd_lookup_index(s, winptr, ambiguous);
|
|
|
|
else
|
|
|
|
idx = s->curw->idx - offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (idx);
|
|
|
|
}
|
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
/*
|
|
|
|
* Find the target session, window and pane number or report an error and
|
|
|
|
* return NULL. The pane number is separated from the session:window by a .,
|
|
|
|
* such as mysession:mywindow.0.
|
|
|
|
*/
|
|
|
|
struct winlink *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_find_pane(struct cmd_q *cmdq,
|
2009-07-30 13:45:56 +00:00
|
|
|
const char *arg, struct session **sp, struct window_pane **wpp)
|
|
|
|
{
|
2011-06-05 10:53:05 +00:00
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
|
|
|
const char *period, *errstr;
|
|
|
|
char *winptr, *paneptr;
|
|
|
|
u_int idx;
|
2009-07-30 13:45:56 +00:00
|
|
|
|
|
|
|
/* Get the current session. */
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((s = cmd_current_session(cmdq, 0)) == NULL) {
|
|
|
|
cmdq_error(cmdq, "can't establish current session");
|
2009-07-30 13:45:56 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
/* A NULL argument means the current session, window and pane. */
|
|
|
|
if (arg == NULL) {
|
|
|
|
*wpp = s->curw->window->active;
|
|
|
|
return (s->curw);
|
|
|
|
}
|
|
|
|
|
2011-03-27 20:27:26 +00:00
|
|
|
/* Lookup as pane id. */
|
|
|
|
if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
s = cmd_window_session(cmdq, (*wpp)->window, &wl);
|
2011-03-27 20:27:26 +00:00
|
|
|
if (sp != NULL)
|
|
|
|
*sp = s;
|
|
|
|
return (wl);
|
|
|
|
}
|
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
/* Look for a separating period. */
|
|
|
|
if ((period = strrchr(arg, '.')) == NULL)
|
|
|
|
goto no_period;
|
|
|
|
|
|
|
|
/* Pull out the window part and parse it. */
|
|
|
|
winptr = xstrdup(arg);
|
|
|
|
winptr[period - arg] = '\0';
|
|
|
|
if (*winptr == '\0')
|
|
|
|
wl = s->curw;
|
2013-03-24 09:54:10 +00:00
|
|
|
else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
|
2009-07-30 13:45:56 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Find the pane section and look it up. */
|
|
|
|
paneptr = winptr + (period - arg) + 1;
|
|
|
|
if (*paneptr == '\0')
|
|
|
|
*wpp = wl->window->active;
|
2010-07-14 18:37:49 +00:00
|
|
|
else if (paneptr[0] == '+' || paneptr[0] == '-')
|
|
|
|
*wpp = cmd_find_pane_offset(paneptr, wl);
|
|
|
|
else {
|
2009-07-30 13:45:56 +00:00
|
|
|
idx = strtonum(paneptr, 0, INT_MAX, &errstr);
|
2009-12-08 07:49:31 +00:00
|
|
|
if (errstr != NULL)
|
|
|
|
goto lookup_string;
|
2009-07-30 13:45:56 +00:00
|
|
|
*wpp = window_pane_at_index(wl->window, idx);
|
2009-12-08 07:49:31 +00:00
|
|
|
if (*wpp == NULL)
|
|
|
|
goto lookup_string;
|
2009-07-30 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(winptr);
|
2009-07-30 13:45:56 +00:00
|
|
|
return (wl);
|
|
|
|
|
2009-12-08 07:49:31 +00:00
|
|
|
lookup_string:
|
|
|
|
/* Try pane string description. */
|
2011-06-05 10:53:05 +00:00
|
|
|
if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "can't find pane: %s", paneptr);
|
2009-12-08 07:49:31 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(winptr);
|
2010-03-22 19:07:52 +00:00
|
|
|
return (wl);
|
2009-12-08 07:49:31 +00:00
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
no_period:
|
|
|
|
/* Try as a pane number alone. */
|
|
|
|
idx = strtonum(arg, 0, INT_MAX, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
goto lookup_window;
|
|
|
|
|
|
|
|
/* Try index in the current session and window. */
|
|
|
|
if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
|
|
|
|
goto lookup_window;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
return (s->curw);
|
|
|
|
|
|
|
|
lookup_window:
|
2009-12-08 07:49:31 +00:00
|
|
|
/* Try pane string description. */
|
2011-06-05 10:53:05 +00:00
|
|
|
if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
|
2009-12-08 07:49:31 +00:00
|
|
|
return (s->curw);
|
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
/* Try as a window and use the active pane. */
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
|
2009-07-30 13:45:56 +00:00
|
|
|
*wpp = wl->window->active;
|
|
|
|
return (wl);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
error:
|
2012-07-10 11:53:01 +00:00
|
|
|
free(winptr);
|
2009-07-30 13:45:56 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2009-08-25 12:18:51 +00:00
|
|
|
|
2010-07-14 18:37:49 +00:00
|
|
|
struct window_pane *
|
|
|
|
cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
|
|
|
|
{
|
|
|
|
struct window *w = wl->window;
|
|
|
|
struct window_pane *wp = w->active;
|
|
|
|
u_int offset = 1;
|
|
|
|
|
|
|
|
if (paneptr[1] != '\0')
|
|
|
|
offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
|
|
|
|
if (offset > 0) {
|
|
|
|
if (paneptr[0] == '+')
|
|
|
|
wp = window_pane_next_by_number(w, wp, offset);
|
|
|
|
else
|
|
|
|
wp = window_pane_previous_by_number(w, wp, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (wp);
|
|
|
|
}
|
|
|
|
|
2009-08-25 12:18:51 +00:00
|
|
|
/* Replace the first %% or %idx in template by s. */
|
|
|
|
char *
|
2013-03-21 16:22:48 +00:00
|
|
|
cmd_template_replace(const char *template, const char *s, int idx)
|
2009-08-25 12:18:51 +00:00
|
|
|
{
|
2013-03-21 16:22:48 +00:00
|
|
|
char ch, *buf;
|
|
|
|
const char *ptr;
|
2013-03-25 11:53:54 +00:00
|
|
|
int replaced;
|
|
|
|
size_t len;
|
2009-08-25 12:18:51 +00:00
|
|
|
|
2013-03-21 16:22:48 +00:00
|
|
|
if (strchr(template, '%') == NULL)
|
2009-08-25 12:18:51 +00: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);
|
|
|
|
buf = xrealloc(buf, 1, len + 1);
|
|
|
|
strlcat(buf, s, len + 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
buf = xrealloc(buf, 1, len + 2);
|
|
|
|
buf[len++] = ch;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (buf);
|
|
|
|
}
|
2011-12-09 16:28:18 +00:00
|
|
|
|
2012-01-31 15:52:21 +00:00
|
|
|
/*
|
|
|
|
* Return the default path for a new pane, using the given path or the
|
|
|
|
* default-path option if it is NULL. Several special values are accepted: the
|
|
|
|
* empty string or relative path for the current pane's working directory, ~
|
|
|
|
* for the user's home, - for the session working directory, . for the tmux
|
|
|
|
* server's working directory. The default on failure is the session's working
|
|
|
|
* directory.
|
|
|
|
*/
|
2012-01-20 19:54:07 +00:00
|
|
|
const char *
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_get_default_path(struct cmd_q *cmdq, const char *cwd)
|
2011-12-09 16:28:18 +00:00
|
|
|
{
|
2013-03-24 09:54:10 +00:00
|
|
|
struct client *c = cmdq->client;
|
2011-12-09 16:28:18 +00:00
|
|
|
struct session *s;
|
2011-12-27 14:07:20 +00:00
|
|
|
struct environ_entry *envent;
|
2012-01-31 15:52:21 +00:00
|
|
|
const char *root;
|
|
|
|
char tmp[MAXPATHLEN];
|
|
|
|
struct passwd *pw;
|
|
|
|
int n;
|
|
|
|
size_t skip;
|
|
|
|
static char path[MAXPATHLEN];
|
2011-12-09 16:28:18 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((s = cmd_current_session(cmdq, 0)) == NULL)
|
2011-12-09 16:28:18 +00:00
|
|
|
return (NULL);
|
|
|
|
|
2012-01-31 15:52:21 +00:00
|
|
|
if (cwd == NULL)
|
|
|
|
cwd = options_get_string(&s->options, "default-path");
|
|
|
|
|
|
|
|
skip = 1;
|
|
|
|
if (strcmp(cwd, "$HOME") == 0 || strncmp(cwd, "$HOME/", 6) == 0) {
|
|
|
|
/* User's home directory - $HOME. */
|
|
|
|
skip = 5;
|
|
|
|
goto find_home;
|
|
|
|
} else if (cwd[0] == '~' && (cwd[1] == '\0' || cwd[1] == '/')) {
|
|
|
|
/* User's home directory - ~. */
|
|
|
|
goto find_home;
|
|
|
|
} else if (cwd[0] == '-' && (cwd[1] == '\0' || cwd[1] == '/')) {
|
|
|
|
/* Session working directory. */
|
|
|
|
root = s->cwd;
|
|
|
|
goto complete_path;
|
2012-06-18 09:20:19 +00:00
|
|
|
} else if (cwd[0] == '.' && (cwd[1] == '\0' || cwd[1] == '/')) {
|
2012-01-31 15:52:21 +00:00
|
|
|
/* Server working directory. */
|
|
|
|
if (getcwd(tmp, sizeof tmp) != NULL) {
|
|
|
|
root = tmp;
|
|
|
|
goto complete_path;
|
2011-12-09 16:28:18 +00:00
|
|
|
}
|
|
|
|
return (s->cwd);
|
2012-01-31 15:52:21 +00:00
|
|
|
} else if (*cwd == '/') {
|
|
|
|
/* Absolute path. */
|
|
|
|
return (cwd);
|
|
|
|
} else {
|
|
|
|
/* Empty or relative path. */
|
2013-03-24 09:54:10 +00:00
|
|
|
if (c != NULL && c->session == NULL && c->cwd != NULL)
|
|
|
|
root = c->cwd;
|
|
|
|
else if (s->curw != NULL)
|
2012-09-24 13:05:10 +00:00
|
|
|
root = get_proc_cwd(s->curw->window->active->fd);
|
2012-01-31 15:52:21 +00:00
|
|
|
else
|
|
|
|
return (s->cwd);
|
|
|
|
skip = 0;
|
2012-02-06 17:29:29 +00:00
|
|
|
if (root != NULL)
|
|
|
|
goto complete_path;
|
2011-12-09 16:28:18 +00:00
|
|
|
}
|
2012-01-31 15:52:21 +00:00
|
|
|
|
|
|
|
return (s->cwd);
|
|
|
|
|
|
|
|
find_home:
|
|
|
|
envent = environ_find(&global_environ, "HOME");
|
|
|
|
if (envent != NULL && *envent->value != '\0')
|
|
|
|
root = envent->value;
|
|
|
|
else if ((pw = getpwuid(getuid())) != NULL)
|
|
|
|
root = pw->pw_dir;
|
|
|
|
else
|
|
|
|
return (s->cwd);
|
|
|
|
|
|
|
|
complete_path:
|
2012-04-23 22:43:09 +00:00
|
|
|
if (root[skip] == '\0') {
|
|
|
|
strlcpy(path, root, sizeof path);
|
|
|
|
return (path);
|
|
|
|
}
|
2012-01-31 15:52:21 +00:00
|
|
|
n = snprintf(path, sizeof path, "%s/%s", root, cwd + skip);
|
|
|
|
if (n > 0 && (size_t)n < sizeof path)
|
|
|
|
return (path);
|
|
|
|
return (s->cwd);
|
2011-12-09 16:28:18 +00:00
|
|
|
}
|