~ubuntu-branches/ubuntu/warty/openafs/warty

« back to all changes in this revision

Viewing changes to src/gtx/keymap.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2004-01-10 16:37:33 UTC
  • Revision ID: james.westby@ubuntu.com-20040110163733-jvr0n1uahshlb1uu
Tags: upstream-1.2.11
ImportĀ upstreamĀ versionĀ 1.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2000, International Business Machines Corporation and others.
 
3
 * All Rights Reserved.
 
4
 * 
 
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
 
8
 */
 
9
 
 
10
#include <afsconfig.h>
 
11
#include <afs/param.h>
 
12
 
 
13
RCSID("$Header: /afs/sipb.mit.edu/project/openafs/debian/cvs/openafs/src/gtx/keymap.c,v 1.1.1.5 2001/09/11 14:32:46 hartmans Exp $");
 
14
 
 
15
#include "gtxkeymap.h"
 
16
 
 
17
struct keymap_map *keymap_Create() {
 
18
    register struct keymap_map *tmap;
 
19
 
 
20
    tmap = (struct keymap_map *) malloc(sizeof(struct keymap_map));
 
21
    if (tmap != (struct keymap_map *)0)
 
22
      memset(tmap, 0, sizeof(*tmap));
 
23
    return(tmap);
 
24
}
 
25
 
 
26
/* make a copy of a string; generic utility */
 
27
char *gtx_CopyString(aval)
 
28
register char *aval; {
 
29
    register char *tp;
 
30
 
 
31
    if (!aval) return (char *) 0;       /* propagate null strings around */
 
32
    tp = (char *) malloc(strlen(aval)+1);
 
33
    if (tp != (char *)0)
 
34
      strcpy(tp, aval);
 
35
    return(tp);
 
36
}
 
37
 
 
38
static int BindIt(amap, aslot, atype, aproc, aname, arock)
 
39
struct keymap_map *amap;
 
40
char *arock;
 
41
int aslot;
 
42
int atype;
 
43
char *aproc;
 
44
char *aname; {
 
45
    register char *tp;
 
46
    register struct keymap_entry *tentry;
 
47
 
 
48
    if (aslot < 0 || aslot >= KEYMAP_NENTRIES) return -1;
 
49
    tentry = &amap->entries[aslot];
 
50
    tentry->type = atype;
 
51
    if (tp=tentry->name)
 
52
        free(tp);
 
53
    if (atype == KEYMAP_EMPTY) {
 
54
        tentry->u.generic = (char *) 0;
 
55
        tentry->name = (char *) 0;
 
56
    }
 
57
    else {
 
58
        tentry->name = gtx_CopyString(aname);
 
59
        tentry->u.generic = aproc;
 
60
    }
 
61
    tentry->rock = arock;
 
62
    return 0;
 
63
}
 
64
 
 
65
keymap_BindToString(amap, astring, aproc, aname, arock)
 
66
register struct keymap_map *amap;
 
67
char *astring;
 
68
char *arock;
 
69
int (*aproc)();
 
70
char *aname; {
 
71
    register char *cptr;
 
72
    register int tc;
 
73
    register afs_int32 code;
 
74
    struct keymap_map *tmap;
 
75
 
 
76
    cptr = astring;
 
77
    /* walk down string, building submaps if possible, until we get to function
 
78
       at the end */
 
79
    while (tc = *cptr++) {
 
80
        /* see if we should do submap or final function */
 
81
        if (*cptr == 0) {       /* we're peeking: already skipped command char */
 
82
            /* last character, do final function */
 
83
            if (!aproc) /* delete the entry */
 
84
                code = BindIt(amap, tc, KEYMAP_EMPTY, (char *) 0,
 
85
                              (char *) 0, (char *) 0);
 
86
            else
 
87
                code = BindIt(amap, tc, KEYMAP_PROC, (char *) aproc, aname, arock);
 
88
            if (code) return code;
 
89
        }
 
90
        else {
 
91
            /* more characters after this; do submap */
 
92
            if (amap->entries[tc].type != KEYMAP_SUBMAP) {
 
93
                tmap = keymap_Create();
 
94
                code = BindIt(amap, tc, KEYMAP_SUBMAP, (char *) tmap,
 
95
                              (char *) 0, (char *) 0);
 
96
            }
 
97
            else {
 
98
                tmap = amap->entries[tc].u.submap;
 
99
                code = 0;
 
100
            }
 
101
            if (code) return code;
 
102
            amap = tmap;        /* continue processing this map */
 
103
        }
 
104
    }   /* while loop */
 
105
    /* here when all characters are gone */
 
106
    return 0;
 
107
}
 
108
 
 
109
/* delete a keymap and all of its recursively-included maps */
 
110
keymap_Delete(amap)
 
111
register struct keymap_map *amap; {
 
112
    register int i;
 
113
    register struct keymap_entry *tentry;
 
114
 
 
115
    for(i=0;i<KEYMAP_NENTRIES;i++) {
 
116
        tentry = &amap->entries[i];
 
117
        if (tentry->name) free(tentry->name);
 
118
        if (tentry->type == KEYMAP_SUBMAP)
 
119
            keymap_Delete(tentry->u.submap);
 
120
    }
 
121
    free(amap);
 
122
    return 0;
 
123
}
 
124
 
 
125
keymap_InitState(astate, amap)
 
126
register struct keymap_state *astate;
 
127
struct keymap_map *amap; {
 
128
    memset(astate, 0, sizeof(*astate));
 
129
    astate->initMap = amap;
 
130
    astate->currentMap = amap;
 
131
    return 0;
 
132
}
 
133
 
 
134
keymap_ProcessKey(astate, akey, arock)
 
135
register struct keymap_state *astate;
 
136
char *arock;
 
137
register int akey; {
 
138
    register struct keymap_entry *tentry;
 
139
    register afs_int32 code;
 
140
 
 
141
    if (akey < 0 || akey >= KEYMAP_NENTRIES) return -1;
 
142
    tentry = &astate->currentMap->entries[akey];
 
143
    code = 0;
 
144
    switch(tentry->type) {
 
145
      case KEYMAP_EMPTY:
 
146
        keymap_ResetState(astate);
 
147
        return -1;
 
148
        /* break; */
 
149
        /* break commented out because of return above causing compiler warnings */
 
150
 
 
151
      case KEYMAP_SUBMAP:
 
152
        astate->currentMap = tentry->u.submap;
 
153
        break;
 
154
 
 
155
      case KEYMAP_PROC:
 
156
        code = (*tentry->u.proc)(arock, tentry->rock);
 
157
        keymap_ResetState(astate);
 
158
        break;
 
159
    }
 
160
    return code;
 
161
}
 
162
 
 
163
keymap_ResetState(astate)
 
164
register struct keymap_state *astate; {
 
165
    return keymap_InitState(astate, astate->initMap);
 
166
}