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/demo_menus.c b/test/demo_menus.c
index ccd48b0..7b0bf30 100644
--- a/test/demo_menus.c
+++ b/test/demo_menus.c
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2005-2013,2014 Free Software Foundation, Inc.              *
+ * Copyright 2019-2022,2023 Thomas E. Dickey                                *
+ * Copyright 2003-2016,2017 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            *
@@ -26,7 +27,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: demo_menus.c,v 1.54 2014/09/05 08:34:06 tom Exp $
+ * $Id: demo_menus.c,v 1.80 2023/05/27 20:13:10 tom Exp $
  *
  * Demonstrate a variety of functions from the menu library.
  * Thomas Dickey - 2005/4/9
@@ -75,11 +76,8 @@
 #ifdef NCURSES_VERSION
 #ifdef TRACE
 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
-extern unsigned _nc_tracing;
 static MENU *mpTrace;
 #endif
-#else
-#undef TRACE
 #endif
 
 typedef enum {
@@ -114,6 +112,18 @@
 
 static char empty[1];
 
+#ifdef TRACE
+static GCC_NORETURN void failed(const char *s);
+
+static void
+failed(const char *s)
+{
+    perror(s);
+    endwin();
+    ExitProgram(EXIT_FAILURE);
+}
+#endif
+
 /* Common function to allow ^T to toggle trace-mode in the middle of a test
  * so that trace-files can be made smaller.
  */
@@ -130,7 +140,7 @@
 	} else {
 	    _nc_tracing = save_trace;
 	}
-	trace(_nc_tracing);
+	curses_trace(_nc_tracing);
 	if (_nc_tracing)
 	    Trace(("TOGGLE-TRACING ON"));
     }
@@ -186,9 +196,9 @@
     int result = 0;
 
     if (okMenuNo(number)) {
-	int spc_desc, spc_rows, spc_cols;
-
+	int spc_rows;
 #ifdef NCURSES_VERSION
+	int spc_desc, spc_cols;
 	menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
 #else
 	spc_rows = 0;
@@ -270,13 +280,6 @@
     if (mcols + (2 * margin + x) >= COLS)
 	mcols = COLS - (2 * margin + x);
 
-#ifdef TRACE
-    if (number == eTrace)
-	menu_opts_off(result, O_ONEVALUE);
-    else
-	menu_opts_on(result, O_ONEVALUE);
-#endif
-
     menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
     set_menu_win(result, menuwin);
     keypad(menuwin, TRUE);
@@ -285,6 +288,16 @@
 
     set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
 
+#ifdef TRACE
+    if (number == eTrace)
+	menu_opts_off(result, O_ONEVALUE);
+    else
+	menu_opts_on(result, O_ONEVALUE);
+#endif
+#if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
+    menu_opts_on(result, O_MOUSE_MENU);
+#endif
+
     post_menu(result);
 
     set_menu_init(result, my_menu_init);
@@ -295,14 +308,13 @@
 }
 
 static void
-menu_destroy(MENU * m)
+menu_destroy(MENU * m, int itemsToo)
 {
-    int count;
-
     Trace(("menu_destroy %p", (void *) m));
     if (m != 0) {
 	ITEM **items = menu_items(m);
 	const char *blob = 0;
+	int count;
 
 	count = item_count(m);
 	Trace(("menu_destroy %p count %d", (void *) m, count));
@@ -319,18 +331,18 @@
 		Trace(("freeing blob %p", blob));
 		free((void *) blob);
 	    }
-	    free(items);
-	    items = 0;
 	}
-#ifdef TRACE
-	if ((count > 0) && (m == mpTrace)) {
-	    ITEM **ip = items;
-	    if (ip != 0) {
-		while (*ip)
-		    free(*ip++);
+	if (count > 0 && itemsToo) {
+	    if (itemsToo & 1) {
+		ITEM **ip = items;
+		if (ip != 0) {
+		    while (*ip)
+			free_item(*ip++);
+		}
 	    }
+	    if (itemsToo & 2)
+		free(items);
 	}
-#endif
     }
 }
 
