forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.c
2298 lines (2085 loc) · 76.3 KB
/
end.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 3.7 end.c $NHDT-Date: 1693519356 2023/08/31 22:02:36 $ $NHDT-Branch: keni-crashweb2 $:$NHDT-Revision: 1.277 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
#define NEED_VARARGS /* comment line for pre-compiled headers */
#include "hack.h"
#ifndef NO_SIGNAL
#include <signal.h>
#endif
#include <ctype.h>
#ifndef LONG_MAX
#include <limits.h>
#endif
#include "dlb.h"
/* add b to long a, convert wraparound to max value */
#define nowrap_add(a, b) (a = ((a + b) < 0 ? LONG_MAX : (a + b)))
#ifndef NO_SIGNAL
static void done_intr(int);
#if defined(UNIX) || defined(VMS) || defined(__EMX__)
static void done_hangup(int);
#endif
#endif
static void disclose(int, boolean);
static void get_valuables(struct obj *);
static void sort_valuables(struct valuable_data *, int);
static void artifact_score(struct obj *, boolean, winid);
static boolean fuzzer_savelife(int);
ATTRNORETURN static void really_done(int) NORETURN;
static void savelife(int);
static boolean should_query_disclose_option(int, char *);
#ifdef DUMPLOG
static void dump_plines(void);
#endif
static void dump_everything(int, time_t);
#if defined(__BEOS__) || defined(MICRO) || defined(OS2) || defined(WIN32)
ATTRNORETURN extern void nethack_exit(int) NORETURN;
#else
#define nethack_exit exit
#endif
#define done_stopprint gp.program_state.stopprint
#ifndef PANICTRACE
#define NH_abort(x) NH_abort_
#endif
#ifdef AMIGA
#define NH_abort_ Abort(0)
#else
#ifdef SYSV
#define NH_abort_ (void) abort()
#else
#ifdef WIN32
#define NH_abort_ win32_abort()
#else
#define NH_abort_ abort()
#endif
#endif /* !SYSV */
#endif /* !AMIGA */
#ifdef PANICTRACE
#include <errno.h>
#ifdef PANICTRACE_LIBC
#include <execinfo.h>
#endif
/* What do we try and in what order? Tradeoffs:
* libc: +no external programs required
* -requires newish libc/glibc
* -requires -rdynamic
* gdb: +gives more detailed information
* +works on more OS versions
* -requires -g, which may preclude -O on some compilers
*
* And the UI: if sysopt.crashreporturl, and defined(CRASHREPORT)
* we gather the stacktrace (etc) and launch a helper to submit a bug report
* otherwise we just use stdout. Requires libc for now.
*/
#ifdef SYSCF
#define SYSOPT_PANICTRACE_GDB sysopt.panictrace_gdb
#ifdef PANICTRACE_LIBC
#define SYSOPT_PANICTRACE_LIBC sysopt.panictrace_libc
#else
#define SYSOPT_PANICTRACE_LIBC 0
#endif
#else
#define SYSOPT_PANICTRACE_GDB (nh_getenv("NETHACK_USE_GDB") == 0 ? 0 : 2)
#ifdef PANICTRACE_LIBC
#define SYSOPT_PANICTRACE_LIBC 1
#else
#define SYSOPT_PANICTRACE_LIBC 0
#endif
#endif
#ifdef PANICTRACE
static void NH_abort(char *);
#endif
#ifndef NO_SIGNAL
static void panictrace_handler(int);
#endif
static boolean NH_panictrace_libc(char *);
static boolean NH_panictrace_gdb(void);
#ifndef NO_SIGNAL
/* called as signal() handler, so sent at least one arg */
/*ARGUSED*/
void
panictrace_handler(int sig_unused UNUSED)
{
#define SIG_MSG "\nSignal received.\n"
int f2;
#ifdef CURSES_GRAPHICS
if (iflags.window_inited && WINDOWPORT(curses)) {
extern void curses_uncurse_terminal(void); /* wincurs.h */
/* it is risky calling this during a program-terminating signal,
but without it the subsequent backtrace is useless because
that ends up being scrawled all over the screen; call is
here rather than in NH_abort() because panic() calls both
exit_nhwindows(), which makes this same call under curses,
then NH_abort() and we don't want to call this twice */
curses_uncurse_terminal();
}
#endif
f2 = (int) write(2, SIG_MSG, sizeof SIG_MSG - 1);
nhUse(f2); /* what could we do if write to fd#2 (stderr) fails */
NH_abort(NULL); /* ... and we're already in the process of quitting? */
}
void
panictrace_setsignals(boolean set)
{
#define SETSIGNAL(sig) \
(void) signal(sig, set ? (SIG_RET_TYPE) panictrace_handler : SIG_DFL);
#ifdef SIGILL
SETSIGNAL(SIGILL);
#endif
#ifdef SIGTRAP
SETSIGNAL(SIGTRAP);
#endif
#ifdef SIGIOT
SETSIGNAL(SIGIOT);
#endif
#ifdef SIGBUS
SETSIGNAL(SIGBUS);
#endif
#ifdef SIGFPE
SETSIGNAL(SIGFPE);
#endif
#ifdef SIGSEGV
SETSIGNAL(SIGSEGV);
#endif
#ifdef SIGSTKFLT
SETSIGNAL(SIGSTKFLT);
#endif
#ifdef SIGSYS
SETSIGNAL(SIGSYS);
#endif
#ifdef SIGEMT
SETSIGNAL(SIGEMT);
#endif
#undef SETSIGNAL
}
#endif /* NO_SIGNAL */
#ifdef PANICTRACE
static void
NH_abort(char *why)
{
int gdb_prio = SYSOPT_PANICTRACE_GDB;
int libc_prio = SYSOPT_PANICTRACE_LIBC;
static volatile boolean aborting = FALSE;
/* don't execute this code recursively if a second abort is requested
while this routine or the code it calls is executing */
if (aborting)
return;
aborting = TRUE;
#ifndef VMS
if (gdb_prio == libc_prio && gdb_prio > 0)
gdb_prio++;
if (gdb_prio > libc_prio) {
(void) (NH_panictrace_gdb() || (libc_prio && NH_panictrace_libc(why)));
} else {
(void) (NH_panictrace_libc(why) || (gdb_prio && NH_panictrace_gdb()));
}
#else /* VMS */
/* overload otherwise unused priority for debug mode: 1 = show
traceback and exit; 2 = show traceback and stay in debugger */
/* if (wizard && gdb_prio == 1) gdb_prio = 2; */
vms_traceback(gdb_prio);
nhUse(libc_prio);
#endif /* ?VMS */
#ifndef NO_SIGNAL
panictrace_setsignals(FALSE);
#endif
NH_abort_;
}
#endif
/* Build a URL with a query string and try to launch a new browser window
* to report from panic() or impossible(). Requires libc support for
* the stacktrace. Uses memory on the stack to avoid memory allocation
* (but libc can still do anything it wants). */
/* size of argument list for execve(2) */
#define SWR_LINES 20
/* max stack frames and header lines (details field) */
#define SWR_FRAMES 20
#define SWR_ADD(line) {if(xargc<(SWR_LINES-1)) xargv[xargc++] = line;}
#ifdef CRASHREPORT
# include <fcntl.h>
# define HASH_PRAGMA_START \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
# define HASH_PRAGMA_END _Pragma("GCC diagnostic pop");
# ifdef MACOS
# include <CommonCrypto/CommonDigest.h>
# define HASH_CONTEXT CC_MD4_CTX
# define HASH_INIT(ctx) CC_MD4_Init(ctx)
# define HASH_UPDATE(ctx, ptr, len) CC_MD4_Update(ctx, ptr, len)
# define HASH_FINISH(ctx, out) CC_MD4_Final(out, ctx)
# define HASH_RESULT_SIZE CC_MD4_DIGEST_LENGTH
# endif
# ifdef __linux__
# include <openssl/md4.h>
# define HASH_CONTEXT MD4_CTX
# define HASH_INIT(ctx) MD4_Init(ctx)
# define HASH_UPDATE(ctx, ptr, len) MD4_Update(ctx, ptr, len)
# define HASH_FINISH(ctx, out) MD4_Final(out, ctx)
# define HASH_RESULT_SIZE MD4_DIGEST_LENGTH
# endif
// Binary ID - Use only as a hint to contact.html for recognizing our own
// binaries. This is easily spoofed!
static char bid[(2*HASH_RESULT_SIZE)+1];
/* ARGSUSED */
void
crashreport_init(int argc UNUSED, char *argv[] UNUSED){
unsigned char tmp[HASH_RESULT_SIZE];
HASH_PRAGMA_START
HASH_CONTEXT ctx;
HASH_INIT(&ctx);
#ifdef MACOS
char *binfile = argv[0];
if (!binfile || !*binfile) {
# ifdef BETA
// If this triggers, investigate CFBundleGetMainBundle
// or CFBundleCopyExecutableURL.
raw_print("BETA warning: crashreport_init called without useful info");
# endif
goto skip;
}
#endif
#ifdef __linux__
char binfile[PATH_MAX+1];
int len = readlink("/proc/self/exe", binfile, sizeof(binfile)-1);
if (len>0) {
binfile[len] = '\0';
} else {
goto skip;
}
#endif
int fd = open(binfile, O_RDONLY, 0);
if (fd == -1) {
# ifdef BETA
raw_printf("open e=%s",strerror(errno));
# endif
goto skip;
}
int segsize;
char segment[4096];
while (0 < (segsize = read(fd, segment,sizeof(segment)))) {
HASH_UPDATE(&ctx, segment, segsize);
}
HASH_FINISH(&ctx, tmp);
close(fd);
char *p = bid;
unsigned char *in = &tmp[0];
char cnt=HASH_RESULT_SIZE;
while (cnt--) {
p += snprintf(p, HASH_RESULT_SIZE-(p-bid), "%02x",*(in++));
}
*p = '\0';
return;
skip:
strncpy((char *)bid,"unknown",sizeof(bid)-1);
HASH_PRAGMA_END
}
#undef HASH_CONTEXT
#undef HASH_INIT
#undef HASH_UPDATE
#undef HASH_FINISH
#undef HASH_RESULT_SIZE
#undef HASH_PRAGMA_START
#undef HASH_PRAGMA_END
void
crashreport_bidshow(void){
raw_print(bid);
}
boolean
submit_web_report(const char *msg, char *why){
if (sysopt.crashreporturl) {
const char *xargv[SWR_LINES];
char version[100];
char versionstring[200]; // used twice as a temp
int xargc = 0;
char wholetrace[SWR_LINES*80]; // XXX roughly 71 on MacOS, plus buffer
int prelines = 0; // count of lines in trace header
char nbuf[6]; // number buffer
extern char **environ;
pid_t pid;
SWR_ADD(CRASHREPORT);
SWR_ADD(sysopt.crashreporturl);
// then pairs of key value
// subject, generate something useful
SWR_ADD("subject");
snprintf(version, sizeof(version), "%s report for NetHack %s",
msg, version_string(versionstring, sizeof(versionstring)));
SWR_ADD(version);
// name: someday, this might be stored in nethackcnf
// email: someday, this might be stored in nethackcnf
// gitver, pull from version.c
SWR_ADD("gitver");
SWR_ADD(getversionstring(versionstring, sizeof(versionstring)));
// hardware: leave for user
// software: leave for user
// comments: leave for user
// details: stack trace
SWR_ADD("details");
// XXX header for wholetrace - what other info do we want?
// NB: prelines not tested against size of SWR_FRAMES.
#define SWR_HDR(line) \
if (endp<&wholetrace[sizeof(wholetrace)]) { \
endp+=snprintf(endp, sizeof(wholetrace)-(endp-wholetrace), "%s\n",line); \
prelines++; \
}
#define SWR_HDRnonl(line) \
if (endp<&wholetrace[sizeof(wholetrace)]) { \
endp+=snprintf(endp, sizeof(wholetrace)-(endp-wholetrace), "%s",line); \
}
char *endp = wholetrace;
wholetrace[0] = 0;
if (why) {
SWR_HDR(why);
}
SWR_HDRnonl("bid: ");
SWR_HDR(bid);
void *bt[SWR_FRAMES];
int count, x;
char **info, buf[BUFSZ];
count = backtrace(bt, SIZE(bt));
info = backtrace_symbols(bt, count);
for (x = 0; x < count; x++) {
copynchars(buf, info[x], (int) sizeof buf - 1);
/* try to remove up to 16 blank spaces by removing 8 twice */
(void) strsubst(buf, " ", "");
(void) strsubst(buf, " ", "");
snprintf(endp, SWR_FRAMES*80-(endp-wholetrace), "[%02lu] %s\n",
(unsigned long) x, buf);
endp = eos(endp);
}
*(endp-1) = '\0'; // remove last newline
SWR_ADD(wholetrace);
// detailrows min(actual,50) Guess since we can't know the
// width of the window.
SWR_ADD("detailrows");
(void)snprintf(nbuf,sizeof(nbuf),"%d",count+prelines);
SWR_ADD(nbuf);
xargv[xargc++] = 0; // terminate array
pid = fork();
if (pid == 0) {
execve(CRASHREPORT, (char * const *)xargv, environ);
char err[100];
sprintf(err, "Can't start " CRASHREPORT ": %s", strerror(errno));
raw_print(err);
} else {
int status;
errno=0;
// XXX do we _really_ know this is the right pid?
(void)waitpid(pid, &status, 0);
if (status) { // XXX check could be more precise
#if 0
// Not useful at the moment. XXX
char err[100];
sprintf(err, "pid=%d e=%d status=%0x",wpid,errno,status);
raw_print(err);
#endif
return FALSE;
}
}
/* free(info); -- Don't risk it. */
return TRUE;
}
return FALSE;
}
#endif /* CRASHREPORT */
#undef SWR_ADD
#undef SWR_FRAMES
#undef SWR_HDR
#undef SWR_LINES
/*ARGSUSED*/
static boolean
NH_panictrace_libc(char *why UNUSED)
{
#ifdef CRASHREPORT
if(submit_web_report("Panic",why)) return TRUE;
#endif
#ifdef PANICTRACE_LIBC
void *bt[20];
int count, x;
char **info, buf[BUFSZ];
raw_print(" Generating more information you may report:\n");
count = backtrace(bt, SIZE(bt));
info = backtrace_symbols(bt, count);
for (x = 0; x < count; x++) {
copynchars(buf, info[x], (int) sizeof buf - 1);
/* try to remove up to 16 blank spaces by removing 8 twice */
(void) strsubst(buf, " ", "");
(void) strsubst(buf, " ", "");
raw_printf("[%02lu] %s", (unsigned long) x, buf);
}
/* free(info); -- Don't risk it. */
return TRUE;
#else
return FALSE;
#endif /* !PANICTRACE_LIBC */
}
/*
* fooPATH file system path for foo
* fooVAR (possibly const) variable containing fooPATH
*/
#ifdef PANICTRACE_GDB
#ifdef SYSCF
#define GDBVAR sysopt.gdbpath
#define GREPVAR sysopt.greppath
#else /* SYSCF */
#define GDBVAR GDBPATH
#define GREPVAR GREPPATH
#endif /* SYSCF */
#endif /* PANICTRACE_GDB */
static boolean
NH_panictrace_gdb(void)
{
#ifdef PANICTRACE_GDB
/* A (more) generic method to get a stack trace - invoke
* gdb on ourself. */
const char *gdbpath = GDBVAR;
const char *greppath = GREPVAR;
char buf[BUFSZ];
FILE *gdb;
if (gdbpath == NULL || gdbpath[0] == 0)
return FALSE;
if (greppath == NULL || greppath[0] == 0)
return FALSE;
sprintf(buf, "%s -n -q %s %d 2>&1 | %s '^#'",
gdbpath, ARGV0, getpid(), greppath);
gdb = popen(buf, "w");
if (gdb) {
raw_print(" Generating more information you may report:\n");
fprintf(gdb, "bt\nquit\ny");
fflush(gdb);
sleep(4); /* ugly */
pclose(gdb);
return TRUE;
} else {
return FALSE;
}
#else
return FALSE;
#endif /* !PANICTRACE_GDB */
}
#endif /* PANICTRACE */
/*
* The order of these needs to match the macros in hack.h.
*/
static NEARDATA const char *deaths[] = {
/* the array of death */
"died", "choked", "poisoned", "starvation", "drowning", "burning",
"dissolving under the heat and pressure", "crushed", "turned to stone",
"turned into slime", "genocided", "panic", "trickery", "quit",
"escaped", "ascended"
};
static NEARDATA const char *ends[] = {
/* "when you %s" */
"died", "choked", "were poisoned",
"starved", "drowned", "burned",
"dissolved in the lava",
"were crushed", "turned to stone",
"turned into slime", "were genocided",
"panicked", "were tricked", "quit",
"escaped", "ascended"
};
static boolean Schroedingers_cat = FALSE;
/* called as signal() handler, so sent at least one arg */
/*ARGSUSED*/
void
done1(int sig_unused UNUSED)
{
#ifndef NO_SIGNAL
(void) signal(SIGINT, SIG_IGN);
#endif
if (flags.ignintr) {
#ifndef NO_SIGNAL
(void) signal(SIGINT, (SIG_RET_TYPE) done1);
#endif
clear_nhwindow(WIN_MESSAGE);
curs_on_u();
wait_synch();
if (gm.multi > 0)
nomul(0);
} else {
(void) done2();
}
}
/* "#quit" command or keyboard interrupt */
int
done2(void)
{
boolean abandon_tutorial = FALSE;
if (In_tutorial(&u.uz)
&& y_n("Switch from the tutorial back to regular play?") == 'y')
abandon_tutorial = TRUE;
if (abandon_tutorial || !paranoid_query(ParanoidQuit, "Really quit?")) {
#ifndef NO_SIGNAL
(void) signal(SIGINT, (SIG_RET_TYPE) done1);
#endif
clear_nhwindow(WIN_MESSAGE);
curs_on_u();
wait_synch();
if (gm.multi > 0)
nomul(0);
if (gm.multi == 0) {
u.uinvulnerable = FALSE; /* avoid ctrl-C bug -dlc */
u.usleep = 0;
}
if (abandon_tutorial)
schedule_goto(&u.ucamefrom, UTOTYPE_ATSTAIRS,
"Resuming regular play", (char *) 0);
return ECMD_OK;
}
#if (defined(UNIX) || defined(VMS) || defined(LATTICE))
if (wizard) {
int c;
#ifdef VMS
extern int debuggable; /* sys/vms/vmsmisc.c, vmsunix.c */
c = !debuggable ? 'n' : ynq("Enter debugger?");
#else
#ifdef LATTICE
c = ynq("Create SnapShot?");
#else
c = ynq("Dump core?");
#endif
#endif
if (c == 'y') {
#ifndef NO_SIGNAL
(void) signal(SIGINT, (SIG_RET_TYPE) done1);
#endif
if (soundprocs.sound_exit_nhsound)
(*soundprocs.sound_exit_nhsound)("done2");
exit_nhwindows((char *) 0);
NH_abort(NULL);
} else if (c == 'q')
done_stopprint++;
}
#endif
done(QUIT);
return ECMD_OK;
}
#ifndef NO_SIGNAL
/* called as signal() handler, so sent at least 1 arg */
/*ARGSUSED*/
static void
done_intr(int sig_unused UNUSED)
{
done_stopprint++;
(void) signal(SIGINT, SIG_IGN);
#if defined(UNIX) || defined(VMS)
#ifndef VMSVSI
(void) signal(SIGQUIT, SIG_IGN);
#endif
#endif
return;
}
#if defined(UNIX) || defined(VMS) || defined(__EMX__)
/* signal() handler */
static void
done_hangup(int sig)
{
#ifdef HANGUPHANDLING
gp.program_state.done_hup++;
#endif
sethanguphandler((void (*)(int)) SIG_IGN);
done_intr(sig);
return;
}
#endif
#endif /* NO_SIGNAL */
DISABLE_WARNING_FORMAT_NONLITERAL /* one compiler warns if the format
string is the result of a ? x : y */
void
done_in_by(struct monst *mtmp, int how)
{
char buf[BUFSZ];
struct permonst *mptr = mtmp->data,
*champtr = (mtmp->cham >= LOW_PM) ? &mons[mtmp->cham]
: mptr;
boolean distorted = (boolean) (Hallucination && canspotmon(mtmp)),
mimicker = (M_AP_TYPE(mtmp) == M_AP_MONSTER),
imitator = (mptr != champtr || mimicker);
You((how == STONING) ? "turn to stone..." : "die...");
mark_synch(); /* flush buffered screen output */
buf[0] = '\0';
gk.killer.format = KILLED_BY_AN;
/* "killed by the high priest of Crom" is okay,
"killed by the high priest" alone isn't */
if ((mptr->geno & G_UNIQ) != 0 && !(imitator && !mimicker)
&& !(mptr == &mons[PM_HIGH_CLERIC] && !mtmp->ispriest)) {
if (!type_is_pname(mptr))
Strcat(buf, "the ");
gk.killer.format = KILLED_BY;
}
/* _the_ <invisible> <distorted> ghost of Dudley */
if (mptr == &mons[PM_GHOST] && has_mgivenname(mtmp)) {
Strcat(buf, "the ");
gk.killer.format = KILLED_BY;
}
(void) monhealthdescr(mtmp, TRUE, eos(buf));
if (mtmp->minvis)
Strcat(buf, "invisible ");
if (distorted)
Strcat(buf, "hallucinogen-distorted ");
if (imitator) {
char shape[BUFSZ];
const char *realnm = pmname(champtr, Mgender(mtmp)),
*fakenm = pmname(mptr, Mgender(mtmp));
boolean alt = is_vampshifter(mtmp);
if (mimicker) {
/* realnm is already correct because champtr==mptr;
set up fake mptr for type_is_pname/the_unique_pm */
mptr = &mons[mtmp->mappearance];
fakenm = pmname(mptr, Mgender(mtmp));
} else if (alt && strstri(realnm, "vampire")
&& !strcmp(fakenm, "vampire bat")) {
/* special case: use "vampire in bat form" in preference
to redundant looking "vampire in vampire bat form" */
fakenm = "bat";
}
/* for the alternate format, always suppress any article;
pname and the_unique should also have s_suffix() applied,
but vampires don't take on any shapes which warrant that */
if (alt || type_is_pname(mptr)) /* no article */
Strcpy(shape, fakenm);
else if (the_unique_pm(mptr)) /* "the"; don't use the() here */
Sprintf(shape, "the %s", fakenm);
else /* "a"/"an" */
Strcpy(shape, an(fakenm));
/* omit "called" to avoid excessive verbosity */
Sprintf(eos(buf),
alt ? "%s in %s form"
: mimicker ? "%s disguised as %s"
: "%s imitating %s",
realnm, shape);
mptr = mtmp->data; /* reset for mimicker case */
} else if (mptr == &mons[PM_GHOST]) {
Strcat(buf, "ghost");
if (has_mgivenname(mtmp))
Sprintf(eos(buf), " of %s", MGIVENNAME(mtmp));
} else if (mtmp->isshk) {
const char *shknm = shkname(mtmp),
*honorific = shkname_is_pname(mtmp) ? ""
: mtmp->female ? "Ms. " : "Mr. ";
Sprintf(eos(buf), "%s%s, the shopkeeper", honorific, shknm);
gk.killer.format = KILLED_BY;
} else if (mtmp->ispriest || mtmp->isminion) {
/* m_monnam() suppresses "the" prefix plus "invisible", and
it overrides the effect of Hallucination on priestname() */
Strcat(buf, m_monnam(mtmp));
} else {
Strcat(buf, pmname(mptr, Mgender(mtmp)));
if (has_mgivenname(mtmp))
Sprintf(eos(buf), " called %s", MGIVENNAME(mtmp));
}
Strcpy(gk.killer.name, buf);
/* might need to fix up multi_reason if 'mtmp' caused the reason */
if (gm.multi_reason
&& gm.multi_reason > gm.multireasonbuf
&& gm.multi_reason
< gm.multireasonbuf + sizeof gm.multireasonbuf - 1) {
char reasondummy, *p;
unsigned reasonmid = 0;
/*
* multireasonbuf[] contains 'm_id:reason' and multi_reason
* points at the text past the colon, so we have something
* like "42:paralyzed by a ghoul"; if mtmp->m_id matches 42
* then we truncate 'reason' at its first space so that final
* death reason becomes "Killed by a ghoul, while paralyzed."
* instead of "Killed by a ghoul, while paralyzed by a ghoul."
* (3.6.x gave "Killed by a ghoul, while paralyzed by a monster."
* which is potentially misleading when the monster is also
* the killer.)
*
* Note that if the hero is life-saved and then killed again
* before the helplessness has cleared, the second death will
* report the truncated helplessness reason even if some other
* monster performs the /coup de grace/.
*/
if (sscanf(gm.multireasonbuf, "%u:%c", &reasonmid, &reasondummy) == 2
&& mtmp->m_id == reasonmid) {
if ((p = strchr(gm.multireasonbuf, ' ')) != 0)
*p = '\0';
}
}
/*
* Chicken and egg issue:
* Ordinarily Unchanging ought to override something like this,
* but the transformation occurs at death. With the current code,
* the effectiveness of Unchanging stops first, but a case could
* be made that it should last long enough to prevent undead
* transformation. (Turning to slime isn't an issue here because
* Unchanging prevents that from happening.)
*/
if (mptr->mlet == S_WRAITH)
u.ugrave_arise = PM_WRAITH;
else if (mptr->mlet == S_MUMMY && gu.urace.mummynum != NON_PM)
u.ugrave_arise = gu.urace.mummynum;
else if (zombie_maker(mtmp) && gu.urace.zombienum != NON_PM)
u.ugrave_arise = gu.urace.zombienum;
else if (mptr->mlet == S_VAMPIRE && Race_if(PM_HUMAN))
u.ugrave_arise = PM_VAMPIRE;
else if (mptr == &mons[PM_GHOUL])
u.ugrave_arise = PM_GHOUL;
/* this could happen if a high-end vampire kills the hero
when ordinary vampires are genocided; ditto for wraiths */
if (u.ugrave_arise >= LOW_PM
&& (gm.mvitals[u.ugrave_arise].mvflags & G_GENOD))
u.ugrave_arise = NON_PM;
done(how);
return;
}
RESTORE_WARNING_FORMAT_NONLITERAL
/* some special cases for overriding while-helpless reason */
static const struct {
int why, unmulti;
const char *exclude, *include;
} death_fixups[] = {
/* "petrified by <foo>, while getting stoned" -- "while getting stoned"
prevented any last-second recovery, but it was not the cause of
"petrified by <foo>" */
{ STONING, 1, "getting stoned", (char *) 0 },
/* "died of starvation, while fainted from lack of food" is accurate
but sounds a fairly silly (and doesn't actually appear unless you
splice together death and while-helpless from xlogfile) */
{ STARVING, 0, "fainted from lack of food", "fainted" },
};
/* clear away while-helpless when the cause of death caused that
helplessness (ie, "petrified by <foo> while getting stoned") */
static void
fixup_death(int how)
{
int i;
if (gm.multi_reason) {
for (i = 0; i < SIZE(death_fixups); ++i)
if (death_fixups[i].why == how
&& !strcmp(death_fixups[i].exclude, gm.multi_reason)) {
if (death_fixups[i].include) /* substitute alternate reason */
gm.multi_reason = death_fixups[i].include;
else /* remove the helplessness reason */
gm.multi_reason = (char *) 0;
gm.multireasonbuf[0] = '\0'; /* dynamic buf stale either way */
if (death_fixups[i].unmulti) /* possibly hide helplessness */
gm.multi = 0L;
break;
}
}
}
#if defined(WIN32) && !defined(SYSCF)
#define NOTIFY_NETHACK_BUGS
#endif
DISABLE_WARNING_FORMAT_NONLITERAL
/*VARARGS1*/
ATTRNORETURN void
panic VA_DECL(const char *, str)
{
char buf[BUFSZ];
VA_START(str);
VA_INIT(str, char *);
if (gp.program_state.panicking++)
NH_abort(NULL); /* avoid loops - this should never happen*/
if (iflags.window_inited) {
raw_print("\r\nOops...");
wait_synch(); /* make sure all pending output gets flushed */
if (soundprocs.sound_exit_nhsound)
(*soundprocs.sound_exit_nhsound)("panic");
exit_nhwindows((char *) 0);
iflags.window_inited = 0; /* they're gone; force raw_print()ing */
}
raw_print(gp.program_state.gameover
? "Postgame wrapup disrupted."
: !gp.program_state.something_worth_saving
? "Program initialization has failed."
: "Suddenly, the dungeon collapses.");
#ifndef MICRO
#ifdef NOTIFY_NETHACK_BUGS
if (!wizard)
raw_printf("Report the following error to \"%s\" or at \"%s\".",
DEVTEAM_EMAIL, DEVTEAM_URL);
else if (gp.program_state.something_worth_saving)
raw_print("\nError save file being written.\n");
#else /* !NOTIFY_NETHACK_BUGS */
if (!wizard) {
const char *maybe_rebuild = !gp.program_state.something_worth_saving
? "."
: "\nand it may be possible to rebuild.";
// XXX this is probably wrong if defined(CRASHREPORT)
if (sysopt.support)
raw_printf("To report this error, %s%s", sysopt.support,
maybe_rebuild);
else if (sysopt.fmtd_wizard_list) /* formatted SYSCF WIZARDS */
raw_printf("To report this error, contact %s%s",
sysopt.fmtd_wizard_list, maybe_rebuild);
else
raw_printf("Report error to \"%s\"%s", WIZARD_NAME,
maybe_rebuild);
}
#endif /* ?NOTIFY_NETHACK_BUGS */
/* XXX can we move this above the prints? Then we'd be able to
* suppress "it may be possible to rebuild" based on dosave0()
* or say it's NOT possible to rebuild. */
if (gp.program_state.something_worth_saving && !iflags.debug_fuzzer) {
set_error_savefile();
if (dosave0()) {
/* os/win port specific recover instructions */
if (sysopt.recover)
raw_printf("%s", sysopt.recover);
}
}
#endif /* !MICRO */
(void) vsnprintf(buf, sizeof buf, str, VA_ARGS);
raw_print(buf);
paniclog("panic", buf);
#ifdef WIN32
interject(INTERJECT_PANIC);
#endif
#if defined(UNIX) || defined(VMS) || defined(LATTICE) || defined(WIN32)
# ifndef CRASHREPORT
if (wizard)
# endif
NH_abort(buf); /* generate core dump */
#endif
VA_END();
really_done(PANICKED);
}
RESTORE_WARNING_FORMAT_NONLITERAL
static boolean
should_query_disclose_option(int category, char *defquery)
{
int idx;
char disclose, *dop;
*defquery = 'n';
if ((dop = strchr(disclosure_options, category)) != 0) {
idx = (int) (dop - disclosure_options);
if (idx < 0 || idx >= NUM_DISCLOSURE_OPTIONS) {
impossible(
"should_query_disclose_option: bad disclosure index %d %c",
idx, category);
*defquery = DISCLOSE_PROMPT_DEFAULT_YES;
return TRUE;
}
disclose = flags.end_disclose[idx];
if (disclose == DISCLOSE_YES_WITHOUT_PROMPT) {
*defquery = 'y';
return FALSE;
} else if (disclose == DISCLOSE_SPECIAL_WITHOUT_PROMPT) {
*defquery = 'a';
return FALSE;
} else if (disclose == DISCLOSE_NO_WITHOUT_PROMPT) {
*defquery = 'n';
return FALSE;
} else if (disclose == DISCLOSE_PROMPT_DEFAULT_YES) {
*defquery = 'y';
return TRUE;
} else if (disclose == DISCLOSE_PROMPT_DEFAULT_SPECIAL) {
*defquery = 'a';
return TRUE;
} else {
*defquery = 'n';
return TRUE;
}
}
impossible("should_query_disclose_option: bad category %c", category);
return TRUE;
}
#ifdef DUMPLOG
static void
dump_plines(void)
{
int i, j;
char buf[BUFSZ], **strp;
Strcpy(buf, " "); /* one space for indentation */
putstr(0, 0, "Latest messages:");
for (i = 0, j = (int) gs.saved_pline_index; i < DUMPLOG_MSG_COUNT;
++i, j = (j + 1) % DUMPLOG_MSG_COUNT) {
strp = &gs.saved_plines[j];
if (*strp) {
copynchars(&buf[1], *strp, BUFSZ - 1 - 1);
putstr(0, 0, buf);
#ifdef FREE_ALL_MEMORY
free(*strp), *strp = 0;
#endif
}
}
}
#endif
/*ARGSUSED*/
static void
dump_everything(
int how, /* ASCENDED, ESCAPED, QUIT, etc */
time_t when) /* date+time at end of game */
{
#ifdef DUMPLOG
char pbuf[BUFSZ], datetimebuf[24]; /* [24]: room for 64-bit bogus value */
dump_redirect(TRUE);
if (!iflags.in_dumplog)
return;