]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/demo_menus.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / demo_menus.c
index 5673deff216dd231386111798e91e8d4bfe0f885..7b0bf30316e558f6aa6b88559feb6b3e092b1548 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2005-2017,2019 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.66 2019/04/06 20:42:48 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
@@ -110,7 +111,9 @@ static WINDOW *status;
 static bool loaded_file = FALSE;
 
 static char empty[1];
-static void failed(const char *s) GCC_NORETURN;
+
+#ifdef TRACE
+static GCC_NORETURN void failed(const char *s);
 
 static void
 failed(const char *s)
@@ -119,6 +122,7 @@ failed(const char *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.
@@ -136,7 +140,7 @@ wGetchar(WINDOW *win)
        } else {
            _nc_tracing = save_trace;
        }
-       trace(_nc_tracing);
+       curses_trace(_nc_tracing);
        if (_nc_tracing)
            Trace(("TOGGLE-TRACING ON"));
     }
@@ -304,14 +308,13 @@ menu_create(ITEM ** items, int count, int ncols, MenuNo number)
 }
 
 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));
@@ -328,18 +331,18 @@ menu_destroy(MENU * m)
                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
     }
 }
 
@@ -426,7 +429,6 @@ build_select_menu(MenuNo number, char *filename)
            && (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);
 
@@ -439,6 +441,7 @@ build_select_menu(MenuNo number, char *filename)
                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;
@@ -562,7 +565,7 @@ tracetrace(unsigned tlevel)
     }
     _nc_SPRINTF(buf, _nc_SLIMIT(need) "0x%02x = {", tlevel);
     if (tlevel == 0) {
-       _nc_STRCAT(buf, t_tbl[0].name, need);
+       _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++)
@@ -584,13 +587,14 @@ static bool
 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);
@@ -606,7 +610,6 @@ perform_trace_menu(int cmd)
 /* interactively set the trace level */
 {
     ITEM **ip;
-    unsigned newtrace;
     int result;
 
     for (ip = menu_items(mpTrace); *ip; ip++) {
@@ -622,14 +625,14 @@ perform_trace_menu(int cmd)
 
     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,
@@ -922,19 +925,18 @@ perform_menus(void)
        }
        if (code == E_REQUEST_DENIED)
            beep();
-       continue;
     }
 }
 
 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
@@ -974,51 +976,60 @@ call_files(int code)
 }
 
 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, "fht:")) != -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 */
        }
     }