| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Build static universal hash tables |
| 4 | * |
| 5 | * (c) 2003 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 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 | |
| 48 | static 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 | |
| 58 | static void version(FILE *fp) |
| 59 | { |
| 60 | pquis(fp, "$, mLib version " VERSION "\n"); |
| 61 | } |
| 62 | |
| 63 | static void usage(FILE *fp) |
| 64 | { |
| 65 | pquis(fp, "Usage: $ [-Cc] [-o FILE] [-g GUARD] [-i HEADER] [-s SYM]\n\ |
| 66 | [-k KEY]\n"); |
| 67 | } |
| 68 | |
| 69 | static void help(FILE *fp) |
| 70 | { |
| 71 | version(fp); |
| 72 | putc('\n', stdout); |
| 73 | usage(fp); |
| 74 | fputs("\n\ |
| 75 | Emits 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 | -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\ |
| 88 | ", stdout); |
| 89 | } |
| 90 | |
| 91 | int main(int argc, char *argv[]) |
| 92 | { |
| 93 | uint32 key = 0xe07e5bd1; |
| 94 | unsigned flags = 0; |
| 95 | const char *sym = 0; |
| 96 | const char *inc = 0; |
| 97 | const char *guard = 0; |
| 98 | const char *file = 0; |
| 99 | FILE *fp; |
| 100 | unihash_info u; |
| 101 | int i, j, k; |
| 102 | |
| 103 | #define f_bogus 1u |
| 104 | #define f_ctab 2u |
| 105 | #define f_const 4u |
| 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 | { "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' }, |
| 122 | |
| 123 | { 0, 0, 0, 0 } |
| 124 | }; |
| 125 | int i = mdwopt(argc, argv, "hvu o:cCk:s:i:g:", opts, 0, 0, 0); |
| 126 | |
| 127 | if (i < 0) |
| 128 | break; |
| 129 | switch (i) { |
| 130 | case 'h': |
| 131 | help(stdout); |
| 132 | exit(0); |
| 133 | case 'v': |
| 134 | version(stdout); |
| 135 | exit(0); |
| 136 | case 'u': |
| 137 | usage(stdout); |
| 138 | exit(0); |
| 139 | |
| 140 | case 'o': |
| 141 | file = optarg; |
| 142 | break; |
| 143 | case 'c': |
| 144 | flags |= f_ctab; |
| 145 | break; |
| 146 | case 'C': |
| 147 | flags |= f_const; |
| 148 | break; |
| 149 | case 's': |
| 150 | sym = optarg; |
| 151 | break; |
| 152 | case 'i': |
| 153 | inc = optarg; |
| 154 | break; |
| 155 | case 'g': |
| 156 | guard = optarg; |
| 157 | break; |
| 158 | case 'k': |
| 159 | key = getint(optarg, 0xffffffff, "key"); |
| 160 | break; |
| 161 | |
| 162 | default: |
| 163 | flags |= f_bogus; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if ((flags & f_bogus) || optind != argc) { |
| 168 | usage(stderr); |
| 169 | exit(EXIT_FAILURE); |
| 170 | } |
| 171 | |
| 172 | /* --- Sort stuff out --- */ |
| 173 | |
| 174 | unihash_setkey(&u, key); |
| 175 | if (!sym) |
| 176 | sym = (flags & f_ctab) ? "uhi" : "UHI_INIT"; |
| 177 | |
| 178 | /* --- Start output --- */ |
| 179 | |
| 180 | if (!file) |
| 181 | fp = stdout; |
| 182 | else { |
| 183 | if (!(flags & f_ctab) && !guard) { |
| 184 | char *p; |
| 185 | const char *q; |
| 186 | if ((p = malloc(strlen(file) + 1)) == 0) |
| 187 | die(EXIT_FAILURE, "not enough memory"); |
| 188 | guard = p; |
| 189 | for (q = file; *q; p++, q++) { |
| 190 | if (isalnum((unsigned char)*q)) |
| 191 | *p = toupper((unsigned char)*q); |
| 192 | else |
| 193 | *p = '_'; |
| 194 | } |
| 195 | *p++ = 0; |
| 196 | } |
| 197 | if ((fp = fopen(file, "w")) == 0) |
| 198 | die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno)); |
| 199 | } |
| 200 | |
| 201 | /* --- Dump out the first chunk of the file --- */ |
| 202 | |
| 203 | fprintf(fp, "\ |
| 204 | /* -*-c-*-\n\ |
| 205 | *\n\ |
| 206 | * Unihash table (key = %08lx) [generated]\n\ |
| 207 | */\n\ |
| 208 | \n", |
| 209 | (unsigned long)key); |
| 210 | |
| 211 | if (flags & f_ctab) { |
| 212 | if (inc) |
| 213 | fprintf(fp, "#include \"%s\"\n\n", inc); |
| 214 | else |
| 215 | fputs("#include <mLib/unihash.h>\n\n", fp); |
| 216 | fprintf(fp, "%sunihash_info %s = { {\n", |
| 217 | (flags&f_const) ? "const " : "", sym); |
| 218 | } else { |
| 219 | int n; |
| 220 | if (guard) |
| 221 | fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard); |
| 222 | n = fprintf(fp, "#define %s { {", sym); |
| 223 | while (n < BSCOL) { |
| 224 | fputc('\t', fp); |
| 225 | n = (n + 8) & ~7; |
| 226 | } |
| 227 | fputc('\n', fp); |
| 228 | } |
| 229 | |
| 230 | /* --- Main output --- */ |
| 231 | |
| 232 | for (i = 0; i < N(u.s); i++) { |
| 233 | fputs(" {", fp); |
| 234 | for (j = 0; j < N(u.s[i]); j++) { |
| 235 | fputs(" {", fp); |
| 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) { |
| 239 | fputc(',', fp); |
| 240 | if (k % 4 == 3) |
| 241 | fputs(flags & f_ctab ? "\n " : "\t\t\t\\\n ", fp); |
| 242 | } |
| 243 | } |
| 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); |
| 247 | } |
| 248 | } |
| 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); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* --- Done --- */ |
| 256 | |
| 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); |
| 261 | |
| 262 | return (0); |
| 263 | } |
| 264 | |
| 265 | /*----- That's all, folks -------------------------------------------------*/ |