1*61357867Sdholland /* $NetBSD: main.c,v 1.17 2009/05/24 22:55:03 dholland 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. 15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors 1661f28255Scgd * may be used to endorse or promote products derived from this software 1761f28255Scgd * without specific prior written permission. 1861f28255Scgd * 1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2961f28255Scgd * SUCH DAMAGE. 3061f28255Scgd */ 3161f28255Scgd 32ef383c95Schristos #include <sys/cdefs.h> 3361f28255Scgd #ifndef lint 342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\ 352fe2731dSlukem The Regents of the University of California. All rights reserved."); 3661f28255Scgd #endif /* not lint */ 3761f28255Scgd 3861f28255Scgd #ifndef lint 39887dd216Scgd #if 0 40887dd216Scgd static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; 41887dd216Scgd #else 42*61357867Sdholland __RCSID("$NetBSD: main.c,v 1.17 2009/05/24 22:55:03 dholland Exp $"); 43887dd216Scgd #endif 4461f28255Scgd #endif /* not lint */ 4561f28255Scgd 4661f28255Scgd #include <stdio.h> 4761f28255Scgd #include <setjmp.h> 4867bd328aSjtc #include <termios.h> 49ef383c95Schristos #include <stdlib.h> 50ef383c95Schristos #include <unistd.h> 51ef383c95Schristos #include <err.h> 5263b3971aScjs #include <time.h> 5320e33050Sjsm #include <sys/types.h> 54ef383c95Schristos #include "trek.h" 55ef383c95Schristos #include "getpar.h" 5661f28255Scgd 5761f28255Scgd #define PRIO 00 /* default priority */ 5861f28255Scgd 5920e33050Sjsm uid_t Mother = 51 + (51 << 8); 6061f28255Scgd 6161f28255Scgd /* 6261f28255Scgd ** #### ##### # #### ##### #### ##### # # 6361f28255Scgd ** # # # # # # # # # # # # 6461f28255Scgd ** ### # ##### #### # #### ### ### 6561f28255Scgd ** # # # # # # # # # # # # 6661f28255Scgd ** #### # # # # # # # # ##### # # 6761f28255Scgd ** 6861f28255Scgd ** C version by Eric P. Allman 5/76 (U.C. Berkeley) with help 6961f28255Scgd ** from Jeff Poskanzer and Pete Rubinstein. 7061f28255Scgd ** 7161f28255Scgd ** I also want to thank everyone here at Berkeley who 7261f28255Scgd ** where crazy enough to play the undebugged game. I want to 7361f28255Scgd ** particularly thank Nick Whyte, who made considerable 7461f28255Scgd ** suggestions regarding the content of the game. Why, I'll 7561f28255Scgd ** never forget the time he suggested the name for the 7661f28255Scgd ** "capture" command. 7761f28255Scgd ** 7861f28255Scgd ** Please send comments, questions, and suggestions about this 7961f28255Scgd ** game to: 8061f28255Scgd ** Eric P. Allman 8161f28255Scgd ** Project INGRES 8261f28255Scgd ** Electronics Research Laboratory 8361f28255Scgd ** Cory Hall 8461f28255Scgd ** University of California 8561f28255Scgd ** Berkeley, California 94720 8661f28255Scgd ** 8761f28255Scgd ** If you make ANY changes in the game, I sure would like to 8861f28255Scgd ** know about them. It is sort of an ongoing project for me, 8961f28255Scgd ** and I very much want to put in any bug fixes and improvements 9061f28255Scgd ** that you might come up with. 9161f28255Scgd ** 9261f28255Scgd ** FORTRASH version by Kay R. Fisher (DEC) "and countless others". 9361f28255Scgd ** That was adapted from the "original BASIC program" (ha!) by 9461f28255Scgd ** Mike Mayfield (Centerline Engineering). 9561f28255Scgd ** 9661f28255Scgd ** Additional inspiration taken from FORTRAN version by 9761f28255Scgd ** David Matuszek and Paul Reynolds which runs on the CDC 9861f28255Scgd ** 7600 at Lawrence Berkeley Lab, maintained there by 9961f28255Scgd ** Andy Davidson. This version is also available at LLL 10061f28255Scgd ** and at LMSC. In all fairness, this version was the 10161f28255Scgd ** major inspiration for this version of the game (trans- 10261f28255Scgd ** lation: I ripped off a whole lot of code). 10361f28255Scgd ** 10461f28255Scgd ** Minor other input from the "Battelle Version 7A" by Joe Miller 10561f28255Scgd ** (Graphics Systems Group, Battelle-Columbus Labs) and 10661f28255Scgd ** Ross Pavlac (Systems Programmer, Battelle Memorial 10761f28255Scgd ** Institute). That version was written in December '74 10861f28255Scgd ** and extensively modified June '75. It was adapted 10961f28255Scgd ** from the FTN version by Ron Williams of CDC Sunnyvale, 11061f28255Scgd ** which was adapted from the Basic version distributed 11161f28255Scgd ** by DEC. It also had "neat stuff swiped" from T. T. 11261f28255Scgd ** Terry and Jim Korp (University of Texas), Hicks (Penn 11361f28255Scgd ** U.), and Rick Maus (Georgia Tech). Unfortunately, it 11461f28255Scgd ** was not as readable as it could have been and so the 11561f28255Scgd ** translation effort was severely hampered. None the 11661f28255Scgd ** less, I got the idea of inhabited starsystems from this 11761f28255Scgd ** version. 11861f28255Scgd ** 11961f28255Scgd ** Permission is given for use, copying, and modification of 12061f28255Scgd ** all or part of this program and related documentation, 12161f28255Scgd ** provided that all reference to the authors are maintained. 12261f28255Scgd ** 12361f28255Scgd ** 12461f28255Scgd ********************************************************************** 12561f28255Scgd ** 12661f28255Scgd ** NOTES TO THE MAINTAINER: 12761f28255Scgd ** 12861f28255Scgd ** There is a compilation option xTRACE which must be set for any 12961f28255Scgd ** trace information to be generated. It is probably defined in 13061f28255Scgd ** the version that you get. It can be removed, however, if you 13161f28255Scgd ** have trouble finding room in core. 13261f28255Scgd ** 13361f28255Scgd ** Many things in trek are not as clear as they might be, but are 13461f28255Scgd ** done to reduce space. I compile with the -f and -O flags. I 135a9356936Swiz ** am constrained to running with non-separated I/D space, since 13661f28255Scgd ** we don't have doubleing point hardware here; even if we did, I 13761f28255Scgd ** would like trek to be available to the large number of people 13861f28255Scgd ** who either have an 11/40 or do not have FP hardware. I also 13961f28255Scgd ** found it desirable to make the code run reentrant, so this 14061f28255Scgd ** added even more space constraints. 14161f28255Scgd ** 14261f28255Scgd ** I use the portable C library to do my I/O. This is done be- 14361f28255Scgd ** cause I wanted the game easily transportable to other C 14461f28255Scgd ** implementations, and because I was too lazy to do the doubleing 14561f28255Scgd ** point input myself. Little did I know. The portable C library 14661f28255Scgd ** released by Bell Labs has more bugs than you would believe, so 14761f28255Scgd ** I ended up rewriting the whole blessed thing. Trek excercises 14861f28255Scgd ** many of the bugs in it, as well as bugs in some of the section 14961f28255Scgd ** III UNIX routines. We have fixed them here. One main problem 15061f28255Scgd ** was a bug in alloc() that caused it to always ask for a large 15161f28255Scgd ** hunk of memory, which worked fine unless you were almost out, 15261f28255Scgd ** which I inevitably was. If you want the code for all of this 15361f28255Scgd ** stuff, it is also available through me. 15461f28255Scgd ** 15561f28255Scgd *********************************************************************** 15661f28255Scgd */ 15761f28255Scgd 15861f28255Scgd jmp_buf env; 15961f28255Scgd 160cb5fd834Sjsm int main(int, char **); 161ef383c95Schristos 162ef383c95Schristos int 163bf0917b6Sdholland main(int argc, char **argv) 16461f28255Scgd { 16563b3971aScjs time_t curtime; 16661f28255Scgd long vect; 167ef383c95Schristos char opencode; 16861f28255Scgd int prio; 169ef383c95Schristos int ac; 170ef383c95Schristos char **av; 17167bd328aSjtc struct termios argp; 17261f28255Scgd 1735367f340Sjsm /* Revoke setgid privileges */ 174f9eca697Smycroft setgid(getgid()); 1755367f340Sjsm 17661f28255Scgd av = argv; 17761f28255Scgd ac = argc; 17861f28255Scgd av++; 17963b3971aScjs time(&curtime); 18063b3971aScjs vect = (long) curtime; 18161f28255Scgd srand(vect); 18261f28255Scgd opencode = 'w'; 18361f28255Scgd prio = PRIO; 18467bd328aSjtc 18550b862e2Sdholland if (tcgetattr(1, &argp) == 0) { 18667bd328aSjtc if (cfgetispeed(&argp) < B1200) 18761f28255Scgd Etc.fast++; 18861f28255Scgd } 18967bd328aSjtc 19050b862e2Sdholland while (ac > 1 && av[0][0] == '-') { 19150b862e2Sdholland switch (av[0][1]) { 19261f28255Scgd case 'a': /* append to log file */ 19361f28255Scgd opencode = 'a'; 19461f28255Scgd break; 19561f28255Scgd 19661f28255Scgd case 'f': /* set fast mode */ 19761f28255Scgd Etc.fast++; 19861f28255Scgd break; 19961f28255Scgd 20061f28255Scgd case 's': /* set slow mode */ 20161f28255Scgd Etc.fast = 0; 20261f28255Scgd break; 20361f28255Scgd 20461f28255Scgd #ifdef xTRACE 20561f28255Scgd case 't': /* trace */ 20661f28255Scgd if (getuid() != Mother) 20761f28255Scgd goto badflag; 20861f28255Scgd Trace++; 20961f28255Scgd break; 21061f28255Scgd #endif 21161f28255Scgd 21261f28255Scgd case 'p': /* set priority */ 21361f28255Scgd if (getuid() != Mother) 21461f28255Scgd goto badflag; 21561f28255Scgd prio = atoi(av[0] + 2); 21661f28255Scgd break; 21761f28255Scgd 21861f28255Scgd default: 21961f28255Scgd badflag: 22061f28255Scgd printf("Invalid option: %s\n", av[0]); 22161f28255Scgd 22261f28255Scgd } 22361f28255Scgd ac--; 22461f28255Scgd av++; 22561f28255Scgd } 22661f28255Scgd if (ac > 2) 227ef383c95Schristos errx(1, "arg count"); 22861f28255Scgd /* 22961f28255Scgd if (ac > 1) 23061f28255Scgd f_log = fopen(av[0], opencode); 23161f28255Scgd */ 23261f28255Scgd 233*61357867Sdholland printf("\n * * * S T A R T R E K * * *\n\n" 234*61357867Sdholland "Press return to continue.\n"); 23561f28255Scgd 23650b862e2Sdholland if (setjmp(env)) { 23761f28255Scgd if ( !getynpar("Another game") ) 23861f28255Scgd exit(0); 23961f28255Scgd } 24050b862e2Sdholland 24150b862e2Sdholland do { 24261f28255Scgd setup(); 24361f28255Scgd play(); 24461f28255Scgd } while (getynpar("Another game")); 24561f28255Scgd 24661f28255Scgd fflush(stdout); 247ef383c95Schristos return 0; 24861f28255Scgd } 249