hack.main.c revision 1.11 1 /* $NetBSD: hack.main.c,v 1.11 2009/06/07 18:30:39 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 * Amsterdam
6 * 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 are
10 * met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.main.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
67 #endif /* not lint */
68
69 #include <signal.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <fcntl.h>
73 #include "hack.h"
74 #include "extern.h"
75
76 #ifdef QUEST
77 #define gamename "quest"
78 #else
79 #define gamename "hack"
80 #endif
81
82 int (*afternmv)(void);
83 int (*occupation)(void);
84 const char *occtxt; /* defined when occupation != NULL */
85
86 int hackpid; /* current pid */
87 int locknum; /* max num of players */
88 #ifdef DEF_PAGER
89 const char *catmore; /* default pager */
90 #endif
91 char SAVEF[PL_NSIZ + 11] = "save/"; /* save/99999player */
92 char *hname; /* name of the game (argv[0] of call) */
93 char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
94
95 int main(int, char *[]);
96 static void chdirx(const char *, boolean);
97
98 int
99 main(int argc, char *argv[])
100 {
101 int fd;
102 #ifdef CHDIR
103 char *dir;
104 #endif
105
106 /* Check for dirty tricks with closed fds 0, 1, 2 */
107 fd = open("/dev/null", O_RDONLY);
108 if (fd < 3)
109 exit(1);
110 close(fd);
111
112 hname = argv[0];
113 hackpid = getpid();
114
115 #ifdef CHDIR /* otherwise no chdir() */
116 /*
117 * See if we must change directory to the playground.
118 * (Perhaps hack runs suid and playground is inaccessible
119 * for the player.)
120 * The environment variable HACKDIR is overridden by a
121 * -d command line option (must be the first option given)
122 */
123
124 dir = getenv("HACKDIR");
125 if (argc > 1 && !strncmp(argv[1], "-d", 2)) {
126 argc--;
127 argv++;
128 dir = argv[0] + 2;
129 if (*dir == '=' || *dir == ':')
130 dir++;
131 if (!*dir && argc > 1) {
132 argc--;
133 argv++;
134 dir = argv[0];
135 }
136 if (!*dir)
137 error("Flag -d must be followed by a directory name.");
138 }
139 #endif
140
141 /*
142 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
143 * 2. Use $USER or $LOGNAME (if 1. fails)
144 * 3. Use getlogin() (if 2. fails)
145 * The resulting name is overridden by command line options.
146 * If everything fails, or if the resulting name is some generic
147 * account like "games", "play", "player", "hack" then eventually
148 * we'll ask him.
149 * Note that we trust him here; it is possible to play under
150 * somebody else's name.
151 */
152 {
153 char *s;
154
155 initoptions();
156 if (!*plname && (s = getenv("USER")))
157 (void) strncpy(plname, s, sizeof(plname) - 1);
158 if (!*plname && (s = getenv("LOGNAME")))
159 (void) strncpy(plname, s, sizeof(plname) - 1);
160 if (!*plname && (s = getlogin()))
161 (void) strncpy(plname, s, sizeof(plname) - 1);
162 }
163
164 /*
165 * Now we know the directory containing 'record' and
166 * may do a prscore().
167 */
168 if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
169 #ifdef CHDIR
170 chdirx(dir, 0);
171 #endif
172 prscore(argc, argv);
173 exit(0);
174 }
175 /*
176 * It seems he really wants to play.
177 * Remember tty modes, to be restored on exit.
178 */
179 gettty();
180 setbuf(stdout, obuf);
181 setrandom();
182 startup();
183 cls();
184 u.uhp = 1; /* prevent RIP on early quits */
185 u.ux = FAR; /* prevent nscr() */
186 (void) signal(SIGHUP, hangup);
187
188 /*
189 * Find the creation date of this game,
190 * so as to avoid restoring outdated savefiles.
191 */
192 gethdate(hname);
193
194 /*
195 * We cannot do chdir earlier, otherwise gethdate will fail.
196 */
197 #ifdef CHDIR
198 chdirx(dir, 1);
199 #endif
200
201 /*
202 * Process options.
203 */
204 while (argc > 1 && argv[1][0] == '-') {
205 argv++;
206 argc--;
207 switch (argv[0][1]) {
208 #ifdef WIZARD
209 case 'D':
210 /* if(!strcmp(getlogin(), WIZARD)) */
211 wizard = TRUE;
212 /*
213 * else printf("Sorry.\n");
214 */
215 break;
216 #endif
217 #ifdef NEWS
218 case 'n':
219 flags.nonews = TRUE;
220 break;
221 #endif
222 case 'u':
223 if (argv[0][2])
224 (void) strncpy(plname, argv[0] + 2, sizeof(plname) - 1);
225 else if (argc > 1) {
226 argc--;
227 argv++;
228 (void) strncpy(plname, argv[0], sizeof(plname) - 1);
229 } else
230 printf("Player name expected after -u\n");
231 break;
232 default:
233 /* allow -T for Tourist, etc. */
234 (void) strncpy(pl_character, argv[0] + 1,
235 sizeof(pl_character) - 1);
236
237 /* printf("Unknown option: %s\n", *argv); */
238 }
239 }
240
241 if (argc > 1)
242 locknum = atoi(argv[1]);
243 #ifdef MAX_NR_OF_PLAYERS
244 if (!locknum || locknum > MAX_NR_OF_PLAYERS)
245 locknum = MAX_NR_OF_PLAYERS;
246 #endif
247 #ifdef DEF_PAGER
248 if (((catmore = getenv("HACKPAGER")) == NULL &&
249 (catmore = getenv("PAGER")) == NULL) ||
250 catmore[0] == '\0')
251 catmore = DEF_PAGER;
252 #endif
253 #ifdef MAIL
254 getmailstatus();
255 #endif
256 #ifdef WIZARD
257 if (wizard)
258 (void) strcpy(plname, "wizard");
259 else
260 #endif
261 if (!*plname || !strncmp(plname, "player", 4)
262 || !strncmp(plname, "games", 4))
263 askname();
264 plnamesuffix(); /* strip suffix from name; calls askname() */
265 /* again if suffix was whole name */
266 /* accepts any suffix */
267 #ifdef WIZARD
268 if (!wizard) {
269 #endif
270 /*
271 * check for multiple games under the same name
272 * (if !locknum) or check max nr of players (otherwise)
273 */
274 (void) signal(SIGQUIT, SIG_IGN);
275 (void) signal(SIGINT, SIG_IGN);
276 if (!locknum)
277 (void) strcpy(lock, plname);
278 getlock(); /* sets lock if locknum != 0 */
279 #ifdef WIZARD
280 } else {
281 char *sfoo;
282 (void) strcpy(lock, plname);
283 if ((sfoo = getenv("MAGIC")) != NULL)
284 while (*sfoo) {
285 switch (*sfoo++) {
286 case 'n':
287 (void) srandom(*sfoo++);
288 break;
289 }
290 }
291 if ((sfoo = getenv("GENOCIDED")) != NULL) {
292 if (*sfoo == '!') {
293 const struct permonst *pm = mons;
294 char *gp = genocided;
295
296 while (pm < mons + CMNUM + 2) {
297 if (!strchr(sfoo, pm->mlet))
298 *gp++ = pm->mlet;
299 pm++;
300 }
301 *gp = 0;
302 } else
303 (void) strcpy(genocided, sfoo);
304 (void) strcpy(fut_geno, genocided);
305 }
306 }
307 #endif
308 setftty();
309 (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
310 regularize(SAVEF + 5); /* avoid . or / in name */
311 if ((fd = open(SAVEF, O_RDONLY)) >= 0 &&
312 (uptodate(fd) || unlink(SAVEF) == 666)) {
313 (void) signal(SIGINT, done1);
314 pline("Restoring old save file...");
315 (void) fflush(stdout);
316 if (!dorecover(fd))
317 goto not_recovered;
318 pline("Hello %s, welcome to %s!", plname, gamename);
319 flags.move = 0;
320 } else {
321 not_recovered:
322 fobj = fcobj = invent = 0;
323 fmon = fallen_down = 0;
324 ftrap = 0;
325 fgold = 0;
326 flags.ident = 1;
327 init_objects();
328 u_init();
329
330 (void) signal(SIGINT, done1);
331 mklev();
332 u.ux = xupstair;
333 u.uy = yupstair;
334 (void) inshop();
335 setsee();
336 flags.botlx = 1;
337 makedog();
338 {
339 struct monst *mtmp;
340 if ((mtmp = m_at(u.ux, u.uy)) != NULL)
341 mnexto(mtmp); /* riv05!a3 */
342 }
343 seemons();
344 #ifdef NEWS
345 if (flags.nonews || !readnews())
346 /* after reading news we did docrt() already */
347 #endif
348 docrt();
349
350 /* give welcome message before pickup messages */
351 pline("Hello %s, welcome to %s!", plname, gamename);
352
353 pickup(1);
354 read_engr_at(u.ux, u.uy);
355 flags.move = 1;
356 }
357
358 flags.moonphase = phase_of_the_moon();
359 if (flags.moonphase == FULL_MOON) {
360 pline("You are lucky! Full moon tonight.");
361 u.uluck++;
362 } else if (flags.moonphase == NEW_MOON) {
363 pline("Be careful! New moon tonight.");
364 }
365 initrack();
366
367 for (;;) {
368 if (flags.move) { /* actual time passed */
369
370 settrack();
371
372 if (moves % 2 == 0 ||
373 (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
374 movemon();
375 if (!rn2(70))
376 (void) makemon((struct permonst *) 0, 0, 0);
377 }
378 if (Glib)
379 glibr();
380 timeout();
381 ++moves;
382 if (flags.time)
383 flags.botl = 1;
384 if (u.uhp < 1) {
385 pline("You die...");
386 done("died");
387 }
388 if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) {
389 wailmsg = moves;
390 if (u.uhp == 1)
391 pline("You hear the wailing of the Banshee...");
392 else
393 pline("You hear the howling of the CwnAnnwn...");
394 }
395 if (u.uhp < u.uhpmax) {
396 if (u.ulevel > 9) {
397 if (Regeneration || !(moves % 3)) {
398 flags.botl = 1;
399 u.uhp += rnd((int) u.ulevel - 9);
400 if (u.uhp > u.uhpmax)
401 u.uhp = u.uhpmax;
402 }
403 } else if (Regeneration ||
404 (!(moves % (22 - u.ulevel * 2)))) {
405 flags.botl = 1;
406 u.uhp++;
407 }
408 }
409 if (Teleportation && !rn2(85))
410 tele();
411 if (Searching && multi >= 0)
412 (void) dosearch();
413 gethungry();
414 invault();
415 amulet();
416 }
417 if (multi < 0) {
418 if (!++multi) {
419 pline(nomovemsg ? nomovemsg :
420 "You can move again.");
421 nomovemsg = 0;
422 if (afternmv)
423 (*afternmv) ();
424 afternmv = 0;
425 }
426 }
427 find_ac();
428 #ifndef QUEST
429 if (!flags.mv || Blind)
430 #endif
431 {
432 seeobjs();
433 seemons();
434 nscr();
435 }
436 if (flags.botl || flags.botlx)
437 bot();
438
439 flags.move = 1;
440
441 if (multi >= 0 && occupation) {
442 if (monster_nearby())
443 stop_occupation();
444 else if ((*occupation) () == 0)
445 occupation = 0;
446 continue;
447 }
448 if (multi > 0) {
449 #ifdef QUEST
450 if (flags.run >= 4)
451 finddir();
452 #endif
453 lookaround();
454 if (!multi) { /* lookaround may clear multi */
455 flags.move = 0;
456 continue;
457 }
458 if (flags.mv) {
459 if (multi < COLNO && !--multi)
460 flags.mv = flags.run = 0;
461 domove();
462 } else {
463 --multi;
464 rhack(save_cm);
465 }
466 } else if (multi == 0) {
467 #ifdef MAIL
468 ckmailstatus();
469 #endif
470 rhack((char *) 0);
471 }
472 if (multi && multi % 7 == 0)
473 (void) fflush(stdout);
474 }
475 }
476
477 void
478 glo(int foo)
479 {
480 /* construct the string xlock.n */
481 char *tf;
482
483 tf = lock;
484 while (*tf && *tf != '.')
485 tf++;
486 (void) sprintf(tf, ".%d", foo);
487 }
488
489 /*
490 * plname is filled either by an option (-u Player or -uPlayer) or
491 * explicitly (-w implies wizard) or by askname.
492 * It may still contain a suffix denoting pl_character.
493 */
494 void
495 askname(void)
496 {
497 int c, ct;
498 printf("\nWho are you? ");
499 (void) fflush(stdout);
500 ct = 0;
501 while ((c = getchar()) != '\n') {
502 if (c == EOF)
503 error("End of input\n");
504 /* some people get confused when their erase char is not ^H */
505 if (c == '\010') {
506 if (ct)
507 ct--;
508 continue;
509 }
510 if (c != '-')
511 if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
512 c = '_';
513 if (ct < (int)sizeof(plname) - 1)
514 plname[ct++] = c;
515 }
516 plname[ct] = 0;
517 if (ct == 0)
518 askname();
519 }
520
521 /* VARARGS1 */
522 void
523 impossible(const char *s, ...)
524 {
525 va_list ap;
526
527 va_start(ap, s);
528 vpline(s, ap);
529 va_end(ap);
530 pline("Program in disorder - perhaps you'd better Quit.");
531 }
532
533 #ifdef CHDIR
534 static void
535 chdirx(const char *dir, boolean wr)
536 {
537
538 #ifdef SECURE
539 if (dir /* User specified directory? */
540 #ifdef HACKDIR
541 && strcmp(dir, HACKDIR) /* and not the default? */
542 #endif
543 ) {
544 (void) setuid(getuid()); /* Ron Wessels */
545 (void) setgid(getgid());
546 }
547 #endif
548
549 #ifdef HACKDIR
550 if (dir == NULL)
551 dir = HACKDIR;
552 #endif
553
554 if (dir && chdir(dir) < 0) {
555 perror(dir);
556 error("Cannot chdir to %s.", dir);
557 }
558 /* warn the player if he cannot write the record file */
559 /* perhaps we should also test whether . is writable */
560 /* unfortunately the access systemcall is worthless */
561 if (wr) {
562 int fd;
563
564 if (dir == NULL)
565 dir = ".";
566 if ((fd = open(RECORD, O_RDWR)) < 0) {
567 printf("Warning: cannot write %s/%s", dir, RECORD);
568 getret();
569 } else
570 (void) close(fd);
571 }
572 }
573 #endif
574
575 void
576 stop_occupation(void)
577 {
578 if (occupation) {
579 pline("You stop %s.", occtxt);
580 occupation = 0;
581 }
582 }
583