Commit | Line | Data |
---|---|---|
992de934 GM |
1 | /* |
2 | * Copyright (c) 1984-1987 by the Regents of the | |
3 | * University of California and by Gregory Glenn Minshall. | |
4 | * | |
5 | * Permission to use, copy, modify, and distribute these | |
6 | * programs and their documentation for any purpose and | |
7 | * without fee is hereby granted, provided that this | |
8 | * copyright and permission appear on all copies and | |
9 | * supporting documentation, the name of the Regents of | |
10 | * the University of California not be used in advertising | |
11 | * or publicity pertaining to distribution of the programs | |
12 | * without specific prior permission, and notice be given in | |
13 | * supporting documentation that copying and distribution is | |
14 | * by permission of the Regents of the University of California | |
15 | * and by Gregory Glenn Minshall. Neither the Regents of the | |
16 | * University of California nor Gregory Glenn Minshall make | |
17 | * representations about the suitability of this software | |
18 | * for any purpose. It is provided "as is" without | |
19 | * express or implied warranty. | |
20 | */ | |
21 | ||
22 | #ifndef lint | |
c578271f | 23 | static char sccsid[] = "@(#)dohits.c 3.1 (Berkeley) %G%"; |
992de934 GM |
24 | #endif /* not lint */ |
25 | ||
95a11955 GM |
26 | /* |
27 | * This program scans a file which describes a keyboard. The output | |
28 | * of the program is a series of 'C' declarations which describe a | |
29 | * mapping between (scancode, shiftstate, altstate) and 3270 functions, | |
30 | * characters, and AIDs. | |
31 | * | |
32 | * The format of the input file is as follows: | |
33 | * | |
34 | * keynumber [ scancode [ unshifted [ shifted [ alted [ shiftalted ] ] ] ] ] | |
35 | * | |
36 | * keynumber is in decimal, and starts in column 1. | |
37 | * scancode is hexadecimal. | |
38 | * unshifted, etc. - these are either a single ascii character, | |
39 | * or the name of a function or an AID-generating key. | |
40 | * | |
41 | * all fields are separated by a single space. | |
42 | */ | |
43 | ||
44 | #include <stdio.h> | |
e56fc5cb | 45 | #if defined(unix) |
577b1ba9 | 46 | #include <strings.h> |
e56fc5cb GM |
47 | #else /* defined(unix) */ |
48 | #include <string.h> | |
49 | #endif /* defined(unix) */ | |
95a11955 | 50 | #include <ctype.h> |
03bae598 | 51 | #include "../general/general.h" |
4718d085 GM |
52 | #include "../api/asc_ebc.h" |
53 | #include "../api/ebc_disp.h" | |
bfbc528f | 54 | #include "../ctlr/function.h" |
95a11955 GM |
55 | |
56 | #include "dohits.h" | |
57 | ||
58 | struct Hits Hits[256]; /* one for each of 0x00-0xff */ | |
59 | ||
60 | struct thing *table[100]; | |
61 | ||
bfbc528f GM |
62 | extern char *malloc(); |
63 | ||
95a11955 GM |
64 | unsigned int |
65 | dohash(seed, string) | |
66 | unsigned int seed; | |
67 | char *string; | |
68 | { | |
69 | register unsigned int i = seed; | |
70 | register unsigned char c; | |
71 | ||
72 | while (c = *string++) { | |
73 | if (c >= 0x60) { | |
817f9fdf | 74 | c -= (0x60+0x20); |
95a11955 GM |
75 | } else { |
76 | c -= 0x20; | |
77 | } | |
78 | i = (i>>26) + (i<<6) + (c&0x3f); | |
79 | } | |
80 | return i; | |
81 | } | |
82 | ||
83 | void | |
84 | add(first, second, value) | |
d5c90a59 GM |
85 | char *first, *second; |
86 | int value; | |
95a11955 GM |
87 | { |
88 | struct thing **item, *this; | |
89 | ||
90 | item = &firstentry(second); | |
91 | this = (struct thing *) malloc(sizeof *this); | |
92 | this->next = *item; | |
93 | *item = this; | |
94 | this->value = value; | |
95 | strcpy(this->name, first); | |
96 | strcpy(this->name+strlen(this->name), second); | |
97 | } | |
98 | ||
99 | void | |
d5c90a59 GM |
100 | scanwhite(file, prefix) |
101 | char *file, /* Name of file to scan for whitespace prefix */ | |
102 | *prefix; /* prefix of what should be picked up */ | |
103 | { | |
104 | FILE *ourfile; | |
105 | char compare[100]; | |
106 | char what[100], value[100]; | |
107 | char line[200]; | |
108 | ||
109 | sprintf(compare, " %s%%[^,\t \n]", prefix); | |
110 | if ((ourfile = fopen(file, "r")) == NULL) { | |
111 | perror("fopen"); | |
112 | exit(1); | |
113 | } | |
114 | while (!feof(ourfile)) { | |
115 | if (fscanf(ourfile, compare, what) == 1) { | |
116 | add(prefix, what, 0); | |
117 | } | |
118 | do { | |
119 | if (fgets(line, sizeof line, ourfile) == NULL) { | |
120 | if (!feof(ourfile)) { | |
121 | perror("fgets"); | |
122 | } | |
123 | break; | |
124 | } | |
125 | } while (line[strlen(line)-1] != '\n'); | |
126 | } | |
127 | } | |
128 | ||
129 | void | |
130 | scandefine(file, prefix) | |
131 | char *file, /* Name of file to scan for #define prefix */ | |
95a11955 GM |
132 | *prefix; /* prefix of what should be picked up */ |
133 | { | |
134 | FILE *ourfile; | |
135 | char compare[100]; | |
136 | char what[100], value[100]; | |
137 | char line[200]; | |
138 | int whatitis; | |
139 | ||
140 | sprintf(compare, "#define %s%%s %%s", prefix); | |
141 | if ((ourfile = fopen(file, "r")) == NULL) { | |
142 | perror("fopen"); | |
143 | exit(1); | |
144 | } | |
145 | while (!feof(ourfile)) { | |
146 | if (fscanf(ourfile, compare, what, value) == 2) { | |
147 | if (value[0] == '0') { | |
148 | if ((value[1] == 'x') || (value[1] == 'X')) { | |
149 | sscanf(value, "0x%x", &whatitis); | |
150 | } else { | |
151 | sscanf(value, "0%o", &whatitis); | |
152 | } | |
153 | } else { | |
154 | sscanf(value, "%d", &whatitis); | |
155 | } | |
156 | add(prefix, what, whatitis); | |
157 | } | |
158 | do { | |
159 | if (fgets(line, sizeof line, ourfile) == NULL) { | |
160 | if (!feof(ourfile)) { | |
161 | perror("fgets"); | |
162 | } | |
163 | break; | |
164 | } | |
165 | } while (line[strlen(line)-1] != '\n'); | |
166 | } | |
167 | } | |
168 | ||
bfbc528f GM |
169 | char *savechr(c) |
170 | unsigned char c; | |
171 | { | |
172 | char *foo; | |
173 | ||
174 | foo = malloc(sizeof c); | |
175 | if (foo == 0) { | |
176 | fprintf(stderr, "No room for ascii characters!\n"); | |
177 | exit(1); | |
178 | } | |
179 | *foo = c; | |
180 | return foo; | |
181 | } | |
95a11955 GM |
182 | |
183 | char * | |
184 | doit(hit, type, hits) | |
185 | struct hit *hit; | |
186 | unsigned char *type; | |
187 | struct Hits *hits; | |
188 | { | |
189 | struct thing *this; | |
190 | ||
d5c90a59 | 191 | hit->ctlrfcn = FCN_NULL; |
95a11955 GM |
192 | if (type[0] == 0) { |
193 | return 0; | |
194 | } | |
195 | if (type[1] == 0) { /* character */ | |
d5c90a59 | 196 | hit->ctlrfcn = FCN_CHARACTER; |
138011c0 | 197 | hit->code = ebc_disp[asc_ebc[type[0]]]; |
bfbc528f | 198 | return savechr(*type); /* The character is the name */ |
95a11955 GM |
199 | } else { |
200 | for (this = firstentry(type); this; this = this->next) { | |
201 | if ((type[0] == this->name[4]) | |
202 | && (strcmp(type, this->name+4) == 0)) { | |
203 | this->hits = hits; | |
204 | if (this->name[0] == 'F') { | |
d5c90a59 | 205 | hit->ctlrfcn = FCN_NULL; /* XXX */ |
95a11955 | 206 | } else { |
d5c90a59 | 207 | hit->ctlrfcn = FCN_AID; |
95a11955 GM |
208 | } |
209 | return this->name; | |
210 | } | |
211 | } | |
bfbc528f | 212 | fprintf(stderr, "Error: Unknown type %s.\n", type); |
95a11955 GM |
213 | return 0; |
214 | } | |
215 | } | |
216 | ||
217 | ||
218 | void | |
d5c90a59 GM |
219 | dohits(aidfile, fcnfile) |
220 | char *aidfile, *fcnfile; | |
95a11955 GM |
221 | { |
222 | unsigned char plain[100], shifted[100], alted[100], shiftalted[100]; | |
223 | unsigned char line[200]; | |
224 | int keynumber, scancode; | |
225 | int empty; | |
226 | int i; | |
227 | struct hit *hit; | |
228 | struct hits *ph; | |
229 | struct Hits *Ph; | |
230 | ||
231 | memset((char *)Hits, 0, sizeof Hits); | |
232 | ||
233 | /* | |
234 | * First, we read "host3270.h" to find the names/values of | |
235 | * various AID; then we read kbd3270.h to find the names/values | |
236 | * of various FCNs. | |
237 | */ | |
238 | ||
d5c90a59 GM |
239 | if (aidfile == 0) { |
240 | aidfile = "../ctlr/hostctlr.h"; | |
241 | } | |
242 | scandefine(aidfile, "AID_"); | |
243 | if (fcnfile == 0) { | |
244 | fcnfile = "../ctlr/function.h"; | |
245 | } | |
246 | scanwhite(fcnfile, "FCN_"); | |
95a11955 GM |
247 | |
248 | while (gets(line) != NULL) { | |
249 | if (!isdigit(line[0])) { | |
250 | continue; | |
251 | } | |
252 | plain[0] = shifted[0] = alted[0] = shiftalted[0] = 0; | |
253 | keynumber = -1; | |
254 | scancode = -1; | |
255 | (void) sscanf(line, "%d %x %s %s %s %s", &keynumber, | |
256 | &scancode, plain, shifted, alted, shiftalted); | |
257 | if ((keynumber == -1) || (scancode == -1) | |
258 | || ((plain[0] == 0) | |
259 | && (shifted[0] == 0) | |
260 | && (alted[0] == 0) | |
261 | && (shiftalted[0] == 0))) { | |
262 | continue; | |
263 | } | |
264 | if (scancode >= 256) { | |
bfbc528f GM |
265 | fprintf(stderr, |
266 | "Error: scancode 0x%02x for keynumber %d\n", scancode, | |
95a11955 GM |
267 | keynumber); |
268 | break; | |
269 | } | |
d5c90a59 | 270 | if (Hits[scancode].hits.hit[0].ctlrfcn != undefined) { |
bfbc528f GM |
271 | fprintf(stderr, |
272 | "Error: duplicate scancode 0x%02x for keynumber %d\n", | |
95a11955 GM |
273 | scancode, keynumber); |
274 | break; | |
275 | } | |
276 | hit = Hits[scancode].hits.hit; | |
277 | Hits[scancode].hits.keynumber = keynumber; | |
278 | Hits[scancode].name[0] = doit(hit, plain, &Hits[scancode]); | |
279 | Hits[scancode].name[1] = doit(hit+1, shifted, &Hits[scancode]); | |
280 | Hits[scancode].name[2] = doit(hit+2, alted, &Hits[scancode]); | |
281 | Hits[scancode].name[3] = doit(hit+3, shiftalted, &Hits[scancode]); | |
282 | } | |
283 | } |