Wed Aug 5 04:03:47 2009 UTC ()
Use getopt instead of hand-rolled options code. Document all the arguments
and options. Don't allow the previously undocumented method to change the
maximum number of scores kept per user to be used on the system-wide high
score file. Sort options list in the man page. Bump its date.


(dholland)
diff -r1.29 -r1.30 src/games/robots/main.c
diff -r1.14 -r1.15 src/games/robots/robots.6

cvs diff -r1.29 -r1.30 src/games/robots/main.c (expand / switch to context diff)
--- src/games/robots/main.c 2009/07/20 06:43:18 1.29
+++ src/games/robots/main.c 2009/08/05 04:03:47 1.30
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.29 2009/07/20 06:43:18 dholland Exp $	*/
+/*	$NetBSD: main.c,v 1.30 2009/08/05 04:03:47 dholland Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: main.c,v 1.29 2009/07/20 06:43:18 dholland Exp $");
+__RCSID("$NetBSD: main.c,v 1.30 2009/08/05 04:03:47 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -58,13 +58,15 @@
 extern int Max_per_uid;
 
 int
-main(int ac, char **av)
+main(int argc, char **argv)
 {
-	const char *sp;
-	bool bad_arg;
+	const char *word;
 	bool show_only;
 	int score_wfd; /* high score writable file descriptor */
 	int score_err = 0; /* hold errno from score file open */
+	int maximum = 0;
+	char ch;
+	int i;
 
 	score_wfd = open(Scorefile, O_RDWR);
 	if (score_wfd < 0)
@@ -77,64 +79,60 @@
 
 	show_only = false;
 	Num_games = 1;
