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