3 * Build static universal hash tables
5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- 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: $ [-Cc] [-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 -C, --const Declare table to be `const' (only useful with `-c').\n\
83 -k, --key=KEY Use KEY as the universal hashing key.\n\
84 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
85 -i, --include=HEADER Include HEADER at top of C source file.\n\
86 -s, --symbol=SYM Name the generated table SYM.\n\
87 -o, --output=FILE Write the output to FILE.\n\
91 int main(int argc
, char *argv
[])
93 uint32 key
= 0xe07e5bd1;
97 const char *guard
= 0;
110 static struct option opts
[] = {
111 { "help", 0, 0, 'h' },
112 { "version", 0, 0, 'v' },
113 { "usage", 0, 0, 'u' },
115 { "output", OPTF_ARGREQ
, 0, 'o' },
116 { "c-source", 0, 0, 'c' },
117 { "const", 0, 0, 'C' },
118 { "key", OPTF_ARGREQ
, 0, 'k' },
119 { "symbol", OPTF_ARGREQ
, 0, 's' },
120 { "include", OPTF_ARGREQ
, 0, 'i' },
121 { "guard", OPTF_ARGREQ
, 0, 'g' },
125 int i
= mdwopt(argc
, argv
, "hvu o:cCk:s:i:g:", opts
, 0, 0, 0);
159 key
= getint(optarg
, 0xffffffff, "key");
167 if ((flags
& f_bogus
) || optind
!= argc
) {
172 /* --- Sort stuff out --- */
174 unihash_setkey(&u
, key
);
176 sym
= (flags
& f_ctab
) ?
"uhi" : "UHI_INIT";
178 /* --- Start output --- */
183 if (!(flags
& f_ctab
) && !guard
) {
186 if ((p
= malloc(strlen(file
) + 1)) == 0)
187 die(EXIT_FAILURE
, "not enough memory");
189 for (q
= file
; *q
; p
++, q
++) {
190 if (isalnum((unsigned char)*q
))
191 *p
= toupper((unsigned char)*q
);
197 if ((fp
= fopen(file
, "w")) == 0)
198 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
201 /* --- Dump out the first chunk of the file --- */
206 * Unihash table (key = %08lx) [generated]\n\
211 if (flags
& f_ctab
) {
213 fprintf(fp
, "#include \"%s\"\n\n", inc
);
215 fputs("#include <mLib/unihash.h>\n\n", fp
);
216 fprintf(fp
, "%sunihash_info %s = { {\n",
217 (flags
&f_const
) ?
"const " : "", sym
);
221 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
222 n
= fprintf(fp
, "#define %s { {", sym
);
230 /* --- Main output --- */
232 for (i
= 0; i
< N(u
.s
); i
++) {
234 for (j
= 0; j
< N(u
.s
[i
]); j
++) {
236 for (k
= 0; k
< N(u
.s
[i
][j
]); k
++) {
237 fprintf(fp
, " 0x%08lx", (unsigned long)u
.s
[i
][j
][k
]);
238 if (k
< N(u
.s
[i
][j
]) - 1) {
241 fputs(flags
& f_ctab ?
"\n " : "\t\t\t\\\n ", fp
);
244 if (j
< N(u
.s
[i
]) - 1) {
245 fputs(flags
& f_ctab ?
" },\n\n " :
246 " },\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n ", fp
);
249 if (i
< N(u
.s
) - 1) {
250 fputs(flags
& f_ctab ?
" } },\n\n" :
251 " } },\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n", fp
);
257 fputs(flags
& f_ctab ?
" } }\n} };\n" :
258 " } }\t\t\\\n} }\n", fp
);
259 if (!(flags
& f_ctab
) && guard
)
260 fputs("\n#endif\n", fp
);
265 /*----- That's all, folks -------------------------------------------------*/