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