From: PatR Date: Sat, 11 Mar 2017 00:41:49 +0000 (-0800) Subject: dumplog message history groundwork X-Git-Tag: NetHack-3.6.1_RC01~514 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=278b6d52ebbf475cae41a892fd5c66f95fc5d610;p=nethack dumplog message history groundwork Separate the message logging out of pline so that other things (for instance, one-line summary for quest block messages) can be logged. The code that utilizes this isn't ready for prime time yet. For FREE_ALL_MEMORY, release DUMPLOG message history when saving. (Actually, this frees it unconditionally rather just doing so for FREE_ALL_MEMORY.) It was being freed when logged at end of game, but not during save. If dumplog message history and interface message history get integrated, the existing message history save/restore handling should become applicable instead. --- diff --git a/include/extern.h b/include/extern.h index 800969d6f..3f5b34971 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1,4 +1,4 @@ -/* NetHack 3.6 extern.h $NHDT-Date: 1488075978 2017/02/26 02:26:18 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.582 $ */ +/* NetHack 3.6 extern.h $NHDT-Date: 1489192904 2017/03/11 00:41:44 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.583 $ */ /* Copyright (c) Steve Creps, 1988. */ /* NetHack may be freely redistributed. See license for details. */ @@ -1801,6 +1801,10 @@ E boolean FDECL(is_autopickup_exception, (struct obj *, BOOLEAN_P)); /* ### pline.c ### */ +#ifdef DUMPLOG +E void FDECL(dumplogmsg, (const char *)); +E void NDECL(dumplogfreemessages); +#endif E void VDECL(pline, (const char *, ...)) PRINTF_F(1, 2); E void VDECL(Norep, (const char *, ...)) PRINTF_F(1, 2); E void NDECL(free_youbuf); diff --git a/src/pline.c b/src/pline.c index 6103d29cf..2a473625b 100644 --- a/src/pline.c +++ b/src/pline.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 pline.c $NHDT-Date: 1461437814 2016/04/23 18:56:54 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.51 $ */ +/* NetHack 3.6 pline.c $NHDT-Date: 1489192905 2017/03/11 00:41:45 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.57 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -18,6 +18,47 @@ static void FDECL(execplinehandler, (const char *)); /* also used in end.c */ unsigned saved_pline_index = 0; /* slot in saved_plines[] to use next */ char *saved_plines[DUMPLOG_MSG_COUNT] = { (char *) 0 }; + +/* keep the most recent DUMPLOG_MSG_COUNT messages */ +void +dumplogmsg(line) +const char *line; +{ + /* + * TODO: + * This essentially duplicates message history, which is + * currently implemented in an interface-specific manner. + * The core should take responsibility for that and have + * this share it. + */ + unsigned indx = saved_pline_index; /* next slot to use */ + char *oldest = saved_plines[indx]; /* current content of that slot */ + + if (oldest && strlen(oldest) >= strlen(line)) { + /* this buffer will gradually shrink until the 'else' is needed; + there's no pressing need to track allocation size instead */ + Strcpy(oldest, line); + } else { + if (oldest) + free((genericptr_t) oldest); + saved_plines[indx] = dupstr(line); + } + saved_pline_index = (indx + 1) % DUMPLOG_MSG_COUNT; +} + +/* called during save (unlike the interface-specific message history, + this data isn't saved and restored); end-of-game releases saved_pline[] + while writing its contents to the final dump log */ +void +dumplogfreemessages() +{ + unsigned indx; + + for (indx = 0; indx < DUMPLOG_MSG_COUNT; ++indx) + if (saved_plines[indx]) + free((genericptr_t) saved_plines[indx]), saved_plines[indx] = 0; + saved_pline_index = 0; +} #endif /*VARARGS1*/ @@ -86,7 +127,6 @@ VA_DECL(const char *, line) pbuf[BUFSZ - 1 - 1] = line[ln - 1]; pbuf[BUFSZ - 1] = '\0'; line = pbuf; - ln = BUFSZ - 1; } if (!iflags.window_inited) { raw_print(line); @@ -99,30 +139,7 @@ VA_DECL(const char *, line) * Unfortunately, that means Norep() isn't honored (general issue) and * that short lines aren't combined into one longer one (tty behavior). */ - { - /* - * TODO: - * This essentially duplicates message history, which is - * currently implemented in an interface-specific manner. - * The core should take responsibility for that and have - * this share it. - * Ideally history for prompt lines should be augmented - * with the player's response once that has been specified. - */ - unsigned indx = saved_pline_index; /* next slot to use */ - char *oldest = saved_plines[indx]; /* current content of that slot */ - - if (oldest && (int) strlen(oldest) >= ln) { /* ln==strlen(line) */ - /* this buffer will gradually shrink until the 'else' is needed; - there's no pressing need to track allocation size instead */ - Strcpy(oldest, line); - } else { - if (oldest) - free((genericptr_t) oldest); - saved_plines[indx] = dupstr(line); - } - saved_pline_index = (indx + 1) % DUMPLOG_MSG_COUNT; - } + dumplogmsg(line); #endif msgtyp = msgtype_type(line, no_repeat); diff --git a/src/save.c b/src/save.c index 078f276af..5270cc657 100644 --- a/src/save.c +++ b/src/save.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 save.c $NHDT-Date: 1450231175 2015/12/16 01:59:35 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.98 $ */ +/* NetHack 3.6 save.c $NHDT-Date: 1489192905 2017/03/11 00:41:45 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.101 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -1410,6 +1410,9 @@ freedynamicdata() #ifdef STATUS_VIA_WINDOWPORT status_finish(); #endif +#ifdef DUMPLOG + dumplogfreemessages(); +#endif /* last, because it frees data that might be used by panic() to provide feedback to the user; conceivably other freeing might trigger panic */