2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
15 #include "gtxkeymap.h"
20 struct keymap_map *tmap;
22 tmap = (struct keymap_map *)malloc(sizeof(struct keymap_map));
23 if (tmap != (struct keymap_map *)0)
24 memset(tmap, 0, sizeof(*tmap));
28 /* make a copy of a string; generic utility */
30 gtx_CopyString(char *aval)
33 return NULL; /* propagate null strings around */
38 BindIt(struct keymap_map *amap, int aslot, int atype, void *aproc, char *aname, void *arock)
41 struct keymap_entry *tentry;
43 if (aslot < 0 || aslot >= KEYMAP_NENTRIES)
45 tentry = &amap->entries[aslot];
47 if ((tp = tentry->name))
49 if (atype == KEYMAP_EMPTY) {
50 tentry->u.generic = NULL;
53 tentry->name = gtx_CopyString(aname);
54 tentry->u.generic = aproc;
61 keymap_BindToString(struct keymap_map *amap, char *astring,
62 int (*aproc)(void *, void *),
63 char *aname, void *arock)
68 struct keymap_map *tmap;
71 /* walk down string, building submaps if possible, until we get to function
73 while ((tc = *cptr++)) {
74 /* see if we should do submap or final function */
75 if (*cptr == 0) { /* we're peeking: already skipped command char */
76 /* last character, do final function */
77 if (!aproc) /* delete the entry */
78 code = BindIt(amap, tc, KEYMAP_EMPTY, NULL, NULL, NULL);
81 BindIt(amap, tc, KEYMAP_PROC, aproc, aname, arock);
85 /* more characters after this; do submap */
86 if (amap->entries[tc].type != KEYMAP_SUBMAP) {
87 tmap = keymap_Create();
89 BindIt(amap, tc, KEYMAP_SUBMAP, tmap, NULL, NULL);
91 tmap = amap->entries[tc].u.submap;
96 amap = tmap; /* continue processing this map */
99 /* here when all characters are gone */
103 /* delete a keymap and all of its recursively-included maps */
105 keymap_Delete(struct keymap_map *amap)
108 struct keymap_entry *tentry;
110 for (i = 0; i < KEYMAP_NENTRIES; i++) {
111 tentry = &amap->entries[i];
114 if (tentry->type == KEYMAP_SUBMAP)
115 keymap_Delete(tentry->u.submap);
122 keymap_InitState(struct keymap_state *astate, struct keymap_map *amap)
124 memset(astate, 0, sizeof(*astate));
125 astate->initMap = amap;
126 astate->currentMap = amap;
131 keymap_ProcessKey(struct keymap_state *astate, int akey, void *arock)
133 struct keymap_entry *tentry;
136 if (akey < 0 || akey >= KEYMAP_NENTRIES)
138 tentry = &astate->currentMap->entries[akey];
140 switch (tentry->type) {
142 keymap_ResetState(astate);
145 /* break commented out because of return above causing compiler warnings */
148 astate->currentMap = tentry->u.submap;
152 code = (*tentry->u.proc) (arock, tentry->rock);
153 keymap_ResetState(astate);
160 keymap_ResetState(struct keymap_state *astate)
162 return keymap_InitState(astate, astate->initMap);