162 lines · c
1// SPDX-License-Identifier: GPL-2.0+2/* genmap.c3 * originally written by: Kirk Reiser.4 *5 ** Copyright (C) 2002 Kirk Reiser.6 * Copyright (C) 2003 David Borowski.7 */8 9#include <stdlib.h>10#include <stdio.h>11#include <libgen.h>12#include <string.h>13#include <ctype.h>14#include "utils.h"15 16struct st_key_init {17 char *name;18 int value, shift;19};20 21static unsigned char key_data[MAXKEYVAL][16], *kp;22 23#include "mapdata.h"24 25static const char delims[] = "\t\n ";26static char *cp;27static int map_ver = 119; /* an arbitrary number so speakup can check */28static int shift_table[17];29static int max_states = 1, flags;30/* flags reserved for later, maybe for individual console maps */31 32static int get_shift_value(int state)33{34 int i;35 36 for (i = 0; shift_table[i] != state; i++) {37 if (shift_table[i] == -1) {38 if (i >= 16)39 oops("too many shift states", NULL);40 shift_table[i] = state;41 max_states = i+1;42 break;43 }44 }45 return i;46}47 48int49main(int argc, char *argv[])50{51 int value, shift_state, i, spk_val = 0, lock_val = 0;52 int max_key_used = 0, num_keys_used = 0;53 struct st_key *this;54 struct st_key_init *p_init;55 char buffer[256];56 57 bzero(key_table, sizeof(key_table));58 bzero(key_data, sizeof(key_data));59 60 shift_table[0] = 0;61 for (i = 1; i <= 16; i++)62 shift_table[i] = -1;63 64 if (argc < 2) {65 fputs("usage: genmap filename\n", stderr);66 exit(1);67 }68 69 for (p_init = init_key_data; p_init->name[0] != '.'; p_init++)70 add_key(p_init->name, p_init->value, p_init->shift);71 72 open_input(NULL, argv[1]);73 while (fgets(buffer, sizeof(buffer), infile)) {74 lc++;75 value = shift_state = 0;76 77 cp = strtok(buffer, delims);78 if (*cp == '#')79 continue;80 81 while (cp) {82 if (*cp == '=')83 break;84 this = find_key(cp);85 if (this == NULL)86 oops("unknown key/modifier", cp);87 if (this->shift == is_shift) {88 if (value)89 oops("modifiers must come first", cp);90 shift_state += this->value;91 } else if (this->shift == is_input)92 value = this->value;93 else94 oops("bad modifier or key", cp);95 cp = strtok(0, delims);96 }97 if (!cp)98 oops("no = found", NULL);99 100 cp = strtok(0, delims);101 if (!cp)102 oops("no speakup function after =", NULL);103 104 this = find_key(cp);105 if (this == NULL || this->shift != is_spk)106 oops("invalid speakup function", cp);107 108 i = get_shift_value(shift_state);109 if (key_data[value][i]) {110 while (--cp > buffer)111 if (!*cp)112 *cp = ' ';113 oops("two functions on same key combination", cp);114 }115 key_data[value][i] = (char)this->value;116 if (value > max_key_used)117 max_key_used = value;118 }119 fclose(infile);120 121 this = find_key("spk_key");122 if (this)123 spk_val = this->value;124 125 this = find_key("spk_lock");126 if (this)127 lock_val = this->value;128 129 for (lc = 1; lc <= max_key_used; lc++) {130 kp = key_data[lc];131 if (!memcmp(key_data[0], kp, 16))132 continue;133 num_keys_used++;134 for (i = 0; i < max_states; i++) {135 if (kp[i] != spk_val && kp[i] != lock_val)136 continue;137 shift_state = shift_table[i];138 if (shift_state&16)139 continue;140 shift_state = get_shift_value(shift_state+16);141 kp[shift_state] = kp[i];142 /* fill in so we can process the key up, as spk bit will be set */143 }144 }145 146 printf("\t%d, %d, %d,\n\t", map_ver, num_keys_used, max_states);147 for (i = 0; i < max_states; i++)148 printf("%d, ", shift_table[i]);149 printf("%d,", flags);150 for (lc = 1; lc <= max_key_used; lc++) {151 kp = key_data[lc];152 if (!memcmp(key_data[0], kp, 16))153 continue;154 printf("\n\t%d,", lc);155 for (i = 0; i < max_states; i++)156 printf(" %d,", (unsigned int)kp[i]);157 }158 printf("\n\t0, %d\n", map_ver);159 160 exit(0);161}162