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