From 4846ad16579f7fcf06fe01e03a56815b2d8c4a93 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Wed, 7 Jan 2009 22:52:33 +0000 Subject: [PATCH] Introduce nitems() and use it; use bsearch. --- client-msg.c | 5 ++- input-keys.c | 7 ++-- input.c | 90 ++++++++++++++++++++++++++++---------------------- key-bindings.c | 4 +-- key-string.c | 7 ++-- mode-key.c | 8 ++--- server-msg.c | 7 ++-- tmux.h | 5 ++- tty-keys.c | 5 ++- 9 files changed, 73 insertions(+), 65 deletions(-) diff --git a/client-msg.c b/client-msg.c index 65287f50..c1e28ecb 100644 --- a/client-msg.c +++ b/client-msg.c @@ -1,4 +1,4 @@ -/* $Id: client-msg.c,v 1.14 2007-12-06 09:46:21 nicm Exp $ */ +/* $Id: client-msg.c,v 1.15 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -43,7 +43,6 @@ struct client_msg client_msg_table[] = { { MSG_EXIT, client_msg_fn_exit }, { MSG_EXITED, client_msg_fn_exited } }; -#define NCLIENTMSG (sizeof client_msg_table / sizeof client_msg_table[0]) int client_msg_dispatch(struct client_ctx *cctx, char **error) @@ -59,7 +58,7 @@ client_msg_dispatch(struct client_ctx *cctx, char **error) return (1); buffer_remove(cctx->srv_in, sizeof hdr); - for (i = 0; i < NCLIENTMSG; i++) { + for (i = 0; i < nitems(client_msg_table); i++) { msg = client_msg_table + i; if (msg->type == hdr.type) { if (msg->fn(&hdr, cctx, error) != 0) diff --git a/input-keys.c b/input-keys.c index 69e0f859..cc7e8f8f 100644 --- a/input-keys.c +++ b/input-keys.c @@ -1,4 +1,4 @@ -/* $Id: input-keys.c,v 1.15 2008-12-22 17:26:51 nicm Exp $ */ +/* $Id: input-keys.c,v 1.16 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -94,7 +94,6 @@ struct { { KEYC_KP4_0, "\033Op", 0 }, { KEYC_KP4_2, "\033On", 0 }, }; -#define NINPUTKEYS (sizeof input_keys / sizeof input_keys[0]) /* Translate a key code from client into an output key sequence. */ void @@ -114,7 +113,7 @@ input_key(struct window *w, int key) return; } - for (i = 0; i < NINPUTKEYS; i++) { + for (i = 0; i < nitems(input_keys); i++) { if ((input_keys[i].flags & INPUTKEY_KEYPAD) && !(w->screen->mode & MODE_KKEYPAD)) continue; @@ -124,7 +123,7 @@ input_key(struct window *w, int key) if (input_keys[i].key == key) break; } - if (i == NINPUTKEYS) { + if (i == nitems(input_keys)) { log_debug2("key 0x%x missing", key); return; } diff --git a/input.c b/input.c index 9f5cb6b0..1d9f529b 100644 --- a/input.c +++ b/input.c @@ -1,4 +1,4 @@ -/* $Id: input.c,v 1.69 2008-12-08 16:19:51 nicm Exp $ */ +/* $Id: input.c,v 1.70 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -82,6 +82,43 @@ void input_handle_sequence_decstbm(struct input_ctx *); void input_handle_sequence_sgr(struct input_ctx *); void input_handle_sequence_dsr(struct input_ctx *); +int input_sequence_cmp(const void *, const void *); + +struct input_sequence_entry { + u_char ch; + void (*fn)(struct input_ctx *); +}; +const struct input_sequence_entry input_sequence_table[] = { + { '@', input_handle_sequence_ich }, + { 'A', input_handle_sequence_cuu }, + { 'B', input_handle_sequence_cud }, + { 'C', input_handle_sequence_cuf }, + { 'D', input_handle_sequence_cub }, + { 'G', input_handle_sequence_hpa }, + { 'H', input_handle_sequence_cup }, + { 'J', input_handle_sequence_ed }, + { 'K', input_handle_sequence_el }, + { 'L', input_handle_sequence_il }, + { 'M', input_handle_sequence_dl }, + { 'P', input_handle_sequence_dch }, + { 'd', input_handle_sequence_vpa }, + { 'f', input_handle_sequence_cup }, + { 'h', input_handle_sequence_sm }, + { 'l', input_handle_sequence_rm }, + { 'm', input_handle_sequence_sgr }, + { 'n', input_handle_sequence_dsr }, + { 'r', input_handle_sequence_decstbm }, +}; + +int +input_sequence_cmp(const void *a, const void *b) +{ + int ai = ((const struct input_sequence_entry *) a)->ch; + int bi = ((const struct input_sequence_entry *) b)->ch; + + return (ai - bi); +} + int input_new_argument(struct input_ctx *ictx) { @@ -572,7 +609,6 @@ input_handle_c0_control(u_char ch, struct input_ctx *ictx) screen_write_cursorleft(&ictx->ctx, 1); break; case '\011': /* TAB */ - /* XXX right? */ s->cx = ((s->cx / 8) * 8) + 8; if (s->cx > screen_size_x(s) - 1) { s->cx = 0; @@ -702,33 +738,10 @@ input_handle_standard_two(u_char ch, struct input_ctx *ictx) void input_handle_sequence(u_char ch, struct input_ctx *ictx) { - static const struct { - u_char ch; - void (*fn)(struct input_ctx *); - } table[] = { - { '@', input_handle_sequence_ich }, - { 'A', input_handle_sequence_cuu }, - { 'B', input_handle_sequence_cud }, - { 'C', input_handle_sequence_cuf }, - { 'D', input_handle_sequence_cub }, - { 'G', input_handle_sequence_hpa }, - { 'H', input_handle_sequence_cup }, - { 'J', input_handle_sequence_ed }, - { 'K', input_handle_sequence_el }, - { 'L', input_handle_sequence_il }, - { 'M', input_handle_sequence_dl }, - { 'P', input_handle_sequence_dch }, - { 'd', input_handle_sequence_vpa }, - { 'f', input_handle_sequence_cup }, - { 'h', input_handle_sequence_sm }, - { 'l', input_handle_sequence_rm }, - { 'm', input_handle_sequence_sgr }, - { 'n', input_handle_sequence_dsr }, - { 'r', input_handle_sequence_decstbm }, - }; - struct screen *s = ictx->ctx.s; - u_int i; - struct input_arg *iarg; + struct input_sequence_entry *entry, find; + struct screen *s = ictx->ctx.s; + u_int i; + struct input_arg *iarg; log_debug2("-- sq %zu: %hhu (%c): %u [sx=%u, sy=%u, cx=%u, cy=%u, " "ru=%u, rl=%u]", ictx->off, ch, ch, ARRAY_LENGTH(&ictx->args), @@ -739,16 +752,15 @@ input_handle_sequence(u_char ch, struct input_ctx *ictx) if (*iarg->data != '\0') log_debug2(" ++ %u: %s", i, iarg->data); } - - /* XXX bsearch? */ - for (i = 0; i < (sizeof table / sizeof table[0]); i++) { - if (table[i].ch == ch) { - table[i].fn(ictx); - return; - } - } - - log_debug("unknown sq: %c (%hhu %hhu)", ch, ch, ictx->private); + + find.ch = ch; + entry = bsearch(&find, + input_sequence_table, nitems(input_sequence_table), + sizeof input_sequence_table[0], input_sequence_cmp); + if (entry != NULL) + entry->fn(ictx); + else + log_debug("unknown sq: %c (%hhu %hhu)", ch, ch, ictx->private); } void diff --git a/key-bindings.c b/key-bindings.c index 7c0698d7..3ffd5a6b 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -1,4 +1,4 @@ -/* $Id: key-bindings.c,v 1.40 2009-01-06 14:47:56 nicm Exp $ */ +/* $Id: key-bindings.c,v 1.41 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -111,7 +111,7 @@ key_bindings_init(void) SPLAY_INIT(&key_bindings); - for (i = 0; i < (sizeof table / sizeof table[0]); i++) { + for (i = 0; i < nitems(table); i++) { cmd = xmalloc(sizeof *cmd); cmd->entry = table[i].entry; cmd->data = NULL; diff --git a/key-string.c b/key-string.c index 5e45d301..981ba21d 100644 --- a/key-string.c +++ b/key-string.c @@ -1,4 +1,4 @@ -/* $Id: key-string.c,v 1.8 2008-12-16 08:25:48 nicm Exp $ */ +/* $Id: key-string.c,v 1.9 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -193,7 +193,6 @@ struct { { "KP0", KEYC_KP4_0 }, { "KP.", KEYC_KP4_2 }, }; -#define NKEYSTRINGS (sizeof key_string_table / sizeof key_string_table[0]) int key_string_lookup_string(const char *string) @@ -236,7 +235,7 @@ key_string_lookup_string(const char *string) return (KEYC_ADDESCAPE(key)); } - for (i = 0; i < NKEYSTRINGS; i++) { + for (i = 0; i < nitems(key_string_table); i++) { if (strcasecmp(string, key_string_table[i].string) == 0) return (key_string_table[i].key); } @@ -274,7 +273,7 @@ key_string_lookup_key(int key) return (tmp); } - for (i = 0; i < NKEYSTRINGS; i++) { + for (i = 0; i < nitems(key_string_table); i++) { if (key == key_string_table[i].key) return (key_string_table[i].string); } diff --git a/mode-key.c b/mode-key.c index d8412ba9..f33202a8 100644 --- a/mode-key.c +++ b/mode-key.c @@ -1,4 +1,4 @@ -/* $Id: mode-key.c,v 1.3 2008-07-03 15:26:32 nicm Exp $ */ +/* $Id: mode-key.c,v 1.4 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -48,7 +48,6 @@ const struct mode_key_entry mode_key_table_vi[] = { { MODEKEY_UP, 'k' }, { MODEKEY_UP, KEYC_UP }, }; -#define NKEYVI (sizeof mode_key_table_vi / sizeof mode_key_table_vi[0]) const struct mode_key_entry mode_key_table_emacs[] = { { MODEKEY_BOL, '\001' }, @@ -70,7 +69,6 @@ const struct mode_key_entry mode_key_table_emacs[] = { { MODEKEY_STARTSEL, '\000' }, { MODEKEY_UP, KEYC_UP }, }; -#define NKEYEMACS (sizeof mode_key_table_emacs / sizeof mode_key_table_emacs[0]) enum mode_key mode_key_lookup(int table, int key) @@ -80,10 +78,10 @@ mode_key_lookup(int table, int key) if (table == MODEKEY_EMACS) { ptr = mode_key_table_emacs; - n = NKEYEMACS; + n = nitems(mode_key_table_emacs); } else if (table == MODEKEY_VI) { ptr = mode_key_table_vi; - n = NKEYVI; + n = nitems(mode_key_table_vi); } else return (MODEKEY_NONE); diff --git a/server-msg.c b/server-msg.c index 85344f72..e8d85033 100644 --- a/server-msg.c +++ b/server-msg.c @@ -1,4 +1,4 @@ -/* $Id: server-msg.c,v 1.52 2008-09-26 06:45:27 nicm Exp $ */ +/* $Id: server-msg.c,v 1.53 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -48,7 +48,6 @@ const struct server_msg server_msg_table[] = { { MSG_RESIZE, server_msg_fn_resize }, { MSG_EXITING, server_msg_fn_exiting } }; -#define NSERVERMSG (sizeof server_msg_table / sizeof server_msg_table[0]) int server_msg_dispatch(struct client *c) @@ -66,7 +65,7 @@ server_msg_dispatch(struct client *c) return (0); buffer_remove(c->in, sizeof hdr); - for (i = 0; i < NSERVERMSG; i++) { + for (i = 0; i < nitems(server_msg_table); i++) { msg = server_msg_table + i; if (msg->type == hdr.type) { if ((n = msg->fn(&hdr, c)) != 0) @@ -74,7 +73,7 @@ server_msg_dispatch(struct client *c) break; } } - if (i == NSERVERMSG) + if (i == nitems(server_msg_table)) fatalx("unexpected message"); } } diff --git a/tmux.h b/tmux.h index 11294f38..7adf228c 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.208 2009-01-07 19:53:17 nicm Exp $ */ +/* $Id: tmux.h,v 1.209 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -135,6 +135,9 @@ extern const char *__progname; #define printflike3 __attribute__ ((format (printf, 3, 4))) #define printflike4 __attribute__ ((format (printf, 4, 5))) +/* Number of items in array. */ +#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) + /* Buffer macros. */ #define BUFFER_USED(b) ((b)->size) #define BUFFER_FREE(b) ((b)->space - (b)->off - (b)->size) diff --git a/tty-keys.c b/tty-keys.c index 87c26cb4..ca96300d 100644 --- a/tty-keys.c +++ b/tty-keys.c @@ -1,4 +1,4 @@ -/* $Id: tty-keys.c,v 1.11 2008-09-26 06:45:28 nicm Exp $ */ +/* $Id: tty-keys.c,v 1.12 2009-01-07 22:52:33 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -205,7 +205,6 @@ struct { { "-\033Op", KEYC_KP4_0 }, { "-\033On", KEYC_KP4_2 }, }; -#define NTTYKEYS (sizeof tty_keys / sizeof tty_keys[0]) RB_GENERATE(tty_keys, tty_key, entry, tty_keys_cmp); @@ -227,7 +226,7 @@ tty_keys_init(struct tty *tty) RB_INIT(&tty->ktree); tty->ksize = 0; - for (i = 0; i < NTTYKEYS; i++) { + for (i = 0; i < nitems(tty_keys); i++) { if (*tty_keys[i].name == '-') s = tty_keys[i].name + 1; else {