Home | History | Annotate | Line # | Download | only in atc
main.c revision 1.13
      1  1.13       agc /*	$NetBSD: main.c,v 1.13 2003/08/07 09:36:54 agc 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.5     lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
     47   1.5     lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.13       agc __RCSID("$NetBSD: main.c,v 1.13 2003/08/07 09:36:54 agc Exp $");
     55   1.3       cgd #endif
     56   1.1       cgd #endif /* not lint */
     57   1.1       cgd 
     58   1.1       cgd #include "include.h"
     59   1.1       cgd #include "pathnames.h"
     60   1.1       cgd 
     61  1.12  christos extern FILE	*yyin;
     62   1.5     lukem 
     63   1.5     lukem int
     64   1.1       cgd main(ac, av)
     65   1.5     lukem 	int	 ac;
     66   1.1       cgd 	char	*av[];
     67   1.1       cgd {
     68   1.1       cgd 	int			seed;
     69   1.1       cgd 	int			f_usage = 0, f_list = 0, f_showscore = 0;
     70   1.1       cgd 	int			f_printpath = 0;
     71   1.8   hubertf 	const char		*file = NULL;
     72  1.10       mjl 	int			ch;
     73   1.4   mycroft 	struct sigaction	sa;
     74   1.1       cgd #ifdef BSD
     75   1.1       cgd 	struct itimerval	itv;
     76   1.1       cgd #endif
     77   1.9   hubertf 
     78   1.9   hubertf 	/* Open the score file then revoke setgid privileges */
     79   1.9   hubertf 	open_score_file();
     80  1.11   mycroft 	setgid(getgid());
     81   1.1       cgd 
     82  1.10       mjl 	start_time = seed = time(NULL);
     83   1.1       cgd 
     84  1.10       mjl 	while ((ch = getopt(ac, av, "ulstpg:f:r:")) != -1) {
     85  1.10       mjl 		switch (ch) {
     86  1.10       mjl 		case '?':
     87  1.10       mjl 		case 'u':
     88  1.10       mjl 		default:
     89  1.10       mjl 			f_usage++;
     90  1.10       mjl 			break;
     91  1.10       mjl 		case 'l':
     92  1.10       mjl 			f_list++;
     93  1.10       mjl 			break;
     94  1.10       mjl 		case 's':
     95  1.10       mjl 		case 't':
     96  1.10       mjl 			f_showscore++;
     97  1.10       mjl 			break;
     98  1.10       mjl 		case 'p':
     99  1.10       mjl 			f_printpath++;
    100  1.10       mjl 			break;
    101  1.10       mjl 		case 'r':
    102  1.10       mjl 			seed = atoi(optarg);
    103  1.10       mjl 			break;
    104  1.10       mjl 		case 'f':
    105  1.10       mjl 		case 'g':
    106  1.10       mjl 			file = optarg;
    107   1.1       cgd 			break;
    108   1.1       cgd 		}
    109   1.1       cgd 	}
    110  1.10       mjl 	if (optind < ac)
    111  1.10       mjl 		f_usage++;
    112   1.1       cgd 	srandom(seed);
    113   1.1       cgd 
    114   1.1       cgd 	if (f_usage)
    115   1.1       cgd 		fprintf(stderr,
    116   1.1       cgd 		    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
    117  1.10       mjl 			av[0]);
    118   1.1       cgd 	if (f_showscore)
    119   1.1       cgd 		log_score(1);
    120   1.1       cgd 	if (f_list)
    121   1.1       cgd 		list_games();
    122   1.1       cgd 	if (f_printpath) {
    123   1.1       cgd 		char	buf[100];
    124   1.1       cgd 
    125   1.1       cgd 		strcpy(buf, _PATH_GAMES);
    126   1.1       cgd 		buf[strlen(buf) - 1] = '\0';
    127   1.1       cgd 		puts(buf);
    128   1.1       cgd 	}
    129   1.1       cgd 
    130   1.1       cgd 	if (f_usage || f_showscore || f_list || f_printpath)
    131   1.1       cgd 		exit(0);
    132   1.1       cgd 
    133   1.1       cgd 	if (file == NULL)
    134   1.1       cgd 		file = default_game();
    135   1.1       cgd 	else
    136   1.1       cgd 		file = okay_game(file);
    137   1.1       cgd 
    138   1.1       cgd 	if (file == NULL || read_file(file) < 0)
    139   1.1       cgd 		exit(1);
    140   1.1       cgd 
    141   1.1       cgd 	init_gr();
    142   1.1       cgd 	setup_screen(sp);
    143   1.1       cgd 
    144   1.1       cgd 	addplane();
    145   1.1       cgd 
    146   1.1       cgd 	signal(SIGINT, quit);
    147   1.1       cgd 	signal(SIGQUIT, quit);
    148   1.1       cgd #ifdef BSD
    149   1.1       cgd 	signal(SIGTSTP, SIG_IGN);
    150   1.1       cgd 	signal(SIGSTOP, SIG_IGN);
    151   1.1       cgd #endif
    152   1.5     lukem 	signal(SIGHUP, log_score_quit);
    153   1.5     lukem 	signal(SIGTERM, log_score_quit);
    154   1.1       cgd 
    155   1.4   mycroft 	tcgetattr(fileno(stdin), &tty_start);
    156   1.4   mycroft 	tty_new = tty_start;
    157   1.4   mycroft 	tty_new.c_lflag &= ~(ICANON|ECHO);
    158   1.7   hubertf 	tty_new.c_iflag |= ICRNL;
    159   1.1       cgd 	tty_new.c_cc[VMIN] = 1;
    160   1.1       cgd 	tty_new.c_cc[VTIME] = 0;
    161   1.4   mycroft 	tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
    162   1.1       cgd 
    163   1.4   mycroft 	sa.sa_handler = update;
    164   1.4   mycroft 	sigemptyset(&sa.sa_mask);
    165   1.4   mycroft 	sigaddset(&sa.sa_mask, SIGALRM);
    166   1.4   mycroft 	sigaddset(&sa.sa_mask, SIGINT);
    167   1.4   mycroft 	sa.sa_flags = 0;
    168   1.4   mycroft 	sigaction(SIGALRM, &sa, (struct sigaction *)0);
    169   1.1       cgd 
    170   1.1       cgd #ifdef BSD
    171   1.1       cgd 	itv.it_value.tv_sec = 0;
    172   1.1       cgd 	itv.it_value.tv_usec = 1;
    173   1.1       cgd 	itv.it_interval.tv_sec = sp->update_secs;
    174   1.1       cgd 	itv.it_interval.tv_usec = 0;
    175   1.1       cgd 	setitimer(ITIMER_REAL, &itv, NULL);
    176   1.1       cgd #endif
    177   1.1       cgd #ifdef SYSV
    178   1.1       cgd 	alarm(sp->update_secs);
    179   1.1       cgd #endif
    180   1.1       cgd 
    181   1.1       cgd 	for (;;) {
    182   1.1       cgd 		if (getcommand() != 1)
    183   1.1       cgd 			planewin();
    184   1.1       cgd 		else {
    185   1.1       cgd #ifdef BSD
    186   1.1       cgd 			itv.it_value.tv_sec = 0;
    187   1.1       cgd 			itv.it_value.tv_usec = 0;
    188   1.1       cgd 			setitimer(ITIMER_REAL, &itv, NULL);
    189   1.1       cgd #endif
    190   1.1       cgd #ifdef SYSV
    191   1.1       cgd 			alarm(0);
    192   1.1       cgd #endif
    193   1.1       cgd 
    194   1.5     lukem 			update(0);
    195   1.1       cgd 
    196   1.1       cgd #ifdef BSD
    197   1.1       cgd 			itv.it_value.tv_sec = sp->update_secs;
    198   1.1       cgd 			itv.it_value.tv_usec = 0;
    199   1.1       cgd 			itv.it_interval.tv_sec = sp->update_secs;
    200   1.1       cgd 			itv.it_interval.tv_usec = 0;
    201   1.1       cgd 			setitimer(ITIMER_REAL, &itv, NULL);
    202   1.1       cgd #endif
    203   1.1       cgd #ifdef SYSV
    204   1.1       cgd 			alarm(sp->update_secs);
    205   1.1       cgd #endif
    206   1.1       cgd 		}
    207   1.1       cgd 	}
    208   1.1       cgd }
    209   1.1       cgd 
    210   1.5     lukem int
    211   1.1       cgd read_file(s)
    212   1.8   hubertf 	const char	*s;
    213   1.1       cgd {
    214   1.1       cgd 	int		retval;
    215   1.1       cgd 
    216   1.1       cgd 	file = s;
    217   1.1       cgd 	yyin = fopen(s, "r");
    218   1.1       cgd 	if (yyin == NULL) {
    219   1.6     lukem 		warn("fopen %s", s);
    220   1.1       cgd 		return (-1);
    221   1.1       cgd 	}
    222   1.1       cgd 	retval = yyparse();
    223   1.1       cgd 	fclose(yyin);
    224   1.1       cgd 
    225   1.1       cgd 	if (retval != 0)
    226   1.1       cgd 		return (-1);
    227   1.1       cgd 	else
    228   1.1       cgd 		return (0);
    229   1.1       cgd }
    230   1.1       cgd 
    231   1.8   hubertf const char	*
    232   1.1       cgd default_game()
    233   1.1       cgd {
    234   1.1       cgd 	FILE		*fp;
    235   1.1       cgd 	static char	file[256];
    236   1.1       cgd 	char		line[256], games[256];
    237   1.1       cgd 
    238   1.1       cgd 	strcpy(games, _PATH_GAMES);
    239   1.1       cgd 	strcat(games, GAMES);
    240   1.1       cgd 
    241   1.1       cgd 	if ((fp = fopen(games, "r")) == NULL) {
    242   1.6     lukem 		warn("fopen %s", games);
    243   1.1       cgd 		return (NULL);
    244   1.1       cgd 	}
    245   1.1       cgd 	if (fgets(line, sizeof(line), fp) == NULL) {
    246   1.1       cgd 		fprintf(stderr, "%s: no default game available\n", games);
    247   1.1       cgd 		return (NULL);
    248   1.1       cgd 	}
    249   1.1       cgd 	fclose(fp);
    250   1.1       cgd 	line[strlen(line) - 1] = '\0';
    251   1.1       cgd 	strcpy(file, _PATH_GAMES);
    252   1.1       cgd 	strcat(file, line);
    253   1.1       cgd 	return (file);
    254   1.1       cgd }
    255   1.1       cgd 
    256   1.8   hubertf const char	*
    257   1.1       cgd okay_game(s)
    258   1.8   hubertf 	const char	*s;
    259   1.1       cgd {
    260   1.1       cgd 	FILE		*fp;
    261   1.1       cgd 	static char	file[256];
    262   1.8   hubertf 	const char	*ret = NULL;
    263   1.8   hubertf 	char		line[256], games[256];
    264   1.1       cgd 
    265   1.1       cgd 	strcpy(games, _PATH_GAMES);
    266   1.1       cgd 	strcat(games, GAMES);
    267   1.1       cgd 
    268   1.1       cgd 	if ((fp = fopen(games, "r")) == NULL) {
    269   1.6     lukem 		warn("fopen %s", games);
    270   1.1       cgd 		return (NULL);
    271   1.1       cgd 	}
    272   1.1       cgd 	while (fgets(line, sizeof(line), fp) != NULL) {
    273   1.1       cgd 		line[strlen(line) - 1] = '\0';
    274   1.1       cgd 		if (strcmp(s, line) == 0) {
    275   1.1       cgd 			strcpy(file, _PATH_GAMES);
    276   1.1       cgd 			strcat(file, line);
    277   1.1       cgd 			ret = file;
    278   1.1       cgd 			break;
    279   1.1       cgd 		}
    280   1.1       cgd 	}
    281   1.1       cgd 	fclose(fp);
    282   1.1       cgd 	if (ret == NULL) {
    283   1.1       cgd 		test_mode = 1;
    284   1.1       cgd 		ret = s;
    285   1.1       cgd 		fprintf(stderr, "%s: %s: game not found\n", games, s);
    286   1.1       cgd 		fprintf(stderr, "Your score will not be logged.\n");
    287   1.1       cgd 		sleep(2);	/* give the guy time to read it */
    288   1.1       cgd 	}
    289   1.1       cgd 	return (ret);
    290   1.1       cgd }
    291   1.1       cgd 
    292   1.5     lukem int
    293   1.1       cgd list_games()
    294   1.1       cgd {
    295   1.1       cgd 	FILE		*fp;
    296   1.1       cgd 	char		line[256], games[256];
    297   1.1       cgd 	int		num_games = 0;
    298   1.1       cgd 
    299   1.1       cgd 	strcpy(games, _PATH_GAMES);
    300   1.1       cgd 	strcat(games, GAMES);
    301   1.1       cgd 
    302   1.1       cgd 	if ((fp = fopen(games, "r")) == NULL) {
    303   1.6     lukem 		warn("fopen %s", games);
    304   1.1       cgd 		return (-1);
    305   1.1       cgd 	}
    306   1.1       cgd 	puts("available games:");
    307   1.1       cgd 	while (fgets(line, sizeof(line), fp) != NULL) {
    308   1.1       cgd 		printf("	%s", line);
    309   1.1       cgd 		num_games++;
    310   1.1       cgd 	}
    311   1.1       cgd 	fclose(fp);
    312   1.1       cgd 	if (num_games == 0) {
    313   1.1       cgd 		fprintf(stderr, "%s: no games available\n", games);
    314   1.1       cgd 		return (-1);
    315   1.1       cgd 	}
    316   1.1       cgd 	return (0);
    317   1.1       cgd }
    318