ncurses 6.2 - patch 20200404
[ncurses.git] / ncurses / trace / lib_tracemse.c
index e062a26af52371c2352287999c89bf35cb890f3a..33d4d3d300d3a4ffb75c585760b0fafe749d17cd 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc.              *
+ * Copyright 2020 Thomas E. Dickey                                          *
+ * Copyright 1998-2012,2014 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_tracemse.c,v 1.20 2012/02/22 22:40:24 tom Exp $")
+MODULE_ID("$Id: lib_tracemse.c,v 1.23 2020/02/02 23:34:34 tom Exp $")
 
 #ifdef TRACE
 
 #define my_buffer sp->tracemse_buf
 
-static char *
-_trace_mmask_t(SCREEN *sp, mmask_t code)
+NCURSES_EXPORT(char *)
+_nc_trace_mmask_t(SCREEN *sp, mmask_t code)
 {
 #define SHOW(m, s) \
     if ((code & m) == m) { \
-       _nc_STRCAT(my_buffer, s, sizeof(my_buffer)); \
+       size_t n = strlen(my_buffer); \
+       if (n && (my_buffer[n-1] != '{')) \
        _nc_STRCAT(my_buffer, ", ", sizeof(my_buffer)); \
+       _nc_STRCAT(my_buffer, s, sizeof(my_buffer)); \
     }
 
     SHOW(BUTTON1_RELEASED, "release-1");
@@ -114,24 +117,33 @@ _trace_mmask_t(SCREEN *sp, mmask_t code)
 NCURSES_EXPORT(char *)
 _nc_tracemouse(SCREEN *sp, MEVENT const *ep)
 {
-    _nc_SPRINTF(my_buffer, _nc_SLIMIT(sizeof(my_buffer))
-               TRACEMSE_FMT,
-               ep->id,
-               ep->x,
-               ep->y,
-               ep->z,
-               (unsigned long) ep->bstate);
-
-    (void) _trace_mmask_t(sp, ep->bstate);
-    _nc_STRCAT(my_buffer, "}", sizeof(my_buffer));
-    return (my_buffer);
+    char *result = 0;
+
+    if (sp != 0) {
+       _nc_SPRINTF(my_buffer, _nc_SLIMIT(sizeof(my_buffer))
+                   TRACEMSE_FMT,
+                   ep->id,
+                   ep->x,
+                   ep->y,
+                   ep->z,
+                   (unsigned long) ep->bstate);
+
+       (void) _nc_trace_mmask_t(sp, ep->bstate);
+       _nc_STRCAT(my_buffer, "}", sizeof(my_buffer));
+       result = (my_buffer);
+    }
+    return result;
 }
 
 NCURSES_EXPORT(mmask_t)
 _nc_retrace_mmask_t(SCREEN *sp, mmask_t code)
 {
-    *my_buffer = '\0';
-    T((T_RETURN("{%s}"), _trace_mmask_t(sp, code)));
+    if (sp != 0) {
+       *my_buffer = '\0';
+       T((T_RETURN("{%s}"), _nc_trace_mmask_t(sp, code)));
+    } else {
+       T((T_RETURN("{?}")));
+    }
     return code;
 }