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