chiark / gitweb /
Infrastructure: Split the files into subdirectories.
[mLib] / hash / unihash-mkstatic.c
CommitLineData
18756bee 1/* -*-c-*-
18756bee 2 *
3 * Build static universal hash tables
4 *
5 * (c) 2003 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
18756bee 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
18756bee 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
d4efbcd9 21 *
18756bee 22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
18756bee 28/*----- Header files ------------------------------------------------------*/
29
a0150d8d
MW
30#include "config.h"
31
18756bee 32#include <ctype.h>
33#include <errno.h>
34#include <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37
38#include "macros.h"
39#include "mdwopt.h"
40#include "quis.h"
41#include "report.h"
42#include "unihash.h"
43
44/*----- Main code ---------------------------------------------------------*/
45
46#define BSCOL 72
47
48static unsigned long getint(const char *p, unsigned long max,
49 const char *what)
50{
51 char *pp;
52 unsigned long x = strtoul(p, &pp, 0);
53 if (*pp || (max && x > max))
54 die(EXIT_FAILURE, "bad %s `%s'", what, p);
55 return (x);
56}
57
58static void version(FILE *fp)
59{
60 pquis(fp, "$, mLib version " VERSION "\n");
61}
62
63static void usage(FILE *fp)
64{
65 pquis(fp, "Usage: $ [-c] [-o FILE] [-g GUARD] [-i HEADER] [-s SYM]\n\
66 [-k KEY]\n");
67}
68
69static void help(FILE *fp)
70{
71 version(fp);
72 putc('\n', stdout);
73 usage(fp);
74 fputs("\n\
75Emits a precomputed unihash_info structure for a given key.\n\
76\n\
77-h, --help Show this help text.\n\
78-v, --version Show the program's version number.\n\
79-u, --usage Show a terse usage message.\n\
80\n\
81-c, --c-source Emit a C source file rather than a header.\n\
82-k, --key=KEY Use KEY as the universal hashing key.\n\
83-g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
84-i, --include=HEADER Include HEADER at top of C source file.\n\
85-s, --symbol=SYM Name the generated table SYM.\n\
86-o, --output=FILE Write the output to FILE.\n\
87", stdout);
88}
89
90int main(int argc, char *argv[])
91{
92 uint32 key = 0xe07e5bd1;
93 unsigned flags = 0;
94 const char *sym = 0;
95 const char *inc = 0;
96 const char *guard = 0;
97 const char *file = 0;
98 FILE *fp;
99 unihash_info u;
100 int i, j, k;
101
102#define f_bogus 1u
103#define f_ctab 2u
104
105 ego(argv[0]);
106
107 for (;;) {
108 static struct option opts[] = {
109 { "help", 0, 0, 'h' },
110 { "version", 0, 0, 'v' },
111 { "usage", 0, 0, 'u' },
112
113 { "output", OPTF_ARGREQ, 0, 'o' },
114 { "c-source", 0, 0, 'c' },
115 { "key", OPTF_ARGREQ, 0, 'k' },
116 { "symbol", OPTF_ARGREQ, 0, 's' },
117 { "include", OPTF_ARGREQ, 0, 'i' },
118 { "guard", OPTF_ARGREQ, 0, 'g' },
119
120 { 0, 0, 0, 0 }
121 };
122 int i = mdwopt(argc, argv, "hvu o:ck:s:i:g:", opts, 0, 0, 0);
123
124 if (i < 0)
125 break;
126 switch (i) {
127 case 'h':
128 help(stdout);
129 exit(0);
130 case 'v':
131 version(stdout);
132 exit(0);
133 case 'u':
134 usage(stdout);
135 exit(0);
136
137 case 'o':
138 file = optarg;
139 break;
140 case 'c':
141 flags |= f_ctab;
142 break;
143 case 's':
144 sym = optarg;
145 break;
146 case 'i':
147 inc = optarg;
148 break;
149 case 'g':
150 guard = optarg;
151 break;
152 case 'k':
153 key = getint(optarg, 0xffffffff, "key");
154 break;
155
156 default:
157 flags |= f_bogus;
158 break;
159 }
160 }
161 if ((flags & f_bogus) || optind != argc) {
162 usage(stderr);
163 exit(EXIT_FAILURE);
164 }
165
166 /* --- Sort stuff out --- */
167
168 unihash_setkey(&u, key);
169 if (!sym)
170 sym = (flags & f_ctab) ? "uhi" : "UHI_INIT";
d4efbcd9 171
18756bee 172 /* --- Start output --- */
173
174 if (!file)
175 fp = stdout;
176 else {
177 if (!(flags & f_ctab) && !guard) {
178 char *p;
179 const char *q;
180 if ((p = malloc(strlen(file) + 1)) == 0)
181 die(EXIT_FAILURE, "not enough memory");
182 guard = p;
183 for (q = file; *q; p++, q++) {
184 if (isalnum((unsigned char)*q))
185 *p = toupper((unsigned char)*q);
186 else
187 *p = '_';
188 }
189 *p++ = 0;
190 }
191 if ((fp = fopen(file, "w")) == 0)
192 die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno));
193 }
194
195 /* --- Dump out the first chunk of the file --- */
196
197 fprintf(fp, "\
198/* -*-c-*-\n\
199 *\n\
200 * Unihash table (key = %08lx) [generated]\n\
201 */\n\
202\n",
203 (unsigned long)key);
204
205 if (flags & f_ctab) {
206 if (inc)
207 fprintf(fp, "#include \"%s\"\n\n", inc);
208 else
209 fputs("#include <mLib/unihash.h>\n\n", fp);
210 fprintf(fp, "unihash_info %s = { {\n", sym);
211 } else {
212 int n;
213 if (guard)
214 fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard);
215 n = fprintf(fp, "#define %s { {", sym);
216 while (n < BSCOL) {
217 fputc('\t', fp);
218 n = (n + 8) & ~7;
219 }
220 fputc('\n', fp);
221 }
222
223 /* --- Main output --- */
224
225 for (i = 0; i < N(u.s); i++) {
226 fputs(" {", fp);
227 for (j = 0; j < N(u.s[i]); j++) {
228 fputs(" {", fp);
229 for (k = 0; k < N(u.s[i][j]); k++) {
230 fprintf(fp, " 0x%08lx", (unsigned long)u.s[i][j][k]);
231 if (k < N(u.s[i][j]) - 1) {
232 fputc(',', fp);
233 if (k % 4 == 3)
d4efbcd9 234 fputs(flags & f_ctab ? "\n " : "\t\t\t\\\n ", fp);
18756bee 235 }
236 }
237 if (j < N(u.s[i]) - 1) {
d4efbcd9
MW
238 fputs(flags & f_ctab ? " },\n\n " :
239 " },\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n ", fp);
18756bee 240 }
241 }
242 if (i < N(u.s) - 1) {
243 fputs(flags & f_ctab ? " } },\n\n" :
244 " } },\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n", fp);
245 }
246 }
247
248 /* --- Done --- */
249
250 fputs(flags & f_ctab ? " } }\n} };\n" :
251 " } }\t\t\\\n} }\n", fp);
252 if (!(flags & f_ctab) && guard)
253 fputs("\n#endif\n", fp);
254
255 return (0);
256}
257
258/*----- That's all, folks -------------------------------------------------*/