Update to ncurses-6.0

Change-Id: I98ab2ea8a5e13cca9f8b7cf6277b9b14a4da4299
diff --git a/ncurses/tinfo/lib_options.c b/ncurses/tinfo/lib_options.c
index f3b1485..b736d5f 100644
--- a/ncurses/tinfo/lib_options.c
+++ b/ncurses/tinfo/lib_options.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2006,2008 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-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            *
@@ -30,6 +30,7 @@
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  *     and: Thomas E. Dickey                        1996-on                 *
+ *     and: Juergen Pfeifer                         2009                    *
  ****************************************************************************/
 
 /*
@@ -41,53 +42,71 @@
 
 #include <curses.priv.h>
 
-#include <term.h>
+#ifndef CUR
+#define CUR SP_TERMTYPE
+#endif
 
-MODULE_ID("$Id: lib_options.c,v 1.58 2008/08/16 21:20:48 Werner.Fink Exp $")
-
-static int _nc_curs_set(SCREEN *, int);
-static int _nc_meta(SCREEN *, bool);
+MODULE_ID("$Id: lib_options.c,v 1.78 2014/09/27 21:55:24 tom Exp $")
 
 NCURSES_EXPORT(int)
 idlok(WINDOW *win, bool flag)
 {
-    T((T_CALLED("idlok(%p,%d)"), win, flag));
+    int res = ERR;
+    T((T_CALLED("idlok(%p,%d)"), (void *) win, flag));
 
     if (win) {
-	_nc_idlok = win->_idlok = (flag && (has_il() || change_scroll_region));
-	returnCode(OK);
-    } else
-	returnCode(ERR);
+	SCREEN *sp = _nc_screen_of(win);
+	if (sp != 0
+#ifdef USE_TERM_DRIVER
+	    && IsTermInfo(sp)
+#endif
+	    ) {
+	    sp->_nc_sp_idlok =
+		win->_idlok = (flag && (NCURSES_SP_NAME(has_il) (NCURSES_SP_ARG)
+					|| change_scroll_region));
+	    res = OK;
+	}
+    }
+    returnCode(res);
 }
 
 NCURSES_EXPORT(void)
 idcok(WINDOW *win, bool flag)
 {
-    T((T_CALLED("idcok(%p,%d)"), win, flag));
+    T((T_CALLED("idcok(%p,%d)"), (void *) win, flag));
 
-    if (win)
-	_nc_idcok = win->_idcok = (flag && has_ic());
-
+    if (win) {
+	SCREEN *sp = _nc_screen_of(win);
+	sp->_nc_sp_idcok = win->_idcok = (flag && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG));
+    }
     returnVoid;
 }
 
 NCURSES_EXPORT(int)
-halfdelay(int t)
+NCURSES_SP_NAME(halfdelay) (NCURSES_SP_DCLx int t)
 {
-    T((T_CALLED("halfdelay(%d)"), t));
+    T((T_CALLED("halfdelay(%p,%d)"), (void *) SP_PARM, t));
 
-    if (t < 1 || t > 255 || SP == 0)
+    if (t < 1 || t > 255 || !IsValidTIScreen(SP_PARM))
 	returnCode(ERR);
 
-    cbreak();
-    SP->_cbreak = t + 1;
+    NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
+    SP_PARM->_cbreak = t + 1;
     returnCode(OK);
 }
 
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(int)
+halfdelay(int t)
+{
+    return NCURSES_SP_NAME(halfdelay) (CURRENT_SCREEN, t);
+}
+#endif
+
 NCURSES_EXPORT(int)
 nodelay(WINDOW *win, bool flag)
 {
-    T((T_CALLED("nodelay(%p,%d)"), win, flag));
+    T((T_CALLED("nodelay(%p,%d)"), (void *) win, flag));
 
     if (win) {
 	if (flag == TRUE)
@@ -102,7 +121,7 @@
 NCURSES_EXPORT(int)
 notimeout(WINDOW *win, bool f)
 {
-    T((T_CALLED("notimeout(%p,%d)"), win, f));
+    T((T_CALLED("notimeout(%p,%d)"), (void *) win, f));
 
     if (win) {
 	win->_notimeout = f;
@@ -114,7 +133,7 @@
 NCURSES_EXPORT(void)
 wtimeout(WINDOW *win, int delay)
 {
-    T((T_CALLED("wtimeout(%p,%d)"), win, delay));
+    T((T_CALLED("wtimeout(%p,%d)"), (void *) win, delay));
 
     if (win) {
 	win->_delay = delay;
@@ -125,11 +144,11 @@
 NCURSES_EXPORT(int)
 keypad(WINDOW *win, bool flag)
 {
-    T((T_CALLED("keypad(%p,%d)"), win, flag));
+    T((T_CALLED("keypad(%p,%d)"), (void *) win, flag));
 
     if (win) {
 	win->_use_keypad = flag;
-	returnCode(_nc_keypad(SP, flag));
+	returnCode(_nc_keypad(_nc_screen_of(win), flag));
     } else
 	returnCode(ERR);
 }
@@ -137,38 +156,107 @@
 NCURSES_EXPORT(int)
 meta(WINDOW *win GCC_UNUSED, bool flag)
 {
-    int result;
+    int result = ERR;
+    SCREEN *sp = (win == 0) ? CURRENT_SCREEN : _nc_screen_of(win);
 
     /* Ok, we stay relaxed and don't signal an error if win is NULL */
