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