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 unified 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,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: main.c,v 1.29 2009/07/20 06:43:18 dholland Exp $ */ 1/* $NetBSD: main.c,v 1.30 2009/08/05 04:03:47 dholland Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1980, 1993 4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -29,122 +29,120 @@ @@ -29,122 +29,120 @@
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1980, 1993\ 34__COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved."); 35 The Regents of the University of California. All rights reserved.");
36#endif /* not lint */ 36#endif /* not lint */
37 37
38#ifndef lint 38#ifndef lint
39#if 0 39#if 0
40static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; 40static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
41#else 41#else
42__RCSID("$NetBSD: main.c,v 1.29 2009/07/20 06:43:18 dholland Exp $"); 42__RCSID("$NetBSD: main.c,v 1.30 2009/08/05 04:03:47 dholland Exp $");
43#endif 43#endif
44#endif /* not lint */ 44#endif /* not lint */
45 45
46#include <ctype.h> 46#include <ctype.h>
47#include <curses.h> 47#include <curses.h>
48#include <err.h> 48#include <err.h>
49#include <errno.h> 49#include <errno.h>
50#include <fcntl.h> 50#include <fcntl.h>
51#include <signal.h> 51#include <signal.h>
52#include <stdlib.h> 52#include <stdlib.h>
53#include <time.h> 53#include <time.h>
54#include <unistd.h> 54#include <unistd.h>
55#include "robots.h" 55#include "robots.h"
56 56
57extern const char *Scorefile; 57extern const char *Scorefile;
58extern int Max_per_uid; 58extern int Max_per_uid;
59 59
60int 60int
61main(int ac, char **av) 61main(int argc, char **argv)
62{ 62{
63 const char *sp; 63 const char *word;
64 bool bad_arg; 
65 bool show_only; 64 bool show_only;
66 int score_wfd; /* high score writable file descriptor */ 65 int score_wfd; /* high score writable file descriptor */
67 int score_err = 0; /* hold errno from score file open */ 66 int score_err = 0; /* hold errno from score file open */
 67 int maximum = 0;
 68 char ch;
 69 int i;
68 70
69 score_wfd = open(Scorefile, O_RDWR); 71 score_wfd = open(Scorefile, O_RDWR);
70 if (score_wfd < 0) 72 if (score_wfd < 0)
71 score_err = errno; 73 score_err = errno;
72 else if (score_wfd < 3) 74 else if (score_wfd < 3)
73 exit(1); 75 exit(1);
74 76
75 /* Revoke setgid privileges */ 77 /* Revoke setgid privileges */
76 setgid(getgid()); 78 setgid(getgid());
77 79
78 show_only = false; 80 show_only = false;
79 Num_games = 1; 81 Num_games = 1;
80 if (ac > 1) { 82
81 bad_arg = false; 83 while ((ch = getopt(argc, argv, "Aajnrst")) != -1) {
82 for (++av; ac > 1 && *av[0]; av++, ac--) 84 switch (ch) {
83 if (av[0][0] != '-') 85 case 'A':
84 if (isdigit((unsigned char)av[0][0])) 86 Auto_bot = true;
85 Max_per_uid = atoi(av[0]); 87 break;
86 else { 88 case 'a':
87 Scorefile = av[0]; 89 Start_level = 4;
88 if (score_wfd >= 0) 90 break;
89 close(score_wfd); 91 case 'j':
90 score_wfd = open(Scorefile, O_RDWR); 92 Jump = true;
91 if (score_wfd < 0) 93 break;
92 score_err = errno; 94 case 'n':
 95 Num_games++;
 96 break;
 97 case 'r':
 98 Real_time = true;
 99 break;
 100 case 's':
 101 show_only = true;
 102 break;
 103 case 't':
 104 Teleport = true;
 105 break;
 106 default:
 107 errx(1,
 108 "Usage: robots [-Aajnrst] [maximum] [scorefile]");
 109 break;
 110 }
 111 }
 112
 113 for (i = optind; i < argc; i++) {
 114 word = argv[i];
 115 if (isdigit((unsigned char)word[0])) {
 116 maximum = atoi(word);
 117 } else {
 118 Scorefile = word;
 119 Max_per_uid = maximum;
 120 if (score_wfd >= 0)
 121 close(score_wfd);
 122 score_wfd = open(Scorefile, O_RDWR);
 123 if (score_wfd < 0)
 124 score_err = errno;
93#ifdef FANCY 125#ifdef FANCY
94 sp = strrchr(Scorefile, '/'); 126 word = strrchr(Scorefile, '/');
95 if (sp == NULL) 127 if (word == NULL)
96 sp = Scorefile; 128 word = Scorefile;
97 if (strcmp(sp, "pattern_roll") == 0) 129 if (strcmp(word, "pattern_roll") == 0)
98 Pattern_roll = true; 130 Pattern_roll = true;
99 else if (strcmp(sp, "stand_still") == 0) 131 else if (strcmp(word, "stand_still") == 0)
100 Stand_still = true; 132 Stand_still = true;
101 if (Pattern_roll || Stand_still) 133 if (Pattern_roll || Stand_still)
102 Teleport = true; 134 Teleport = true;
103#endif 135#endif
104 } 
105 else 
106 for (sp = &av[0][1]; *sp; sp++) 
107 switch (*sp) { 
108 case 'A': 
109 Auto_bot = true; 
110 break; 
111 case 's': 
112 show_only = true; 
113 break; 
114 case 'r': 
115 Real_time = true; 
116 break; 
117 case 'a': 
118 Start_level = 4; 
119 break; 
120 case 'n': 
121 Num_games++; 
122 break; 
123 case 'j': 
124 Jump = true; 
125 break; 
126 case 't': 
127 Teleport = true; 
128 break; 
129 
130 default: 
131 fprintf(stderr, "robots: unknown option: %c\n", *sp); 
132 bad_arg = true; 
133 break; 
134 } 
135 if (bad_arg) { 
136 exit(1); 
137 /* NOTREACHED */ 
138 } 136 }
139 } 137 }
140 138
141 if (show_only) { 139 if (show_only) {
142 show_score(); 140 show_score();
143 exit(0); 141 exit(0);
144 /* NOTREACHED */ 142 /* NOTREACHED */
145 } 143 }
146 144
147 if (score_wfd < 0) { 145 if (score_wfd < 0) {
148 errno = score_err; 146 errno = score_err;
149 warn("%s", Scorefile); 147 warn("%s", Scorefile);
150 warnx("High scores will not be recorded!"); 148 warnx("High scores will not be recorded!");

cvs diff -r1.14 -r1.15 src/games/robots/robots.6 (expand / switch to unified 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,14 +1,14 @@ @@ -1,14 +1,14 @@
1.\" $NetBSD: robots.6,v 1.14 2009/04/09 03:52:54 joerg Exp $ 1.\" $NetBSD: robots.6,v 1.15 2009/08/05 04:03:47 dholland Exp $
2.\" 2.\"
3.\" Copyright (c) 1991, 1993 3.\" Copyright (c) 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
5.\" 5.\"
6.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
8.\" are met: 8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright 9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer. 10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the 12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution. 13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors 14.\" 3. Neither the name of the University nor the names of its contributors
@@ -19,35 +19,36 @@ @@ -19,35 +19,36 @@
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE. 28.\" SUCH DAMAGE.
29.\" 29.\"
30.\" @(#)robots.6 8.1 (Berkeley) 5/31/93 30.\" @(#)robots.6 8.1 (Berkeley) 5/31/93
31.\" 31.\"
32.Dd May 31, 1993 32.Dd August 4, 2009
33.Dt ROBOTS 6 33.Dt ROBOTS 6
34.Os 34.Os
35.Sh NAME 35.Sh NAME
36.Nm robots 36.Nm robots
37.Nd fight off villainous robots 37.Nd fight off villainous robots
38.Sh SYNOPSIS 38.Sh SYNOPSIS
39.Nm 39.Nm
40.Op Fl Asjtan 40.Op Fl Aajnrst
 41.Op Ar maximum
41.Op Ar scorefile 42.Op Ar scorefile
42.Sh DESCRIPTION 43.Sh DESCRIPTION
43.Nm 44.Nm
44pits you against evil robots, who are trying to kill you (which is why 45pits you against evil robots, who are trying to kill you (which is why
45they are evil). 46they are evil).
46Fortunately for you, even though they are evil, they are not very bright 47Fortunately for you, even though they are evil, they are not very bright
47and have a habit of bumping into each other, thus destroying themselves. 48and have a habit of bumping into each other, thus destroying themselves.
48In order to survive, you must get them to kill each other off, since you 49In order to survive, you must get them to kill each other off, since you
49have no offensive weaponry. 50have no offensive weaponry.
50.Pp 51.Pp
51Since you are stuck without offensive weaponry, you are endowed with one 52Since you are stuck without offensive weaponry, you are endowed with one
52piece of defensive weaponry: a teleportation device. 53piece of defensive weaponry: a teleportation device.
53When two robots run into each other or a junk pile, they die. 54When two robots run into each other or a junk pile, they die.
@@ -105,52 +106,64 @@ If you use the @@ -105,52 +106,64 @@ If you use the
105.Sq Ic w 106.Sq Ic w
106command and survive to the next level, you will get a bonus of 10% 107command and survive to the next level, you will get a bonus of 10%
107for each robot which died after you decided to wait. 108for each robot which died after you decided to wait.
108If you die, however, you get nothing. 109If you die, however, you get nothing.
109For all other commands, the program will save you from typos 110For all other commands, the program will save you from typos
110by stopping short of being eaten. 111by stopping short of being eaten.
111However, with 112However, with
112.Sq Ic w 113.Sq Ic w
113you take the risk of dying by miscalculation. 114you take the risk of dying by miscalculation.
114.Pp 115.Pp
115Only five scores are allowed per user on the score file. 116Only five scores are allowed per user on the score file.
116If you make it into the score file, you will be shown the list at the end 117If you make it into the score file, you will be shown the list at the end
117of the game. 118of the game.
118If an alternative score file is specified, that will be used instead of the 119If an alternative score file is named on the command line, that file
119standard file for scores. 120will be used instead of the standard file for scores.
 121The score file must be created empty beforehand, e.g. with
 122.Xr touch 1 .
 123If the argument
 124.Ar maximum ,
 125which must be a number, is provided when a score file is first used,
 126the value given will be used as the maximum number of scores to keep
 127per user instead of the default five.
 128This value is a property of the score file and cannot be changed later.
120.Pp 129.Pp
121The options are 130The options are
122.Bl -tag -width indent 131.Bl -tag -width indent
123.It Fl s 132.It Fl A
124Don't play, just show the score file. 133Auto-bot mode.
 134Lets the game play itself.
 135.It Fl a
 136Advance into the higher levels directly, skipping the lower, easier levels.
125.It Fl j 137.It Fl j
126Jump, 138Jump,
127.Em i.e. , 139.Em i.e. ,
128when you run, don't show any intermediate positions; only show things at 140when you run, don't show any intermediate positions; only show things at
129the end. 141the end.
130This is useful on slow terminals. 142This is useful on slow terminals.
 143.It Fl n
 144Increase the number of games played by one; that is, automatically
 145choose to play again one time per usage of this option.
 146.It Fl r
 147Play in real time; that is, if you do nothing for a few seconds the
 148game will assume you meant to do nothing and move the robots.
 149.It Fl s
 150Don't play, just show the score file.
131.It Fl t 151.It Fl t
132Teleport automatically when you have no other option. 152Teleport automatically when you have no other option.
133This is a little disconcerting until you get used to it, and then it is 153This is a little disconcerting until you get used to it, and then it is
134very nice. 154very nice.
135.It Fl a 
136Advance into the higher levels directly, skipping the lower, easier levels. 
137.It Fl A 
138Auto-bot mode. 
139Lets the game play itself. 
140.It Fl n 
141Increase the number of games played by one. 
142.El 155.El
143.Sh FILES 156.Sh FILES
144.Bl -tag -width /var/games/robots_roll -compact 157.Bl -tag -width /var/games/robots_roll -compact
145.It Pa /var/games/robots_roll 158.It Pa /var/games/robots_roll
146the score file 159The score file.
147.El 160.El
148.Sh AUTHORS 161.Sh AUTHORS
149.An Ken Arnold 162.An Ken Arnold
150.An Christos Zoulas 163.An Christos Zoulas
151(autobot mode) 164(autobot mode)
152.Sh BUGS 165.Sh BUGS
153Bugs? 166Bugs?
154You 167You
155.Em crazy , 168.Em crazy ,
156man?!? 169man?!?