2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)main.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/atc/main.c,v 1.9 1999/11/30 03:48:21 billf Exp $
38 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
40 * Copy permission is hereby granted provided that this notice is
41 * retained on all partial or complete copies.
43 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
48 #include "pathnames.h"
51 extern int yyparse(void);
52 static int read_file(const char *);
53 static const char *default_game(void);
54 static const char *okay_game(const char *);
55 static int list_games(void);
58 main(int argc __unused, char *argv[])
61 int f_usage = 0, f_list = 0, f_showscore = 0;
63 const char *file = NULL;
67 /* Open the score file then revoke setgid privileges */
69 setregid(getgid(), getgid());
109 fprintf(stderr, "Unknown option '%c'\n", *ptr);
121 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
130 strcpy(buf, _PATH_GAMES);
131 buf[strlen(buf) - 1] = '\0';
135 if (f_usage || f_showscore || f_list || f_printpath)
139 file = default_game();
141 file = okay_game(file);
143 if (file == NULL || read_file(file) < 0)
151 signal(SIGINT, quit);
152 signal(SIGQUIT, quit);
153 signal(SIGTSTP, SIG_IGN);
154 signal(SIGSTOP, SIG_IGN);
155 signal(SIGHUP, log_score_quit);
156 signal(SIGTERM, log_score_quit);
158 tcgetattr(fileno(stdin), &tty_start);
159 bcopy(&tty_start, &tty_new, sizeof(tty_new));
160 tty_new.c_lflag &= ~(ICANON|ECHO);
161 tty_new.c_cc[VMIN] = 1;
162 tty_new.c_cc[VTIME] = 0;
163 tcsetattr(fileno(stdin), TCSANOW, &tty_new);
164 signal(SIGALRM, update);
166 itv.it_value.tv_sec = 0;
167 itv.it_value.tv_usec = 1;
168 itv.it_interval.tv_sec = sp->update_secs;
169 itv.it_interval.tv_usec = 0;
170 setitimer(ITIMER_REAL, &itv, NULL);
173 if (getcommand() != 1)
176 itv.it_value.tv_sec = 0;
177 itv.it_value.tv_usec = 0;
178 setitimer(ITIMER_REAL, &itv, NULL);
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);
192 read_file(const char *s)
197 yyin = fopen(s, "r");
215 static char file[256];
216 char line[256], games[256];
218 strcpy(games, _PATH_GAMES);
219 strcat(games, GAMES);
221 if ((fp = fopen(games, "r")) == NULL) {
225 if (fgets(line, sizeof(line), fp) == NULL) {
226 fprintf(stderr, "%s: no default game available\n", games);
230 line[strlen(line) - 1] = '\0';
231 strcpy(file, _PATH_GAMES);
237 okay_game(const char *s)
240 static char file[256];
241 const char *ret = NULL;
242 char line[256], games[256];
244 strcpy(games, _PATH_GAMES);
245 strcat(games, GAMES);
247 if ((fp = fopen(games, "r")) == NULL) {
251 while (fgets(line, sizeof(line), fp) != NULL) {
252 line[strlen(line) - 1] = '\0';
253 if (strcmp(s, line) == 0) {
254 strcpy(file, _PATH_GAMES);
264 fprintf(stderr, "%s: %s: game not found\n", games, s);
265 fprintf(stderr, "Your score will not be logged.\n");
266 sleep(2); /* give the guy time to read it */
275 char line[256], games[256];
278 strcpy(games, _PATH_GAMES);
279 strcat(games, GAMES);
281 if ((fp = fopen(games, "r")) == NULL) {
285 puts("available games:");
286 while (fgets(line, sizeof(line), fp) != NULL) {
291 if (num_games == 0) {
292 fprintf(stderr, "%s: no games available\n", games);