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>
17 #include "gtxkeymap.h"
22 struct keymap_map
*tmap
;
24 tmap
= (struct keymap_map
*)malloc(sizeof(struct keymap_map
));
25 if (tmap
!= (struct keymap_map
*)0)
26 memset(tmap
, 0, sizeof(*tmap
));
30 /* make a copy of a string; generic utility */
32 gtx_CopyString(char *aval
)
37 return NULL
; /* propagate null strings around */
38 tp
= (char *)malloc(strlen(aval
) + 1);
45 BindIt(struct keymap_map
*amap
, int aslot
, int atype
, void *aproc
, char *aname
, void *arock
)
48 struct keymap_entry
*tentry
;
50 if (aslot
< 0 || aslot
>= KEYMAP_NENTRIES
)
52 tentry
= &amap
->entries
[aslot
];
54 if ((tp
= tentry
->name
))
56 if (atype
== KEYMAP_EMPTY
) {
57 tentry
->u
.generic
= NULL
;
60 tentry
->name
= gtx_CopyString(aname
);
61 tentry
->u
.generic
= aproc
;
68 keymap_BindToString(struct keymap_map
*amap
, char *astring
,
69 int (*aproc
)(void *, void *),
70 char *aname
, void *arock
)
75 struct keymap_map
*tmap
;
78 /* walk down string, building submaps if possible, until we get to function
80 while ((tc
= *cptr
++)) {
81 /* see if we should do submap or final function */
82 if (*cptr
== 0) { /* we're peeking: already skipped command char */
83 /* last character, do final function */
84 if (!aproc
) /* delete the entry */
85 code
= BindIt(amap
, tc
, KEYMAP_EMPTY
, NULL
, NULL
, NULL
);
88 BindIt(amap
, tc
, KEYMAP_PROC
, aproc
, aname
, arock
);
92 /* more characters after this; do submap */
93 if (amap
->entries
[tc
].type
!= KEYMAP_SUBMAP
) {
94 tmap
= keymap_Create();
96 BindIt(amap
, tc
, KEYMAP_SUBMAP
, tmap
, NULL
, NULL
);
98 tmap
= amap
->entries
[tc
].u
.submap
;
103 amap
= tmap
; /* continue processing this map */
106 /* here when all characters are gone */
110 /* delete a keymap and all of its recursively-included maps */
112 keymap_Delete(struct keymap_map
*amap
)
115 struct keymap_entry
*tentry
;
117 for (i
= 0; i
< KEYMAP_NENTRIES
; i
++) {
118 tentry
= &amap
->entries
[i
];
121 if (tentry
->type
== KEYMAP_SUBMAP
)
122 keymap_Delete(tentry
->u
.submap
);
129 keymap_InitState(struct keymap_state
*astate
, struct keymap_map
*amap
)
131 memset(astate
, 0, sizeof(*astate
));
132 astate
->initMap
= amap
;
133 astate
->currentMap
= amap
;
138 keymap_ProcessKey(struct keymap_state
*astate
, int akey
, void *arock
)
140 struct keymap_entry
*tentry
;
143 if (akey
< 0 || akey
>= KEYMAP_NENTRIES
)
145 tentry
= &astate
->currentMap
->entries
[akey
];
147 switch (tentry
->type
) {
149 keymap_ResetState(astate
);
152 /* break commented out because of return above causing compiler warnings */
155 astate
->currentMap
= tentry
->u
.submap
;
159 code
= (*tentry
->u
.proc
) (arock
, tentry
->rock
);
160 keymap_ResetState(astate
);
167 keymap_ResetState(struct keymap_state
*astate
)
169 return keymap_InitState(astate
, astate
->initMap
);