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.4 2006/10/08 17:11:30 pavalos 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 <stdlib.h> 52 #include "include.h" 53 #include "pathnames.h" 54 55 extern FILE *yyin; 56 static int read_file(const char *); 57 static const char *default_game(void); 58 static const char *okay_game(const char *); 59 static int list_games(void); 60 61 62 int 63 main(__unused int ac, char **av) 64 { 65 int seed = 0; 66 int f_usage = 0, f_list = 0, f_showscore = 0; 67 int f_printpath = 0; 68 const char *file = NULL; 69 char *p_name, *ptr; 70 struct itimerval itv; 71 72 /* Open the score file then revoke setgid privileges */ 73 open_score_file(); 74 setregid(getgid(), getgid()); 75 76 start_time = time(0); 77 78 p_name = *av++; 79 while (*av) { 80 #ifndef SAVEDASH 81 if (**av == '-') 82 ++*av; 83 else 84 break; 85 #endif 86 ptr = *av++; 87 while (*ptr) { 88 switch (*ptr) { 89 case '?': 90 case 'u': 91 f_usage++; 92 break; 93 case 'l': 94 f_list++; 95 break; 96 case 's': 97 case 't': 98 f_showscore++; 99 break; 100 case 'p': 101 f_printpath++; 102 break; 103 case 'r': 104 srandom(atoi(*av)); 105 seed = 1; 106 av++; 107 break; 108 case 'f': 109 case 'g': 110 file = *av; 111 av++; 112 break; 113 default: 114 fprintf(stderr, "Unknown option '%c'\n", *ptr); 115 f_usage++; 116 break; 117 } 118 ptr++; 119 } 120 } 121 if (!seed) 122 srandomdev(); 123 124 if (f_usage) 125 fprintf(stderr, 126 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n", 127 p_name); 128 if (f_showscore) 129 log_score(1); 130 if (f_list) 131 list_games(); 132 if (f_printpath) { 133 char buf[100]; 134 135 strcpy(buf, _PATH_GAMES); 136 buf[strlen(buf) - 1] = '\0'; 137 puts(buf); 138 } 139 140 if (f_usage || f_showscore || f_list || f_printpath) 141 exit(0); 142 143 if (file == NULL) 144 file = default_game(); 145 else 146 file = okay_game(file); 147 148 if (file == NULL || read_file(file) < 0) 149 exit(1); 150 151 init_gr(); 152 setup_screen(sp); 153 154 addplane(); 155 156 signal(SIGINT, (sig_t)quit); 157 signal(SIGQUIT, (sig_t)quit); 158 signal(SIGTSTP, SIG_IGN); 159 signal(SIGSTOP, SIG_IGN); 160 signal(SIGHUP, (sig_t)log_score); 161 signal(SIGTERM, (sig_t)log_score); 162 163 tcgetattr(fileno(stdin), &tty_start); 164 bcopy(&tty_start, &tty_new, sizeof(tty_new)); 165 tty_new.c_lflag &= ~ICANON; 166 tty_new.c_lflag &= ~ECHO; 167 tty_new.c_cc[VMIN] = 1; 168 tty_new.c_cc[VTIME] = 0; 169 tcsetattr(fileno(stdin), TCSANOW, &tty_new); 170 signal(SIGALRM, (sig_t)update); 171 172 itv.it_value.tv_sec = 0; 173 itv.it_value.tv_usec = 1; 174 itv.it_interval.tv_sec = sp->update_secs; 175 itv.it_interval.tv_usec = 0; 176 setitimer(ITIMER_REAL, &itv, NULL); 177 178 for (;;) { 179 if (getcommand() != 1) 180 planewin(); 181 else { 182 itv.it_value.tv_sec = 0; 183 itv.it_value.tv_usec = 0; 184 setitimer(ITIMER_REAL, &itv, NULL); 185 186 update(); 187 188 itv.it_value.tv_sec = sp->update_secs; 189 itv.it_value.tv_usec = 0; 190 itv.it_interval.tv_sec = sp->update_secs; 191 itv.it_interval.tv_usec = 0; 192 setitimer(ITIMER_REAL, &itv, NULL); 193 } 194 } 195 } 196 197 static int 198 read_file(const char *s) 199 { 200 int retval; 201 202 filename = s; 203 yyin = fopen(s, "r"); 204 if (yyin == NULL) { 205 perror(s); 206 return (-1); 207 } 208 retval = yyparse(); 209 fclose(yyin); 210 211 if (retval != 0) 212 return (-1); 213 else 214 return (0); 215 } 216 217 static const char * 218 default_game(void) 219 { 220 FILE *fp; 221 static char file[256]; 222 char line[256], games[256]; 223 224 strcpy(games, _PATH_GAMES); 225 strcat(games, GAMES); 226 227 if ((fp = fopen(games, "r")) == NULL) { 228 perror(games); 229 return (NULL); 230 } 231 if (fgets(line, sizeof(line), fp) == NULL) { 232 fprintf(stderr, "%s: no default game available\n", games); 233 return (NULL); 234 } 235 fclose(fp); 236 line[strlen(line) - 1] = '\0'; 237 strcpy(file, _PATH_GAMES); 238 strcat(file, line); 239 return (file); 240 } 241 242 static const char * 243 okay_game(const char *s) 244 { 245 FILE *fp; 246 static char file[256]; 247 const char *ret = NULL; 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 while (fgets(line, sizeof(line), fp) != NULL) { 258 line[strlen(line) - 1] = '\0'; 259 if (strcmp(s, line) == 0) { 260 strcpy(file, _PATH_GAMES); 261 strcat(file, line); 262 ret = file; 263 break; 264 } 265 } 266 fclose(fp); 267 if (ret == NULL) { 268 test_mode = 1; 269 ret = s; 270 fprintf(stderr, "%s: %s: game not found\n", games, s); 271 fprintf(stderr, "Your score will not be logged.\n"); 272 sleep(2); /* give the guy time to read it */ 273 } 274 return (ret); 275 } 276 277 static int 278 list_games(void) 279 { 280 FILE *fp; 281 char line[256], games[256]; 282 int num_games = 0; 283 284 strcpy(games, _PATH_GAMES); 285 strcat(games, GAMES); 286 287 if ((fp = fopen(games, "r")) == NULL) { 288 perror(games); 289 return (-1); 290 } 291 puts("available games:"); 292 while (fgets(line, sizeof(line), fp) != NULL) { 293 printf(" %s", line); 294 num_games++; 295 } 296 fclose(fp); 297 if (num_games == 0) { 298 fprintf(stderr, "%s: no games available\n", games); 299 return (-1); 300 } 301 return (0); 302 } 303