diff options
author | max <max@FreeBSD.org> | 1997-01-30 07:12:59 +0000 |
---|---|---|
committer | max <max@FreeBSD.org> | 1997-01-30 07:12:59 +0000 |
commit | 0988434e5dc03c34f02e712f48e3cd9f66e8443e (patch) | |
tree | 69dd948eb7d0e51090079fb252afa1158177f968 /games | |
parent | 71baeec496d609059f7e6b14bd7cac2919adac98 (diff) | |
download | FreeBSD-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')
-rw-r--r-- | games/backgammon/backgammon/main.c | 17 | ||||
-rw-r--r-- | games/backgammon/common_source/back.h | 4 | ||||
-rw-r--r-- | games/backgammon/common_source/init.c | 2 | ||||
-rw-r--r-- | games/backgammon/common_source/subs.c | 53 | ||||
-rw-r--r-- | games/backgammon/teachgammon/teach.c | 17 |
5 files changed, 48 insertions, 45 deletions
diff --git a/games/backgammon/backgammon/main.c b/games/backgammon/backgammon/main.c index 0102a92..64cd2a6 100644 --- a/games/backgammon/backgammon/main.c +++ b/games/backgammon/backgammon/main.c @@ -42,6 +42,7 @@ static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; #endif /* not lint */ #include <stdio.h> +#include <unistd.h> #include "back.h" #define MVPAUSE 5 /* time to sleep when stuck */ @@ -105,6 +106,7 @@ char **argv; /* initialization */ bflag = 2; /* default no board */ + acnt = 1; /* Nuber of args */ signal (2,getout); /* trap interrupts */ if (gtty (0,&tty) == -1) /* get old tty mode */ errexit ("backgammon(gtty)"); @@ -138,13 +140,8 @@ char **argv; t = time(0); srandom(t); /* 'random' seed */ -#ifdef V7 - while (*++argv != 0) /* process arguments */ -#else - while (*++argv != -1) /* process arguments */ -#endif - getarg (&argv); - args[acnt] = '\0'; + getarg (argc, argv); + args[acnt] = NULL; if (tflag) { /* clear screen */ noech &= ~(CRMOD|XTABS); raw &= ~(CRMOD|XTABS); @@ -174,7 +171,8 @@ char **argv; if (yorn(0)) { fixtty (old); /* restore tty */ - execl (TEACH,"teachgammon",args,0); + args[0] = strdup("teachgammon"); + execv (TEACH,args); tflag = 0; /* error! */ writel (noteach); @@ -189,6 +187,9 @@ char **argv; } } + for (i = 0; i < acnt; i++) + free(args[i]); + init(); /* initialize board */ if (pnum == 2) { /* ask for color(s) */ 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; diff --git a/games/backgammon/teachgammon/teach.c b/games/backgammon/teachgammon/teach.c index 9a97452..8d3ae25 100644 --- a/games/backgammon/teachgammon/teach.c +++ b/games/backgammon/teachgammon/teach.c @@ -41,6 +41,7 @@ static char copyright[] = static char sccsid[] = "@(#)teach.c 8.1 (Berkeley) 5/31/93"; #endif /* not lint */ +#include <string.h> #include "back.h" char *hello[]; @@ -78,6 +79,7 @@ char **argv; { register int i; + acnt = 1; signal (2,getout); if (gtty (0,&tty) == -1) /* get old tty mode */ errexit ("teachgammon(gtty)"); @@ -89,12 +91,7 @@ char **argv; #endif ospeed = tty.sg_ospeed; /* for termlib */ tflag = getcaps (getenv ("TERM")); -#ifdef V7 - while (*++argv != 0) -#else - while (*++argv != -1) -#endif - getarg (&argv); + getarg (argc, argv); if (tflag) { noech &= ~(CRMOD|XTABS); raw &= ~(CRMOD|XTABS); @@ -155,12 +152,18 @@ char **argv; } leave() { + register int i; if (tflag) clear(); else writec ('\n'); fixtty(old); - execl (EXEC,"backgammon",args,"n",0); + args[0] = strdup("backgammon"); + args[acnt++] = strdup("-n"); + args[acnt] = 0; + execv (EXEC,args); + for (i = 0; i < acnt; i++) + free(args[i]); writel ("Help! Backgammon program is missing\007!!\n"); exit (-1); } |