tmux/cmd.c

206 lines
4.1 KiB
C
Raw Normal View History

2007-10-25 17:44:25 +00:00
/* $Id: cmd.c,v 1.20 2007-10-25 17:44:24 nicm Exp $ */
/*
* 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 <getopt.h>
2007-10-03 11:26:34 +00:00
#include <string.h>
#include "tmux.h"
const struct cmd_entry *cmd_table[] = {
&cmd_attach_session_entry,
2007-10-04 09:30:53 +00:00
&cmd_bind_key_entry,
&cmd_detach_session_entry,
2007-10-25 17:44:25 +00:00
&cmd_has_session_entry,
&cmd_kill_window_entry,
2007-10-04 09:30:53 +00:00
&cmd_last_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,
&cmd_list_sessions_entry,
2007-10-04 11:23:17 +00:00
&cmd_list_windows_entry,
&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,
&cmd_previous_window_entry,
2007-10-19 09:21:26 +00:00
&cmd_refresh_session_entry,
2007-10-04 10:39:07 +00:00
&cmd_rename_window_entry,
2007-10-04 10:54:21 +00:00
&cmd_select_window_entry,
2007-10-12 13:03:58 +00:00
&cmd_send_prefix_entry,
&cmd_set_option_entry,
2007-10-04 09:30:53 +00:00
&cmd_unbind_key_entry,
NULL
2007-10-03 11:26:34 +00:00
};
struct cmd *
cmd_parse(int argc, char **argv, char **cause)
2007-10-03 11:26:34 +00:00
{
const struct cmd_entry **entryp, *entry;
struct cmd *cmd;
int opt;
*cause = NULL;
if (argc == 0)
return (NULL);
entry = NULL;
for (entryp = cmd_table; *entryp != NULL; entryp++) {
2007-10-12 13:51:44 +00:00
if ((*entryp)->alias != NULL &&
strcmp((*entryp)->alias, argv[0]) == 0) {
entry = *entryp;
2007-10-03 11:26:34 +00:00
break;
}
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
continue;
if (entry != NULL) {
xasprintf(cause, "ambiguous command: %s", argv[0]);
return (NULL);
2007-10-03 11:26:34 +00:00
}
entry = *entryp;
2007-10-03 11:26:34 +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
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
}
argc -= optind;
argv += optind;
if (argc != 0)
goto usage;
2007-10-03 11:26:34 +00:00
}
cmd = xmalloc(sizeof *cmd);
cmd->entry = entry;
if (entry->parse != NULL) {
if (entry->parse(&cmd->data, argc, argv, cause) != 0) {
xfree(cmd);
return (NULL);
}
}
return (cmd);
2007-10-03 11:26:34 +00:00
usage:
usage(cause, "%s %s", entry->name, entry->usage);
return (NULL);
2007-10-03 11:26:34 +00:00
}
void
cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
{
return (cmd->entry->exec(cmd->data, ctx));
}
void
cmd_send(struct cmd *cmd, struct buffer *b)
{
const struct cmd_entry **entryp;
u_int n;
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);
}
struct cmd *
cmd_recv(struct buffer *b)
{
const struct cmd_entry **entryp;
struct cmd *cmd;
u_int m, n;
buffer_read(b, &m, sizeof m);
n = 0;
for (entryp = cmd_table; *entryp != NULL; entryp++) {
if (n == m)
break;
n++;
}
if (*entryp == NULL)
fatalx("command not found");
cmd = xmalloc(sizeof *cmd);
cmd->entry = *entryp;
if (cmd->entry->recv != NULL)
cmd->entry->recv(&cmd->data, b);
return (cmd);
}
void
cmd_free(struct cmd *cmd)
{
2007-10-04 09:30:53 +00:00
if (cmd->data != NULL && cmd->entry->free != NULL)
cmd->entry->free(cmd->data);
xfree(cmd);
}
void
cmd_send_string(struct buffer *b, const char *s)
{
size_t n;
if (s == NULL) {
n = 0;
buffer_write(b, &n, sizeof n);
return;
}
n = strlen(s) + 1;
buffer_write(b, &n, sizeof n);
buffer_write(b, s, n);
}
char *
cmd_recv_string(struct buffer *b)
{
char *s;
size_t n;
buffer_read(b, &n, sizeof n);
if (n == 0)
return (NULL);
s = xmalloc(n);
buffer_read(b, s, n);
s[n - 1] = '\0';
return (s);
}