-    T((T_CALLED("meta(%p,%d)"), win, flag));
-    result = _nc_meta(SP, flag);
+    T((T_CALLED("meta(%p,%d)"), (void *) win, flag));
+
+    /* Ok, we stay relaxed and don't signal an error if win is NULL */
+
+    if (sp != 0) {
+	sp->_use_meta = flag;
+#ifdef USE_TERM_DRIVER
+	if (IsTermInfo(sp)) {
+	    if (flag) {
+		NCURSES_PUTP2("meta_on", meta_on);
+	    } else {
+		NCURSES_PUTP2("meta_off", meta_off);
+	    }
+	}
+#else
+	if (flag) {
+	    NCURSES_PUTP2("meta_on", meta_on);
+	} else {
+	    NCURSES_PUTP2("meta_off", meta_off);
+	}
+#endif
+	result = OK;
+    }
     returnCode(result);
 }
 
 /* curs_set() moved here to narrow the kernel interface */
 
 NCURSES_EXPORT(int)
-curs_set(int vis)
+NCURSES_SP_NAME(curs_set) (NCURSES_SP_DCLx int vis)
 {
-    int result;
+    int code = ERR;
+    T((T_CALLED("curs_set(%p,%d)"), (void *) SP_PARM, vis));
 
-    T((T_CALLED("curs_set(%d)"), vis));
-    result = _nc_curs_set(SP, vis);
-    returnCode(result);
+    if (SP_PARM != 0 && vis >= 0 && vis <= 2) {
+	int cursor = SP_PARM->_cursor;
+	if (vis == cursor) {
+	    code = cursor;
+	} else {
+#ifdef USE_TERM_DRIVER
+	    code = CallDriver_1(SP_PARM, td_cursorSet, vis);
+#else
+	    if (IsTermInfo(SP_PARM)) {
+		switch (vis) {
+		case 2:
+		    code = NCURSES_PUTP2_FLUSH("cursor_visible",
+					       cursor_visible);
+		    break;
+		case 1:
+		    code = NCURSES_PUTP2_FLUSH("cursor_normal",
+					       cursor_normal);
+		    break;
+		case 0:
+		    code = NCURSES_PUTP2_FLUSH("cursor_invisible",
+					       cursor_invisible);
+		    break;
+		}
+	    } else {
+		code = ERR;
+	    }
+#endif
+	    if (code != ERR)
+		code = (cursor == -1 ? 1 : cursor);
+	    SP_PARM->_cursor = vis;
+	}
+    }
+    returnCode(code);
 }
 
+#if NCURSES_SP_FUNCS
 NCURSES_EXPORT(int)
-typeahead(int fd)
+curs_set(int vis)
 {
-    T((T_CALLED("typeahead(%d)"), fd));
-    if (SP != 0) {
-	SP->_checkfd = fd;
+    return (NCURSES_SP_NAME(curs_set) (CURRENT_SCREEN, vis));
+}
+#endif
+
+NCURSES_EXPORT(int)
+NCURSES_SP_NAME(typeahead) (NCURSES_SP_DCLx int fd)
+{
+    T((T_CALLED("typeahead(%p, %d)"), (void *) SP_PARM, fd));
+    if (IsValidTIScreen(SP_PARM)) {
+	SP_PARM->_checkfd = fd;
 	returnCode(OK);
     } else {
 	returnCode(ERR);
     }
 }
 
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(int)
+typeahead(int fd)
+{
+    return NCURSES_SP_NAME(typeahead) (CURRENT_SCREEN, fd);
+}
+#endif
+
 /*
 **      has_key()
 **
@@ -189,43 +277,50 @@
 		|| has_key_internal(keycode, tp->sibling));
 }
 
+#ifdef USE_TERM_DRIVER
+NCURSES_EXPORT(int)
+TINFO_HAS_KEY(SCREEN *sp, int keycode)
+{
+    return IsValidTIScreen(sp) ?
+	has_key_internal(keycode, sp->_keytry) : 0;
+}
+#else
+NCURSES_EXPORT(int)
+NCURSES_SP_NAME(has_key) (NCURSES_SP_DCLx int keycode)
+{
+    T((T_CALLED("has_key(%p,%d)"), (void *) SP_PARM, keycode));
+    returnCode(SP != 0 ? has_key_internal(keycode, SP_PARM->_keytry) : FALSE);
+}
+
+#if NCURSES_SP_FUNCS
 NCURSES_EXPORT(int)
 has_key(int keycode)
 {
-    T((T_CALLED("has_key(%d)"), keycode));
-    returnCode(SP != 0 ? has_key_internal(keycode, SP->_keytry) : FALSE);
+    return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
 }
+#endif
+#endif
 #endif /* NCURSES_EXT_FUNCS */
 
-/*
- * Internal entrypoints use SCREEN* parameter to obtain capabilities rather
- * than cur_term.
- */
-#undef CUR
-#define CUR (sp->_term)->type.
-
-static int
-_nc_putp(const char *name GCC_UNUSED, const char *value)
+NCURSES_EXPORT(int)
+NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_DCLx
+				 const char *name, const char *value)
 {
-    int rc = ERR;
-
-    if (value) {
-	TPUTS_TRACE(name);
-	rc = putp(value);
-    }
-    return rc;
-}
-
-static int
-_nc_putp_flush(const char *name, const char *value)
-{
-    int rc = _nc_putp(name, value);
+    int rc = NCURSES_PUTP2(name, value);
     if (rc != ERR) {
 	_nc_flush();
     }
     return rc;
 }
 
