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