| 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 | #include <string.h> |
| 21 | #include <ctype.h> |
| 22 | #include "../ascebc.h" |
| 23 | #include "../ebc_disp.h" |
| 24 | #include "../kbd3270.h" |
| 25 | |
| 26 | #include "dohits.h" |
| 27 | |
| 28 | struct Hits Hits[256]; /* one for each of 0x00-0xff */ |
| 29 | |
| 30 | struct thing *table[100]; |
| 31 | |
| 32 | unsigned int |
| 33 | dohash(seed, string) |
| 34 | unsigned int seed; |
| 35 | char *string; |
| 36 | { |
| 37 | register unsigned int i = seed; |
| 38 | register unsigned char c; |
| 39 | |
| 40 | while (c = *string++) { |
| 41 | if (c >= 0x60) { |
| 42 | c - = (0x60+0x20); |
| 43 | } else { |
| 44 | c -= 0x20; |
| 45 | } |
| 46 | i = (i>>26) + (i<<6) + (c&0x3f); |
| 47 | } |
| 48 | return i; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | add(first, second, value) |
| 53 | { |
| 54 | struct thing **item, *this; |
| 55 | |
| 56 | item = &firstentry(second); |
| 57 | this = (struct thing *) malloc(sizeof *this); |
| 58 | this->next = *item; |
| 59 | *item = this; |
| 60 | this->value = value; |
| 61 | strcpy(this->name, first); |
| 62 | strcpy(this->name+strlen(this->name), second); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | scan(file, prefix) |
| 67 | char *file, /* Name of file to scan */ |
| 68 | *prefix; /* prefix of what should be picked up */ |
| 69 | { |
| 70 | FILE *ourfile; |
| 71 | char compare[100]; |
| 72 | char what[100], value[100]; |
| 73 | char line[200]; |
| 74 | int whatitis; |
| 75 | |
| 76 | sprintf(compare, "#define %s%%s %%s", prefix); |
| 77 | if ((ourfile = fopen(file, "r")) == NULL) { |
| 78 | perror("fopen"); |
| 79 | exit(1); |
| 80 | } |
| 81 | while (!feof(ourfile)) { |
| 82 | if (fscanf(ourfile, compare, what, value) == 2) { |
| 83 | if (value[0] == '0') { |
| 84 | if ((value[1] == 'x') || (value[1] == 'X')) { |
| 85 | sscanf(value, "0x%x", &whatitis); |
| 86 | } else { |
| 87 | sscanf(value, "0%o", &whatitis); |
| 88 | } |
| 89 | } else { |
| 90 | sscanf(value, "%d", &whatitis); |
| 91 | } |
| 92 | add(prefix, what, whatitis); |
| 93 | } |
| 94 | do { |
| 95 | if (fgets(line, sizeof line, ourfile) == NULL) { |
| 96 | if (!feof(ourfile)) { |
| 97 | perror("fgets"); |
| 98 | } |
| 99 | break; |
| 100 | } |
| 101 | } while (line[strlen(line)-1] != '\n'); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | char * |
| 107 | doit(hit, type, hits) |
| 108 | struct hit *hit; |
| 109 | unsigned char *type; |
| 110 | struct Hits *hits; |
| 111 | { |
| 112 | struct thing *this; |
| 113 | |
| 114 | hit->type = illegal; |
| 115 | if (type[0] == 0) { |
| 116 | return 0; |
| 117 | } |
| 118 | if (type[1] == 0) { /* character */ |
| 119 | hit->type = character; |
| 120 | hit->code = ebc_disp[ascebc[AE_IN][type[0]]]; |
| 121 | } else { |
| 122 | for (this = firstentry(type); this; this = this->next) { |
| 123 | if ((type[0] == this->name[4]) |
| 124 | && (strcmp(type, this->name+4) == 0)) { |
| 125 | this->hits = hits; |
| 126 | if (this->name[0] == 'F') { |
| 127 | hit->type = function; |
| 128 | } else { |
| 129 | hit->type = aid; |
| 130 | } |
| 131 | return this->name; |
| 132 | } |
| 133 | } |
| 134 | printf("Error: Unknown type %s.\n", type); |
| 135 | return 0; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void |
| 141 | dohits() |
| 142 | { |
| 143 | unsigned char plain[100], shifted[100], alted[100], shiftalted[100]; |
| 144 | unsigned char line[200]; |
| 145 | int keynumber, scancode; |
| 146 | int empty; |
| 147 | int i; |
| 148 | struct hit *hit; |
| 149 | struct hits *ph; |
| 150 | struct Hits *Ph; |
| 151 | |
| 152 | memset((char *)Hits, 0, sizeof Hits); |
| 153 | |
| 154 | /* |
| 155 | * First, we read "host3270.h" to find the names/values of |
| 156 | * various AID; then we read kbd3270.h to find the names/values |
| 157 | * of various FCNs. |
| 158 | */ |
| 159 | |
| 160 | scan("host3270.h", "AID_"); |
| 161 | scan("kbd3270.h", "FCN_"); |
| 162 | |
| 163 | while (gets(line) != NULL) { |
| 164 | if (!isdigit(line[0])) { |
| 165 | continue; |
| 166 | } |
| 167 | plain[0] = shifted[0] = alted[0] = shiftalted[0] = 0; |
| 168 | keynumber = -1; |
| 169 | scancode = -1; |
| 170 | (void) sscanf(line, "%d %x %s %s %s %s", &keynumber, |
| 171 | &scancode, plain, shifted, alted, shiftalted); |
| 172 | if ((keynumber == -1) || (scancode == -1) |
| 173 | || ((plain[0] == 0) |
| 174 | && (shifted[0] == 0) |
| 175 | && (alted[0] == 0) |
| 176 | && (shiftalted[0] == 0))) { |
| 177 | continue; |
| 178 | } |
| 179 | if (scancode >= 256) { |
| 180 | printf("Error: scancode 0x%02x for keynumber %d\n", scancode, |
| 181 | keynumber); |
| 182 | break; |
| 183 | } |
| 184 | if (Hits[scancode].hits.hit[0].type != undefined) { |
| 185 | printf("Error: duplicate scancode 0x%02x for keynumber %d\n", |
| 186 | scancode, keynumber); |
| 187 | break; |
| 188 | } |
| 189 | hit = Hits[scancode].hits.hit; |
| 190 | Hits[scancode].hits.keynumber = keynumber; |
| 191 | Hits[scancode].name[0] = doit(hit, plain, &Hits[scancode]); |
| 192 | Hits[scancode].name[1] = doit(hit+1, shifted, &Hits[scancode]); |
| 193 | Hits[scancode].name[2] = doit(hit+2, alted, &Hits[scancode]); |
| 194 | Hits[scancode].name[3] = doit(hit+3, shiftalted, &Hits[scancode]); |
| 195 | } |
| 196 | } |