Home | History | Annotate | Line # | Download | only in trek
main.c revision 1.9
      1  1.9   mycroft /*	$NetBSD: main.c,v 1.9 2000/05/08 07:56:06 mycroft Exp $	*/
      2  1.4       cgd 
      3  1.1       cgd /*
      4  1.4       cgd  * Copyright (c) 1980, 1993
      5  1.4       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.5  christos #include <sys/cdefs.h>
     37  1.1       cgd #ifndef lint
     38  1.5  christos __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39  1.5  christos 	The Regents of the University of California.  All rights reserved.\n");
     40  1.1       cgd #endif /* not lint */
     41  1.1       cgd 
     42  1.1       cgd #ifndef lint
     43  1.4       cgd #if 0
     44  1.4       cgd static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
     45  1.4       cgd #else
     46  1.9   mycroft __RCSID("$NetBSD: main.c,v 1.9 2000/05/08 07:56:06 mycroft Exp $");
     47  1.4       cgd #endif
     48  1.1       cgd #endif /* not lint */
     49  1.1       cgd 
     50  1.5  christos #include <stdio.h>
     51  1.5  christos #include <setjmp.h>
     52  1.5  christos #include <termios.h>
     53  1.5  christos #include <stdlib.h>
     54  1.5  christos #include <unistd.h>
     55  1.5  christos #include <err.h>
     56  1.6       cjs #include <time.h>
     57  1.8       jsm #include <sys/types.h>
     58  1.5  christos #include "trek.h"
     59  1.5  christos #include "getpar.h"
     60  1.1       cgd 
     61  1.1       cgd # define	PRIO		00	/* default priority */
     62  1.1       cgd 
     63  1.8       jsm uid_t	Mother	= 51 + (51 << 8);
     64  1.1       cgd 
     65  1.1       cgd /*
     66  1.1       cgd **	 ####  #####    #    ####          #####  ####   #####  #   #
     67  1.1       cgd **	#        #     # #   #   #           #    #   #  #      #  #
     68  1.1       cgd **	 ###     #    #####  ####            #    ####   ###    ###
     69  1.1       cgd **	    #    #    #   #  #  #            #    #  #   #      #  #
     70  1.1       cgd **	####     #    #   #  #   #           #    #   #  #####  #   #
     71  1.1       cgd **
     72  1.1       cgd **	C version by Eric P. Allman 5/76 (U.C. Berkeley) with help
     73  1.1       cgd **		from Jeff Poskanzer and Pete Rubinstein.
     74  1.1       cgd **
     75  1.1       cgd **	I also want to thank everyone here at Berkeley who
     76  1.1       cgd **	where crazy enough to play the undebugged game.  I want to
     77  1.1       cgd **	particularly thank Nick Whyte, who made considerable
     78  1.1       cgd **	suggestions regarding the content of the game.  Why, I'll
     79  1.1       cgd **	never forget the time he suggested the name for the
     80  1.1       cgd **	"capture" command.
     81  1.1       cgd **
     82  1.1       cgd **	Please send comments, questions, and suggestions about this
     83  1.1       cgd **		game to:
     84  1.1       cgd **			Eric P. Allman
     85  1.1       cgd **			Project INGRES
     86  1.1       cgd **			Electronics Research Laboratory
     87  1.1       cgd **			Cory Hall
     88  1.1       cgd **			University of California
     89  1.1       cgd **			Berkeley, California  94720
     90  1.1       cgd **
     91  1.1       cgd **	If you make ANY changes in the game, I sure would like to
     92  1.1       cgd **	know about them.  It is sort of an ongoing project for me,
     93  1.1       cgd **	and I very much want to put in any bug fixes and improvements
     94  1.1       cgd **	that you might come up with.
     95  1.1       cgd **
     96  1.1       cgd **	FORTRASH version by Kay R. Fisher (DEC) "and countless others".
     97  1.1       cgd **	That was adapted from the "original BASIC program" (ha!) by
     98  1.1       cgd **		Mike Mayfield (Centerline Engineering).
     99  1.1       cgd **
    100  1.1       cgd **	Additional inspiration taken from FORTRAN version by
    101  1.1       cgd **		David Matuszek and Paul Reynolds which runs on the CDC
    102  1.1       cgd **		7600 at Lawrence Berkeley Lab, maintained there by
    103  1.1       cgd **		Andy Davidson.  This version is also available at LLL
    104  1.1       cgd **		and at LMSC.  In all fairness, this version was the
    105  1.1       cgd **		major inspiration for this version of the game (trans-
    106  1.1       cgd **		lation:  I ripped off a whole lot of code).
    107  1.1       cgd **
    108  1.1       cgd **	Minor other input from the "Battelle Version 7A" by Joe Miller
    109  1.1       cgd **		(Graphics Systems Group, Battelle-Columbus Labs) and
    110  1.1       cgd **		Ross Pavlac (Systems Programmer, Battelle Memorial
    111  1.1       cgd **		Institute).  That version was written in December '74
    112  1.1       cgd **		and extensively modified June '75.  It was adapted
    113  1.1       cgd **		from the FTN version by Ron Williams of CDC Sunnyvale,
    114  1.1       cgd **		which was adapted from the Basic version distributed
    115  1.1       cgd **		by DEC.  It also had "neat stuff swiped" from T. T.
    116  1.1       cgd **		Terry and Jim Korp (University of Texas), Hicks (Penn
    117  1.1       cgd **		U.), and Rick Maus (Georgia Tech).  Unfortunately, it
    118  1.1       cgd **		was not as readable as it could have been and so the
    119  1.1       cgd **		translation effort was severely hampered.  None the
    120  1.1       cgd **		less, I got the idea of inhabited starsystems from this
    121  1.1       cgd **		version.
    122  1.1       cgd **
    123  1.1       cgd **	Permission is given for use, copying, and modification of
    124  1.1       cgd **		all or part of this program and related documentation,
    125  1.1       cgd **		provided that all reference to the authors are maintained.
    126  1.1       cgd **
    127  1.1       cgd **
    128  1.1       cgd **********************************************************************
    129  1.1       cgd **
    130  1.1       cgd **  NOTES TO THE MAINTAINER:
    131  1.1       cgd **
    132  1.1       cgd **	There is a compilation option xTRACE which must be set for any
    133  1.1       cgd **	trace information to be generated.  It is probably defined in
    134  1.1       cgd **	the version that you get.  It can be removed, however, if you
    135  1.1       cgd **	have trouble finding room in core.
    136  1.1       cgd **
    137  1.1       cgd **	Many things in trek are not as clear as they might be, but are
    138  1.1       cgd **	done to reduce space.  I compile with the -f and -O flags.  I
    139  1.1       cgd **	am constrained to running with non-seperated I/D space, since
    140  1.1       cgd **	we don't have doubleing point hardware here; even if we did, I
    141  1.1       cgd **	would like trek to be available to the large number of people
    142  1.1       cgd **	who either have an 11/40 or do not have FP hardware.  I also
    143  1.1       cgd **	found it desirable to make the code run reentrant, so this
    144  1.1       cgd **	added even more space constraints.
    145  1.1       cgd **
    146  1.1       cgd **	I use the portable C library to do my I/O.  This is done be-
    147  1.1       cgd **	cause I wanted the game easily transportable to other C
    148  1.1       cgd **	implementations, and because I was too lazy to do the doubleing
    149  1.1       cgd **	point input myself.  Little did I know.  The portable C library
    150  1.1       cgd **	released by Bell Labs has more bugs than you would believe, so
    151  1.1       cgd **	I ended up rewriting the whole blessed thing.  Trek excercises
    152  1.1       cgd **	many of the bugs in it, as well as bugs in some of the section
    153  1.1       cgd **	III UNIX routines.  We have fixed them here.  One main problem
    154  1.1       cgd **	was a bug in alloc() that caused it to always ask for a large
    155  1.1       cgd **	hunk of memory, which worked fine unless you were almost out,
    156  1.1       cgd **	which I inevitably was.  If you want the code for all of this
    157  1.1       cgd **	stuff, it is also available through me.
    158  1.1       cgd **
    159  1.1       cgd ***********************************************************************
    160  1.1       cgd */
    161  1.1       cgd 
    162  1.1       cgd jmp_buf env;
    163  1.1       cgd 
    164  1.5  christos int main __P((int, char **));
    165  1.5  christos 
    166  1.5  christos int
    167  1.1       cgd main(argc, argv)
    168  1.1       cgd int	argc;
    169  1.1       cgd char	**argv;
    170  1.1       cgd {
    171  1.6       cjs 	time_t		curtime;
    172  1.1       cgd 	long			vect;
    173  1.5  christos 	char		opencode;
    174  1.1       cgd 	int			prio;
    175  1.5  christos 	int		ac;
    176  1.5  christos 	char		**av;
    177  1.3       jtc 	struct	termios		argp;
    178  1.7       jsm 
    179  1.7       jsm 	/* Revoke setgid privileges */
    180  1.9   mycroft 	setgid(getgid());
    181  1.1       cgd 
    182  1.1       cgd 	av = argv;
    183  1.1       cgd 	ac = argc;
    184  1.1       cgd 	av++;
    185  1.6       cjs 	time(&curtime);
    186  1.6       cjs 	vect = (long) curtime;
    187  1.1       cgd 	srand(vect);
    188  1.1       cgd 	opencode = 'w';
    189  1.1       cgd 	prio = PRIO;
    190  1.3       jtc 
    191  1.3       jtc 	if (tcgetattr(1, &argp) == 0)
    192  1.1       cgd 	{
    193  1.3       jtc 		if (cfgetispeed(&argp) < B1200)
    194  1.1       cgd 			Etc.fast++;
    195  1.1       cgd 	}
    196  1.3       jtc 
    197  1.1       cgd 	while (ac > 1 && av[0][0] == '-')
    198  1.1       cgd 	{
    199  1.1       cgd 		switch (av[0][1])
    200  1.1       cgd 		{
    201  1.1       cgd 		  case 'a':	/* append to log file */
    202  1.1       cgd 			opencode = 'a';
    203  1.1       cgd 			break;
    204  1.1       cgd 
    205  1.1       cgd 		  case 'f':	/* set fast mode */
    206  1.1       cgd 			Etc.fast++;
    207  1.1       cgd 			break;
    208  1.1       cgd 
    209  1.1       cgd 		  case 's':	/* set slow mode */
    210  1.1       cgd 			Etc.fast = 0;
    211  1.1       cgd 			break;
    212  1.1       cgd 
    213  1.1       cgd #		ifdef xTRACE
    214  1.1       cgd 		  case 't':	/* trace */
    215  1.1       cgd 			if (getuid() != Mother)
    216  1.1       cgd 				goto badflag;
    217  1.1       cgd 			Trace++;
    218  1.1       cgd 			break;
    219  1.1       cgd #		endif
    220  1.1       cgd 
    221  1.1       cgd 		  case 'p':	/* set priority */
    222  1.1       cgd 			if (getuid() != Mother)
    223  1.1       cgd 				goto badflag;
    224  1.1       cgd 			prio = atoi(av[0] + 2);
    225  1.1       cgd 			break;
    226  1.1       cgd 
    227  1.1       cgd 		  default:
    228  1.1       cgd 		  badflag:
    229  1.1       cgd 			printf("Invalid option: %s\n", av[0]);
    230  1.1       cgd 
    231  1.1       cgd 		}
    232  1.1       cgd 		ac--;
    233  1.1       cgd 		av++;
    234  1.1       cgd 	}
    235  1.1       cgd 	if (ac > 2)
    236  1.5  christos 		errx(1, "arg count");
    237  1.1       cgd 		/*
    238  1.1       cgd 	if (ac > 1)
    239  1.1       cgd 		f_log = fopen(av[0], opencode);
    240  1.1       cgd 		*/
    241  1.1       cgd 
    242  1.1       cgd 	printf("\n   * * *   S T A R   T R E K   * * *\n\nPress return to continue.\n");
    243  1.1       cgd 
    244  1.1       cgd 	if (setjmp(env))
    245  1.1       cgd 	{
    246  1.1       cgd 		if ( !getynpar("Another game") )
    247  1.1       cgd 			exit(0);
    248  1.1       cgd 	}
    249  1.1       cgd 	do
    250  1.1       cgd 	{
    251  1.1       cgd 		setup();
    252  1.1       cgd 		play();
    253  1.1       cgd 	} while (getynpar("Another game"));
    254  1.1       cgd 
    255  1.1       cgd 	fflush(stdout);
    256  1.5  christos 	return 0;
    257  1.1       cgd }
    258