Update to ncurses-6.0

Change-Id: I98ab2ea8a5e13cca9f8b7cf6277b9b14a4da4299
diff --git a/ncurses/widechar/lib_add_wch.c b/ncurses/widechar/lib_add_wch.c
index 93b41bb..38d3130 100644
--- a/ncurses/widechar/lib_add_wch.c
+++ b/ncurses/widechar/lib_add_wch.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2004,2006 Free Software Foundation, Inc.                   *
+ * Copyright (c) 2004-2010,2011 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            *
@@ -35,39 +35,387 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_add_wch.c,v 1.6 2006/12/02 21:19:17 tom Exp $")
+#if HAVE_WCTYPE_H
+#include <wctype.h>
+#endif
+
+MODULE_ID("$Id: lib_add_wch.c,v 1.12 2011/03/22 09:31:15 Petr.Pavlu Exp $")
+
+/* clone/adapt lib_addch.c */
+static const cchar_t blankchar = NewChar(BLANK_TEXT);
+
+/*
+ * Ugly microtweaking alert.  Everything from here to end of module is
+ * likely to be speed-critical -- profiling data sure says it is!
+ * Most of the important screen-painting functions are shells around
+ * wadd_wch().  So we make every effort to reduce function-call overhead
+ * by inlining stuff, even at the cost of making wrapped copies for
+ * export.  Also we supply some internal versions that don't call the
+ * window sync hook, for use by string-put functions.
+ */
+
+/* Return bit mask for clearing color pair number if given ch has color */
+#define COLOR_MASK(ch) (~(attr_t)((ch) & A_COLOR ? A_COLOR : 0))
+
+static NCURSES_INLINE cchar_t
+render_char(WINDOW *win, cchar_t ch)
+/* compute a rendition of the given char correct for the current context */
+{
+    attr_t a = WINDOW_ATTRS(win);
+    int pair = GetPair(ch);
+
+    if (ISBLANK(ch)
+	&& AttrOf(ch) == A_NORMAL
+	&& pair == 0) {
+	/* color/pair in attrs has precedence over bkgrnd */
+	ch = win->_nc_bkgd;
+	SetAttr(ch, a | AttrOf(win->_nc_bkgd));
+	if ((pair = GET_WINDOW_PAIR(win)) == 0)
+	    pair = GetPair(win->_nc_bkgd);
+	SetPair(ch, pair);
+    } else {
+	/* color in attrs has precedence over bkgrnd */
+	a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
+	/* color in ch has precedence */
+	if (pair == 0) {
+	    if ((pair = GET_WINDOW_PAIR(win)) == 0)
+		pair = GetPair(win->_nc_bkgd);
+	}
+	AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
+	SetPair(ch, pair);
+    }
+
+    TR(TRACE_VIRTPUT,
+       ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
+	_tracech_t2(1, CHREF(win->_nc_bkgd)),
+	GetPair(win->_nc_bkgd),
+	_traceattr(WINDOW_ATTRS(win)),
+	GET_WINDOW_PAIR(win),
+	_tracech_t2(3, CHREF(ch)),
+	GetPair(ch)));
+
+    return (ch);
+}
+
+/* check if position is legal; if not, return error */
+#ifndef NDEBUG			/* treat this like an assertion */
+#define CHECK_POSITION(win, x, y) \
+	if (y > win->_maxy \
+	 || x > win->_maxx \
+	 || y < 0 \
+	 || x < 0) { \
+		TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
+				   "(_maxx = %d, _maxy = %d)", win, x, y, \
+				   win->_maxx, win->_maxy)); \
+		return(ERR); \
+	}
+#else
+#define CHECK_POSITION(win, x, y)	/* nothing */
+#endif
+
+static bool
+newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T * ypos)
+{
+    bool result = FALSE;
+
+    if (*ypos >= win->_regtop && *ypos == win->_regbottom) {
+	*ypos = win->_regbottom;
+	result = TRUE;
+    } else {
+	*ypos = (NCURSES_SIZE_T) (*ypos + 1);
+    }
+    return result;
+}
+
+/*
+ * The _WRAPPED flag is useful only for telling an application that we've just
+ * wrapped the cursor.  We don't do anything with this flag except set it when
+ * wrapping, and clear it whenever we move the cursor.  If we try to wrap at
+ * the lower-right corner of a window, we cannot move the cursor (since that
+ * wouldn't be legal).  So we return an error (which is what SVr4 does). 
+ * Unlike SVr4, we can successfully add a character to the lower-right corner
+ * (Solaris 2.6 does this also, however).
+ */
+static int
+wrap_to_next_line(WINDOW *win)
+{
+    win->_flags |= _WRAPPED;
+    if (newline_forces_scroll(win, &(win->_cury))) {
+	win->_curx = win->_maxx;
+	if (!win->_scroll)
+	    return (ERR);
+	scroll(win);
+    }
+    win->_curx = 0;
+    return (OK);
+}
+
+static int wadd_wch_literal(WINDOW *, cchar_t);
+/*
+ * Fill the given number of cells with blanks using the current background
+ * rendition.  This saves/restores the current x-position.
+ */
+static void
+fill_cells(WINDOW *win, int count)
+{
+    cchar_t blank = blankchar;
+    int save_x = win->_curx;
+    int save_y = win->_cury;
+
+    while (count-- > 0) {
+	if (wadd_wch_literal(win, blank) == ERR)
+	    break;
+    }
+    win->_curx = (NCURSES_SIZE_T) save_x;
+    win->_cury = (NCURSES_SIZE_T) save_y;
+}
+
+static int
+wadd_wch_literal(WINDOW *win, cchar_t ch)
+{
+    int x;
+    int y;
+    struct ldat *line;
+
+    x = win->_curx;
+    y = win->_cury;
+
+    CHECK_POSITION(win, x, y);
+
+    ch = render_char(win, ch);
+
+    line = win->_line + y;
+
+    CHANGED_CELL(line, x);
+
+    /*
+     * Non-spacing characters are added to the current cell.
+     *
+     * Spacing characters that are wider than one column require some display
+     * adjustments.
+     */
+    {
+	int len = wcwidth(CharOf(ch));
+	int i;
+	int j;
+	wchar_t *chars;
+
+	if (len == 0) {		/* non-spacing */
+	    if ((x > 0 && y >= 0)
+		|| (win->_maxx >= 0 && win->_cury >= 1)) {
+		if (x > 0 && y >= 0)
+		    chars = (win->_line[y].text[x - 1].chars);
+		else
+		    chars = (win->_line[y - 1].text[win->_maxx].chars);
+		for (i = 0; i < CCHARW_MAX; ++i) {
+		    if (chars[i] == 0) {
+			TR(TRACE_VIRTPUT,
+			   ("added non-spacing %d: %x",
+			    x, (int) CharOf(ch)));
+			chars[i] = CharOf(ch);
+			break;
+		    }
+		}
+	    }
+	    goto testwrapping;
+	} else if (len > 1) {	/* multi-column characters */
+	    /*
+	     * Check if the character will fit on the current line.  If it does
+	     * not fit, fill in the remainder of the line with blanks.  and
+	     * move to the next line.
+	     */
+	    if (len > win->_maxx + 1) {
+		TR(TRACE_VIRTPUT, ("character will not fit"));
+		return ERR;
+	    } else if (x + len > win->_maxx + 1) {
+		int count = win->_maxx + 1 - x;
+		TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
+		fill_cells(win, count);
+		if (wrap_to_next_line(win) == ERR)
+		    return ERR;
+		x = win->_curx;
+		y = win->_cury;
+		line = win->_line + y;
+	    }
+	    /*
+	     * Check for cells which are orphaned by adding this character, set
+	     * those to blanks.
+	     *
+	     * FIXME: this actually could fill j-i cells, more complicated to
+	     * setup though.
+	     */
+	    for (i = 0; i < len; ++i) {
+		if (isWidecBase(win->_line[y].text[x + i])) {
+		    break;
+		} else if (isWidecExt(win->_line[y].text[x + i])) {
+		    for (j = i; x + j <= win->_maxx; ++j) {
+			if (!isWidecExt(win->_line[y].text[x + j])) {
+			    TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
+			    fill_cells(win, j);
+			    break;
+			}
+		    }
+		    break;
+		}
+	    }
+	    /*
+	     * Finally, add the cells for this character.
+	     */
+	    for (i = 0; i < len; ++i) {
+		cchar_t value = ch;
+		SetWidecExt(value, i);
+		TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
+				   i + 1, len,
+				   win->_begy + y, win->_begx + x));
+		line->text[x] = value;
+		CHANGED_CELL(line, x);
+		++x;
+	    }
+	    goto testwrapping;
+	}
+    }
+
+    /*
+     * Single-column characters.
+     */
+    line->text[x++] = ch;
+    /*
+     * This label is used only for wide-characters.
+     */
+  testwrapping:
+
+    TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
+		       (long) win->_cury, (long) win->_curx, x - 1,
+		       _tracech_t(CHREF(ch))));
+
+    if (x > win->_maxx) {
+	return wrap_to_next_line(win);
+    }
+    win->_curx = (NCURSES_SIZE_T) x;
+    return OK;
+}
+
+static NCURSES_INLINE int
+wadd_wch_nosync(WINDOW *win, cchar_t ch)
+/* the workhorse function -- add a character to the given window */
+{
+    NCURSES_SIZE_T x, y;
+    wchar_t *s;
+    int tabsize = 8;
+#if USE_REENTRANT
+    SCREEN *sp = _nc_screen_of(win);
+#endif
+
+    /*
+     * If we are using the alternate character set, forget about locale.
+     * Otherwise, if the locale claims the code is printable, treat it that
+     * way.
+     */
+    if ((AttrOf(ch) & A_ALTCHARSET)
+	|| iswprint((wint_t) CharOf(ch)))
+	return wadd_wch_literal(win, ch);
+
+    /*
+     * Handle carriage control and other codes that are not printable, or are
+     * known to expand to more than one character according to unctrl().
+     */
+    x = win->_curx;
+    y = win->_cury;
+
+    switch (CharOf(ch)) {
+    case '\t':
+#if USE_REENTRANT
+	tabsize = *ptrTabsize(sp);
+#else
+	tabsize = TABSIZE;
+#endif
+	x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize)));
+	/*
+	 * Space-fill the tab on the bottom line so that we'll get the
+	 * "correct" cursor position.
+	 */
+	if ((!win->_scroll && (y == win->_regbottom))
+	    || (x <= win->_maxx)) {
+	    cchar_t blank = blankchar;
+	    AddAttr(blank, AttrOf(ch));
+	    while (win->_curx < x) {
+		if (wadd_wch_literal(win, blank) == ERR)
+		    return (ERR);
+	    }
+	    break;
+	} else {
+	    wclrtoeol(win);
+	    win->_flags |= _WRAPPED;
+	    if (newline_forces_scroll(win, &y)) {
+		x = win->_maxx;
+		if (win->_scroll) {
+		    scroll(win);
+		    x = 0;
+		}
+	    } else {
+		x = 0;
+	    }
+	}
+	break;
+    case '\n':
+	wclrtoeol(win);
+	if (newline_forces_scroll(win, &y)) {
+	    if (win->_scroll)
+		scroll(win);
+	    else
+		return (ERR);
+	}
+	/* FALLTHRU */
+    case '\r':
+	x = 0;
+	win->_flags &= ~_WRAPPED;
+	break;
+    case '\b':
+	if (x == 0)
+	    return (OK);
+	x--;
+	win->_flags &= ~_WRAPPED;
+	break;
+    default:
+	if ((s = wunctrl(&ch)) != 0) {
+	    while (*s) {
+		cchar_t sch;
+		SetChar(sch, *s++, AttrOf(ch));
+		if_EXT_COLORS(SetPair(sch, GetPair(ch)));
+		if (wadd_wch_literal(win, sch) == ERR)
+		    return ERR;
+	    }
+	    return OK;
+	}
+	return ERR;
+    }
+
+    win->_curx = x;
+    win->_cury = y;
+
+    return OK;
+}
+
+/*
+ * The versions below call _nc_synchook().  We wanted to avoid this in the
+ * version exported for string puts; they'll call _nc_synchook once at end
+ * of run.
+ */
+
+/* These are actual entry points */
 
 NCURSES_EXPORT(int)
 wadd_wch(WINDOW *win, const cchar_t *wch)
 {
-    PUTC_DATA;
-    int n;
     int code = ERR;
 
-    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"), win,
-				      _tracech_t(wch)));
+    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"),
+				      (void *) win,
+				      _tracecchar_t(wch)));
 
