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