-	if (ac > 1) {
-		bad_arg = false;
-		for (++av; ac > 1 && *av[0]; av++, ac--)
-			if (av[0][0] != '-')
-				if (isdigit((unsigned char)av[0][0]))
-					Max_per_uid = atoi(av[0]);
-				else {
-					Scorefile = av[0];
-					if (score_wfd >= 0)
-						close(score_wfd);
-					score_wfd = open(Scorefile, O_RDWR);
-					if (score_wfd < 0)
-						score_err = errno;
+
+	while ((ch = getopt(argc, argv, "Aajnrst")) != -1) {
+		switch (ch) {
+		    case 'A':
+			Auto_bot = true;
+			break;
+		    case 'a':
+			Start_level = 4;
+			break;
+		    case 'j':
+			Jump = true;
+			break;
+		    case 'n':
+			Num_games++;
+			break;
+		    case 'r':
+			Real_time = true;
+			break;
+		    case 's':
+			show_only = true;
+			break;
+		    case 't':
+			Teleport = true;
+			break;
+		    default:
+			errx(1,
+			    "Usage: robots [-Aajnrst] [maximum] [scorefile]");
+			break;
+		}
+	}
+
+	for (i = optind; i < argc; i++) {
+		word = argv[i];
+		if (isdigit((unsigned char)word[0])) {
+			maximum = atoi(word);
+		} else {
+			Scorefile = word;
+			Max_per_uid = maximum;
+			if (score_wfd >= 0)
+				close(score_wfd);
+			score_wfd = open(Scorefile, O_RDWR);
+			if (score_wfd < 0)
+				score_err = errno;
 #ifdef FANCY
-					sp = strrchr(Scorefile, '/');
-					if (sp == NULL)
-						sp = Scorefile;
-					if (strcmp(sp, "pattern_roll") == 0)
-						Pattern_roll = true;
-					else if (strcmp(sp, "stand_still") == 0)
-						Stand_still = true;
-					if (Pattern_roll || Stand_still)
-						Teleport = true;
+			word = strrchr(Scorefile, '/');
+			if (word == NULL)
+				word = Scorefile;
+			if (strcmp(word, "pattern_roll") == 0)
+				Pattern_roll = true;
+			else if (strcmp(word, "stand_still") == 0)
+				Stand_still = true;
+			if (Pattern_roll || Stand_still)
+				Teleport = true;
 #endif
-				}
-			else
-				for (sp = &av[0][1]; *sp; sp++)
-					switch (*sp) {
-					  case 'A':
-						Auto_bot = true;
-						break;
-					  case 's':
-						show_only = true;
-						break;
-					  case 'r':
-						Real_time = true;
-						break;
-					  case 'a':
-						Start_level = 4;
-						break;
-					  case 'n':
-						Num_games++;
-						break;
-					  case 'j':
-						Jump = true;
-						break;
-					  case 't':
-						Teleport = true;
-						break;
-
-					  default:
-						fprintf(stderr, "robots: unknown option: %c\n", *sp);
-						bad_arg = true;
-						break;
-					}
-		if (bad_arg) {
-			exit(1);
-			/* NOTREACHED */
 		}
 	}
 

cvs diff -r1.14 -r1.15 src/games/robots/robots.6 (expand / switch to context diff)
--- src/games/robots/robots.6 2009/04/09 03:52:54 1.14
+++ src/games/robots/robots.6 2009/08/05 04:03:47 1.15
@@ -1,4 +1,4 @@
-.\"	$NetBSD: robots.6,v 1.14 2009/04/09 03:52:54 joerg Exp $
+.\"	$NetBSD: robots.6,v 1.15 2009/08/05 04:03:47 dholland Exp $
 .\"
 .\" Copyright (c) 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	@(#)robots.6	8.1 (Berkeley) 5/31/93
 .\"
-.Dd May 31, 1993
+.Dd August 4, 2009
 .Dt ROBOTS 6
 .Os
 .Sh NAME
@@ -37,7 +37,8 @@
 .Nd fight off villainous robots
 .Sh SYNOPSIS
 .Nm
-.Op Fl Asjtan
+.Op Fl Aajnrst
+.Op Ar maximum
 .Op Ar scorefile
 .Sh DESCRIPTION
 .Nm
@@ -115,35 +116,47 @@
 Only five scores are allowed per user on the score file.
 If you make it into the score file, you will be shown the list at the end
 of the game.
-If an alternative score file is specified, that will be used instead of the
-standard file for scores.
+If an alternative score file is named on the command line, that file
+will be used instead of the standard file for scores.
+The score file must be created empty beforehand, e.g. with
+.Xr touch 1 .
+If the argument
+.Ar maximum ,
+which must be a number, is provided when a score file is first used,
+the value given will be used as the maximum number of scores to keep
+per user instead of the default five.
+This value is a property of the score file and cannot be changed later.
 .Pp
 The options are
 .Bl -tag -width indent
-.It Fl s
-Don't play, just show the score file.
+.It Fl A
+Auto-bot mode.
+Lets the game play itself.
+.It Fl a
+Advance into the higher levels directly, skipping the lower, easier levels.
 .It Fl j
 Jump,
 .Em i.e. ,
 when you run, don't show any intermediate positions; only show things at
 the end.
 This is useful on slow terminals.
+.It Fl n
+Increase the number of games played by one; that is, automatically
+choose to play again one time per usage of this option.
+.It Fl r
+Play in real time; that is, if you do nothing for a few seconds the
+game will assume you meant to do nothing and move the robots.
+.It Fl s
+Don't play, just show the score file.
 .It Fl t
 Teleport automatically when you have no other option.
 This is a little disconcerting until you get used to it, and then it is
 very nice.
-.It Fl a
-Advance into the higher levels directly, skipping the lower, easier levels.
-.It Fl A
-Auto-bot mode.
-Lets the game play itself.
-.It Fl n
-Increase the number of games played by one.
 .El
 .Sh FILES
 .Bl -tag -width /var/games/robots_roll -compact
 .It Pa /var/games/robots_roll
-the score file
+The score file.
 .El
 .Sh AUTHORS
 .An Ken Arnold