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