hash/crc-mktab.c, hash/unihash-mkstatic.c: Add `const' option.
[mLib] / hash / crc-mktab.c
index d6f4149..4453cf3 100644 (file)
@@ -57,6 +57,7 @@ static FILE *fp;
 #define f_bogus 1u
 #define f_ctab 2u
 #define f_reverse 4u
+#define f_const 8u
 
 #define BSCOL 72
 
@@ -79,7 +80,7 @@ static void version(FILE *fp)
 
 static void usage(FILE *fp)
 {
-  pquis(fp, "Usage: $ [-cr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
+  pquis(fp, "Usage: $ [-Ccr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
        [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n");
 }
 
@@ -97,6 +98,7 @@ of options are provided:\n\
 -u, --usage            Show a terse usage message.\n\
 \n\
 -c, --c-source         Emit a C source file rather than a header.\n\
+-C, --const            Declare table to be `const' (only useful with `-c').\n\
 -b, --bits=BITS                Emit a table for a BITS bits-wide CRC.\n\
 -B, --bit-chunk=BITS   Emit a table to process BITS bits at a time.\n\
 -p, --polynomial=POLY  Use the POLY as the dividing polynomial.\n\
@@ -117,7 +119,7 @@ unsigned long reflect(unsigned long x, unsigned b)
   if (!(flags & f_reverse))
     return (x);
   xm = 1;
-  ym = 1 << (b - 1);
+  ym = 1u << (b - 1);
   for (i = 0; i < b; i++) {
     if (x & xm)
       y |= ym;
@@ -143,6 +145,7 @@ int main(int argc, char *argv[])
 
       { "output",      OPTF_ARGREQ,    0,      'o' },
       { "c-source",    0,              0,      'c' },
+      { "const",       0,              0,      'C' },
       { "symbol",      OPTF_ARGREQ,    0,      's' },
       { "type",                OPTF_ARGREQ,    0,      't' },
       { "include",     OPTF_ARGREQ,    0,      'i' },
@@ -155,7 +158,7 @@ int main(int argc, char *argv[])
 
       { 0,             0,              0,      0 }
     };
-    int i = mdwopt(argc, argv, "hvu o:cs:t:i:g: b:B:p:r", opts, 0, 0, 0);
+    int i = mdwopt(argc, argv, "hvu o:cCs:t:i:g: b:B:p:r", opts, 0, 0, 0);
 
     if (i < 0)
       break;
@@ -176,6 +179,9 @@ int main(int argc, char *argv[])
       case 'c':
        flags |= f_ctab;
        break;
+      case 'C':
+       flags |= f_const;
+       break;
       case 's':
        sym = optarg;
        break;
@@ -287,7 +293,8 @@ int main(int argc, char *argv[])
   if (flags & f_ctab) {
     if (inc)
       fprintf(fp, "#include \"%s\"\n\n", inc);
-    fprintf(fp, "%s %s[] = {\n", type, sym);
+    fprintf(fp, "%s%s %s[] = {\n",
+           (flags&f_const) ? "const " : "", type, sym);
   } else {
     int n;
     if (guard)