]> granicus.if.org Git - nethack/commitdiff
some livelog cleanup
authorPatR <rankin@nethack.org>
Wed, 2 Mar 2022 21:09:42 +0000 (13:09 -0800)
committerPatR <rankin@nethack.org>
Wed, 2 Mar 2022 21:09:42 +0000 (13:09 -0800)
The gamelog structure's type/flags field is 'long' but the
corresponding livelog event type field and the argument passed to
gamelog's logging were 'unsigned'.  They take the same values and
those values mean the same things so change them all to long.

The actual livelog logging assumed that time_t is a long number of
seconds, and was also using a boolean as an array index.  Perform
proper type conversions.

sysconf parsing used 'int' to hold strtol() value; change to long.
Also it was using raw_printf() instead of config_error_add() to
complain about any problems.  Clearly the livelog patch was not
updated to the current code base before being incorporated.

include/extern.h
include/sys.h
src/files.c
src/pline.c

index 71f8704266bc654a91dddd36b4fcc29602e1cb96..9c60cb2d75c1f48f23ab3d282e7cd86902acc44d 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 extern.h        $NHDT-Date: 1646171621 2022/03/01 21:53:41 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.1063 $ */
+/* NetHack 3.7 extern.h        $NHDT-Date: 1646255373 2022/03/02 21:09:33 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.1064 $ */
 /* Copyright (c) Steve Creps, 1988.                              */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -884,7 +884,7 @@ extern void reveal_paths(void);
 extern boolean read_tribute(const char *, const char *, int, char *, int,
                             unsigned);
 extern boolean Death_quote(char *, int);
-extern void livelog_add(unsigned int ll_type, const char *);
+extern void livelog_add(long, const char *);
 
 /* ### fountain.c ### */
 
@@ -2049,8 +2049,8 @@ extern void You_see(const char *, ...) PRINTF_F(1, 2);
 extern void pline_The(const char *, ...) PRINTF_F(1, 2);
 extern void There(const char *, ...) PRINTF_F(1, 2);
 extern void verbalize(const char *, ...) PRINTF_F(1, 2);
-extern void gamelog_add(unsigned int, long, const char *);
-extern void livelog_printf(unsigned int, const char *, ...) PRINTF_F(2, 3);
+extern void gamelog_add(long, long, const char *);
+extern void livelog_printf(long, const char *, ...) PRINTF_F(2, 3);
 extern void raw_printf(const char *, ...) PRINTF_F(1, 2);
 extern void impossible(const char *, ...) PRINTF_F(1, 2);
 extern void config_error_add(const char *, ...) PRINTF_F(1, 2);
index 78cf47eee702423bcf5529e8ea5a94693433e158..8caa39b43cc362e2a87e146ede131c4b1ea94b89 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 sys.h   $NHDT-Date: 1596498561 2020/08/03 23:49:21 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.36 $ */
+/* NetHack 3.7 sys.h   $NHDT-Date: 1646255373 2022/03/02 21:09:33 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.38 $ */
 /* Copyright (c) Kenneth Lorber, Kensington, Maryland, 2008. */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -28,7 +28,7 @@ struct sysopt {
     int check_save_uid; /* restoring savefile checks UID? */
     int check_plname; /* use plname for checking wizards/explorers/shellers */
     int bones_pools;
-    unsigned int livelog; /* LL_foo events to livelog */
+    long livelog; /* LL_foo events to livelog */
 
     /* record file */
     int persmax;
index edbc7b1ec45f230c8eb5643410a3a34c5a76ddbc..628f6f58fdf336850e0ee48eeef029ee1a5565b7 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 files.c $NHDT-Date: 1620522110 2021/05/09 01:01:50 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.334 $ */
+/* NetHack 3.7 files.c $NHDT-Date: 1646255374 2022/03/02 21:09:34 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.345 $ */
 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
 /*-Copyright (c) Derek S. Ray, 2015. */
 /* NetHack may be freely redistributed.  See license for details. */
@@ -2578,13 +2578,18 @@ parse_config_line(char *origbuf)
             n = 10;
         }
         sysopt.tt_oname_maxrank = n;
