]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/make_keys.c
ncurses 6.2 - patch 20201227
[ncurses.git] / ncurses / tinfo / make_keys.c
1 /****************************************************************************
2  * Copyright 2020 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.22 2020/02/02 23:34:34 tom Exp $")
44
45 #include <names.c>
46
47 static unsigned
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
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
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
120 write_list(FILE *ofp, const char **list)
121 {
122     while (*list != 0)
123         fprintf(ofp, "%s\n", *list++);
124 }
125
126 int
127 main(int argc, char *argv[])
128 {
129     static const char *prefix[] =
130     {
131         "#ifndef NCU_KEYS_H",
132         "#define NCU_KEYS_H 1",
133         "",
134         "/* This file was generated by MAKE_KEYS */",
135         "",
136         "#if BROKEN_LINKER",
137         "static",
138         "#endif",
139         "const struct tinfo_fkeys _nc_tinfo_fkeys[] = {",
140         0
141     };
142     static const char *suffix[] =
143     {
144         "\t{ 0, 0} };",
145         "",
146         "#endif /* NCU_KEYS_H */",
147         0
148     };
149
150     write_list(stdout, prefix);
151     if (argc > 1) {
152         int n;
153         for (n = 1; n < argc; n++) {
154             FILE *fp = fopen(argv[n], "r");
155             if (fp != 0) {
156                 make_keys(fp, stdout);
157                 fclose(fp);
158             }
159         }
160     } else {
161         make_keys(stdin, stdout);
162     }
163     write_list(stdout, suffix);
164     return EXIT_SUCCESS;
165 }