+#if 0 && NCURSES_SP_FUNCS
+NCURSES_EXPORT(int)
+_nc_putp_flush(const char *name, const char *value)
+{
+    return NCURSES_SP_NAME(_nc_putp_flush) (CURRENT_SCREEN, name, value);
+}
+#endif
+
 /* Turn the keypad on/off
  *
  * Note:  we flush the output because changing this mode causes some terminals
@@ -234,7 +329,7 @@
  * the terminal state _before_ switching modes.
  */
 NCURSES_EXPORT(int)
-_nc_keypad(SCREEN *sp, bool flag)
+_nc_keypad(SCREEN *sp, int flag)
 {
     int rc = ERR;
 
@@ -245,12 +340,12 @@
 	 * has wgetch() reading in more than one thread.  putp() and below
 	 * may use SP explicitly.
 	 */
-	if (_nc_use_pthreads && sp != SP) {
+	if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
 	    SCREEN *save_sp;
 
 	    /* cannot use use_screen(), since that is not in tinfo library */
 	    _nc_lock_global(curses);
-	    save_sp = SP;
+	    save_sp = CURRENT_SCREEN;
 	    _nc_set_screen(sp);
 	    rc = _nc_keypad(sp, flag);
 	    _nc_set_screen(save_sp);
@@ -258,10 +353,15 @@
 	} else
 #endif
 	{
+#ifdef USE_TERM_DRIVER
+	    rc = CallDriver_1(sp, td_kpad, flag);
+	    if (rc == OK)
+		sp->_keypad_on = flag;
+#else
 	    if (flag) {
-		(void) _nc_putp_flush("keypad_xmit", keypad_xmit);
+		(void) NCURSES_PUTP2_FLUSH("keypad_xmit", keypad_xmit);
 	    } else if (!flag && keypad_local) {
-		(void) _nc_putp_flush("keypad_local", keypad_local);
+		(void) NCURSES_PUTP2_FLUSH("keypad_local", keypad_local);
 	    }
 
 	    if (flag && !sp->_tried) {
@@ -270,58 +370,8 @@
 	    }
 	    sp->_keypad_on = flag;
 	    rc = OK;
+#endif
 	}
     }
     return (rc);
 }
-
-static int
-_nc_curs_set(SCREEN *sp, int vis)
-{
-    int result = ERR;
-
-    T((T_CALLED("curs_set(%d)"), vis));
-    if (sp != 0 && vis >= 0 && vis <= 2) {
-	int cursor = sp->_cursor;
-
-	if (vis == cursor) {
-	    result = cursor;
-	} else {
-	    switch (vis) {
-	    case 2:
-		result = _nc_putp_flush("cursor_visible", cursor_visible);
-		break;
-	    case 1:
-		result = _nc_putp_flush("cursor_normal", cursor_normal);
-		break;
-	    case 0:
-		result = _nc_putp_flush("cursor_invisible", cursor_invisible);
-		break;
-	    }
-	    if (result != ERR)
-		result = (cursor == -1 ? 1 : cursor);
-	    sp->_cursor = vis;
-	}
-    }
-    returnCode(result);
-}
-
-static int
-_nc_meta(SCREEN *sp, bool flag)
-{
-    int result = ERR;
-
-    /* Ok, we stay relaxed and don't signal an error if win is NULL */
-
-    if (SP != 0) {
-	SP->_use_meta = flag;
-
-	if (flag) {
-	    _nc_putp("meta_on", meta_on);
-	} else {
-	    _nc_putp("meta_off", meta_off);
-	}
-	result = OK;
-    }
-    return result;
-}