xref: /linux/drivers/accessibility/speakup/makemapdata.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* makemapdata.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 static char buffer[256];
17 
18 static int get_define(void)
19 {
20 	char *c;
21 
22 	while (fgets(buffer, sizeof(buffer)-1, infile)) {
23 		lc++;
24 		if (strncmp(buffer, "#define", 7))
25 			continue;
26 		c = buffer + 7;
27 		while (*c == ' ' || *c == '\t')
28 			c++;
29 		def_name = c;
30 		while (*c && *c != ' ' && *c != '\t' && *c != '\n')
31 			c++;
32 		if (!*c || *c == '\n')
33 			continue;
34 		*c++ = '\0';
35 		while (*c == ' ' || *c == '\t' || *c == '(')
36 			c++;
37 		def_val = c;
38 		while (*c && *c != '\n' && *c != ')')
39 			c++;
40 		*c++ = '\0';
41 		return 1;
42 	}
43 	fclose(infile);
44 	infile = 0;
45 	return 0;
46 }
47 
48 int
49 main(int argc, char *argv[])
50 {
51 	int value, i;
52 	struct st_key *this;
53 	const char *dir_name, *spk_dir_name;
54 	char *cp;
55 
56 	dir_name = getenv("TOPDIR");
57 	if (!dir_name)
58 		dir_name = ".";
59 	spk_dir_name = getenv("SPKDIR");
60 	if (!spk_dir_name)
61 		spk_dir_name = "drivers/accessibility/speakup";
62 	bzero(key_table, sizeof(key_table));
63 	add_key("shift",	1, is_shift);
64 	add_key("altgr",	2, is_shift);
65 	add_key("ctrl",	4, is_shift);
66 	add_key("alt",	8, is_shift);
67 	add_key("spk", 16, is_shift);
68 	add_key("double", 32, is_shift);
69 
70 	open_input(dir_name, "include/linux/input.h");
71 	while (get_define()) {
72 		if (strncmp(def_name, "KEY_", 4))
73 			continue;
74 		value = atoi(def_val);
75 		if (value > 0 && value < MAXKEYVAL)
76 			add_key(def_name, value, is_input);
77 	}
78 
79 	open_input(dir_name, "include/uapi/linux/input-event-codes.h");
80 	while (get_define()) {
81 		if (strncmp(def_name, "KEY_", 4))
82 			continue;
83 		value = atoi(def_val);
84 		if (value > 0 && value < MAXKEYVAL)
85 			add_key(def_name, value, is_input);
86 	}
87 
88 	open_input(spk_dir_name, "spk_priv_keyinfo.h");
89 	while (get_define()) {
90 		if (strlen(def_val) > 5) {
91 			//if (def_val[0] == '(')
92 			//	def_val++;
93 			cp = strchr(def_val, '+');
94 			if (!cp)
95 				continue;
96 			if (cp[-1] == ' ')
97 				cp[-1] = '\0';
98 			*cp++ = '\0';
99 			this = find_key(def_val);
100 			while (*cp == ' ')
101 				cp++;
102 			if (!this || *cp < '0' || *cp > '9')
103 				continue;
104 			value = this->value+atoi(cp);
105 		} else if (!strncmp(def_val, "0x", 2))
106 			sscanf(def_val+2, "%x", &value);
107 		else if (*def_val >= '0' && *def_val <= '9')
108 			value = atoi(def_val);
109 		else
110 			continue;
111 		add_key(def_name, value, is_spk);
112 	}
113 
114 	printf("struct st_key_init init_key_data[] = {\n");
115 	for (i = 0; i < HASHSIZE; i++) {
116 		this = &key_table[i];
117 		if (!this->name)
118 			continue;
119 		do {
120 			printf("\t{ \"%s\", %d, %d, },\n", this->name, this->value, this->shift);
121 			this = this->next;
122 		} while (this);
123 	}
124 	printf("\t{ \".\", 0, 0 }\n};\n");
125 
126 	exit(0);
127 }
128