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 <afs/param.h>
11 #include "gtxkeymap.h"
13 struct keymap_map *keymap_Create() {
14 register struct keymap_map *tmap;
16 tmap = (struct keymap_map *) malloc(sizeof(struct keymap_map));
17 if (tmap != (struct keymap_map *)0)
18 bzero(tmap, sizeof(*tmap));
22 /* make a copy of a string; generic utility */
23 char *gtx_CopyString(aval)
24 register char *aval; {
27 if (!aval) return (char *) 0; /* propagate null strings around */
28 tp = (char *) malloc(strlen(aval)+1);
34 static int BindIt(amap, aslot, atype, aproc, aname, arock)
35 struct keymap_map *amap;
42 register struct keymap_entry *tentry;
44 if (aslot < 0 || aslot >= KEYMAP_NENTRIES) return -1;
45 tentry = &amap->entries[aslot];
49 if (atype == KEYMAP_EMPTY) {
50 tentry->u.generic = (char *) 0;
51 tentry->name = (char *) 0;
54 tentry->name = gtx_CopyString(aname);
55 tentry->u.generic = aproc;
61 keymap_BindToString(amap, astring, aproc, aname, arock)
62 register struct keymap_map *amap;
69 register afs_int32 code;
70 struct keymap_map *tmap;
73 /* walk down string, building submaps if possible, until we get to function
75 while (tc = *cptr++) {
76 /* see if we should do submap or final function */
77 if (*cptr == 0) { /* we're peeking: already skipped command char */
78 /* last character, do final function */
79 if (!aproc) /* delete the entry */
80 code = BindIt(amap, tc, KEYMAP_EMPTY, (char *) 0,
81 (char *) 0, (char *) 0);
83 code = BindIt(amap, tc, KEYMAP_PROC, (char *) aproc, aname, arock);
84 if (code) return code;
87 /* more characters after this; do submap */
88 if (amap->entries[tc].type != KEYMAP_SUBMAP) {
89 tmap = keymap_Create();
90 code = BindIt(amap, tc, KEYMAP_SUBMAP, (char *) tmap,
91 (char *) 0, (char *) 0);
94 tmap = amap->entries[tc].u.submap;
97 if (code) return code;
98 amap = tmap; /* continue processing this map */
101 /* here when all characters are gone */
105 /* delete a keymap and all of its recursively-included maps */
107 register struct keymap_map *amap; {
109 register struct keymap_entry *tentry;
111 for(i=0;i<KEYMAP_NENTRIES;i++) {
112 tentry = &amap->entries[i];
113 if (tentry->name) free(tentry->name);
114 if (tentry->type == KEYMAP_SUBMAP)
115 keymap_Delete(tentry->u.submap);
121 keymap_InitState(astate, amap)
122 register struct keymap_state *astate;
123 struct keymap_map *amap; {
124 bzero(astate, sizeof(*astate));
125 astate->initMap = amap;
126 astate->currentMap = amap;
130 keymap_ProcessKey(astate, akey, arock)
131 register struct keymap_state *astate;
134 register struct keymap_entry *tentry;
135 register afs_int32 code;
137 if (akey < 0 || akey >= KEYMAP_NENTRIES) return -1;
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);
159 keymap_ResetState(astate)
160 register struct keymap_state *astate; {
161 return keymap_InitState(astate, astate->initMap);