src/keys.c: Use generic list implementation
Be consistent and replace the custom linked list implementation we used here by the generic list implementation we use everywhere else. This reduces code complexity, while slightly improving memory overhead. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
parent
50be1a1772
commit
d0b0ed3c20
113
src/keys.c
113
src/keys.c
@ -46,13 +46,7 @@ struct keydef_s {
|
|||||||
char *binding;
|
char *binding;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct key_str_s {
|
static llist_t keys[NBKEYS];
|
||||||
char *str;
|
|
||||||
struct key_str_s *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct key_str_s *keys[NBKEYS];
|
|
||||||
|
|
||||||
static enum key actions[MAXKEYVAL];
|
static enum key actions[MAXKEYVAL];
|
||||||
|
|
||||||
static struct keydef_s keydef[NBKEYS] = {
|
static struct keydef_s keydef[NBKEYS] = {
|
||||||
@ -135,28 +129,25 @@ keys_init (void)
|
|||||||
|
|
||||||
for (i = 0; i < MAXKEYVAL; i++)
|
for (i = 0; i < MAXKEYVAL; i++)
|
||||||
actions[i] = KEY_UNDEF;
|
actions[i] = KEY_UNDEF;
|
||||||
memset (keys, 0, NBKEYS);
|
for (i = 0; i < NBKEYS; i++)
|
||||||
|
LLIST_INIT (&keys[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
key_free (char *s)
|
||||||
|
{
|
||||||
|
mem_free (s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
keys_free (void)
|
keys_free (void)
|
||||||
{
|
{
|
||||||
struct key_str_s *o, **i;
|
int i;
|
||||||
int key;
|
|
||||||
|
|
||||||
for (key = 0; key < NBKEYS; key++)
|
for (i = 0; i < NBKEYS; i++)
|
||||||
{
|
{
|
||||||
if (keys[key] == NULL)
|
LLIST_FREE_INNER (&keys[i], key_free);
|
||||||
continue;
|
LLIST_FREE (&keys[i]);
|
||||||
|
|
||||||
i = &keys[key];
|
|
||||||
while (*i)
|
|
||||||
{
|
|
||||||
o = *i;
|
|
||||||
*i = o->next;
|
|
||||||
mem_free (o->str);
|
|
||||||
mem_free (o);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,29 +217,10 @@ keys_getch (WINDOW *win, int *count)
|
|||||||
static void
|
static void
|
||||||
add_key_str (enum key action, int key)
|
add_key_str (enum key action, int key)
|
||||||
{
|
{
|
||||||
struct key_str_s *new, **i;
|
|
||||||
|
|
||||||
if (action < 0 || action > NBKEYS)
|
if (action < 0 || action > NBKEYS)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
new = mem_malloc (sizeof (struct key_str_s));
|
LLIST_ADD (&keys[action], mem_strdup (keys_int2str (key)));
|
||||||
new->str = mem_strdup (keys_int2str (key));
|
|
||||||
new->next = NULL;
|
|
||||||
i = &keys[action];
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (*i == NULL)
|
|
||||||
{
|
|
||||||
*i = new;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if ((*i)->next == NULL)
|
|
||||||
{
|
|
||||||
(*i)->next = new;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i = &(*i)->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -268,22 +240,20 @@ keys_assign_binding (int key, enum key action)
|
|||||||
static void
|
static void
|
||||||
del_key_str (enum key action, int key)
|
del_key_str (enum key action, int key)
|
||||||
{
|
{
|
||||||
struct key_str_s *old, **i;
|
llist_item_t *i;
|
||||||
char oldstr[BUFSIZ];
|
char oldstr[BUFSIZ];
|
||||||
|
|
||||||
if (action < 0 || action > NBKEYS)
|
if (action < 0 || action > NBKEYS)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
strncpy (oldstr, keys_int2str (key), BUFSIZ);
|
strncpy (oldstr, keys_int2str (key), BUFSIZ);
|
||||||
for (i = &keys[action]; *i; i = &(*i)->next)
|
|
||||||
|
LLIST_FOREACH (&keys[action], i)
|
||||||
{
|
{
|
||||||
if (!strcmp ((*i)->str, oldstr))
|
if (strcmp (LLIST_GET_DATA (i), oldstr) == 0)
|
||||||
{
|
{
|
||||||
old = *i;
|
LLIST_REMOVE (&keys[action], i);
|
||||||
*i = old->next;
|
return;
|
||||||
mem_free (old->str);
|
|
||||||
mem_free (old);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,9 +261,7 @@ del_key_str (enum key action, int key)
|
|||||||
void
|
void
|
||||||
keys_remove_binding (int key, enum key action)
|
keys_remove_binding (int key, enum key action)
|
||||||
{
|
{
|
||||||
if (key < 0 || key > MAXKEYVAL)
|
if (key >= 0 && key <= MAXKEYVAL)
|
||||||
return;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
actions[key] = KEY_UNDEF;
|
actions[key] = KEY_UNDEF;
|
||||||
del_key_str (action, key);
|
del_key_str (action, key);
|
||||||
@ -378,52 +346,43 @@ keys_int2str (int key)
|
|||||||
int
|
int
|
||||||
keys_action_count_keys (enum key action)
|
keys_action_count_keys (enum key action)
|
||||||
{
|
{
|
||||||
struct key_str_s *key;
|
llist_item_t *i;
|
||||||
int i;
|
int n = 0;
|
||||||
|
|
||||||
i = 0;
|
LLIST_FOREACH (&keys[action], i)
|
||||||
for (key = keys[action]; key; key = key->next)
|
n++;
|
||||||
i++;
|
|
||||||
|
|
||||||
return i;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
keys_action_firstkey (enum key action)
|
keys_action_firstkey (enum key action)
|
||||||
{
|
{
|
||||||
return (keys[action] != NULL) ? keys[action]->str : "XXX";
|
char *s = LLIST_GET_DATA (LLIST_FIRST (&keys[action]));
|
||||||
|
return (s != NULL) ? s : "XXX";
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
keys_action_nkey (enum key action, int keynum)
|
keys_action_nkey (enum key action, int keynum)
|
||||||
{
|
{
|
||||||
struct key_str_s *key;
|
return LLIST_GET_DATA (LLIST_NTH (&keys[action], keynum));
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
for (key = keys[action]; key; key = key->next)
|
|
||||||
{
|
|
||||||
if (i == keynum)
|
|
||||||
return key->str;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
keys_action_allkeys (enum key action)
|
keys_action_allkeys (enum key action)
|
||||||
{
|
{
|
||||||
|
llist_item_t *i;
|
||||||
static char keystr[BUFSIZ];
|
static char keystr[BUFSIZ];
|
||||||
struct key_str_s *i;
|
|
||||||
const char *CHAR_SPACE = " ";
|
const char *CHAR_SPACE = " ";
|
||||||
|
|
||||||
if (keys[action] == NULL)
|
if (!LLIST_FIRST (&keys[action]))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
keystr[0] = '\0';
|
keystr[0] = '\0';
|
||||||
for (i = keys[action]; i; i = i->next)
|
LLIST_FOREACH (&keys[action], i)
|
||||||
{
|
{
|
||||||
const int MAXLEN = sizeof (keystr) - 1 - strlen (keystr);
|
const int MAXLEN = sizeof (keystr) - 1 - strlen (keystr);
|
||||||
strncat (keystr, i->str, MAXLEN - 1);
|
strncat (keystr, LLIST_GET_DATA (i), MAXLEN - 1);
|
||||||
strncat (keystr, CHAR_SPACE, 1);
|
strncat (keystr, CHAR_SPACE, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -624,7 +583,7 @@ keys_check_missing_bindings (void)
|
|||||||
|
|
||||||
for (i = 0; i < NBKEYS; i++)
|
for (i = 0; i < NBKEYS; i++)
|
||||||
{
|
{
|
||||||
if (keys[i] == NULL)
|
if (!LLIST_FIRST (&keys[i]))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -637,7 +596,7 @@ keys_fill_missing (void)
|
|||||||
|
|
||||||
for (i = 0; i < NBKEYS; i++)
|
for (i = 0; i < NBKEYS; i++)
|
||||||
{
|
{
|
||||||
if (keys[i] == NULL)
|
if (!LLIST_FIRST (&keys[i]))
|
||||||
{
|
{
|
||||||
char *p, tmpbuf[BUFSIZ];
|
char *p, tmpbuf[BUFSIZ];
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user