libncurses: Import https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.5.tar.gz changes

Change-Id: I3433d30ca01359fd2e3623ede96b531f0b39cbfa
Signed-off-by: micky387 <mickaelsaibi@free.fr>
diff --git a/test/testcurs.c b/test/testcurs.c
index 7a44a2c..750e5a4 100644
--- a/test/testcurs.c
+++ b/test/testcurs.c
@@ -6,13 +6,13 @@
  *  wrs(5/28/93) -- modified to be consistent (perform identically) with either
  *                  PDCurses or under Unix System V, R4
  *
- * $Id: testcurs.c,v 1.50 2015/07/05 00:11:10 tom Exp $
+ * $Id: testcurs.c,v 1.58 2023/05/28 14:23:34 tom Exp $
  */
 
 #include <test.priv.h>
 
 #if defined(XCURSES)
-char *XCursesProgramName = "testcurs";
+const char *XCursesProgramName = "testcurs";
 #endif
 
 static int initTest(WINDOW **);
@@ -20,127 +20,16 @@
 static void inputTest(WINDOW *);
 static void introTest(WINDOW *);
 static void outputTest(WINDOW *);
+#if HAVE_NEWPAD
 static void padTest(WINDOW *);
+#endif
 static void scrollTest(WINDOW *);
 #if defined(PDCURSES) && !defined(XCURSES)
 static void resizeTest(WINDOW *);
 #endif
 
-struct commands {
-    NCURSES_CONST char *text;
-    void (*function) (WINDOW *);
-};
-typedef struct commands COMMAND;
-
-static const COMMAND command[] =
-{
-    {"General Test", introTest},
-    {"Pad Test", padTest},
-#if defined(PDCURSES) && !defined(XCURSES)
-    {"Resize Test", resizeTest},
-#endif
-    {"Scroll Test", scrollTest},
-    {"Input Test", inputTest},
-    {"Output Test", outputTest}
-};
-#define MAX_OPTIONS (int) SIZEOF(command)
-
 static int width, height;
 
