BSD 4_3_Net_2 development
[unix-history] / .ref-8c8a5b54e79564c14fc7a2823a21a8f048449bcf / usr / src / games / atc / main.c
CommitLineData
e04f5cf8
KB
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 * %sccs.include.redist.c%
9 */
10
eb107d76
KB
11/*
12 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
13 *
14 * Copy permission is hereby granted provided that this notice is
15 * retained on all partial or complete copies.
16 *
17 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
18 */
19
e04f5cf8
KB
20#ifndef lint
21char copyright[] =
22"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
23 All rights reserved.\n";
24#endif /* not lint */
25
26#ifndef lint
c88cb05c 27static char sccsid[] = "@(#)main.c 5.4 (Berkeley) %G%";
e04f5cf8
KB
28#endif /* not lint */
29
eb107d76 30#include "include.h"
3e3e3488 31#include "pathnames.h"
eb107d76
KB
32
33main(ac, av)
34 char *av[];
35{
36 int seed;
37 int f_usage = 0, f_list = 0, f_showscore = 0;
38 int f_printpath = 0;
39 char *file = NULL;
40 char *name, *ptr;
41#ifdef BSD
42 struct itimerval itv;
43#endif
eb107d76 44 extern char *default_game(), *okay_game();
c88cb05c 45 extern void log_score(), quit(), update();
eb107d76
KB
46
47 start_time = seed = time(0);
48
49 name = *av++;
50 while (*av) {
51#ifndef SAVEDASH
52 if (**av == '-')
53 *++*av;
54 else
55 break;
56#endif
57 ptr = *av++;
58 while (*ptr) {
59 switch (*ptr) {
60 case '?':
61 case 'u':
62 f_usage++;
63 break;
64 case 'l':
65 f_list++;
66 break;
67 case 's':
68 case 't':
69 f_showscore++;
70 break;
71 case 'p':
72 f_printpath++;
73 break;
74 case 'r':
75 seed = atoi(*av);
76 av++;
77 break;
78 case 'f':
79 case 'g':
80 file = *av;
81 av++;
82 break;
83 default:
84 fprintf(stderr, "Unknown option '%c'\n", *ptr,
85 name);
86 f_usage++;
87 break;
88 }
89 ptr++;
90 }
91 }
92 srandom(seed);
93
94 if (f_usage)
95 fprintf(stderr,
96 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
97 name);
98 if (f_showscore)
99 log_score(1);
100 if (f_list)
101 list_games();
102 if (f_printpath) {
103 char buf[100];
104
3e3e3488 105 strcpy(buf, _PATH_GAMES);
eb107d76
KB
106 buf[strlen(buf) - 1] = '\0';
107 puts(buf);
108 }
109
110 if (f_usage || f_showscore || f_list || f_printpath)
111 exit(0);
112
113 if (file == NULL)
114 file = default_game();
115 else
116 file = okay_game(file);
117
118 if (file == NULL || read_file(file) < 0)
119 exit(1);
120
121 init_gr();
122 setup_screen(sp);
123
124 addplane();
125
126 signal(SIGINT, quit);
127 signal(SIGQUIT, quit);
128#ifdef BSD
129 signal(SIGTSTP, SIG_IGN);
130 signal(SIGSTOP, SIG_IGN);
131#endif
132 signal(SIGHUP, log_score);
133 signal(SIGTERM, log_score);
134
135#ifdef BSD
136 ioctl(fileno(stdin), TIOCGETP, &tty_start);
137 bcopy(&tty_start, &tty_new, sizeof(tty_new));
138 tty_new.sg_flags |= CBREAK;
139 tty_new.sg_flags &= ~ECHO;
140 ioctl(fileno(stdin), TIOCSETP, &tty_new);
141#endif
142
143#ifdef SYSV
144 ioctl(fileno(stdin), TCGETA, &tty_start);
145 bcopy(&tty_start, &tty_new, sizeof(tty_new));
146 tty_new.c_lflag &= ~ICANON;
147 tty_new.c_lflag &= ~ECHO;
148 tty_new.c_cc[VMIN] = 1;
149 tty_new.c_cc[VTIME] = 0;
150 ioctl(fileno(stdin), TCSETAW, &tty_new);
151#endif
152
153 signal(SIGALRM, update);
154
155#ifdef BSD
156 itv.it_value.tv_sec = 0;
157 itv.it_value.tv_usec = 1;
158 itv.it_interval.tv_sec = sp->update_secs;
159 itv.it_interval.tv_usec = 0;
160 setitimer(ITIMER_REAL, &itv, NULL);
161#endif
162#ifdef SYSV
163 alarm(sp->update_secs);
164#endif
165
166 for (;;) {
167 if (getcommand() != 1)
168 planewin();
169 else {
170#ifdef BSD
171 itv.it_value.tv_sec = 0;
172 itv.it_value.tv_usec = 0;
173 setitimer(ITIMER_REAL, &itv, NULL);
174#endif
175#ifdef SYSV
176 alarm(0);
177#endif
178
179 update();
180
181#ifdef BSD
182 itv.it_value.tv_sec = sp->update_secs;
183 itv.it_value.tv_usec = 0;
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 }
193}
194
195read_file(s)
196 char *s;
197{
198 extern FILE *yyin;
199 int retval;
200
201 file = s;
202 yyin = fopen(s, "r");
203 if (yyin == NULL) {
204 perror(s);
205 return (-1);
206 }
207 retval = yyparse();
208 fclose(yyin);
209
210 if (retval != 0)
211 return (-1);
212 else
213 return (0);
214}
215
216char *
217default_game()
218{
219 FILE *fp;
220 static char file[256];
221 char line[256], games[256];
222
3e3e3488 223 strcpy(games, _PATH_GAMES);
eb107d76
KB
224 strcat(games, GAMES);
225
226 if ((fp = fopen(games, "r")) == NULL) {
227 perror(games);
228 return (NULL);
229 }
230 if (fgets(line, sizeof(line), fp) == NULL) {
231 fprintf(stderr, "%s: no default game available\n", games);
232 return (NULL);
233 }
234 fclose(fp);
235 line[strlen(line) - 1] = '\0';
3e3e3488 236 strcpy(file, _PATH_GAMES);
eb107d76
KB
237 strcat(file, line);
238 return (file);
239}
240
241char *
242okay_game(s)
243 char *s;
244{
245 FILE *fp;
246 static char file[256];
247 char *ret = NULL, line[256], games[256];
248
3e3e3488 249 strcpy(games, _PATH_GAMES);
eb107d76
KB
250 strcat(games, GAMES);
251
252 if ((fp = fopen(games, "r")) == NULL) {
253 perror(games);
254 return (NULL);
255 }
256 while (fgets(line, sizeof(line), fp) != NULL) {
257 line[strlen(line) - 1] = '\0';
258 if (strcmp(s, line) == 0) {
3e3e3488 259 strcpy(file, _PATH_GAMES);
eb107d76
KB
260 strcat(file, line);
261 ret = file;
262 break;
263 }
264 }
265 fclose(fp);
266 if (ret == NULL) {
267 test_mode = 1;
268 ret = s;
269 fprintf(stderr, "%s: %s: game not found\n", games, s);
270 fprintf(stderr, "Your score will not be logged.\n");
271 sleep(2); /* give the guy time to read it */
272 }
273 return (ret);
274}
275
276list_games()
277{
278 FILE *fp;
279 char line[256], games[256];
280 int num_games = 0;
281
3e3e3488 282 strcpy(games, _PATH_GAMES);
eb107d76
KB
283 strcat(games, GAMES);
284
285 if ((fp = fopen(games, "r")) == NULL) {
286 perror(games);
287 return (-1);
288 }
289 puts("available games:");
290 while (fgets(line, sizeof(line), fp) != NULL) {
291 printf(" %s", line);
292 num_games++;
293 }
294 fclose(fp);
295 if (num_games == 0) {
296 fprintf(stderr, "%s: no games available\n", games);
297 return (-1);
298 }
299 return (0);
300}