3 * $Id: unihash-mkstatic.c,v 1.2 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
44 /*----- Main code ---------------------------------------------------------*/
48 static unsigned long getint(const char *p, unsigned long max,
52 unsigned long x = strtoul(p, &pp, 0);
53 if (*pp || (max && x > max))
54 die(EXIT_FAILURE, "bad %s `%s'", what, p);
58 static void version(FILE *fp)
60 pquis(fp, "$, mLib version " VERSION "\n");
63 static void usage(FILE *fp)
65 pquis(fp, "Usage: $ [-c] [-o FILE] [-g GUARD] [-i HEADER] [-s SYM]\n\
69 static void help(FILE *fp)
75 Emits a precomputed unihash_info structure for a given key.\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\
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\
90 int main(int argc, char *argv[])
92 uint32 key = 0xe07e5bd1;
96 const char *guard = 0;
108 static struct option opts[] = {
109 { "help", 0, 0, 'h' },
110 { "version", 0, 0, 'v' },
111 { "usage", 0, 0, 'u' },
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' },
122 int i = mdwopt(argc, argv, "hvu o:ck:s:i:g:", opts, 0, 0, 0);
153 key = getint(optarg, 0xffffffff, "key");
161 if ((flags & f_bogus) || optind != argc) {
166 /* --- Sort stuff out --- */
168 unihash_setkey(&u, key);
170 sym = (flags & f_ctab) ? "uhi" : "UHI_INIT";
172 /* --- Start output --- */
177 if (!(flags & f_ctab) && !guard) {
180 if ((p = malloc(strlen(file) + 1)) == 0)
181 die(EXIT_FAILURE, "not enough memory");
183 for (q = file; *q; p++, q++) {
184 if (isalnum((unsigned char)*q))
185 *p = toupper((unsigned char)*q);
191 if ((fp = fopen(file, "w")) == 0)
192 die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno));
195 /* --- Dump out the first chunk of the file --- */
200 * Unihash table (key = %08lx) [generated]\n\
205 if (flags & f_ctab) {
207 fprintf(fp, "#include \"%s\"\n\n", inc);
209 fputs("#include <mLib/unihash.h>\n\n", fp);
210 fprintf(fp, "unihash_info %s = { {\n", sym);
214 fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard);
215 n = fprintf(fp, "#define %s { {", sym);
223 /* --- Main output --- */
225 for (i = 0; i < N(u.s); i++) {
227 for (j = 0; j < N(u.s[i]); j++) {
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) {
234 fputs(flags & f_ctab ? "\n " : "\t\t\t\\\n ", fp);
237 if (j < N(u.s[i]) - 1) {
238 fputs(flags & f_ctab ? " },\n\n " :
239 " },\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n ", fp);
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);
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);
258 /*----- That's all, folks -------------------------------------------------*/