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