]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/curses.priv.h
ncurses 5.7 - patch 20090314
[ncurses.git] / ncurses / curses.priv.h
index 6056ad1126b0da9a0144f68074fede1909b846be..95f66529bdab3d7d7a7a333b0fa1490443896ccb 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2008,2009 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            *
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  *     and: Thomas E. Dickey                        1996-on                 *
+ *     and: Juergen Pfeifer                                                 *
  ****************************************************************************/
 
 
 /*
- * $Id: curses.priv.h,v 1.335 2007/07/16 20:32:12 tom Exp $
+ * $Id: curses.priv.h,v 1.403 2009/02/28 20:55:48 tom Exp $
  *
  *     curses.priv.h
  *
@@ -97,8 +98,6 @@ extern "C" {
 extern int errno;
 #endif
 
-#include <nc_panel.h>
-
 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
 #if HAVE_WORKING_POLL
 #define USE_FUNC_POLL 1
@@ -147,7 +146,7 @@ extern int errno;
 #define HAVE_SIZECHANGE 0
 #endif
 
-#if HAVE_SIZECHANGE && defined(SIGWINCH)
+#if HAVE_SIZECHANGE && USE_SIGWINCH && defined(SIGWINCH)
 #define USE_SIZECHANGE 1
 #else
 #define USE_SIZECHANGE 0
@@ -257,6 +256,38 @@ color_t;
 #define NCURSES_OPAQUE 0
 
 #include <curses.h>    /* we'll use -Ipath directive to get the right one! */
+
+/*
+ * If curses.h did not expose the SCREEN-functions, then we do not need the
+ * parameter in the corresponding unextended functions.
+ */
+#if NCURSES_SP_FUNCS
+#define SP_PARM         sp     /* use parameter */
+#define NCURSES_SP_ARG          SP_PARM
+#define NCURSES_SP_DCL  SCREEN *NCURSES_SP_ARG
+#define NCURSES_SP_DCL0 NCURSES_SP_DCL
+#define NCURSES_SP_ARGx         NCURSES_SP_ARG,
+#define NCURSES_SP_DCLx SCREEN *NCURSES_SP_ARGx
+#else
+#define SP_PARM         SP     /* use global variable */
+#define NCURSES_SP_ARG
+#define NCURSES_SP_DCL
+#define NCURSES_SP_DCL0 void
+#define NCURSES_SP_ARGx
+#define NCURSES_SP_DCLx
+#endif
+
+#include <nc_panel.h>
+
+#define IsPreScreen(sp)      (((sp)!=0) && sp->_prescreen)
+#define HasTerminal(sp)      (((sp)!=0) && (0!=((sp)->_term)))
+#define IsValidScreen(sp)    (HasTerminal(sp) && !sp->_prescreen)
+#if BROKEN_LINKER || USE_REENTRANT
+#define TerminalOf(sp)       ((sp)?((sp)->_term?(sp)->_term:_nc_prescreen._cur_term): _nc_prescreen._cur_term)
+#else
+#define TerminalOf(sp)       ((sp)?((sp)->_term?(sp)->_term:cur_term):cur_term)
+#endif
+
 #include <term.h>
 #include <term_entry.h>
 #include <nc_tparm.h>
@@ -286,6 +317,15 @@ color_t;
 #define VIDATTR(attr, pair)    vidattr(attr)
 #endif
 
+#if NCURSES_NO_PADDING
+#define GetNoPadding(sp)       ((sp) ? (sp)->_no_padding : _nc_prescreen._no_padding)
+#define SetNoPadding(sp)       _nc_set_no_padding(sp)
+extern NCURSES_EXPORT(void)     _nc_set_no_padding(SCREEN *);
+#else
+#define GetNoPadding(sp)       FALSE
+#define SetNoPadding(sp)       /*nothing*/
+#endif
+
 #define WINDOW_ATTRS(w)                ((w)->_attrs)
 
 #define SCREEN_ATTRS(s)                (*((s)->_current_attr))
@@ -293,13 +333,101 @@ color_t;
 #define SET_SCREEN_PAIR(s,p)   SetPair(SCREEN_ATTRS(s), p)
 
 #if USE_REENTRANT
-#define SET_LINES(value) SP->_LINES = value
-#define SET_COLS(value)  SP->_COLS = value
+NCURSES_EXPORT(int *) _nc_ptr_Lines (void);
+NCURSES_EXPORT(int *) _nc_ptr_Cols (void);
+#define ptrLines() (SP ? &(SP->_LINES) : &(_nc_prescreen._LINES))
+#define ptrCols()  (SP ? &(SP->_COLS)  : &(_nc_prescreen._COLS))
+#define SET_LINES(value) *_nc_ptr_Lines() = value
+#define SET_COLS(value)  *_nc_ptr_Cols() = value
 #else
+#define ptrLines() &LINES
+#define ptrCols()  &COLS
 #define SET_LINES(value) LINES = value
 #define SET_COLS(value)  COLS = value
 #endif
 