-    if (win != 0) {
-	PUTC_INIT;
-	for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
-	    attr_t attrs = (wch->attr & A_ATTRIBUTES);
-
-	    if ((PUTC_ch = wch->chars[PUTC_i]) == L'\0')
-		break;
-	    if ((PUTC_n = wcrtomb(PUTC_buf, PUTC_ch, &PUT_st)) <= 0) {
-		code = ERR;
-		if (is8bits(PUTC_ch))
-		    code = waddch(win, UChar(PUTC_ch) | attrs);
-		break;
-	    }
-	    for (n = 0; n < PUTC_n; n++) {
-		if ((code = waddch(win, UChar(PUTC_buf[n]) | attrs)) == ERR) {
-		    break;
-		}
-	    }
-	    if (code == ERR)
-		break;
-	}
+    if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
+	_nc_synchook(win);
+	code = OK;
     }
 
     TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
@@ -77,37 +425,19 @@
 NCURSES_EXPORT(int)
 wecho_wchar(WINDOW *win, const cchar_t *wch)
 {
-    PUTC_DATA;
-    int n;
     int code = ERR;
 
-    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wecho_wchar(%p, %s)"), win,
-				      _tracech_t(wch)));
+    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
+				      (void *) win,
+				      _tracecchar_t(wch)));
 
-    if (win != 0) {
-	PUTC_INIT;
-	for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
-	    attr_t attrs = (wch->attr & A_ATTRIBUTES);
-
-	    if ((PUTC_ch = wch->chars[PUTC_i]) == L'\0')
-		break;
-	    if ((PUTC_n = wcrtomb(PUTC_buf, PUTC_ch, &PUT_st)) <= 0) {
-		code = ERR;
-		if (is8bits(PUTC_ch))
-		    code = waddch(win, UChar(PUTC_ch) | attrs);
-		break;
-	    }
-	    for (n = 0; n < PUTC_n; n++) {
-		if ((code = waddch(win, UChar(PUTC_buf[n]) | attrs)) == ERR) {
-		    break;
-		}
-	    }
-	    if (code == ERR)
-		break;
-	}
-	wrefresh(win);
+    if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
+	bool save_immed = win->_immed;
+	win->_immed = TRUE;
+	_nc_synchook(win);
+	win->_immed = save_immed;
+	code = OK;
     }
-
     TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
     return (code);
 }
