main.c 7.9 KB

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