+#define TR_MUTEX(data) _tracef("%s@%d: me:%08lX COUNT:%2u/%2d/%6d/%2d/%s%9u: " #data, \
+           __FILE__, __LINE__, \
+           (unsigned long) (pthread_self()), \
+           data.__data.__lock, \
+           data.__data.__count, \
+           data.__data.__owner, \
+           data.__data.__kind, \
+           (data.__data.__nusers > 5) ? " OOPS " : "", \
+           data.__data.__nusers)
+#define TR_GLOBAL_MUTEX(name) TR_MUTEX(_nc_globals.mutex_##name)
+
+#ifdef USE_PTHREADS
+
+#if USE_REENTRANT
+#include <pthread.h>
+extern NCURSES_EXPORT(void) _nc_init_pthreads(void);
+extern NCURSES_EXPORT(void) _nc_mutex_init(pthread_mutex_t *);
+extern NCURSES_EXPORT(int) _nc_mutex_lock(pthread_mutex_t *);
+extern NCURSES_EXPORT(int) _nc_mutex_trylock(pthread_mutex_t *);
+extern NCURSES_EXPORT(int) _nc_mutex_unlock(pthread_mutex_t *);
+#define _nc_lock_global(name)  _nc_mutex_lock(&_nc_globals.mutex_##name)
+#define _nc_try_global(name)    _nc_mutex_trylock(&_nc_globals.mutex_##name)
+#define _nc_unlock_global(name)        _nc_mutex_unlock(&_nc_globals.mutex_##name)
+
+#else
+#error POSIX threads requires --enable-reentrant option
+#endif
+
+#if USE_WEAK_SYMBOLS
+#if defined(__GNUC__)
+#  if defined __USE_ISOC99
+#    define _cat_pragma(exp)   _Pragma(#exp)
+#    define _weak_pragma(exp)  _cat_pragma(weak name)
+#  else
+#    define _weak_pragma(exp)
+#  endif
+#  define _declare(name)       __extension__ extern __typeof__(name) name
+#  define weak_symbol(name)    _weak_pragma(name) _declare(name) __attribute__((weak))
+#endif
+#endif
+
+#ifdef USE_PTHREADS
+#  if USE_WEAK_SYMBOLS
+weak_symbol(pthread_sigmask);
+weak_symbol(pthread_self);
+weak_symbol(pthread_equal);
+weak_symbol(pthread_mutex_init);
+weak_symbol(pthread_mutex_lock);
+weak_symbol(pthread_mutex_unlock);
+weak_symbol(pthread_mutex_trylock);
+weak_symbol(pthread_mutexattr_settype);
+weak_symbol(pthread_mutexattr_init);
+extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
+#    undef  sigprocmask
+#    define sigprocmask _nc_sigprocmask
+#  endif
+#endif
+
+#if HAVE_NANOSLEEP
+#undef HAVE_NANOSLEEP
+#define HAVE_NANOSLEEP 0       /* nanosleep suspends all threads */
+#endif
+
+#else /* !USE_PTHREADS */
+
+#define _nc_init_pthreads()    /* nothing */
+#define _nc_mutex_init(obj)    /* nothing */
+
+#define _nc_lock_global(name)  /* nothing */
+#define _nc_try_global(name)    0
+#define _nc_unlock_global(name)        /* nothing */
+
+#endif /* USE_PTHREADS */
+
+#if HAVE_GETTIMEOFDAY
+# define PRECISE_GETTIME 1
+# define TimeType struct timeval
+#else
+# define PRECISE_GETTIME 0
+# define TimeType time_t
+#endif
+
 /*
  * Definitions for color pairs
  */
@@ -307,6 +435,8 @@ typedef unsigned colorpair_t;       /* type big enough to store PAIR_OF() */
 #define C_SHIFT 9              /* we need more bits than there are colors */
 #define C_MASK                 ((1 << C_SHIFT) - 1)
 #define PAIR_OF(fg, bg)                ((((fg) & C_MASK) << C_SHIFT) | ((bg) & C_MASK))
+#define FORE_OF(c)             (((c) >> C_SHIFT) & C_MASK)
+#define BACK_OF(c)             ((c) & C_MASK)
 #define isDefaultColor(c)      ((c) >= COLOR_DEFAULT || (c) < 0)
 
 #define COLOR_DEFAULT          C_MASK
@@ -367,19 +497,20 @@ typedef struct
 } slk_ent;
 
 typedef struct _SLK {
-       char dirty;             /* all labels have changed */
-       char hidden;            /* soft labels are hidden */
-       WINDOW *win;
+       bool    dirty;          /* all labels have changed */
+       bool    hidden;         /* soft labels are hidden */
+       WINDOW  *win;
        slk_ent *ent;
-       short  maxlab;          /* number of available labels */
-       short  labcnt;          /* number of allocated labels */
-       short  maxlen;          /* length of labels */
+       short   maxlab;         /* number of available labels */
+       short   labcnt;         /* number of allocated labels */
+       short   maxlen;         /* length of labels */
        NCURSES_CH_T attr;      /* soft label attribute */
 } SLK;
 
 #endif /* USE_TERMLIB */
 
 typedef        struct {
+       WINDOW *win;            /* the window used in the hook      */
        int     line;           /* lines to take, < 0 => from bottom*/
        int     (*hook)(WINDOW *, int); /* callback for user        */
 } ripoff_t;
