main.c revision 1.3 1 /* $NetBSD: main.c,v 1.3 1995/03/21 15:04:24 cgd 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 (at) berkeley.edu.
46 */
47
48 #ifndef lint
49 static char copyright[] =
50 "@(#) 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 static char rcsid[] = "$NetBSD: main.c,v 1.3 1995/03/21 15:04:24 cgd Exp $";
59 #endif
60 #endif /* not lint */
61
62 #include "include.h"
63 #include "pathnames.h"
64
65 main(ac, av)
66 char *av[];
67 {
68 int seed;
69 int f_usage = 0, f_list = 0, f_showscore = 0;
70 int f_printpath = 0;
71 char *file = NULL;
72 char *name, *ptr;
73 #ifdef BSD
74 struct itimerval itv;
75 #endif
76 extern char *default_game(), *okay_game();
77 extern void log_score(), quit(), update();
78
79 start_time = seed = time(0);
80
81 name = *av++;
82 while (*av) {
83 #ifndef SAVEDASH
84 if (**av == '-')
85 *++*av;
86 else
87 break;
88 #endif
89 ptr = *av++;
90 while (*ptr) {
91 switch (*ptr) {
92 case '?':
93 case 'u':
94 f_usage++;
95 break;
96 case 'l':
97 f_list++;
98 break;
99 case 's':
100 case 't':
101 f_showscore++;
102 break;
103 case 'p':
104 f_printpath++;
105 break;
106 case 'r':
107 seed = atoi(*av);
108 av++;
109 break;
110 case 'f':
111 case 'g':
112 file = *av;
113 av++;
114 break;
115 default:
116 fprintf(stderr, "Unknown option '%c'\n", *ptr,
117 name);
118 f_usage++;
119 break;
120 }
121 ptr++;
122 }
123 }
124 srandom(seed);
125
126 if (f_usage)
127 fprintf(stderr,
128 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
129 name);
130 if (f_showscore)
131 log_score(1);
132 if (f_list)
133 list_games();
134 if (f_printpath) {
135 char buf[100];
136
137 strcpy(buf, _PATH_GAMES);
138 buf[strlen(buf) - 1] = '\0';
139 puts(buf);
140 }
141
142 if (f_usage || f_showscore || f_list || f_printpath)
143 exit(0);
144
145 if (file == NULL)
146 file = default_game();
147 else
148 file = okay_game(file);
149
150 if (file == NULL || read_file(file) < 0)
151 exit(1);
152
153 init_gr();
154 setup_screen(sp);
155
156 addplane();
157
158 signal(SIGINT, quit);
159 signal(SIGQUIT, quit);
160 #ifdef BSD
161 signal(SIGTSTP, SIG_IGN);
162 signal(SIGSTOP, SIG_IGN);
163 #endif
164 signal(SIGHUP, log_score);
165 signal(SIGTERM, log_score);
166
167 #ifdef BSD
168 ioctl(fileno(stdin), TIOCGETP, &tty_start);
169 bcopy(&tty_start, &tty_new, sizeof(tty_new));
170 tty_new.sg_flags |= CBREAK;
171 tty_new.sg_flags &= ~ECHO;
172 ioctl(fileno(stdin), TIOCSETP, &tty_new);
173 #endif
174
175 #ifdef SYSV
176 ioctl(fileno(stdin), TCGETA, &tty_start);
177 bcopy(&tty_start, &tty_new, sizeof(tty_new));
178 tty_new.c_lflag &= ~ICANON;
179 tty_new.c_lflag &= ~ECHO;
180 tty_new.c_cc[VMIN] = 1;
181 tty_new.c_cc[VTIME] = 0;
182 ioctl(fileno(stdin), TCSETAW, &tty_new);
183 #endif
184
185 signal(SIGALRM, update);
186
187 #ifdef BSD
188 itv.it_value.tv_sec = 0;
189 itv.it_value.tv_usec = 1;
190 itv.it_interval.tv_sec = sp->update_secs;
191 itv.it_interval.tv_usec = 0;
192 setitimer(ITIMER_REAL, &itv, NULL);
193 #endif
194 #ifdef SYSV
195 alarm(sp->update_secs);
196 #endif
197
198 for (;;) {
199 if (getcommand() != 1)
200 planewin();
201 else {
202 #ifdef BSD
203 itv.it_value.tv_sec = 0;
204 itv.it_value.tv_usec = 0;
205 setitimer(ITIMER_REAL, &itv, NULL);
206 #endif
207 #ifdef SYSV
208 alarm(0);
209 #endif
210
211 update();
212
213 #ifdef BSD
214 itv.it_value.tv_sec = sp->update_secs;
215 itv.it_value.tv_usec = 0;
216 itv.it_interval.tv_sec = sp->update_secs;
217 itv.it_interval.tv_usec = 0;
218 setitimer(ITIMER_REAL, &itv, NULL);
219 #endif
220 #ifdef SYSV
221 alarm(sp->update_secs);
222 #endif
223 }
224 }
225 }
226
227 read_file(s)
228 char *s;
229 {
230 extern FILE *yyin;
231 int retval;
232
233 file = s;
234 yyin = fopen(s, "r");
235 if (yyin == NULL) {
236 perror(s);
237 return (-1);
238 }
239 retval = yyparse();
240 fclose(yyin);
241
242 if (retval != 0)
243 return (-1);
244 else
245 return (0);
246 }
247
248 char *
249 default_game()
250 {
251 FILE *fp;
252 static char file[256];
253 char line[256], games[256];
254
255 strcpy(games, _PATH_GAMES);
256 strcat(games, GAMES);
257
258 if ((fp = fopen(games, "r")) == NULL) {
259 perror(games);
260 return (NULL);
261 }
262 if (fgets(line, sizeof(line), fp) == NULL) {
263 fprintf(stderr, "%s: no default game available\n", games);
264 return (NULL);
265 }
266 fclose(fp);
267 line[strlen(line) - 1] = '\0';
268 strcpy(file, _PATH_GAMES);
269 strcat(file, line);
270 return (file);
271 }
272
273 char *
274 okay_game(s)
275 char *s;
276 {
277 FILE *fp;
278 static char file[256];
279 char *ret = NULL, line[256], games[256];
280
281 strcpy(games, _PATH_GAMES);
282 strcat(games, GAMES);
283
284 if ((fp = fopen(games, "r")) == NULL) {
285 perror(games);
286 return (NULL);
287 }
288 while (fgets(line, sizeof(line), fp) != NULL) {
289 line[strlen(line) - 1] = '\0';
290 if (strcmp(s, line) == 0) {
291 strcpy(file, _PATH_GAMES);
292 strcat(file, line);
293 ret = file;
294 break;
295 }
296 }
297 fclose(fp);
298 if (ret == NULL) {
299 test_mode = 1;
300 ret = s;
301 fprintf(stderr, "%s: %s: game not found\n", games, s);
302 fprintf(stderr, "Your score will not be logged.\n");
303 sleep(2); /* give the guy time to read it */
304 }
305 return (ret);
306 }
307
308 list_games()
309 {
310 FILE *fp;
311 char line[256], games[256];
312 int num_games = 0;
313
314 strcpy(games, _PATH_GAMES);
315 strcat(games, GAMES);
316
317 if ((fp = fopen(games, "r")) == NULL) {
318 perror(games);
319 return (-1);
320 }
321 puts("available games:");
322 while (fgets(line, sizeof(line), fp) != NULL) {
323 printf(" %s", line);
324 num_games++;
325 }
326 fclose(fp);
327 if (num_games == 0) {
328 fprintf(stderr, "%s: no games available\n", games);
329 return (-1);
330 }
331 return (0);
332 }
333