xref: /netbsd-src/games/atc/main.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: main.c,v 1.19 2008/07/20 01:03:20 lukem 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.19 2008/07/20 01:03:20 lukem Exp $");
55 #endif
56 #endif /* not lint */
57 
58 #include "include.h"
59 #include "pathnames.h"
60 
61 extern FILE	*yyin;
62 
63 int
64 main(int argc, char *argv[])
65 {
66 	unsigned long		seed;
67 	int			f_usage = 0, f_list = 0, f_showscore = 0;
68 	int			f_printpath = 0;
69 	const char		*file = NULL;
70 	int			ch;
71 	struct sigaction	sa;
72 #ifdef BSD
73 	struct itimerval	itv;
74 #endif
75 
76 	/* Open the score file then revoke setgid privileges */
77 	open_score_file();
78 	(void)setgid(getgid());
79 
80 	start_time = time(NULL);
81 	seed = start_time;
82 
83 	while ((ch = getopt(argc, argv, ":u?lstpg:f:r:")) != -1) {
84 		switch (ch) {
85 		case '?':
86 		case 'u':
87 		default:
88 			f_usage++;
89 			break;
90 		case 'l':
91 			f_list++;
92 			break;
93 		case 's':
94 		case 't':
95 			f_showscore++;
96 			break;
97 		case 'p':
98 			f_printpath++;
99 			break;
100 		case 'r':
101 			seed = atoi(optarg);
102 			break;
103 		case 'f':
104 		case 'g':
105 			file = optarg;
106 			break;
107 		}
108 	}
109 	if (optind < argc)
110 		f_usage++;
111 	srandom(seed);
112 
113 	if (f_usage)
114 		(void)fprintf(stderr,
115 		    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
116 		    argv[0]);
117 	if (f_showscore)
118 		(void)log_score(1);
119 	if (f_list)
120 		(void)list_games();
121 	if (f_printpath) {
122 		char	buf[100];
123 
124 		(void)strlcpy(buf, _PATH_GAMES, 100);
125 		(void)puts(buf);
126 	}
127 
128 	if (f_usage || f_showscore || f_list || f_printpath)
129 		exit(0);
130 
131 	if (file == NULL)
132 		file = default_game();
133 	else
134 		file = okay_game(file);
135 
136 	if (file == NULL || read_file(file) < 0)
137 		exit(1);
138 
139 	init_gr();
140 	setup_screen(sp);
141 
142 	(void)addplane();
143 
144 	(void)signal(SIGINT, quit);
145 	(void)signal(SIGQUIT, quit);
146 #ifdef BSD
147 	(void)signal(SIGTSTP, SIG_IGN);
148 #endif
149 	(void)signal(SIGHUP, log_score_quit);
150 	(void)signal(SIGTERM, log_score_quit);
151 
152 	(void)tcgetattr(fileno(stdin), &tty_start);
153 	tty_new = tty_start;
154 	tty_new.c_lflag &= ~(ICANON|ECHO);
155 	tty_new.c_iflag |= ICRNL;
156 	tty_new.c_cc[VMIN] = 1;
157 	tty_new.c_cc[VTIME] = 0;
158 	(void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
159 
160 	sa.sa_handler = update;
161 	(void)sigemptyset(&sa.sa_mask);
162 	(void)sigaddset(&sa.sa_mask, SIGALRM);
163 	(void)sigaddset(&sa.sa_mask, SIGINT);
164 	sa.sa_flags = 0;
165 	(void)sigaction(SIGALRM, &sa, (struct sigaction *)0);
166 
167 #ifdef BSD
168 	itv.it_value.tv_sec = 0;
169 	itv.it_value.tv_usec = 1;
170 	itv.it_interval.tv_sec = sp->update_secs;
171 	itv.it_interval.tv_usec = 0;
172 	(void)setitimer(ITIMER_REAL, &itv, NULL);
173 #endif
174 #ifdef SYSV
175 	alarm(sp->update_secs);
176 #endif
177 
178 	for (;;) {
179 		if (getcommand() != 1)
180 			planewin();
181 		else {
182 #ifdef BSD
183 			itv.it_value.tv_sec = 0;
184 			itv.it_value.tv_usec = 0;
185 			(void)setitimer(ITIMER_REAL, &itv, NULL);
186 #endif
187 #ifdef SYSV
188 			alarm(0);
189 #endif
190 
191 			update(0);
192 
193 #ifdef BSD
194 			itv.it_value.tv_sec = sp->update_secs;
195 			itv.it_value.tv_usec = 0;
196 			itv.it_interval.tv_sec = sp->update_secs;
197 			itv.it_interval.tv_usec = 0;
198 			(void)setitimer(ITIMER_REAL, &itv, NULL);
199 #endif
200 #ifdef SYSV
201 			alarm(sp->update_secs);
202 #endif
203 		}
204 	}
205 }
206 
207 int
208 read_file(const char *s)
209 {
210 	int		retval;
211 
212 	filename = s;
213 	yyin = fopen(s, "r");
214 	if (yyin == NULL) {
215 		warn("fopen %s", s);
216 		return (-1);
217 	}
218 	retval = yyparse();
219 	(void)fclose(yyin);
220 
221 	if (retval != 0)
222 		return (-1);
223 	else
224 		return (0);
225 }
226 
227 const char *
228 default_game(void)
229 {
230 	FILE		*fp;
231 	static char	file[256];
232 	char		line[256], games[256];
233 
234 	(void)strlcpy(games, _PATH_GAMES, 256);
235 	(void)strlcat(games, GAMES, 256);
236 
237 	if ((fp = fopen(games, "r")) == NULL) {
238 		warn("fopen %s", games);
239 		return (NULL);
240 	}
241 	if (fgets(line, sizeof(line), fp) == NULL) {
242 		(void)fprintf(stderr, "%s: no default game available\n", games);
243 		fclose(fp);
244 		return (NULL);
245 	}
246 	(void)fclose(fp);
247 	line[strlen(line) - 1] = '\0';
248 	(void)strlcpy(file, _PATH_GAMES, 256);
249 	(void)strlcat(file, line, 256);
250 	return (file);
251 }
252 
253 const char *
254 okay_game(const char *s)
255 {
256 	FILE		*fp;
257 	static char	file[256];
258 	const char	*ret = NULL;
259 	char		line[256], games[256];
260 
261 	(void)strlcpy(games, _PATH_GAMES, 256);
262 	(void)strlcat(games, GAMES, 256);
263 
264 	if ((fp = fopen(games, "r")) == NULL) {
265 		warn("fopen %s", games);
266 		return (NULL);
267 	}
268 	while (fgets(line, sizeof(line), fp) != NULL) {
269 		line[strlen(line) - 1] = '\0';
270 		if (strcmp(s, line) == 0) {
271 			(void)strlcpy(file, _PATH_GAMES, 256);
272 			(void)strlcat(file, line, 256);
273 			ret = file;
274 			break;
275 		}
276 	}
277 	(void)fclose(fp);
278 	if (ret == NULL) {
279 		test_mode = 1;
280 		ret = s;
281 		(void)fprintf(stderr, "%s: %s: game not found\n", games, s);
282 		(void)fprintf(stderr, "Your score will not be logged.\n");
283 		(void)sleep(2);	/* give the guy time to read it */
284 	}
285 	return (ret);
286 }
287 
288 int
289 list_games(void)
290 {
291 	FILE		*fp;
292 	char		line[256], games[256];
293 	int		num_games = 0;
294 
295 	(void)strlcpy(games, _PATH_GAMES, 256);
296 	(void)strlcat(games, GAMES, 256);
297 
298 	if ((fp = fopen(games, "r")) == NULL) {
299 		warn("fopen %s", games);
300 		return (-1);
301 	}
302 	(void)puts("available games:");
303 	while (fgets(line, sizeof(line), fp) != NULL) {
304 		(void)printf("	%s", line);
305 		num_games++;
306 	}
307 	(void)fclose(fp);
308 	if (num_games == 0) {
309 		(void)fprintf(stderr, "%s: no games available\n", games);
310 		return (-1);
311 	}
312 	return (0);
313 }
314