xref: /linux/drivers/accessibility/speakup/genmap.c (revision c16ce856e422e73a54c41131e0332de1afe09b8b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* genmap.c
3  * 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 
16 struct st_key_init {
17 	char *name;
18 	int value, shift;
19 };
20 
21 static unsigned char key_data[MAXKEYVAL][16], *kp;
22 
23 #include "mapdata.h"
24 
25 static const char delims[] = "\t\n ";
26 static char *cp;
27 static int map_ver = 119; /* an arbitrary number so speakup can check */
28 static int shift_table[17];
29 static int max_states = 1, flags;
30 /* flags reserved for later, maybe for individual console maps */
31 
32 static 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 
48 int
49 main(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 		value = shift_state = 0;
75 
76 		cp = strtok(buffer, delims);
77 		if (*cp == '#')
78 			continue;
79 
80 		while (cp) {
81 			if (*cp == '=')
82 				break;
83 			this = find_key(cp);
84 			if (this == NULL)
85 				oops("unknown key/modifier", cp);
86 			if (this->shift == is_shift) {
87 				if (value)
88 					oops("modifiers must come first", cp);
89 				shift_state += this->value;
90 			} else if (this->shift == is_input)
91 				value = this->value;
92 			else
93 				oops("bad modifier or key", cp);
94 			cp = strtok(0, delims);
95 		}
96 		if (!cp)
97 			oops("no = found", NULL);
98 
99 		cp = strtok(0, delims);
100 		if (!cp)
101 			oops("no speakup function after =", NULL);
102 
103 		this = find_key(cp);
104 		if (this == NULL || this->shift != is_spk)
105 			oops("invalid speakup function", cp);
106 
107 		i = get_shift_value(shift_state);
108 		if (key_data[value][i]) {
109 			while (--cp > buffer)
110 				if (!*cp)
111 					*cp = ' ';
112 			oops("two functions on same key combination", cp);
113 		}
114 		key_data[value][i] = (char)this->value;
115 		if (value > max_key_used)
116 			max_key_used = value;
117 	}
118 	fclose(infile);
119 
120 	this = find_key("spk_key");
121 	if (this)
122 		spk_val = this->value;
123 
124 	this = find_key("spk_lock");
125 	if (this)
126 		lock_val = this->value;
127 
128 	for (lc = 1; lc <= max_key_used; lc++) {
129 		kp = key_data[lc];
130 		if (!memcmp(key_data[0], kp, 16))
131 			continue;
132 		num_keys_used++;
133 		for (i = 0; i < max_states; i++) {
134 			if (kp[i] != spk_val && kp[i] != lock_val)
135 				continue;
136 			shift_state = shift_table[i];
137 			if (shift_state&16)
138 				continue;
139 			shift_state = get_shift_value(shift_state+16);
140 			kp[shift_state] = kp[i];
141 			/* fill in so we can process the key up, as spk bit will be set */
142 		}
143 	}
144 
145 	printf("\t%d, %d, %d,\n\t", map_ver, num_keys_used, max_states);
146 	for (i = 0; i < max_states; i++)
147 		printf("%d, ", shift_table[i]);
148 	printf("%d,", flags);
149 	for (lc = 1; lc <= max_key_used; lc++) {
150 		kp = key_data[lc];
151 		if (!memcmp(key_data[0], kp, 16))
152 			continue;
153 		printf("\n\t%d,", lc);
154 		for (i = 0; i < max_states; i++)
155 			printf(" %u,", (unsigned int)kp[i]);
156 	}
157 	printf("\n\t0, %d\n", map_ver);
158 
159 	exit(0);
160 }
161