1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. 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 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved. 37 * @(#)main.c 8.1 (Berkeley) 5/31/93 38 * $FreeBSD: src/games/atc/main.c,v 1.9 1999/11/30 03:48:21 billf Exp $ 39 * $DragonFly: src/games/atc/main.c,v 1.2 2003/06/17 04:25:22 dillon Exp $ 40 */ 41 42 /* 43 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved. 44 * 45 * Copy permission is hereby granted provided that this notice is 46 * retained on all partial or complete copies. 47 * 48 * For more info on this and all of my stuff, mail edjames@berkeley.edu. 49 */ 50 51 #include <string.h> 52 #include "include.h" 53 #include "pathnames.h" 54 55 main(ac, av) 56 int ac; 57 char *av[]; 58 { 59 int seed = 0; 60 int f_usage = 0, f_list = 0, f_showscore = 0; 61 int f_printpath = 0; 62 const char *file = NULL; 63 char *name, *ptr; 64 #ifdef BSD 65 struct itimerval itv; 66 #endif 67 extern const char *default_game(), *okay_game(); 68 extern void log_score(), quit(), update(); 69 70 /* Open the score file then revoke setgid privileges */ 71 open_score_file(); 72 setregid(getgid(), getgid()); 73 74 start_time = 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 srandom(atoi(*av)); 103 seed = 1; 104 av++; 105 break; 106 case 'f': 107 case 'g': 108 file = *av; 109 av++; 110 break; 111 default: 112 fprintf(stderr, "Unknown option '%c'\n", *ptr); 113 f_usage++; 114 break; 115 } 116 ptr++; 117 } 118 } 119 if (!seed) 120 srandomdev(); 121 122 if (f_usage) 123 fprintf(stderr, 124 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n", 125 name); 126 if (f_showscore) 127 log_score(1); 128 if (f_list) 129 list_games(); 130 if (f_printpath) { 131 char buf[100]; 132 133 strcpy(buf, _PATH_GAMES); 134 buf[strlen(buf) - 1] = '\0'; 135 puts(buf); 136 } 137 138 if (f_usage || f_showscore || f_list || f_printpath) 139 exit(0); 140 141 if (file == NULL) 142 file = default_game(); 143 else 144 file = okay_game(file); 145 146 if (file == NULL || read_file(file) < 0) 147 exit(1); 148 149 init_gr(); 150 setup_screen(sp); 151 152 addplane(); 153 154 signal(SIGINT, quit); 155 signal(SIGQUIT, quit); 156 #ifdef BSD 157 signal(SIGTSTP, SIG_IGN); 158 signal(SIGSTOP, SIG_IGN); 159 #endif 160 signal(SIGHUP, log_score); 161 signal(SIGTERM, log_score); 162 163 #ifdef BSD 164 ioctl(fileno(stdin), TIOCGETP, &tty_start); 165 bcopy(&tty_start, &tty_new, sizeof(tty_new)); 166 tty_new.sg_flags |= CBREAK; 167 tty_new.sg_flags &= ~ECHO; 168 ioctl(fileno(stdin), TIOCSETP, &tty_new); 169 #endif 170 171 #ifdef SYSV 172 ioctl(fileno(stdin), TCGETA, &tty_start); 173 bcopy(&tty_start, &tty_new, sizeof(tty_new)); 174 tty_new.c_lflag &= ~ICANON; 175 tty_new.c_lflag &= ~ECHO; 176 tty_new.c_cc[VMIN] = 1; 177 tty_new.c_cc[VTIME] = 0; 178 ioctl(fileno(stdin), TCSETAW, &tty_new); 179 #endif 180 181 signal(SIGALRM, update); 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 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 setitimer(ITIMER_REAL, &itv, NULL); 202 #endif 203 #ifdef SYSV 204 alarm(0); 205 #endif 206 207 update(); 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 setitimer(ITIMER_REAL, &itv, NULL); 215 #endif 216 #ifdef SYSV 217 alarm(sp->update_secs); 218 #endif 219 } 220 } 221 } 222 223 read_file(s) 224 const char *s; 225 { 226 extern FILE *yyin; 227 int retval; 228 229 file = s; 230 yyin = fopen(s, "r"); 231 if (yyin == NULL) { 232 perror(s); 233 return (-1); 234 } 235 retval = yyparse(); 236 fclose(yyin); 237 238 if (retval != 0) 239 return (-1); 240 else 241 return (0); 242 } 243 244 const char * 245 default_game() 246 { 247 FILE *fp; 248 static char file[256]; 249 char line[256], games[256]; 250 251 strcpy(games, _PATH_GAMES); 252 strcat(games, GAMES); 253 254 if ((fp = fopen(games, "r")) == NULL) { 255 perror(games); 256 return (NULL); 257 } 258 if (fgets(line, sizeof(line), fp) == NULL) { 259 fprintf(stderr, "%s: no default game available\n", games); 260 return (NULL); 261 } 262 fclose(fp); 263 line[strlen(line) - 1] = '\0'; 264 strcpy(file, _PATH_GAMES); 265 strcat(file, line); 266 return (file); 267 } 268 269 const char * 270 okay_game(s) 271 char *s; 272 { 273 FILE *fp; 274 static char file[256]; 275 const char *ret = NULL; 276 char line[256], games[256]; 277 278 strcpy(games, _PATH_GAMES); 279 strcat(games, GAMES); 280 281 if ((fp = fopen(games, "r")) == NULL) { 282 perror(games); 283 return (NULL); 284 } 285 while (fgets(line, sizeof(line), fp) != NULL) { 286 line[strlen(line) - 1] = '\0'; 287 if (strcmp(s, line) == 0) { 288 strcpy(file, _PATH_GAMES); 289 strcat(file, line); 290 ret = file; 291 break; 292 } 293 } 294 fclose(fp); 295 if (ret == NULL) { 296 test_mode = 1; 297 ret = s; 298 fprintf(stderr, "%s: %s: game not found\n", games, s); 299 fprintf(stderr, "Your score will not be logged.\n"); 300 sleep(2); /* give the guy time to read it */ 301 } 302 return (ret); 303 } 304 305 list_games() 306 { 307 FILE *fp; 308 char line[256], games[256]; 309 int num_games = 0; 310 311 strcpy(games, _PATH_GAMES); 312 strcat(games, GAMES); 313 314 if ((fp = fopen(games, "r")) == NULL) { 315 perror(games); 316 return (-1); 317 } 318 puts("available games:"); 319 while (fgets(line, sizeof(line), fp) != NULL) { 320 printf(" %s", line); 321 num_games++; 322 } 323 fclose(fp); 324 if (num_games == 0) { 325 fprintf(stderr, "%s: no games available\n", games); 326 return (-1); 327 } 328 return (0); 329 } 330