]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/make_keys.c
ncurses 6.1 - patch 20191102
[ncurses.git] / ncurses / tinfo / make_keys.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2015 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                    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
40 #include <build.priv.h>
41
42 MODULE_ID("$Id: make_keys.c,v 1.21 2015/07/16 01:10:03 tom Exp $")
43
44 #include <names.c>
45
46 static unsigned
47 unknown(void)
48 {
49     static unsigned result = 0;
50
51     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
63 static unsigned
64 lookup(const char *name)
65 {
66     unsigned n;
67     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     }
82     return found ? n : unknown();
83 }
84
85 static void
86 make_keys(FILE *ifp, FILE *ofp)
87 {
88     char buffer[BUFSIZ];
89     char from[256];
90     char to[256];
91     unsigned ignore = unknown();
92     unsigned maxlen = 16;
93     int scanned;
94
95     while (fgets(buffer, (int) sizeof(buffer), ifp) != 0) {
96         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) {
104             unsigned code = lookup(from);
105             if (code == ignore)
106                 continue;
107             if (strlen(from) > maxlen)
108                 maxlen = (unsigned) strlen(from);
109             fprintf(ofp, "\t{ %4u, %-*.*s },\t/* %s */\n",
110                     code,
111                     (int) maxlen, (int) maxlen,
112                     to,
113                     from);
114         }
115     }
116 }
117
118 static void
119 write_list(FILE *ofp, const char **list)
120 {
121     while (*list != 0)
122         fprintf(ofp, "%s\n", *list++);
123 }
124
125 int
126 main(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 }