]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/trace/visbuf.c
ncurses 6.4 - patch 20240414
[ncurses.git] / ncurses / trace / visbuf.c
index a10b805e29750e812f8c4c4dcac226ce15605396..590e42306f5163acfceb0c0baa2959006b309520 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 2001-2011,2012 Free Software Foundation, Inc.              *
+ * Copyright 2019-2021,2023 Thomas E. Dickey                                *
+ * Copyright 2001-2016,2017 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
 #include <tic.h>
 #include <ctype.h>
 
 #include <tic.h>
 #include <ctype.h>
 
-MODULE_ID("$Id: visbuf.c,v 1.41 2012/09/01 23:53:30 tom Exp $")
+MODULE_ID("$Id: visbuf.c,v 1.54 2023/05/27 20:13:10 tom Exp $")
 
 #define NUM_VISBUFS 4
 
 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
 
 #define NUM_VISBUFS 4
 
 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
-#define WideLen(len)   (size_t) (((size_t)(len) + 1) * 4 * MB_CUR_MAX)
+#define WideLen(len)   (size_t) (((size_t)(len) + 1) * 4 * (size_t) MB_CUR_MAX)
 
 #ifdef TRACE
 static const char d_quote[] = StringOf(D_QUOTE);
 
 #ifdef TRACE
 static const char d_quote[] = StringOf(D_QUOTE);
