141166Sbostic /*-
2*60740Sbostic * Copyright (c) 1990, 1993
3*60740Sbostic * The Regents of the University of California. All rights reserved.
441166Sbostic *
541166Sbostic * This code is derived from software contributed to Berkeley by
641166Sbostic * Ed James.
741166Sbostic *
841166Sbostic * %sccs.include.redist.c%
941166Sbostic */
1041166Sbostic
1141162Sbostic /*
1241162Sbostic * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
1341162Sbostic *
1441162Sbostic * Copy permission is hereby granted provided that this notice is
1541162Sbostic * retained on all partial or complete copies.
1641162Sbostic *
1741162Sbostic * For more info on this and all of my stuff, mail edjames@berkeley.edu.
1841162Sbostic */
1941162Sbostic
2041166Sbostic #ifndef lint
21*60740Sbostic static char copyright[] =
22*60740Sbostic "@(#) Copyright (c) 1990, 1993\n\
23*60740Sbostic The Regents of the University of California. All rights reserved.\n";
2441166Sbostic #endif /* not lint */
2541166Sbostic
2641166Sbostic #ifndef lint
27*60740Sbostic static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 05/31/93";
2841166Sbostic #endif /* not lint */
2941166Sbostic
3041162Sbostic #include "include.h"
3141165Sbostic #include "pathnames.h"
3241162Sbostic
main(ac,av)3341162Sbostic main(ac, av)
3441162Sbostic char *av[];
3541162Sbostic {
3641162Sbostic int seed;
3741162Sbostic int f_usage = 0, f_list = 0, f_showscore = 0;
3841162Sbostic int f_printpath = 0;
3941162Sbostic char *file = NULL;
4041162Sbostic char *name, *ptr;
4141162Sbostic #ifdef BSD
4241162Sbostic struct itimerval itv;
4341162Sbostic #endif
4441162Sbostic extern char *default_game(), *okay_game();
4546992Sbostic extern void log_score(), quit(), update();
4641162Sbostic
4741162Sbostic start_time = seed = time(0);
4841162Sbostic
4941162Sbostic name = *av++;
5041162Sbostic while (*av) {
5141162Sbostic #ifndef SAVEDASH
5241162Sbostic if (**av == '-')
5341162Sbostic *++*av;
5441162Sbostic else
5541162Sbostic break;
5641162Sbostic #endif
5741162Sbostic ptr = *av++;
5841162Sbostic while (*ptr) {
5941162Sbostic switch (*ptr) {
6041162Sbostic case '?':
6141162Sbostic case 'u':
6241162Sbostic f_usage++;
6341162Sbostic break;
6441162Sbostic case 'l':
6541162Sbostic f_list++;
6641162Sbostic break;
6741162Sbostic case 's':
6841162Sbostic case 't':
6941162Sbostic f_showscore++;
7041162Sbostic break;
7141162Sbostic case 'p':
7241162Sbostic f_printpath++;
7341162Sbostic break;
7441162Sbostic case 'r':
7541162Sbostic seed = atoi(*av);
7641162Sbostic av++;
7741162Sbostic break;
7841162Sbostic case 'f':
7941162Sbostic case 'g':
8041162Sbostic file = *av;
8141162Sbostic av++;
8241162Sbostic break;
8341162Sbostic default:
8441162Sbostic fprintf(stderr, "Unknown option '%c'\n", *ptr,
8541162Sbostic name);
8641162Sbostic f_usage++;
8741162Sbostic break;
8841162Sbostic }
8941162Sbostic ptr++;
9041162Sbostic }
9141162Sbostic }
9241162Sbostic srandom(seed);
9341162Sbostic
9441162Sbostic if (f_usage)
9541162Sbostic fprintf(stderr,
9641162Sbostic "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
9741162Sbostic name);
9841162Sbostic if (f_showscore)
9941162Sbostic log_score(1);
10041162Sbostic if (f_list)
10141162Sbostic list_games();
10241162Sbostic if (f_printpath) {
10341162Sbostic char buf[100];
10441162Sbostic
10541165Sbostic strcpy(buf, _PATH_GAMES);
10641162Sbostic buf[strlen(buf) - 1] = '\0';
10741162Sbostic puts(buf);
10841162Sbostic }
10941162Sbostic
11041162Sbostic if (f_usage || f_showscore || f_list || f_printpath)
11141162Sbostic exit(0);
11241162Sbostic
11341162Sbostic if (file == NULL)
11441162Sbostic file = default_game();
11541162Sbostic else
11641162Sbostic file = okay_game(file);
11741162Sbostic
11841162Sbostic if (file == NULL || read_file(file) < 0)
11941162Sbostic exit(1);
12041162Sbostic
12141162Sbostic init_gr();
12241162Sbostic setup_screen(sp);
12341162Sbostic
12441162Sbostic addplane();
12541162Sbostic
12641162Sbostic signal(SIGINT, quit);
12741162Sbostic signal(SIGQUIT, quit);
12841162Sbostic #ifdef BSD
12941162Sbostic signal(SIGTSTP, SIG_IGN);
13041162Sbostic signal(SIGSTOP, SIG_IGN);
13141162Sbostic #endif
13241162Sbostic signal(SIGHUP, log_score);
13341162Sbostic signal(SIGTERM, log_score);
13441162Sbostic
13541162Sbostic #ifdef BSD
13641162Sbostic ioctl(fileno(stdin), TIOCGETP, &tty_start);
13741162Sbostic bcopy(&tty_start, &tty_new, sizeof(tty_new));
13841162Sbostic tty_new.sg_flags |= CBREAK;
13941162Sbostic tty_new.sg_flags &= ~ECHO;
14041162Sbostic ioctl(fileno(stdin), TIOCSETP, &tty_new);
14141162Sbostic #endif
14241162Sbostic
14341162Sbostic #ifdef SYSV
14441162Sbostic ioctl(fileno(stdin), TCGETA, &tty_start);
14541162Sbostic bcopy(&tty_start, &tty_new, sizeof(tty_new));
14641162Sbostic tty_new.c_lflag &= ~ICANON;
14741162Sbostic tty_new.c_lflag &= ~ECHO;
14841162Sbostic tty_new.c_cc[VMIN] = 1;
14941162Sbostic tty_new.c_cc[VTIME] = 0;
15041162Sbostic ioctl(fileno(stdin), TCSETAW, &tty_new);
15141162Sbostic #endif
15241162Sbostic
15341162Sbostic signal(SIGALRM, update);
15441162Sbostic
15541162Sbostic #ifdef BSD
15641162Sbostic itv.it_value.tv_sec = 0;
15741162Sbostic itv.it_value.tv_usec = 1;
15841162Sbostic itv.it_interval.tv_sec = sp->update_secs;
15941162Sbostic itv.it_interval.tv_usec = 0;
16041162Sbostic setitimer(ITIMER_REAL, &itv, NULL);
16141162Sbostic #endif
16241162Sbostic #ifdef SYSV
16341162Sbostic alarm(sp->update_secs);
16441162Sbostic #endif
16541162Sbostic
16641162Sbostic for (;;) {
16741162Sbostic if (getcommand() != 1)
16841162Sbostic planewin();
16941162Sbostic else {
17041162Sbostic #ifdef BSD
17141162Sbostic itv.it_value.tv_sec = 0;
17241162Sbostic itv.it_value.tv_usec = 0;
17341162Sbostic setitimer(ITIMER_REAL, &itv, NULL);
17441162Sbostic #endif
17541162Sbostic #ifdef SYSV
17641162Sbostic alarm(0);
17741162Sbostic #endif
17841162Sbostic
17941162Sbostic update();
18041162Sbostic
18141162Sbostic #ifdef BSD
18241162Sbostic itv.it_value.tv_sec = sp->update_secs;
18341162Sbostic itv.it_value.tv_usec = 0;
18441162Sbostic itv.it_interval.tv_sec = sp->update_secs;
18541162Sbostic itv.it_interval.tv_usec = 0;
18641162Sbostic setitimer(ITIMER_REAL, &itv, NULL);
18741162Sbostic #endif
18841162Sbostic #ifdef SYSV
18941162Sbostic alarm(sp->update_secs);
19041162Sbostic #endif
19141162Sbostic }
19241162Sbostic }
19341162Sbostic }
19441162Sbostic
read_file(s)19541162Sbostic read_file(s)
19641162Sbostic char *s;
19741162Sbostic {
19841162Sbostic extern FILE *yyin;
19941162Sbostic int retval;
20041162Sbostic
20141162Sbostic file = s;
20241162Sbostic yyin = fopen(s, "r");
20341162Sbostic if (yyin == NULL) {
20441162Sbostic perror(s);
20541162Sbostic return (-1);
20641162Sbostic }
20741162Sbostic retval = yyparse();
20841162Sbostic fclose(yyin);
20941162Sbostic
21041162Sbostic if (retval != 0)
21141162Sbostic return (-1);
21241162Sbostic else
21341162Sbostic return (0);
21441162Sbostic }
21541162Sbostic
21641162Sbostic char *
default_game()21741162Sbostic default_game()
21841162Sbostic {
21941162Sbostic FILE *fp;
22041162Sbostic static char file[256];
22141162Sbostic char line[256], games[256];
22241162Sbostic
22341165Sbostic strcpy(games, _PATH_GAMES);
22441162Sbostic strcat(games, GAMES);
22541162Sbostic
22641162Sbostic if ((fp = fopen(games, "r")) == NULL) {
22741162Sbostic perror(games);
22841162Sbostic return (NULL);
22941162Sbostic }
23041162Sbostic if (fgets(line, sizeof(line), fp) == NULL) {
23141162Sbostic fprintf(stderr, "%s: no default game available\n", games);
23241162Sbostic return (NULL);
23341162Sbostic }
23441162Sbostic fclose(fp);
23541162Sbostic line[strlen(line) - 1] = '\0';
23641165Sbostic strcpy(file, _PATH_GAMES);
23741162Sbostic strcat(file, line);
23841162Sbostic return (file);
23941162Sbostic }
24041162Sbostic
24141162Sbostic char *
okay_game(s)24241162Sbostic okay_game(s)
24341162Sbostic char *s;
24441162Sbostic {
24541162Sbostic FILE *fp;
24641162Sbostic static char file[256];
24741162Sbostic char *ret = NULL, line[256], games[256];
24841162Sbostic
24941165Sbostic strcpy(games, _PATH_GAMES);
25041162Sbostic strcat(games, GAMES);
25141162Sbostic
25241162Sbostic if ((fp = fopen(games, "r")) == NULL) {
25341162Sbostic perror(games);
25441162Sbostic return (NULL);
25541162Sbostic }
25641162Sbostic while (fgets(line, sizeof(line), fp) != NULL) {
25741162Sbostic line[strlen(line) - 1] = '\0';
25841162Sbostic if (strcmp(s, line) == 0) {
25941165Sbostic strcpy(file, _PATH_GAMES);
26041162Sbostic strcat(file, line);
26141162Sbostic ret = file;
26241162Sbostic break;
26341162Sbostic }
26441162Sbostic }
26541162Sbostic fclose(fp);
26641162Sbostic if (ret == NULL) {
26741162Sbostic test_mode = 1;
26841162Sbostic ret = s;
26941162Sbostic fprintf(stderr, "%s: %s: game not found\n", games, s);
27041162Sbostic fprintf(stderr, "Your score will not be logged.\n");
27141162Sbostic sleep(2); /* give the guy time to read it */
27241162Sbostic }
27341162Sbostic return (ret);
27441162Sbostic }
27541162Sbostic
list_games()27641162Sbostic list_games()
27741162Sbostic {
27841162Sbostic FILE *fp;
27941162Sbostic char line[256], games[256];
28041162Sbostic int num_games = 0;
28141162Sbostic
28241165Sbostic strcpy(games, _PATH_GAMES);
28341162Sbostic strcat(games, GAMES);
28441162Sbostic
28541162Sbostic if ((fp = fopen(games, "r")) == NULL) {
28641162Sbostic perror(games);
28741162Sbostic return (-1);
28841162Sbostic }
28941162Sbostic puts("available games:");
29041162Sbostic while (fgets(line, sizeof(line), fp) != NULL) {
29141162Sbostic printf(" %s", line);
29241162Sbostic num_games++;
29341162Sbostic }
29441162Sbostic fclose(fp);
29541162Sbostic if (num_games == 0) {
29641162Sbostic fprintf(stderr, "%s: no games available\n", games);
29741162Sbostic return (-1);
29841162Sbostic }
29941162Sbostic return (0);
30041162Sbostic }
301