@@ -458,6 +589,17 @@ typedef struct {
     size_t size;
 } TRACEBUF;
 
+/*
+ * The filesystem database normally uses a single-letter for the lower level
+ * of directories.  Use a hexadecimal code for filesystems which do not
+ * preserve mixed-case names.
+ */
+#if MIXEDCASE_FILENAMES
+#define LEAF_FMT "%c"
+#else
+#define LEAF_FMT "%02x"
+#endif
+
 /*
  * TRACEMSE_FMT is no longer than 80 columns, there are 5 numbers that
  * could at most have 10 digits, and the mask contains no more than 32 bits
@@ -491,6 +633,8 @@ typedef struct {
        char            *first_name;
        char            **keyname_table;
 
+       int             slk_format;
+
        char            *safeprint_buf;
        size_t          safeprint_used;
 
@@ -498,6 +642,9 @@ typedef struct {
        int             tgetent_index;
        long            tgetent_sequence;
 
+       WINDOWLIST      *_nc_windowlist;
+#define _nc_windows    _nc_globals._nc_windowlist
+
 #if USE_HOME_TERMINFO
        char            *home_terminfo;
 #endif
@@ -524,18 +671,23 @@ typedef struct {
        char            *tracedmp_buf;
        size_t          tracedmp_used;
 
-       char            tracemse_buf[TRACEMSE_MAX];
-
        unsigned char   *tracetry_buf;
        size_t          tracetry_used;
 
-#ifndef USE_TERMLIB
        char            traceatr_color_buf[2][80];
        int             traceatr_color_sel;
        int             traceatr_color_last;
-#endif /* USE_TERMLIB */
 
 #endif /* TRACE */
+
+#ifdef USE_PTHREADS
+       pthread_mutex_t mutex_curses;
+       pthread_mutex_t mutex_tst_tracef;
+       pthread_mutex_t mutex_tracef;
+       int             nested_tracef;
+       int             use_pthreads;
+#define _nc_use_pthreads       _nc_globals.use_pthreads
+#endif
 } NCURSES_GLOBALS;
 
 extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
