]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_sgr.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / test_sgr.c
index b113e63fdf4c281f1a7730bc46dc72262367c8b4..8763dca3cd34156f91addef70624f4bfc154dd2a 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2015,2016 Free Software Foundation, Inc.                   *
+ * Copyright 2019-2022,2023 Thomas E. Dickey                                *
+ * Copyright 2015-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            *
@@ -29,7 +30,7 @@
 /*
  * Author: Thomas E. Dickey
  *
- * $Id: test_sgr.c,v 1.6 2016/02/13 22:08:16 tom Exp $
+ * $Id: test_sgr.c,v 1.22 2023/05/27 20:13:10 tom Exp $
  *
  * A simple demo of the sgr/sgr0 terminal capabilities.
  */
@@ -37,7 +38,7 @@
 #include <test.priv.h>
 
 #if !HAVE_TIGETSTR
-static void failed(const char *) GCC_NORETURN;
+static GCC_NORETURN void failed(const char *);
 
 static void
 failed(const char *msg)
@@ -49,6 +50,7 @@ failed(const char *msg)
 
 #if HAVE_TIGETSTR
 
+static bool no_init = FALSE;
 static bool q_opt = FALSE;
 
 static char *d_opt;
@@ -59,10 +61,11 @@ static int db_item;
 static long total_values;
 
 static char *
-make_dbitem(char *p, char *q)
+make_dbitem(const char *const p, const char *const q)
 {
-    char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
-    sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
+    size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
+    char *result = malloc(need);
+    _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
     return result;
 }
 
@@ -113,11 +116,11 @@ next_dbitem(void)
            db_item++;
        }
     }
-    printf("** %s\n", result);
+    printf("** %s\n", result ? result : "<null>");
     return result;
 }
 
-#ifdef NO_LEAKS
+#if NO_LEAKS
 static void
 free_dblist(void)
 {
@@ -145,7 +148,7 @@ dumpit(unsigned bits, unsigned ignore, const char *sgr, const char *sgr0)
     static char params[] = "SURBDBIPA";
     unsigned n;
 
-    printf("%4d ", bits);
+    printf("%4u ", bits);
     bits &= ~ignore;
     for (n = 0; n < MAXPAR; ++n) {
        putchar((int) ((bits & (unsigned) (1 << n)) ? params[n] : '-'));
@@ -180,15 +183,23 @@ brute_force(const char *name)
     char *my_bold;
     char *my_revs;
     char *my_smso;
+    char *my_name = strdup(name);
 
     if (db_list) {
        putenv(next_dbitem());
     }
+
     if (!q_opt)
-       printf("Terminal type \"%s\"\n", name);
-    setupterm((NCURSES_CONST char *) name, 1, (int *) 0);
+       printf("Terminal type \"%s\"\n", my_name);
+
+    if (no_init) {
+       START_TRACE();
+    } else {
+       setupterm((NCURSES_CONST char *) my_name, 1, (int *) 0);
+    }
+
     if (!q_opt) {
-       if (strcmp(name, ttytype))
+       if (strcmp(my_name, ttytype))
            printf("... actual \"%s\"\n", ttytype);
     }
 
@@ -203,7 +214,7 @@ brute_force(const char *name)
     } else if (!VALID_STRING(my_sgr0)) {
        fprintf(stderr, "no \"sgr0\" capability found\n");
     } else {
-       char *values[MAXSGR];
+       char *values[MAXSGR + MAXPAR];
        unsigned j;
        unsigned ignore = 0;
        unsigned reason = 0;
@@ -285,57 +296,73 @@ brute_force(const char *name)
                }
            }
        }
+       for (count = 0; count < MAXSGR; ++count) {
+           free(values[count]);
+       }
     }
+    free(my_name);
     del_curterm(cur_term);
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
-       "Usage: test_sgr [options] [terminal]",
-       "",
-       "Print all distinct combinations of sgr capability.",
-       "",
-       "Options:",
-       " -d LIST  colon-separated list of databases to use",
-       " -e NAME  environment variable to set with -d option",
-       " -q       quiet (prints only counts)",
+       "Usage: test_sgr [options] [terminal]"
+       ,""
+       ,"Print all distinct combinations of sgr capability."
+       ,""
+       ,USAGE_COMMON
+       ,"Options:"
+       ," -d LIST  colon-separated list of databases to use"
+       ," -e NAME  environment variable to set with -d option"
+       ," -n       do not initialize terminal, to test error-checking"
+       ," -q       quiet (prints only counts)"
     };
     unsigned n;
     for (n = 0; n < SIZEOF(msg); ++n) {
        fprintf(stderr, "%s\n", msg[n]);
     }
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
 {
-    int n;
+    int ch;
     char *name;
 
-    while ((n = getopt(argc, argv, "d:e:q")) != -1) {
-       switch (n) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "d:e:nq")) != -1) {
+       switch (ch) {
        case 'd':
            d_opt = optarg;
            break;
        case 'e':
            e_opt = optarg;
            break;
+       case 'n':
+           no_init = TRUE;
+           break;
        case 'q':
            q_opt = TRUE;
            break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           break;
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
 
     make_dblist();
 
     if (optind < argc) {
+       int n;
        for (n = optind; n < argc; ++n) {
            brute_force(argv[n]);
        }
@@ -348,7 +375,7 @@ main(int argc, char *argv[])
 
     printf("%ld distinct values\n", total_values);
 
-#ifdef NO_LEAKS
+#if NO_LEAKS
     free_dblist();
 #endif
 
@@ -357,7 +384,7 @@ main(int argc, char *argv[])
 
 #else /* !HAVE_TIGETSTR */
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(void)
 {
     failed("This program requires the terminfo functions such as tigetstr");
     ExitProgram(EXIT_FAILURE);