@@ -417,7 +429,6 @@
 	    && (sb.st_mode & S_IFMT) == S_IFREG
 	    && sb.st_size != 0) {
 	    size_t size = (size_t) sb.st_size;
-	    unsigned j, k;
 	    char *blob = typeMalloc(char, size + 1);
 	    MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
 
@@ -430,6 +441,7 @@
 		if (fp != 0) {
 		    if (fread(blob, sizeof(char), size, fp) == size) {
 			bool mark = TRUE;
+			unsigned j, k;
 			for (j = k = 0; j < size; ++j) {
 			    if (mark) {
 				list[k++].name = blob + j;
@@ -541,27 +553,31 @@
 tracetrace(unsigned tlevel)
 {
     static char *buf;
+    static size_t need = 12;
     int n;
 
     if (buf == 0) {
-	size_t need = 12;
 	for (n = 0; t_tbl[n].name != 0; n++)
 	    need += strlen(t_tbl[n].name) + 2;
 	buf = typeMalloc(char, need);
+	if (!buf)
+	    failed("tracetrace");
     }
-    sprintf(buf, "0x%02x = {", tlevel);
+    _nc_SPRINTF(buf, _nc_SLIMIT(need) "0x%02x = {", tlevel);
     if (tlevel == 0) {
-	sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
+	_nc_STRCAT(buf, t_tbl[0].name ? t_tbl[0].name : "", need);
+	_nc_STRCAT(buf, ", ", need);
     } else {
 	for (n = 1; t_tbl[n].name != 0; n++)
 	    if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
-		strcat(buf, t_tbl[n].name);
-		strcat(buf, ", ");
+		_nc_STRCAT(buf, t_tbl[n].name, need);
+		_nc_STRCAT(buf, ", ", need);
 	    }
     }
     if (buf[strlen(buf) - 2] == ',')
 	buf[strlen(buf) - 2] = '\0';
-    return (strcat(buf, "}"));
+    _nc_STRCAT(buf, "}", need);
+    return buf;
 }
 
 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
@@ -571,13 +587,14 @@
 update_trace_menu(MENU * m)
 {
     ITEM **items;
-    ITEM *i, **p;
+    ITEM *i;
     bool changed = FALSE;
 
     items = menu_items(m);
     i = current_item(m);
     if (i == items[0]) {
 	if (item_value(i)) {
+	    ITEM **p;
 	    for (p = items + 1; *p != 0; p++)
 		if (item_value(*p)) {
 		    set_item_value(*p, FALSE);
@@ -593,7 +610,6 @@
 /* interactively set the trace level */
 {
     ITEM **ip;
-    unsigned newtrace;
     int result;
 
     for (ip = menu_items(mpTrace); *ip; ip++) {
@@ -609,14 +625,14 @@
 
     if (result == E_OK) {
 	if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
-	    newtrace = 0;
+	    unsigned newtrace = 0;
 	    for (ip = menu_items(mpTrace); *ip; ip++) {
 		if (item_value(*ip)) {
 		    MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
 		    newtrace |= td->mask;
 		}
 	    }
-	    trace(newtrace);
+	    curses_trace(newtrace);
 	    Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
 
 	    MvWPrintw(status, 1, 0,
@@ -751,6 +767,36 @@
     }
 }
 
+#if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
+static void
+resize_menu(MENU ** menu)
+{
+#if 0
+    WINDOW *win = menu_win(*menu);
+    WINDOW *sub = menu_sub(*menu);
+#endif
+    (void) menu;
+}
+
+static void
+resize_menus(MENU * current)
+{
+    (void) current;
+
+    werase(status);
+    wnoutrefresh(status);
+    wresize(status, 1, COLS);
+    mvwin(status, LINES - 1, 0);
+
+    resize_menu(&mpBanner);
+    resize_menu(&mpFile);
+    resize_menu(&mpSelect);
+#ifdef TRACE
+    resize_menu(&mpTrace);
+#endif
+}
+#endif /* defined(KEY_RESIZE) && NCURSES_EXT_FUNCS */
+
 static void
 show_status(int ch, MENU * menu)
 {
@@ -774,7 +820,7 @@
     int ch = ERR;
 
 #ifdef NCURSES_MOUSE_VERSION
-    mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
+    mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
 #endif
 
     menu_display(last_menu);
@@ -803,6 +849,11 @@
 	case KEY_SRIGHT:
 	    move_menus(last_menu, 0, 1);
 	    continue;
+#if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
+	case KEY_RESIZE:
+	    resize_menus(last_menu);
+	    continue;
+#endif
 	}
 	cmd = menu_virtualize(ch);
 
@@ -836,9 +887,17 @@
 #endif
 	    }
 
+#if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
 	    if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
+		(void) menu_getc(mpBanner);
 		code = menu_driver(mpBanner, cmd);
+		if (code == E_REQUEST_DENIED) {
+		    MEVENT event;
+		    if (menu_getc(mpBanner) == KEY_MOUSE)
+			getmouse(&event);	/* give up */
+		}
 	    }
+#endif
 
 	    break;
 	}
@@ -866,23 +925,18 @@
 	}
 	if (code == E_REQUEST_DENIED)
 	    beep();
-	continue;
     }
-
-#ifdef NCURSES_MOUSE_VERSION
-    mousemask(0, (mmask_t *) 0);
-#endif
 }
 
 static void
 destroy_menus(void)
 {
-    menu_destroy(mpFile);
-    menu_destroy(mpSelect);
+    menu_destroy(mpFile, 1);
+    menu_destroy(mpSelect, 3);
 #ifdef TRACE
-    menu_destroy(mpTrace);
+    menu_destroy(mpTrace, 1);
 #endif
-    menu_destroy(mpBanner);
+    menu_destroy(mpBanner, 1);
 }
 
 #if HAVE_RIPOFFLINE
@@ -922,51 +976,60 @@
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *const tbl[] =
     {
 	"Usage: demo_menus [options] [menu-file]"
 	,""
+	,USAGE_COMMON
 	,"Options:"
 #if HAVE_RIPOFFLINE
-	,"  -f       rip-off footer line (can repeat)"
-	,"  -h       rip-off header line (can repeat)"
+	," -F       rip-off footer line (can repeat)"
+	," -H       rip-off header line (can repeat)"
 #endif
 #ifdef TRACE
-	,"  -t mask  specify default trace-level (may toggle with ^T)"
+	," -t mask  specify default trace-level (may toggle with ^T)"
 #endif
     };
     size_t n;
     for (n = 0; n < SIZEOF(tbl); n++)
 	fprintf(stderr, "%s\n", tbl[n]);
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
 {
-    int c;
+    int ch;
 
     setlocale(LC_ALL, "");
+    START_TRACE();
 
-    while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
-	switch (c) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "FHt:")) != -1) {
+	switch (ch) {
 #if HAVE_RIPOFFLINE
-	case 'f':
+	case 'F':
 	    ripoffline(-1, rip_footer);
 	    break;
-	case 'h':
+	case 'H':
 	    ripoffline(1, rip_header);
 	    break;
 #endif /* HAVE_RIPOFFLINE */
 #ifdef TRACE
 	case 't':
-	    trace((unsigned) strtoul(optarg, 0, 0));
+	    curses_trace((unsigned) strtoul(optarg, 0, 0));
 	    break;
 #endif
+	case OPTS_VERSION:
+	    show_version(argv);
+	    ExitProgram(EXIT_SUCCESS);
 	default:
-	    usage();
+	    usage(ch == OPTS_USAGE);
+	    /* NOTREACHED */
 	}
     }