1 /* $OpenBSD: main.c,v 1.16 2005/05/01 02:43:12 djm Exp $ */ 2 /* $NetBSD: main.c,v 1.5 1995/04/22 10:08:54 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1980, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; 42 #else 43 static char rcsid[] = "$OpenBSD: main.c,v 1.16 2005/05/01 02:43:12 djm Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include "robots.h" 48 49 void 50 usage(void) 51 { 52 fprintf(stderr, "usage: robots [-ajrst] [scorefile]\n"); 53 exit(1); 54 } 55 56 int 57 main(int ac, char *av[]) 58 { 59 bool show_only; 60 extern char *Scorefile; 61 int score_wfd; /* high score writable file descriptor */ 62 int score_err = 0; /* hold errno from score file open */ 63 int ch; 64 extern int optind; 65 gid_t gid; 66 #ifdef FANCY 67 char *sp; 68 #endif 69 70 if ((score_wfd = open(Scorefile, O_RDWR)) < 0) 71 score_err = errno; 72 73 /* revoke privs */ 74 gid = getgid(); 75 setresgid(gid, gid, gid); 76 77 show_only = FALSE; 78 while ((ch = getopt(ac, av, "srajt")) != -1) 79 switch (ch) { 80 case 's': 81 show_only = TRUE; 82 break; 83 case 'r': 84 Real_time = TRUE; 85 /* Could be a command-line option */ 86 tv.tv_sec = 3; 87 tv.tv_usec = 0; 88 FD_ZERO(&rset); 89 break; 90 case 'a': 91 Start_level = 4; 92 break; 93 case 'j': 94 Jump = TRUE; 95 break; 96 case 't': 97 Teleport = TRUE; 98 break; 99 case '?': 100 default: 101 usage(); 102 } 103 ac -= optind; 104 av += optind; 105 106 if (ac > 1) 107 usage(); 108 if (ac == 1) { 109 Scorefile = av[0]; 110 if (score_wfd >= 0) 111 close(score_wfd); 112 /* This file requires no special privileges. */ 113 if ((score_wfd = open(Scorefile, O_RDWR)) < 0) 114 score_err = errno; 115 #ifdef FANCY 116 sp = strrchr(Scorefile, '/'); 117 if (sp == NULL) 118 sp = Scorefile; 119 if (strcmp(sp, "pattern_roll") == 0) 120 Pattern_roll = TRUE; 121 else if (strcmp(sp, "stand_still") == 0) 122 Stand_still = TRUE; 123 if (Pattern_roll || Stand_still) 124 Teleport = TRUE; 125 #endif 126 } 127 128 if (show_only) { 129 show_score(); 130 exit(0); 131 } 132 133 if (score_wfd < 0) { 134 warnx("%s: %s; no scores will be saved", Scorefile, 135 strerror(score_err)); 136 sleep(1); 137 } 138 139 initscr(); 140 signal(SIGINT, quit); 141 cbreak(); 142 noecho(); 143 nonl(); 144 if (LINES != Y_SIZE || COLS != X_SIZE) { 145 if (LINES < Y_SIZE || COLS < X_SIZE) { 146 endwin(); 147 errx(1, "Need at least a %dx%d screen", Y_SIZE, X_SIZE); 148 } 149 delwin(stdscr); 150 stdscr = newwin(Y_SIZE, X_SIZE, 0, 0); 151 } 152 153 srandomdev(); 154 do { 155 init_field(); 156 for (Level = Start_level; !Dead; Level++) { 157 make_level(); 158 play_level(); 159 } 160 move(My_pos.y, My_pos.x); 161 printw("AARRrrgghhhh...."); 162 refresh(); 163 score(score_wfd); 164 } while (another()); 165 quit(0); 166 /* NOT REACHED */ 167 } 168 169 /* 170 * quit: 171 * Leave the program elegantly. 172 */ 173 void 174 quit(int dummy) 175 { 176 endwin(); 177 exit(0); 178 } 179 180 /* 181 * another: 182 * See if another game is desired 183 */ 184 bool 185 another(void) 186 { 187 int y; 188 189 #ifdef FANCY 190 if ((Stand_still || Pattern_roll) && !Newscore) 191 return TRUE; 192 #endif 193 194 if (query("Another game?")) { 195 if (Full_clear) { 196 for (y = 1; y <= Num_scores; y++) { 197 move(y, 1); 198 clrtoeol(); 199 } 200 refresh(); 201 } 202 return TRUE; 203 } 204 return FALSE; 205 } 206