2008-06-02 21:08:36 +00:00
|
|
|
/* $Id: cmd.c,v 1.36 2008-06-02 21:08:36 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>
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
#include <getopt.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,
|
2007-11-22 18:09:43 +00:00
|
|
|
&cmd_copy_mode_entry,
|
2007-11-16 21:12:31 +00:00
|
|
|
&cmd_detach_client_entry,
|
2007-10-25 17:44:25 +00:00
|
|
|
&cmd_has_session_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,
|
2007-10-23 09:36:19 +00:00
|
|
|
&cmd_list_clients_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,
|
2007-10-03 21:31:07 +00:00
|
|
|
&cmd_new_session_entry,
|
2007-10-03 23:32:26 +00:00
|
|
|
&cmd_new_window_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,
|
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,
|
2007-11-21 13:11:41 +00:00
|
|
|
&cmd_scroll_mode_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,
|
2007-12-06 09:46:23 +00:00
|
|
|
&cmd_set_option_entry,
|
2008-06-02 21:08:36 +00:00
|
|
|
&cmd_start_server_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,
|
2007-10-03 21:31:07 +00:00
|
|
|
NULL
|
2007-10-03 11:26:34 +00:00
|
|
|
};
|
|
|
|
|
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];
|
2007-10-03 21:31:07 +00:00
|
|
|
int opt;
|
|
|
|
|
|
|
|
*cause = NULL;
|
|
|
|
if (argc == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
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) {
|
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)
|
|
|
|
goto ambiguous;
|
2007-10-04 22:04:01 +00:00
|
|
|
entry = *entryp;
|
2007-10-03 11:26:34 +00:00
|
|
|
}
|
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
|
|
|
}
|
2007-10-03 11:26:34 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
optind = 1;
|
|
|
|
if (entry->parse == NULL) {
|
|
|
|
while ((opt = getopt(argc, argv, "")) != EOF) {
|
|
|
|
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;
|
|
|
|
if (entry->parse != NULL) {
|
2008-06-02 18:08:17 +00:00
|
|
|
if (entry->parse(cmd, &cmd->data, 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
|
|
|
|
|
|
|
void
|
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
|
|
|
{
|
2007-10-30 10:59:43 +00:00
|
|
|
cmd->entry->exec(cmd->data, 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)
|
|
|
|
cmd->entry->send(cmd->data, 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)
|
|
|
|
cmd->entry->recv(&cmd->data, b);
|
|
|
|
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)
|
2007-10-03 21:31:07 +00:00
|
|
|
cmd->entry->free(cmd->data);
|
|
|
|
xfree(cmd);
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to establish session. This looks first at the command-line argument
|
|
|
|
* if any, then sees if there is a session in the context, then finally tries
|
|
|
|
* the session data passed up from the client $TMUX variable.
|
|
|
|
*/
|
|
|
|
struct session *
|
|
|
|
cmd_find_session(struct cmd_ctx *ctx, const char *arg)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct msg_command_data *data = ctx->msgdata;
|
|
|
|
u_int i, n;
|
|
|
|
|
|
|
|
if (arg != NULL) {
|
|
|
|
if ((s = session_find(arg)) == NULL) {
|
|
|
|
ctx->error(ctx, "session not found: %s", arg);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->cursession != NULL)
|
|
|
|
return (ctx->cursession);
|
|
|
|
|
|
|
|
if (data != NULL && data->pid != -1) {
|
|
|
|
if (data->pid != getpid()) {
|
|
|
|
ctx->error(ctx, "wrong server: %lld", data->pid);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (data->idx > ARRAY_LENGTH(&sessions)) {
|
|
|
|
ctx->error(ctx, "index out of range: %d", data->idx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL) {
|
|
|
|
ctx->error(ctx, "session doesn't exist: %u", data->idx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
s = NULL;
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
if (ARRAY_ITEM(&sessions, i) != NULL) {
|
|
|
|
s = ARRAY_ITEM(&sessions, i);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s == NULL) {
|
|
|
|
ctx->error(ctx, "no sessions found");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (n != 1) {
|
|
|
|
ctx->error(ctx, "multiple sessions and session not specified");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out the client. Try the current client (if any) first, then try to
|
|
|
|
* figure it out from the argument.
|
|
|
|
*/
|
|
|
|
struct client *
|
|
|
|
cmd_find_client(unused struct cmd_ctx *ctx, const char *arg)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (ctx->curclient != NULL)
|
|
|
|
return (ctx->curclient);
|
|
|
|
|
|
|
|
if (arg == NULL) {
|
|
|
|
ctx->error(ctx, "must specify a client");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
c = ARRAY_ITEM(&clients, i);
|
|
|
|
if (c != NULL && strcmp(arg, c->tty.path) == 0)
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->error(ctx, "client not found: %s", arg);
|
|
|
|
return (NULL);
|
|
|
|
}
|