-    } else if (src == set_in_sysconf && match_varname(buf, "LIVELOG", 7)) {
-        n = strtol(bufp,NULL,0);
-        if (n < 0 || n > 0xFFFF) {
-            raw_printf("Illegal value in LIVELOG (must be between 0 and 0xFFFF).");
+    } else if (in_sysconf && match_varname(buf, "LIVELOG", 7)) {
+        /* using 0 for base accepts "dddd" as decimal provided that first 'd'
+           isn't '0', "0xhhhh" as hexadecimal, and "0oooo" as octal; ignores
+           any trailing junk, including '8' or '9' for leading '0' octal */
+        long L = strtol(bufp, NULL, 0);
+
+        if (L < 0L || L > 0xffffL) {
+            config_error_add(
+                 "Illegal value for LIVELOG (must be between 0 and 0xFFFF).");
             return 0;
         }
-        sysopt.livelog = n;
+        sysopt.livelog = L;
 
     /* SYSCF PANICTRACE options */
     } else if (in_sysconf && match_varname(buf, "PANICTRACE_LIBC", 15)) {
@@ -4667,52 +4672,66 @@ Death_quote(char *buf, int bufsz)
 
 /* ----------  END TRIBUTE ----------- */
 
-#if defined LIVELOG
-#define LLOG_SEP '\t' /* livelog field separator */
+#ifdef LIVELOG
+#define LLOG_SEP "\t" /* livelog field separator, as a string literal */
+#define LLOG_EOL "\n" /* end-of-line, for abstraction consistency */
 
 /* Locks the live log file and writes 'buffer'
- * IF the ll_type matches sysopt.livelog mask
- * lltype is included in LL entry for post-process filtering also
+ * iff the ll_type matches sysopt.livelog mask.
+ * lltype is included in LL entry for post-process filtering also.
  */
 void
-livelog_add(unsigned int ll_type, const char *str)
+livelog_add(long ll_type, const char *str)
 {
-    FILE* livelogfile;
+    FILE *livelogfile;
+    time_t now;
+    int gindx, aindx;
 
     if (!(ll_type & sysopt.livelog))
         return;
+
     if (lock_file(LIVELOGFILE, SCOREPREFIX, 10)) {
         if (!(livelogfile = fopen_datafile(LIVELOGFILE, "a", SCOREPREFIX))) {
             pline("Cannot open live log file!");
             unlock_file(LIVELOGFILE);
             return;
         }
-        fprintf(livelogfile,
-                 "lltype=%d%cname=%s%crole=%s%crace=%s%cgender=%s%c"
-                 "align=%s%cturns=%ld%cstarttime=%ld%ccurtime=%ld%c"
-                 "message=%s\n",
-                 (ll_type & sysopt.livelog), LLOG_SEP,
-                 g.plname, LLOG_SEP,
-                 g.urole.filecode, LLOG_SEP,
-                 g.urace.filecode, LLOG_SEP,
-                 genders[flags.female].filecode, LLOG_SEP,
-                 aligns[1-u.ualign.type].filecode, LLOG_SEP,
-                 g.moves, LLOG_SEP,
-                 (long)ubirthday, LLOG_SEP,
-                 (long)time(NULL),
-                 LLOG_SEP, str);
+
+        now = getnow();
+        gindx = flags.female ? 1 : 0;
+        /* note on alignment designation:
+               aligns[] uses [0] lawful, [1] neutral, [2] chaotic;
+               u.ualign.type uses -1 chaotic, 0 neutral, 1 lawful;
+           so subtracting from 1 converts from either to the other */
+        aindx = 1 - u.ualign.type;
+        /* format relies on STD C's implicit concatenation of
+           adjacent string literals */
+        (void) fprintf(livelogfile,
+                       "lltype=%ld"  LLOG_SEP  "name=%s"       LLOG_SEP
+                       "role=%s"     LLOG_SEP  "race=%s"       LLOG_SEP
+                       "gender=%s"   LLOG_SEP  "align=%s"      LLOG_SEP
+                       "turns=%ld"   LLOG_SEP  "starttime=%ld" LLOG_SEP
+                       "curtime=%ld" LLOG_SEP  "message=%s"    LLOG_EOL,
+                       (ll_type & sysopt.livelog), g.plname,
+                       g.urole.filecode, g.urace.filecode,
+                       genders[gindx].filecode, aligns[aindx].filecode,
+                       g.moves, timet_to_seconds(ubirthday),
+                       timet_to_seconds(now), str);
         (void) fclose(livelogfile);
         unlock_file(LIVELOGFILE);
     }
 }
 #undef LLOG_SEP
+#undef LLOG_EOL
 
 #else
+
 void
-livelog_add(unsigned int ll_type UNUSED, const char *str UNUSED)
+livelog_add(long ll_type UNUSED, const char *str UNUSED)
 {
     /* nothing here */
 }
+
 #endif /* !LIVELOG */
 
 /*files.c*/
index 89b175566c593a2efb92dfd6b073813fd21ef5b0..617d743b5fedfb648c7a3c98c266bd3ed0535e98 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 pline.c $NHDT-Date: 1637982230 2021/11/27 03:03:50 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.104 $ */
+/* NetHack 3.7 pline.c $NHDT-Date: 1646255375 2022/03/02 21:09:35 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.109 $ */
 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
 /*-Copyright (c) Robert Patrick Rankin, 2018. */
 /* NetHack may be freely redistributed.  See license for details. */
@@ -401,7 +401,7 @@ verbalize(const char *line, ...)
 #ifdef CHRONICLE
 
 void
-gamelog_add(unsigned int glflags, long gltime, const char *str)
+gamelog_add(long glflags, long gltime, const char *str)
 {
     struct gamelog_line *tmp;
     struct gamelog_line *lst = g.gamelog;
@@ -420,13 +420,13 @@ gamelog_add(unsigned int glflags, long gltime, const char *str)
 }
 
 void
-livelog_printf(unsigned ll_type, const char *line, ...)
+livelog_printf(long ll_type, const char *line, ...)
 {
     char gamelogbuf[BUFSZ * 2];
     va_list the_args;
 
     va_start(the_args, line);
-    vsnprintf(gamelogbuf, sizeof gamelogbuf, line, the_args);
+    (void) vsnprintf(gamelogbuf, sizeof gamelogbuf, line, the_args);
     va_end(the_args);
 
     gamelog_add(ll_type, g.moves, gamelogbuf);
@@ -438,13 +438,14 @@ livelog_printf(unsigned ll_type, const char *line, ...)
 
 void
 gamelog_add(
-    unsigned glflags UNUSED, long gltime UNUSED, const char *msg UNUSED)
+    long glflags UNUSED, long gltime UNUSED, const char *msg UNUSED)
 {
     ; /* nothing here */
 }
 
 void
-livelog_printf(unsigned ll_type UNUSED, const char *line UNUSED, ...)
+livelog_printf(
+    long ll_type UNUSED, const char *line UNUSED, ...)
 {
     ; /* nothing here */
 }