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