1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2011,2015 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Thomas E. Dickey 1997-on *
32 ****************************************************************************/
33
34 /*
35 * This replaces an awk script which translated keys.list into keys.tries by
36 * making the output show the indices into the TERMTYPE Strings array. Doing
37 * it that way lets us cut down on the size of the init_keytry() function.
38 */
39
40 #define USE_TERMLIB 1
41 #include <build.priv.h>
42
43 MODULE_ID("$Id: make_keys.c,v 1.23 2021/08/18 20:55:25 tom Exp $")
44
45 #include <names.c>
46
47 static unsigned
unknown(void)48 unknown(void)
49 {
50 static unsigned result = 0;
51
52 if (result == 0) {
53 unsigned n;
54 for (n = 0; strnames[n] != 0; n++) {
55 ++result;
56 }
57 for (n = 0; strfnames[n] != 0; n++) {
58 ++result;
59 }
60 }
61 return result;
62 }
63
64 static unsigned
lookup(const char * name)65 lookup(const char *name)
66 {
67 unsigned n;
68 bool found = FALSE;
69 for (n = 0; strnames[n] != 0; n++) {
70 if (!strcmp(name, strnames[n])) {
71 found = TRUE;
72 break;
73 }
74 }
75 if (!found) {
76 for (n = 0; strfnames[n] != 0; n++) {
77 if (!strcmp(name, strfnames[n])) {
78 found = TRUE;
79 break;
80 }
81 }
82 }
83 return found ? n : unknown();
84 }
85
86 static void
make_keys(FILE * ifp,FILE * ofp)87 make_keys(FILE *ifp, FILE *ofp)
88 {
89 char buffer[BUFSIZ];
90 char from[256];
91 char to[256];
92 unsigned ignore = unknown();
93 unsigned maxlen = 16;
94 int scanned;
95
96 while (fgets(buffer, (int) sizeof(buffer), ifp) != 0) {
97 if (*buffer == '#')
98 continue;
99
100 to[sizeof(to) - 1] = '\0';
101 from[sizeof(from) - 1] = '\0';
102
103 scanned = sscanf(buffer, "%255s %255s", to, from);
104 if (scanned == 2) {
105 unsigned code = lookup(from);
106 if (code == ignore)
107 continue;
108 if (strlen(from) > maxlen)
109 maxlen = (unsigned) strlen(from);
110 fprintf(ofp, "\t{ %4u, %-*.*s },\t/* %s */\n",
111 code,
112 (int) maxlen, (int) maxlen,
113 to,
114 from);
115 }
116 }
117 }
118
119 static void
write_list(FILE * ofp,const char ** list)120 write_list(FILE *ofp, const char **list)
121 {
122 while (*list != 0)
123 fprintf(ofp, "%s\n", *list++);
124 }
125
126 int
main(int argc,char * argv[])127 main(int argc, char *argv[])
128 {
129 static const char *prefix[] =
130 {
131 "#ifndef _INIT_KEYTRY_H",
132 "#define _INIT_KEYTRY_H 1",
133 "",
134 "/* This file was generated by MAKE_KEYS */",
135 "",
136 "#include <tic.h>",
137 "",
138 "#if BROKEN_LINKER",
139 "static",
140 "#endif",
141 "const struct tinfo_fkeys _nc_tinfo_fkeys[] = {",
142 0
143 };
144 static const char *suffix[] =
145 {
146 "\t{ 0, 0} };",
147 "",
148 "#endif /* _INIT_KEYTRY_H */",
149 0
150 };
151
152 write_list(stdout, prefix);
153 if (argc > 1) {
154 int n;
155 for (n = 1; n < argc; n++) {
156 FILE *fp = fopen(argv[n], "r");
157 if (fp != 0) {
158 make_keys(fp, stdout);
159 fclose(fp);
160 }
161 }
162 } else {
163 make_keys(stdin, stdout);
164 }
165 write_list(stdout, suffix);
166 return EXIT_SUCCESS;
167 }
168