@@ -66,10 +67,12 @@ static const char r_brace[] = StringOf(R_BRACE);
 static char *
 _nc_vischar(char *tp, unsigned c LIMIT_ARG)
 {
 static char *
 _nc_vischar(char *tp, unsigned c LIMIT_ARG)
 {
-    if (c == '"' || c == '\\') {
+    if (tp == NULL) {
+       return NULL;
+    } else if (c == '"' || c == '\\') {
        *tp++ = '\\';
        *tp++ = (char) c;
        *tp++ = '\\';
        *tp++ = (char) c;
-    } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
+    } else if (is7bits((int) c) && (isgraph((int) c) || c == ' ')) {
        *tp++ = (char) c;
     } else if (c == '\n') {
        *tp++ = '\\';
        *tp++ = (char) c;
     } else if (c == '\n') {
        *tp++ = '\\';
@@ -80,6 +83,9 @@ _nc_vischar(char *tp, unsigned c LIMIT_ARG)
     } else if (c == '\b') {
        *tp++ = '\\';
        *tp++ = 'b';
     } else if (c == '\b') {
        *tp++ = '\\';
        *tp++ = 'b';
+    } else if (c == '\t') {
+       *tp++ = '\\';
+       *tp++ = 't';
     } else if (c == '\033') {
        *tp++ = '\\';
        *tp++ = 'e';
     } else if (c == '\033') {
        *tp++ = '\\';
        *tp++ = 'e';
@@ -105,7 +111,6 @@ _nc_visbuf2n(int bufnum, const char *buf, int len)
 {
     const char *vbuf = 0;
     char *tp;
 {
     const char *vbuf = 0;
     char *tp;
-    int c;
     int count;
 
     if (buf == 0)
     int count;
 
     if (buf == 0)
@@ -122,6 +127,8 @@ _nc_visbuf2n(int bufnum, const char *buf, int len)
 #else
     {
        static char *mybuf[NUM_VISBUFS];
 #else
     {
        static char *mybuf[NUM_VISBUFS];
+       int c;
+
        if (bufnum < 0) {
            for (c = 0; c < NUM_VISBUFS; ++c) {
                FreeAndNull(mybuf[c]);
        if (bufnum < 0) {
            for (c = 0; c < NUM_VISBUFS; ++c) {
                FreeAndNull(mybuf[c]);
@@ -134,6 +141,8 @@ _nc_visbuf2n(int bufnum, const char *buf, int len)
     }
 #endif
     if (tp != 0) {
     }
 #endif
     if (tp != 0) {
+       int c;
+
        *tp++ = D_QUOTE;
        while ((--count >= 0) && (c = *buf++) != '\0') {
            tp = VisChar(tp, UChar(c), NormalLen(len));
        *tp++ = D_QUOTE;
        while ((--count >= 0) && (c = *buf++) != '\0') {
            tp = VisChar(tp, UChar(c), NormalLen(len));
@@ -185,7 +194,6 @@ _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
 {
     const char *vbuf;
     char *tp;
 {
     const char *vbuf;
     char *tp;
-    wchar_t c;
     int count;
 
     if (buf == 0)
     int count;
 
     if (buf == 0)
@@ -205,6 +213,8 @@ _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
     }
 #endif
     if (tp != 0) {
     }
 #endif
     if (tp != 0) {
+       wchar_t c;
+
        *tp++ = D_QUOTE;
        while ((--count >= 0) && (c = *buf++) != '\0') {
            char temp[CCHARW_MAX + 80];
        *tp++ = D_QUOTE;
        while ((--count >= 0) && (c = *buf++) != '\0') {
            char temp[CCHARW_MAX + 80];
@@ -262,10 +272,12 @@ _nc_viswibuf(const wint_t *buf)
        else
            mybuf = typeMalloc(wchar_t, mylen);
     }
        else
            mybuf = typeMalloc(wchar_t, mylen);
     }
-    for (n = 0; buf[n] != 0; ++n) {
-       mybuf[n] = (wchar_t) buf[n];
+    if (mybuf != 0) {
+       for (n = 0; buf[n] != 0; ++n) {
+           mybuf[n] = (wchar_t) buf[n];
+       }
+       mybuf[n] = L'\0';
     }
     }
-    mybuf[n] = L'\0';
 
     return _nc_viswbuf2(0, mybuf);
 }
 
     return _nc_viswbuf2(0, mybuf);
 }
@@ -273,13 +285,13 @@ _nc_viswibuf(const wint_t *buf)
 
 /* use these functions for displaying parts of a line within a window */
 NCURSES_EXPORT(const char *)
 
 /* use these functions for displaying parts of a line within a window */
 NCURSES_EXPORT(const char *)
-_nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
+_nc_viscbuf2(int bufnum, const NCURSES_CH_T *buf, int len)
 {
     char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
 {
     char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
-    int first;
-    const char *found;
 
     if (result != 0) {
 
     if (result != 0) {
+       int first = 0;
+
 #if USE_WIDEC_SUPPORT
        if (len < 0)
            len = _nc_wchstrlen(buf);
 #if USE_WIDEC_SUPPORT
        if (len < 0)
            len = _nc_wchstrlen(buf);
@@ -288,7 +300,6 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
        /*
         * Display one or more strings followed by attributes.
         */
        /*
         * Display one or more strings followed by attributes.
         */
-       first = 0;
        while (first < len) {
            attr_t attr = AttrOf(buf[first]);
            int last = len - 1;
        while (first < len) {
            attr_t attr = AttrOf(buf[first]);
            int last = len - 1;
@@ -304,7 +315,8 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
            (void) _nc_trace_bufcat(bufnum, l_brace);
            (void) _nc_trace_bufcat(bufnum, d_quote);
            for (j = first; j <= last; ++j) {
            (void) _nc_trace_bufcat(bufnum, l_brace);
            (void) _nc_trace_bufcat(bufnum, d_quote);
            for (j = first; j <= last; ++j) {
-               found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
+               const char *found = _nc_altcharset_name(attr, (chtype)
+                                                       CharOf(buf[j]));
                if (found != 0) {
                    (void) _nc_trace_bufcat(bufnum, found);
                    attr &= ~A_ALTCHARSET;
                if (found != 0) {
                    (void) _nc_trace_bufcat(bufnum, found);
                    attr &= ~A_ALTCHARSET;
@@ -313,9 +325,9 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
                if (!isWidecExt(buf[j])) {
                    PUTC_DATA;
 
                if (!isWidecExt(buf[j])) {
                    PUTC_DATA;
 
-                   PUTC_INIT;
                    for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
                        int k;
                    for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
                        int k;
+                       char temp[80];
 
                        PUTC_ch = buf[j].chars[PUTC_i];
                        if (PUTC_ch == L'\0') {
 
                        PUTC_ch = buf[j].chars[PUTC_i];
                        if (PUTC_ch == L'\0') {
@@ -323,12 +335,18 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
                                (void) _nc_trace_bufcat(bufnum, "\\000");
                            break;
                        }
                                (void) _nc_trace_bufcat(bufnum, "\\000");
                            break;
                        }
+                       PUTC_INIT;
                        PUTC_n = (int) wcrtomb(PUTC_buf,
                                               buf[j].chars[PUTC_i], &PUT_st);
                        PUTC_n = (int) wcrtomb(PUTC_buf,
                                               buf[j].chars[PUTC_i], &PUT_st);
-                       if (PUTC_n <= 0)
+                       if (PUTC_n <= 0 || buf[j].chars[PUTC_i] > 255) {
+                           _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+                                       "{%d:\\u%lx}",
+                                       _nc_wacs_width(buf[j].chars[PUTC_i]),
+                                       (unsigned long) buf[j].chars[PUTC_i]);
+                           (void) _nc_trace_bufcat(bufnum, temp);
                            break;
                            break;
+                       }
                        for (k = 0; k < PUTC_n; k++) {
                        for (k = 0; k < PUTC_n; k++) {
-                           char temp[80];
                            VisChar(temp, UChar(PUTC_buf[k]), sizeof(temp));
                            (void) _nc_trace_bufcat(bufnum, temp);
                        }
                            VisChar(temp, UChar(PUTC_buf[k]), sizeof(temp));
                            (void) _nc_trace_bufcat(bufnum, temp);
                        }
@@ -355,7 +373,7 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
 }
 
 NCURSES_EXPORT(const char *)
 }
 
 NCURSES_EXPORT(const char *)
-_nc_viscbuf(const NCURSES_CH_T * buf, int len)
+_nc_viscbuf(const NCURSES_CH_T *buf, int len)
 {
     return _nc_viscbuf2(0, buf, len);
 }
 {
     return _nc_viscbuf2(0, buf, len);
 }