1*36b47bd3Slukem /* $NetBSD: main.c,v 1.6 1997/10/11 02:01:05 lukem Exp $ */ 2101657d1Scgd 361f28255Scgd /*- 4101657d1Scgd * Copyright (c) 1990, 1993 5101657d1Scgd * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * This code is derived from software contributed to Berkeley by 861f28255Scgd * Ed James. 961f28255Scgd * 1061f28255Scgd * Redistribution and use in source and binary forms, with or without 1161f28255Scgd * modification, are permitted provided that the following conditions 1261f28255Scgd * are met: 1361f28255Scgd * 1. Redistributions of source code must retain the above copyright 1461f28255Scgd * notice, this list of conditions and the following disclaimer. 1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1661f28255Scgd * notice, this list of conditions and the following disclaimer in the 1761f28255Scgd * documentation and/or other materials provided with the distribution. 1861f28255Scgd * 3. All advertising materials mentioning features or use of this software 1961f28255Scgd * must display the following acknowledgement: 2061f28255Scgd * This product includes software developed by the University of 2161f28255Scgd * California, Berkeley and its contributors. 2261f28255Scgd * 4. Neither the name of the University nor the names of its contributors 2361f28255Scgd * may be used to endorse or promote products derived from this software 2461f28255Scgd * without specific prior written permission. 2561f28255Scgd * 2661f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2761f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2861f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2961f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3061f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3161f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3261f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3361f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3461f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3561f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3661f28255Scgd * SUCH DAMAGE. 3761f28255Scgd */ 3861f28255Scgd 3961f28255Scgd /* 4061f28255Scgd * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved. 4161f28255Scgd * 4261f28255Scgd * Copy permission is hereby granted provided that this notice is 4361f28255Scgd * retained on all partial or complete copies. 4461f28255Scgd * 4561f28255Scgd * For more info on this and all of my stuff, mail edjames@berkeley.edu. 4661f28255Scgd */ 4761f28255Scgd 48ca57cf90Slukem #include <sys/cdefs.h> 4961f28255Scgd #ifndef lint 50ca57cf90Slukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\ 51ca57cf90Slukem The Regents of the University of California. All rights reserved.\n"); 5261f28255Scgd #endif /* not lint */ 5361f28255Scgd 5461f28255Scgd #ifndef lint 55101657d1Scgd #if 0 56101657d1Scgd static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; 57101657d1Scgd #else 58*36b47bd3Slukem __RCSID("$NetBSD: main.c,v 1.6 1997/10/11 02:01:05 lukem Exp $"); 59101657d1Scgd #endif 6061f28255Scgd #endif /* not lint */ 6161f28255Scgd 6261f28255Scgd #include "include.h" 6361f28255Scgd #include "pathnames.h" 6461f28255Scgd 65ca57cf90Slukem 66ca57cf90Slukem int 6761f28255Scgd main(ac, av) 68ca57cf90Slukem int ac; 6961f28255Scgd char *av[]; 7061f28255Scgd { 7161f28255Scgd int seed; 7261f28255Scgd int f_usage = 0, f_list = 0, f_showscore = 0; 7361f28255Scgd int f_printpath = 0; 7461f28255Scgd char *file = NULL; 7561f28255Scgd char *name, *ptr; 76ca2582cfSmycroft struct sigaction sa; 7761f28255Scgd #ifdef BSD 7861f28255Scgd struct itimerval itv; 7961f28255Scgd #endif 8061f28255Scgd 8161f28255Scgd start_time = seed = time(0); 8261f28255Scgd 8361f28255Scgd name = *av++; 8461f28255Scgd while (*av) { 8561f28255Scgd #ifndef SAVEDASH 8661f28255Scgd if (**av == '-') 87ca57cf90Slukem ++*av; 8861f28255Scgd else 8961f28255Scgd break; 9061f28255Scgd #endif 9161f28255Scgd ptr = *av++; 9261f28255Scgd while (*ptr) { 9361f28255Scgd switch (*ptr) { 9461f28255Scgd case '?': 9561f28255Scgd case 'u': 9661f28255Scgd f_usage++; 9761f28255Scgd break; 9861f28255Scgd case 'l': 9961f28255Scgd f_list++; 10061f28255Scgd break; 10161f28255Scgd case 's': 10261f28255Scgd case 't': 10361f28255Scgd f_showscore++; 10461f28255Scgd break; 10561f28255Scgd case 'p': 10661f28255Scgd f_printpath++; 10761f28255Scgd break; 10861f28255Scgd case 'r': 10961f28255Scgd seed = atoi(*av); 11061f28255Scgd av++; 11161f28255Scgd break; 11261f28255Scgd case 'f': 11361f28255Scgd case 'g': 11461f28255Scgd file = *av; 11561f28255Scgd av++; 11661f28255Scgd break; 11761f28255Scgd default: 118ca57cf90Slukem warnx("unknown option '%c'\n", *ptr); 11961f28255Scgd f_usage++; 12061f28255Scgd break; 12161f28255Scgd } 12261f28255Scgd ptr++; 12361f28255Scgd } 12461f28255Scgd } 12561f28255Scgd srandom(seed); 12661f28255Scgd 12761f28255Scgd if (f_usage) 12861f28255Scgd fprintf(stderr, 12961f28255Scgd "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n", 13061f28255Scgd name); 13161f28255Scgd if (f_showscore) 13261f28255Scgd log_score(1); 13361f28255Scgd if (f_list) 13461f28255Scgd list_games(); 13561f28255Scgd if (f_printpath) { 13661f28255Scgd char buf[100]; 13761f28255Scgd 13861f28255Scgd strcpy(buf, _PATH_GAMES); 13961f28255Scgd buf[strlen(buf) - 1] = '\0'; 14061f28255Scgd puts(buf); 14161f28255Scgd } 14261f28255Scgd 14361f28255Scgd if (f_usage || f_showscore || f_list || f_printpath) 14461f28255Scgd exit(0); 14561f28255Scgd 14661f28255Scgd if (file == NULL) 14761f28255Scgd file = default_game(); 14861f28255Scgd else 14961f28255Scgd file = okay_game(file); 15061f28255Scgd 15161f28255Scgd if (file == NULL || read_file(file) < 0) 15261f28255Scgd exit(1); 15361f28255Scgd 15461f28255Scgd init_gr(); 15561f28255Scgd setup_screen(sp); 15661f28255Scgd 15761f28255Scgd addplane(); 15861f28255Scgd 15961f28255Scgd signal(SIGINT, quit); 16061f28255Scgd signal(SIGQUIT, quit); 16161f28255Scgd #ifdef BSD 16261f28255Scgd signal(SIGTSTP, SIG_IGN); 16361f28255Scgd signal(SIGSTOP, SIG_IGN); 16461f28255Scgd #endif 165ca57cf90Slukem signal(SIGHUP, log_score_quit); 166ca57cf90Slukem signal(SIGTERM, log_score_quit); 16761f28255Scgd 168ca2582cfSmycroft tcgetattr(fileno(stdin), &tty_start); 169ca2582cfSmycroft tty_new = tty_start; 170ca2582cfSmycroft tty_new.c_lflag &= ~(ICANON|ECHO); 17161f28255Scgd tty_new.c_cc[VMIN] = 1; 17261f28255Scgd tty_new.c_cc[VTIME] = 0; 173ca2582cfSmycroft tcsetattr(fileno(stdin), TCSADRAIN, &tty_new); 17461f28255Scgd 175ca2582cfSmycroft sa.sa_handler = update; 176ca2582cfSmycroft sigemptyset(&sa.sa_mask); 177ca2582cfSmycroft sigaddset(&sa.sa_mask, SIGALRM); 178ca2582cfSmycroft sigaddset(&sa.sa_mask, SIGINT); 179ca2582cfSmycroft sa.sa_flags = 0; 180ca2582cfSmycroft sigaction(SIGALRM, &sa, (struct sigaction *)0); 18161f28255Scgd 18261f28255Scgd #ifdef BSD 18361f28255Scgd itv.it_value.tv_sec = 0; 18461f28255Scgd itv.it_value.tv_usec = 1; 18561f28255Scgd itv.it_interval.tv_sec = sp->update_secs; 18661f28255Scgd itv.it_interval.tv_usec = 0; 18761f28255Scgd setitimer(ITIMER_REAL, &itv, NULL); 18861f28255Scgd #endif 18961f28255Scgd #ifdef SYSV 19061f28255Scgd alarm(sp->update_secs); 19161f28255Scgd #endif 19261f28255Scgd 19361f28255Scgd for (;;) { 19461f28255Scgd if (getcommand() != 1) 19561f28255Scgd planewin(); 19661f28255Scgd else { 19761f28255Scgd #ifdef BSD 19861f28255Scgd itv.it_value.tv_sec = 0; 19961f28255Scgd itv.it_value.tv_usec = 0; 20061f28255Scgd setitimer(ITIMER_REAL, &itv, NULL); 20161f28255Scgd #endif 20261f28255Scgd #ifdef SYSV 20361f28255Scgd alarm(0); 20461f28255Scgd #endif 20561f28255Scgd 206ca57cf90Slukem update(0); 20761f28255Scgd 20861f28255Scgd #ifdef BSD 20961f28255Scgd itv.it_value.tv_sec = sp->update_secs; 21061f28255Scgd itv.it_value.tv_usec = 0; 21161f28255Scgd itv.it_interval.tv_sec = sp->update_secs; 21261f28255Scgd itv.it_interval.tv_usec = 0; 21361f28255Scgd setitimer(ITIMER_REAL, &itv, NULL); 21461f28255Scgd #endif 21561f28255Scgd #ifdef SYSV 21661f28255Scgd alarm(sp->update_secs); 21761f28255Scgd #endif 21861f28255Scgd } 21961f28255Scgd } 22061f28255Scgd } 22161f28255Scgd 222ca57cf90Slukem int 22361f28255Scgd read_file(s) 22461f28255Scgd char *s; 22561f28255Scgd { 22661f28255Scgd extern FILE *yyin; 22761f28255Scgd int retval; 22861f28255Scgd 22961f28255Scgd file = s; 23061f28255Scgd yyin = fopen(s, "r"); 23161f28255Scgd if (yyin == NULL) { 232*36b47bd3Slukem warn("fopen %s", s); 23361f28255Scgd return (-1); 23461f28255Scgd } 23561f28255Scgd retval = yyparse(); 23661f28255Scgd fclose(yyin); 23761f28255Scgd 23861f28255Scgd if (retval != 0) 23961f28255Scgd return (-1); 24061f28255Scgd else 24161f28255Scgd return (0); 24261f28255Scgd } 24361f28255Scgd 24461f28255Scgd char * 24561f28255Scgd default_game() 24661f28255Scgd { 24761f28255Scgd FILE *fp; 24861f28255Scgd static char file[256]; 24961f28255Scgd char line[256], games[256]; 25061f28255Scgd 25161f28255Scgd strcpy(games, _PATH_GAMES); 25261f28255Scgd strcat(games, GAMES); 25361f28255Scgd 25461f28255Scgd if ((fp = fopen(games, "r")) == NULL) { 255*36b47bd3Slukem warn("fopen %s", games); 25661f28255Scgd return (NULL); 25761f28255Scgd } 25861f28255Scgd if (fgets(line, sizeof(line), fp) == NULL) { 25961f28255Scgd fprintf(stderr, "%s: no default game available\n", games); 26061f28255Scgd return (NULL); 26161f28255Scgd } 26261f28255Scgd fclose(fp); 26361f28255Scgd line[strlen(line) - 1] = '\0'; 26461f28255Scgd strcpy(file, _PATH_GAMES); 26561f28255Scgd strcat(file, line); 26661f28255Scgd return (file); 26761f28255Scgd } 26861f28255Scgd 26961f28255Scgd char * 27061f28255Scgd okay_game(s) 27161f28255Scgd char *s; 27261f28255Scgd { 27361f28255Scgd FILE *fp; 27461f28255Scgd static char file[256]; 27561f28255Scgd char *ret = NULL, line[256], games[256]; 27661f28255Scgd 27761f28255Scgd strcpy(games, _PATH_GAMES); 27861f28255Scgd strcat(games, GAMES); 27961f28255Scgd 28061f28255Scgd if ((fp = fopen(games, "r")) == NULL) { 281*36b47bd3Slukem warn("fopen %s", games); 28261f28255Scgd return (NULL); 28361f28255Scgd } 28461f28255Scgd while (fgets(line, sizeof(line), fp) != NULL) { 28561f28255Scgd line[strlen(line) - 1] = '\0'; 28661f28255Scgd if (strcmp(s, line) == 0) { 28761f28255Scgd strcpy(file, _PATH_GAMES); 28861f28255Scgd strcat(file, line); 28961f28255Scgd ret = file; 29061f28255Scgd break; 29161f28255Scgd } 29261f28255Scgd } 29361f28255Scgd fclose(fp); 29461f28255Scgd if (ret == NULL) { 29561f28255Scgd test_mode = 1; 29661f28255Scgd ret = s; 29761f28255Scgd fprintf(stderr, "%s: %s: game not found\n", games, s); 29861f28255Scgd fprintf(stderr, "Your score will not be logged.\n"); 29961f28255Scgd sleep(2); /* give the guy time to read it */ 30061f28255Scgd } 30161f28255Scgd return (ret); 30261f28255Scgd } 30361f28255Scgd 304ca57cf90Slukem int 30561f28255Scgd list_games() 30661f28255Scgd { 30761f28255Scgd FILE *fp; 30861f28255Scgd char line[256], games[256]; 30961f28255Scgd int num_games = 0; 31061f28255Scgd 31161f28255Scgd strcpy(games, _PATH_GAMES); 31261f28255Scgd strcat(games, GAMES); 31361f28255Scgd 31461f28255Scgd if ((fp = fopen(games, "r")) == NULL) { 315*36b47bd3Slukem warn("fopen %s", games); 31661f28255Scgd return (-1); 31761f28255Scgd } 31861f28255Scgd puts("available games:"); 31961f28255Scgd while (fgets(line, sizeof(line), fp) != NULL) { 32061f28255Scgd printf(" %s", line); 32161f28255Scgd num_games++; 32261f28255Scgd } 32361f28255Scgd fclose(fp); 32461f28255Scgd if (num_games == 0) { 32561f28255Scgd fprintf(stderr, "%s: no games available\n", games); 32661f28255Scgd return (-1); 32761f28255Scgd } 32861f28255Scgd return (0); 32961f28255Scgd } 330