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