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