Provide proper help and options parsing. Allow more bases. Use
authormdw <mdw>
Sun, 13 Jan 2002 19:51:59 +0000 (19:51 +0000)
committermdw <mdw>
Sun, 13 Jan 2002 19:51:59 +0000 (19:51 +0000)
@mptext@ to read integers for the better base support.

factorial.c

index 71f2ad2..789de90 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: factorial.c,v 1.2 2001/06/16 13:22:59 mdw Exp $
+ * $Id: factorial.c,v 1.3 2002/01/13 19:51:59 mdw Exp $
  *
  * Example factorial computation
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: factorial.c,v $
+ * Revision 1.3  2002/01/13 19:51:59  mdw
+ * Provide proper help and options parsing.  Allow more bases.  Use
+ * @mptext@ to read integers for the better base support.
+ *
  * Revision 1.2  2001/06/16 13:22:59  mdw
  * Added command-line option to select output radix.
  *
 
 /*----- Header files ------------------------------------------------------*/
 
+#include "config.h"
+
+#include <ctype.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <mLib/quis.h>
 #include <mLib/report.h>
 
+#include "mpint.h"
 #include "mpmul.h"
+#include "mptext.h"
 
 /*----- Main code ---------------------------------------------------------*/
 
+static void usage(FILE *fp)
+{
+  pquis(fp, "Usage: $ [-r radix] integer\n");
+}
+
+static void version(FILE *fp)
+{
+  pquis(fp, "$, Catacomb version " VERSION "\n");
+}
+
+static void help(FILE *fp)
+{
+  version(fp);
+  putc('\n', fp);
+  usage(fp);
+  fputs("\n\
+Prints the factorial of the given integer on its output.  Input may be\n\
+in decimal (the default), octal with preceding zero, hex with preceding\n\
+`0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
+Output may be in any base between 2 and 62; the default is base 10.  For\n\
+bases between 11 and 36 inclusive, lowercase letters of either case are\n\
+used as additional digits with values 10 upwards; lowercase is always\n\
+used for output.  For bases between 37 and 62 inclusive, lowercase letters\n\
+have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
+the value 36.\n\
+\n\
+Options provided:\n\
+\n\
+-h, --help             Display this help message.\n\
+-v, --version          Display the version number.\n\
+-u, --usage            Display a usage message.\n\
+\n\
+-r, --radix=N          Write output in base N.\n\
+", fp);
+}
+
 int main(int argc, char *argv[])
 {
   unsigned long x;
   int r = 10;
   char *p;
-  mp *f;
+  mp *f, *ulmax, *xx;
+  unsigned fl = 0;
+
+#define f_bogus 1u
 
   ego(argv[0]);
 
   for (;;) {
     static const struct option opt[] = {
+      { "help",                0,              0,      'h' },
+      { "version",     0,              0,      'v' },
+      { "usage",       0,              0,      'u' },
       { "radix",       OPTF_ARGREQ,    0,      'r' },
       { 0,             0,              0,      0 }
     };
-    int i = mdwopt(argc, argv, "r:", opt, 0, 0, 0);
+    int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
     if (i < 0)
       break;
     switch (i) {
+      case 'h':
+       help(stdout);
+       exit(0);
+      case 'v':
+       version(stdout);
+       exit(0);
+      case 'u':
+       usage(stdout);
+       exit(0);
       case 'r':
        r = atoi(optarg);
-       if (r < 2 || r > 36)
+       if (r < 2 || r > 62)
          die(EXIT_FAILURE, "bad radix `%s'", optarg);
        break;
       default:
-       exit(EXIT_FAILURE);
+       fl |= f_bogus;
+       break;
     }
   }
 
-  if (optind + 1 != argc) {
-    pquis(stderr, "Usage: $ [-r radix] integer\n");
+  if (optind + 1 != argc || (fl & f_bogus)) {
+    usage(stderr);
     exit(EXIT_FAILURE);
   }
-  x = strtoul(argv[optind], &p, 0);
-  if (*p)
+  ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
+  p = argv[optind];
+  while (isspace((unsigned char)*p))
+    p++;
+  xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
+  while (isspace((unsigned char)*p))
+    p++;
+  if (!xx || *p || MP_CMP(xx, >, ulmax))
     die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
+  x = mp_toulong(xx);
+  mp_drop(xx);
+  mp_drop(ulmax);
   f = mp_factorial(x);
   mp_writefile(f, stdout, r);
   fputc('\n', stdout);