Introduce nitems() and use it; use bsearch.

This commit is contained in:
Nicholas Marriott 2009-01-07 22:52:33 +00:00
parent 4924d8e1e2
commit 4846ad1657
9 changed files with 73 additions and 65 deletions

View File

@ -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 <nicm@users.sourceforge.net>
@ -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)

View File

@ -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 <nicm@users.sourceforge.net>
@ -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;
}

80
input.c
View File

@ -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 <nicm@users.sourceforge.net>
@ -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,30 +738,7 @@ 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 input_sequence_entry *entry, find;
struct screen *s = ictx->ctx.s;
u_int i;
struct input_arg *iarg;
@ -740,14 +753,13 @@ input_handle_sequence(u_char ch, struct input_ctx *ictx)
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;
}
}
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);
}

View File

@ -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 <nicm@users.sourceforge.net>
@ -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;

View File

@ -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 <nicm@users.sourceforge.net>
@ -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);
}

View File

@ -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 <nicm@users.sourceforge.net>
@ -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);

View File

@ -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 <nicm@users.sourceforge.net>
@ -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");
}
}

5
tmux.h
View File

@ -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 <nicm@users.sourceforge.net>
@ -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)

View File

@ -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 <nicm@users.sourceforge.net>
@ -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 {