xref: /netbsd/games/atc/main.c (revision 6550d01e)
1 /*	$NetBSD: main.c,v 1.20 2009/08/12 04:48:03 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ed James.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
37  *
38  * Copy permission is hereby granted provided that this notice is
39  * retained on all partial or complete copies.
40  *
41  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
42  */
43 
44 #include <sys/cdefs.h>
45 #ifndef lint
46 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
47  The Regents of the University of California.  All rights reserved.");
48 #endif /* not lint */
49 
50 #ifndef lint
51 #if 0
52 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
53 #else
54 __RCSID("$NetBSD: main.c,v 1.20 2009/08/12 04:48:03 dholland Exp $");
55 #endif
56 #endif /* not lint */
57 
58 #include "include.h"
59 #include "pathnames.h"
60 
61 extern FILE	*yyin;
62 
63 static int read_file(const char *);
64 static const char *default_game(void);
65 static const char *okay_game(const char *);
66 static int list_games(void);
67 
68 int
69 main(int argc, char *argv[])
70 {
71 	unsigned long		seed;
72 	int			f_usage = 0, f_list = 0, f_showscore = 0;
73 	int			f_printpath = 0;
74 	const char		*file = NULL;
75 	int			ch;
76 	struct sigaction	sa;
77 #ifdef BSD
78 	struct itimerval	itv;
79 #endif
80 
81 	/* Open the score file then revoke setgid privileges */
82 	open_score_file();
83 	(void)setgid(getgid());
84 
85 	start_time = time(NULL);
86 	seed = start_time;
87 
88 	while ((ch = getopt(argc, argv, ":u?lstpg:f:r:")) != -1) {
89 		switch (ch) {
90 		case '?':
91 		case 'u':
92 		default:
93 			f_usage++;
94 			break;
95 		case 'l':
96 			f_list++;
97 			break;
98 		case 's':
99 		case 't':
100 			f_showscore++;
101 			break;
102 		case 'p':
103 			f_printpath++;
104 			break;
105 		case 'r':
106 			seed = atoi(optarg);
107 			break;
108 		case 'f':
109 		case 'g':
110 			file = optarg;
111 			break;
112 		}
113 	}
114 	if (optind < argc)
115 		f_usage++;
116 	srandom(seed);
117 
118 	if (f_usage)
119 		(void)fprintf(stderr,
120 		    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
121 		    argv[0]);
122 	if (f_showscore)
123 		(void)log_score(1);
124 	if (f_list)
125 		(void)list_games();
126 	if (f_printpath) {
127 		char	buf[100];
128 
129 		(void)strlcpy(buf, _PATH_GAMES, 100);
130 		(void)puts(buf);
131 	}
132 
133 	if (f_usage || f_showscore || f_list || f_printpath)
134 		exit(0);
135 
136 	if (file == NULL)
137 		file = default_game();
138 	else
139 		file = okay_game(file);
140 
141 	if (file == NULL || read_file(file) < 0)
142 		exit(1);
143 
144 	init_gr();
145 	setup_screen(sp);
146 
147 	(void)addplane();
148 
149 	(void)signal(SIGINT, quit);
150 	(void)signal(SIGQUIT, quit);
151 #ifdef BSD
152 	(void)signal(SIGTSTP, SIG_IGN);
153 #endif
154 	(void)signal(SIGHUP, log_score_quit);
155 	(void)signal(SIGTERM, log_score_quit);
156 
157 	(void)tcgetattr(fileno(stdin), &tty_start);
158 	tty_new = tty_start;
159 	tty_new.c_lflag &= ~(ICANON|ECHO);
160 	tty_new.c_iflag |= ICRNL;
161 	tty_new.c_cc[VMIN] = 1;
162 	tty_new.c_cc[VTIME] = 0;
163 	(void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
164 
165 	sa.sa_handler = update;
166 	(void)sigemptyset(&sa.sa_mask);
167 	(void)sigaddset(&sa.sa_mask, SIGALRM);
168 	(void)sigaddset(&sa.sa_mask, SIGINT);
169 	sa.sa_flags = 0;
170 	(void)sigaction(SIGALRM, &sa, (struct sigaction *)0);
171 
172 #ifdef BSD
173 	itv.it_value.tv_sec = 0;
174 	itv.it_value.tv_usec = 1;
175 	itv.it_interval.tv_sec = sp->update_secs;
176 	itv.it_interval.tv_usec = 0;
177 	(void)setitimer(ITIMER_REAL, &itv, NULL);
178 #endif
179 #ifdef SYSV
180 	alarm(sp->update_secs);
181 #endif
182 
183 	for (;;) {
184 		if (getcommand() != 1)
185 			planewin();
186 		else {
187 #ifdef BSD
188 			itv.it_value.tv_sec = 0;
189 			itv.it_value.tv_usec = 0;
190 			(void)setitimer(ITIMER_REAL, &itv, NULL);
191 #endif
192 #ifdef SYSV
193 			alarm(0);
194 #endif
195 
196 			update(0);
197 
198 #ifdef BSD
199 			itv.it_value.tv_sec = sp->update_secs;
200 			itv.it_value.tv_usec = 0;
201 			itv.it_interval.tv_sec = sp->update_secs;
202 			itv.it_interval.tv_usec = 0;
203 			(void)setitimer(ITIMER_REAL, &itv, NULL);
204 #endif
205 #ifdef SYSV
206 			alarm(sp->update_secs);
207 #endif
208 		}
209 	}
210 }
211 
212 static int
213 read_file(const char *s)
214 {
215 	int		retval;
216 
217 	filename = s;
218 	yyin = fopen(s, "r");
219 	if (yyin == NULL) {
220 		warn("fopen %s", s);
221 		return (-1);
222 	}
223 	retval = yyparse();
224 	(void)fclose(yyin);
225 
226 	if (retval != 0)
227 		return (-1);
228 	else
229 		return (0);
230 }
231 
232 static const char *
233 default_game(void)
234 {
235 	FILE		*fp;
236 	static char	file[256];
237 	char		line[256], games[256];
238 
239 	(void)strlcpy(games, _PATH_GAMES, 256);
240 	(void)strlcat(games, GAMES, 256);
241 
242 	if ((fp = fopen(games, "r")) == NULL) {
243 		warn("fopen %s", games);
244 		return (NULL);
245 	}
246 	if (fgets(line, sizeof(line), fp) == NULL) {
247 		(void)fprintf(stderr, "%s: no default game available\n", games);
248 		fclose(fp);
249 		return (NULL);
250 	}
251 	(void)fclose(fp);
252 	line[strlen(line) - 1] = '\0';
253 	(void)strlcpy(file, _PATH_GAMES, 256);
254 	(void)strlcat(file, line, 256);
255 	return (file);
256 }
257 
258 static const char *
259 okay_game(const char *s)
260 {
261 	FILE		*fp;
262 	static char	file[256];
263 	const char	*ret = NULL;
264 	char		line[256], games[256];
265 
266 	(void)strlcpy(games, _PATH_GAMES, 256);
267 	(void)strlcat(games, GAMES, 256);
268 
269 	if ((fp = fopen(games, "r")) == NULL) {
270 		warn("fopen %s", games);
271 		return (NULL);
272 	}
273 	while (fgets(line, sizeof(line), fp) != NULL) {
274 		line[strlen(line) - 1] = '\0';
275 		if (strcmp(s, line) == 0) {
276 			(void)strlcpy(file, _PATH_GAMES, 256);
277 			(void)strlcat(file, line, 256);
278 			ret = file;
279 			break;
280 		}
281 	}
282 	(void)fclose(fp);
283 	if (ret == NULL) {
284 		test_mode = 1;
285 		ret = s;
286 		(void)fprintf(stderr, "%s: %s: game not found\n", games, s);
287 		(void)fprintf(stderr, "Your score will not be logged.\n");
288 		(void)sleep(2);	/* give the guy time to read it */
289 	}
290 	return (ret);
291 }
292 
293 static int
294 list_games(void)
295 {
296 	FILE		*fp;
297 	char		line[256], games[256];
298 	int		num_games = 0;
299 
300 	(void)strlcpy(games, _PATH_GAMES, 256);
301 	(void)strlcat(games, GAMES, 256);
302 
303 	if ((fp = fopen(games, "r")) == NULL) {
304 		warn("fopen %s", games);
305 		return (-1);
306 	}
307 	(void)puts("available games:");
308 	while (fgets(line, sizeof(line), fp) != NULL) {
309 		(void)printf("	%s", line);
310 		num_games++;
311 	}
312 	(void)fclose(fp);
313 	if (num_games == 0) {
314 		(void)fprintf(stderr, "%s: no games available\n", games);
315 		return (-1);
316 	}
317 	return (0);
318 }
319