]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_setupterm.c
ncurses 6.3 - patch 20221112
[ncurses.git] / test / test_setupterm.c
index 6d7e0e29bc3a51cfe0a982c80290a2fdbbe96dd7..0dc7494bda1a75d64b7f222c7b0466ce8ceff0ce 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2015 Free Software Foundation, Inc.                        *
+ * Copyright 2020,2022 Thomas E. Dickey                                     *
+ * Copyright 2015,2016 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            *
@@ -29,7 +30,7 @@
 /*
  * Author: Thomas E. Dickey
  *
- * $Id: test_setupterm.c,v 1.8 2015/06/28 00:53:46 tom Exp $
+ * $Id: test_setupterm.c,v 1.12 2022/05/15 16:36:00 tom Exp $
  *
  * A simple demo of setupterm/restartterm.
  */
@@ -42,6 +43,46 @@ static bool f_opt = FALSE;
 static bool n_opt = FALSE;
 static bool r_opt = FALSE;
 
+#if NO_LEAKS
+static TERMINAL **saved_terminals;
+static size_t num_saved;
+static size_t max_saved;
+
+static void
+finish(int code)
+{
+    size_t n;
+    for (n = 0; n < num_saved; ++n)
+       del_curterm(saved_terminals[n]);
+    free(saved_terminals);
+    ExitProgram(code);
+}
+
+static void
+save_curterm(void)
+{
+    size_t n;
+    bool found = FALSE;
+    for (n = 0; n < num_saved; ++n) {
+       if (saved_terminals[n] == cur_term) {
+           found = TRUE;
+           break;
+       }
+    }
+    if (!found) {
+       if (num_saved + 1 >= max_saved) {
+           max_saved += 100;
+           saved_terminals = typeRealloc(TERMINAL *, max_saved, saved_terminals);
+       }
+       saved_terminals[num_saved++] = cur_term;
+    }
+}
+
+#else
+#define finish(code) ExitProgram(code)
+#define save_curterm()         /* nothing */
+#endif
+
 static void
 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
 {
@@ -96,12 +137,14 @@ test_setupterm(NCURSES_CONST char *name)
     int rc;
     int err = -99;
 
-    if (r_opt) {
+#if HAVE_RESTARTTERM
+    if (r_opt)
        rc = restartterm(name, 0, f_opt ? NULL : &err);
-    } else {
+    else
+#endif
        rc = setupterm(name, 0, f_opt ? NULL : &err);
-    }
     test_rc(name, rc, err);
+    save_curterm();
 }
 
 static void
@@ -118,13 +161,15 @@ usage(void)
        " -f       treat errors as fatal",
        " -n       set environment to disable terminfo database, assuming",
        "          the compiled-in paths for database also fail",
+#if HAVE_RESTARTTERM
        " -r       test restartterm rather than setupterm",
+#endif
     };
     unsigned n;
     for (n = 0; n < SIZEOF(msg); ++n) {
        fprintf(stderr, "%s\n", msg[n]);
     }
-    ExitProgram(EXIT_FAILURE);
+    finish(EXIT_FAILURE);
 }
 
 int
@@ -143,9 +188,11 @@ main(int argc, char *argv[])
        case 'n':
            n_opt = TRUE;
            break;
+#if HAVE_RESTARTTERM
        case 'r':
            r_opt = TRUE;
            break;
+#endif
        default:
            usage();
            break;
@@ -176,6 +223,7 @@ main(int argc, char *argv[])
     if (r_opt) {
        newterm("ansi", stdout, stdin);
        reset_shell_mode();
+       save_curterm();
     }
 
     if (a_opt) {
@@ -197,7 +245,7 @@ main(int argc, char *argv[])
        }
     }
 
-    ExitProgram(EXIT_SUCCESS);
+    finish(EXIT_SUCCESS);
 }
 
 #else /* !HAVE_TIGETSTR */