@@ -543,7 +695,7 @@ extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
 #define N_RIPS 5
 
 /*
- * Global data which is swept up into a SCREEN when one is created.
+ * Global data which can be swept up into a SCREEN when one is created.
  * It may be modified before the next SCREEN is created.
  */
 typedef struct {
@@ -554,13 +706,24 @@ typedef struct {
        ripoff_t        *rsp;
        TPARM_STATE     tparm_state;
        TTY             *saved_tty;     /* savetty/resetty information      */
+#if NCURSES_NO_PADDING
+       bool            _no_padding;    /* flag to set if padding disabled  */
+#endif
 #if BROKEN_LINKER || USE_REENTRANT
        chtype          *real_acs_map;
        int             _LINES;
        int             _COLS;
+       TERMINAL        *_cur_term;
+#ifdef TRACE
+       long            _outchars;
+       const char      *_tputs_trace;
+#endif
 #endif
 } NCURSES_PRESCREEN;
 
+#define ripoff_sp      _nc_prescreen.rsp
+#define ripoff_stack   _nc_prescreen.rippedoff
+
 extern NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen;
 
 /*
@@ -573,6 +736,8 @@ struct screen {
        char            *_setbuf;       /* buffered I/O for output          */
        bool            _filtered;      /* filter() was called              */
        bool            _buffered;      /* setvbuf uses _setbuf data        */
+       bool            _prescreen;     /* is in prescreen phase            */
+       bool            _use_env;       /* LINES & COLS from environment?   */
        int             _checkfd;       /* filedesc for typeahead check     */
        TERMINAL        *_term;         /* terminal type information        */
        TTY             _saved_tty;     /* savetty/resetty information      */
@@ -616,7 +781,7 @@ struct screen {
        int             slk_format;     /* selected format for this screen  */
        /* cursor movement costs; units are 10ths of milliseconds */
 #if NCURSES_NO_PADDING
-       int             _no_padding;    /* flag to set if padding disabled  */
+       bool            _no_padding;    /* flag to set if padding disabled  */
 #endif
        int             _char_padding;  /* cost of character put            */
        int             _cr_cost;       /* cost of (carriage_return)        */
@@ -664,11 +829,13 @@ struct screen {
        int             _color_count;   /* count of colors in palette        */
        colorpair_t     *_color_pairs;  /* screen's color pair list          */
        int             _pair_count;    /* count of color pairs              */
+       int             _pair_limit;    /* actual limit of color-pairs       */
 #if NCURSES_EXT_FUNCS
        bool            _default_color; /* use default colors                */
        bool            _has_sgr_39_49; /* has ECMA default color support    */
        int             _default_fg;    /* assumed default foreground        */
        int             _default_bg;    /* assumed default background        */
+       int             _default_pairs; /* count pairs using default color   */
 #endif
        chtype          _ok_attributes; /* valid attributes for terminal     */
        chtype          _xmc_suppress;  /* attributes to suppress if xmc     */
@@ -701,7 +868,7 @@ struct screen {
        int             _maxclick;
        bool            (*_mouse_event) (SCREEN *);
        bool            (*_mouse_inline)(SCREEN *);
-       bool            (*_mouse_parse) (int);
+       bool            (*_mouse_parse) (SCREEN *, int);
        void            (*_mouse_resume)(SCREEN *);
        void            (*_mouse_wrap)  (SCREEN *);
        int             _mouse_fd;      /* file-descriptor, if any */
@@ -715,6 +882,7 @@ struct screen {
        bool            _mouse_gpm_loaded;
        bool            _mouse_gpm_found;
 #ifdef HAVE_LIBDL
+       void            *_dlopen_gpm;
        TYPE_gpm_fd     _mouse_gpm_fd;
        TYPE_Gpm_Open   _mouse_Gpm_Open;
        TYPE_Gpm_Close  _mouse_Gpm_Close;
@@ -752,12 +920,6 @@ struct screen {
         * per screen basis.
         */
        struct panelhook _panelHook;
-       /*
-        * Linked-list of all windows, to support '_nc_resizeall()' and
-        * '_nc_freeall()'
-        */
-       WINDOWLIST      *_nc_sp_windows;
-#define _nc_windows SP->_nc_sp_windows
 
        bool            _sig_winch;
        SCREEN          *_next_screen;
@@ -780,6 +942,15 @@ struct screen {
        int             _TABSIZE;
        int             _LINES;
        int             _COLS;
+#ifdef TRACE
+       long            _outchars;
+       const char      *_tputs_trace;
+#endif
+#endif
+
+#ifdef TRACE
+       char            tracechr_buf[40];
+       char            tracemse_buf[TRACEMSE_MAX];
 #endif
        /*
         * ncurses/ncursesw are the same up to this point.
@@ -796,8 +967,9 @@ extern NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain;
 extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 
        WINDOWLIST {
-       WINDOW  win;    /* first, so WINDOW_EXT() works */
+       WINDOW  win;            /* first, so WINDOW_EXT() works */
        WINDOWLIST *next;
+       SCREEN *screen;         /* screen containing the window */
 #ifdef _XOPEN_SOURCE_EXTENDED
        char addch_work[(MB_LEN_MAX * 9) + 1];
        unsigned addch_used;    /* number of bytes in addch_work[] */
@@ -870,11 +1042,17 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #endif
 
 #ifdef TRACE
-#define TRACE_OUTCHARS(n) _nc_outchars += (n);
+#if USE_REENTRANT
+#define COUNT_OUTCHARS(n) _nc_count_outchars(n);
+#else
+#define COUNT_OUTCHARS(n) _nc_outchars += (n);
+#endif
 #else
-#define TRACE_OUTCHARS(n) /* nothing */
+#define COUNT_OUTCHARS(n) /* nothing */
 #endif
 
+#define RESET_OUTCHARS() COUNT_OUTCHARS(-_nc_outchars)
+
 #define UChar(c)       ((unsigned char)(c))
 #define ChCharOf(c)    ((c) & (chtype)A_CHARTEXT)
 #define ChAttrOf(c)    ((c) & (chtype)A_ATTRIBUTES)
@@ -884,7 +1062,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #endif
 
 #if USE_WIDEC_SUPPORT /* { */
-#define isEILSEQ(status) ((status == (size_t)-1) && (errno == EILSEQ))
+#define isEILSEQ(status) (((size_t)status == (size_t)-1) && (errno == EILSEQ))
 
 #define init_mb(state) memset(&state, 0, sizeof(state))
 
@@ -897,12 +1075,26 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define NulChar                0,0,0,0 /* FIXME: see CCHARW_MAX */
 #define CharOf(c)      ((c).chars[0])
 #define AttrOf(c)      ((c).attr)
+
 #define AddAttr(c,a)   AttrOf(c) |=  ((a) & A_ATTRIBUTES)
 #define RemAttr(c,a)   AttrOf(c) &= ~((a) & A_ATTRIBUTES)
 #define SetAttr(c,a)   AttrOf(c) =   ((a) & A_ATTRIBUTES) | WidecExt(c)
+
 #define NewChar2(c,a)  { a, { c, NulChar } NulColor }
 #define NewChar(ch)    NewChar2(ChCharOf(ch), ChAttrOf(ch))
+
+#if CCHARW_MAX == 5
+#define CharEq(a,b)    (((a).attr == (b).attr) \
+                      && (a).chars[0] == (b).chars[0] \
+                      && (a).chars[1] == (b).chars[1] \
+                      && (a).chars[2] == (b).chars[2] \
+                      && (a).chars[3] == (b).chars[3] \
+                      && (a).chars[4] == (b).chars[4] \
+                       if_EXT_COLORS(&& (a).ext_color == (b).ext_color))
+#else
 #define CharEq(a,b)    (!memcmp(&(a), &(b), sizeof(a)))
+#endif
+
 #define SetChar(ch,c,a) do {                                                       \
                            NCURSES_CH_T *_cp = &ch;                                \
                            memset(_cp, 0, sizeof(ch));                             \
@@ -920,7 +1112,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define PUTC(ch,b)     do { if(!isWidecExt(ch)) {                                  \
                        if (Charable(ch)) {                                         \
                            fputc(CharOf(ch), b);                                   \
-                           TRACE_OUTCHARS(1);                                      \
+                           COUNT_OUTCHARS(1);                                      \
                        } else {                                                    \
                            PUTC_INIT;                                              \
                            for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {       \
@@ -936,11 +1128,11 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
                                }                                                   \
                                fwrite(PUTC_buf, (unsigned) PUTC_n, 1, b);          \
                            }                                                       \
-                           TRACE_OUTCHARS(PUTC_i);                                 \
+                           COUNT_OUTCHARS(PUTC_i);                                 \
                        } } } while (0)
 
-#define BLANK          { WA_NORMAL, {' '} NulColor }
-#define ZEROS          { WA_NORMAL, {'\0'} NulColor }
+#define BLANK          NewChar2(' ', WA_NORMAL)
+#define ZEROS          NewChar2('\0', WA_NORMAL)
 #define ISBLANK(ch)    ((ch).chars[0] == L' ' && (ch).chars[1] == L'\0')
 
        /*
@@ -1055,6 +1247,13 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 # endif
 #endif
 
+/*
+ * Standardize/simplify common loops
+ */
+#define each_screen(p) p = _nc_screen_chain; p != 0; p = (p)->_next_screen
+#define each_window(p) p = _nc_windows; p != 0; p = (p)->next
+#define each_ripoff(p) p = ripoff_stack; (p - ripoff_stack) < N_RIPS; ++p
+
 /*
  * Prefixes for call/return points of library function traces.  We use these to
  * instrument the public functions so that the traces can be easily transformed
@@ -1066,6 +1265,12 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 
 #ifdef TRACE
 
+#if USE_REENTRANT
+#define TPUTS_TRACE(s) _nc_set_tputs_trace(s);
+#else
+#define TPUTS_TRACE(s) _nc_tputs_trace = s;
+#endif
+
 #define START_TRACE() \
        if ((_nc_tracing & TRACE_MAXIMUM) == 0) { \
            int t = _nc_getenv_num("NCURSES_TRACE"); \
@@ -1073,9 +1278,21 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
                trace((unsigned) t); \
        }
 
-#define TR(n, a)       if (_nc_tracing & (n)) _tracef a
+/*
+ * Many of the _tracef() calls use static buffers; lock the trace state before
+ * trying to fill them.
+ */
+#if USE_REENTRANT
+#define USE_TRACEF(mask) _nc_use_tracef(mask)
+extern NCURSES_EXPORT(int)     _nc_use_tracef (unsigned);
+extern NCURSES_EXPORT(void)    _nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE(1,2);
+#else
+#define USE_TRACEF(mask) (_nc_tracing & (mask))
+#define _nc_locked_tracef _tracef
+#endif
+
+#define TR(n, a)       if (USE_TRACEF(n)) _nc_locked_tracef a
 #define T(a)           TR(TRACE_CALLS, a)
-#define TPUTS_TRACE(s) _nc_tputs_trace = s;
 #define TRACE_RETURN(value,type) return _nc_retrace_##type(value)
 
 #define returnAttr(code)       TRACE_RETURN(code,attr_t)
@@ -1083,7 +1300,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define returnBool(code)       TRACE_RETURN(code,bool)
 #define returnCPtr(code)       TRACE_RETURN(code,cptr)
 #define returnCVoidPtr(code)   TRACE_RETURN(code,cvoid_ptr)
-#define returnChar(code)       TRACE_RETURN(code,chtype)
+#define returnChtype(code)     TRACE_RETURN(code,chtype)
 #define returnCode(code)       TRACE_RETURN(code,int)
 #define returnPtr(code)                TRACE_RETURN(code,ptr)
 #define returnSP(code)         TRACE_RETURN(code,sp)
@@ -1105,9 +1322,20 @@ extern NCURSES_EXPORT(const char *)     _nc_retrace_cptr (const char *);
 extern NCURSES_EXPORT(int)              _nc_retrace_int (int);
 extern NCURSES_EXPORT(unsigned)         _nc_retrace_unsigned (unsigned);
 extern NCURSES_EXPORT(void *)           _nc_retrace_void_ptr (void *);
-extern NCURSES_EXPORT(void)             _nc_fifo_dump (void);
+extern NCURSES_EXPORT(void)             _nc_fifo_dump (SCREEN *);
+
+#if USE_REENTRANT
+NCURSES_WRAPPED_VAR(long, _nc_outchars);
+NCURSES_WRAPPED_VAR(const char *, _nc_tputs_trace);
+#define _nc_outchars       NCURSES_PUBLIC_VAR(_nc_outchars())
+#define _nc_tputs_trace    NCURSES_PUBLIC_VAR(_nc_tputs_trace())
+extern NCURSES_EXPORT(void)            _nc_set_tputs_trace (const char *);
+extern NCURSES_EXPORT(void)            _nc_count_outchars (long);
+#else
 extern NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace;
 extern NCURSES_EXPORT_VAR(long)         _nc_outchars;
+#endif
+
 extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
 
 #if USE_WIDEC_SUPPORT
@@ -1131,7 +1359,7 @@ extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
 #define returnBool(code)       return code
 #define returnCPtr(code)       return code
 #define returnCVoidPtr(code)   return code
-#define returnChar(code)       return code
+#define returnChtype(code)     return code
 #define returnCode(code)       return code
 #define returnPtr(code)                return code
 #define returnSP(code)         return code
@@ -1151,7 +1379,7 @@ extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
 extern NCURSES_EXPORT(const char *) _nc_visbuf2 (int, const char *);
 extern NCURSES_EXPORT(const char *) _nc_visbufn (const char *, int);
 
-#define empty_module(name) \
+#define EMPTY_MODULE(name) \
 extern NCURSES_EXPORT(void) name (void); \
        NCURSES_EXPORT(void) name (void) { }
 
@@ -1251,6 +1479,10 @@ extern NCURSES_EXPORT(void) _nc_expanded (void);
 
 #endif
 
+#if !NCURSES_EXT_FUNCS
+#define set_escdelay(value) ESCDELAY = value
+#endif
+
 #if !HAVE_GETCWD
 #define getcwd(buf,len) getwd(buf)
 #endif
@@ -1262,6 +1494,21 @@ extern NCURSES_EXPORT(int) _nc_to_char(wint_t);
 extern NCURSES_EXPORT(wint_t) _nc_to_widechar(int);
 #endif
 
+/* comp_captab.c */
+typedef struct {
+       short   nte_name;       /* offset of name to hash on */
+       int     nte_type;       /* BOOLEAN, NUMBER or STRING */
+       short   nte_index;      /* index of associated variable in its array */
+       short   nte_link;       /* index in table of next hash, or -1 */
+} name_table_data;
+
+typedef struct
+{
+       short   from;
+       short   to;
+       short   source;
+} alias_table_data;
+
 /* doupdate.c */
 #if USE_XMC_SUPPORT
 extern NCURSES_EXPORT(void) _nc_do_xmc_glitch (attr_t);
@@ -1309,7 +1556,7 @@ extern NCURSES_EXPORT(void) _nc_screen_resume (void);
 extern NCURSES_EXPORT(void) _nc_screen_wrap (void);
 
 /* lib_mouse.c */
-extern NCURSES_EXPORT(int) _nc_has_mouse (void);
+extern NCURSES_EXPORT(bool) _nc_has_mouse (SCREEN *);
 
 /* lib_mvcur.c */
 #define INFINITY       1000000 /* cost: too high to use */
@@ -1318,17 +1565,20 @@ extern NCURSES_EXPORT(int) _nc_has_mouse (void);
 /* lib_setup.c */
 extern NCURSES_EXPORT(char *) _nc_get_locale(void);
 extern NCURSES_EXPORT(int) _nc_unicode_locale(void);
-extern NCURSES_EXPORT(int) _nc_locale_breaks_acs(void);
+extern NCURSES_EXPORT(int) _nc_locale_breaks_acs(TERMINAL *);
 extern NCURSES_EXPORT(int) _nc_setupterm(NCURSES_CONST char *, int, int *, bool);
-extern NCURSES_EXPORT(void) _nc_get_screensize(int *, int *);
+extern NCURSES_EXPORT(void) _nc_get_screensize(SCREEN *, int *, int *);
 
 /* lib_tstp.c */
 #if USE_SIGWINCH
-extern NCURSES_EXPORT(int) _nc_handle_sigwinch(int);
+extern NCURSES_EXPORT(int) _nc_handle_sigwinch(SCREEN *);
 #else
 #define _nc_handle_sigwinch(a) /* nothing */
 #endif
 
+/* lib_ungetch.c */
+extern NCURSES_EXPORT(int) _nc_ungetch (SCREEN *, int);
+
 /* lib_wacs.c */
 #if USE_WIDEC_SUPPORT
 extern NCURSES_EXPORT(void) _nc_init_wacs(void);
@@ -1363,26 +1613,34 @@ extern NCURSES_EXPORT(int) _nc_remove_key (TRIES **, unsigned);
 extern NCURSES_EXPORT(int) _nc_remove_string (TRIES **, const char *);
 
 /* elsewhere ... */
-extern NCURSES_EXPORT(ENTRY *) _nc_delink_entry(ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(ENTRY *) _nc_delink_entry (ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *, int);
+extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_unctrl (SCREEN *, chtype);
+extern NCURSES_EXPORT(SCREEN *) _nc_screen_of (WINDOW *);
 extern NCURSES_EXPORT(WINDOW *) _nc_makenew (int, int, int, int, int);
 extern NCURSES_EXPORT(char *) _nc_trace_buf (int, size_t);
 extern NCURSES_EXPORT(char *) _nc_trace_bufcat (int, const char *);
+extern NCURSES_EXPORT(char *) _nc_tracechar (SCREEN *, int);
+extern NCURSES_EXPORT(char *) _nc_tracemouse (SCREEN *, MEVENT const *);
 extern NCURSES_EXPORT(int) _nc_access (const char *, int);
 extern NCURSES_EXPORT(int) _nc_baudrate (int);
 extern NCURSES_EXPORT(int) _nc_freewin (WINDOW *);
 extern NCURSES_EXPORT(int) _nc_getenv_num (const char *);
-extern NCURSES_EXPORT(int) _nc_keypad (bool);
+extern NCURSES_EXPORT(int) _nc_keypad (SCREEN *, bool);
 extern NCURSES_EXPORT(int) _nc_ospeed (int);
 extern NCURSES_EXPORT(int) _nc_outch (int);
+extern NCURSES_EXPORT(int) _nc_putp(const char *, const char *);
+extern NCURSES_EXPORT(int) _nc_putp_flush(const char *, const char *);
 extern NCURSES_EXPORT(int) _nc_read_termcap_entry (const char *const, TERMTYPE *const);
 extern NCURSES_EXPORT(int) _nc_setupscreen (int, int, FILE *, bool, int);
-extern NCURSES_EXPORT(int) _nc_timed_wait(int, int, int * EVENTLIST_2nd(_nc_eventlist *));
+extern NCURSES_EXPORT(int) _nc_timed_wait (SCREEN *, int, int, int * EVENTLIST_2nd(_nc_eventlist *));
 extern NCURSES_EXPORT(void) _nc_do_color (short, short, bool, int (*)(int));
 extern NCURSES_EXPORT(void) _nc_flush (void);
-extern NCURSES_EXPORT(void) _nc_free_entry(ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(void) _nc_free_and_exit (int);
+extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, TERMTYPE *);
 extern NCURSES_EXPORT(void) _nc_freeall (void);
 extern NCURSES_EXPORT(void) _nc_hash_map (void);
-extern NCURSES_EXPORT(void) _nc_init_keytry (void);
+extern NCURSES_EXPORT(void) _nc_init_keytry (SCREEN *);
 extern NCURSES_EXPORT(void) _nc_keep_tic_dir (const char *);
 extern NCURSES_EXPORT(void) _nc_make_oldhash (int i);
 extern NCURSES_EXPORT(void) _nc_scroll_oldhash (int n, int top, int bot);
@@ -1395,8 +1653,11 @@ extern NCURSES_EXPORT(void) _nc_trace_tries (TRIES *);
 #if NO_LEAKS
 extern NCURSES_EXPORT(void) _nc_alloc_entry_leaks(void);
 extern NCURSES_EXPORT(void) _nc_captoinfo_leaks(void);
+extern NCURSES_EXPORT(void) _nc_codes_leaks(void);
+extern NCURSES_EXPORT(void) _nc_comp_captab_leaks(void);
 extern NCURSES_EXPORT(void) _nc_comp_scan_leaks(void);
 extern NCURSES_EXPORT(void) _nc_keyname_leaks(void);
+extern NCURSES_EXPORT(void) _nc_names_leaks(void);
 extern NCURSES_EXPORT(void) _nc_tgetent_leaks(void);
 #endif
 
@@ -1407,15 +1668,11 @@ extern NCURSES_EXPORT(void) _nc_scroll_window (WINDOW *, int const, NCURSES_SIZE
 #endif
 
 #if USE_WIDEC_SUPPORT && !defined(USE_TERMLIB)
-#ifdef linux
 extern NCURSES_EXPORT(size_t) _nc_wcrtomb (char *, wchar_t, mbstate_t *);
-#else
-#define _nc_wcrtomb(s,wc,ps) wcrtomb(s,wc,ps)
-#endif
 #endif
 
 #if USE_SIZECHANGE
-extern NCURSES_EXPORT(void) _nc_update_screensize (void);
+extern NCURSES_EXPORT(void) _nc_update_screensize (SCREEN *);
 #endif
 
 #if HAVE_RESIZETERM
@@ -1445,7 +1702,7 @@ extern NCURSES_EXPORT_VAR(int *) _nc_oldnums;
 
 #define NC_BUFFERED(flag) _nc_set_buffer(SP->_ofp, flag)
 
-#define NC_OUTPUT ((SP != 0) ? SP->_ofp : stdout)
+#define NC_OUTPUT ((SP_PARM != 0) ? SP_PARM->_ofp : stdout)
 
 /*
  * On systems with a broken linker, define 'SP' as a function to force the
@@ -1463,6 +1720,8 @@ extern NCURSES_EXPORT_VAR(SCREEN *) SP;
 #define _nc_set_screen(sp) SP = sp
 #endif
 
+#define CURRENT_SCREEN SP
+
 /*
  * We don't want to use the lines or columns capabilities internally, because
  * if the application is running multiple screens under X, it's quite possible
@@ -1471,7 +1730,6 @@ extern NCURSES_EXPORT_VAR(SCREEN *) SP;
 #define screen_lines   SP->_lines
 #define screen_columns SP->_columns
 
-extern NCURSES_EXPORT_VAR(int) _nc_slk_format;  /* != 0 if slk_init() called */
 extern NCURSES_EXPORT(int) _nc_slk_initialize (WINDOW *, int);
 
 /*
@@ -1498,6 +1756,79 @@ extern NCURSES_EXPORT(int) _nc_ripoffline (int line, int (*init)(WINDOW *,int));
 #define MSG_NO_MEMORY "Out of memory"
 #define MSG_NO_INPUTS "Premature EOF"
 
+/* timed_wait flag definitions */
+#define TW_NONE    0
+#define TW_INPUT   1
+#define TW_MOUSE   2
+#define TW_ANY     (TW_INPUT | TW_MOUSE)
+#define TW_EVENT   4
+
+/*
+ * Exported entrypoints beyond the published API
+ */
+#if NCURSES_SP_FUNCS
+extern NCURSES_EXPORT(WINDOW *) _nc_curscr_of(SCREEN*);
+extern NCURSES_EXPORT(WINDOW *) _nc_newscr_of(SCREEN*);
+extern NCURSES_EXPORT(WINDOW *) _nc_stdscr_of(SCREEN*);
+extern NCURSES_EXPORT(int)      _nc_outc_wrapper(SCREEN*,int);
+
+extern NCURSES_EXPORT(TERMINAL*) NCURSES_SP_NAME(_nc_set_curterm)(SCREEN*,TERMINAL*);
+extern NCURSES_EXPORT(int)       NCURSES_SP_NAME(_nc_del_curterm)(SCREEN*,TERMINAL*);
+
+#if NCURSES_EXT_FUNCS
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tabsize)(SCREEN*, int);
+#endif
+
+extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
+
+/*
+ * We put the safe versions of various calls here as they are not published
+ * part of the API up to now
+ */
+extern NCURSES_EXPORT(SCREEN*)  _nc_SP(void);
+
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidputs)(SCREEN*,chtype,int(*) (SCREEN*, int));
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidattr)(SCREEN *, chtype);
+
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_restore)(SCREEN*, const char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_init)(SCREEN*,const char*);
+
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_restartterm)(SCREEN*,NCURSES_CONST char*,int,int*);
+extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(_nc_is_term_resized)(SCREEN*,int,int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resize_term)(SCREEN*,int,int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resizeterm)(SCREEN*,int,int);
+
+extern NCURSES_EXPORT(int)     NCURSES_SP_NAME(_nc_outch)(SCREEN*, int ch);
+extern NCURSES_EXPORT(void)    NCURSES_SP_NAME(_nc_flush)(SCREEN*);
+extern NCURSES_EXPORT(int)     NCURSES_SP_NAME(_nc_putp)(SCREEN*, const char *, const char *);
+extern NCURSES_EXPORT(int)     NCURSES_SP_NAME(_nc_putp_flush)(SCREEN*, const char *, const char *);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetent)(SCREEN*,char*,const char *);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tputs)(SCREEN*,const char*,int,int(*)(SCREEN*, int));
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_savetty)(SCREEN*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resetty)(SCREEN*);
+
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mcprint)(SCREEN*,char*, int);
+extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_tgetstr)(SCREEN*,NCURSES_CONST char*,char**);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetflag)(SCREEN*,NCURSES_CONST char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetnum)(SCREEN*,NCURSES_CONST char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetflag)(SCREEN*,NCURSES_CONST char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetnum)(SCREEN*,NCURSES_CONST char*);
+extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_tigetstr)(SCREEN*, NCURSES_CONST char*);
+extern NCURSES_EXPORT(void)     _nc_cookie_init(SCREEN *sp);
+extern NCURSES_EXPORT(int)      _nc_tinfo_doupdate(SCREEN *sp);
+extern NCURSES_EXPORT(int)      _nc_tinfo_mcprint(SCREEN*,char*,int);
+#if USE_WIDEC_SUPPORT
+extern NCURSES_EXPORT(wchar_t *) NCURSES_SP_NAME(_nc_wunctrl)(SCREEN*, cchar_t *);
+#endif
+
+/* FIXME - move these to curses.h.in */
+extern NCURSES_EXPORT(int)     NCURSES_SP_NAME(getmouse) (SCREEN*, MEVENT *);
+extern NCURSES_EXPORT(int)     NCURSES_SP_NAME(ungetmouse) (SCREEN*, MEVENT *);
+extern NCURSES_EXPORT(mmask_t) NCURSES_SP_NAME(mousemask) (SCREEN*, mmask_t, mmask_t *);
+extern NCURSES_EXPORT(int)     NCURSES_SP_NAME(mouseinterval) (SCREEN*, int);
+extern NCURSES_EXPORT(bool)    NCURSES_SP_NAME(has_mouse) (SCREEN*);
+#endif /* NCURSES_SP_FUNCS */
+
 #ifdef __cplusplus
 }
 #endif