NetBSD/games/atc/main.c

358 lines
7.9 KiB
C
Raw Normal View History

/* $NetBSD: main.c,v 1.24 2015/06/25 05:33:02 dholland Exp $ */
1995-03-21 18:03:38 +03:00
1993-03-21 12:45:37 +03:00
/*-
1995-03-21 18:03:38 +03:00
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* This code is derived from software contributed to Berkeley by
* Ed James.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
*
* Copy permission is hereby granted provided that this notice is
* retained on all partial or complete copies.
*
* For more info on this and all of my stuff, mail edjames@berkeley.edu.
*/
1997-10-10 06:07:01 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1990, 1993\
The Regents of the University of California. All rights reserved.");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1995-03-21 18:03:38 +03:00
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: main.c,v 1.24 2015/06/25 05:33:02 dholland Exp $");
1995-03-21 18:03:38 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#include <getopt.h>
#include <err.h>
1993-03-21 12:45:37 +03:00
#include "pathnames.h"
#include "def.h"
#include "struct.h"
#include "extern.h"
#include "tunable.h"
1993-03-21 12:45:37 +03:00
2001-02-05 03:22:52 +03:00
extern FILE *yyin;
1997-10-10 06:07:01 +04:00
2009-08-12 08:48:03 +04:00
static int read_file(const char *);
static const char *default_game(void);
static const char *okay_game(const char *);
static int list_games(void);
static void quit(int);
2009-08-12 08:48:03 +04:00
1997-10-10 06:07:01 +04:00
int
2005-07-01 04:48:34 +04:00
main(int argc, char *argv[])
1993-03-21 12:45:37 +03:00
{
2008-01-31 08:19:44 +03:00
unsigned long seed;
1993-03-21 12:45:37 +03:00
int f_usage = 0, f_list = 0, f_showscore = 0;
int f_printpath = 0;
const char *file = NULL;
int ch;
1995-04-28 01:22:19 +04:00
struct sigaction sa;
1993-03-21 12:45:37 +03:00
#ifdef BSD
struct itimerval itv;
#endif
/* Open the score file then revoke setgid privileges */
open_score_file();
2005-08-10 21:53:28 +04:00
(void)setgid(getgid());
2008-01-31 08:19:44 +03:00
start_time = time(NULL);
seed = start_time;
1993-03-21 12:45:37 +03:00
while ((ch = getopt(argc, argv, ":u?lstpg:f:r:")) != -1) {
switch (ch) {
case '?':
case 'u':
default:
f_usage = 1;
break;
case 'l':
f_list = 1;
break;
case 's':
case 't':
f_showscore = 1;
break;
case 'p':
f_printpath = 1;
break;
case 'r':
seed = atoi(optarg);
break;
case 'f':
case 'g':
file = optarg;
1993-03-21 12:45:37 +03:00
break;
}
}
2005-07-01 04:48:34 +04:00
if (optind < argc)
f_usage = 1;
2008-01-31 08:19:44 +03:00
srandom(seed);
1993-03-21 12:45:37 +03:00
if (f_usage)
2005-08-10 21:53:28 +04:00
(void)fprintf(stderr,
1993-03-21 12:45:37 +03:00
"Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
2005-08-10 21:53:28 +04:00
argv[0]);
1993-03-21 12:45:37 +03:00
if (f_showscore)
2005-08-10 21:53:28 +04:00
(void)log_score(1);
1993-03-21 12:45:37 +03:00
if (f_list)
2005-08-10 21:53:28 +04:00
(void)list_games();
1993-03-21 12:45:37 +03:00
if (f_printpath) {
char buf[100];
(void)strlcpy(buf, _PATH_GAMES, 100);
2005-08-10 21:53:28 +04:00
(void)puts(buf);
1993-03-21 12:45:37 +03:00
}
if (f_usage || f_showscore || f_list || f_printpath)
exit(0);
if (file == NULL)
file = default_game();
else
file = okay_game(file);
if (file == NULL || read_file(file) < 0)
exit(1);
init_gr();
setup_screen(sp);
addplane();
1993-03-21 12:45:37 +03:00
2005-08-10 21:53:28 +04:00
(void)signal(SIGINT, quit);
(void)signal(SIGQUIT, quit);
1993-03-21 12:45:37 +03:00
#ifdef BSD
2005-08-10 21:53:28 +04:00
(void)signal(SIGTSTP, SIG_IGN);
1993-03-21 12:45:37 +03:00
#endif
2005-08-10 21:53:28 +04:00
(void)signal(SIGHUP, log_score_quit);
(void)signal(SIGTERM, log_score_quit);
1993-03-21 12:45:37 +03:00
2005-08-10 21:53:28 +04:00
(void)tcgetattr(fileno(stdin), &tty_start);
1995-04-28 01:22:19 +04:00
tty_new = tty_start;
tty_new.c_lflag &= ~(ICANON|ECHO);
tty_new.c_iflag |= ICRNL;
1993-03-21 12:45:37 +03:00
tty_new.c_cc[VMIN] = 1;
tty_new.c_cc[VTIME] = 0;
2005-08-10 21:53:28 +04:00
(void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
1995-04-28 01:22:19 +04:00
sa.sa_handler = update;
2005-08-10 21:53:28 +04:00
(void)sigemptyset(&sa.sa_mask);
(void)sigaddset(&sa.sa_mask, SIGALRM);
(void)sigaddset(&sa.sa_mask, SIGINT);
1995-04-28 01:22:19 +04:00
sa.sa_flags = 0;
2005-08-10 21:53:28 +04:00
(void)sigaction(SIGALRM, &sa, (struct sigaction *)0);
1993-03-21 12:45:37 +03:00
#ifdef BSD
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 1;
itv.it_interval.tv_sec = sp->update_secs;
itv.it_interval.tv_usec = 0;
2005-08-10 21:53:28 +04:00
(void)setitimer(ITIMER_REAL, &itv, NULL);
1993-03-21 12:45:37 +03:00
#endif
#ifdef SYSV
alarm(sp->update_secs);
#endif
for (;;) {
if (getcommand() != 1)
planewin();
else {
#ifdef BSD
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 0;
2005-08-10 21:53:28 +04:00
(void)setitimer(ITIMER_REAL, &itv, NULL);
1993-03-21 12:45:37 +03:00
#endif
#ifdef SYSV
alarm(0);
#endif
1997-10-10 06:07:01 +04:00
update(0);
1993-03-21 12:45:37 +03:00
#ifdef BSD
itv.it_value.tv_sec = sp->update_secs;
itv.it_value.tv_usec = 0;
itv.it_interval.tv_sec = sp->update_secs;
itv.it_interval.tv_usec = 0;
2005-08-10 21:53:28 +04:00
(void)setitimer(ITIMER_REAL, &itv, NULL);
1993-03-21 12:45:37 +03:00
#endif
#ifdef SYSV
alarm(sp->update_secs);
#endif
}
}
}
2009-08-12 08:48:03 +04:00
static int
2005-07-01 04:48:34 +04:00
read_file(const char *s)
1993-03-21 12:45:37 +03:00
{
int retval;
2005-07-01 04:48:34 +04:00
filename = s;
1993-03-21 12:45:37 +03:00
yyin = fopen(s, "r");
if (yyin == NULL) {
1997-10-11 06:01:02 +04:00
warn("fopen %s", s);
1993-03-21 12:45:37 +03:00
return (-1);
}
retval = yyparse();
2005-08-10 21:53:28 +04:00
(void)fclose(yyin);
1993-03-21 12:45:37 +03:00
if (retval != 0)
return (-1);
else
return (0);
}
2009-08-12 08:48:03 +04:00
static const char *
2005-07-01 04:48:34 +04:00
default_game(void)
1993-03-21 12:45:37 +03:00
{
FILE *fp;
static char file[256];
char line[256], games[256];
(void)strlcpy(games, _PATH_GAMES, 256);
(void)strlcat(games, GAMES, 256);
1993-03-21 12:45:37 +03:00
if ((fp = fopen(games, "r")) == NULL) {
1997-10-11 06:01:02 +04:00
warn("fopen %s", games);
1993-03-21 12:45:37 +03:00
return (NULL);
}
if (fgets(line, sizeof(line), fp) == NULL) {
2005-08-10 21:53:28 +04:00
(void)fprintf(stderr, "%s: no default game available\n", games);
2006-03-19 02:38:12 +03:00
fclose(fp);
1993-03-21 12:45:37 +03:00
return (NULL);
}
2005-08-10 21:53:28 +04:00
(void)fclose(fp);
1993-03-21 12:45:37 +03:00
line[strlen(line) - 1] = '\0';
(void)strlcpy(file, _PATH_GAMES, 256);
(void)strlcat(file, line, 256);
1993-03-21 12:45:37 +03:00
return (file);
}
2009-08-12 08:48:03 +04:00
static const char *
2005-07-01 04:48:34 +04:00
okay_game(const char *s)
1993-03-21 12:45:37 +03:00
{
FILE *fp;
static char file[256];
const char *ret = NULL;
char line[256], games[256];
1993-03-21 12:45:37 +03:00
(void)strlcpy(games, _PATH_GAMES, 256);
(void)strlcat(games, GAMES, 256);
1993-03-21 12:45:37 +03:00
if ((fp = fopen(games, "r")) == NULL) {
1997-10-11 06:01:02 +04:00
warn("fopen %s", games);
1993-03-21 12:45:37 +03:00
return (NULL);
}
while (fgets(line, sizeof(line), fp) != NULL) {
line[strlen(line) - 1] = '\0';
if (strcmp(s, line) == 0) {
(void)strlcpy(file, _PATH_GAMES, 256);
(void)strlcat(file, line, 256);
1993-03-21 12:45:37 +03:00
ret = file;
break;
}
}
2005-08-10 21:53:28 +04:00
(void)fclose(fp);
1993-03-21 12:45:37 +03:00
if (ret == NULL) {
test_mode = 1;
ret = s;
2005-08-10 21:53:28 +04:00
(void)fprintf(stderr, "%s: %s: game not found\n", games, s);
(void)fprintf(stderr, "Your score will not be logged.\n");
(void)sleep(2); /* give the guy time to read it */
1993-03-21 12:45:37 +03:00
}
return (ret);
}
2009-08-12 08:48:03 +04:00
static int
2005-07-01 04:48:34 +04:00
list_games(void)
1993-03-21 12:45:37 +03:00
{
FILE *fp;
char line[256], games[256];
int num_games = 0;
(void)strlcpy(games, _PATH_GAMES, 256);
(void)strlcat(games, GAMES, 256);
1993-03-21 12:45:37 +03:00
if ((fp = fopen(games, "r")) == NULL) {
1997-10-11 06:01:02 +04:00
warn("fopen %s", games);
1993-03-21 12:45:37 +03:00
return (-1);
}
2005-08-10 21:53:28 +04:00
(void)puts("available games:");
1993-03-21 12:45:37 +03:00
while (fgets(line, sizeof(line), fp) != NULL) {
2005-08-10 21:53:28 +04:00
(void)printf(" %s", line);
1993-03-21 12:45:37 +03:00
num_games++;
}
2005-08-10 21:53:28 +04:00
(void)fclose(fp);
1993-03-21 12:45:37 +03:00
if (num_games == 0) {
2005-08-10 21:53:28 +04:00
(void)fprintf(stderr, "%s: no games available\n", games);
1993-03-21 12:45:37 +03:00
return (-1);
}
return (0);
}
/* ARGSUSED */
static void
quit(int dummy __unused)
{
int c;
#ifdef BSD
struct itimerval itv;
#endif
ioaskquit();
c = getAChar();
if (c == EOF || c == 'y') {
/* disable timer */
#ifdef BSD
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 0;
(void)setitimer(ITIMER_REAL, &itv, NULL);
#endif
#ifdef SYSV
alarm(0);
#endif
shutdown_gr();
(void)log_score(0);
exit(0);
}
ionoquit();
}