1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
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 <dickey@clark.net> 1997                        *
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 #include <curses.priv.h>
39 
40 MODULE_ID("$Id: make_keys.c,v 1.8 2000/03/12 02:55:50 Todd.C.Miller Exp $")
41 
42 #include <names.c>
43 
44 #define UNKNOWN (SIZEOF(strnames) + SIZEOF(strfnames))
45 
46 static size_t lookup(const char *name)
47 {
48 	size_t n;
49 	bool found = FALSE;
50 	for (n = 0; strnames[n] != 0; n++) {
51 		if (!strcmp(name, strnames[n])) {
52 			found = TRUE;
53 			break;
54 		}
55 	}
56 	if (!found) {
57 		for (n = 0; strfnames[n] != 0; n++) {
58 			if (!strcmp(name, strfnames[n])) {
59 				found = TRUE;
60 				break;
61 			}
62 		}
63 	}
64 	return found ? n : UNKNOWN;
65 }
66 
67 static void make_keys(FILE *ifp, FILE *ofp)
68 {
69 	char buffer[BUFSIZ];
70 	char from[BUFSIZ];
71 	char to[BUFSIZ];
72 	int maxlen = 16;
73 
74 	while (fgets(buffer, sizeof(buffer), ifp) != 0) {
75 		if (*buffer == '#')
76 			continue;
77 		if (sscanf(buffer, "%s %s", to, from) == 2) {
78 			int code = lookup(from);
79 			if (code == UNKNOWN)
80 				continue;
81 			if ((int)strlen(from) > maxlen)
82 				maxlen = strlen(from);
83 			fprintf(ofp, "\t{ %4d, %-*.*s },\t/* %s */\n",
84 				code,
85 				maxlen, maxlen,
86 				to,
87 				from);
88 		}
89 	}
90 }
91 
92 static void write_list(FILE *ofp, const char **list)
93 {
94 	while (*list != 0)
95 		fprintf(ofp, "%s\n", *list++);
96 }
97 
98 int main(int argc, char *argv[])
99 {
100 	static const char *prefix[] = {
101 		"#ifndef NCU_KEYS_H",
102 		"#define NCU_KEYS_H 1",
103 		"",
104 		"/* This file was generated by MAKE_KEYS */",
105 		"",
106 		"#if BROKEN_LINKER",
107 		"static",
108 		"#endif",
109 		"struct tinfo_fkeys _nc_tinfo_fkeys[] = {",
110 		0
111 	};
112 	static const char *suffix[] = {
113 		"\t{ 0, 0} };",
114 		"",
115 		"#endif /* NCU_KEYS_H */",
116 		0
117 	};
118 
119 	write_list(stdout, prefix);
120 	if (argc > 1) {
121 		int n;
122 		for (n = 1; n < argc; n++) {
123 			FILE *fp = fopen(argv[n], "r");
124 			if (fp != 0) {
125 				make_keys(fp, stdout);
126 				fclose(fp);
127 			}
128 		}
129 	} else {
130 		make_keys(stdin, stdout);
131 	}
132 	write_list(stdout, suffix);
133 	return EXIT_SUCCESS;
134 }
135