]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/curses.priv.h
ncurses 6.2 - patch 20211016
[ncurses.git] / ncurses / curses.priv.h
index ec17d884c071c2d1db99ad6585856cbfcf94960c..872d25af95425afde4a2936ce07682557a9b3e42 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2014,2015 Free Software Foundation, Inc.              *
+ * Copyright 2018-2020,2021 Thomas E. Dickey                                *
+ * Copyright 1998-2017,2018 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            *
@@ -34,7 +35,7 @@
  ****************************************************************************/
 
 /*
- * $Id: curses.priv.h,v 1.547 2015/06/27 01:22:16 tom Exp $
+ * $Id: curses.priv.h,v 1.646 2021/09/24 16:09:35 tom Exp $
  *
  *     curses.priv.h
  *
@@ -71,10 +72,6 @@ extern "C" {
 #include <unistd.h>
 #endif
 
-#if HAVE_SYS_BSDTYPES_H
-#include <sys/bsdtypes.h>      /* needed for ISC */
-#endif
-
 #if HAVE_LIMITS_H
 # include <limits.h>
 #elif HAVE_SYS_PARAM_H
@@ -106,6 +103,24 @@ extern "C" {
 extern int errno;
 #endif
 
+/* Some Windows related defines */
+#undef _NC_WINDOWS
+#if (defined(_WIN32) || defined(_WIN64))
+#define _NC_WINDOWS
+#else
+#undef EXP_WIN32_DRIVER
+#endif
+
+#undef _NC_MINGW
+#if (defined(__MINGW32__) || defined(__MINGW64__))
+#define _NC_MINGW
+#endif
+
+#undef _NC_MSC
+#ifdef _MSC_VER
+#define _NC_MSC
+#endif
+
 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
 #if HAVE_WORKING_POLL
 #define USE_FUNC_POLL 1
@@ -184,24 +199,13 @@ extern int errno;
  * the path separator in configure doesn't work properly. So, if building
  * for MinGW, we enforce the correct Windows PATH separator
  */
-#ifdef __MINGW32__
+#if defined(_NC_WINDOWS)
 #  ifdef NCURSES_PATHSEP
 #    undef NCURSES_PATHSEP
 #  endif
 #  define NCURSES_PATHSEP ';'
 #endif
 
-/*
- * If desired, one can configure this, disabling environment variables that
- * point to custom terminfo/termcap locations.
- */
-#ifdef USE_ROOT_ENVIRON
-#define use_terminfo_vars() 1
-#else
-#define use_terminfo_vars() _nc_env_access()
-extern NCURSES_EXPORT(int) _nc_env_access (void);
-#endif
-
 /*
  * Not all platforms have memmove; some have an equivalent bcopy.  (Some may
  * have neither).
@@ -239,6 +243,18 @@ extern NCURSES_EXPORT(void *) _nc_memmove (void *, const void *, size_t);
 #define S_ISREG(mode) ((mode & S_IFMT) == S_IFREG)
 #endif
 
+/*
+ * POSIX ignores the "b", which c89 specified.  Some very old systems do not
+ * accept it.
+ */
+#if USE_FOPEN_BIN_R
+#define BIN_R  "rb"
+#define BIN_W  "wb"
+#else
+#define BIN_R  "r"
+#define BIN_W  "w"
+#endif
+
 /*
  * Scroll hints are useless when hashmap is used
  */
@@ -262,11 +278,20 @@ extern NCURSES_EXPORT(void *) _nc_memmove (void *, const void *, size_t);
  * Options for terminal drivers, etc...
  */
 #ifdef USE_TERM_DRIVER
+#define NO_TERMINAL "unknown"
 #define USE_SP_RIPOFF     1
 #define USE_SP_TERMTYPE   1
 #define USE_SP_WINDOWLIST 1
+#else
+#define NO_TERMINAL 0
 #endif
 
+#define VALID_TERM_ENV(term_env, no_terminal) \
+       (term_env = (NonEmpty(term_env) \
+                    ? term_env \
+                    : no_terminal), \
+        NonEmpty(term_env))
+
 /*
  * Note:  ht/cbt expansion flakes out randomly under Linux 1.1.47, but only
  * when we're throwing control codes at the screen at high volume.  To see
@@ -324,14 +349,27 @@ typedef TRIES {
 
 #include <curses.h>    /* we'll use -Ipath directive to get the right one! */
 
+#if !(defined(NCURSES_WGETCH_EVENTS) && defined(NEED_KEY_EVENT))
+#undef KEY_EVENT               /* reduce compiler-warnings with Visual C++ */
+#endif
+
 typedef struct
 {
-    NCURSES_COLOR_T red, green, blue;  /* what color_content() returns */
-    NCURSES_COLOR_T r, g, b;           /* params to init_color() */
+    int red, green, blue;      /* what color_content() returns */
+    int r, g, b;               /* params to init_color() */
     int init;                  /* true if we called init_color() */
 }
 color_t;
 
+typedef union {
+    struct {
+       unsigned char red;
+       unsigned char green;
+       unsigned char blue;
+    } bits;                    /* bits per color-value in RGB */
+    unsigned value;
+} rgb_bits_t;
+
 /*
  * If curses.h did not expose the SCREEN-functions, then we do not need the
  * parameter in the corresponding unextended functions.
@@ -358,6 +396,9 @@ color_t;
 
 #include <nc_panel.h>
 
+#include <term.priv.h>
+#include <nc_termios.h>
+
 #define IsPreScreen(sp)      (((sp) != 0) && sp->_prescreen)
 #define HasTerminal(sp)      (((sp) != 0) && (0 != ((sp)->_term)))
 #define IsValidScreen(sp)    (HasTerminal(sp) && !IsPreScreen(sp))
@@ -374,8 +415,31 @@ color_t;
 #define TerminalOf(sp)       CurTerm
 #endif
 
-#include <term.h>
-#include <nc_termios.h>
+/*
+ * The legacy layout for TERMTYPE uses "short" for all of the numbers.  Moving
+ * past that, numeric capabilities can be "int" by using a TERMTYPE2 structure
+ * in TERMINAL, and doing most of the internal work using TERMTYPE2.  There are
+ * a few places (mostly to expose the legacy layout) where the distinction
+ * needs attention.
+ */
+#if NCURSES_EXT_COLORS && HAVE_INIT_EXTENDED_COLOR
+#define NCURSES_EXT_NUMBERS  1
+#define NCURSES_INT2         int
+#define SIZEOF_INT2          4
+#define TerminalType(tp)     (tp)->type2
+#else
+#define NCURSES_EXT_NUMBERS  0
+#define NCURSES_INT2         short
+#define SIZEOF_INT2          2
+#define TerminalType(tp)     (tp)->type
+#endif
+
+#define SIZEOF_SHORT         2
+
+#ifdef CUR
+#undef CUR
+#define CUR TerminalType(cur_term).
+#endif
 
 /*
  * Reduce dependency on cur_term global by using terminfo data from SCREEN's
@@ -385,7 +449,7 @@ color_t;
 #undef CUR
 #endif
 
-#define SP_TERMTYPE TerminalOf(sp)->type.
+#define SP_TERMTYPE TerminalType(TerminalOf(sp)).
 
 #include <term_entry.h>
 
@@ -394,10 +458,11 @@ color_t;
 /*
  * Simplify ifdef's for the "*_ATTR" macros in case italics are not configured.
  */
-#ifdef A_ITALIC
+#if defined(A_ITALIC) && defined(exit_italics_mode)
 #define USE_ITALIC 1
 #else
 #define USE_ITALIC 0
+#undef  A_ITALIC
 #define A_ITALIC 0
 #endif
 
@@ -406,7 +471,7 @@ color_t;
  * option for compiling the tracing into the library.
  */
 #if 1
-#define ColorPair(n)           NCURSES_BITS(n, 0)
+#define ColorPair(n)           (NCURSES_BITS(n, 0) & A_COLOR)
 #define PairNumber(a)          (NCURSES_CAST(int,(((unsigned long)(a) & A_COLOR) >> NCURSES_ATTR_SHIFT)))
 #else
 #define ColorPair(pair)                COLOR_PAIR(pair)
@@ -426,7 +491,7 @@ color_t;
 #define if_EXT_COLORS(stmt)    stmt
 #define SetPair(value,p)       SetPair2((value).ext_color, AttrOf(value), p)
 #define SetPair2(c,a,p)                c = (p), \
-                               a = (unColor2(a) | (A_COLOR & (unsigned) ColorPair(oldColor(c))))
+                               a = (unColor2(a) | ColorPair(oldColor(c)))
 #define GetPair(value)         GetPair2((value).ext_color, AttrOf(value))
 #define GetPair2(c,a)          ((c) ? (c) : PairNumber(a))
 #define oldColor(p)            (((p) > 255) ? 255 : (p))
@@ -434,20 +499,27 @@ color_t;
 #define SET_WINDOW_PAIR(w,p)   (w)->_color = (p)
 #define SameAttrOf(a,b)                (AttrOf(a) == AttrOf(b) && GetPair(a) == GetPair(b))
 
-#define VIDATTR(sp,attr,pair)  NCURSES_SP_NAME(vid_puts)(NCURSES_SP_ARGx attr, (short) pair, 0, NCURSES_OUTC_FUNC)
+#define VIDPUTS(sp,attr,pair)  do { \
+                                   int vid_pair = pair; \
+                                   NCURSES_SP_NAME(vid_puts)( \
+                                       NCURSES_SP_ARGx attr, \
+                                       (NCURSES_PAIRS_T) pair, \
+                                       &vid_pair, \
+                                       NCURSES_OUTC_FUNC); \
+                               } while (0)
 
 #else /* !NCURSES_EXT_COLORS */
 
 #define if_EXT_COLORS(stmt)    /* nothing */
 #define SetPair(value,p)       RemAttr(value, A_COLOR), \
