|
|
1.1 ! root 1: /*- ! 2: * Copyright (c) 1990 The Regents of the University of California. ! 3: * 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 are permitted ! 9: * provided that: (1) source distributions retain this entire copyright ! 10: * notice and comment, and (2) distributions including binaries display ! 11: * the following acknowledgement: ``This product includes software ! 12: * developed by the University of California, Berkeley and its contributors'' ! 13: * in the documentation or other materials provided with the distribution ! 14: * and in all advertising materials mentioning features or use of this ! 15: * software. Neither the name of the University nor the names of its ! 16: * contributors may be used to endorse or promote products derived ! 17: * from this software without specific prior written permission. ! 18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 19: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 20: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 21: */ ! 22: ! 23: /* ! 24: * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved. ! 25: * ! 26: * Copy permission is hereby granted provided that this notice is ! 27: * retained on all partial or complete copies. ! 28: * ! 29: * For more info on this and all of my stuff, mail [email protected]. ! 30: */ ! 31: ! 32: #ifndef lint ! 33: char copyright[] = ! 34: "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ ! 35: All rights reserved.\n"; ! 36: #endif /* not lint */ ! 37: ! 38: #ifndef lint ! 39: static char sccsid[] = "@(#)main.c 5.3 (Berkeley) 4/30/90"; ! 40: #endif /* not lint */ ! 41: ! 42: #include "include.h" ! 43: #include "pathnames.h" ! 44: ! 45: main(ac, av) ! 46: char *av[]; ! 47: { ! 48: int seed; ! 49: int f_usage = 0, f_list = 0, f_showscore = 0; ! 50: int f_printpath = 0; ! 51: char *file = NULL; ! 52: char *name, *ptr; ! 53: #ifdef BSD ! 54: struct itimerval itv; ! 55: #endif ! 56: extern int update(), quit(), log_score(); ! 57: extern char *default_game(), *okay_game(); ! 58: ! 59: start_time = seed = time(0); ! 60: ! 61: name = *av++; ! 62: while (*av) { ! 63: #ifndef SAVEDASH ! 64: if (**av == '-') ! 65: *++*av; ! 66: else ! 67: break; ! 68: #endif ! 69: ptr = *av++; ! 70: while (*ptr) { ! 71: switch (*ptr) { ! 72: case '?': ! 73: case 'u': ! 74: f_usage++; ! 75: break; ! 76: case 'l': ! 77: f_list++; ! 78: break; ! 79: case 's': ! 80: case 't': ! 81: f_showscore++; ! 82: break; ! 83: case 'p': ! 84: f_printpath++; ! 85: break; ! 86: case 'r': ! 87: seed = atoi(*av); ! 88: av++; ! 89: break; ! 90: case 'f': ! 91: case 'g': ! 92: file = *av; ! 93: av++; ! 94: break; ! 95: default: ! 96: fprintf(stderr, "Unknown option '%c'\n", *ptr, ! 97: name); ! 98: f_usage++; ! 99: break; ! 100: } ! 101: ptr++; ! 102: } ! 103: } ! 104: srandom(seed); ! 105: ! 106: if (f_usage) ! 107: fprintf(stderr, ! 108: "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n", ! 109: name); ! 110: if (f_showscore) ! 111: log_score(1); ! 112: if (f_list) ! 113: list_games(); ! 114: if (f_printpath) { ! 115: char buf[100]; ! 116: ! 117: strcpy(buf, _PATH_GAMES); ! 118: buf[strlen(buf) - 1] = '\0'; ! 119: puts(buf); ! 120: } ! 121: ! 122: if (f_usage || f_showscore || f_list || f_printpath) ! 123: exit(0); ! 124: ! 125: if (file == NULL) ! 126: file = default_game(); ! 127: else ! 128: file = okay_game(file); ! 129: ! 130: if (file == NULL || read_file(file) < 0) ! 131: exit(1); ! 132: ! 133: init_gr(); ! 134: setup_screen(sp); ! 135: ! 136: addplane(); ! 137: ! 138: signal(SIGINT, quit); ! 139: signal(SIGQUIT, quit); ! 140: #ifdef BSD ! 141: signal(SIGTSTP, SIG_IGN); ! 142: signal(SIGSTOP, SIG_IGN); ! 143: #endif ! 144: signal(SIGHUP, log_score); ! 145: signal(SIGTERM, log_score); ! 146: ! 147: #ifdef BSD ! 148: ioctl(fileno(stdin), TIOCGETP, &tty_start); ! 149: bcopy(&tty_start, &tty_new, sizeof(tty_new)); ! 150: tty_new.sg_flags |= CBREAK; ! 151: tty_new.sg_flags &= ~ECHO; ! 152: ioctl(fileno(stdin), TIOCSETP, &tty_new); ! 153: #endif ! 154: ! 155: #ifdef SYSV ! 156: ioctl(fileno(stdin), TCGETA, &tty_start); ! 157: bcopy(&tty_start, &tty_new, sizeof(tty_new)); ! 158: tty_new.c_lflag &= ~ICANON; ! 159: tty_new.c_lflag &= ~ECHO; ! 160: tty_new.c_cc[VMIN] = 1; ! 161: tty_new.c_cc[VTIME] = 0; ! 162: ioctl(fileno(stdin), TCSETAW, &tty_new); ! 163: #endif ! 164: ! 165: signal(SIGALRM, update); ! 166: ! 167: #ifdef BSD ! 168: itv.it_value.tv_sec = 0; ! 169: itv.it_value.tv_usec = 1; ! 170: itv.it_interval.tv_sec = sp->update_secs; ! 171: itv.it_interval.tv_usec = 0; ! 172: setitimer(ITIMER_REAL, &itv, NULL); ! 173: #endif ! 174: #ifdef SYSV ! 175: alarm(sp->update_secs); ! 176: #endif ! 177: ! 178: for (;;) { ! 179: if (getcommand() != 1) ! 180: planewin(); ! 181: else { ! 182: #ifdef BSD ! 183: itv.it_value.tv_sec = 0; ! 184: itv.it_value.tv_usec = 0; ! 185: setitimer(ITIMER_REAL, &itv, NULL); ! 186: #endif ! 187: #ifdef SYSV ! 188: alarm(0); ! 189: #endif ! 190: ! 191: update(); ! 192: ! 193: #ifdef BSD ! 194: itv.it_value.tv_sec = sp->update_secs; ! 195: itv.it_value.tv_usec = 0; ! 196: itv.it_interval.tv_sec = sp->update_secs; ! 197: itv.it_interval.tv_usec = 0; ! 198: setitimer(ITIMER_REAL, &itv, NULL); ! 199: #endif ! 200: #ifdef SYSV ! 201: alarm(sp->update_secs); ! 202: #endif ! 203: } ! 204: } ! 205: } ! 206: ! 207: read_file(s) ! 208: char *s; ! 209: { ! 210: extern FILE *yyin; ! 211: int retval; ! 212: ! 213: file = s; ! 214: yyin = fopen(s, "r"); ! 215: if (yyin == NULL) { ! 216: perror(s); ! 217: return (-1); ! 218: } ! 219: retval = yyparse(); ! 220: fclose(yyin); ! 221: ! 222: if (retval != 0) ! 223: return (-1); ! 224: else ! 225: return (0); ! 226: } ! 227: ! 228: char * ! 229: default_game() ! 230: { ! 231: FILE *fp; ! 232: static char file[256]; ! 233: char line[256], games[256]; ! 234: ! 235: strcpy(games, _PATH_GAMES); ! 236: strcat(games, GAMES); ! 237: ! 238: if ((fp = fopen(games, "r")) == NULL) { ! 239: perror(games); ! 240: return (NULL); ! 241: } ! 242: if (fgets(line, sizeof(line), fp) == NULL) { ! 243: fprintf(stderr, "%s: no default game available\n", games); ! 244: return (NULL); ! 245: } ! 246: fclose(fp); ! 247: line[strlen(line) - 1] = '\0'; ! 248: strcpy(file, _PATH_GAMES); ! 249: strcat(file, line); ! 250: return (file); ! 251: } ! 252: ! 253: char * ! 254: okay_game(s) ! 255: char *s; ! 256: { ! 257: FILE *fp; ! 258: static char file[256]; ! 259: char *ret = NULL, line[256], games[256]; ! 260: ! 261: strcpy(games, _PATH_GAMES); ! 262: strcat(games, GAMES); ! 263: ! 264: if ((fp = fopen(games, "r")) == NULL) { ! 265: perror(games); ! 266: return (NULL); ! 267: } ! 268: while (fgets(line, sizeof(line), fp) != NULL) { ! 269: line[strlen(line) - 1] = '\0'; ! 270: if (strcmp(s, line) == 0) { ! 271: strcpy(file, _PATH_GAMES); ! 272: strcat(file, line); ! 273: ret = file; ! 274: break; ! 275: } ! 276: } ! 277: fclose(fp); ! 278: if (ret == NULL) { ! 279: test_mode = 1; ! 280: ret = s; ! 281: fprintf(stderr, "%s: %s: game not found\n", games, s); ! 282: fprintf(stderr, "Your score will not be logged.\n"); ! 283: sleep(2); /* give the guy time to read it */ ! 284: } ! 285: return (ret); ! 286: } ! 287: ! 288: list_games() ! 289: { ! 290: FILE *fp; ! 291: char line[256], games[256]; ! 292: int num_games = 0; ! 293: ! 294: strcpy(games, _PATH_GAMES); ! 295: strcat(games, GAMES); ! 296: ! 297: if ((fp = fopen(games, "r")) == NULL) { ! 298: perror(games); ! 299: return (-1); ! 300: } ! 301: puts("available games:"); ! 302: while (fgets(line, sizeof(line), fp) != NULL) { ! 303: printf(" %s", line); ! 304: num_games++; ! 305: } ! 306: fclose(fp); ! 307: if (num_games == 0) { ! 308: fprintf(stderr, "%s: no games available\n", games); ! 309: return (-1); ! 310: } ! 311: return (0); ! 312: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.