Home | History | Annotate | Line # | Download | only in atc
      1  1.25    rillig /*	$NetBSD: main.c,v 1.25 2021/05/02 12:50:43 rillig Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*-
      4   1.3       cgd  * Copyright (c) 1990, 1993
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Ed James.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.1       cgd /*
     36   1.1       cgd  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
     37   1.1       cgd  *
     38   1.1       cgd  * Copy permission is hereby granted provided that this notice is
     39   1.1       cgd  * retained on all partial or complete copies.
     40   1.1       cgd  *
     41   1.1       cgd  * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
     42   1.1       cgd  */
     43   1.1       cgd 
     44   1.5     lukem #include <sys/cdefs.h>
     45   1.1       cgd #ifndef lint
     46  1.19     lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
     47  1.19     lukem  The Regents of the University of California.  All rights reserved.");
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #ifndef lint
     51   1.3       cgd #if 0
     52   1.3       cgd static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
     53   1.3       cgd #else
     54  1.25    rillig __RCSID("$NetBSD: main.c,v 1.25 2021/05/02 12:50:43 rillig Exp $");
     55   1.3       cgd #endif
     56   1.1       cgd #endif /* not lint */
     57   1.1       cgd 
     58  1.23  dholland #include <stdio.h>
     59  1.23  dholland #include <stdlib.h>
     60  1.23  dholland #include <string.h>
     61  1.23  dholland #include <signal.h>
     62  1.23  dholland #include <termios.h>
     63  1.23  dholland #include <getopt.h>
     64  1.23  dholland #include <err.h>
     65  1.23  dholland 
     66   1.1       cgd #include "pathnames.h"
     67  1.23  dholland #include "def.h"
     68  1.23  dholland #include "struct.h"
     69  1.23  dholland #include "extern.h"
     70  1.23  dholland #include "tunable.h"
     71   1.1       cgd 
     72  1.12  christos extern FILE	*yyin;
     73   1.5     lukem 
     74  1.20  dholland static int read_file(const char *);
     75  1.20  dholland static const char *default_game(void);
     76  1.20  dholland static const char *okay_game(const char *);
     77  1.20  dholland static int list_games(void);
     78  1.24  dholland static void quit(int);
     79  1.20  dholland 
     80   1.5     lukem int
     81  1.14       jmc main(int argc, char *argv[])
     82   1.1       cgd {
     83  1.18  dholland 	unsigned long		seed;
     84   1.1       cgd 	int			f_usage = 0, f_list = 0, f_showscore = 0;
     85   1.1       cgd 	int			f_printpath = 0;
     86   1.8   hubertf 	const char		*file = NULL;
     87  1.10       mjl 	int			ch;
     88   1.4   mycroft 	struct sigaction	sa;
     89   1.1       cgd #ifdef BSD
     90   1.1       cgd 	struct itimerval	itv;
     91   1.1       cgd #endif
     92   1.9   hubertf 
     93   1.9   hubertf 	/* Open the score file then revoke setgid privileges */
     94   1.9   hubertf 	open_score_file();
     95  1.15    rpaulo 	(void)setgid(getgid());
     96   1.1       cgd 
     97  1.18  dholland 	start_time = time(NULL);
     98  1.18  dholland 	seed = start_time;
     99   1.1       cgd 
    100  1.17   jnemeth 	while ((ch = getopt(argc, argv, ":u?lstpg:f:r:")) != -1) {
    101  1.10       mjl 		switch (ch) {
    102  1.10       mjl 		case '?':
    103  1.10       mjl 		case 'u':
    104  1.25    rillig 		default:
    105  1.21  dholland 			f_usage = 1;
    106  1.10       mjl 			break;
    107  1.10       mjl 		case 'l':
    108  1.21  dholland 			f_list = 1;
    109  1.10       mjl 			break;
    110  1.10       mjl 		case 's':
    111  1.10       mjl 		case 't':
    112  1.21  dholland 			f_showscore = 1;
    113  1.10       mjl 			break;
    114  1.10       mjl 		case 'p':
    115  1.21  dholland 			f_printpath = 1;
    116  1.10       mjl 			break;
    117  1.10       mjl 		case 'r':
    118  1.10       mjl 			seed = atoi(optarg);
    119  1.10       mjl 			break;
    120  1.10       mjl 		case 'f':
    121  1.10       mjl 		case 'g':
    122  1.10       mjl 			file = optarg;
    123   1.1       cgd 			break;
    124   1.1       cgd 		}
    125   1.1       cgd 	}
    126  1.14       jmc 	if (optind < argc)
    127  1.21  dholland 		f_usage = 1;
    128  1.18  dholland 	srandom(seed);
    129   1.1       cgd 
    130   1.1       cgd 	if (f_usage)
    131  1.25    rillig 		(void)fprintf(stderr,
    132   1.1       cgd 		    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
    133  1.15    rpaulo 		    argv[0]);
    134   1.1       cgd 	if (f_showscore)
    135  1.15    rpaulo 		(void)log_score(1);
    136   1.1       cgd 	if (f_list)
    137  1.15    rpaulo 		(void)list_games();
    138   1.1       cgd 	if (f_printpath) {
    139   1.1       cgd 		char	buf[100];
    140   1.1       cgd 
    141  1.17   jnemeth 		(void)strlcpy(buf, _PATH_GAMES, 100);
    142  1.15    rpaulo 		(void)puts(buf);
    143   1.1       cgd 	}
    144  1.25    rillig 
    145   1.1       cgd 	if (f_usage || f_showscore || f_list || f_printpath)
    146   1.1       cgd 		exit(0);
    147   1.1       cgd 
    148   1.1       cgd 	if (file == NULL)
    149   1.1       cgd 		file = default_game();
    150   1.1       cgd 	else
    151   1.1       cgd 		file = okay_game(file);
    152   1.1       cgd 
    153   1.1       cgd 	if (file == NULL || read_file(file) < 0)
    154   1.1       cgd 		exit(1);
    155   1.1       cgd 
    156   1.1       cgd 	init_gr();
    157   1.1       cgd 	setup_screen(sp);
    158   1.1       cgd 
    159  1.22  dholland 	addplane();
    160   1.1       cgd 
    161  1.15    rpaulo 	(void)signal(SIGINT, quit);
    162  1.15    rpaulo 	(void)signal(SIGQUIT, quit);
    163   1.1       cgd #ifdef BSD
    164  1.15    rpaulo 	(void)signal(SIGTSTP, SIG_IGN);
    165   1.1       cgd #endif
    166  1.15    rpaulo 	(void)signal(SIGHUP, log_score_quit);
    167  1.15    rpaulo 	(void)signal(SIGTERM, log_score_quit);
    168   1.1       cgd 
    169  1.15    rpaulo 	(void)tcgetattr(fileno(stdin), &tty_start);
    170   1.4   mycroft 	tty_new = tty_start;
    171   1.4   mycroft 	tty_new.c_lflag &= ~(ICANON|ECHO);
    172   1.7   hubertf 	tty_new.c_iflag |= ICRNL;
    173   1.1       cgd 	tty_new.c_cc[VMIN] = 1;
    174   1.1       cgd 	tty_new.c_cc[VTIME] = 0;
    175  1.15    rpaulo 	(void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
    176   1.1       cgd 
    177   1.4   mycroft 	sa.sa_handler = update;
    178  1.15    rpaulo 	(void)sigemptyset(&sa.sa_mask);
    179  1.15    rpaulo 	(void)sigaddset(&sa.sa_mask, SIGALRM);
    180  1.15    rpaulo 	(void)sigaddset(&sa.sa_mask, SIGINT);
    181   1.4   mycroft 	sa.sa_flags = 0;
    182  1.15    rpaulo 	(void)sigaction(SIGALRM, &sa, (struct sigaction *)0);
    183   1.1       cgd 
    184   1.1       cgd #ifdef BSD
    185   1.1       cgd 	itv.it_value.tv_sec = 0;
    186   1.1       cgd 	itv.it_value.tv_usec = 1;
    187   1.1       cgd 	itv.it_interval.tv_sec = sp->update_secs;
    188   1.1       cgd 	itv.it_interval.tv_usec = 0;
    189  1.15    rpaulo 	(void)setitimer(ITIMER_REAL, &itv, NULL);
    190   1.1       cgd #endif
    191   1.1       cgd #ifdef SYSV
    192   1.1       cgd 	alarm(sp->update_secs);
    193   1.1       cgd #endif
    194   1.1       cgd 
    195   1.1       cgd 	for (;;) {
    196   1.1       cgd 		if (getcommand() != 1)
    197   1.1       cgd 			planewin();
    198   1.1       cgd 		else {
    199   1.1       cgd #ifdef BSD
    200   1.1       cgd 			itv.it_value.tv_sec = 0;
    201   1.1       cgd 			itv.it_value.tv_usec = 0;
    202  1.15    rpaulo 			(void)setitimer(ITIMER_REAL, &itv, NULL);
    203   1.1       cgd #endif
    204   1.1       cgd #ifdef SYSV
    205   1.1       cgd 			alarm(0);
    206   1.1       cgd #endif
    207   1.1       cgd 
    208   1.5     lukem 			update(0);
    209   1.1       cgd 
    210   1.1       cgd #ifdef BSD
    211   1.1       cgd 			itv.it_value.tv_sec = sp->update_secs;
    212   1.1       cgd 			itv.it_value.tv_usec = 0;
    213   1.1       cgd 			itv.it_interval.tv_sec = sp->update_secs;
    214   1.1       cgd 			itv.it_interval.tv_usec = 0;
    215  1.15    rpaulo 			(void)setitimer(ITIMER_REAL, &itv, NULL);
    216   1.1       cgd #endif
    217   1.1       cgd #ifdef SYSV
    218   1.1       cgd 			alarm(sp->update_secs);
    219   1.1       cgd #endif
    220   1.1       cgd 		}
    221   1.1       cgd 	}
    222   1.1       cgd }
    223   1.1       cgd 
    224  1.20  dholland static int
    225  1.14       jmc read_file(const char *s)
    226   1.1       cgd {
    227   1.1       cgd 	int		retval;
    228   1.1       cgd 
    229  1.14       jmc 	filename = s;
    230   1.1       cgd 	yyin = fopen(s, "r");
    231   1.1       cgd 	if (yyin == NULL) {
    232   1.6     lukem 		warn("fopen %s", s);
    233   1.1       cgd 		return (-1);
    234   1.1       cgd 	}
    235   1.1       cgd 	retval = yyparse();
    236  1.15    rpaulo 	(void)fclose(yyin);
    237   1.1       cgd 
    238   1.1       cgd 	if (retval != 0)
    239   1.1       cgd 		return (-1);
    240   1.1       cgd 	else
    241   1.1       cgd 		return (0);
    242   1.1       cgd }
    243   1.1       cgd 
    244  1.20  dholland static const char *
    245  1.14       jmc default_game(void)
    246   1.1       cgd {
    247   1.1       cgd 	FILE		*fp;
    248   1.1       cgd 	static char	file[256];
    249   1.1       cgd 	char		line[256], games[256];
    250   1.1       cgd 
    251  1.17   jnemeth 	(void)strlcpy(games, _PATH_GAMES, 256);
    252  1.17   jnemeth 	(void)strlcat(games, GAMES, 256);
    253   1.1       cgd 
    254   1.1       cgd 	if ((fp = fopen(games, "r")) == NULL) {
    255   1.6     lukem 		warn("fopen %s", games);
    256   1.1       cgd 		return (NULL);
    257   1.1       cgd 	}
    258   1.1       cgd 	if (fgets(line, sizeof(line), fp) == NULL) {
    259  1.15    rpaulo 		(void)fprintf(stderr, "%s: no default game available\n", games);
    260  1.16  christos 		fclose(fp);
    261   1.1       cgd 		return (NULL);
    262   1.1       cgd 	}
    263  1.15    rpaulo 	(void)fclose(fp);
    264   1.1       cgd 	line[strlen(line) - 1] = '\0';
    265  1.17   jnemeth 	(void)strlcpy(file, _PATH_GAMES, 256);
    266  1.17   jnemeth 	(void)strlcat(file, line, 256);
    267   1.1       cgd 	return (file);
    268   1.1       cgd }
    269   1.1       cgd 
    270  1.20  dholland static const char *
    271  1.14       jmc okay_game(const char *s)
    272   1.1       cgd {
    273   1.1       cgd 	FILE		*fp;
    274   1.1       cgd 	static char	file[256];
    275   1.8   hubertf 	const char	*ret = NULL;
    276   1.8   hubertf 	char		line[256], games[256];
    277   1.1       cgd 
    278  1.17   jnemeth 	(void)strlcpy(games, _PATH_GAMES, 256);
    279  1.17   jnemeth 	(void)strlcat(games, GAMES, 256);
    280   1.1       cgd 
    281   1.1       cgd 	if ((fp = fopen(games, "r")) == NULL) {
    282   1.6     lukem 		warn("fopen %s", games);
    283   1.1       cgd 		return (NULL);
    284   1.1       cgd 	}
    285   1.1       cgd 	while (fgets(line, sizeof(line), fp) != NULL) {
    286   1.1       cgd 		line[strlen(line) - 1] = '\0';
    287   1.1       cgd 		if (strcmp(s, line) == 0) {
    288  1.17   jnemeth 			(void)strlcpy(file, _PATH_GAMES, 256);
    289  1.17   jnemeth 			(void)strlcat(file, line, 256);
    290   1.1       cgd 			ret = file;
    291   1.1       cgd 			break;
    292   1.1       cgd 		}
    293   1.1       cgd 	}
    294  1.15    rpaulo 	(void)fclose(fp);
    295   1.1       cgd 	if (ret == NULL) {
    296   1.1       cgd 		test_mode = 1;
    297   1.1       cgd 		ret = s;
    298  1.15    rpaulo 		(void)fprintf(stderr, "%s: %s: game not found\n", games, s);
    299  1.15    rpaulo 		(void)fprintf(stderr, "Your score will not be logged.\n");
    300  1.15    rpaulo 		(void)sleep(2);	/* give the guy time to read it */
    301   1.1       cgd 	}
    302   1.1       cgd 	return (ret);
    303   1.1       cgd }
    304   1.1       cgd 
    305  1.20  dholland static int
    306  1.14       jmc list_games(void)
    307   1.1       cgd {
    308   1.1       cgd 	FILE		*fp;
    309   1.1       cgd 	char		line[256], games[256];
    310   1.1       cgd 	int		num_games = 0;
    311   1.1       cgd 
    312  1.17   jnemeth 	(void)strlcpy(games, _PATH_GAMES, 256);
    313  1.17   jnemeth 	(void)strlcat(games, GAMES, 256);
    314   1.1       cgd 
    315   1.1       cgd 	if ((fp = fopen(games, "r")) == NULL) {
    316   1.6     lukem 		warn("fopen %s", games);
    317   1.1       cgd 		return (-1);
    318   1.1       cgd 	}
    319  1.15    rpaulo 	(void)puts("available games:");
    320   1.1       cgd 	while (fgets(line, sizeof(line), fp) != NULL) {
    321  1.15    rpaulo 		(void)printf("	%s", line);
    322   1.1       cgd 		num_games++;
    323   1.1       cgd 	}
    324  1.15    rpaulo 	(void)fclose(fp);
    325   1.1       cgd 	if (num_games == 0) {
    326  1.15    rpaulo 		(void)fprintf(stderr, "%s: no games available\n", games);
    327   1.1       cgd 		return (-1);
    328   1.1       cgd 	}
    329   1.1       cgd 	return (0);
    330   1.1       cgd }
    331  1.24  dholland 
    332  1.24  dholland /* ARGSUSED */
    333  1.24  dholland static void
    334  1.24  dholland quit(int dummy __unused)
    335  1.24  dholland {
    336  1.24  dholland 	int c;
    337  1.24  dholland #ifdef BSD
    338  1.24  dholland 	struct itimerval	itv;
    339  1.24  dholland #endif
    340  1.24  dholland 	ioaskquit();
    341  1.24  dholland 	c = getAChar();
    342  1.24  dholland 	if (c == EOF || c == 'y') {
    343  1.24  dholland 		/* disable timer */
    344  1.24  dholland #ifdef BSD
    345  1.24  dholland 		itv.it_value.tv_sec = 0;
    346  1.24  dholland 		itv.it_value.tv_usec = 0;
    347  1.24  dholland 		(void)setitimer(ITIMER_REAL, &itv, NULL);
    348  1.24  dholland #endif
    349  1.24  dholland #ifdef SYSV
    350  1.24  dholland 		alarm(0);
    351  1.24  dholland #endif
    352  1.24  dholland 		shutdown_gr();
    353  1.24  dholland 		(void)log_score(0);
    354  1.24  dholland 		exit(0);
    355  1.24  dholland 	}
    356  1.24  dholland 	ionoquit();
    357  1.24  dholland }
    358