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