1*a9356936Swiz /* $NetBSD: main.c,v 1.10 2001/07/22 13:34:01 wiz Exp $ */ 2887dd216Scgd 361f28255Scgd /* 4887dd216Scgd * Copyright (c) 1980, 1993 5887dd216Scgd * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * Redistribution and use in source and binary forms, with or without 861f28255Scgd * modification, are permitted provided that the following conditions 961f28255Scgd * are met: 1061f28255Scgd * 1. Redistributions of source code must retain the above copyright 1161f28255Scgd * notice, this list of conditions and the following disclaimer. 1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1361f28255Scgd * notice, this list of conditions and the following disclaimer in the 1461f28255Scgd * documentation and/or other materials provided with the distribution. 1561f28255Scgd * 3. All advertising materials mentioning features or use of this software 1661f28255Scgd * must display the following acknowledgement: 1761f28255Scgd * This product includes software developed by the University of 1861f28255Scgd * California, Berkeley and its contributors. 1961f28255Scgd * 4. Neither the name of the University nor the names of its contributors 2061f28255Scgd * may be used to endorse or promote products derived from this software 2161f28255Scgd * without specific prior written permission. 2261f28255Scgd * 2361f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2461f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2561f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2661f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2761f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2861f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2961f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3061f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3161f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3261f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3361f28255Scgd * SUCH DAMAGE. 3461f28255Scgd */ 3561f28255Scgd 36ef383c95Schristos #include <sys/cdefs.h> 3761f28255Scgd #ifndef lint 38ef383c95Schristos __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 39ef383c95Schristos The Regents of the University of California. All rights reserved.\n"); 4061f28255Scgd #endif /* not lint */ 4161f28255Scgd 4261f28255Scgd #ifndef lint 43887dd216Scgd #if 0 44887dd216Scgd static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; 45887dd216Scgd #else 46*a9356936Swiz __RCSID("$NetBSD: main.c,v 1.10 2001/07/22 13:34:01 wiz Exp $"); 47887dd216Scgd #endif 4861f28255Scgd #endif /* not lint */ 4961f28255Scgd 5061f28255Scgd #include <stdio.h> 5161f28255Scgd #include <setjmp.h> 5267bd328aSjtc #include <termios.h> 53ef383c95Schristos #include <stdlib.h> 54ef383c95Schristos #include <unistd.h> 55ef383c95Schristos #include <err.h> 5663b3971aScjs #include <time.h> 5720e33050Sjsm #include <sys/types.h> 58ef383c95Schristos #include "trek.h" 59ef383c95Schristos #include "getpar.h" 6061f28255Scgd 6161f28255Scgd # define PRIO 00 /* default priority */ 6261f28255Scgd 6320e33050Sjsm uid_t Mother = 51 + (51 << 8); 6461f28255Scgd 6561f28255Scgd /* 6661f28255Scgd ** #### ##### # #### ##### #### ##### # # 6761f28255Scgd ** # # # # # # # # # # # # 6861f28255Scgd ** ### # ##### #### # #### ### ### 6961f28255Scgd ** # # # # # # # # # # # # 7061f28255Scgd ** #### # # # # # # # # ##### # # 7161f28255Scgd ** 7261f28255Scgd ** C version by Eric P. Allman 5/76 (U.C. Berkeley) with help 7361f28255Scgd ** from Jeff Poskanzer and Pete Rubinstein. 7461f28255Scgd ** 7561f28255Scgd ** I also want to thank everyone here at Berkeley who 7661f28255Scgd ** where crazy enough to play the undebugged game. I want to 7761f28255Scgd ** particularly thank Nick Whyte, who made considerable 7861f28255Scgd ** suggestions regarding the content of the game. Why, I'll 7961f28255Scgd ** never forget the time he suggested the name for the 8061f28255Scgd ** "capture" command. 8161f28255Scgd ** 8261f28255Scgd ** Please send comments, questions, and suggestions about this 8361f28255Scgd ** game to: 8461f28255Scgd ** Eric P. Allman 8561f28255Scgd ** Project INGRES 8661f28255Scgd ** Electronics Research Laboratory 8761f28255Scgd ** Cory Hall 8861f28255Scgd ** University of California 8961f28255Scgd ** Berkeley, California 94720 9061f28255Scgd ** 9161f28255Scgd ** If you make ANY changes in the game, I sure would like to 9261f28255Scgd ** know about them. It is sort of an ongoing project for me, 9361f28255Scgd ** and I very much want to put in any bug fixes and improvements 9461f28255Scgd ** that you might come up with. 9561f28255Scgd ** 9661f28255Scgd ** FORTRASH version by Kay R. Fisher (DEC) "and countless others". 9761f28255Scgd ** That was adapted from the "original BASIC program" (ha!) by 9861f28255Scgd ** Mike Mayfield (Centerline Engineering). 9961f28255Scgd ** 10061f28255Scgd ** Additional inspiration taken from FORTRAN version by 10161f28255Scgd ** David Matuszek and Paul Reynolds which runs on the CDC 10261f28255Scgd ** 7600 at Lawrence Berkeley Lab, maintained there by 10361f28255Scgd ** Andy Davidson. This version is also available at LLL 10461f28255Scgd ** and at LMSC. In all fairness, this version was the 10561f28255Scgd ** major inspiration for this version of the game (trans- 10661f28255Scgd ** lation: I ripped off a whole lot of code). 10761f28255Scgd ** 10861f28255Scgd ** Minor other input from the "Battelle Version 7A" by Joe Miller 10961f28255Scgd ** (Graphics Systems Group, Battelle-Columbus Labs) and 11061f28255Scgd ** Ross Pavlac (Systems Programmer, Battelle Memorial 11161f28255Scgd ** Institute). That version was written in December '74 11261f28255Scgd ** and extensively modified June '75. It was adapted 11361f28255Scgd ** from the FTN version by Ron Williams of CDC Sunnyvale, 11461f28255Scgd ** which was adapted from the Basic version distributed 11561f28255Scgd ** by DEC. It also had "neat stuff swiped" from T. T. 11661f28255Scgd ** Terry and Jim Korp (University of Texas), Hicks (Penn 11761f28255Scgd ** U.), and Rick Maus (Georgia Tech). Unfortunately, it 11861f28255Scgd ** was not as readable as it could have been and so the 11961f28255Scgd ** translation effort was severely hampered. None the 12061f28255Scgd ** less, I got the idea of inhabited starsystems from this 12161f28255Scgd ** version. 12261f28255Scgd ** 12361f28255Scgd ** Permission is given for use, copying, and modification of 12461f28255Scgd ** all or part of this program and related documentation, 12561f28255Scgd ** provided that all reference to the authors are maintained. 12661f28255Scgd ** 12761f28255Scgd ** 12861f28255Scgd ********************************************************************** 12961f28255Scgd ** 13061f28255Scgd ** NOTES TO THE MAINTAINER: 13161f28255Scgd ** 13261f28255Scgd ** There is a compilation option xTRACE which must be set for any 13361f28255Scgd ** trace information to be generated. It is probably defined in 13461f28255Scgd ** the version that you get. It can be removed, however, if you 13561f28255Scgd ** have trouble finding room in core. 13661f28255Scgd ** 13761f28255Scgd ** Many things in trek are not as clear as they might be, but are 13861f28255Scgd ** done to reduce space. I compile with the -f and -O flags. I 139*a9356936Swiz ** am constrained to running with non-separated I/D space, since 14061f28255Scgd ** we don't have doubleing point hardware here; even if we did, I 14161f28255Scgd ** would like trek to be available to the large number of people 14261f28255Scgd ** who either have an 11/40 or do not have FP hardware. I also 14361f28255Scgd ** found it desirable to make the code run reentrant, so this 14461f28255Scgd ** added even more space constraints. 14561f28255Scgd ** 14661f28255Scgd ** I use the portable C library to do my I/O. This is done be- 14761f28255Scgd ** cause I wanted the game easily transportable to other C 14861f28255Scgd ** implementations, and because I was too lazy to do the doubleing 14961f28255Scgd ** point input myself. Little did I know. The portable C library 15061f28255Scgd ** released by Bell Labs has more bugs than you would believe, so 15161f28255Scgd ** I ended up rewriting the whole blessed thing. Trek excercises 15261f28255Scgd ** many of the bugs in it, as well as bugs in some of the section 15361f28255Scgd ** III UNIX routines. We have fixed them here. One main problem 15461f28255Scgd ** was a bug in alloc() that caused it to always ask for a large 15561f28255Scgd ** hunk of memory, which worked fine unless you were almost out, 15661f28255Scgd ** which I inevitably was. If you want the code for all of this 15761f28255Scgd ** stuff, it is also available through me. 15861f28255Scgd ** 15961f28255Scgd *********************************************************************** 16061f28255Scgd */ 16161f28255Scgd 16261f28255Scgd jmp_buf env; 16361f28255Scgd 164ef383c95Schristos int main __P((int, char **)); 165ef383c95Schristos 166ef383c95Schristos int 16761f28255Scgd main(argc, argv) 16861f28255Scgd int argc; 16961f28255Scgd char **argv; 17061f28255Scgd { 17163b3971aScjs time_t curtime; 17261f28255Scgd long vect; 173ef383c95Schristos char opencode; 17461f28255Scgd int prio; 175ef383c95Schristos int ac; 176ef383c95Schristos char **av; 17767bd328aSjtc struct termios argp; 17861f28255Scgd 1795367f340Sjsm /* Revoke setgid privileges */ 180f9eca697Smycroft setgid(getgid()); 1815367f340Sjsm 18261f28255Scgd av = argv; 18361f28255Scgd ac = argc; 18461f28255Scgd av++; 18563b3971aScjs time(&curtime); 18663b3971aScjs vect = (long) curtime; 18761f28255Scgd srand(vect); 18861f28255Scgd opencode = 'w'; 18961f28255Scgd prio = PRIO; 19067bd328aSjtc 19167bd328aSjtc if (tcgetattr(1, &argp) == 0) 19261f28255Scgd { 19367bd328aSjtc if (cfgetispeed(&argp) < B1200) 19461f28255Scgd Etc.fast++; 19561f28255Scgd } 19667bd328aSjtc 19761f28255Scgd while (ac > 1 && av[0][0] == '-') 19861f28255Scgd { 19961f28255Scgd switch (av[0][1]) 20061f28255Scgd { 20161f28255Scgd case 'a': /* append to log file */ 20261f28255Scgd opencode = 'a'; 20361f28255Scgd break; 20461f28255Scgd 20561f28255Scgd case 'f': /* set fast mode */ 20661f28255Scgd Etc.fast++; 20761f28255Scgd break; 20861f28255Scgd 20961f28255Scgd case 's': /* set slow mode */ 21061f28255Scgd Etc.fast = 0; 21161f28255Scgd break; 21261f28255Scgd 21361f28255Scgd # ifdef xTRACE 21461f28255Scgd case 't': /* trace */ 21561f28255Scgd if (getuid() != Mother) 21661f28255Scgd goto badflag; 21761f28255Scgd Trace++; 21861f28255Scgd break; 21961f28255Scgd # endif 22061f28255Scgd 22161f28255Scgd case 'p': /* set priority */ 22261f28255Scgd if (getuid() != Mother) 22361f28255Scgd goto badflag; 22461f28255Scgd prio = atoi(av[0] + 2); 22561f28255Scgd break; 22661f28255Scgd 22761f28255Scgd default: 22861f28255Scgd badflag: 22961f28255Scgd printf("Invalid option: %s\n", av[0]); 23061f28255Scgd 23161f28255Scgd } 23261f28255Scgd ac--; 23361f28255Scgd av++; 23461f28255Scgd } 23561f28255Scgd if (ac > 2) 236ef383c95Schristos errx(1, "arg count"); 23761f28255Scgd /* 23861f28255Scgd if (ac > 1) 23961f28255Scgd f_log = fopen(av[0], opencode); 24061f28255Scgd */ 24161f28255Scgd 24261f28255Scgd printf("\n * * * S T A R T R E K * * *\n\nPress return to continue.\n"); 24361f28255Scgd 24461f28255Scgd if (setjmp(env)) 24561f28255Scgd { 24661f28255Scgd if ( !getynpar("Another game") ) 24761f28255Scgd exit(0); 24861f28255Scgd } 24961f28255Scgd do 25061f28255Scgd { 25161f28255Scgd setup(); 25261f28255Scgd play(); 25361f28255Scgd } while (getynpar("Another game")); 25461f28255Scgd 25561f28255Scgd fflush(stdout); 256ef383c95Schristos return 0; 25761f28255Scgd } 258