gnushogi-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnushogi-devel] [Patch v2 1/1] Switch the argument parsing to getopt


From: Justin Vreeland
Subject: [Gnushogi-devel] [Patch v2 1/1] Switch the argument parsing to getopt
Date: Tue, 13 Feb 2018 18:06:38 -0700

---
 TODO            |   1 -
 gnushogi/main.c | 244 +++++++++++++++++++++++++++-----------------------------
 2 files changed, 116 insertions(+), 129 deletions(-)

diff --git a/TODO b/TODO
index dc772b2..e60c177 100644
--- a/TODO
+++ b/TODO
@@ -17,7 +17,6 @@ Target for v1.5:
 
 Generic cleanups
 
-- switch cli parsing to getopt
 - hunt for extern's and prototypes spread all over the source
 - hunt for more hardcoded variant-specific constants
   - position of captured pieces in curses mode
diff --git a/gnushogi/main.c b/gnushogi/main.c
index d9fae21..60ce16d 100644
--- a/gnushogi/main.c
+++ b/gnushogi/main.c
@@ -33,62 +33,43 @@
 #include "gnushogi.h"
 
 #include <signal.h>
-
-
-void print_arglist(int argc, char **argv)
-{
-    int i;
-
-    for (i = 0; i < argc; i++)
-        printf("argv[%d] = %s\n", i, argv[i]);
-
-    printf("\n");
-}
+#include <getopt.h>
 
 
 int
 main (int argc, char **argv)
 {
+    int opt;
     /*
      * Process command-line arguments.
      */
 
-    /* Get rid of the program name. */
-
-    argc--;
-    argv++;
-
     /* CHECKME: get rid of the '+' syntax? */
 
-    while ((argc > 0) && ((argv[0][0] == '-') || (argv[0][0] == '+')))
+    while ((opt = getopt(argc, argv, "ab:B:Chl:L::s:P:RS:r:T:c:tvXx:")) != -1)
     {
-        switch (argv[0][1])
+
+        switch (opt)
         {
         case 'a':
             /* Need the "+" syntax here... */
-            ahead = ((argv[0][0] == '-') ? false : true);
+            /* Defaults to true, so it doesn't matter if we notice the
+             * '+' symbol anyway we can ignore it an so will getopt */
+            /* if POSIXLY_CORRECT is set it will stop parsing on '+' symbols */
+            ahead = false;
             break;
 
 
         case 'b':
-            argc--;
-            argv++;
-
-            if (argc > 0)
-            {
-                bookfile = argv[0];
+            bookfile = optarg;
 #ifdef BINBOOK
-                binbookfile = NULL;
+            binbookfile = NULL;
 #endif
-            }
             break;
 
 #ifdef BINBOOK
         case 'B':
-            argc--;
-            argv++;
-            if (argc > 0)
-                binbookfile = argv[0];
+            binbookfile = optarg;
             break;
 #endif
 
@@ -101,40 +82,24 @@ main (int argc, char **argv)
 #endif
 
         case 'h':
-            /* Need the "+" syntax here... */
-            hash = ((argv[0][0] == '-') ? false : true);
+            /* Defaults to true, so  it doesn't matter if we notice the
+             * '+' symbol anyway we can ignore it an so will getopt */
+            /* if POSIXLY_CORRECT is set it will stop parsing on '+' symbols */
+            hash = false;
             break;
 
         case 'l':
-            argc--;
-            argv++;
-
-            if (argc > 0)
-                Lang = argv[0];
+            Lang = optarg;
             break;
 
         case 'L':
-            argc--;
-            argv++;
-
-            if (argc > 0)
-                strcpy(listfile, argv[0]);
-            break;
+            strcpy(listfile, optarg);
 
         case 's':
-            argc--;
-            argv++;
-
-            if (argc > 0)
-                strcpy(savefile, argv[0]);
-            break;
+            strcpy(savefile, optarg);
 
         case 'P':
-            argc--;
-            argv++;
-
-            if (argc > 0)
-                bookmaxply = atoi(argv[0]);
+            bookmaxply = atoi(optarg);
             break;
 
         case 'R':
@@ -144,41 +109,33 @@ main (int argc, char **argv)
             break;
 
         case 'S':
-            argc--;
-            argv++;
-
-            if (argc > 0)
-                booksize = atoi(argv[0]);
+            booksize = atoi(optarg);
             break;
 
 #if ttblsz
         case 'r':
-            argc--;
-            argv++;
 
-            if (argc > 0)
-                rehash = atoi(argv[0]);
+            rehash = atoi(optarg);
+
             if (rehash > MAXrehash)
                 rehash = MAXrehash;
+
             break;
 
         case 'T':
-            argc--;
-            argv++;
 
-            if (argc > 0)
-                ttblsize = atoi(argv[0]);
+            ttblsize = atoi(optarg);
+
             if (ttblsize <= MINTTABLE)
                 ttblsize = (MINTTABLE) + 1;
+
             break;
 
 #ifdef HASHFILE
         case 'c':   /* Create or test persistent transposition table. */
-            argc--;
-            argv++;
 
-            if (argc > 0)
-                filesz = atoi(argv[0]);
+            if (optarg)
+                filesz = atoi(optarg);
             else
                 filesz = vfilesz;
 
@@ -279,11 +236,8 @@ main (int argc, char **argv)
             break;
 
         case 'x':
-            argc--;
-            argv++;
 
-            if (argc > 0)
-                xwin = argv[0];
+            xwin = optarg;
             break;
 
         default:
@@ -291,82 +245,116 @@ main (int argc, char **argv)
             exit(1);
         }
 
-        argc--;
-        argv++;
     }
 
-    if (argc == 2)
+    /* ignore + signs */
+    while (optind < argc)
     {
-        char *p;
+        if (argv[optind][0] != '+')
+            break;
 
-        MaxResponseTime = 100L * strtol(argv[1], &p, 10);
+        optind += 1;
+    }
 
-        if (*p == ':')
+
+    if (optind != argc) {
+        /* gnushogi <max response time> */
+        if ((argc - optind) == 1)
         {
-            MaxResponseTime = 60L * MaxResponseTime +
-                100L * strtol(++p, (char **) NULL, 10);
-        }
+            char *p;
 
-        TCflag    = false;
-        TCmoves   = 0;
-        TCminutes = 0;
-        TCseconds = 0;
-    }
+            MaxResponseTime = 100L * strtol(argv[optind], &p, 10);
 
-    if (argc >= 3)
-    {
-        char *p;
+            if (p == argv[optind])
+            {
+                    fprintf(stderr, "Invalid time: %s\n", argv[optind]);
+                    exit(1);
+            }
 
-        if (argc > 9)
-        {
-            printf("Time Control Error\n");
-            exit(1);
-        }
+            if (*p == ':')
+            {
+                MaxResponseTime = 60L * MaxResponseTime +
+                    100L * strtol(p + 1, &p, 10);
+            }
 
-        TCmoves   = atoi(argv[1]);
-        TCminutes = (short)strtol(argv[2], &p, 10);
+            /* Trailing non-digit characters */
+            if (*p != '\0')
+            {
+               fprintf(stderr, "Ivalid responce time: %s\n", argv[optind]);
+               exit(1);
+            }
 
-        if (*p == ':')
-            TCseconds = (short)strtol(p + 1, (char **) NULL, 10);
-        else
+            TCflag    = false;
+            TCmoves   = 0;
+            TCminutes = 0;
             TCseconds = 0;
 
-        TCflag = true;
-        argc -= 3;
-        argv += 3;
-
-        while (argc > 1)
+        /* TC arguments must be no more than 4, and provided as pairs */
+        } else if (((argc - optind) > 8) || ((argc - optind) % 2))
         {
-            XCmoves[XC]   = atoi(argv[0]);
-            XCminutes[XC] = (short)strtol(argv[1], &p, 10);
+            fputs("Too many or uneven number of arguments\n", stderr);
+            exit(1);
+        } else
+        {
+            char *p;
+
+            TCmoves   = atoi(argv[optind]);
+            TCminutes = (short)strtol(argv[optind + 1], &p, 10);
 
             if (*p == ':')
-                XCseconds[XC] = (short)strtol(p + 1, (char **) NULL, 10);
+                TCseconds = (short)strtol(p + 1, &p, 10);
             else
-                XCseconds[XC] = 0;
+                TCseconds = 0;
 
-            if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
-                XC++;
-            else
+            if (p == argv[optind + 1])
             {
-                printf("Time Control Error\n");
+                    fprintf(stderr, "Invalid time: %s\n", argv[optind + 1]);
+                    exit(1);
+            }
+
+            if (*p != '\0') {
+                fprintf(stderr, "Invalid time: %s\n", argv[optind + 1]);
                 exit(1);
             }
 
-            argc -= 2;
-            argv += 2;
-        }
+            TCflag = true;
 
-        if (argc)
-        {
-            /*
-             * If we got here, there are unknown arguments, so issue
-             * an error message and quit.
-             */
+            optind += 2;
+            while (argc - optind)
+            {
+                XCmoves[XC]   = atoi(argv[optind]);
+                XCminutes[XC] = (short)strtol(argv[optind + 1], &p, 10);
 
-            printf("Invalid command-line arguments:\n");
-            print_arglist(argc, argv);
-            exit(1);
+                if (p == argv[optind + 1])
+                {
+                        fprintf(stderr, "Invalid time: %s\n", argv[optind + 
1]);
+                        exit(1);
+                }
+
+                if (*p == ':')
+                {
+                    XCseconds[XC] = (short)strtol(p + 1, &p, 10);
+                    if (*p != '\0')
+                    {
+                        fprintf(stderr, "Invalid time: %s\n", argv[optind + 
1]);
+                        exit(1);
+                    }
+                } else
+                {
+                    XCseconds[XC] = 0;
+                }
+
+                if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
+                {
+                    XC++;
+                } else
+                {
+                    printf("Time Control Error\n");
+                    exit(1);
+                }
+
+                optind += 2;
+            }
         }
     }
 
-- 
2.16.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]