3 * $Id: unihash-mkstatic.c,v 1.1 2003/12/15 20:52:56 mdw Exp $
5 * Build static universal hash tables
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: unihash-mkstatic.c,v $
33 * Revision 1.1 2003/12/15 20:52:56 mdw
34 * New program to make fixed tables for universal hashing.
38 /*----- Header files ------------------------------------------------------*/
52 /*----- Main code ---------------------------------------------------------*/
56 static unsigned long getint(const char *p, unsigned long max,
60 unsigned long x = strtoul(p, &pp, 0);
61 if (*pp || (max && x > max))
62 die(EXIT_FAILURE, "bad %s `%s'", what, p);
66 static void version(FILE *fp)
68 pquis(fp, "$, mLib version " VERSION "\n");
71 static void usage(FILE *fp)
73 pquis(fp, "Usage: $ [-c] [-o FILE] [-g GUARD] [-i HEADER] [-s SYM]\n\
77 static void help(FILE *fp)
83 Emits a precomputed unihash_info structure for a given key.\n\
85 -h, --help Show this help text.\n\
86 -v, --version Show the program's version number.\n\
87 -u, --usage Show a terse usage message.\n\
89 -c, --c-source Emit a C source file rather than a header.\n\
90 -k, --key=KEY Use KEY as the universal hashing key.\n\
91 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
92 -i, --include=HEADER Include HEADER at top of C source file.\n\
93 -s, --symbol=SYM Name the generated table SYM.\n\
94 -o, --output=FILE Write the output to FILE.\n\
98 int main(int argc, char *argv[])
100 uint32 key = 0xe07e5bd1;
104 const char *guard = 0;
105 const char *file = 0;
116 static struct option opts[] = {
117 { "help", 0, 0, 'h' },
118 { "version", 0, 0, 'v' },
119 { "usage", 0, 0, 'u' },
121 { "output", OPTF_ARGREQ, 0, 'o' },
122 { "c-source", 0, 0, 'c' },
123 { "key", OPTF_ARGREQ, 0, 'k' },
124 { "symbol", OPTF_ARGREQ, 0, 's' },
125 { "include", OPTF_ARGREQ, 0, 'i' },
126 { "guard", OPTF_ARGREQ, 0, 'g' },
130 int i = mdwopt(argc, argv, "hvu o:ck:s:i:g:", opts, 0, 0, 0);
161 key = getint(optarg, 0xffffffff, "key");
169 if ((flags & f_bogus) || optind != argc) {
174 /* --- Sort stuff out --- */
176 unihash_setkey(&u, key);
178 sym = (flags & f_ctab) ? "uhi" : "UHI_INIT";
180 /* --- Start output --- */
185 if (!(flags & f_ctab) && !guard) {
188 if ((p = malloc(strlen(file) + 1)) == 0)
189 die(EXIT_FAILURE, "not enough memory");
191 for (q = file; *q; p++, q++) {
192 if (isalnum((unsigned char)*q))
193 *p = toupper((unsigned char)*q);
199 if ((fp = fopen(file, "w")) == 0)
200 die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno));
203 /* --- Dump out the first chunk of the file --- */
208 * Unihash table (key = %08lx) [generated]\n\
213 if (flags & f_ctab) {
215 fprintf(fp, "#include \"%s\"\n\n", inc);
217 fputs("#include <mLib/unihash.h>\n\n", fp);
218 fprintf(fp, "unihash_info %s = { {\n", sym);
222 fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard);
223 n = fprintf(fp, "#define %s { {", sym);
231 /* --- Main output --- */
233 for (i = 0; i < N(u.s); i++) {
235 for (j = 0; j < N(u.s[i]); j++) {
237 for (k = 0; k < N(u.s[i][j]); k++) {
238 fprintf(fp, " 0x%08lx", (unsigned long)u.s[i][j][k]);
239 if (k < N(u.s[i][j]) - 1) {
242 fputs(flags & f_ctab ? "\n " : "\t\t\t\\\n ", fp);
245 if (j < N(u.s[i]) - 1) {
246 fputs(flags & f_ctab ? " },\n\n " :
247 " },\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n ", fp);
250 if (i < N(u.s) - 1) {
251 fputs(flags & f_ctab ? " } },\n\n" :
252 " } },\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n", fp);
258 fputs(flags & f_ctab ? " } }\n} };\n" :
259 " } }\t\t\\\n} }\n", fp);
260 if (!(flags & f_ctab) && guard)
261 fputs("\n#endif\n", fp);
266 /*----- That's all, folks -------------------------------------------------*/