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 return calloc(1, sizeof(struct keymap_map
));
23 /* make a copy of a string; generic utility */
25 gtx_CopyString(char *aval
)
28 return NULL
; /* propagate null strings around */
33 BindIt(struct keymap_map
*amap
, int aslot
, int atype
, void *aproc
, char *aname
, void *arock
)
36 struct keymap_entry
*tentry
;
38 if (aslot
< 0 || aslot
>= KEYMAP_NENTRIES
)
40 tentry
= &amap
->entries
[aslot
];
42 if ((tp
= tentry
->name
))
44 if (atype
== KEYMAP_EMPTY
) {
45 tentry
->u
.generic
= NULL
;
48 tentry
->name
= gtx_CopyString(aname
);
49 tentry
->u
.generic
= aproc
;
56 keymap_BindToString(struct keymap_map
*amap
, char *astring
,
57 int (*aproc
)(void *, void *),
58 char *aname
, void *arock
)
63 struct keymap_map
*tmap
;
66 /* walk down string, building submaps if possible, until we get to function
68 while ((tc
= *cptr
++)) {
69 /* see if we should do submap or final function */
70 if (*cptr
== 0) { /* we're peeking: already skipped command char */
71 /* last character, do final function */
72 if (!aproc
) /* delete the entry */
73 code
= BindIt(amap
, tc
, KEYMAP_EMPTY
, NULL
, NULL
, NULL
);
76 BindIt(amap
, tc
, KEYMAP_PROC
, aproc
, aname
, arock
);
80 /* more characters after this; do submap */
81 if (amap
->entries
[tc
].type
!= KEYMAP_SUBMAP
) {
82 tmap
= keymap_Create();
84 BindIt(amap
, tc
, KEYMAP_SUBMAP
, tmap
, NULL
, NULL
);
86 tmap
= amap
->entries
[tc
].u
.submap
;
91 amap
= tmap
; /* continue processing this map */
94 /* here when all characters are gone */
98 /* delete a keymap and all of its recursively-included maps */
100 keymap_Delete(struct keymap_map
*amap
)
103 struct keymap_entry
*tentry
;
105 for (i
= 0; i
< KEYMAP_NENTRIES
; i
++) {
106 tentry
= &amap
->entries
[i
];
109 if (tentry
->type
== KEYMAP_SUBMAP
)
110 keymap_Delete(tentry
->u
.submap
);
117 keymap_InitState(struct keymap_state
*astate
, struct keymap_map
*amap
)
119 memset(astate
, 0, sizeof(*astate
));
120 astate
->initMap
= amap
;
121 astate
->currentMap
= amap
;
126 keymap_ProcessKey(struct keymap_state
*astate
, int akey
, void *arock
)
128 struct keymap_entry
*tentry
;
131 if (akey
< 0 || akey
>= KEYMAP_NENTRIES
)
133 tentry
= &astate
->currentMap
->entries
[akey
];
135 switch (tentry
->type
) {
137 keymap_ResetState(astate
);
140 /* break commented out because of return above causing compiler warnings */
143 astate
->currentMap
= tentry
->u
.submap
;
147 code
= (*tentry
->u
.proc
) (arock
, tentry
->rock
);
148 keymap_ResetState(astate
);
155 keymap_ResetState(struct keymap_state
*astate
)
157 return keymap_InitState(astate
, astate
->initMap
);