-                               SetAttr(value, AttrOf(value) | (A_COLOR & (attr_t) ColorPair(p)))
+                               SetAttr(value, AttrOf(value) | ColorPair(p))
 #define GetPair(value)         PairNumber(AttrOf(value))
 #define GET_WINDOW_PAIR(w)     PairNumber(WINDOW_ATTRS(w))
 #define SET_WINDOW_PAIR(w,p)   WINDOW_ATTRS(w) &= ALL_BUT_COLOR, \
-                               WINDOW_ATTRS(w) |= (A_COLOR & (attr_t) ColorPair(p))
+                               WINDOW_ATTRS(w) |= ColorPair(p)
 #define SameAttrOf(a,b)                (AttrOf(a) == AttrOf(b))
 
-#define VIDATTR(sp,attr,pair)  NCURSES_SP_NAME(vidputs)(NCURSES_SP_ARGx attr, NCURSES_OUTC_FUNC)
+#define VIDPUTS(sp,attr,pair)  NCURSES_SP_NAME(vidputs)(NCURSES_SP_ARGx attr, NCURSES_OUTC_FUNC)
 
 #endif /* NCURSES_EXT_COLORS */
 
@@ -503,6 +575,8 @@ NCURSES_EXPORT(int *)        _nc_ptr_Escdelay (SCREEN *);
 
 #endif
 
+#define HasHardTabs()  (NonEmpty(clear_all_tabs) && NonEmpty(set_tab))
+
 #define TR_MUTEX(data) _tracef("%s@%d: me:%08lX COUNT:%2u/%2d/%6d/%2d/%s%9u: " #data, \
            __FILE__, __LINE__, \
            (unsigned long) (pthread_self()), \
@@ -561,7 +635,10 @@ 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
+#    define sigprocmask(a, b, c) _nc_sigprocmask(a, b, c)
+#    define GetThreadID() (((pthread_self)) ? pthread_self() : (pthread_t) getpid())
+#  else
+#    define GetThreadID() pthread_self()
 #  endif
 #endif
 
@@ -581,7 +658,7 @@ weak_symbol(pthread_self);
 weak_symbol(pthread_equal);
 extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
 #    undef  sigprocmask
-#    define sigprocmask _nc_sigprocmask
+#    define sigprocmask(a, b, c) _nc_sigprocmask(a, b, c)
 #  endif
 #endif /* USE_PTHREADS_EINTR */
 
@@ -635,15 +712,13 @@ extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
 /*
  * Definitions for color pairs
  */
