Lose command index enum in favour of runtime-calculated index.

This commit is contained in:
Nicholas Marriott 2007-10-04 22:04:01 +00:00
parent 75e1e4b91e
commit 26b340964a
18 changed files with 65 additions and 75 deletions

View File

@ -1,4 +1,4 @@
/* $Id: cmd-attach-session.c,v 1.4 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-attach-session.c,v 1.5 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -37,7 +37,7 @@ struct cmd_attach_session_data {
};
const struct cmd_entry cmd_attach_session_entry = {
CMD_NEWWINDOW, "attach-session", "attach", "[-d]",
"attach-session", "attach", "[-d]",
0,
cmd_attach_session_parse,
cmd_attach_session_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-bind-key.c,v 1.3 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-bind-key.c,v 1.4 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -38,7 +38,7 @@ struct cmd_bind_key_data {
};
const struct cmd_entry cmd_bind_key_entry = {
CMD_BINDKEY, "bind-key", "bind", "key command [arguments]",
"bind-key", "bind", "key command [arguments]",
CMD_NOSESSION,
cmd_bind_key_parse,
cmd_bind_key_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-detach-session.c,v 1.4 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-detach-session.c,v 1.5 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -38,7 +38,7 @@ struct cmd_detach_session_data {
};
const struct cmd_entry cmd_detach_session_entry = {
CMD_DETACHSESSION, "detach-session", "detach", "[-a]",
"detach-session", "detach", "[-a]",
0,
cmd_detach_session_parse,
cmd_detach_session_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-last-window.c,v 1.2 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-last-window.c,v 1.3 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -30,7 +30,7 @@
void cmd_last_window_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_last_window_entry = {
CMD_LASTWINDOW, "last-window", "last", "",
"last-window", "last", "",
0,
NULL,
cmd_last_window_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-list-keys.c,v 1.2 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-list-keys.c,v 1.3 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -30,7 +30,7 @@
void cmd_list_keys_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_list_keys_entry = {
CMD_LISTKEYS, "list-keys", "lsk", "",
"list-keys", "lsk", "",
CMD_NOSESSION,
NULL,
cmd_list_keys_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-list-sessions.c,v 1.3 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-list-sessions.c,v 1.4 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -30,7 +30,7 @@
void cmd_list_sessions_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_list_sessions_entry = {
CMD_LISTSESSIONS, "list-sessions", "ls", "",
"list-sessions", "ls", "",
CMD_NOSESSION,
NULL,
cmd_list_sessions_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-list-windows.c,v 1.1 2007-10-04 11:56:19 nicm Exp $ */
/* $Id: cmd-list-windows.c,v 1.2 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -30,7 +30,7 @@
void cmd_list_windows_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_list_windows_entry = {
CMD_LISTWINDOWS, "list-windows", "lsw", 0,
"list-windows", "lsw", 0,
NULL,
NULL,
cmd_list_windows_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-new-session.c,v 1.9 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-new-session.c,v 1.10 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -39,7 +39,7 @@ struct cmd_new_session_data {
};
const struct cmd_entry cmd_new_session_entry = {
CMD_NEWSESSION, "new-session", "new", "[-d] [-n session name] [command]",
"new-session", "new", "[-d] [-n session name] [command]",
CMD_STARTSERVER|CMD_NOSESSION,
cmd_new_session_parse,
cmd_new_session_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-new-window.c,v 1.6 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-new-window.c,v 1.7 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -39,7 +39,7 @@ struct cmd_new_window_data {
};
const struct cmd_entry cmd_new_window_entry = {
CMD_NEWWINDOW, "new-window", "neww", "[-d] [-n name] [command]",
"new-window", "neww", "[-d] [-n name] [command]",
0,
cmd_new_window_parse,
cmd_new_window_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-next-window.c,v 1.2 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-next-window.c,v 1.3 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -30,7 +30,7 @@
void cmd_next_window_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_next_window_entry = {
CMD_NEXTWINDOW, "next-window", "next", "",
"next-window", "next", "",
0,
NULL,
cmd_next_window_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-previous-window.c,v 1.2 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-previous-window.c,v 1.3 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -30,7 +30,7 @@
void cmd_previous_window_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_previous_window_entry = {
CMD_PREVIOUSWINDOW, "previous-window", "prev", "",
"previous-window", "prev", "",
0,
NULL,
cmd_previous_window_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-rename-window.c,v 1.3 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-rename-window.c,v 1.4 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -39,7 +39,7 @@ struct cmd_rename_window_data {
};
const struct cmd_entry cmd_rename_window_entry = {
CMD_RENAMEWINDOW, "rename-window", "renamew", "[-i index] newname",
"rename-window", "renamew", "[-i index] newname",
0,
cmd_rename_window_parse,
cmd_rename_window_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-select-window.c,v 1.2 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-select-window.c,v 1.3 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -38,7 +38,7 @@ struct cmd_select_window_data {
};
const struct cmd_entry cmd_select_window_entry = {
CMD_SELECTWINDOW, "select-window", "selectw", "[command]",
"select-window", "selectw", "[command]",
0,
cmd_select_window_parse,
cmd_select_window_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-set-option.c,v 1.2 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-set-option.c,v 1.3 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -41,7 +41,7 @@ struct cmd_set_option_data {
};
const struct cmd_entry cmd_set_option_entry = {
CMD_SETOPTION, "set-option", "set", "option value",
"set-option", "set", "option value",
CMD_NOSESSION,
cmd_set_option_parse,
cmd_set_option_exec,

View File

@ -1,4 +1,4 @@
/* $Id: cmd-unbind-key.c,v 1.3 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd-unbind-key.c,v 1.4 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -38,7 +38,7 @@ struct cmd_unbind_key_data {
};
const struct cmd_entry cmd_unbind_key_entry = {
CMD_UNBINDKEY, "unbind-key", "unbind", "key",
"unbind-key", "unbind", "key",
CMD_NOSESSION,
cmd_unbind_key_parse,
cmd_unbind_key_exec,

54
cmd.c
View File

@ -1,4 +1,4 @@
/* $Id: cmd.c,v 1.13 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: cmd.c,v 1.14 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -45,7 +45,7 @@ const struct cmd_entry *cmd_table[] = {
struct cmd *
cmd_parse(int argc, char **argv, char **cause)
{
const struct cmd_entry **this, *entry;
const struct cmd_entry **entryp, *entry;
struct cmd *cmd;
int opt;
@ -54,19 +54,19 @@ cmd_parse(int argc, char **argv, char **cause)
return (NULL);
entry = NULL;
for (this = cmd_table; *this != NULL; this++) {
if (strcmp((*this)->alias, argv[0]) == 0) {
entry = *this;
for (entryp = cmd_table; *entryp != NULL; entryp++) {
if (strcmp((*entryp)->alias, argv[0]) == 0) {
entry = *entryp;
break;
}
if (strncmp((*this)->name, argv[0], strlen(argv[0])) != 0)
if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
continue;
if (entry != NULL) {
xasprintf(cause, "ambiguous command: %s", argv[0]);
return (NULL);
}
entry = *this;
entry = *entryp;
}
if (entry == NULL) {
xasprintf(cause, "unknown command: %s", argv[0]);
@ -111,34 +111,44 @@ cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
void
cmd_send(struct cmd *cmd, struct buffer *b)
{
buffer_write(b, &cmd->entry->type, sizeof cmd->entry->type);
const struct cmd_entry **entryp;
u_int n;
if (cmd->entry->send == NULL)
return;
return (cmd->entry->send(cmd->data, b));
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 **this, *entry;
const struct cmd_entry **entryp;
struct cmd *cmd;
enum cmd_type type;
u_int m, n;
buffer_read(b, &type, sizeof type);
buffer_read(b, &m, sizeof m);
entry = NULL;
for (this = cmd_table; *this != NULL; this++) {
if ((*this)->type == type) {
entry = *this;
n = 0;
for (entryp = cmd_table; *entryp != NULL; entryp++) {
if (n == m)
break;
}
n++;
}
if (*this == NULL)
return (NULL);
if (*entryp == NULL)
fatalx("command not found");
cmd = xmalloc(sizeof *cmd);
cmd->entry = entry;
cmd->entry = *entryp;
if (cmd->entry->recv != NULL)
cmd->entry->recv(&cmd->data, b);

View File

@ -1,4 +1,4 @@
/* $Id: server-msg.c,v 1.24 2007-10-04 19:03:52 nicm Exp $ */
/* $Id: server-msg.c,v 1.25 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -115,8 +115,7 @@ server_msg_fn_command(struct hdr *hdr, struct client *c)
buffer_read(c->in, &data, sizeof data);
cmd = cmd_recv(c->in);
log_debug("got command %u %s from client %d",
cmd->entry->type, cmd->entry->name, c->fd);
log_debug("got command %s from client %d", cmd->entry->name, c->fd);
ctx.error = server_msg_fn_command_error;
ctx.print = server_msg_fn_command_print;

21
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.49 2007-10-04 21:48:11 nicm Exp $ */
/* $Id: tmux.h,v 1.50 2007-10-04 22:04:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -456,24 +456,6 @@ struct client_ctx {
};
/* Key/command line command. */
enum cmd_type {
CMD_ATTACHSESSION,
CMD_BINDKEY,
CMD_DETACHSESSION,
CMD_LASTWINDOW,
CMD_LISTKEYS,
CMD_LISTSESSIONS,
CMD_LISTWINDOWS,
CMD_NEWSESSION,
CMD_NEWWINDOW,
CMD_NEXTWINDOW,
CMD_PREVIOUSWINDOW,
CMD_RENAMEWINDOW,
CMD_SELECTWINDOW,
CMD_SETOPTION,
CMD_UNBINDKEY,
};
struct cmd_ctx {
struct client *client;
struct session *session;
@ -486,7 +468,6 @@ struct cmd_ctx {
};
struct cmd_entry {
enum cmd_type type;
const char *name;
const char *alias;
const char *usage;