From 26b340964a55f754a3001c18598f670d20578777 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 4 Oct 2007 22:04:01 +0000 Subject: [PATCH] Lose command index enum in favour of runtime-calculated index. --- cmd-attach-session.c | 4 ++-- cmd-bind-key.c | 4 ++-- cmd-detach-session.c | 4 ++-- cmd-last-window.c | 4 ++-- cmd-list-keys.c | 4 ++-- cmd-list-sessions.c | 4 ++-- cmd-list-windows.c | 4 ++-- cmd-new-session.c | 4 ++-- cmd-new-window.c | 4 ++-- cmd-next-window.c | 4 ++-- cmd-previous-window.c | 4 ++-- cmd-rename-window.c | 4 ++-- cmd-select-window.c | 4 ++-- cmd-set-option.c | 4 ++-- cmd-unbind-key.c | 4 ++-- cmd.c | 54 +++++++++++++++++++++++++------------------ server-msg.c | 5 ++-- tmux.h | 21 +---------------- 18 files changed, 65 insertions(+), 75 deletions(-) diff --git a/cmd-attach-session.c b/cmd-attach-session.c index 9e87380b..18c388f3 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -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 @@ -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, diff --git a/cmd-bind-key.c b/cmd-bind-key.c index bb100634..8beeecda 100644 --- a/cmd-bind-key.c +++ b/cmd-bind-key.c @@ -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 @@ -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, diff --git a/cmd-detach-session.c b/cmd-detach-session.c index 4aff3d5c..56fa3930 100644 --- a/cmd-detach-session.c +++ b/cmd-detach-session.c @@ -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 @@ -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, diff --git a/cmd-last-window.c b/cmd-last-window.c index 81d0ce6d..4823b46b 100644 --- a/cmd-last-window.c +++ b/cmd-last-window.c @@ -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 @@ -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, diff --git a/cmd-list-keys.c b/cmd-list-keys.c index b999e594..ff750438 100644 --- a/cmd-list-keys.c +++ b/cmd-list-keys.c @@ -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 @@ -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, diff --git a/cmd-list-sessions.c b/cmd-list-sessions.c index 2ae33c5f..a9f6cef5 100644 --- a/cmd-list-sessions.c +++ b/cmd-list-sessions.c @@ -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 @@ -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, diff --git a/cmd-list-windows.c b/cmd-list-windows.c index 323867b4..6e4716d2 100644 --- a/cmd-list-windows.c +++ b/cmd-list-windows.c @@ -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 @@ -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, diff --git a/cmd-new-session.c b/cmd-new-session.c index e217ca9b..dfc894f3 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -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 @@ -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, diff --git a/cmd-new-window.c b/cmd-new-window.c index 8ac21737..7f628c48 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -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 @@ -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, diff --git a/cmd-next-window.c b/cmd-next-window.c index 073d2485..3c398500 100644 --- a/cmd-next-window.c +++ b/cmd-next-window.c @@ -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 @@ -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, diff --git a/cmd-previous-window.c b/cmd-previous-window.c index a9d6a651..1c2412b4 100644 --- a/cmd-previous-window.c +++ b/cmd-previous-window.c @@ -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 @@ -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, diff --git a/cmd-rename-window.c b/cmd-rename-window.c index 9e22d72c..2a6d4bb8 100644 --- a/cmd-rename-window.c +++ b/cmd-rename-window.c @@ -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 @@ -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, diff --git a/cmd-select-window.c b/cmd-select-window.c index cf8de36f..8044b7ac 100644 --- a/cmd-select-window.c +++ b/cmd-select-window.c @@ -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 @@ -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, diff --git a/cmd-set-option.c b/cmd-set-option.c index 586ae230..2ead2752 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -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 @@ -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, diff --git a/cmd-unbind-key.c b/cmd-unbind-key.c index 5bb80a1e..4caec2df 100644 --- a/cmd-unbind-key.c +++ b/cmd-unbind-key.c @@ -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 @@ -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, diff --git a/cmd.c b/cmd.c index e4c1afdf..cc0818e0 100644 --- a/cmd.c +++ b/cmd.c @@ -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 @@ -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); diff --git a/server-msg.c b/server-msg.c index 701300f5..41f4aa63 100644 --- a/server-msg.c +++ b/server-msg.c @@ -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 @@ -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; diff --git a/tmux.h b/tmux.h index 7f5200f6..0100f042 100644 --- a/tmux.h +++ b/tmux.h @@ -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 @@ -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;