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