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. 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. 19 * 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 30 * SUCH DAMAGE. 31 * 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 $ 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 #include <stdlib.h> 47 #include "include.h" 48 #include "pathnames.h" 49 50 extern FILE *yyin; 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); 56 57 int 58 main(int argc __unused, char *argv[]) 59 { 60 int seed = 0; 61 int f_usage = 0, f_list = 0, f_showscore = 0; 62 int f_printpath = 0; 63 const char *file = NULL; 64 char *p_name, *ptr; 65 struct itimerval itv; 66 67 /* Open the score file then revoke setgid privileges */ 68 open_score_file(); 69 setregid(getgid(), getgid()); 70 71 start_time = time(0); 72 73 p_name = *argv++; 74 while (*argv) { 75 #ifndef SAVEDASH 76 if (**argv == '-') 77 ++*argv; 78 else 79 break; 80 #endif 81 ptr = *argv++; 82 while (*ptr) { 83 switch (*ptr) { 84 case '?': 85 case 'u': 86 f_usage++; 87 break; 88 case 'l': 89 f_list++; 90 break; 91 case 's': 92 case 't': 93 f_showscore++; 94 break; 95 case 'p': 96 f_printpath++; 97 break; 98 case 'r': 99 srandom(atoi(*argv)); 100 seed = 1; 101 argv++; 102 break; 103 case 'f': 104 case 'g': 105 file = *argv; 106 argv++; 107 break; 108 default: 109 fprintf(stderr, "Unknown option '%c'\n", *ptr); 110 f_usage++; 111 break; 112 } 113 ptr++; 114 } 115 } 116 if (!seed) 117 srandomdev(); 118 119 if (f_usage) 120 fprintf(stderr, 121 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n", 122 p_name); 123 if (f_showscore) 124 log_score(1); 125 if (f_list) 126 list_games(); 127 if (f_printpath) { 128 char buf[100]; 129 130 strcpy(buf, _PATH_GAMES); 131 buf[strlen(buf) - 1] = '\0'; 132 puts(buf); 133 } 134 135 if (f_usage || f_showscore || f_list || f_printpath) 136 exit(0); 137 138 if (file == NULL) 139 file = default_game(); 140 else 141 file = okay_game(file); 142 143 if (file == NULL || read_file(file) < 0) 144 exit(1); 145 146 init_gr(); 147 setup_screen(sp); 148 149 addplane(); 150 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); 157 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); 165 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); 171 172 for (;;) { 173 if (getcommand() != 1) 174 planewin(); 175 else { 176 itv.it_value.tv_sec = 0; 177 itv.it_value.tv_usec = 0; 178 setitimer(ITIMER_REAL, &itv, NULL); 179 180 update(0); 181 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 } 188 } 189 } 190 191 static int 192 read_file(const char *s) 193 { 194 int retval; 195 196 filename = s; 197 yyin = fopen(s, "r"); 198 if (yyin == NULL) { 199 perror(s); 200 return (-1); 201 } 202 retval = yyparse(); 203 fclose(yyin); 204 205 if (retval != 0) 206 return (-1); 207 else 208 return (0); 209 } 210 211 static const char * 212 default_game(void) 213 { 214 FILE *fp; 215 static char file[256]; 216 char line[256], games[256]; 217 218 strcpy(games, _PATH_GAMES); 219 strcat(games, GAMES); 220 221 if ((fp = fopen(games, "r")) == NULL) { 222 perror(games); 223 return (NULL); 224 } 225 if (fgets(line, sizeof(line), fp) == NULL) { 226 fprintf(stderr, "%s: no default game available\n", games); 227 return (NULL); 228 } 229 fclose(fp); 230 line[strlen(line) - 1] = '\0'; 231 strcpy(file, _PATH_GAMES); 232 strcat(file, line); 233 return (file); 234 } 235 236 static const char * 237 okay_game(const char *s) 238 { 239 FILE *fp; 240 static char file[256]; 241 const char *ret = NULL; 242 char line[256], games[256]; 243 244 strcpy(games, _PATH_GAMES); 245 strcat(games, GAMES); 246 247 if ((fp = fopen(games, "r")) == NULL) { 248 perror(games); 249 return (NULL); 250 } 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); 255 strcat(file, line); 256 ret = file; 257 break; 258 } 259 } 260 fclose(fp); 261 if (ret == NULL) { 262 test_mode = 1; 263 ret = s; 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 */ 267 } 268 return (ret); 269 } 270 271 static int 272 list_games(void) 273 { 274 FILE *fp; 275 char line[256], games[256]; 276 int num_games = 0; 277 278 strcpy(games, _PATH_GAMES); 279 strcat(games, GAMES); 280 281 if ((fp = fopen(games, "r")) == NULL) { 282 perror(games); 283 return (-1); 284 } 285 puts("available games:"); 286 while (fgets(line, sizeof(line), fp) != NULL) { 287 printf(" %s", line); 288 num_games++; 289 } 290 fclose(fp); 291 if (num_games == 0) { 292 fprintf(stderr, "%s: no games available\n", games); 293 return (-1); 294 } 295 return (0); 296 } 297