-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)                (colorpair_t) ((((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
+#define MAX_OF_TYPE(t)   (int)(((unsigned t)(~0))>>1)
+
+#include <new_pair.h>
+
+#define isDefaultColor(c)      ((c) < 0)
+#define COLOR_DEFAULT          -1
 
 #if defined(USE_BUILD_CC) || (defined(USE_TERMLIB) && !defined(NEED_NCURSES_CH_T))
 
@@ -724,12 +799,6 @@ typedef struct _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;
-
 #if USE_GPM_SUPPORT
 #undef buttons                 /* term.h defines this, and gpm uses it! */
 #include <gpm.h>
@@ -757,55 +826,15 @@ typedef int (*TYPE_Gpm_GetEvent) (Gpm_Event *);
 #endif /* HAVE_LIBDL */
 #endif /* USE_GPM_SUPPORT */
 
-typedef struct {
-    long sequence;
-    bool last_used;
-    char *fix_sgr0;            /* this holds the filtered sgr0 string */
-    char *last_bufp;           /* help with fix_sgr0 leak */
-    TERMINAL *last_term;
-} TGETENT_CACHE;
-
-#define TGETENT_MAX 4
-
 /*
- * State of tparm().
+ * When converting from terminfo to termcap, check for cases where we can trim
+ * octal escapes down to 2-character form.  It is useful for terminfo format
+ * also, but not as important.
  */
-#define STACKSIZE 20
-
-typedef struct {
-       union {
-               int     num;
-               char    *str;
-       } data;
-       bool num_type;
-} STACK_FRAME;
+#define MAX_TC_FIXUPS  10
+#define MIN_TC_FIXUPS  4
 
-#define NUM_VARS 26
-
-typedef struct {
-#ifdef TRACE
-       const char      *tname;
-#endif
-       const char      *tparam_base;
-
-       STACK_FRAME     stack[STACKSIZE];
-       int             stack_ptr;
-
-       char            *out_buff;
-       size_t          out_size;
-       size_t          out_used;
-
-       char            *fmt_buff;
-       size_t          fmt_size;
-
-       int             dynamic_var[NUM_VARS];
-       int             static_vars[NUM_VARS];
-} TPARM_STATE;
-
-typedef struct {
-    char *text;
-    size_t size;
-} TRACEBUF;
+#define isoctal(c) ((c) >= '0' && (c) <= '7')
 
 /*
  * The filesystem database normally uses a single-letter for the lower level
@@ -837,146 +866,16 @@ struct DriverTCB; /* Terminal Control Block forward declaration */
 #define INIT_TERM_DRIVER()     /* nothing */
 #endif
 
-typedef struct {
-    const char *name;
-    char *value;
-} ITERATOR_VARS;
-
-/*
- * Global data which is not specific to a screen.
- */
-typedef struct {
-       SIG_ATOMIC_T    have_sigtstp;
-       SIG_ATOMIC_T    have_sigwinch;
-       SIG_ATOMIC_T    cleanup_nested;
-
-       bool            init_signals;
-       bool            init_screen;
-
-       char            *comp_sourcename;
-       char            *comp_termtype;
-
-       bool            have_tic_directory;
-       bool            keep_tic_directory;
-       const char      *tic_directory;
-
-       char            *dbi_list;
-       int             dbi_size;
-
-       char            *first_name;
-       char            **keyname_table;
-       int             init_keyname;
-
-       int             slk_format;
-
-       char            *safeprint_buf;
-       size_t          safeprint_used;
-
-       TGETENT_CACHE   tgetent_cache[TGETENT_MAX];
-       int             tgetent_index;
-       long            tgetent_sequence;
-
-       char            *dbd_blob;      /* string-heap for dbd_list[] */
-       char            **dbd_list;     /* distinct places to look for data */
-       int             dbd_size;       /* length of dbd_list[] */
-       time_t          dbd_time;       /* cache last updated */
-       ITERATOR_VARS   dbd_vars[dbdLAST];
-
-#ifndef USE_SP_WINDOWLIST
-       WINDOWLIST      *_nc_windowlist;
-#define WindowList(sp) _nc_globals._nc_windowlist
-#endif
-
-#if USE_HOME_TERMINFO
-       char            *home_terminfo;
-#endif
-
-#if !USE_SAFE_SPRINTF
-       int             safeprint_cols;
-       int             safeprint_rows;
-#endif
-
-#ifdef USE_TERM_DRIVER
-       int             (*term_driver)(struct DriverTCB*, const char*, int*);
-#endif
-
-#ifdef TRACE
-       bool            init_trace;
-       char            trace_fname[PATH_MAX];
-       int             trace_level;
-       FILE            *trace_fp;
-
-       char            *tracearg_buf;
-       size_t          tracearg_used;
-
-       TRACEBUF        *tracebuf_ptr;
-       size_t          tracebuf_used;
-
-       char            tracechr_buf[40];
-
-       char            *tracedmp_buf;
-       size_t          tracedmp_used;
-
-       unsigned char   *tracetry_buf;
-       size_t          tracetry_used;
-
-       char            traceatr_color_buf[2][80];
-       int             traceatr_color_sel;
-       int             traceatr_color_last;
-#if !defined(USE_PTHREADS) && USE_REENTRANT
-       int             nested_tracef;
-#endif
-#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
-#if USE_PTHREADS_EINTR
-       pthread_t       read_thread;            /* The reading thread */
-#endif
-} NCURSES_GLOBALS;
-
 extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
 
-#define N_RIPS 5
-
-/*
- * 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 {
-       bool            use_env;
-       bool            filter_mode;
-       attr_t          previous_attr;
-#ifndef USE_SP_RIPOFF
-       ripoff_t        rippedoff[N_RIPS];
-       ripoff_t        *rsp;
-#endif
-       TPARM_STATE     tparm_state;
-       TTY             *saved_tty;     /* savetty/resetty information      */
-#if NCURSES_NO_PADDING
-       bool            _no_padding;    /* flag to set if padding disabled  */
-#endif
-       NCURSES_SP_OUTC _outch;         /* output handler if not putc */
-#if BROKEN_LINKER || USE_REENTRANT
-       chtype          *real_acs_map;
-       int             _LINES;
-       int             _COLS;
-       int             _TABSIZE;
-       int             _ESCDELAY;
-       TERMINAL        *_cur_term;
-#ifdef TRACE
-       long            _outchars;
-       const char      *_tputs_trace;
-#endif
-#endif
-       bool            use_tioctl;
-} NCURSES_PRESCREEN;
+/* The limit reserves one byte for a terminating NUL */
+#define my_getstr_limit        (_nc_globals.getstr_limit - 1)
+#define _nc_getstr_limit(n) \
+       (((n) < 0) \
+        ? my_getstr_limit \
+        : (((n) > my_getstr_limit) \
+           ? my_getstr_limit \
+           : (n)))
 
 /*
  * Use screen-specific ripoff data (for softkeys) rather than global.
@@ -991,11 +890,16 @@ typedef struct {
 
 extern NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen;
 
+typedef enum {
+    ewInitial = 0,
+    ewRunning,
+    ewSuspend
+} ENDWIN;
+
 /*
  * The SCREEN structure.
  */
-
-struct screen {
+typedef struct screen {
        int             _ifd;           /* input file descriptor for screen */
        int             _ofd;           /* output file descriptor for screen */
        FILE            *_ofp;          /* output file ptr for screen       */
@@ -1051,18 +955,11 @@ struct screen {
        struct _SLK     *_slk;          /* ptr to soft key struct / NULL    */
        int             slk_format;     /* selected format for this screen  */
        /* cursor movement costs; units are 10ths of milliseconds */
-#if NCURSES_NO_PADDING
-       bool            _no_padding;    /* flag to set if padding disabled  */
-#endif
        int             _char_padding;  /* cost of character put            */
        int             _cr_cost;       /* cost of (carriage_return)        */
        int             _cup_cost;      /* cost of (cursor_address)         */
        int             _home_cost;     /* cost of (cursor_home)            */
        int             _ll_cost;       /* cost of (cursor_to_ll)           */
-#if USE_HARD_TABS
-       int             _ht_cost;       /* cost of (tab)                    */
-       int             _cbt_cost;      /* cost of (backtab)                */
-#endif /* USE_HARD_TABS */
        int             _cub1_cost;     /* cost of (cursor_left)            */
        int             _cuf1_cost;     /* cost of (cursor_right)           */
        int             _cud1_cost;     /* cost of (cursor_down)            */
@@ -1096,19 +993,13 @@ struct screen {
        int             _scrolling;     /* 1 if terminal's smart enough to  */
 
        /* used in lib_color.c */
+       rgb_bits_t      _direct_color;  /* RGB overrides color-table         */
        color_t         *_color_table;  /* screen's color palette            */
        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_count;    /* same as COLOR_PAIRS               */
        int             _pair_limit;    /* actual limit of color-pairs       */
-#if NCURSES_EXT_FUNCS
-       bool            _assumed_color; /* use assumed colors                */
-       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
+       int             _pair_alloc;    /* current table-size of color-pairs */
        chtype          _ok_attributes; /* valid attributes for terminal     */
        chtype          _xmc_suppress;  /* attributes to suppress if xmc     */
        chtype          _xmc_triggers;  /* attributes to process if xmc      */
@@ -1119,13 +1010,6 @@ struct screen {
        /* used in lib_vidattr.c */
        bool            _use_rmso;      /* true if we may use 'rmso'         */
        bool            _use_rmul;      /* true if we may use 'rmul'         */
-#if USE_ITALIC
-       bool            _use_ritm;      /* true if we may use 'ritm'         */
-#endif
-
-#if USE_KLIBC_KBD
-       bool            _extended_key;  /* true if an extended key           */
-#endif
 
        /*
         * These data correspond to the state of the idcok() and idlok()
@@ -1158,6 +1042,60 @@ struct screen {
        MEVENT          _mouse_events[EV_MAX];  /* hold the last mouse event seen */
        MEVENT          *_mouse_eventp; /* next free slot in event queue */
 
+       /*
+        * These are data that support the proper handling of the panel stack on an
+        * per screen basis.
+        */
+       struct panelhook _panelHook;
+
+       bool            _sig_winch;
+       SCREEN          *_next_screen;
+
+       /* hashes for old and new lines */
+       unsigned long   *oldhash, *newhash;
+       HASHMAP         *hashtab;
+       int             hashtab_len;
+       int             *_oldnum_list;
+       int             _oldnum_size;
+
+       NCURSES_SP_OUTC _outch;         /* output handler if not putc */
+       NCURSES_OUTC    jump;
+
+       ripoff_t        rippedoff[N_RIPS];
+       ripoff_t        *rsp;
+
+       int             _legacy_coding; /* see use_legacy_coding() */
+
+#if NCURSES_NO_PADDING
+       bool            _no_padding;    /* flag to set if padding disabled  */
+#endif
+
+#if USE_HARD_TABS
+       int             _ht_cost;       /* cost of (tab)                    */
+       int             _cbt_cost;      /* cost of (backtab)                */
+#endif /* USE_HARD_TABS */
+
+       /* used in lib_vidattr.c */
+#if USE_ITALIC
+       bool            _use_ritm;      /* true if we may use 'ritm'         */
+#endif
+
+       /* used in getch/twait */
+#if USE_KLIBC_KBD
+       bool            _extended_key;  /* true if an extended key           */
+#endif
+
+       /* used in lib_color.c */
+#if NCURSES_EXT_FUNCS
+       bool            _assumed_color; /* use assumed colors                */
+       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
+
+       /* system-dependent mouse data */
 #if USE_GPM_SUPPORT
        bool            _mouse_gpm_loaded;
        bool            _mouse_gpm_found;
@@ -1188,7 +1126,7 @@ struct screen {
        int             _sysmouse_new_buttons;
 #endif
 
-#ifdef USE_TERM_DRIVER
+#if defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER)
        MEVENT          _drv_mouse_fifo[FIFO_SIZE];
        int             _drv_mouse_head;
        int             _drv_mouse_tail;
@@ -1203,25 +1141,10 @@ struct screen {
        int             (*_ungetch)(SCREEN *, int);
 #endif
 
-       /*
-        * These are data that support the proper handling of the panel stack on an
-        * per screen basis.
-        */
-       struct panelhook _panelHook;
-
-       bool            _sig_winch;
-       SCREEN          *_next_screen;
-
-       /* hashes for old and new lines */
-       unsigned long   *oldhash, *newhash;
-       HASHMAP         *hashtab;
-       int             hashtab_len;
-       int             *_oldnum_list;
-       int             _oldnum_size;
-
-       NCURSES_SP_OUTC _outch;         /* output handler if not putc */
-
-       int             _legacy_coding; /* see use_legacy_coding() */
+#ifdef USE_SP_WINDOWLIST
+       WINDOWLIST*     _windowlist;
+#define WindowList(sp)  (sp)->_windowlist
+#endif
 
 #if USE_REENTRANT
        char            _ttytype[NAMESIZE];
@@ -1229,24 +1152,11 @@ 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
-#ifdef USE_SP_WINDOWLIST
-       WINDOWLIST*     _windowlist;
-#define WindowList(sp)  (sp)->_windowlist
+#if NCURSES_SP_FUNCS
+       bool            use_tioctl;
 #endif
-       NCURSES_OUTC    jump;
-
-       ripoff_t        rippedoff[N_RIPS];
-       ripoff_t        *rsp;
 
        /*
         * ncurses/ncursesw are the same up to this point.
@@ -1259,8 +1169,22 @@ struct screen {
        bool            _screen_unicode;
 #endif
 
-       bool            _use_tioctl;
-};
+#if NCURSES_EXT_FUNCS && NCURSES_EXT_COLORS
+       void            *_ordered_pairs; /* index used by alloc_pair()       */
+       int             _pairs_used;    /* actual number of color-pairs used */
+       int             _recent_pair;   /* number for most recent free-pair  */
+#endif
+
+#ifdef TRACE
+       char            tracechr_buf[40];
+       char            tracemse_buf[TRACEMSE_MAX];
+#if USE_REENTRANT
+       long            _outchars;
+       const char      *_tputs_trace;
+#endif
+#endif
+#undef SCREEN
+} SCREEN;
 
 extern NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain;
 extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
@@ -1279,6 +1203,12 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 
 #define WINDOW_EXT(w,m) (((WINDOWLIST *)((void *)((char *)(w) - offsetof(WINDOWLIST, win))))->m)
 
+#ifdef USE_SP_WINDOWLIST
+#define SP_INIT_WINDOWLIST(sp) WindowList(sp) = 0
+#else
+#define SP_INIT_WINDOWLIST(sp) /* nothing */
+#endif
+
 #define SP_PRE_INIT(sp)                         \
     sp->_cursrow = -1;                          \
     sp->_curscol = -1;                          \
@@ -1287,9 +1217,9 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
     sp->_cbreak = 0;                            \
     sp->_echo = TRUE;                           \
     sp->_fifohead = -1;                         \
-    sp->_endwin = TRUE;                         \
+    sp->_endwin = ewSuspend;                    \
     sp->_cursor = -1;                           \
-    WindowList(sp) = 0;                         \
+    SP_INIT_WINDOWLIST(sp);                     \
     sp->_outch = NCURSES_OUTC_FUNC;             \
     sp->jump = 0                                \
 
@@ -1371,6 +1301,8 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define ChCharOf(c)    ((chtype)(c) & (chtype)A_CHARTEXT)
 #define ChAttrOf(c)    ((chtype)(c) & (chtype)A_ATTRIBUTES)
 
+#define TR_PUTC(c)     TR(TRACE_CHARPUT, ("PUTC %#x", UChar(c)))
+
 #ifndef MB_LEN_MAX
 #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
 #endif
@@ -1378,7 +1310,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #if USE_WIDEC_SUPPORT /* { */
 #define isEILSEQ(status) (((size_t)status == (size_t)-1) && (errno == EILSEQ))
 
-#define init_mb(state) memset(&state, 0, sizeof(state))
+#define init_mb(state) memset(&(state), 0, sizeof(state))
 
 #if NCURSES_EXT_COLORS
 #define NulColor       , 0
@@ -1405,19 +1337,21 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
                       && (a).chars[3] == (b).chars[3] \
                       && (a).chars[4] == (b).chars[4] \
                        if_EXT_COLORS(&& (a).ext_color == (b).ext_color))
+#elif CCHARW_MAX > 0
+#error Inconsistent values for CCHARW_MAX
 #else
 #define CharEq(a,b)    (!memcmp(&(a), &(b), sizeof(a)))
 #endif
 
 #define SetChar(ch,c,a) do {                                                       \
-                           NCURSES_CH_T *_cp = &ch;                                \
+                           NCURSES_CH_T *_cp = &(ch);                              \
                            memset(_cp, 0, sizeof(ch));                             \
                            _cp->chars[0] = (wchar_t) (c);                          \
                            _cp->attr = (a);                                        \
                            if_EXT_COLORS(SetPair(ch, PairNumber(a)));              \
                        } while (0)
-#define CHREF(wch)     (&wch)
-#define CHDEREF(wch)   (*wch)
+#define CHREF(wch)     (&(wch))
+#define CHDEREF(wch)   (*(wch))
 #define ARG_CH_T       NCURSES_CH_T *
 #define CARG_CH_T      const NCURSES_CH_T *
 #define PUTC_DATA      char PUTC_buf[MB_LEN_MAX]; int PUTC_i, PUTC_n; \
@@ -1425,23 +1359,27 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define PUTC_INIT      init_mb (PUT_st)
 #define PUTC(ch)       do { if(!isWidecExt(ch)) {                                  \
                        if (Charable(ch)) {                                         \
-                           NCURSES_OUTC_FUNC (NCURSES_SP_ARGx CharOf(ch)); \
+                           TR_PUTC(CharOf(ch));                                    \
+                           NCURSES_OUTC_FUNC (NCURSES_SP_ARGx CharOf(ch));         \
                            COUNT_OUTCHARS(1);                                      \
                        } else {                                                    \
-                           PUTC_INIT;                                              \
                            for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {       \
                                PUTC_ch = (ch).chars[PUTC_i];                       \
                                if (PUTC_ch == L'\0')                               \
                                    break;                                          \
+                               PUTC_INIT;                                          \
                                PUTC_n = (int) wcrtomb(PUTC_buf,                    \
                                                       (ch).chars[PUTC_i], &PUT_st); \
                                if (PUTC_n <= 0) {                                  \
-                                   if (PUTC_ch && is8bits(PUTC_ch) && PUTC_i == 0) \
+                                   if (PUTC_ch && is8bits(PUTC_ch) && PUTC_i == 0) { \
+                                       TR_PUTC(CharOf(ch));                        \
                                        NCURSES_OUTC_FUNC (NCURSES_SP_ARGx CharOf(ch)); \
+                                   }                                               \
                                    break;                                          \
                                } else {                                            \
                                    int PUTC_j;                                     \
                                    for (PUTC_j = 0; PUTC_j < PUTC_n; ++PUTC_j) {   \
+                                       TR_PUTC(PUTC_buf[PUTC_j]);                  \
                                        NCURSES_OUTC_FUNC (NCURSES_SP_ARGx PUTC_buf[PUTC_j]); \
                                    }                                               \
                                }                                                   \
@@ -1489,7 +1427,10 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define ARG_CH_T       NCURSES_CH_T
 #define CARG_CH_T      NCURSES_CH_T
 #define PUTC_DATA      /* nothing */
-#define PUTC(ch)       NCURSES_OUTC_FUNC (NCURSES_SP_ARGx (int) ch)
+#define PUTC(ch)       { \
+                           TR_PUTC(ch); \
+                           NCURSES_OUTC_FUNC (NCURSES_SP_ARGx (int) ch); \
+                       }
 
 #define BLANK          (' '|A_NORMAL)
 #define ZEROS          ('\0'|A_NORMAL)
@@ -1498,6 +1439,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define isWidecExt(ch) (0)
 #define if_WIDEC(code) /* nothing */
 
+#define Charable(ch)   ((ch) >= ' ' && (ch) <= '~')
 #define L(ch)          ch
 #endif /* } */
 
@@ -1544,23 +1486,28 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 /* FreeAndNull() is not a comma-separated expression because some compilers
  * do not accept a mixture of void with values.
  */
-#define FreeAndNull(p)   free(p); p = 0
+#define FreeAndNull(p)   do { free(p); p = 0; } while (0)
 
 #include <nc_alloc.h>
+#include <nc_access.h>
 
 /*
  * Use these for tic/infocmp malloc failures.  Generally the ncurses library
  * tries to limp along after a failure.
  */
 #define TYPE_MALLOC(type, size, name) \
-       name = typeMalloc(type, size); \
-       if (name == 0) \
-           _nc_err_abort(MSG_NO_MEMORY)
+       do { \
+           name = typeMalloc(type, size); \
+           if (name == 0) \
+               _nc_err_abort(MSG_NO_MEMORY); \
+       } while (0)
 
 #define TYPE_REALLOC(type, size, name) \
-       name = typeRealloc(type, size, name); \
-       if (name == 0) \
-           _nc_err_abort(MSG_NO_MEMORY)
+       do { \
+           name = typeRealloc(type, size, name); \
+           if (name == 0) \
+               _nc_err_abort(MSG_NO_MEMORY); \
+       } while (0)
 
 /*
  * TTY bit definition for converting tabs to spaces.
@@ -1579,6 +1526,14 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 # endif
 #endif
 
+#ifdef __TANDEM
+#define ROOT_UID 65535
+#endif
+
+#ifndef ROOT_UID
+#define ROOT_UID 0
+#endif
+
 /*
  * Standardize/simplify common loops
  */
@@ -1595,6 +1550,9 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define T_CREATE(fmt) "create :" fmt
 #define T_RETURN(fmt) "return }" fmt
 
+#define NonNull(s)              ((s) != 0 ? s : "<null>")
+#define NonEmpty(s)             ((s) != 0 && *(s) != '\0')
+
 #ifdef TRACE
 
 #if USE_REENTRANT
@@ -1603,12 +1561,16 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #define TPUTS_TRACE(s) _nc_tputs_trace = s;
 #endif
 
+#ifdef HAVE_CONSISTENT_GETENV
 #define START_TRACE() \
        if ((_nc_tracing & TRACE_MAXIMUM) == 0) { \
            int t = _nc_getenv_num("NCURSES_TRACE"); \
            if (t >= 0) \
-               trace((unsigned) t); \
+               curses_trace((unsigned) t); \
        }
+#else
+#define START_TRACE() /* nothing */
+#endif
 
 /*
  * Many of the _tracef() calls use static buffers; lock the trace state before
@@ -1630,7 +1592,11 @@ extern NCURSES_EXPORT(void)      _nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE
 #define TRACE_RETURN2(value,dst,src) return _nc_retrace_##dst##_##src(value)
 #define TRACE_RETURN_SP(value,type)  return _nc_retrace_##type(SP_PARM, value)
 
-#define NonNull(s)     ((s) != 0 ? s : "<null>")
+typedef void VoidFunc(void);
+
+#define TR_FUNC_LEN            ((sizeof(void *) + sizeof(void (*)(void))) * 2 + 4)
+#define TR_FUNC_BFR(max)       char tr_func_data[max][TR_FUNC_LEN]
+#define TR_FUNC_ARG(num,func)  _nc_fmt_funcptr(&tr_func_data[num][0], (const char *)&(func), sizeof((func)))
 
 #define returnAttr(code)       TRACE_RETURN(code,attr_t)
 #define returnBits(code)       TRACE_RETURN(code,unsigned)
@@ -1648,13 +1614,15 @@ extern NCURSES_EXPORT(void)     _nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE
 #define returnVoidPtr(code)    TRACE_RETURN1(code,void_ptr)
 #define returnWin(code)                TRACE_RETURN1(code,win)
 
+#define returnDB(rc)           do { TR(TRACE_DATABASE,(T_RETURN("code %d"), (rc))); return (rc); } while (0)
+
 extern NCURSES_EXPORT(NCURSES_BOOL)     _nc_retrace_bool (int);
 extern NCURSES_EXPORT(NCURSES_CONST void *) _nc_retrace_cvoid_ptr (NCURSES_CONST void *);
 extern NCURSES_EXPORT(SCREEN *)         _nc_retrace_sp (SCREEN *);
 extern NCURSES_EXPORT(WINDOW *)         _nc_retrace_win (WINDOW *);
 extern NCURSES_EXPORT(attr_t)           _nc_retrace_attr_t (attr_t);
 extern NCURSES_EXPORT(char *)           _nc_retrace_ptr (char *);
-extern NCURSES_EXPORT(char *)           _nc_trace_ttymode(TTY *tty);
+extern NCURSES_EXPORT(char *)           _nc_trace_ttymode(const TTY *tty);
 extern NCURSES_EXPORT(char *)           _nc_varargs (const char *, va_list);
 extern NCURSES_EXPORT(chtype)           _nc_retrace_chtype (chtype);
 extern NCURSES_EXPORT(const char *)     _nc_altcharset_name(attr_t, chtype);
@@ -1667,6 +1635,8 @@ extern NCURSES_EXPORT(unsigned)         _nc_retrace_unsigned (unsigned);
 extern NCURSES_EXPORT(void *)           _nc_retrace_void_ptr (void *);
 extern NCURSES_EXPORT(void)             _nc_fifo_dump (SCREEN *);
 
+extern NCURSES_EXPORT(char *)           _nc_fmt_funcptr(char *, const char *, size_t);
+
 #if USE_REENTRANT
 NCURSES_WRAPPED_VAR(long, _nc_outchars);
 NCURSES_WRAPPED_VAR(const char *, _nc_tputs_trace);
@@ -1681,6 +1651,10 @@ extern NCURSES_EXPORT_VAR(long)         _nc_outchars;
 
 extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
 
+extern NCURSES_EXPORT(char *) _nc_tracebits (void);
+extern NCURSES_EXPORT(char *) _tracemouse (const MEVENT *);
+extern NCURSES_EXPORT(void) _tracedump (const char *, WINDOW *);
+
 #if USE_WIDEC_SUPPORT
 extern NCURSES_EXPORT(const char *) _nc_viswbuf2 (int, const wchar_t *);
 extern NCURSES_EXPORT(const char *) _nc_viswbufn (const wchar_t *, int);
@@ -1696,6 +1670,7 @@ extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
 #define T(a)
 #define TR(n, a)
 #define TPUTS_TRACE(s)
+#define TR_FUNC_BFR(max)
 
 #define returnAttr(code)       return code
 #define returnBits(code)       return code
@@ -1713,6 +1688,8 @@ extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
 #define returnVoidPtr(code)    return code
 #define returnWin(code)                return code
 
+#define returnDB(code)         return code
+
 #endif /* TRACE/!TRACE */
 
 /*
@@ -1780,7 +1757,7 @@ extern    NCURSES_EXPORT(void) name (void); \
 #if USE_XMC_SUPPORT
 #define UpdateAttrs(sp,c) if (!SameAttrOf(SCREEN_ATTRS(sp), c)) { \
                                attr_t chg = AttrOf(SCREEN_ATTRS(sp)); \
-                               VIDATTR(sp, AttrOf(c), GetPair(c)); \
+                               VIDPUTS(sp, AttrOf(c), GetPair(c)); \
                                if (magic_cookie_glitch > 0 \
                                 && XMC_CHANGES((chg ^ AttrOf(SCREEN_ATTRS(sp))))) { \
                                        T(("%s @%d before glitch %d,%d", \
@@ -1792,7 +1769,7 @@ extern    NCURSES_EXPORT(void) name (void); \
                        }
 #else
 #define UpdateAttrs(sp,c) if (!SameAttrOf(SCREEN_ATTRS(sp), c)) { \
-                                   VIDATTR(sp, AttrOf(c), GetPair(c)); \
+                                   VIDPUTS(sp, AttrOf(c), GetPair(c)); \
                        }
 #endif
 
@@ -1859,6 +1836,20 @@ extern NCURSES_EXPORT(void) _nc_expanded (void);
 #define getcwd(buf,len) getwd(buf)
 #endif
 
+#define save_ttytype(termp) \
+       if (TerminalType(termp).term_names != 0) { \
+           _nc_STRNCPY(ttytype, \
+                       TerminalType(termp).term_names, \
+                       NAMESIZE - 1); \
+           ttytype[NAMESIZE - 1] = '\0'; \
+       }
+
+#if !NCURSES_WCWIDTH_GRAPHICS
+extern NCURSES_EXPORT(int) _nc_wacs_width(unsigned);
+#else
+#define _nc_wacs_width(ch) wcwidth(ch)
+#endif
+
 /* charable.c */
 #if USE_WIDEC_SUPPORT
 extern NCURSES_EXPORT(bool) _nc_is_charable(wchar_t);
@@ -1881,6 +1872,16 @@ typedef struct
        short   source;
 } alias_table_data;
 
+/* comp_userdefs.c */
+typedef struct {
+       short   ute_name;       /* offset of name to hash on */
+       unsigned ute_type;      /* mask (BOOLEAN, NUMBER, STRING) */
+       unsigned ute_argc;      /* number of parameters */
+       unsigned ute_args;      /* bit-mask for string parameters */
+       short   ute_index;      /* index of associated variable in its array */
+       short   ute_link;       /* index in table of next hash, or -1 */
+} user_table_data;
+
 /* doupdate.c */
 #if USE_XMC_SUPPORT
 extern NCURSES_EXPORT(void) _nc_do_xmc_glitch (attr_t);
@@ -1906,7 +1907,16 @@ extern NCURSES_EXPORT(int) _nc_wchstrlen(const cchar_t *);
 #endif
 
 /* lib_color.c */
+extern NCURSES_EXPORT(int) _nc_init_color(SCREEN *, int, int, int, int);
+extern NCURSES_EXPORT(int) _nc_init_pair(SCREEN *, int, int, int);
+extern NCURSES_EXPORT(int) _nc_pair_content(SCREEN *, int, int *, int *);
 extern NCURSES_EXPORT(bool) _nc_reset_colors(void);
+extern NCURSES_EXPORT(void) _nc_reserve_pairs(SCREEN *, int);
+extern NCURSES_EXPORT(void) _nc_change_pair(SCREEN *, int);
+
+#define ReservePairs(sp,want) \
+           if ((sp->_color_pairs == 0) || (want >= sp->_pair_alloc)) \
+               _nc_reserve_pairs(sp, want)
 
 /* lib_getch.c */
 extern NCURSES_EXPORT(int) _nc_wgetch(WINDOW *, int *, int EVENTLIST_2nd(_nc_eventlist *));
@@ -1940,30 +1950,43 @@ extern NCURSES_EXPORT(bool) _nc_has_mouse (SCREEN *);
 extern NCURSES_EXPORT(char *) _nc_get_locale(void);
 extern NCURSES_EXPORT(int)    _nc_unicode_locale(void);
 extern NCURSES_EXPORT(int)    _nc_locale_breaks_acs(TERMINAL *);
-extern NCURSES_EXPORT(int)    _nc_setupterm(NCURSES_CONST char *, int, int *, int);
+extern NCURSES_EXPORT(int)    _nc_setupterm(const char *, int, int *, int);
 extern NCURSES_EXPORT(void)   _nc_tinfo_cmdch(TERMINAL *, int);
 
+#ifdef USE_PTHREADS
+extern NCURSES_EXPORT(SCREEN *) _nc_find_prescr(void);
+extern NCURSES_EXPORT(void)   _nc_forget_prescr(void);
+#else
+#define _nc_find_prescr()     _nc_prescreen.allocated
+#define _nc_forget_prescr()   _nc_prescreen.allocated = 0
+#endif
+
 /* lib_set_term.c */
 extern NCURSES_EXPORT(int)    _nc_ripoffline(int, int(*)(WINDOW*, int));
 
 /* lib_setup.c */
+#define ExitTerminfo(code)    exit_terminfo(code)
+
 #define SETUP_FAIL ERR
-#define ret_error(code, fmt, arg)      if (errret) {\
-                                           *errret = code;\
+
+#define ret_error(rc, fmt, p, q)       if (errret) {\
+                                           *errret = rc;\
+                                           q;\
                                            returnCode(SETUP_FAIL);\
                                        } else {\
-                                           fprintf(stderr, fmt, arg);\
-                                           exit(EXIT_FAILURE);\
+                                           fprintf(stderr, fmt, p);\
+                                           q;\
+                                           ExitTerminfo(EXIT_FAILURE);\
                                        }
 
-#define ret_error1(code, fmt, arg)     ret_error(code, "'%s': " fmt, arg)
+#define ret_error1(rc, fmt, p, q)      ret_error(rc, "'%s': " fmt, p, q)
 
-#define ret_error0(code, msg)          if (errret) {\
-                                           *errret = code;\
+#define ret_error0(rc, msg)            if (errret) {\
+                                           *errret = rc;\
                                            returnCode(SETUP_FAIL);\
                                        } else {\
                                            fprintf(stderr, msg);\
-                                           exit(EXIT_FAILURE);\
+                                           ExitTerminfo(EXIT_FAILURE);\
                                        }
 
 /* lib_tstp.c */
@@ -2007,7 +2030,6 @@ 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(SCREEN *) _nc_screen_of (WINDOW *);
 extern NCURSES_EXPORT(TERMINAL*) _nc_get_cur_term (void);
 extern NCURSES_EXPORT(WINDOW *) _nc_makenew (int, int, int, int, int);
@@ -2026,14 +2048,14 @@ extern NCURSES_EXPORT(int) _nc_outch (int);
 extern NCURSES_EXPORT(int) _nc_putchar (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_setup_tinfo(const char *, TERMTYPE *);
+extern NCURSES_EXPORT(int) _nc_read_termcap_entry (const char *const, TERMTYPE2 *const);
+extern NCURSES_EXPORT(int) _nc_setup_tinfo(const char *, TERMTYPE2 *);
 extern NCURSES_EXPORT(int) _nc_setupscreen (int, int, FILE *, int, int);
 extern NCURSES_EXPORT(int) _nc_timed_wait (SCREEN *, int, int, int * EVENTLIST_2nd(_nc_eventlist *));
+extern NCURSES_EXPORT(void) _nc_init_termtype (TERMTYPE2 *const);
 extern NCURSES_EXPORT(void) _nc_do_color (int, int, int, NCURSES_OUTC);
 extern NCURSES_EXPORT(void) _nc_flush (void);
-extern NCURSES_EXPORT(void) _nc_free_and_exit (int) GCC_NORETURN;
-extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, TERMTYPE2 *);
 extern NCURSES_EXPORT(void) _nc_freeall (void);
 extern NCURSES_EXPORT(void) _nc_hash_map (void);
 extern NCURSES_EXPORT(void) _nc_init_keytry (SCREEN *);
@@ -2047,6 +2069,23 @@ extern NCURSES_EXPORT(void) _nc_signal_handler (int);
 extern NCURSES_EXPORT(void) _nc_synchook (WINDOW *);
 extern NCURSES_EXPORT(void) _nc_trace_tries (TRIES *);
 
+#if NCURSES_EXT_NUMBERS
+extern NCURSES_EXPORT(const TERMTYPE2 *) _nc_fallback2 (const char *);
+#else
+#define _nc_fallback2(tp) _nc_fallback(tp)
+#endif
+
+#if NCURSES_EXT_NUMBERS
+extern NCURSES_EXPORT(void) _nc_copy_termtype2 (TERMTYPE2 *, const TERMTYPE2 *);
+extern NCURSES_EXPORT(void) _nc_export_termtype2(TERMTYPE *, const TERMTYPE2 *);
+#else
+#define _nc_copy_termtype2(dst,src) _nc_copy_termtype((dst),(src))
+#define _nc_export_termtype2(dst,src) /* nothing */
+#define _nc_free_termtype2(t) _nc_free_termtype(t)
+/* also... */
+#define _nc_read_entry2 _nc_read_entry
+#endif
+
 #if NO_LEAKS
 extern NCURSES_EXPORT(void) _nc_alloc_entry_leaks(void);
 extern NCURSES_EXPORT(void) _nc_captoinfo_leaks(void);
@@ -2054,9 +2093,11 @@ extern NCURSES_EXPORT(void) _nc_codes_leaks(void);
 extern NCURSES_EXPORT(void) _nc_comp_captab_leaks(void);
 extern NCURSES_EXPORT(void) _nc_comp_error_leaks(void);
 extern NCURSES_EXPORT(void) _nc_comp_scan_leaks(void);
+extern NCURSES_EXPORT(void) _nc_comp_userdefs_leaks(void);
 extern NCURSES_EXPORT(void) _nc_db_iterator_leaks(void);
 extern NCURSES_EXPORT(void) _nc_keyname_leaks(void);
 extern NCURSES_EXPORT(void) _nc_names_leaks(void);
+extern NCURSES_EXPORT(void) _nc_tgetent_leak(TERMINAL *);
 extern NCURSES_EXPORT(void) _nc_tgetent_leaks(void);
 #endif
 
@@ -2096,11 +2137,10 @@ extern NCURSES_EXPORT(int) _nc_eventlist_timeout(_nc_eventlist *);
  */
 #if USE_WIDEC_SUPPORT
 
-#if defined(__MINGW32__)
+#if defined(_NC_WINDOWS) && !defined(_NC_MSC) && !defined(EXP_WIN32_DRIVER)
 /*
  * MinGW has wide-character functions, but they do not work correctly.
  */
-
 extern int __MINGW_NOTHROW _nc_wctomb(char *, wchar_t);
 #define wctomb(s,wc) _nc_wctomb(s,wc)
 #define wcrtomb(s,wc,n) _nc_wctomb(s,wc)
@@ -2111,19 +2151,19 @@ extern int __MINGW_NOTHROW _nc_mbtowc(wchar_t *, const char *, size_t);
 extern int __MINGW_NOTHROW _nc_mblen(const char *, size_t);
 #define mblen(s,n) _nc_mblen(s, n)
 
-#endif /* __MINGW32__ */
+#endif /* _NC_WINDOWS && !_NC_MSC */
 
 #if HAVE_MBTOWC && HAVE_MBLEN
 #define reset_mbytes(state) IGNORE_RC(mblen(NULL, (size_t) 0)), IGNORE_RC(mbtowc(NULL, NULL, (size_t) 0))
 #define count_mbytes(buffer,length,state) mblen(buffer,length)
 #define check_mbytes(wch,buffer,length,state) \
-       (int) mbtowc(&wch, buffer, length)
+       (int) mbtowc(&(wch), buffer, length)
 #define state_unused
 #elif HAVE_MBRTOWC && HAVE_MBRLEN
 #define reset_mbytes(state) init_mb(state)
-#define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
+#define count_mbytes(buffer,length,state) mbrlen(buffer,length,&(state))
 #define check_mbytes(wch,buffer,length,state) \
-       (int) mbrtowc(&wch, buffer, length, &state)
+       (int) mbrtowc(&(wch), buffer, length, &(state))
 #else
 make an error
 #endif
@@ -2142,7 +2182,7 @@ extern NCURSES_EXPORT_VAR(int *) _nc_oldnums;
 
 #define USE_SETBUF_0 0
 
-#define NC_OUTPUT(sp) ((sp != 0) ? sp->_ofp : stdout)
+#define NC_OUTPUT(sp) ((sp != 0 && sp->_ofp != 0) ? sp->_ofp : stdout)
 
 /*
  * On systems with a broken linker, define 'SP' as a function to force the
@@ -2172,7 +2212,7 @@ extern NCURSES_EXPORT_VAR(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
+ * if the application is running multiple screens under X, it is quite possible
  * they could all have type xterm but have different sizes!  So...
  */
 #define screen_lines(sp)        (sp)->_lines
@@ -2333,12 +2373,16 @@ extern NCURSES_EXPORT(int)      TINFO_MVCUR(SCREEN*, int, int, int, int);
 #define TINFO_MVCUR             NCURSES_SP_NAME(_nc_mvcur)
 #endif
 
+#if defined(EXP_WIN32_DRIVER)
+#include <nc_win32.h>
+#endif
+
 /*
  * Entrypoints using an extra parameter with the terminal driver.
  */
 #ifdef USE_TERM_DRIVER
 extern NCURSES_EXPORT(void)   _nc_get_screensize(SCREEN *, TERMINAL *, int *, int *);
-extern NCURSES_EXPORT(int)    _nc_setupterm_ex(TERMINAL **, NCURSES_CONST char *, int , int *, int);
+extern NCURSES_EXPORT(int)    _nc_setupterm_ex(TERMINAL **, const char *, int , int *, int);
 #define TINFO_GET_SIZE(sp, tp, lp, cp) \
        _nc_get_screensize(sp, tp, lp, cp)
 #define TINFO_SET_CURTERM(sp, tp) \
@@ -2355,6 +2399,9 @@ extern NCURSES_EXPORT(void)   _nc_get_screensize(SCREEN *, int *, int *);
        _nc_setupterm(name, fd, err, reuse)
 #endif /* !USE_TERM_DRIVER */
 
+#ifdef EXP_WIN32_DRIVER
+extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_TINFO_DRIVER;
+#else
 #ifdef USE_TERM_DRIVER
 #if defined(USE_WIN32CON_DRIVER)
 #include <nc_mingw.h>
@@ -2368,13 +2415,16 @@ extern NCURSES_EXPORT(int) _nc_mingw_console_read(
 extern NCURSES_EXPORT(int) _nc_mingw_testmouse(
     SCREEN * sp,
     HANDLE fd,
-    int delay);
+    int delay EVENTLIST_2nd(_nc_eventlist*));
 #else
 #endif
 extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_TINFO_DRIVER;
-#endif
+#endif /* USE_TERM_DRIVER */
+#endif /* EXP_WIN32_DRIVER */
 
-#if defined(USE_TERM_DRIVER) && defined(USE_WIN32CON_DRIVER)
+#if defined(USE_TERM_DRIVER) && defined(EXP_WIN32_DRIVER)
+#define NC_ISATTY(fd) (0 != _nc_console_isatty(fd))
+#elif defined(USE_TERM_DRIVER) && defined(USE_WIN32CON_DRIVER)
 #define NC_ISATTY(fd) _nc_mingw_isatty(fd)
 #else
 #define NC_ISATTY(fd) isatty(fd)
@@ -2383,15 +2433,21 @@ extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_TINFO_DRIVER;
 #ifdef USE_TERM_DRIVER
 #  define IsTermInfo(sp)       ((TCBOf(sp) != 0) && ((TCBOf(sp)->drv->isTerminfo)))
 #  define HasTInfoTerminal(sp) ((0 != TerminalOf(sp)) && IsTermInfo(sp))
-#  if defined(USE_WIN32CON_DRIVER)
-#    define IsTermInfoOnConsole(sp) (IsTermInfo(sp)&&_nc_mingw_isconsole(TerminalOf(sp)->Filedes))
-#else
+#  if defined(EXP_WIN32_DRIVER)
+#    define IsTermInfoOnConsole(sp) (IsTermInfo(sp) && _nc_console_test(TerminalOf(sp)->Filedes))
+#  elif defined(USE_WIN32CON_DRIVER)
+#    define IsTermInfoOnConsole(sp) (IsTermInfo(sp) && _nc_mingw_isconsole(TerminalOf(sp)->Filedes))
+#  else
 #    define IsTermInfoOnConsole(sp) FALSE
 #  endif
 #else
 #  define IsTermInfo(sp)       TRUE
 #  define HasTInfoTerminal(sp) (0 != TerminalOf(sp))
-#  define IsTermInfoOnConsole(sp) FALSE
+#  if defined(EXP_WIN32_DRIVER)
+#    define IsTermInfoOnConsole(sp) _nc_console_test(TerminalOf(sp)->Filedes)
+#  else
+#    define IsTermInfoOnConsole(sp) FALSE
+#  endif
 #endif
 
 #define IsValidTIScreen(sp)  (HasTInfoTerminal(sp))
@@ -2423,7 +2479,6 @@ extern NCURSES_EXPORT(WINDOW *) NCURSES_SP_NAME(_nc_makenew) (SCREEN*, int, int,
 extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(_nc_reset_colors)(SCREEN*);
 extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_printf_string)(SCREEN*, const char *, va_list);
 extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_curs_set)(SCREEN*,int);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_get_tty_mode)(SCREEN*,TTY*);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mcprint)(SCREEN*,char*, int);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_msec_cost)(SCREEN*, const char *, int);
@@ -2432,23 +2487,15 @@ extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_outch)(SCREEN*, int);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putchar)(SCREEN*, int);
 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_resetty)(SCREEN*);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resize_term)(SCREEN*,int,int);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_ripoffline)(SCREEN*, int, int (*)(WINDOW *,int));
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_savetty)(SCREEN*);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_init)(SCREEN*,const char*);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_restore)(SCREEN*, const char*);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scrolln)(SCREEN*, int, int, int, int);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tty_mode)(SCREEN*, TTY*);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_setupscreen)(SCREEN**, int, int, FILE *, int, int);
 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetent)(SCREEN*,char*,const char *);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetnum)(SCREEN*,NCURSES_CONST char*);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vid_attr)(SCREEN *, attr_t, NCURSES_COLOR_T, void *);
-extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidputs)(SCREEN*,chtype,int(*) (SCREEN*, int));
 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_color)(SCREEN*, int, int, int, NCURSES_SP_OUTC);
 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_xmc_glitch)(SCREEN*, attr_t);
 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_flush)(SCREEN*);
-extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_free_and_exit)(SCREEN*, int) GCC_NORETURN;
+extern GCC_NORETURN NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_free_and_exit)(SCREEN*, int);
 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_freeall)(SCREEN*);
 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_hash_map)(SCREEN*);
 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_init_acs)(SCREEN*);