Gather up another utility.
[u/mdw/catacomb] / genprimes.c
index 4474110..93e041b 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: genprimes.c,v 1.1 1999/11/19 13:19:37 mdw Exp $
+ * $Id: genprimes.c,v 1.7 2004/04/08 01:36:15 mdw Exp $
  *
  * Generate prime number table
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: genprimes.c,v $
- * Revision 1.1  1999/11/19 13:19:37  mdw
- * Generate small primes table.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <ctype.h>
@@ -59,16 +51,17 @@ int main(int argc, char *argv[])
 {
   int p_max = 0, p_n = 0;
   char *type = "unsigned int";
-  char *header = "ptab.h";
-  char *source = "ptab.c";
-  char *name = "ptab";
+  char *header = "primetab.h";
+  char *source = "primetab.c";
+  char *name = "primetab";
+  char *sym = 0;
   intv p = DA_INIT;
   int i;
 
   ego(argv[0]);
 
   for (;;) {
-    int i = getopt(argc, argv, "h:c:i:n:m:t:");
+    int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
     if (i < 0)
       break;
     switch (i) {
@@ -92,6 +85,9 @@ int main(int argc, char *argv[])
       case 't':
        type = optarg;
        break;
+      case 's':
+       sym = optarg;
+       break;
       default:
        pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
        exit(EXIT_FAILURE);
@@ -101,7 +97,7 @@ int main(int argc, char *argv[])
   if (!p_max && !p_n)
     die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
 
-  if (p_max >= 2)
+  if (p_n || p_max >= 2)
     DA_PUSH(&p, 2);
   for (i = 3; (!p_max && !p_n) ||
              (p_n && DA_LEN(&p) < p_n) ||
@@ -122,15 +118,18 @@ int main(int argc, char *argv[])
     char *q;
     if (!fp)
       die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
-    for (q = header; *q; q++) {
-      int ch = (unsigned char)*q;
-      if (isalnum(ch))
-       ch = toupper(ch);
-      else
-       ch = '_';
-      DPUTC(&d, ch);
+    if (!sym) {
+      for (q = header; *q; q++) {
+       int ch = (unsigned char)*q;
+       if (isalnum(ch))
+         ch = toupper(ch);
+       else
+         ch = '_';
+       DPUTC(&d, ch);
+      }
+      DPUTZ(&d);
+      sym = d.buf;
     }
-    DPUTZ(&d);
     fprintf(fp, "\
 /* -*-c-*-\n\
  *\n\
@@ -140,16 +139,17 @@ int main(int argc, char *argv[])
 #ifndef %s\n\
 #define %s\n\
 \n\
-#define NPRIME %lu\n\
-#define MAXPRIME %i\n\
+#define NPRIME %luu\n\
+#define MAXPRIME %uu\n\
 \n\
-extern %s %s[];\n\
+typedef %s smallprime;\n\
+extern const smallprime %s[];\n\
 \n\
 #endif\n\
 ",
-           d.buf, d.buf,
+           sym, sym,
            (unsigned long)DA_LEN(&p),
-           DA(&p)[DA_LEN(&p) - 1],
+           DA_LAST(&p),
            type, name);
     dstr_destroy(&d);
     if (fclose(fp) == EOF) {
@@ -171,7 +171,7 @@ extern %s %s[];\n\
 \n\
 #include \"%s\"\n\
 \n\
-%s %s[] = {",
+const %s %s[] = {",
            header, type, name);
     for (i = 0; i < DA_LEN(&p); i++) {
       if (i % 8 == 0)