diff options
Diffstat (limited to 'lib/libcurses/trace')
-rw-r--r-- | lib/libcurses/trace/lib_trace.c | 322 | ||||
-rw-r--r-- | lib/libcurses/trace/lib_traceatr.c | 338 | ||||
-rw-r--r-- | lib/libcurses/trace/lib_tracebits.c | 124 | ||||
-rw-r--r-- | lib/libcurses/trace/lib_tracechr.c | 55 | ||||
-rw-r--r-- | lib/libcurses/trace/lib_tracedmp.c | 157 | ||||
-rw-r--r-- | lib/libcurses/trace/lib_tracemse.c | 129 | ||||
-rw-r--r-- | lib/libcurses/trace/trace_buf.c | 106 | ||||
-rw-r--r-- | lib/libcurses/trace/trace_tries.c | 34 | ||||
-rw-r--r-- | lib/libcurses/trace/trace_xnames.c | 4 | ||||
-rw-r--r-- | lib/libcurses/trace/varargs.c | 186 | ||||
-rw-r--r-- | lib/libcurses/trace/visbuf.c | 333 |
11 files changed, 1358 insertions, 430 deletions
diff --git a/lib/libcurses/trace/lib_trace.c b/lib/libcurses/trace/lib_trace.c index 2089f0e4977..ed8d45dd9e7 100644 --- a/lib/libcurses/trace/lib_trace.c +++ b/lib/libcurses/trace/lib_trace.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lib_trace.c,v 1.7 2003/03/17 19:16:59 millert Exp $ */ +/* $OpenBSD: lib_trace.c,v 1.8 2010/01/12 23:22:06 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -31,10 +31,16 @@ /**************************************************************************** * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * + * and: Thomas E. Dickey 1996-on * ****************************************************************************/ /* * lib_trace.c - Tracing/Debugging routines + * + * The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982. + * pcurses allowed one to enable/disable tracing using traceon() and traceoff() + * functions. ncurses provides a trace() function which allows one to + * selectively enable or disable several tracing features. */ #include <curses.priv.h> @@ -42,32 +48,73 @@ #include <ctype.h> -MODULE_ID("$From: lib_trace.c,v 1.38 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: lib_trace.c,v 1.8 2010/01/12 23:22:06 nicm Exp $") -NCURSES_EXPORT_VAR(unsigned) -_nc_tracing = 0; /* always define this */ +NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */ #ifdef TRACE -NCURSES_EXPORT_VAR(const char *) -_nc_tputs_trace = ""; -NCURSES_EXPORT_VAR(long) -_nc_outchars = 0; - static FILE *tracefp; /* default to writing to stderr */ +#if USE_REENTRANT +NCURSES_EXPORT(const char *) +NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void) +{ + return SP ? SP->_tputs_trace : _nc_prescreen._tputs_trace; +} +NCURSES_EXPORT(long) +NCURSES_PUBLIC_VAR(_nc_outchars) (void) +{ + return SP ? SP->_outchars : _nc_prescreen._outchars; +} +NCURSES_EXPORT(void) +_nc_set_tputs_trace(const char *s) +{ + if (SP) + SP->_tputs_trace = s; + else + _nc_prescreen._tputs_trace = s; +} +NCURSES_EXPORT(void) +_nc_count_outchars(long increment) +{ + if (SP) + SP->_outchars += increment; + else + _nc_prescreen._outchars += increment; +} +#else +NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = ""; +NCURSES_EXPORT_VAR(long) _nc_outchars = 0; +#endif + +#define TraceFP _nc_globals.trace_fp +#define TracePath _nc_globals.trace_fname +#define TraceLevel _nc_globals.trace_level NCURSES_EXPORT(void) -trace(const unsigned int tracelevel GCC_UNUSED) +trace(const unsigned int tracelevel) { - static bool been_here = FALSE; - static char my_name[] = "trace"; + if ((TraceFP == 0) && tracelevel) { + const char *mode = _nc_globals.init_trace ? "ab" : "wb"; - _nc_tracing = tracelevel; - if (!been_here && tracelevel) { - been_here = TRUE; + if (TracePath[0] == '\0') { + int size = sizeof(TracePath) - 12; + if (getcwd(TracePath, size) == 0) { + perror("curses: Can't get working directory"); + exit(EXIT_FAILURE); + } + TracePath[size] = '\0'; + assert(strlen(TracePath) <= size); + strlcat(TracePath, "/trace", sizeof(TracePath)); + if (_nc_is_dir_path(TracePath)) { + strlcat(TracePath, ".log", sizeof(TracePath)); + } + } - if (_nc_access(my_name, W_OK) < 0 - || (tracefp = fopen(my_name, "wb")) == 0) { - perror("curses: Can't open 'trace' file: "); + _nc_globals.init_trace = TRUE; + _nc_tracing = tracelevel; + if (_nc_access(TracePath, W_OK) < 0 + || (TraceFP = fopen(TracePath, mode)) == 0) { + perror("curses: Can't open 'trace' file"); exit(EXIT_FAILURE); } /* Try to set line-buffered mode, or (failing that) unbuffered, @@ -75,100 +122,46 @@ trace(const unsigned int tracelevel GCC_UNUSED) * end of each line. This is useful in case the program dies. */ #if HAVE_SETVBUF /* ANSI */ - (void) setvbuf(tracefp, (char *) 0, _IOLBF, 0); + (void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0); #elif HAVE_SETBUF /* POSIX */ - (void) setbuffer(tracefp, (char *) 0); -#endif - _tracef("TRACING NCURSES version %s", curses_version()); - } -} -#endif - -NCURSES_EXPORT(const char *) -_nc_visbuf2(int bufnum, const char *buf) -/* visibilize a given string */ -{ - size_t vbsize; - char *vbuf; - char *tp; - int c; - - if (buf == 0) - return ("(null)"); - if (buf == CANCELLED_STRING) - return ("(cancelled)"); - - vbsize = (strlen(buf) * 4) + 5; -#ifdef TRACE - tp = vbuf = _nc_trace_buf(bufnum, vbsize); -#else - { - static char *mybuf[2]; - mybuf[bufnum] = _nc_doalloc(mybuf[bufnum], vbsize); - tp = vbuf = mybuf[bufnum]; - } + (void) setbuffer(TraceFP, (char *) 0); #endif - *tp++ = '"'; - while ((c = *buf++) != '\0') { - if (c == '"') { - *tp++ = '\\'; - *tp++ = '"'; - } else if (is7bits(c) && (isgraph(c) || c == ' ')) { - *tp++ = c; - } else if (c == '\n') { - *tp++ = '\\'; - *tp++ = 'n'; - } else if (c == '\r') { - *tp++ = '\\'; - *tp++ = 'r'; - } else if (c == '\b') { - *tp++ = '\\'; - *tp++ = 'b'; - } else if (c == '\033') { - *tp++ = '\\'; - *tp++ = 'e'; - } else if (is7bits(c) && iscntrl(c)) { - *tp++ = '\\'; - *tp++ = '^'; - *tp++ = '@' + c; - } else { - snprintf(tp, vbsize - (tp - vbuf), "\\%03o", CharOf(c)); - tp += strlen(tp); + _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)", + NCURSES_VERSION, + NCURSES_VERSION_PATCH, + tracelevel); + } else if (tracelevel == 0) { + if (TraceFP != 0) { + fclose(TraceFP); + TraceFP = 0; } + _nc_tracing = tracelevel; + } else if (_nc_tracing != tracelevel) { + _nc_tracing = tracelevel; + _tracef("tracelevel=%#x", tracelevel); } - *tp++ = '"'; - *tp++ = '\0'; - return (vbuf); -} - -NCURSES_EXPORT(const char *) -_nc_visbuf(const char *buf) -{ - return _nc_visbuf2(0, buf); } -#ifdef TRACE -NCURSES_EXPORT(void) -_tracef(const char *fmt,...) +static void +_nc_va_tracef(const char *fmt, va_list ap) { static const char Called[] = T_CALLED(""); static const char Return[] = T_RETURN(""); - static int level; - va_list ap; + bool before = FALSE; bool after = FALSE; - int doit = _nc_tracing; + unsigned doit = _nc_tracing; int save_err = errno; if (strlen(fmt) >= sizeof(Called) - 1) { if (!strncmp(fmt, Called, sizeof(Called) - 1)) { before = TRUE; - level++; + TraceLevel++; } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) { after = TRUE; } if (before || after) { - if ((level <= 1) + if ((TraceLevel <= 1) || (doit & TRACE_ICALLS) != 0) doit &= (TRACE_CALLS | TRACE_CCALLS); else @@ -177,25 +170,57 @@ _tracef(const char *fmt,...) } if (doit != 0) { - if (tracefp == 0) - tracefp = stderr; + if (TraceFP == 0) + TraceFP = stderr; +#ifdef USE_PTHREADS + /* + * TRACE_ICALLS is "really" needed to show normal use with threaded + * applications, since anything can be running during a napms(), + * making it appear in the hierarchical trace as it other functions + * are being called. + * + * Rather than add the complication of a per-thread stack, just + * show the thread-id in each line of the trace. + */ +# if USE_WEAK_SYMBOLS + if ((pthread_self)) +# endif + fprintf(TraceFP, "%#lx:", (long) (void *) pthread_self()); +#endif if (before || after) { int n; - for (n = 1; n < level; n++) - fputs("+ ", tracefp); + for (n = 1; n < TraceLevel; n++) + fputs("+ ", TraceFP); } - va_start(ap, fmt); - vfprintf(tracefp, fmt, ap); - fputc('\n', tracefp); - va_end(ap); - fflush(tracefp); + vfprintf(TraceFP, fmt, ap); + fputc('\n', TraceFP); + fflush(TraceFP); } - if (after && level) - level--; + if (after && TraceLevel) + TraceLevel--; + errno = save_err; } +NCURSES_EXPORT(void) +_tracef(const char *fmt,...) +{ + va_list ap; + + va_start(ap, fmt); + _nc_va_tracef(fmt, ap); + va_end(ap); +} + +/* Trace 'bool' return-values */ +NCURSES_EXPORT(NCURSES_BOOL) +_nc_retrace_bool(NCURSES_BOOL code) +{ + T((T_RETURN("%s"), code ? "TRUE" : "FALSE")); + return code; +} + /* Trace 'int' return-values */ NCURSES_EXPORT(int) _nc_retrace_int(int code) @@ -204,6 +229,14 @@ _nc_retrace_int(int code) return code; } +/* Trace 'unsigned' return-values */ +NCURSES_EXPORT(unsigned) +_nc_retrace_unsigned(unsigned code) +{ + T((T_RETURN("%#x"), code)); + return code; +} + /* Trace 'char*' return-values */ NCURSES_EXPORT(char *) _nc_retrace_ptr(char *code) @@ -212,6 +245,38 @@ _nc_retrace_ptr(char *code) return code; } +/* Trace 'const char*' return-values */ +NCURSES_EXPORT(const char *) +_nc_retrace_cptr(const char *code) +{ + T((T_RETURN("%s"), _nc_visbuf(code))); + return code; +} + +/* Trace 'NCURSES_CONST void*' return-values */ +NCURSES_EXPORT(NCURSES_CONST void *) +_nc_retrace_cvoid_ptr(NCURSES_CONST void *code) +{ + T((T_RETURN("%p"), code)); + return code; +} + +/* Trace 'void*' return-values */ +NCURSES_EXPORT(void *) +_nc_retrace_void_ptr(void *code) +{ + T((T_RETURN("%p"), code)); + return code; +} + +/* Trace 'SCREEN *' return-values */ +NCURSES_EXPORT(SCREEN *) +_nc_retrace_sp(SCREEN *code) +{ + T((T_RETURN("%p"), code)); + return code; +} + /* Trace 'WINDOW *' return-values */ NCURSES_EXPORT(WINDOW *) _nc_retrace_win(WINDOW *code) @@ -219,4 +284,53 @@ _nc_retrace_win(WINDOW *code) T((T_RETURN("%p"), code)); return code; } + +#if USE_REENTRANT +/* + * Check if the given trace-mask is enabled. + * + * This function may be called from within one of the functions that fills + * in parameters for _tracef(), but in that case we do not want to lock the + * mutex, since it is already locked. + */ +NCURSES_EXPORT(int) +_nc_use_tracef(unsigned mask) +{ + bool result = FALSE; + + _nc_lock_global(tst_tracef); + if (!_nc_globals.nested_tracef++) { + if ((result = (_nc_tracing & (mask))) != 0 + && _nc_try_global(tracef) == 0) { + /* we will call _nc_locked_tracef(), no nesting so far */ + } else { + /* we will not call _nc_locked_tracef() */ + _nc_globals.nested_tracef = 0; + } + } else { + /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */ + result = (_nc_tracing & (mask)); + } + _nc_unlock_global(tst_tracef); + return result; +} + +/* + * We call this if _nc_use_tracef() returns true, which means we must unlock + * the tracef mutex. + */ +NCURSES_EXPORT(void) +_nc_locked_tracef(const char *fmt,...) +{ + va_list ap; + + va_start(ap, fmt); + _nc_va_tracef(fmt, ap); + va_end(ap); + + if (--(_nc_globals.nested_tracef) == 0) + _nc_unlock_global(tracef); +} +#endif /* USE_REENTRANT */ + #endif /* TRACE */ diff --git a/lib/libcurses/trace/lib_traceatr.c b/lib/libcurses/trace/lib_traceatr.c index e1b813bc3d9..34b65f3d7eb 100644 --- a/lib/libcurses/trace/lib_traceatr.c +++ b/lib/libcurses/trace/lib_traceatr.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lib_traceatr.c,v 1.5 2003/03/18 16:55:54 millert Exp $ */ +/* $OpenBSD: lib_traceatr.c,v 1.6 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1998,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -29,7 +29,8 @@ ****************************************************************************/ /**************************************************************************** - * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * + * Author: Thomas Dickey 1996-on * + * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * ****************************************************************************/ @@ -40,16 +41,42 @@ #include <curses.priv.h> #include <term.h> /* acs_chars */ -MODULE_ID("$From: lib_traceatr.c,v 1.32 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: lib_traceatr.c,v 1.6 2010/01/12 23:22:07 nicm Exp $") -#define COLOR_OF(c) (c < 0 || c > 7 ? "default" : colors[c].name) +#define COLOR_OF(c) ((c < 0) ? "default" : (c > 7 ? color_of(c) : colors[c].name)) #ifdef TRACE + +static const char l_brace[] = StringOf(L_BRACE); +static const char r_brace[] = StringOf(R_BRACE); + +#ifndef USE_TERMLIB + +#define my_buffer _nc_globals.traceatr_color_buf +#define my_select _nc_globals.traceatr_color_sel +#define my_cached _nc_globals.traceatr_color_last + +static char * +color_of(int c) +{ + if (c != my_cached) { + my_cached = c; + my_select = !my_select; + if (c == COLOR_DEFAULT) + strlcpy(my_buffer[my_select], "default", _nc_globals_traceatr_color_buf_size); + else + snprintf(my_buffer[my_select], _nc_globals_traceatr_color_buf_size, "color%d", c); + } + return my_buffer[my_select]; +} + +#undef my_buffer +#undef my_select +#endif /* !USE_TERMLIB */ + NCURSES_EXPORT(char *) -_traceattr2(int bufnum, attr_t newmode) +_traceattr2(int bufnum, chtype newmode) { - char *buf = _nc_trace_buf(bufnum, BUFSIZ); - char *tmp = buf; static const struct { unsigned int val; const char *name; @@ -70,7 +97,9 @@ _traceattr2(int bufnum, attr_t newmode) { A_COLOR, "A_COLOR" }, /* *INDENT-ON* */ - }, + } +#ifndef USE_TERMLIB + , colors[] = { /* *INDENT-OFF* */ @@ -84,46 +113,58 @@ _traceattr2(int bufnum, attr_t newmode) { COLOR_WHITE, "COLOR_WHITE" }, /* *INDENT-ON* */ - }; + } +#endif /* !USE_TERMLIB */ + ; size_t n; - unsigned save_nc_tracing = _nc_tracing; - _nc_tracing = 0; - - *tmp++ = '{'; - *tmp = '\0'; - - for (n = 0; n < SIZEOF(names); n++) { - if ((newmode & names[n].val) != 0) { - if (buf[1] != '\0') - strlcat(tmp, "|", BUFSIZ - (tmp - buf)); - strlcat(tmp, names[n].name, BUFSIZ - (tmp - buf)); - tmp += strlen(tmp); - - if (names[n].val == A_COLOR) { - short pairnum = PAIR_NUMBER(newmode); - short fg, bg; - - if (pair_content(pairnum, &fg, &bg) == OK) - (void) snprintf(tmp, BUFSIZ - (tmp - buf), - "{%d = {%s, %s}}", - pairnum, - COLOR_OF(fg), - COLOR_OF(bg) - ); - else - (void) snprintf(tmp, BUFSIZ - (tmp - buf), "{%d}", pairnum); + char temp[80]; + char *result = _nc_trace_buf(bufnum, BUFSIZ); + + if (result != 0) { + unsigned save_nc_tracing = _nc_tracing; + + _nc_tracing = 0; + + strlcpy(result, l_brace, BUFSIZ); + + for (n = 0; n < SIZEOF(names); n++) { + if ((newmode & names[n].val) != 0) { + if (result[1] != '\0') + result = _nc_trace_bufcat(bufnum, "|"); + result = _nc_trace_bufcat(bufnum, names[n].name); + + if (names[n].val == A_COLOR) { + short pairnum = PAIR_NUMBER(newmode); +#ifdef USE_TERMLIB + /* pair_content lives in libncurses */ + (void) snprintf(temp, sizeof(temp), "{%d}", pairnum); +#else + short fg, bg; + + if (pair_content(pairnum, &fg, &bg) == OK) { + (void) snprintf(temp, sizeof(temp), + "{%d = {%s, %s}}", + pairnum, + COLOR_OF(fg), + COLOR_OF(bg)); + } else { + (void) snprintf(temp, sizeof(temp), "{%d}", pairnum); + } +#endif + result = _nc_trace_bufcat(bufnum, temp); + } } } - } - if (AttrOf(newmode) == A_NORMAL) { - if (buf[1] != '\0') - strlcat(tmp, "|", BUFSIZ - (tmp - buf)); - strlcat(tmp, "A_NORMAL", BUFSIZ - (tmp - buf)); - } + if (ChAttrOf(newmode) == A_NORMAL) { + if (result != 0 && result[1] != '\0') + (void) _nc_trace_bufcat(bufnum, "|"); + (void) _nc_trace_bufcat(bufnum, "A_NORMAL"); + } - _nc_tracing = save_nc_tracing; - strlcat(buf, "}", BUFSIZ); - return (buf); + _nc_tracing = save_nc_tracing; + result = _nc_trace_bufcat(bufnum, r_brace); + } + return result; } NCURSES_EXPORT(char *) @@ -140,105 +181,180 @@ _nc_retrace_attr_t(attr_t code) return code; } -NCURSES_EXPORT(char *) -_tracechtype2(int bufnum, chtype ch) +const char * +_nc_altcharset_name(attr_t attr, chtype ch) { - char *buf = _nc_trace_buf(bufnum, BUFSIZ); - char *found = 0; + typedef struct { + unsigned int val; + const char *name; + } ALT_NAMES; + static const ALT_NAMES names[] = + { + {'l', "ACS_ULCORNER"}, /* upper left corner */ + {'m', "ACS_LLCORNER"}, /* lower left corner */ + {'k', "ACS_URCORNER"}, /* upper right corner */ + {'j', "ACS_LRCORNER"}, /* lower right corner */ + {'t', "ACS_LTEE"}, /* tee pointing right */ + {'u', "ACS_RTEE"}, /* tee pointing left */ + {'v', "ACS_BTEE"}, /* tee pointing up */ + {'w', "ACS_TTEE"}, /* tee pointing down */ + {'q', "ACS_HLINE"}, /* horizontal line */ + {'x', "ACS_VLINE"}, /* vertical line */ + {'n', "ACS_PLUS"}, /* large plus or crossover */ + {'o', "ACS_S1"}, /* scan line 1 */ + {'s', "ACS_S9"}, /* scan line 9 */ + {'`', "ACS_DIAMOND"}, /* diamond */ + {'a', "ACS_CKBOARD"}, /* checker board (stipple) */ + {'f', "ACS_DEGREE"}, /* degree symbol */ + {'g', "ACS_PLMINUS"}, /* plus/minus */ + {'~', "ACS_BULLET"}, /* bullet */ + {',', "ACS_LARROW"}, /* arrow pointing left */ + {'+', "ACS_RARROW"}, /* arrow pointing right */ + {'.', "ACS_DARROW"}, /* arrow pointing down */ + {'-', "ACS_UARROW"}, /* arrow pointing up */ + {'h', "ACS_BOARD"}, /* board of squares */ + {'i', "ACS_LANTERN"}, /* lantern symbol */ + {'0', "ACS_BLOCK"}, /* solid square block */ + {'p', "ACS_S3"}, /* scan line 3 */ + {'r', "ACS_S7"}, /* scan line 7 */ + {'y', "ACS_LEQUAL"}, /* less/equal */ + {'z', "ACS_GEQUAL"}, /* greater/equal */ + {'{', "ACS_PI"}, /* Pi */ + {'|', "ACS_NEQUAL"}, /* not equal */ + {'}', "ACS_STERLING"}, /* UK pound sign */ + {'\0', (char *) 0} + }; - buf[0] = '{'; - buf[1] = '\0'; - if (ch & A_ALTCHARSET) { + const char *result = 0; + + if ((attr & A_ALTCHARSET) && (acs_chars != 0)) { char *cp; - static const struct { - unsigned int val; - const char *name; - } names[] = - { - /* *INDENT-OFF* */ - { 'l', "ACS_ULCORNER" }, /* upper left corner */ - { 'm', "ACS_LLCORNER" }, /* lower left corner */ - { 'k', "ACS_URCORNER" }, /* upper right corner */ - { 'j', "ACS_LRCORNER" }, /* lower right corner */ - { 't', "ACS_LTEE" }, /* tee pointing right */ - { 'u', "ACS_RTEE" }, /* tee pointing left */ - { 'v', "ACS_BTEE" }, /* tee pointing up */ - { 'w', "ACS_TTEE" }, /* tee pointing down */ - { 'q', "ACS_HLINE" }, /* horizontal line */ - { 'x', "ACS_VLINE" }, /* vertical line */ - { 'n', "ACS_PLUS" }, /* large plus or crossover */ - { 'o', "ACS_S1" }, /* scan line 1 */ - { 's', "ACS_S9" }, /* scan line 9 */ - { '`', "ACS_DIAMOND" }, /* diamond */ - { 'a', "ACS_CKBOARD" }, /* checker board (stipple) */ - { 'f', "ACS_DEGREE" }, /* degree symbol */ - { 'g', "ACS_PLMINUS" }, /* plus/minus */ - { '~', "ACS_BULLET" }, /* bullet */ - { ',', "ACS_LARROW" }, /* arrow pointing left */ - { '+', "ACS_RARROW" }, /* arrow pointing right */ - { '.', "ACS_DARROW" }, /* arrow pointing down */ - { '-', "ACS_UARROW" }, /* arrow pointing up */ - { 'h', "ACS_BOARD" }, /* board of squares */ - { 'i', "ACS_LANTERN" }, /* lantern symbol */ - { '0', "ACS_BLOCK" }, /* solid square block */ - { 'p', "ACS_S3" }, /* scan line 3 */ - { 'r', "ACS_S7" }, /* scan line 7 */ - { 'y', "ACS_LEQUAL" }, /* less/equal */ - { 'z', "ACS_GEQUAL" }, /* greater/equal */ - { '{', "ACS_PI" }, /* Pi */ - { '|', "ACS_NEQUAL" }, /* not equal */ - { '}', "ACS_STERLING" }, /* UK pound sign */ - { '\0', (char *) 0 } - /* *INDENT-OFF* */ - }, - *sp; + char *found = 0; + const ALT_NAMES *sp; for (cp = acs_chars; cp[0] && cp[1]; cp += 2) { - if (TextOf(cp[1]) == TextOf(ch)) { + if (ChCharOf(cp[1]) == ChCharOf(ch)) { found = cp; /* don't exit from loop - there may be redefinitions */ } } if (found != 0) { - ch = TextOf(*found); + ch = ChCharOf(*found); for (sp = names; sp->val; sp++) if (sp->val == ch) { - (void) strlcat(buf, sp->name, BUFSIZ); - ch &= ~A_ALTCHARSET; + result = sp->name; break; } } } + return result; +} + +NCURSES_EXPORT(char *) +_tracechtype2(int bufnum, chtype ch) +{ + const char *found; + char *result = _nc_trace_buf(bufnum, BUFSIZ); - if (found == 0) - (void) strlcat(buf, _tracechar(TextOf(ch)), BUFSIZ); + if (result != 0) { + strlcpy(result, l_brace, BUFSIZ); + if ((found = _nc_altcharset_name(ChAttrOf(ch), ch)) != 0) { + (void) _nc_trace_bufcat(bufnum, found); + } else + (void) _nc_trace_bufcat(bufnum, _nc_tracechar(SP, (int) ChCharOf(ch))); - if (AttrOf(ch) != A_NORMAL) - (void) snprintf(buf + strlen(buf), BUFSIZ - strlen(buf), " | %s", - _traceattr2(bufnum + 20, AttrOf(ch))); + if (ChAttrOf(ch) != A_NORMAL) { + (void) _nc_trace_bufcat(bufnum, " | "); + (void) _nc_trace_bufcat(bufnum, + _traceattr2(bufnum + 20, ChAttrOf(ch))); + } - strlcat(buf, "}", BUFSIZ); - return (buf); + result = _nc_trace_bufcat(bufnum, r_brace); + } + return result; } NCURSES_EXPORT(char *) -_tracechtype (chtype ch) +_tracechtype(chtype ch) { return _tracechtype2(0, ch); } /* Trace 'chtype' return-values */ -NCURSES_EXPORT(attr_t) -_nc_retrace_chtype (attr_t code) +NCURSES_EXPORT(chtype) +_nc_retrace_chtype(chtype code) { T((T_RETURN("%s"), _tracechtype(code))); return code; } -#else -extern NCURSES_EXPORT(void) _nc_lib_traceatr (void); -NCURSES_EXPORT(void) _nc_lib_traceatr (void) +#if USE_WIDEC_SUPPORT +NCURSES_EXPORT(char *) +_tracecchar_t2(int bufnum, const cchar_t *ch) { + char *result = _nc_trace_buf(bufnum, BUFSIZ); + attr_t attr; + const char *found; + + if (result != 0) { + strlcpy(result, l_brace, BUFSIZ); + if (ch != 0) { + attr = AttrOfD(ch); + if ((found = _nc_altcharset_name(attr, (chtype) CharOfD(ch))) != 0) { + (void) _nc_trace_bufcat(bufnum, found); + attr &= ~A_ALTCHARSET; + } else if (isWidecExt(CHDEREF(ch))) { + (void) _nc_trace_bufcat(bufnum, "{NAC}"); + attr &= ~A_CHARTEXT; + } else { + PUTC_DATA; + int n; + + PUTC_INIT; + (void) _nc_trace_bufcat(bufnum, "{ "); + for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) { + PUTC_ch = ch->chars[PUTC_i]; + if (PUTC_ch == L'\0') + break; + PUTC_n = wcrtomb(PUTC_buf, ch->chars[PUTC_i], &PUT_st); + if (PUTC_n <= 0) { + if (PUTC_ch != L'\0') { + /* it could not be a multibyte sequence */ + (void) _nc_trace_bufcat(bufnum, + _nc_tracechar(SP, + UChar(ch->chars[PUTC_i]))); + } + break; + } + for (n = 0; n < PUTC_n; n++) { + if (n) + (void) _nc_trace_bufcat(bufnum, ", "); + (void) _nc_trace_bufcat(bufnum, + _nc_tracechar(SP, + UChar(PUTC_buf[n]))); + } + } + (void) _nc_trace_bufcat(bufnum, " }"); + } + if (attr != A_NORMAL) { + (void) _nc_trace_bufcat(bufnum, " | "); + (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); + } + } + + result = _nc_trace_bufcat(bufnum, r_brace); + } + return result; } + +NCURSES_EXPORT(char *) +_tracecchar_t(const cchar_t *ch) +{ + return _tracecchar_t2(0, ch); +} +#endif + +#else +EMPTY_MODULE(_nc_lib_traceatr) #endif /* TRACE */ diff --git a/lib/libcurses/trace/lib_tracebits.c b/lib/libcurses/trace/lib_tracebits.c index b1ad17da12c..4905807f11f 100644 --- a/lib/libcurses/trace/lib_tracebits.c +++ b/lib/libcurses/trace/lib_tracebits.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lib_tracebits.c,v 1.9 2003/03/18 16:55:54 millert Exp $ */ +/* $OpenBSD: lib_tracebits.c,v 1.10 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -31,12 +31,13 @@ /**************************************************************************** * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * + * and: Thomas E. Dickey 1996-on * ****************************************************************************/ #include <curses.priv.h> #include <term.h> /* cur_term */ -MODULE_ID("$From: lib_tracebits.c,v 1.9 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: lib_tracebits.c,v 1.10 2010/01/12 23:22:07 nicm Exp $") #if SVR4_TERMIO && !defined(_POSIX_SOURCE) #define _POSIX_SOURCE @@ -54,10 +55,27 @@ MODULE_ID("$From: lib_tracebits.c,v 1.9 2000/12/10 03:02:45 tom Exp $") #ifndef TOSTOP #define TOSTOP 0 #endif + #ifndef IEXTEN #define IEXTEN 0 #endif +#ifndef ONLCR +#define ONLCR 0 +#endif + +#ifndef OCRNL +#define OCRNL 0 +#endif + +#ifndef ONOCR +#define ONOCR 0 +#endif + +#ifndef ONLRET +#define ONLRET 0 +#endif + #ifdef TRACE typedef struct { @@ -84,7 +102,7 @@ lookup_bits(char *buf, size_t bufsize, const BITNAMES * table, const char *label } NCURSES_EXPORT(char *) -_nc_tracebits(void) +_nc_trace_ttymode(TTY * tty) /* describe the state of the terminal control bits exactly */ { char *buf; @@ -109,8 +127,13 @@ _nc_tracebits(void) }, oflags[] = { {OPOST, "OPOST"}, + {OFLAGS_TABS, "XTABS"}, + {ONLCR, "ONLCR"}, + {OCRNL, "OCRNL"}, + {ONOCR, "ONOCR"}, + {ONLRET, "ONLRET"}, {0, NULL} -#define ALLOUT (OPOST) +#define ALLOUT (OPOST|OFLAGS_TABS|ONLCR|OCRNL|ONOCR|ONLRET) }, cflags[] = { {CLOCAL, "CLOCAL"}, @@ -140,60 +163,55 @@ _nc_tracebits(void) }; bufsize = 8 + sizeof(iflags) + 8 + sizeof(oflags) + 8 + sizeof(cflags) + - 8 + sizeof(lflags) + 8; + 8 + sizeof(lflags) + 8; buf = _nc_trace_buf(0, bufsize); - if (cur_term->Nttyb.c_iflag & ALLIN) - lookup_bits(buf, bufsize, iflags, "iflags", cur_term->Nttyb.c_iflag); + if (buf != 0) { + + if (tty->c_iflag & ALLIN) + lookup_bits(buf, bufsize, iflags, "iflags", tty->c_iflag); - if (cur_term->Nttyb.c_oflag & ALLOUT) - lookup_bits(buf, bufsize, oflags, "oflags", cur_term->Nttyb.c_oflag); + if (tty->c_oflag & ALLOUT) + lookup_bits(buf, bufsize, oflags, "oflags", tty->c_oflag); - if (cur_term->Nttyb.c_cflag & ALLCTRL) - lookup_bits(buf, bufsize, cflags, "cflags", cur_term->Nttyb.c_cflag); + if (tty->c_cflag & ALLCTRL) + lookup_bits(buf, bufsize, cflags, "cflags", tty->c_cflag); #if defined(CS5) && defined(CS8) - { - static struct { - char *name; - int value; - } csizes[] = { - { - "CS5 ", CS5 - }, + { + static struct { + int value; + const char *name; + } csizes[] = { +#define CS_DATA(name) { name, #name " " } + CS_DATA(CS5), #ifdef CS6 - { - "CS6 ", CS6 - }, + CS_DATA(CS6), #endif #ifdef CS7 - { - "CS7 ", CS7 - }, -#endif - { - "CS8 ", CS8 - }, - }; - char *result = "CSIZE? "; - int value = (cur_term->Nttyb.c_cflag & CSIZE); - unsigned n; - - if (value != 0) { - for (n = 0; n < SIZEOF(csizes); n++) { - if (csizes[n].value == value) { - result = csizes[n].name; - break; + CS_DATA(CS7), +#endif + CS_DATA(CS8), + }; + const char *result = "CSIZE? "; + int value = (tty->c_cflag & CSIZE); + unsigned n; + + if (value != 0) { + for (n = 0; n < SIZEOF(csizes); n++) { + if (csizes[n].value == value) { + result = csizes[n].name; + break; + } } } + strlcat(buf, result, bufsize); } - strlcat(buf, result, bufsize); - } #endif - if (cur_term->Nttyb.c_lflag & ALLLOCAL) - lookup_bits(buf, bufsize, lflags, "lflags", cur_term->Nttyb.c_lflag); - + if (tty->c_lflag & ALLLOCAL) + lookup_bits(buf, bufsize, lflags, "lflags", tty->c_lflag); + } #else /* reference: ttcompat(4M) on SunOS 4.1 */ #ifndef EVENP @@ -230,18 +248,24 @@ _nc_tracebits(void) buf = _nc_trace_buf(0, 8 + sizeof(cflags)); - - if (cur_term->Nttyb.sg_flags & ALLCTRL) { - lookup_bits(buf, bufsize, cflags, "cflags", cur_term->Nttyb.sg_flags); + if (buf != 0) { + if (tty->sg_flags & ALLCTRL) { + lookup_bits(buf, cflags, "cflags", tty->sg_flags); + } } #endif return (buf); } + +NCURSES_EXPORT(char *) +_nc_tracebits(void) +{ + return _nc_trace_ttymode(&(cur_term->Nttyb)); +} #else NCURSES_EXPORT(char *) _nc_tracebits(void) { - static char tmp[] = ""; - return tmp; + return NULL; } #endif /* TRACE */ diff --git a/lib/libcurses/trace/lib_tracechr.c b/lib/libcurses/trace/lib_tracechr.c index f69a9c59a1f..e909ab79262 100644 --- a/lib/libcurses/trace/lib_tracechr.c +++ b/lib/libcurses/trace/lib_tracechr.c @@ -1,5 +1,7 @@ +/* $OpenBSD: lib_tracechr.c,v 1.5 2010/01/12 23:22:07 nicm Exp $ */ + /**************************************************************************** - * Copyright (c) 1998,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -29,6 +31,7 @@ /**************************************************************************** * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * + * and: Thomas E. Dickey 1996-on * ****************************************************************************/ /* @@ -36,23 +39,49 @@ */ #include <curses.priv.h> -MODULE_ID("$From: lib_tracechr.c,v 1.4 2000/12/10 03:02:45 tom Exp $") +#include <ctype.h> + +MODULE_ID("$Id: lib_tracechr.c,v 1.5 2010/01/12 23:22:07 nicm Exp $") #ifdef TRACE + NCURSES_EXPORT(char *) -_tracechar(const unsigned char ch) +_nc_tracechar(SCREEN *sp, int ch) { - static char crep[20]; - (void) snprintf(crep, sizeof(crep), "'%s' = 0x%02x", unctrl(ch), - (unsigned) ch); - return (crep); + NCURSES_CONST char *name; + char *MyBuffer = ((sp != 0) + ? sp->tracechr_buf + : _nc_globals.tracechr_buf); + size_t len = ((sp != 0) + ? _nc_screen_tracechr_buf_size + : _nc_globals_traceatr_color_buf_size); + + if (ch > KEY_MIN || ch < 0) { + name = _nc_keyname(sp, ch); + if (name == 0 || *name == '\0') + name = "NULL"; + (void) snprintf(MyBuffer, len, "'%.30s' = %#03o", name, ch); + } else if (!is8bits(ch) || !isprint(UChar(ch))) { + /* + * workaround for glibc bug: + * sprintf changes the result from unctrl() to an empty string if it + * does not correspond to a valid multibyte sequence. + */ + (void) snprintf(MyBuffer, len, "%#03o", ch); + } else { + name = _nc_unctrl(sp, (chtype) ch); + if (name == 0 || *name == 0) + name = "null"; /* shouldn't happen */ + (void) snprintf(MyBuffer, len, "'%.30s' = %#03o", name, ch); + } + return (MyBuffer); } -#else -extern -NCURSES_EXPORT(void) -_nc_lib_tracechr(void); -NCURSES_EXPORT(void) -_nc_lib_tracechr(void) + +NCURSES_EXPORT(char *) +_tracechar(int ch) { + return _nc_tracechar(SP, ch); } +#else +EMPTY_MODULE(_nc_lib_tracechr) #endif diff --git a/lib/libcurses/trace/lib_tracedmp.c b/lib/libcurses/trace/lib_tracedmp.c index 4e557d65974..71759dc21db 100644 --- a/lib/libcurses/trace/lib_tracedmp.c +++ b/lib/libcurses/trace/lib_tracedmp.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lib_tracedmp.c,v 1.3 2003/03/17 19:16:59 millert Exp $ */ +/* $OpenBSD: lib_tracedmp.c,v 1.4 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1998,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -29,7 +29,8 @@ ****************************************************************************/ /**************************************************************************** - * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * + * Author: Thomas E. Dickey 1996-on * + * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * ****************************************************************************/ @@ -38,94 +39,148 @@ */ #include <curses.priv.h> +#include <ctype.h> -MODULE_ID("$From: lib_tracedmp.c,v 1.16 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: lib_tracedmp.c,v 1.4 2010/01/12 23:22:07 nicm Exp $") #ifdef TRACE + +#define my_buffer _nc_globals.tracedmp_buf +#define my_length _nc_globals.tracedmp_used + NCURSES_EXPORT(void) _tracedump(const char *name, WINDOW *win) { int i, j, n, width; /* compute narrowest possible display width */ - for (width = i = 0; i <= win->_maxy; i++) { + for (width = i = 0; i <= win->_maxy; ++i) { n = 0; - for (j = 0; j <= win->_maxx; j++) - if (win->_line[i].text[j] != ' ') + for (j = 0; j <= win->_maxx; ++j) { + if (CharOf(win->_line[i].text[j]) != L(' ') + || AttrOf(win->_line[i].text[j]) != A_NORMAL + || GetPair(win->_line[i].text[j]) != 0) { n = j; + } + } if (n > width) width = n; } if (width < win->_maxx) ++width; + if (++width + 1 > (int) my_length) { + my_length = 2 * (width + 1); + my_buffer = typeRealloc(char, my_length, my_buffer); + } - for (n = 0; n <= win->_maxy; n++) { - char buf[BUFSIZ], *ep; + for (n = 0; n <= win->_maxy; ++n) { + char *ep = my_buffer; bool haveattrs, havecolors; - /* dump A_CHARTEXT part */ - (void) snprintf(buf, sizeof(buf), "%s[%2d] %3d%3d ='", - name, n, - win->_line[n].firstchar, - win->_line[n].lastchar); - ep = buf + strlen(buf); - for (j = 0; j <= width; j++) { - ep[j] = TextOf(win->_line[n].text[j]); - if (ep[j] == 0) - ep[j] = '.'; + /* + * Dump A_CHARTEXT part. It is more important to make the grid line up + * in the trace file than to represent control- and wide-characters, so + * we map those to '.' and '?' respectively. + */ + for (j = 0; j < width; ++j) { + chtype test = CharOf(win->_line[n].text[j]); + ep[j] = (char) ((UChar(test) == test +#if USE_WIDEC_SUPPORT + && (win->_line[n].text[j].chars[1] == 0) +#endif + ) + ? (iscntrl(UChar(test)) + ? '.' + : UChar(test)) + : '?'); } - ep[j] = '\''; - ep[j + 1] = '\0'; - _tracef("%s", buf); + ep[j] = '\0'; + _tracef("%s[%2d] %3ld%3ld ='%s'", + name, n, + (long) win->_line[n].firstchar, + (long) win->_line[n].lastchar, + ep); + + /* if there are multi-column characters on the line, print them now */ + if_WIDEC({ + bool multicolumn = FALSE; + for (j = 0; j < width; ++j) + if (WidecExt(win->_line[n].text[j]) != 0) { + multicolumn = TRUE; + break; + } + if (multicolumn) { + ep = my_buffer; + for (j = 0; j < width; ++j) { + int test = WidecExt(win->_line[n].text[j]); + if (test) { + ep[j] = (char) (test + '0'); + } else { + ep[j] = ' '; + } + } + ep[j] = '\0'; + _tracef("%*s[%2d]%*s='%s'", (int) strlen(name), + "widec", n, 8, " ", my_buffer); + } + }); /* dump A_COLOR part, will screw up if there are more than 96 */ havecolors = FALSE; - for (j = 0; j <= width; j++) - if (win->_line[n].text[j] & A_COLOR) { + for (j = 0; j < width; ++j) + if (GetPair(win->_line[n].text[j]) != 0) { havecolors = TRUE; break; } if (havecolors) { - (void) snprintf(buf, sizeof(buf), "%*s[%2d]%*s='", - (int) strlen(name), "colors", n, 8, " "); - ep = buf + strlen(buf); - for (j = 0; j <= width; j++) - ep[j] = CharOf(win->_line[n].text[j] >> 8) + ' '; - ep[j] = '\''; - ep[j + 1] = '\0'; - _tracef("%s", buf); + ep = my_buffer; + for (j = 0; j < width; ++j) { + int pair = GetPair(win->_line[n].text[j]); + if (pair >= 52) + ep[j] = '?'; + else if (pair >= 36) + ep[j] = (char) (pair + 'A'); + else if (pair >= 10) + ep[j] = (char) (pair + 'a'); + else if (pair >= 1) + ep[j] = (char) (pair + '0'); + else + ep[j] = ' '; + } + ep[j] = '\0'; + _tracef("%*s[%2d]%*s='%s'", (int) strlen(name), + "colors", n, 8, " ", my_buffer); } - for (i = 0; i < 4; i++) { + for (i = 0; i < 4; ++i) { const char *hex = " 123456789ABCDEF"; - chtype mask = (0xf << ((i + 4) * 4)); + attr_t mask = (0xf << ((i + 4) * 4)); haveattrs = FALSE; - for (j = 0; j <= width; j++) - if (win->_line[n].text[j] & mask) { + for (j = 0; j < width; ++j) + if (AttrOf(win->_line[n].text[j]) & mask) { haveattrs = TRUE; break; } if (haveattrs) { - (void) snprintf(buf, sizeof(buf), "%*s%d[%2d]%*s='", - (int) strlen(name) - 1, "attrs", i, n, 8, " "); - ep = buf + strlen(buf); - for (j = 0; j <= width; j++) - ep[j] = hex[(win->_line[n].text[j] & mask) >> ((i + 4) * 4)]; - ep[j] = '\''; - ep[j + 1] = '\0'; - _tracef("%s", buf); + ep = my_buffer; + for (j = 0; j < width; ++j) + ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >> + ((i + 4) * 4)]; + ep[j] = '\0'; + _tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) - + 1, "attrs", i, n, 8, " ", my_buffer); } } } +#if NO_LEAKS + free(my_buffer); + my_buffer = 0; + my_length = 0; +#endif } + #else -extern -NCURSES_EXPORT(void) -_nc_lib_tracedmp(void); -NCURSES_EXPORT(void) -_nc_lib_tracedmp(void) -{ -} +EMPTY_MODULE(_nc_lib_tracedmp) #endif /* TRACE */ diff --git a/lib/libcurses/trace/lib_tracemse.c b/lib/libcurses/trace/lib_tracemse.c index 3346d87e224..798aae9f563 100644 --- a/lib/libcurses/trace/lib_tracemse.c +++ b/lib/libcurses/trace/lib_tracemse.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lib_tracemse.c,v 1.4 2003/03/18 16:55:54 millert Exp $ */ +/* $OpenBSD: lib_tracemse.c,v 1.5 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1998,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -31,6 +31,7 @@ /**************************************************************************** * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * + * and: Thomas E. Dickey 1996-on * ****************************************************************************/ /* @@ -39,62 +40,92 @@ #include <curses.priv.h> -MODULE_ID("$From: lib_tracemse.c,v 1.8 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: lib_tracemse.c,v 1.5 2010/01/12 23:22:07 nicm Exp $") #ifdef TRACE +#define my_buffer sp->tracemse_buf + NCURSES_EXPORT(char *) -_tracemouse(MEVENT const *ep) +_nc_tracemouse(SCREEN *sp, MEVENT const *ep) { - static char buf[80]; - - (void) snprintf(buf, sizeof(buf), "id %2d at (%2d, %2d, %2d) state %4lx = {", - ep->id, ep->x, ep->y, ep->z, ep->bstate); - -#define SHOW(m, s) if ((ep->bstate & m)==m) {strlcat(buf,s,sizeof(buf)); strlcat(buf, ", ",sizeof(buf));} - SHOW(BUTTON1_RELEASED, "release-1") - SHOW(BUTTON1_PRESSED, "press-1") - SHOW(BUTTON1_CLICKED, "click-1") - SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1") - SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1") - SHOW(BUTTON1_RESERVED_EVENT, "reserved-1") - SHOW(BUTTON2_RELEASED, "release-2") - SHOW(BUTTON2_PRESSED, "press-2") - SHOW(BUTTON2_CLICKED, "click-2") - SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2") - SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2") - SHOW(BUTTON2_RESERVED_EVENT, "reserved-2") - SHOW(BUTTON3_RELEASED, "release-3") - SHOW(BUTTON3_PRESSED, "press-3") - SHOW(BUTTON3_CLICKED, "click-3") - SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3") - SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3") - SHOW(BUTTON3_RESERVED_EVENT, "reserved-3") - SHOW(BUTTON4_RELEASED, "release-4") - SHOW(BUTTON4_PRESSED, "press-4") - SHOW(BUTTON4_CLICKED, "click-4") - SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4") - SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4") - SHOW(BUTTON4_RESERVED_EVENT, "reserved-4") - SHOW(BUTTON_CTRL, "ctrl") - SHOW(BUTTON_SHIFT, "shift") - SHOW(BUTTON_ALT, "alt") - SHOW(ALL_MOUSE_EVENTS, "all-events") - SHOW(REPORT_MOUSE_POSITION, "position") + (void) snprintf(my_buffer, TRACEMSE_MAX, TRACEMSE_FMT, + ep->id, + ep->x, + ep->y, + ep->z, + (unsigned long) ep->bstate); + +#define SHOW(m, s) \ + if ((ep->bstate & m) == m) { \ + strlcat(my_buffer, s, TRACEMSE_MAX); \ + strlcat(my_buffer, ", ", TRACEMSE_MAX); \ +} + + SHOW(BUTTON1_RELEASED, "release-1"); + SHOW(BUTTON1_PRESSED, "press-1"); + SHOW(BUTTON1_CLICKED, "click-1"); + SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1"); + SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1"); +#if NCURSES_MOUSE_VERSION == 1 + SHOW(BUTTON1_RESERVED_EVENT, "reserved-1"); +#endif + + SHOW(BUTTON2_RELEASED, "release-2"); + SHOW(BUTTON2_PRESSED, "press-2"); + SHOW(BUTTON2_CLICKED, "click-2"); + SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2"); + SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2"); +#if NCURSES_MOUSE_VERSION == 1 + SHOW(BUTTON2_RESERVED_EVENT, "reserved-2"); +#endif + + SHOW(BUTTON3_RELEASED, "release-3"); + SHOW(BUTTON3_PRESSED, "press-3"); + SHOW(BUTTON3_CLICKED, "click-3"); + SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3"); + SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3"); +#if NCURSES_MOUSE_VERSION == 1 + SHOW(BUTTON3_RESERVED_EVENT, "reserved-3"); +#endif + + SHOW(BUTTON4_RELEASED, "release-4"); + SHOW(BUTTON4_PRESSED, "press-4"); + SHOW(BUTTON4_CLICKED, "click-4"); + SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4"); + SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4"); +#if NCURSES_MOUSE_VERSION == 1 + SHOW(BUTTON4_RESERVED_EVENT, "reserved-4"); +#endif + +#if NCURSES_MOUSE_VERSION == 2 + SHOW(BUTTON5_RELEASED, "release-5"); + SHOW(BUTTON5_PRESSED, "press-5"); + SHOW(BUTTON5_CLICKED, "click-5"); + SHOW(BUTTON5_DOUBLE_CLICKED, "doubleclick-5"); + SHOW(BUTTON5_TRIPLE_CLICKED, "tripleclick-5"); +#endif + + SHOW(BUTTON_CTRL, "ctrl"); + SHOW(BUTTON_SHIFT, "shift"); + SHOW(BUTTON_ALT, "alt"); + SHOW(ALL_MOUSE_EVENTS, "all-events"); + SHOW(REPORT_MOUSE_POSITION, "position"); + #undef SHOW - if (buf[strlen(buf) - 1] == ' ') - buf[strlen(buf) - 2] = '\0'; - (void) strlcat(buf, "}", sizeof(buf)); - return (buf); + if (my_buffer[strlen(my_buffer) - 1] == ' ') + my_buffer[strlen(my_buffer) - 2] = '\0'; + (void) strlcat(my_buffer, "}", TRACEMSE_MAX); + return (my_buffer); } -#else /* !TRACE */ -/* don't make empty module */ -NCURSES_EXPORT(void) -_nc_lib_tracemouse(void); -NCURSES_EXPORT(void) -_nc_lib_tracemouse(void) +NCURSES_EXPORT(char *) +_tracemouse(MEVENT const *ep) { + return _nc_tracemouse(SP, ep); } + +#else /* !TRACE */ +EMPTY_MODULE(_nc_lib_tracemouse) #endif diff --git a/lib/libcurses/trace/trace_buf.c b/lib/libcurses/trace/trace_buf.c index 060ef5e686d..2b57ef77a3f 100644 --- a/lib/libcurses/trace/trace_buf.c +++ b/lib/libcurses/trace/trace_buf.c @@ -1,7 +1,7 @@ -/* $OpenBSD: trace_buf.c,v 1.3 2001/01/22 18:01:58 millert Exp $ */ +/* $OpenBSD: trace_buf.c,v 1.4 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1998,2000 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 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 * @@ -29,7 +29,7 @@ ****************************************************************************/ /**************************************************************************** - * Author: Thomas E. Dickey <dickey@clark.net> 1997 * + * Author: Thomas E. Dickey 1997-on * ****************************************************************************/ /* * trace_buf.c - Tracing/Debugging buffers (attributes) @@ -37,47 +37,85 @@ #include <curses.priv.h> -MODULE_ID("$From: trace_buf.c,v 1.9 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: trace_buf.c,v 1.4 2010/01/12 23:22:07 nicm Exp $") -typedef struct { - char *text; - size_t size; -} LIST; +#define MyList _nc_globals.tracebuf_ptr +#define MySize _nc_globals.tracebuf_used -NCURSES_EXPORT(char *) -_nc_trace_buf(int bufnum, size_t want) +static char * +_nc_trace_alloc(int bufnum, size_t want) { - static LIST *list; - static size_t have; +#ifdef TRACE + char *result = 0; + + if (bufnum >= 0) { + if ((size_t) (bufnum + 1) > MySize) { + size_t need = (bufnum + 1) * 2; + if ((MyList = typeRealloc(TRACEBUF, need, MyList)) != 0) { + while (need > MySize) + MyList[MySize++].text = 0; + } + } + if (MyList != 0) { + if (MyList[bufnum].text == 0 + || want > MyList[bufnum].size) { + MyList[bufnum].text = typeRealloc(char, want, MyList[bufnum].text); + if (MyList[bufnum].text != 0) + MyList[bufnum].size = want; + } + result = MyList[bufnum].text; + } + } #if NO_LEAKS - if (bufnum < 0) { - if (have) { - while (have--) { - free(list[have].text); + else { + if (MySize) { + if (MyList) { + while (MySize--) { + if (MyList[MySize].text != 0) { + free(MyList[MySize].text); + } + } + free(MyList); + MyList = 0; } - free(list); + MySize = 0; } - return 0; } #endif + return result; +#else + return NULL; +#endif +} - if ((size_t) (bufnum + 1) > have) { - size_t need = (bufnum + 1) * 2; - if ((list = typeRealloc(LIST, need, list)) == 0) - return (0); - while (need > have) - list[have++].text = 0; - } +/* + * (re)Allocate a buffer big enough for the caller's wants. + */ +NCURSES_EXPORT(char *) +_nc_trace_buf(int bufnum, size_t want) +{ + char *result = _nc_trace_alloc(bufnum, want); + if (result != 0) + *result = '\0'; + return result; +} - if (list[bufnum].text == 0 - || want > list[bufnum].size) { - if ((list[bufnum].text = typeRealloc(char, want, list[bufnum].text)) - != 0) - list[bufnum].size = want; - } +/* + * Append a new string to an existing buffer. + */ +NCURSES_EXPORT(char *) +_nc_trace_bufcat(int bufnum, const char *value) +{ + char *buffer = _nc_trace_alloc(bufnum, 0); + if (buffer != 0) { + size_t have = strlen(buffer), length; + + length = 1 + have + strlen(value); + buffer = _nc_trace_alloc(bufnum, length); + if (buffer != 0) + (void) strlcpy(buffer + have, value, length); - if (list[bufnum].text != 0) - *(list[bufnum].text) = '\0'; - return list[bufnum].text; + } + return buffer; } diff --git a/lib/libcurses/trace/trace_tries.c b/lib/libcurses/trace/trace_tries.c index d0dd538d9be..0a138b48cab 100644 --- a/lib/libcurses/trace/trace_tries.c +++ b/lib/libcurses/trace/trace_tries.c @@ -1,7 +1,7 @@ -/* $OpenBSD: trace_tries.c,v 1.4 2001/01/22 18:01:59 millert Exp $ */ +/* $OpenBSD: trace_tries.c,v 1.5 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** - * Copyright (c) 1999,2000 Free Software Foundation, Inc. * + * Copyright (c) 1999-2007,2008 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 * @@ -37,25 +37,27 @@ #include <curses.priv.h> -MODULE_ID("$From: trace_tries.c,v 1.8 2000/12/10 03:03:51 tom Exp $") +MODULE_ID("$Id: trace_tries.c,v 1.5 2010/01/12 23:22:07 nicm Exp $") #ifdef TRACE -static unsigned char *buffer; -static unsigned len; +#define my_buffer _nc_globals.tracetry_buf +#define my_length _nc_globals.tracetry_used static void -recur_tries(struct tries *tree, unsigned level) +recur_tries(TRIES * tree, unsigned level) { - if (level > len) - buffer = (unsigned char *) realloc(buffer, len = (level + 1) * 4); + if (level > my_length) { + my_length = (level + 1) * 4; + my_buffer = (unsigned char *) realloc(my_buffer, my_length); + } while (tree != 0) { - if ((buffer[level] = tree->ch) == 0) - buffer[level] = 128; - buffer[level + 1] = 0; + if ((my_buffer[level] = tree->ch) == 0) + my_buffer[level] = 128; + my_buffer[level + 1] = 0; if (tree->value != 0) { _tracef("%5d: %s (%s)", tree->value, - _nc_visbuf((char *) buffer), keyname(tree->value)); + _nc_visbuf((char *) my_buffer), keyname(tree->value)); } if (tree->child) recur_tries(tree->child, level + 1); @@ -64,18 +66,18 @@ recur_tries(struct tries *tree, unsigned level) } NCURSES_EXPORT(void) -_nc_trace_tries(struct tries *tree) +_nc_trace_tries(TRIES * tree) { - buffer = typeMalloc(unsigned char, len = 80); + my_buffer = typeMalloc(unsigned char, my_length = 80); _tracef("BEGIN tries %p", tree); recur_tries(tree, 0); _tracef(". . . tries %p", tree); - free(buffer); + free(my_buffer); } #else NCURSES_EXPORT(void) -_nc_trace_tries(struct tries *tree GCC_UNUSED) +_nc_trace_tries(TRIES * tree) { } #endif diff --git a/lib/libcurses/trace/trace_xnames.c b/lib/libcurses/trace/trace_xnames.c index 22dc761419f..2c5b0ff74dd 100644 --- a/lib/libcurses/trace/trace_xnames.c +++ b/lib/libcurses/trace/trace_xnames.c @@ -1,4 +1,4 @@ -/* $OpenBSD: trace_xnames.c,v 1.3 2001/01/22 18:01:59 millert Exp $ */ +/* $OpenBSD: trace_xnames.c,v 1.4 2010/01/12 23:22:07 nicm Exp $ */ /**************************************************************************** * Copyright (c) 1999,2000 Free Software Foundation, Inc. * @@ -38,7 +38,7 @@ #include <curses.priv.h> #include <term_entry.h> -MODULE_ID("$From: trace_xnames.c,v 1.5 2000/12/10 03:02:45 tom Exp $") +MODULE_ID("$Id: trace_xnames.c,v 1.4 2010/01/12 23:22:07 nicm Exp $") NCURSES_EXPORT(void) _nc_trace_xnames(TERMTYPE * tp GCC_UNUSED) diff --git a/lib/libcurses/trace/varargs.c b/lib/libcurses/trace/varargs.c new file mode 100644 index 00000000000..0a1d5cfa161 --- /dev/null +++ b/lib/libcurses/trace/varargs.c @@ -0,0 +1,186 @@ +/* $OpenBSD: varargs.c,v 1.1 2010/01/12 23:22:07 nicm Exp $ */ + +/**************************************************************************** + * Copyright (c) 2001-2007,2008 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 * + * "Software"), to deal in the Software without restriction, including * + * without limitation the rights to use, copy, modify, merge, publish, * + * distribute, distribute with modifications, sublicense, and/or sell * + * copies of the Software, and to permit persons to whom the Software is * + * furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included * + * in all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * + * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + * * + * Except as contained in this notice, the name(s) of the above copyright * + * holders shall not be used in advertising or otherwise to promote the * + * sale, use or other dealings in this Software without prior written * + * authorization. * + ****************************************************************************/ + +/**************************************************************************** + * Author: Thomas E. Dickey 2001 * + ****************************************************************************/ + +#include <curses.priv.h> + +#include <ctype.h> + +MODULE_ID("$Id: varargs.c,v 1.1 2010/01/12 23:22:07 nicm Exp $") + +#ifdef TRACE + +#define MAX_PARMS 10 + +typedef enum { + atUnknown = 0, atInteger, atFloat, atPoint, atString +} ARGTYPE; + +#define VA_INT(type) ival = va_arg(ap, type) +#define VA_FLT(type) fval = va_arg(ap, type) +#define VA_PTR(type) pval = (char *)va_arg(ap, type) +#define VA_STR(type) sval = va_arg(ap, type) + +#define MyBuffer _nc_globals.tracearg_buf +#define MyLength _nc_globals.tracearg_used + +/* + * Returns a string that represents the parameter list of a printf-style call. + */ +NCURSES_EXPORT(char *) +_nc_varargs(const char *fmt, va_list ap) +{ + static char dummy[] = ""; + + char buffer[BUFSIZ]; + const char *param; + int n; + + if (fmt == 0 || *fmt == '\0') + return dummy; + if (MyLength == 0) + MyBuffer = typeMalloc(char, MyLength = BUFSIZ); + if (MyBuffer == 0) + return dummy; + *MyBuffer = '\0'; + + while (*fmt != '\0') { + if (*fmt == '%') { + char *pval = 0; /* avoid const-cast */ + const char *sval = ""; + double fval = 0.0; + int done = FALSE; + int ival = 0; + int type = 0; + ARGTYPE parm[MAX_PARMS]; + int parms = 0; + ARGTYPE used = atUnknown; + + while (*++fmt != '\0' && !done) { + + if (*fmt == '*') { + VA_INT(int); + if (parms < MAX_PARMS) + parm[parms++] = atInteger; + } else if (isalpha(UChar(*fmt))) { + done = TRUE; + switch (*fmt) { + case 'Z': /* FALLTHRU */ + case 'h': /* FALLTHRU */ + case 'l': /* FALLTHRU */ + done = FALSE; + type = *fmt; + break; + case 'i': /* FALLTHRU */ + case 'd': /* FALLTHRU */ + case 'u': /* FALLTHRU */ + case 'x': /* FALLTHRU */ + case 'X': /* FALLTHRU */ + if (type == 'l') + VA_INT(long); + else if (type == 'Z') + VA_INT(size_t); + else + VA_INT(int); + used = atInteger; + break; + case 'f': /* FALLTHRU */ + case 'e': /* FALLTHRU */ + case 'E': /* FALLTHRU */ + case 'g': /* FALLTHRU */ + case 'G': /* FALLTHRU */ + VA_FLT(double); + used = atFloat; + break; + case 'c': + VA_INT(int); + used = atInteger; + break; + case 's': + VA_STR(const char *); + used = atString; + break; + case 'p': + VA_PTR(void *); + used = atPoint; + break; + case 'n': + VA_PTR(int *); + used = atPoint; + break; + default: + break; + } + } else if (*fmt == '%') { + done = TRUE; + } + if (used != atUnknown && parms < MAX_PARMS) { + parm[parms++] = used; + for (n = 0; n < parms; ++n) { + used = parm[n]; + param = buffer; + switch (used) { + case atInteger: + snprintf(buffer, sizeof(buffer), "%d", ival); + break; + case atFloat: + snprintf(buffer, sizeof(buffer), "%f", fval); + break; + case atPoint: + snprintf(buffer, sizeof(buffer), "%p", pval); + break; + case atString: + param = _nc_visbuf2(1, sval); + break; + case atUnknown: + default: + strlcpy(buffer, "?", sizeof(buffer)); + break; + } + MyLength += strlen(param) + 2; + MyBuffer = typeRealloc(char, MyLength, MyBuffer); + snprintf(MyBuffer + strlen(MyBuffer), MyLength - strlen(MyBuffer), ", %s", param); + } + } + used = atUnknown; + } + } else { + fmt++; + } + } + + return (MyBuffer); +} +#else +EMPTY_MODULE(_nc_varargs) +#endif diff --git a/lib/libcurses/trace/visbuf.c b/lib/libcurses/trace/visbuf.c new file mode 100644 index 00000000000..47efca5e22c --- /dev/null +++ b/lib/libcurses/trace/visbuf.c @@ -0,0 +1,333 @@ +/* $OpenBSD: visbuf.c,v 1.1 2010/01/12 23:22:07 nicm Exp $ */ + +/**************************************************************************** + * Copyright (c) 2001-2007,2008 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 * + * "Software"), to deal in the Software without restriction, including * + * without limitation the rights to use, copy, modify, merge, publish, * + * distribute, distribute with modifications, sublicense, and/or sell * + * copies of the Software, and to permit persons to whom the Software is * + * furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included * + * in all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * + * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + * * + * Except as contained in this notice, the name(s) of the above copyright * + * holders shall not be used in advertising or otherwise to promote the * + * sale, use or other dealings in this Software without prior written * + * authorization. * + ****************************************************************************/ + +/**************************************************************************** + * Author: Thomas E. Dickey 1996-on * + * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * + * and: Eric S. Raymond <esr@snark.thyrsus.com> * + ****************************************************************************/ + +/* + * visbuf.c - Tracing/Debugging support routines + */ + +#define NEED_NCURSES_CH_T +#include <curses.priv.h> + +#include <tic.h> +#include <ctype.h> + +MODULE_ID("$Id: visbuf.c,v 1.1 2010/01/12 23:22:07 nicm Exp $") + +#define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4) +#define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * MB_CUR_MAX) + +#ifdef TRACE +static const char d_quote[] = StringOf(D_QUOTE); +static const char l_brace[] = StringOf(L_BRACE); +static const char r_brace[] = StringOf(R_BRACE); +#endif + +static char * +_nc_vischar(char *tp, size_t tl, unsigned c) +{ + if (c == '"' || c == '\\') { + *tp++ = '\\'; + *tp++ = (char) c; + } else if (is7bits(c) && (isgraph(c) || c == ' ')) { + *tp++ = (char) c; + } else if (c == '\n') { + *tp++ = '\\'; + *tp++ = 'n'; + } else if (c == '\r') { + *tp++ = '\\'; + *tp++ = 'r'; + } else if (c == '\b') { + *tp++ = '\\'; + *tp++ = 'b'; + } else if (c == '\033') { + *tp++ = '\\'; + *tp++ = 'e'; + } else if (UChar(c) == 0x7f) { + *tp++ = '\\'; + *tp++ = '^'; + *tp++ = '?'; + } else if (is7bits(c) && iscntrl(UChar(c))) { + *tp++ = '\\'; + *tp++ = '^'; + *tp++ = (char) ('@' + c); + } else { + snprintf(tp, tl, "\\%03lo", (unsigned long) ChCharOf(c)); + tp += strlen(tp); + } + *tp = 0; + return tp; +} + +static const char * +_nc_visbuf2n(int bufnum, const char *buf, int len) +{ + const char *vbuf; + char *tp; + int c; + + if (buf == 0) + return ("(null)"); + if (buf == CANCELLED_STRING) + return ("(cancelled)"); + + if (len < 0) + len = (int) strlen(buf); + +#ifdef TRACE + vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len)); +#else + { + static char *mybuf[4]; + mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]); + vbuf = tp = mybuf[bufnum]; + } +#endif + if (tp != 0) { + *tp++ = D_QUOTE; + while ((--len >= 0) && (c = *buf++) != '\0') { + tp = _nc_vischar(tp, NormalLen(len) - (tp - vbuf), UChar(c)); + } + *tp++ = D_QUOTE; + *tp++ = '\0'; + } else { + vbuf = ("(_nc_visbuf2n failed)"); + } + return (vbuf); +} + +NCURSES_EXPORT(const char *) +_nc_visbuf2(int bufnum, const char *buf) +{ + return _nc_visbuf2n(bufnum, buf, -1); +} + +NCURSES_EXPORT(const char *) +_nc_visbuf(const char *buf) +{ + return _nc_visbuf2(0, buf); +} + +NCURSES_EXPORT(const char *) +_nc_visbufn(const char *buf, int len) +{ + return _nc_visbuf2n(0, buf, len); +} + +#ifdef TRACE +#if USE_WIDEC_SUPPORT + +#if defined(USE_TERMLIB) +#define _nc_wchstrlen _my_wchstrlen +static int +_nc_wchstrlen(const cchar_t *s) +{ + int result = 0; + while (CharOf(s[result]) != L'\0') { + result++; + } + return result; +} +#endif + +static const char * +_nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) +{ + const char *vbuf; + char *tp; + wchar_t c; + + if (buf == 0) + return ("(null)"); + + if (len < 0) + len = (int) wcslen(buf); + +#ifdef TRACE + vbuf = tp = _nc_trace_buf(bufnum, WideLen(len)); +#else + { + static char *mybuf[2]; + mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]); + vbuf = tp = mybuf[bufnum]; + } +#endif + if (tp != 0) { + *tp++ = D_QUOTE; + while ((--len >= 0) && (c = *buf++) != '\0') { + char temp[CCHARW_MAX + 80]; + int j = wctomb(temp, c), k; + if (j <= 0) { + snprintf(temp, sizeof temp, "\\u%08X", (unsigned) c); + j = (int) strlen(temp); + } + for (k = 0; k < j; ++k) { + tp = _nc_vischar(tp, WideLen(len) - (tp - vbuf), UChar(temp[k])); + } + } + *tp++ = D_QUOTE; + *tp++ = '\0'; + } else { + vbuf = ("(_nc_viswbuf2n failed)"); + } + return (vbuf); +} + +NCURSES_EXPORT(const char *) +_nc_viswbuf2(int bufnum, const wchar_t *buf) +{ + return _nc_viswbuf2n(bufnum, buf, -1); +} + +NCURSES_EXPORT(const char *) +_nc_viswbuf(const wchar_t *buf) +{ + return _nc_viswbuf2(0, buf); +} + +NCURSES_EXPORT(const char *) +_nc_viswbufn(const wchar_t *buf, int len) +{ + return _nc_viswbuf2n(0, buf, len); +} + +/* this special case is used for wget_wstr() */ +NCURSES_EXPORT(const char *) +_nc_viswibuf(const wint_t *buf) +{ + static wchar_t *mybuf; + static unsigned mylen; + unsigned n; + + for (n = 0; buf[n] != 0; ++n) ; + if (mylen < ++n) { + mylen = n + 80; + if (mybuf != 0) + mybuf = typeRealloc(wchar_t, mylen, mybuf); + else + mybuf = typeMalloc(wchar_t, mylen); + } + for (n = 0; buf[n] != 0; ++n) + mybuf[n] = (wchar_t) buf[n]; + + return _nc_viswbuf2(0, mybuf); +} +#endif /* USE_WIDEC_SUPPORT */ + +/* 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) +{ + char *result = _nc_trace_buf(bufnum, BUFSIZ); + int first; + const char *found; + + if (result != 0) { +#if USE_WIDEC_SUPPORT + if (len < 0) + len = _nc_wchstrlen(buf); +#endif /* USE_WIDEC_SUPPORT */ + + /* + * Display one or more strings followed by attributes. + */ + first = 0; + while (first < len) { + attr_t attr = AttrOf(buf[first]); + int last = len - 1; + int j; + + for (j = first + 1; j < len; ++j) { + if (!SameAttrOf(buf[j], buf[first])) { + last = j - 1; + break; + } + } + + result = _nc_trace_bufcat(bufnum, l_brace); + result = _nc_trace_bufcat(bufnum, d_quote); + for (j = first; j <= last; ++j) { + found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j])); + if (found != 0) { + result = _nc_trace_bufcat(bufnum, found); + attr &= ~A_ALTCHARSET; + } else +#if USE_WIDEC_SUPPORT + if (!isWidecExt(buf[j])) { + PUTC_DATA; + + PUTC_INIT; + for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) { + int k; + + PUTC_ch = buf[j].chars[PUTC_i]; + if (PUTC_ch == L'\0') + break; + PUTC_n = (int) wcrtomb(PUTC_buf, buf[j].chars[PUTC_i], &PUT_st); + if (PUTC_n <= 0) + break; + for (k = 0; k < PUTC_n; k++) { + char temp[80]; + _nc_vischar(temp, sizeof(temp), UChar(PUTC_buf[k])); + result = _nc_trace_bufcat(bufnum, temp); + } + } + } +#else + { + char temp[80]; + _nc_vischar(temp, sizeof(temp), UChar(buf[j])); + result = _nc_trace_bufcat(bufnum, temp); + } +#endif /* USE_WIDEC_SUPPORT */ + } + result = _nc_trace_bufcat(bufnum, d_quote); + if (attr != A_NORMAL) { + result = _nc_trace_bufcat(bufnum, " | "); + result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); + } + result = _nc_trace_bufcat(bufnum, r_brace); + first = last + 1; + } + } + return result; +} + +NCURSES_EXPORT(const char *) +_nc_viscbuf(const NCURSES_CH_T * buf, int len) +{ + return _nc_viscbuf2(0, buf, len); +} +#endif /* TRACE */ |