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