[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gnushogi-devel] [Patch 1/1] Switch the argument parsing to getopt
From: |
Justin Vreeland |
Subject: |
[Gnushogi-devel] [Patch 1/1] Switch the argument parsing to getopt |
Date: |
Sun, 4 Feb 2018 14:25:17 -0700 |
---
gnushogi/main.c | 130 +++++++++++++++++++++-----------------------------------
1 file changed, 49 insertions(+), 81 deletions(-)
diff --git a/gnushogi/main.c b/gnushogi/main.c
index d9fae21..7e323ea 100644
--- a/gnushogi/main.c
+++ b/gnushogi/main.c
@@ -33,6 +33,7 @@
#include "gnushogi.h"
#include <signal.h>
+#include <getopt.h>
void print_arglist(int argc, char **argv)
@@ -49,46 +50,37 @@ void print_arglist(int argc, char **argv)
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 +93,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 +120,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 +247,8 @@ main (int argc, char **argv)
break;
case 'x':
- argc--;
- argv++;
- if (argc > 0)
- xwin = argv[0];
+ xwin = optarg;
break;
default:
@@ -291,15 +256,13 @@ main (int argc, char **argv)
exit(1);
}
- argc--;
- argv++;
}
- if (argc == 2)
+ if ((argc - optind) == 2)
{
char *p;
- MaxResponseTime = 100L * strtol(argv[1], &p, 10);
+ MaxResponseTime = 100L * strtol(argv[optind], &p, 10);
if (*p == ':')
{
@@ -313,7 +276,7 @@ main (int argc, char **argv)
TCseconds = 0;
}
- if (argc >= 3)
+ if (optind + 1 < argc)
{
char *p;
@@ -323,8 +286,8 @@ main (int argc, char **argv)
exit(1);
}
- TCmoves = atoi(argv[1]);
- TCminutes = (short)strtol(argv[2], &p, 10);
+ TCmoves = atoi(argv[optind]);
+ TCminutes = (short)strtol(argv[optind + 1], &p, 10);
if (*p == ':')
TCseconds = (short)strtol(p + 1, (char **) NULL, 10);
@@ -332,13 +295,12 @@ main (int argc, char **argv)
TCseconds = 0;
TCflag = true;
- argc -= 3;
- argv += 3;
+ optind += 3;
- while (argc > 1)
+ while ((argc - optind) >= 3)
{
- XCmoves[XC] = atoi(argv[0]);
- XCminutes[XC] = (short)strtol(argv[1], &p, 10);
+ XCmoves[XC] = atoi(argv[optind]);
+ XCminutes[XC] = (short)strtol(argv[optind + 1], &p, 10);
if (*p == ':')
XCseconds[XC] = (short)strtol(p + 1, (char **) NULL, 10);
@@ -352,12 +314,18 @@ main (int argc, char **argv)
printf("Time Control Error\n");
exit(1);
}
+ }
- argc -= 2;
- argv += 2;
+ /* looking for '+' signs */
+ for (int i = 0; i < argc; i++) {
+ if (argv[i][0] == '+') {
+ if (argv[i][1] == 'a' || argv[i][1] == 'h') {
+ optind++;
+ }
+ }
}
- if (argc)
+ if (argc - optind)
{
/*
* If we got here, there are unknown arguments, so issue
@@ -365,7 +333,7 @@ main (int argc, char **argv)
*/
printf("Invalid command-line arguments:\n");
- print_arglist(argc, argv);
+ print_arglist(optind, argv + optind);
exit(1);
}
}
--
2.16.1