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