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>
19 #include "gtxkeymap.h"
24 register struct keymap_map *tmap;
26 tmap = (struct keymap_map *)malloc(sizeof(struct keymap_map));
27 if (tmap != (struct keymap_map *)0)
28 memset(tmap, 0, sizeof(*tmap));
32 /* make a copy of a string; generic utility */
34 gtx_CopyString(register char *aval)
39 return NULL; /* propagate null strings around */
40 tp = (char *)malloc(strlen(aval) + 1);
47 BindIt(struct keymap_map *amap, int aslot, int atype, void *aproc, char *aname, void *arock)
50 register struct keymap_entry *tentry;
52 if (aslot < 0 || aslot >= KEYMAP_NENTRIES)
54 tentry = &amap->entries[aslot];
56 if ((tp = tentry->name))
58 if (atype == KEYMAP_EMPTY) {
59 tentry->u.generic = NULL;
62 tentry->name = gtx_CopyString(aname);
63 tentry->u.generic = aproc;
70 keymap_BindToString(struct keymap_map *amap, char *astring,
71 int (*aproc)(void *, void *),
72 char *aname, void *arock)
76 register afs_int32 code;
77 struct keymap_map *tmap;
80 /* walk down string, building submaps if possible, until we get to function
82 while ((tc = *cptr++)) {
83 /* see if we should do submap or final function */
84 if (*cptr == 0) { /* we're peeking: already skipped command char */
85 /* last character, do final function */
86 if (!aproc) /* delete the entry */
87 code = BindIt(amap, tc, KEYMAP_EMPTY, NULL, NULL, NULL);
90 BindIt(amap, tc, KEYMAP_PROC, aproc, aname, arock);
94 /* more characters after this; do submap */
95 if (amap->entries[tc].type != KEYMAP_SUBMAP) {
96 tmap = keymap_Create();
98 BindIt(amap, tc, KEYMAP_SUBMAP, tmap, NULL, NULL);
100 tmap = amap->entries[tc].u.submap;
105 amap = tmap; /* continue processing this map */
108 /* here when all characters are gone */
112 /* delete a keymap and all of its recursively-included maps */
114 keymap_Delete(struct keymap_map *amap)
117 register struct keymap_entry *tentry;
119 for (i = 0; i < KEYMAP_NENTRIES; i++) {
120 tentry = &amap->entries[i];
123 if (tentry->type == KEYMAP_SUBMAP)
124 keymap_Delete(tentry->u.submap);
131 keymap_InitState(struct keymap_state *astate, struct keymap_map *amap)
133 memset(astate, 0, sizeof(*astate));
134 astate->initMap = amap;
135 astate->currentMap = amap;
140 keymap_ProcessKey(struct keymap_state *astate, int akey, void *arock)
142 register struct keymap_entry *tentry;
143 register afs_int32 code;
145 if (akey < 0 || akey >= KEYMAP_NENTRIES)
147 tentry = &astate->currentMap->entries[akey];
149 switch (tentry->type) {
151 keymap_ResetState(astate);
154 /* break commented out because of return above causing compiler warnings */
157 astate->currentMap = tentry->u.submap;
161 code = (*tentry->u.proc) (arock, tentry->rock);
162 keymap_ResetState(astate);
169 keymap_ResetState(struct keymap_state *astate)
171 return keymap_InitState(astate, astate->initMap);