summaryrefslogtreecommitdiffstats
path: root/lib/libncurses/lib_trace.c
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>1994-10-07 08:58:58 +0000
committerache <ache@FreeBSD.org>1994-10-07 08:58:58 +0000
commita80c0624fbd8bd1c784b0b5b7a0fd20b09d317b9 (patch)
tree4a94ca97fb2fc2fdc1fcdd522a66e39c6e763138 /lib/libncurses/lib_trace.c
downloadFreeBSD-src-a80c0624fbd8bd1c784b0b5b7a0fd20b09d317b9.zip
FreeBSD-src-a80c0624fbd8bd1c784b0b5b7a0fd20b09d317b9.tar.gz
Moved from ports with several enhancements
Diffstat (limited to 'lib/libncurses/lib_trace.c')
-rw-r--r--lib/libncurses/lib_trace.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/lib/libncurses/lib_trace.c b/lib/libncurses/lib_trace.c
new file mode 100644
index 0000000..c17f7dd
--- /dev/null
+++ b/lib/libncurses/lib_trace.c
@@ -0,0 +1,170 @@
+
+/* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for *
+* details. If they are missing then this copy is in violation of *
+* the copyright conditions. */
+
+/*
+ * lib_trace.c - Tracing/Debugging routines
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <errno.h>
+#include "curses.priv.h"
+#include <nterm.h>
+
+#if defined(BRAINDEAD)
+extern int errno;
+#endif
+
+int _tracing = 0;
+
+static int tracefd;
+
+void _tracef(char *fmt, ...);
+
+void _init_trace()
+{
+static int been_here = 0;
+
+ if (! been_here) {
+ been_here = 1;
+
+ if ((tracefd = creat("trace", 0644)) < 0) {
+ write(2, "curses: Can't open 'trace' file: ", 33);
+ write(2, strerror(errno), strlen(strerror(errno)));
+ write(2, "\n", 1);
+ exit(1);
+ }
+ _tracef("TRACING NCURSES version %s", NCURSES_VERSION);
+ }
+}
+
+
+void traceon()
+{
+
+ _tracing = 1;
+}
+
+
+void traceoff()
+{
+
+ _tracing = 0;
+}
+
+char *_traceattr(int newmode)
+{
+static char buf[BUFSIZ];
+struct {unsigned int val; char *name;}
+names[] =
+ {
+ {A_STANDOUT, "A_STANDOUT, ",},
+ {A_UNDERLINE, "A_UNDERLINE, ",},
+ {A_REVERSE, "A_REVERSE, ",},
+ {A_BLINK, "A_BLINK, ",},
+ {A_DIM, "A_DIM, ",},
+ {A_BOLD, "A_BOLD, ",},
+ {A_ALTCHARSET, "A_ALTCHARSET, ",},
+ {A_INVIS, "A_INVIS, ",},
+ {A_PROTECT, "A_PROTECT, ",},
+ {A_CHARTEXT, "A_CHARTEXT, ",},
+ {A_NORMAL, "A_NORMAL, ",},
+ },
+colors[] =
+ {
+ {COLOR_BLACK, "COLOR_BLACK",},
+ {COLOR_RED, "COLOR_RED",},
+ {COLOR_GREEN, "COLOR_GREEN",},
+ {COLOR_YELLOW, "COLOR_YELLOW",},
+ {COLOR_BLUE, "COLOR_BLUE",},
+ {COLOR_MAGENTA, "COLOR_MAGENTA",},
+ {COLOR_CYAN, "COLOR_CYAN",},
+ {COLOR_WHITE, "COLOR_WHITE",},
+ },
+ *sp;
+
+ strcpy(buf, "{");
+ for (sp = names; sp->val; sp++)
+ if (newmode & sp->val)
+ strcat(buf, sp->name);
+ if (newmode & A_COLOR)
+ {
+ int pairnum = PAIR_NUMBER(newmode);
+
+ (void) sprintf(buf + strlen(buf),
+ "COLOR_PAIR(%d) = (%s, %s), ",
+ pairnum,
+ colors[BG(color_pairs[pairnum])].name,
+ colors[FG(color_pairs[pairnum])].name
+ );
+ }
+ if ((newmode & A_ATTRIBUTES) == 0)
+ strcat(buf,"A_NORMAL, ");
+ if (buf[strlen(buf) - 2] == ',')
+ buf[strlen(buf) - 2] = '\0';
+ return(strcat(buf,"}"));
+}
+
+static char *visbuf(const char *buf)
+/* visibilize a given string */
+{
+ static char vbuf[BUFSIZ];
+ char *tp = vbuf;
+
+ while (*buf)
+ {
+ if (isprint(*buf) || *buf == ' ')
+ *tp++ = *buf++;
+ else if (*buf == '\n')
+ {
+ *tp++ = '\\'; *tp++ = 'n';
+ buf++;
+ }
+ else if (*buf == '\r')
+ {
+ *tp++ = '\\'; *tp++ = 'r';
+ buf++;
+ }
+ else if (*buf == '\b')
+ {
+ *tp++ = '\\'; *tp++ = 'b';
+ buf++;
+ }
+ else if (*buf == '\033')
+ {
+ *tp++ = '\\'; *tp++ = 'e';
+ buf++;
+ }
+ else if (*buf < ' ')
+ {
+ *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
+ buf++;
+ }
+ else
+ {
+ (void) sprintf(tp, "\\0x%02x", *buf++);
+ tp += strlen(tp);
+ }
+ }
+ *tp++ = '\0';
+ return(vbuf);
+}
+
+void
+_tracef(char *fmt, ...)
+{
+va_list ap;
+char buffer[256];
+char *vp;
+
+ va_start(ap, fmt);
+ vsprintf(buffer, fmt, ap);
+ vp = visbuf(buffer);
+ write(tracefd, vp, strlen(vp));
+ write(tracefd, "\n", 1);
+}
+
OpenPOWER on IntegriCloud