-int
-main(
-	int argc GCC_UNUSED,
-	char *argv[]GCC_UNUSED)
-{
-    WINDOW *win;
-    int key;
-    int old_option = (-1);
-    int new_option = 0;
-    bool quit = FALSE;
-    int n;
-
-    setlocale(LC_ALL, "");
-
-#ifdef PDCDEBUG
-    PDC_debug("testcurs started\n");
-#endif
-    if (!initTest(&win))
-	ExitProgram(EXIT_FAILURE);
-
-    erase();
-    display_menu(old_option, new_option);
-    for (;;) {
-#ifdef A_COLOR
-	if (has_colors()) {
-	    init_pair(1, COLOR_WHITE, COLOR_BLUE);
-	    wbkgd(win, (chtype) COLOR_PAIR(1));
-	} else
-	    wbkgd(win, A_REVERSE);
-#else
-	wbkgd(win, A_REVERSE);
-#endif
-	werase(win);
-
-	noecho();
-	keypad(stdscr, TRUE);
-	raw();
-	key = getch();
-	if (key < KEY_MIN && key > 0 && isalpha(key)) {
-	    if (islower(key))
-		key = toupper(key);
-	    for (n = 0; n < MAX_OPTIONS; ++n) {
-		if (key == command[n].text[0]) {
-		    display_menu(old_option, new_option = n);
-		    key = ' ';
-		    break;
-		}
-	    }
-	}
-	switch (key) {
-	case 10:
-	case 13:
-	case KEY_ENTER:
-	    erase();
-	    refresh();
-	    (*command[new_option].function) (win);
-	    erase();
-	    display_menu(old_option, new_option);
-	    break;
-	case KEY_UP:
-	    new_option = ((new_option == 0)
-			  ? new_option
-			  : new_option - 1);
-	    display_menu(old_option, new_option);
-	    break;
-	case KEY_DOWN:
-	    new_option = ((new_option == (MAX_OPTIONS - 1))
-			  ? new_option
-			  : new_option + 1);
-	    display_menu(old_option, new_option);
-	    break;
-	case 'Q':
-	case 'q':
-	    quit = TRUE;
-	    break;
-	default:
-	    beep();
-	    break;
-	case ' ':
-	    break;
-	}
-	if (quit == TRUE)
-	    break;
-    }
-
-    delwin(win);
-
-    endwin();
-#ifdef XCURSES
-    XCursesExit();
-#endif
-    ExitProgram(EXIT_SUCCESS);
-}
-
 static void
 Continue(WINDOW *win)
 {
@@ -170,7 +59,7 @@
     PDC_debug("initTest called\n");
 #endif
 #ifdef TRACE
-    trace(TRACE_MAXIMUM);
+    curses_trace(TRACE_MAXIMUM);
 #endif
     initscr();
 #ifdef PDCDEBUG
@@ -184,7 +73,7 @@
     height = 13;		/* Create a drawing window */
     *win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
     if (*win == NULL) {
-	endwin();
+	stop_curses();
 	return 0;
     }
     return 1;
@@ -271,7 +160,7 @@
 {
     int answered;
     int repeat;
-    int w, h, bx, by, sw, sh, i, c, num;
+    int w, h, bx, by, sw, sh, i, num;
     char buffer[80];
     WINDOW *subWin;
     wclear(win);
@@ -349,6 +238,8 @@
 #endif
 
     for (;;) {
+	int c;
+
 	wmove(win, 3, 5);
 	c = wgetch(win);
 	wclrtobot(win);
@@ -361,11 +252,11 @@
 #ifdef KEY_MOUSE
 	if (c == KEY_MOUSE) {
 #if defined(NCURSES_MOUSE_VERSION)
-#define ButtonChanged(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, 037))
-#define ButtonPressed(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_PRESSED))
-#define ButtonDouble(n)  ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_DOUBLE_CLICKED))
-#define ButtonTriple(n)  ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_TRIPLE_CLICKED))
-#define ButtonRelease(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_RELEASED))
+#define ButtonChanged(n) ((event.bstate) & NCURSES_MOUSE_MASK(n, (NCURSES_BUTTON_RELEASED|NCURSES_BUTTON_PRESSED|NCURSES_BUTTON_CLICKED|NCURSES_DOUBLE_CLICKED|NCURSES_TRIPLE_CLICKED|NCURSES_RESERVED_EVENT)))
+#define ButtonPressed(n) ((event.bstate) & NCURSES_MOUSE_MASK(n, NCURSES_BUTTON_PRESSED))
+#define ButtonDouble(n)  ((event.bstate) & NCURSES_MOUSE_MASK(n, NCURSES_DOUBLE_CLICKED))
+#define ButtonTriple(n)  ((event.bstate) & NCURSES_MOUSE_MASK(n, NCURSES_TRIPLE_CLICKED))
+#define ButtonRelease(n) ((event.bstate) & NCURSES_MOUSE_MASK(n, NCURSES_BUTTON_RELEASED))
 	    MEVENT event;
 	    int button = 0;
 
@@ -466,7 +357,6 @@
 static void
 outputTest(WINDOW *win)
 {
-    WINDOW *win1;
     char Buffer[80];
     chtype ch;
     int by, bx;
@@ -506,7 +396,7 @@
 	MvWAddStr(win, 6, 1, "display of at least 24 LINES by 75 COLUMNS");
 	Continue(win);
     } else {
-	win1 = newwin(10, 50, 14, 25);
+	WINDOW *win1 = newwin(10, 50, 14, 25);
 	if (win1 == NULL) {
 	    endwin();
 	    return;
@@ -668,7 +558,7 @@
 
     win1 = newwin(10, 50, 14, 25);
     if (win1 == NULL) {
-	endwin();
+	stop_curses();
 	return;
     }
 #ifdef A_COLOR
@@ -696,12 +586,15 @@
 }
 #endif
 
+#if HAVE_NEWPAD
 static void
 padTest(WINDOW *dummy GCC_UNUSED)
 {
-    WINDOW *pad, *spad;
+    WINDOW *pad;
 
     if ((pad = newpad(50, 100)) != 0) {
+	WINDOW *spad;
+
 	wattron(pad, A_REVERSE);
 	MvWAddStr(pad, 5, 2, "This is a new pad");
 	(void) wattrset(pad, A_NORMAL);
@@ -736,6 +629,28 @@
 	delwin(pad);
     }
 }
+#endif /* HAVE_NEWPAD */
+
+struct commands {
+    NCURSES_CONST char *text;
+    void (*function) (WINDOW *);
+};
+typedef struct commands COMMAND;
+
+static const COMMAND command[] =
+{
+    {"General Test", introTest},
+#if HAVE_NEWPAD
+    {"Pad Test", padTest},
+#endif
+#if defined(PDCURSES) && !defined(XCURSES)
+    {"Resize Test", resizeTest},
+#endif
+    {"Scroll Test", scrollTest},
+    {"Input Test", inputTest},
+    {"Output Test", outputTest}
+};
+#define MAX_OPTIONS (int) SIZEOF(command)
 
 static void
 display_menu(int old_option, int new_option)
@@ -760,3 +675,131 @@
 	     "Use Up and Down Arrows to select - Enter to run - Q to quit");
     refresh();
 }
+
+static void
+usage(int ok)
+{
+    static const char *msg[] =
+    {
+	"Usage: testcurs [options]"
+	,""
+	,USAGE_COMMON
+    };
+    size_t n;
+
+    for (n = 0; n < SIZEOF(msg); n++)
+	fprintf(stderr, "%s\n", msg[n]);
+
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
+
+int
+main(int argc, char *argv[])
+{
+    WINDOW *win;
+    int old_option = (-1);
+    int new_option = 0;
+    bool quit = FALSE;
+    int n;
+    int ch;
+
+    while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
+	switch (ch) {
+	case OPTS_VERSION:
+	    show_version(argv);
+	    ExitProgram(EXIT_SUCCESS);
+	default:
+	    usage(ch == OPTS_USAGE);
+	    /* NOTREACHED */
+	}
+    }
+    if (optind < argc)
+	usage(FALSE);
+
+    setlocale(LC_ALL, "");
+
+#ifdef PDCDEBUG
+    PDC_debug("testcurs started\n");
+#endif
+    if (!initTest(&win))
+	ExitProgram(EXIT_FAILURE);
+
+    erase();
+    display_menu(old_option, new_option);
+
+    for (;;) {
+	int key;
+
+#ifdef A_COLOR
+	if (has_colors()) {
+	    init_pair(1, COLOR_WHITE, COLOR_BLUE);
+	    wbkgd(win, (chtype) COLOR_PAIR(1));
+	} else
+	    wbkgd(win, A_REVERSE);
+#else
+	wbkgd(win, A_REVERSE);
+#endif
+	werase(win);
+
+	noecho();
+	keypad(stdscr, TRUE);
+	raw();
+	key = getch();
+	if (key < KEY_MIN && key > 0 && isalpha(key)) {
+	    if (islower(key))
+		key = toupper(key);
+	    for (n = 0; n < MAX_OPTIONS; ++n) {
+		if (key == command[n].text[0]) {
+		    display_menu(old_option, new_option = n);
+		    key = ' ';
+		    break;
+		}
+	    }
+	}
+	switch (key) {
+	case 10:
+	case 13:
+	case KEY_ENTER:
+	    erase();
+	    refresh();
+	    (*command[new_option].function) (win);
+	    erase();
+	    display_menu(old_option, new_option);
+	    break;
+	case KEY_UP:
+	    new_option = ((new_option == 0)
+			  ? new_option
+			  : new_option - 1);
+	    display_menu(old_option, new_option);
+	    break;
+	case KEY_DOWN:
+	    new_option = ((new_option == (MAX_OPTIONS - 1))
+			  ? new_option
+			  : new_option + 1);
+	    display_menu(old_option, new_option);
+	    break;
+	case 'Q':
+	case 'q':
+	    quit = TRUE;
+	    break;
+	default:
+	    beep();
+	    break;
+	case ' ':
+	    break;
+	}
+	if (quit == TRUE)
+	    break;
+    }
+
+    delwin(win);
+
+    stop_curses();
+#ifdef XCURSES
+    XCursesExit();
+#endif
+    ExitProgram(EXIT_SUCCESS);
+}