| 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> |
| 20 | #if defined(unix) |
| 21 | #include <strings.h> |
| 22 | #else /* defined(unix) */ |
| 23 | #include <string.h> |
| 24 | #endif /* defined(unix) */ |
| 25 | #include <ctype.h> |
| 26 | #include "../general/general.h" |
| 27 | #include "../apilib/asc_ebc.h" |
| 28 | #include "../apilib/ebc_disp.h" |
| 29 | #include "../ctlr/function.h" |
| 30 | |
| 31 | #include "dohits.h" |
| 32 | |
| 33 | struct Hits Hits[256]; /* one for each of 0x00-0xff */ |
| 34 | |
| 35 | struct thing *table[100]; |
| 36 | |
| 37 | extern char *malloc(); |
| 38 | |
| 39 | unsigned int |
| 40 | dohash(seed, string) |
| 41 | unsigned int seed; |
| 42 | char *string; |
| 43 | { |
| 44 | register unsigned int i = seed; |
| 45 | register unsigned char c; |
| 46 | |
| 47 | while (c = *string++) { |
| 48 | if (c >= 0x60) { |
| 49 | c -= (0x60+0x20); |
| 50 | } else { |
| 51 | c -= 0x20; |
| 52 | } |
| 53 | i = (i>>26) + (i<<6) + (c&0x3f); |
| 54 | } |
| 55 | return i; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | add(first, second, value) |
| 60 | char *first, *second; |
| 61 | int value; |
| 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 | |
| 74 | void |
| 75 | scanwhite(file, prefix) |
| 76 | char *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 | |
| 104 | void |
| 105 | scandefine(file, prefix) |
| 106 | char *file, /* Name of file to scan for #define prefix */ |
| 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 | |
| 144 | char *savechr(c) |
| 145 | unsigned 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 | } |
| 157 | |
| 158 | char * |
| 159 | doit(hit, type, hits) |
| 160 | struct hit *hit; |
| 161 | unsigned char *type; |
| 162 | struct Hits *hits; |
| 163 | { |
| 164 | struct thing *this; |
| 165 | |
| 166 | hit->ctlrfcn = FCN_NULL; |
| 167 | if (type[0] == 0) { |
| 168 | return 0; |
| 169 | } |
| 170 | if (type[1] == 0) { /* character */ |
| 171 | hit->ctlrfcn = FCN_CHARACTER; |
| 172 | hit->code = ebc_disp[asc_ebc[type[0]]]; |
| 173 | return savechr(*type); /* The character is the name */ |
| 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') { |
| 180 | hit->ctlrfcn = FCN_NULL; /* XXX */ |
| 181 | } else { |
| 182 | hit->ctlrfcn = FCN_AID; |
| 183 | } |
| 184 | return this->name; |
| 185 | } |
| 186 | } |
| 187 | fprintf(stderr, "Error: Unknown type %s.\n", type); |
| 188 | return 0; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void |
| 194 | dohits(aidfile, fcnfile) |
| 195 | char *aidfile, *fcnfile; |
| 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 | |
| 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_"); |
| 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) { |
| 240 | fprintf(stderr, |
| 241 | "Error: scancode 0x%02x for keynumber %d\n", scancode, |
| 242 | keynumber); |
| 243 | break; |
| 244 | } |
| 245 | if (Hits[scancode].hits.hit[0].ctlrfcn != undefined) { |
| 246 | fprintf(stderr, |
| 247 | "Error: duplicate scancode 0x%02x for keynumber %d\n", |
| 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 | } |