diff --git a/ncurses/widechar/lib_box_set.c b/ncurses/widechar/lib_box_set.c
index 35fce46..f9e701f 100644
--- a/ncurses/widechar/lib_box_set.c
+++ b/ncurses/widechar/lib_box_set.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2002-2009,2011 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            *
@@ -39,7 +39,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_box_set.c,v 1.4 2003/12/06 18:02:13 tom Exp $")
+MODULE_ID("$Id: lib_box_set.c,v 1.6 2011/06/25 19:02:07 Vassili.Courzakis Exp $")
 
 NCURSES_EXPORT(int)
 wborder_set(WINDOW *win,
@@ -52,8 +52,8 @@
     NCURSES_SIZE_T endx, endy;
     NCURSES_CH_T wls, wrs, wts, wbs, wtl, wtr, wbl, wbr;
 
-    T((T_CALLED("wborder(%p,%s,%s,%s,%s,%s,%s,%s,%s)"),
-       win,
+    T((T_CALLED("wborder_set(%p,%s,%s,%s,%s,%s,%s,%s,%s)"),
+       (void *) win,
        _tracech_t2(1, ls),
        _tracech_t2(2, rs),
        _tracech_t2(3, ts),
diff --git a/ncurses/widechar/lib_cchar.c b/ncurses/widechar/lib_cchar.c
index b4a0c37..654bebb 100644
--- a/ncurses/widechar/lib_cchar.c
+++ b/ncurses/widechar/lib_cchar.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2001-2005,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 2001-2012,2014 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            *
@@ -35,7 +35,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_cchar.c,v 1.12 2007/05/12 19:03:06 tom Exp $")
+MODULE_ID("$Id: lib_cchar.c,v 1.27 2014/02/01 22:10:42 tom Exp $")
 
 /* 
  * The SuSv2 description leaves some room for interpretation.  We'll assume wch
@@ -47,20 +47,20 @@
 setcchar(cchar_t *wcval,
 	 const wchar_t *wch,
 	 const attr_t attrs,
-	 short color_pair,
+	 NCURSES_PAIRS_T color_pair,
 	 const void *opts)
 {
-    int i;
-    int len;
+    unsigned i;
+    unsigned len;
     int code = OK;
 
     TR(TRACE_CCALLS, (T_CALLED("setcchar(%p,%s,%lu,%d,%p)"),
-		      wcval, _nc_viswbuf(wch),
-		      (unsigned long) attrs, color_pair, opts));
+		      (void *) wcval, _nc_viswbuf(wch),
+		      (unsigned long) attrs, (int) color_pair, opts));
 
-    len = wcslen(wch);
     if (opts != NULL
-	|| (len > 1 && wcwidth(wch[0]) < 0)) {
+	|| wch == NULL
+	|| ((len = (unsigned) wcslen(wch)) > 1 && wcwidth(wch[0]) < 0)) {
 	code = ERR;
     } else {
 	if (len > CCHARW_MAX)
@@ -80,7 +80,7 @@
 	memset(wcval, 0, sizeof(*wcval));
 
 	if (len != 0) {
-	    SetAttr(*wcval, attrs | COLOR_PAIR(color_pair));
+	    SetAttr(*wcval, attrs);
 	    SetPair(CHDEREF(wcval), color_pair);
 	    memcpy(&wcval->chars, wch, len * sizeof(wchar_t));
 	    TR(TRACE_CCALLS, ("copy %d wchars, first is %s", len,
@@ -96,7 +96,7 @@
 getcchar(const cchar_t *wcval,
 	 wchar_t *wch,
 	 attr_t *attrs,
-	 short *color_pair,
+	 NCURSES_PAIRS_T *color_pair,
 	 void *opts)
 {
     wchar_t *wp;
@@ -104,21 +104,29 @@
     int code = ERR;
 
     TR(TRACE_CCALLS, (T_CALLED("getcchar(%p,%p,%p,%p,%p)"),
-		      wcval, wch, attrs, color_pair, opts));
+		      (const void *) wcval,
+		      (void *) wch,
+		      (void *) attrs,
+		      (void *) color_pair,
+		      opts));
 
-    if (opts == NULL) {
-	len = (wp = wmemchr(wcval->chars, L'\0', CCHARW_MAX))
-	    ? wp - wcval->chars
-	    : CCHARW_MAX;
+    if (opts == NULL && wcval != NULL) {
+	len = ((wp = wmemchr(wcval->chars, L'\0', (size_t) CCHARW_MAX))
+	       ? (int) (wp - wcval->chars)
+	       : CCHARW_MAX);
 
 	if (wch == NULL) {
-	    code = len;
+	    /*
+	     * If the value is a null, set the length to 1.
+	     * If the value is not a null, return the length plus 1 for null.
+	     */
+	    code = (len < CCHARW_MAX) ? (len + 1) : CCHARW_MAX;
 	} else if (attrs == 0 || color_pair == 0) {
 	    code = ERR;
 	} else if (len >= 0) {
 	    *attrs = AttrOf(*wcval) & A_ATTRIBUTES;
-	    *color_pair = GetPair(*wcval);
-	    wmemcpy(wch, wcval->chars, (unsigned) len);
+	    *color_pair = (NCURSES_PAIRS_T) GetPair(*wcval);
+	    wmemcpy(wch, wcval->chars, (size_t) len);
 	    wch[len] = L'\0';
 	    code = OK;
 	}
diff --git a/ncurses/widechar/lib_erasewchar.c b/ncurses/widechar/lib_erasewchar.c
index 7d64553..ddce27a 100644
--- a/ncurses/widechar/lib_erasewchar.c
+++ b/ncurses/widechar/lib_erasewchar.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2002-2010,2014 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            *
@@ -32,7 +32,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_erasewchar.c,v 1.1 2002/05/11 20:38:06 tom Exp $")
+MODULE_ID("$Id: lib_erasewchar.c,v 1.3 2014/02/23 01:21:08 tom Exp $")
 
 /*
  *	erasewchar()
@@ -42,14 +42,14 @@
  */
 
 NCURSES_EXPORT(int)
-erasewchar(wchar_t * wch)
+erasewchar(wchar_t *wch)
 {
     int value;
     int result = ERR;
 
     T((T_CALLED("erasewchar()")));
     if ((value = erasechar()) != ERR) {
-	*wch = value;
+	*wch = (wchar_t) value;
 	result = OK;
     }
     returnCode(result);
@@ -63,14 +63,14 @@
  */
 
 NCURSES_EXPORT(int)
-killwchar(wchar_t * wch)
+killwchar(wchar_t *wch)
 {
     int value;
     int result = ERR;
 
     T((T_CALLED("killwchar()")));
     if ((value = killchar()) != ERR) {
-	*wch = value;
+	*wch = (wchar_t) value;
 	result = OK;
     }
     returnCode(result);
diff --git a/ncurses/widechar/lib_get_wch.c b/ncurses/widechar/lib_get_wch.c
index 6cf3129..71d5603 100644
--- a/ncurses/widechar/lib_get_wch.c
+++ b/ncurses/widechar/lib_get_wch.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2007,2008 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2010,2011 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            *
@@ -40,22 +40,7 @@
 #include <curses.priv.h>
 #include <ctype.h>
 
-MODULE_ID("$Id: lib_get_wch.c,v 1.17 2008/08/16 19:22:55 tom Exp $")
-
-#if HAVE_MBTOWC && HAVE_MBLEN
-#define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
-#define count_mbytes(buffer,length,state) mblen(buffer,length)
-#define check_mbytes(wch,buffer,length,state) \
-	(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 check_mbytes(wch,buffer,length,state) \
-	(int) mbrtowc(&wch, buffer, length, &state)
-#else
-make an error
-#endif
+MODULE_ID("$Id: lib_get_wch.c,v 1.23 2011/05/28 23:00:29 tom Exp $")
 
 NCURSES_EXPORT(int)
 wget_wch(WINDOW *win, wint_t *result)
@@ -65,13 +50,13 @@
     char buffer[(MB_LEN_MAX * 9) + 1];	/* allow some redundant shifts */
     int status;
     size_t count = 0;
-    unsigned long value;
+    int value = 0;
     wchar_t wch;
 #ifndef state_unused
     mbstate_t state;
 #endif
 
-    T((T_CALLED("wget_wch(%p)"), win));
+    T((T_CALLED("wget_wch(%p)"), (void *) win));
 
     /*
      * We can get a stream of single-byte characters and KEY_xxx codes from
@@ -95,12 +80,12 @@
 		 * whether the improvement would be worth the effort.
 		 */
 		if (count != 0) {
-		    _nc_ungetch(sp, (int) value);
+		    safe_ungetch(SP_PARM, value);
 		    code = ERR;
 		}
 		break;
 	    } else if (count + 1 >= sizeof(buffer)) {
-		_nc_ungetch(sp, (int) value);
+		safe_ungetch(SP_PARM, value);
 		code = ERR;
 		break;
 	    } else {
@@ -111,7 +96,7 @@
 		    reset_mbytes(state);
 		    if (check_mbytes(wch, buffer, count, state) != status) {
 			code = ERR;	/* the two calls should match */
-			_nc_ungetch(sp, (int) value);
+			safe_ungetch(SP_PARM, value);
 		    }
 		    value = wch;
 		    break;
@@ -121,8 +106,11 @@
     } else {
 	code = ERR;
     }
-    *result = value;
+
+    if (result != 0)
+	*result = (wint_t) value;
+
     _nc_unlock_global(curses);
-    T(("result %#lo", value));
+    T(("result %#o", value));
     returnCode(code);
 }
diff --git a/ncurses/widechar/lib_get_wstr.c b/ncurses/widechar/lib_get_wstr.c
index baa70a5..27cdae0 100644
--- a/ncurses/widechar/lib_get_wstr.c
+++ b/ncurses/widechar/lib_get_wstr.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2004,2008 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2009,2011 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            *
@@ -38,9 +38,8 @@
 */
 
 #include <curses.priv.h>
-#include <term.h>
 
-MODULE_ID("$Id: lib_get_wstr.c,v 1.10 2008/08/16 19:25:33 tom Exp $")
+MODULE_ID("$Id: lib_get_wstr.c,v 1.13 2011/10/22 16:31:35 tom Exp $")
 
 static int
 wadd_wint(WINDOW *win, wint_t *src)
@@ -50,7 +49,7 @@
 
     wch[0] = (wchar_t) (*src);
     wch[1] = 0;
-    setcchar(&tmp, wch, A_NORMAL, 0, NULL);
+    setcchar(&tmp, wch, A_NORMAL, (short) 0, NULL);
     return wadd_wch(win, &tmp);
 }
 
@@ -59,7 +58,7 @@
  * or other character, and handles reverse wraparound.
  */
 static wint_t *
-WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, bool echoed)
+WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, int echoed)
 {
     if (last > first) {
 	*--last = '\0';
@@ -96,7 +95,7 @@
     wint_t ch;
     int y, x, code;
 
-    T((T_CALLED("wgetn_wstr(%p,%p, %d)"), win, str, maxlen));
+    T((T_CALLED("wgetn_wstr(%p,%p, %d)"), (void *) win, (void *) str, maxlen));
 
     if (!win)
 	returnCode(ERR);
diff --git a/ncurses/widechar/lib_hline_set.c b/ncurses/widechar/lib_hline_set.c
index 43175de..da92b83 100644
--- a/ncurses/widechar/lib_hline_set.c
+++ b/ncurses/widechar/lib_hline_set.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2002-2009,2010 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            *
@@ -39,16 +39,16 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_hline_set.c,v 1.2 2002/03/23 21:35:34 tom Exp $")
+MODULE_ID("$Id: lib_hline_set.c,v 1.4 2010/12/19 01:45:03 tom Exp $")
 
 NCURSES_EXPORT(int)
-whline_set(WINDOW *win, const cchar_t * ch, int n)
+whline_set(WINDOW *win, const cchar_t *ch, int n)
 {
     int code = ERR;
-    NCURSES_SIZE_T start;
-    NCURSES_SIZE_T end;
+    int start;
+    int end;
 
-    T((T_CALLED("whline_set(%p,%s,%d)"), win, _tracecchar_t(ch), n));
+    T((T_CALLED("whline_set(%p,%s,%d)"), (void *) win, _tracecchar_t(ch), n));
 
     if (win) {
 	struct ldat *line = &(win->_line[win->_cury]);
diff --git a/ncurses/widechar/lib_in_wch.c b/ncurses/widechar/lib_in_wch.c
index 5cd92e3..b239650 100644
--- a/ncurses/widechar/lib_in_wch.c
+++ b/ncurses/widechar/lib_in_wch.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2004,2006 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2006,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            *
@@ -39,7 +39,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_in_wch.c,v 1.4 2006/09/03 15:41:22 tom Exp $")
+MODULE_ID("$Id: lib_in_wch.c,v 1.5 2009/10/24 22:37:55 tom Exp $")
 
 NCURSES_EXPORT(int)
 win_wch(WINDOW *win, cchar_t *wcval)
@@ -47,7 +47,7 @@
     int row, col;
     int code = OK;
 
-    TR(TRACE_CCALLS, (T_CALLED("win_wch(%p,%p)"), win, wcval));
+    TR(TRACE_CCALLS, (T_CALLED("win_wch(%p,%p)"), (void *) win, (void *) wcval));
     if (win != 0
 	&& wcval != 0) {
 	getyx(win, row, col);
diff --git a/ncurses/widechar/lib_in_wchnstr.c b/ncurses/widechar/lib_in_wchnstr.c
index e9f0646..57cc2ca 100644
--- a/ncurses/widechar/lib_in_wchnstr.c
+++ b/ncurses/widechar/lib_in_wchnstr.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2004-2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2007,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            *
@@ -39,14 +39,14 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_in_wchnstr.c,v 1.7 2007/02/11 01:00:00 tom Exp $")
+MODULE_ID("$Id: lib_in_wchnstr.c,v 1.8 2009/10/24 22:37:48 tom Exp $")
 
 NCURSES_EXPORT(int)
 win_wchnstr(WINDOW *win, cchar_t *wchstr, int n)
 {
     int code = OK;
 
-    T((T_CALLED("win_wchnstr(%p,%p,%d)"), win, wchstr, n));
+    T((T_CALLED("win_wchnstr(%p,%p,%d)"), (void *) win, (void *) wchstr, n));
     if (win != 0
 	&& wchstr != 0) {
 	NCURSES_CH_T *src;
diff --git a/ncurses/widechar/lib_ins_wch.c b/ncurses/widechar/lib_ins_wch.c
index c3d0420..2aa71a5 100644
--- a/ncurses/widechar/lib_ins_wch.c
+++ b/ncurses/widechar/lib_ins_wch.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2003,2005 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2010,2011 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            *
@@ -39,39 +39,44 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_ins_wch.c,v 1.8 2005/12/03 20:24:19 tom Exp $")
+MODULE_ID("$Id: lib_ins_wch.c,v 1.17 2011/10/22 16:34:50 tom Exp $")
 
 /*
  * Insert the given character, updating the current location to simplify
  * inserting a string.
  */
-static int
+NCURSES_EXPORT(int)
 _nc_insert_wch(WINDOW *win, const cchar_t *wch)
 {
     int cells = wcwidth(CharOf(CHDEREF(wch)));
     int cell;
+    int code = OK;
 
-    if (cells <= 0)
-	cells = 1;
+    if (cells < 0) {
+	code = winsch(win, (chtype) CharOf(CHDEREF(wch)));
+    } else {
+	if (cells == 0)
+	    cells = 1;
 
-    if (win->_curx <= win->_maxx) {
-	struct ldat *line = &(win->_line[win->_cury]);
-	NCURSES_CH_T *end = &(line->text[win->_curx]);
-	NCURSES_CH_T *temp1 = &(line->text[win->_maxx]);
-	NCURSES_CH_T *temp2 = temp1 - cells;
+	if (win->_curx <= win->_maxx) {
+	    struct ldat *line = &(win->_line[win->_cury]);
+	    NCURSES_CH_T *end = &(line->text[win->_curx]);
+	    NCURSES_CH_T *temp1 = &(line->text[win->_maxx]);
+	    NCURSES_CH_T *temp2 = temp1 - cells;
 
-	CHANGED_TO_EOL(line, win->_curx, win->_maxx);
-	while (temp1 > end)
-	    *temp1-- = *temp2--;
+	    CHANGED_TO_EOL(line, win->_curx, win->_maxx);
+	    while (temp1 > end)
+		*temp1-- = *temp2--;
 
-	*temp1 = _nc_render(win, *wch);
-	for (cell = 1; cell < cells; ++cell) {
-	    SetWidecExt(temp1[cell], cell);
+	    *temp1 = _nc_render(win, *wch);
+	    for (cell = 1; cell < cells; ++cell) {
+		SetWidecExt(temp1[cell], cell);
+	    }
+
+	    win->_curx++;
 	}
-
-	win->_curx++;
     }
-    return OK;
+    return code;
 }
 
 NCURSES_EXPORT(int)
@@ -81,7 +86,7 @@
     NCURSES_SIZE_T ox;
     int code = ERR;
 
-    T((T_CALLED("wins_wch(%p, %s)"), win, _tracecchar_t(wch)));
+    T((T_CALLED("wins_wch(%p, %s)"), (void *) win, _tracecchar_t(wch)));
 
     if (win != 0) {
 	oy = win->_cury;
@@ -104,32 +109,35 @@
     NCURSES_SIZE_T ox;
     const wchar_t *cp;
 
-    T((T_CALLED("wins_nwstr(%p,%s,%d)"), win, _nc_viswbufn(wstr, n), n));
+    T((T_CALLED("wins_nwstr(%p,%s,%d)"),
+       (void *) win, _nc_viswbufn(wstr, n), n));
 
     if (win != 0
 	&& wstr != 0) {
 	if (n < 1)
-	    n = wcslen(wstr);
+	    n = (int) wcslen(wstr);
 	code = OK;
 	if (n > 0) {
+	    SCREEN *sp = _nc_screen_of(win);
+
 	    oy = win->_cury;
 	    ox = win->_curx;
 	    for (cp = wstr; *cp && ((cp - wstr) < n); cp++) {
 		int len = wcwidth(*cp);
 
-		if (len != 1 || !is8bits(*cp)) {
+		if ((len >= 0 && len != 1) || !is7bits(*cp)) {
 		    cchar_t tmp_cchar;
 		    wchar_t tmp_wchar = *cp;
 		    memset(&tmp_cchar, 0, sizeof(tmp_cchar));
 		    (void) setcchar(&tmp_cchar,
 				    &tmp_wchar,
 				    WA_NORMAL,
-				    0,
+				    (short) 0,
 				    (void *) 0);
 		    code = _nc_insert_wch(win, &tmp_cchar);
 		} else {
 		    /* tabs, other ASCII stuff */
-		    code = _nc_insert_ch(win, (chtype) (*cp));
+		    code = _nc_insert_ch(sp, win, (chtype) (*cp));
 		}
 		if (code != OK)
 		    break;
diff --git a/ncurses/widechar/lib_inwstr.c b/ncurses/widechar/lib_inwstr.c
index 2207a5f..a4f5b8e 100644
--- a/ncurses/widechar/lib_inwstr.c
+++ b/ncurses/widechar/lib_inwstr.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002,2004 Free Software Foundation, Inc.                   *
+ * Copyright (c) 2002-2009,2011 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            *
@@ -39,7 +39,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_inwstr.c,v 1.4 2004/10/23 20:41:28 tom Exp $")
+MODULE_ID("$Id: lib_inwstr.c,v 1.6 2011/05/28 22:49:49 tom Exp $")
 
 NCURSES_EXPORT(int)
 winnwstr(WINDOW *win, wchar_t *wstr, int n)
@@ -50,7 +50,7 @@
     cchar_t *text;
     wchar_t wch;
 
-    T((T_CALLED("winnwstr(%p,%p,%d)"), win, wstr, n));
+    T((T_CALLED("winnwstr(%p,%p,%d)"), (void *) win, (void *) wstr, n));
     if (wstr != 0) {
 	if (win) {
 	    getyx(win, row, col);
@@ -93,8 +93,13 @@
 winwstr(WINDOW *win, wchar_t *wstr)
 {
     int result = OK;
-    T((T_CALLED("winwstr(%p,%p)"), win, wstr));
-    if (winnwstr(win, wstr, CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR)
+
+    T((T_CALLED("winwstr(%p,%p)"), (void *) win, (void *) wstr));
+    if (win == 0) {
 	result = ERR;
+    } else if (winnwstr(win, wstr,
+			CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR) {
+	result = ERR;
+    }
     returnCode(result);
 }
diff --git a/ncurses/widechar/lib_pecho_wchar.c b/ncurses/widechar/lib_pecho_wchar.c
index e612775..b6da7a8 100644
--- a/ncurses/widechar/lib_pecho_wchar.c
+++ b/ncurses/widechar/lib_pecho_wchar.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2004 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2004,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            *
@@ -32,12 +32,12 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_pecho_wchar.c,v 1.1 2004/01/03 21:42:01 tom Exp $")
+MODULE_ID("$Id: lib_pecho_wchar.c,v 1.2 2009/10/24 22:43:32 tom Exp $")
 
 NCURSES_EXPORT(int)
-pecho_wchar(WINDOW *pad, const cchar_t * wch)
+pecho_wchar(WINDOW *pad, const cchar_t *wch)
 {
-    T((T_CALLED("pecho_wchar(%p, %s)"), pad, _tracech_t(wch)));
+    T((T_CALLED("pecho_wchar(%p, %s)"), (void *) pad, _tracech_t(wch)));
 
     if (pad == 0)
 	returnCode(ERR);
diff --git a/ncurses/widechar/lib_slk_wset.c b/ncurses/widechar/lib_slk_wset.c
index 646b5d9..8927556 100644
--- a/ncurses/widechar/lib_slk_wset.c
+++ b/ncurses/widechar/lib_slk_wset.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2003-2004,2005 Free Software Foundation, Inc.              *
+ * Copyright (c) 2003-2002,2011 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            *
@@ -40,7 +40,7 @@
 #include <wctype.h>
 #endif
 
-MODULE_ID("$Id: lib_slk_wset.c,v 1.11 2005/01/16 01:03:53 tom Exp $")
+MODULE_ID("$Id: lib_slk_wset.c,v 1.13 2011/10/22 15:52:20 tom Exp $")
 
 NCURSES_EXPORT(int)
 slk_wset(int i, const wchar_t *astr, int format)
@@ -53,19 +53,21 @@
 
     T((T_CALLED("slk_wset(%d, %s, %d)"), i, _nc_viswbuf(astr), format));
 
-    init_mb(state);
-    str = astr;
-    if ((arglen = wcsrtombs(NULL, &str, 0, &state)) != (size_t) -1) {
-	if ((mystr = (char *) _nc_doalloc(0, arglen + 1)) != 0) {
-	    str = astr;
-	    if (wcsrtombs(mystr, &str, arglen, &state) != (size_t) -1) {
-		/* glibc documentation claims that the terminating L'\0'
-		 * is written, but it is not...
-		 */
-		mystr[arglen] = 0;
-		result = slk_set(i, mystr, format);
+    if (astr != 0) {
+	init_mb(state);
+	str = astr;
+	if ((arglen = wcsrtombs(NULL, &str, (size_t) 0, &state)) != (size_t) -1) {
+	    if ((mystr = (char *) _nc_doalloc(0, arglen + 1)) != 0) {
+		str = astr;
+		if (wcsrtombs(mystr, &str, arglen, &state) != (size_t) -1) {
+		    /* glibc documentation claims that the terminating L'\0'
+		     * is written, but it is not...
+		     */
+		    mystr[arglen] = 0;
+		    result = slk_set(i, mystr, format);
+		}
+		free(mystr);
 	    }
-	    free(mystr);
 	}
     }
     returnCode(result);
diff --git a/ncurses/widechar/lib_unget_wch.c b/ncurses/widechar/lib_unget_wch.c
index bb2c4a0..d5ae608 100644
--- a/ncurses/widechar/lib_unget_wch.c
+++ b/ncurses/widechar/lib_unget_wch.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2007,2008 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2010,2011 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            *
@@ -39,7 +39,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_unget_wch.c,v 1.10 2008/06/07 14:50:37 tom Exp $")
+MODULE_ID("$Id: lib_unget_wch.c,v 1.15 2011/10/22 16:34:50 tom Exp $")
 
 /*
  * Wrapper for wcrtomb() which obtains the length needed for the given
@@ -55,24 +55,24 @@
 	const wchar_t *tempp = temp;
 	temp[0] = source;
 	temp[1] = 0;
-	result = wcsrtombs(NULL, &tempp, 0, state);
+	result = (int) wcsrtombs(NULL, &tempp, (size_t) 0, state);
     } else {
-	result = wcrtomb(target, source, state);
+	result = (int) wcrtomb(target, source, state);
     }
     if (!isEILSEQ(result) && (result == 0))
 	result = 1;
-    return result;
+    return (size_t) result;
 }
 
 NCURSES_EXPORT(int)
-unget_wch(const wchar_t wch)
+NCURSES_SP_NAME(unget_wch) (NCURSES_SP_DCLx const wchar_t wch)
 {
     int result = OK;
     mbstate_t state;
     size_t length;
     int n;
 
-    T((T_CALLED("unget_wch(%#lx)"), (unsigned long) wch));
+    T((T_CALLED("unget_wch(%p, %#lx)"), (void *) SP_PARM, (unsigned long) wch));
 
     init_mb(state);
     length = _nc_wcrtomb(0, wch, &state);
@@ -83,10 +83,12 @@
 
 	if ((string = (char *) malloc(length)) != 0) {
 	    init_mb(state);
-	    wcrtomb(string, wch, &state);
+	    /* ignore the result, since we already validated the character */
+	    IGNORE_RC((int) wcrtomb(string, wch, &state));
 
 	    for (n = (int) (length - 1); n >= 0; --n) {
-		if (_nc_ungetch(SP, string[n]) != OK) {
+		if (NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx
+					      UChar(string[n])) !=OK) {
 		    result = ERR;
 		    break;
 		}
@@ -101,3 +103,11 @@
 
     returnCode(result);
 }
+
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(int)
+unget_wch(const wchar_t wch)
+{
+    return NCURSES_SP_NAME(unget_wch) (CURRENT_SCREEN, wch);
+}
+#endif
diff --git a/ncurses/widechar/lib_vid_attr.c b/ncurses/widechar/lib_vid_attr.c
index 1dc679e..80c1ea4 100644
--- a/ncurses/widechar/lib_vid_attr.c
+++ b/ncurses/widechar/lib_vid_attr.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002-2006,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 2002-2013,2014 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,16 +31,21 @@
  ****************************************************************************/
 
 #include <curses.priv.h>
-#include <term.h>
 
-MODULE_ID("$Id: lib_vid_attr.c,v 1.5 2007/06/30 22:03:02 tom Exp $")
+#ifndef CUR
+#define CUR SP_TERMTYPE
+#endif
 
-#define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
+MODULE_ID("$Id: lib_vid_attr.c,v 1.23 2014/06/07 22:13:46 tom Exp $")
 
-#define TurnOn(mask,mode) \
+#define doPut(mode) \
+	TPUTS_TRACE(#mode); \
+	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
+
+#define TurnOn(mask, mode) \
 	if ((turn_on & mask) && mode) { doPut(mode); }
 
-#define TurnOff(mask,mode) \
+#define TurnOff(mask, mode) \
 	if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
 
 	/* if there is no current screen, assume we *can* do color */
@@ -50,14 +55,22 @@
 		if ((pair != old_pair) \
 		 || (fix_pair0 && (pair == 0)) \
 		 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
-			_nc_do_color(old_pair, pair, reverse, outc); \
+		    NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
+						   old_pair, pair, \
+						   reverse, outc); \
 		} \
 	}
 
-#define set_color(mode, pair) mode &= ALL_BUT_COLOR; mode |= COLOR_PAIR(pair)
+#define set_color(mode, pair) \
+	mode &= ALL_BUT_COLOR; \
+	mode |= (attr_t) ColorPair(pair)
 
 NCURSES_EXPORT(int)
-vid_puts(attr_t newmode, short pair, void *opts GCC_UNUSED, int (*outc) (int))
+NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx
+			   attr_t newmode,
+			   NCURSES_PAIRS_T pair,
+			   void *opts GCC_UNUSED,
+			   NCURSES_SP_OUTC outc)
 {
 #if NCURSES_EXT_COLORS
     static attr_t previous_attr = A_NORMAL;
@@ -65,9 +78,9 @@
 
     attr_t turn_on, turn_off;
     bool reverse = FALSE;
-    bool can_color = (SP == 0 || SP->_coloron);
+    bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
 #if NCURSES_EXT_FUNCS
-    bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
+    bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
 #else
 #define fix_pair0 FALSE
 #endif
@@ -76,18 +89,18 @@
     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
 
     /* this allows us to go on whether or not newterm() has been called */
-    if (SP) {
-	previous_attr = AttrOf(SCREEN_ATTRS(SP));
-	previous_pair = GetPair(SCREEN_ATTRS(SP));
+    if (SP_PARM) {
+	previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
+	previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
     }
 
     TR(TRACE_ATTRS, ("previous attribute was %s, %d",
 		     _traceattr(previous_attr), previous_pair));
 
 #if !USE_XMC_SUPPORT
-    if ((SP != 0)
+    if ((SP_PARM != 0)
 	&& (magic_cookie_glitch > 0))
-	newmode &= ~(SP->_xmc_suppress);
+	newmode &= ~(SP_PARM->_xmc_suppress);
 #endif
 
     /*
@@ -109,7 +122,7 @@
 	 * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
 	 * macro so this will work properly for the wide-character layout.
 	 */
-	unsigned value = no_color_video;
+	unsigned value = (unsigned) no_color_video;
 	attr_t mask = NCURSES_BITS((value & 63)
 				   | ((value & 192) << 1)
 				   | ((value & 256) >> 2), 8);
@@ -131,7 +144,7 @@
     }
 
     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
-    turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
+    turn_on = (newmode & ~(previous_attr & TPARM_ATTR)) & ALL_BUT_COLOR;
 
     SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair);
 
@@ -144,12 +157,17 @@
 	    if (exit_attribute_mode) {
 		doPut(exit_attribute_mode);
 	    } else {
-		if (!SP || SP->_use_rmul) {
+		if (!SP_PARM || SP_PARM->_use_rmul) {
 		    TurnOff(A_UNDERLINE, exit_underline_mode);
 		}
-		if (!SP || SP->_use_rmso) {
+		if (!SP_PARM || SP_PARM->_use_rmso) {
 		    TurnOff(A_STANDOUT, exit_standout_mode);
 		}
+#if USE_ITALIC
+		if (!SP_PARM || SP_PARM->_use_ritm) {
+		    TurnOff(A_ITALIC, exit_italics_mode);
+		}
+#endif
 	    }
 	    previous_attr &= ALL_BUT_COLOR;
 	    previous_pair = 0;
@@ -159,19 +177,30 @@
     } else if (set_attributes) {
 	if (turn_on || turn_off) {
 	    TPUTS_TRACE("set_attributes");
-	    tputs(TPARM_9(set_attributes,
-			  (newmode & A_STANDOUT) != 0,
-			  (newmode & A_UNDERLINE) != 0,
-			  (newmode & A_REVERSE) != 0,
-			  (newmode & A_BLINK) != 0,
-			  (newmode & A_DIM) != 0,
-			  (newmode & A_BOLD) != 0,
-			  (newmode & A_INVIS) != 0,
-			  (newmode & A_PROTECT) != 0,
-			  (newmode & A_ALTCHARSET) != 0), 1, outc);
+	    NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
+				    TPARM_9(set_attributes,
+					    (newmode & A_STANDOUT) != 0,
+					    (newmode & A_UNDERLINE) != 0,
+					    (newmode & A_REVERSE) != 0,
+					    (newmode & A_BLINK) != 0,
+					    (newmode & A_DIM) != 0,
+					    (newmode & A_BOLD) != 0,
+					    (newmode & A_INVIS) != 0,
+					    (newmode & A_PROTECT) != 0,
+					    (newmode & A_ALTCHARSET) != 0),
+				    1, outc);
 	    previous_attr &= ALL_BUT_COLOR;
 	    previous_pair = 0;
 	}
+#if USE_ITALIC
+	if (!SP_PARM || SP_PARM->_use_ritm) {
+	    if (turn_on & A_ITALIC) {
+		TurnOn(A_ITALIC, enter_italics_mode);
+	    } else if (turn_off & A_ITALIC) {
+		TurnOff(A_ITALIC, exit_italics_mode);
+	    }
+	}
+#endif
 	SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
     } else {
 
@@ -179,14 +208,18 @@
 
 	TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
 
-	if (!SP || SP->_use_rmul) {
+	if (!SP_PARM || SP_PARM->_use_rmul) {
 	    TurnOff(A_UNDERLINE, exit_underline_mode);
 	}
 
-	if (!SP || SP->_use_rmso) {
+	if (!SP_PARM || SP_PARM->_use_rmso) {
 	    TurnOff(A_STANDOUT, exit_standout_mode);
 	}
-
+#if USE_ITALIC
+	if (!SP_PARM || SP_PARM->_use_ritm) {
+	    TurnOff(A_ITALIC, exit_italics_mode);
+	}
+#endif
 	if (turn_off && exit_attribute_mode) {
 	    doPut(exit_attribute_mode);
 	    turn_on |= (newmode & ALL_BUT_COLOR);
@@ -206,6 +239,9 @@
 	TurnOn(A_PROTECT,	enter_protected_mode);
 	TurnOn(A_INVIS,		enter_secure_mode);
 	TurnOn(A_UNDERLINE,	enter_underline_mode);
+#if USE_ITALIC
+	TurnOn(A_ITALIC,	enter_italics_mode);
+#endif
 #if USE_WIDEC_SUPPORT
 	TurnOn(A_HORIZONTAL,	enter_horizontal_hl_mode);
 	TurnOn(A_LEFT,		enter_left_hl_mode);
@@ -221,9 +257,9 @@
     if (reverse)
 	newmode |= A_REVERSE;
 
-    if (SP) {
-	SetAttr(SCREEN_ATTRS(SP), newmode);
-	SetPair(SCREEN_ATTRS(SP), pair);
+    if (SP_PARM) {
+	SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
+	SetPair(SCREEN_ATTRS(SP_PARM), pair);
     } else {
 	previous_attr = newmode;
 	previous_pair = pair;
@@ -231,45 +267,86 @@
 
     returnCode(OK);
 #else
-    T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
+    T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), (int) pair));
     set_color(newmode, pair);
-    returnCode(vidputs(newmode, outc));
+    returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc));
 #endif
 }
 
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(int)
+vid_puts(attr_t newmode,
+	 NCURSES_PAIRS_T pair,
+	 void *opts GCC_UNUSED,
+	 NCURSES_OUTC outc)
+{
+    SetSafeOutcWrapper(outc);
+    return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN,
+				      newmode,
+				      pair,
+				      opts,
+				      _nc_outc_wrapper);
+}
+#endif
+
 #undef vid_attr
 NCURSES_EXPORT(int)
-vid_attr(attr_t newmode, short pair, void *opts)
+NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
+			   attr_t newmode,
+			   NCURSES_PAIRS_T pair,
+			   void *opts)
 {
-    T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair));
-    returnCode(vid_puts(newmode, pair, opts, _nc_outch));
+    T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), (int) pair));
+    returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx
+					  newmode,
+					  pair,
+					  opts,
+					  NCURSES_SP_NAME(_nc_putchar)));
 }
 
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(int)
+vid_attr(attr_t newmode, NCURSES_PAIRS_T pair, void *opts)
+{
+    return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair, opts);
+}
+#endif
+
 /*
  * This implementation uses the same mask values for A_xxx and WA_xxx, so
  * we can use termattrs() for part of the logic.
  */
 NCURSES_EXPORT(attr_t)
-term_attrs(void)
+NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0)
 {
-    attr_t attrs;
+    attr_t attrs = 0;
 
     T((T_CALLED("term_attrs()")));
-    attrs = termattrs();
+    if (SP_PARM) {
+	attrs = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
 
-    /* these are only supported for wide-character mode */
-    if (enter_horizontal_hl_mode)
-	attrs |= WA_HORIZONTAL;
-    if (enter_left_hl_mode)
-	attrs |= WA_LEFT;
-    if (enter_low_hl_mode)
-	attrs |= WA_LOW;
-    if (enter_right_hl_mode)
-	attrs |= WA_RIGHT;
-    if (enter_top_hl_mode)
-	attrs |= WA_TOP;
-    if (enter_vertical_hl_mode)
-	attrs |= WA_VERTICAL;
+	/* these are only supported for wide-character mode */
+	if (enter_horizontal_hl_mode)
+	    attrs |= WA_HORIZONTAL;
+	if (enter_left_hl_mode)
+	    attrs |= WA_LEFT;
+	if (enter_low_hl_mode)
+	    attrs |= WA_LOW;
+	if (enter_right_hl_mode)
+	    attrs |= WA_RIGHT;
+	if (enter_top_hl_mode)
+	    attrs |= WA_TOP;
+	if (enter_vertical_hl_mode)
+	    attrs |= WA_VERTICAL;
+    }
 
     returnAttr(attrs);
 }
+
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(attr_t)
+term_attrs(void)
+{
+    return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN);
+}
+#endif
diff --git a/ncurses/widechar/lib_vline_set.c b/ncurses/widechar/lib_vline_set.c
index af42df1..917caac 100644
--- a/ncurses/widechar/lib_vline_set.c
+++ b/ncurses/widechar/lib_vline_set.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2002-2009,2010 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            *
@@ -39,16 +39,16 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_vline_set.c,v 1.2 2002/03/23 21:36:01 tom Exp $")
+MODULE_ID("$Id: lib_vline_set.c,v 1.4 2010/12/19 01:50:50 tom Exp $")
 
 NCURSES_EXPORT(int)
-wvline_set(WINDOW *win, const cchar_t * ch, int n)
+wvline_set(WINDOW *win, const cchar_t *ch, int n)
 {
     int code = ERR;
-    NCURSES_SIZE_T row, col;
-    NCURSES_SIZE_T end;
+    int row, col;
+    int end;
 
-    T((T_CALLED("wvline(%p,%s,%d)"), win, _tracecchar_t(ch), n));
+    T((T_CALLED("wvline(%p,%s,%d)"), (void *) win, _tracecchar_t(ch), n));
 
     if (win) {
 	NCURSES_CH_T wch;
diff --git a/ncurses/widechar/lib_wacs.c b/ncurses/widechar/lib_wacs.c
index fe893b4..dc3af21 100644
--- a/ncurses/widechar/lib_wacs.c
+++ b/ncurses/widechar/lib_wacs.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002,2006 Free Software Foundation, Inc.                   *
+ * Copyright (c) 2002-2013,2014 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,9 +31,8 @@
  ****************************************************************************/
 
 #include <curses.priv.h>
-#include <term.h>
 
-MODULE_ID("$Id: lib_wacs.c,v 1.7 2006/12/17 15:16:17 tom Exp $")
+MODULE_ID("$Id: lib_wacs.c,v 1.14 2014/02/23 01:21:08 tom Exp $")
 
 NCURSES_EXPORT_VAR(cchar_t) * _nc_wacs = 0;
 
@@ -42,7 +41,7 @@
 {
     /* *INDENT-OFF* */
     static const struct {
-	int	map;
+	unsigned map;
 	int	value[2];
     } table[] = {
 	/* VT100 symbols */
@@ -80,6 +79,30 @@
 	{ '{',	{ '*',	0x03c0 }},	/* greek pi */
 	{ '|',	{ '!',	0x2260 }},	/* not-equal */
 	{ '}',	{ 'f',	0x00a3 }},	/* pound-sterling symbol */
+	/* thick-line-drawing */
+	{ 'L',	{ '+',	0x250f }},	/* upper left corner */
+	{ 'M',	{ '+',	0x2517 }},	/* lower left corner */
+	{ 'K',	{ '+',	0x2513 }},	/* upper right corner */
+	{ 'J',	{ '+',	0x251b }},	/* lower right corner */
+	{ 'T',	{ '+',	0x2523 }},	/* tee pointing left */
+	{ 'U',	{ '+',	0x252b }},	/* tee pointing right */
+	{ 'V',	{ '+',	0x253b }},	/* tee pointing up */
+	{ 'W',	{ '+',	0x2533 }},	/* tee pointing down */
+	{ 'Q',	{ '-',	0x2501 }},	/* horizontal line */
+	{ 'X',	{ '|',	0x2503 }},	/* vertical line */
+	{ 'N',	{ '+',	0x254b }},	/* large plus or crossover */
+	/* double-line-drawing */
+	{ 'C',	{ '+',	0x2554 }},	/* upper left corner */
+	{ 'D',	{ '+',	0x255a }},	/* lower left corner */
+	{ 'B',	{ '+',	0x2557 }},	/* upper right corner */
+	{ 'A',	{ '+',	0x255d }},	/* lower right corner */
+	{ 'G',	{ '+',	0x2563 }},	/* tee pointing left */
+	{ 'F',	{ '+',	0x2560 }},	/* tee pointing right */
+	{ 'H',	{ '+',	0x2569 }},	/* tee pointing up */
+	{ 'I',	{ '+',	0x2566 }},	/* tee pointing down */
+	{ 'R',	{ '-',	0x2550 }},	/* horizontal line */
+	{ 'Y',	{ '|',	0x2551 }},	/* vertical line */
+	{ 'E',	{ '+',	0x256c }},	/* large plus or crossover */
     };
     /* *INDENT-ON* */
 
@@ -96,22 +119,24 @@
     T(("initializing WIDE-ACS map (Unicode is%s active)",
        active ? "" : " not"));
 
-    _nc_wacs = typeCalloc(cchar_t, ACS_LEN);
-    for (n = 0; n < SIZEOF(table); ++n) {
-	int wide = wcwidth(table[n].value[active]);
+    if ((_nc_wacs = typeCalloc(cchar_t, ACS_LEN)) != 0) {
 
-	m = table[n].map;
-	if (active && (wide == 1)) {
-	    SetChar(_nc_wacs[m], table[n].value[active], A_NORMAL);
-	} else if (acs_map[m] & A_ALTCHARSET) {
-	    SetChar(_nc_wacs[m], m, A_ALTCHARSET);
-	} else {
-	    SetChar(_nc_wacs[m], table[n].value[0], A_NORMAL);
+	for (n = 0; n < SIZEOF(table); ++n) {
+	    int wide = wcwidth((wchar_t) table[n].value[active]);
+
+	    m = table[n].map;
+	    if (active && (wide == 1)) {
+		SetChar(_nc_wacs[m], table[n].value[1], A_NORMAL);
+	    } else if (acs_map[m] & A_ALTCHARSET) {
+		SetChar(_nc_wacs[m], m, A_ALTCHARSET);
+	    } else {
+		SetChar(_nc_wacs[m], table[n].value[0], A_NORMAL);
+	    }
+
+	    T(("#%d, SetChar(%c, %#04x) = %s",
+	       n, m,
+	       table[n].value[active],
+	       _tracecchar_t(&_nc_wacs[m])));
 	}
-
-	T(("#%d, SetChar(%c, %#04x) = %s",
-	   n, m,
-	   table[n].value[active],
-	   _tracecchar_t(&_nc_wacs[m])));
     }
 }
diff --git a/ncurses/widechar/lib_wunctrl.c b/ncurses/widechar/lib_wunctrl.c
index be2259a..45d4952 100644
--- a/ncurses/widechar/lib_wunctrl.c
+++ b/ncurses/widechar/lib_wunctrl.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2001-2005,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 2001-2011,2012 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            *
@@ -35,21 +35,36 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_wunctrl.c,v 1.12 2007/06/12 20:22:32 tom Exp $")
+MODULE_ID("$Id: lib_wunctrl.c,v 1.16 2012/12/15 20:53:42 tom Exp $")
 
 NCURSES_EXPORT(wchar_t *)
+NCURSES_SP_NAME(wunctrl) (NCURSES_SP_DCLx cchar_t *wc)
+{
+    static wchar_t str[CCHARW_MAX + 1], *wsp;
+    wchar_t *result;
+
+    if (wc == 0) {
+	result = 0;
+    } else if (SP_PARM != 0 && Charable(*wc)) {
+	const char *p =
+	NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx
+				 (unsigned) _nc_to_char((wint_t)CharOf(*wc)));
+
+	for (wsp = str; *p; ++p) {
+	    *wsp++ = (wchar_t) _nc_to_widechar(*p);
+	}
+	*wsp = 0;
+	result = str;
+    } else {
+	result = wc->chars;
+    }
+    return result;
+}
+
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(wchar_t *)
 wunctrl(cchar_t *wc)
 {
-    static wchar_t str[CCHARW_MAX + 1], *sp;
-
-    if (Charable(*wc)) {
-	const char *p = unctrl((unsigned) _nc_to_char((wint_t) CharOf(*wc)));
-
-	for (sp = str; *p; ++p) {
-	    *sp++ = _nc_to_widechar(*p);
-	}
-	*sp = 0;
-	return str;
-    } else
-	return wc->chars;
+    return NCURSES_SP_NAME(wunctrl) (CURRENT_SCREEN, wc);
 }
+#endif
diff --git a/ncurses/widechar/widechars.c b/ncurses/widechar/widechars.c
new file mode 100644
index 0000000..6951e28
--- /dev/null
+++ b/ncurses/widechar/widechars.c
@@ -0,0 +1,152 @@
+/****************************************************************************
+ * Copyright (c) 2012,2013 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.                                                           *
+ ****************************************************************************/
+
+#include <curses.priv.h>
+
+#if USE_WIDEC_SUPPORT
+
+MODULE_ID("$Id: widechars.c,v 1.5 2013/03/02 18:55:51 tom Exp $")
+
+#if defined(__MINGW32__)
+/*
+ * MinGW has wide-character functions, but they do not work correctly.
+ */
+
+int
+_nc_mbtowc(wchar_t *pwc, const char *s, size_t n)
+{
+    int result;
+    int count;
+    int try;
+
+    if (s != 0 && n != 0) {
+	/*
+	 * MultiByteToWideChar() can decide to return more than one
+	 * wide-character.  We want only one.  Ignore any trailing null, both
+	 * in the initial count and in the conversion.
+	 */
+	count = 0;
+	for (try = 1; try <= (int) n; ++try) {
+	    count = MultiByteToWideChar(CP_UTF8,
+					MB_ERR_INVALID_CHARS,
+					s,
+					try,
+					pwc,
+					0);
+	    TR(TRACE_BITS, ("...try %d:%d", try, count));
+	    if (count > 0) {
+		break;
+	    }
+	}
+	if (count < 1 || count > 2) {
+	    result = -1;
+	} else {
+	    wchar_t actual[2];
+	    memset(&actual, 0, sizeof(actual));
+	    count = MultiByteToWideChar(CP_UTF8,
+					MB_ERR_INVALID_CHARS,
+					s,
+					try,
+					actual,
+					2);
+	    TR(TRACE_BITS, ("\twin32 ->%#x, %#x", actual[0], actual[1]));
+	    *pwc = actual[0];
+	    if (actual[1] != 0)
+		result = -1;
+	    else
+		result = try;
+	}
+    } else {
+	result = 0;
+    }
+
+    return result;
+}
+
+int
+_nc_mblen(const char *s, size_t n)
+{
+    int result = -1;
+    int count;
+    wchar_t temp;
+
+    if (s != 0 && n != 0) {
+	count = _nc_mbtowc(&temp, s, n);
+	if (count == 1) {
+	    int check = WideCharToMultiByte(CP_UTF8,
+					    0,
+					    &temp,
+					    1,
+					    NULL,
+					    0,	/* compute length only */
+					    NULL,
+					    NULL);
+	    TR(TRACE_BITS, ("\tcheck ->%d\n", check));
+	    if (check > 0 && (size_t) check <= n) {
+		result = check;
+	    }
+	}
+    } else {
+	result = 0;
+    }
+
+    return result;
+}
+
+int __MINGW_NOTHROW
+_nc_wctomb(char *s, wchar_t wc)
+{
+    int result;
+    int check;
+
+    check = WideCharToMultiByte(CP_UTF8,
+				0,
+				&wc,
+				1,
+				NULL,
+				0,	/* compute length only */
+				NULL,
+				NULL);
+    if (check > 0) {
+	result = WideCharToMultiByte(CP_UTF8,
+				     0,
+				     &wc,
+				     1,
+				     s,
+				     check + 1,
+				     NULL,
+				     NULL);
+    } else {
+	result = -1;
+    }
+    return result;
+}
+
+#endif /* __MINGW32__ */
+
+#endif /* USE_WIDEC_SUPPORT */