summaryrefslogtreecommitdiffstats
path: root/games/backgammon/common_source
diff options
context:
space:
mode:
authormax <max@FreeBSD.org>1997-01-30 07:12:59 +0000
committermax <max@FreeBSD.org>1997-01-30 07:12:59 +0000
commit0988434e5dc03c34f02e712f48e3cd9f66e8443e (patch)
tree69dd948eb7d0e51090079fb252afa1158177f968 /games/backgammon/common_source
parent71baeec496d609059f7e6b14bd7cac2919adac98 (diff)
downloadFreeBSD-src-0988434e5dc03c34f02e712f48e3cd9f66e8443e.zip
FreeBSD-src-0988434e5dc03c34f02e712f48e3cd9f66e8443e.tar.gz
Better handling of command-line argument:
1. Pass argc and argv to getarg and process them with getopt(). 2. Instead of using an array to save arg characters, use array of pointers and call backgammon/teachgammon with execv, instead of execl. This should fix problems with calling teachgammon. 2.2 candidate.
Diffstat (limited to 'games/backgammon/common_source')
-rw-r--r--games/backgammon/common_source/back.h4
-rw-r--r--games/backgammon/common_source/init.c2
-rw-r--r--games/backgammon/common_source/subs.c53
3 files changed, 29 insertions, 30 deletions
diff --git a/games/backgammon/common_source/back.h b/games/backgammon/common_source/back.h
index b6720d3..136938c 100644
--- a/games/backgammon/common_source/back.h
+++ b/games/backgammon/common_source/back.h
@@ -61,8 +61,8 @@ int pnum; /* color of player:
1 = red
0 = both
2 = not yet init'ed */
-char args[100]; /* args passed to teachgammon and back */
-int acnt; /* length of args */
+char *args[16]; /* args passed to teachgammon and back */
+int acnt; /* number of args */
int aflag; /* flag to ask for rules or instructions */
int bflag; /* flag for automatic board printing */
int cflag; /* case conversion flag */
diff --git a/games/backgammon/common_source/init.c b/games/backgammon/common_source/init.c
index d733ccd..82f8950 100644
--- a/games/backgammon/common_source/init.c
+++ b/games/backgammon/common_source/init.c
@@ -50,7 +50,7 @@ int pnum = 2; /* color of player:
1 = red
0 = both
2 = not yet init'ed */
-int acnt = 0; /* length of args */
+int acnt = 1; /* number of args */
int aflag = 1; /* flag to ask for rules or instructions */
int bflag = 0; /* flag for automatic board printing */
int cflag = 0; /* case conversion flag */
diff --git a/games/backgammon/common_source/subs.c b/games/backgammon/common_source/subs.c
index 5b07ad1..904ccbb 100644
--- a/games/backgammon/common_source/subs.c
+++ b/games/backgammon/common_source/subs.c
@@ -36,6 +36,7 @@ static char sccsid[] = "@(#)subs.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <stdio.h>
+#include <stdlib.h>
#include "back.h"
int buffnum;
@@ -298,25 +299,27 @@ nexturn () {
colorptr += c;
}
-getarg (arg)
-register char ***arg;
+getarg (argc, argv)
+register int argc;
+register char **argv;
{
- register char **s;
+ register char ch;
+ extern int optind;
+ extern char *optarg;
/* process arguments here. dashes are ignored, nbrw are ignored
if the game is being recovered */
- s = *arg;
- while (s[0][0] == '-') {
- switch (s[0][1]) {
+ while ((ch = getopt(argc, argv, "nbrwp:t:s:")) != EOF) {
+ switch (ch) {
/* don't ask if rules or instructions needed */
case 'n':
if (rflag)
break;
aflag = 0;
- args[acnt++] = 'n';
+ args[acnt++] = strdup("-n");
break;
/* player is both read and white */
@@ -325,7 +328,7 @@ register char ***arg;
break;
pnum = 0;
aflag = 0;
- args[acnt++] = 'b';
+ args[acnt++] = strdup("-b");
break;
/* player is red */
@@ -334,7 +337,7 @@ register char ***arg;
break;
pnum = -1;
aflag = 0;
- args[acnt++] = 'r';
+ args[acnt++] = strdup("-r");
break;
/* player is white */
@@ -343,41 +346,37 @@ register char ***arg;
break;
pnum = 1;
aflag = 0;
- args[acnt++] = 'w';
+ args[acnt++] = strdup("-w");
break;
/* print board after move according to following character */
case 'p':
- if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
+ if (optarg[0] != 'r' && optarg[0] != 'w' && optarg[0] != 'b')
break;
- args[acnt++] = 'p';
- args[acnt++] = s[0][2];
- if (s[0][2] == 'r')
+ args[acnt] = strdup("-p ");
+ args[acnt++][2] = optarg[0];
+ if (optarg[0] == 'r')
bflag = 1;
- if (s[0][2] == 'w')
+ if (optarg[0] == 'w')
bflag = -1;
- if (s[0][2] == 'b')
+ if (optarg[0] == 'b')
bflag = 0;
break;
case 't':
- if (s[0][2] == '\0') { /* get terminal caps */
- s++;
- tflag = getcaps (*s);
- } else
- tflag = getcaps (&s[0][2]);
+ tflag = getcaps (optarg);
break;
case 's':
- s++;
/* recover file */
- recover (s[0]);
+ recover (optarg);
break;
}
- s++;
}
- if (s[0] != 0 && s[0][0] != '\0')
- recover(s[0]);
+ argc -= optind;
+ argv += optind;
+ if ( argc && argv[0][0] != '\0' )
+ recover(argv[0]);
}
init () {
@@ -428,7 +427,7 @@ getout () {
/* fix terminal status */
fixtty (old);
- exit();
+ exit(0);
}
roll () {
register char c;
OpenPOWER on IntegriCloud