]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/demo_terminfo.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / demo_terminfo.c
index 533a5f0a1960ac00e84b724d39a54a3155748d46..5e78015c36e0d45709a2fcda7fbc8546e57434c0 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2009-2013,2014 Free Software Foundation, Inc.              *
+ * Copyright 2019-2022,2023 Thomas E. Dickey                                *
+ * Copyright 2009-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: demo_terminfo.c,v 1.34 2014/08/31 23:11:39 tom Exp $
+ * $Id: demo_terminfo.c,v 1.57 2023/05/27 20:13:10 tom Exp $
  *
  * A simple demo of the terminfo interface.
  */
@@ -46,6 +47,8 @@
 #endif
 #endif
 
+static GCC_NORETURN void failed(const char *);
+
 static void
 failed(const char *msg)
 {
@@ -67,8 +70,10 @@ static bool f_opt = FALSE;
 static bool n_opt = FALSE;
 static bool q_opt = FALSE;
 static bool s_opt = FALSE;
+#ifdef NCURSES_VERSION
 static bool x_opt = FALSE;
 static bool y_opt = FALSE;
+#endif
 
 static char *d_opt;
 static char *e_opt;
@@ -88,13 +93,14 @@ static long total_n_values;
 static long total_s_values;
 
 #define FCOLS 8
-#define FNAME(type) "%s %-*s = ", #type, FCOLS
+#define FNAME(type) "%s %-*s = ", #type, f_opt ? 24 : FCOLS
 
 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;
 }
 
@@ -145,11 +151,12 @@ next_dbitem(void)
            db_item++;
        }
     }
-    printf("** %s\n", result);
+    if (result != 0)
+       printf("** %s\n", result);
     return result;
 }
 
-#ifdef NO_LEAKS
+#if NO_LEAKS
 static void
 free_dblist(void)
 {
@@ -164,7 +171,7 @@ free_dblist(void)
 #endif
 
 static void
-dumpit(NCURSES_CONST char *cap)
+dumpit(NCURSES_CONST char *cap, const char *show)
 {
     const char *str;
     int num;
@@ -173,7 +180,7 @@ dumpit(NCURSES_CONST char *cap)
        total_values++;
        total_s_values++;
        if (!q_opt) {
-           printf(FNAME(str), cap);
+           printf(FNAME(str), show ? show : cap);
            while (*str != 0) {
                int ch = UChar(*str++);
                switch (ch) {
@@ -226,14 +233,14 @@ dumpit(NCURSES_CONST char *cap)
        total_values++;
        total_n_values++;
        if (!q_opt) {
-           printf(FNAME(num), cap);
+           printf(FNAME(num), show ? show : cap);
            printf(" %d\n", num);
        }
     } else if ((num = tigetflag(cap)) >= 0) {
        total_values++;
        total_b_values++;
        if (!q_opt) {
-           printf(FNAME(flg), cap);
+           printf(FNAME(flg), show ? show : cap);
            printf("%s\n", num ? "true" : "false");
        }
     }
@@ -266,6 +273,10 @@ abcdefghijklmnopqrstuvwxyz_";
     if (!q_opt)
        printf("Terminal type \"%s\"\n", name);
     setupterm((NCURSES_CONST char *) name, 1, (int *) 0);
+    if (!q_opt) {
+       if (strcmp(name, ttytype))
+           printf("... actual \"%s\"\n", ttytype);
+    }
 
     for (length = 1; length <= MAX_FORCE; ++length) {
        /* set all digits to zeros */
@@ -280,7 +291,7 @@ abcdefghijklmnopqrstuvwxyz_";
                cap[j] = legal[item[j]];
            }
            cap[length] = '\0';
-           dumpit(cap);
+           dumpit(cap, NULL);
 
            k = length - 1;
            do {
@@ -306,6 +317,12 @@ abcdefghijklmnopqrstuvwxyz_";
     del_curterm(cur_term);
 }
 
+#if USE_CODE_LISTS
+#define fullname(type,n) f_opt ? type##fnames[n] : cap
+#else
+#define fullname(type,n) cap
+#endif
+
 static void
 demo_terminfo(char *name)
 {
@@ -321,48 +338,47 @@ demo_terminfo(char *name)
 
     if (b_opt) {
        for (n = 0;; ++n) {
-           cap = f_opt ? boolfnames[n] : my_boolcodes[n];
+           cap = my_boolcodes[n];
            if (cap == 0)
                break;
-           dumpit(cap);
+           dumpit(cap, fullname(bool, n));
        }
     }
 
     if (n_opt) {
        for (n = 0;; ++n) {
-           cap = f_opt ? numfnames[n] : my_numcodes[n];
+           cap = my_numcodes[n];
            if (cap == 0)
                break;
-           dumpit(cap);
+           dumpit(cap, fullname(num, n));
        }
     }
 
     if (s_opt) {
        for (n = 0;; ++n) {
-           cap = f_opt ? strfnames[n] : my_strcodes[n];
+           cap = my_strcodes[n];
            if (cap == 0)
                break;
-           dumpit(cap);
+           dumpit(cap, fullname(str, n));
        }
     }
 #ifdef NCURSES_VERSION
     if (x_opt && (my_blob == 0)) {
-       int mod;
        if (y_opt) {
 #if NCURSES_XNAMES
-           TERMTYPE *term = &(cur_term->type);
+           TERMTYPE *term = (TERMTYPE *) cur_term;
            if (term != 0
                && ((NUM_BOOLEANS(term) != BOOLCOUNT)
                    || (NUM_NUMBERS(term) != NUMCOUNT)
                    || (NUM_STRINGS(term) != STRCOUNT))) {
                for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
-                   dumpit(ExtBoolname(term, (int) n, boolnames));
+                   dumpit(ExtBoolname(term, (int) n, boolnames), NULL);
                }
                for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
-                   dumpit(ExtNumname(term, (int) n, numnames));
+                   dumpit(ExtNumname(term, (int) n, numnames), NULL);
                }
                for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
-                   dumpit(ExtStrname(term, (int) n, strnames));
+                   dumpit(ExtStrname(term, (int) n, strnames), NULL);
                }
            }
 #endif
@@ -374,6 +390,7 @@ demo_terminfo(char *name)
                "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
            };
            for (n = 0; n < SIZEOF(xterm_keys); ++n) {
+               int mod;
                for (mod = 0; mod < 8; ++mod) {
                    if (mod == 0) {
                        /* these happen to be standard - avoid duplicates */
@@ -384,11 +401,13 @@ demo_terminfo(char *name)
                            !strcmp(xterm_keys[n], "kRIT")) {
                            continue;
                        }
-                       sprintf(temp, "%.*s", 8, xterm_keys[n]);
+                       _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+                                   "%.*s", 8, xterm_keys[n]);
                    } else {
-                       sprintf(temp, "%.*s%d", 8, xterm_keys[n], mod);
+                       _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+                                   "%.*s%d", 8, xterm_keys[n], mod);
                    }
-                   dumpit(temp);
+                   dumpit(temp, NULL);
                }
            }
        }
@@ -411,6 +430,9 @@ typedef enum {
 static void
 parse_description(const char *input_name)
 {
+    static char empty[1] =
+    {0};
+
     FILE *fp;
     struct stat sb;
     size_t count_bools = 0;
@@ -442,10 +464,13 @@ parse_description(const char *input_name)
        failed("cannot allocate memory for input-file");
     }
 
-    if ((fp = fopen(input_name, "r")) == 0)
+    if ((fp = fopen(input_name, "r")) == 0) {
        failed("cannot open input-file");
-    len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
-    fclose(fp);
+    } else {
+       len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
+       my_blob[sb.st_size] = '\0';
+       fclose(fp);
+    }
 
     /*
      * First, get rid of comments and escaped newlines, as well as repeated
@@ -628,10 +653,10 @@ parse_description(const char *input_name)
                         * known order.
                         */
                        if (count_strs) {
-                           my_strvalues[count_strs] = "";
+                           my_strvalues[count_strs] = empty;
                            my_strcodes[count_strs++] = &my_blob[j];
                        } else if (count_nums) {
-                           my_numvalues[count_nums] = "";
+                           my_numvalues[count_nums] = empty;
                            my_numcodes[count_nums++] = &my_blob[j];
                        } else {
                            my_boolcodes[count_bools++] = &my_blob[j];
@@ -706,9 +731,8 @@ copy_code_list(NCURSES_CONST char *const *list)
 {
     int pass;
     size_t count;
-    size_t length = 0;
+    size_t length = 1;
     char **result = 0;
-    char *blob = 0;
     char *unused = 0;
 
     for (pass = 0; pass < 2; ++pass) {
@@ -718,12 +742,12 @@ copy_code_list(NCURSES_CONST char *const *list)
                length += chunk;
            } else {
                result[count] = unused;
-               strcpy(unused, list[count]);
+               _nc_STRCPY(unused, list[count], length);
                unused += chunk;
            }
        }
        if (pass == 0) {
-           blob = malloc(length);
+           char *blob = malloc(length);
            result = typeCalloc(char *, count + 1);
            unused = blob;
            if (blob == 0 || result == 0)
@@ -733,52 +757,68 @@ copy_code_list(NCURSES_CONST char *const *list)
 
     return result;
 }
+
+#if NO_LEAKS
+static void
+free_code_list(char **list)
+{
+    if (list) {
+       free(list[0]);
+       free(list);
+    }
+}
 #endif
+#endif /* USE_CODE_LISTS */
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
-       "Usage: demo_terminfo [options] [terminal]",
-       "",
-       "If no options are given, print all (boolean, numeric, string)",
-       "capabilities for the given terminal, using short names.",
-       "",
-       "Options:",
-       " -a       try all names, print capabilities found",
-       " -b       print boolean-capabilities",
-       " -d LIST  colon-separated list of databases to use",
-       " -e NAME  environment variable to set with -d option",
-       " -f       print full names",
-       " -i NAME  terminal description to use as names for \"-a\" option",
-       " -n       print numeric-capabilities",
-       " -q       quiet (prints only counts)",
-       " -r COUNT repeat for given count",
-       " -s       print string-capabilities",
+       "Usage: demo_terminfo [options] [terminal]"
+       ,""
+       ,"If no options are given, print all (boolean, numeric, string)"
+       ,"capabilities for the given terminal, using short names."
+       ,""
+       ,USAGE_COMMON
+       ,"Options:"
+       ," -a       try all names, print capabilities found"
+       ," -b       print boolean-capabilities"
+       ," -d LIST  colon-separated list of databases to use"
+       ," -e NAME  environment variable to set with -d option"
+       ," -f       print full names"
+       ," -i NAME  terminal description to use as names for \"-a\" option"
+       ," -n       print numeric-capabilities"
+       ," -q       quiet (prints only counts)"
+       ," -r COUNT repeat for given count"
+       ," -s       print string-capabilities"
 #ifdef NCURSES_VERSION
-       " -x       print extended capabilities",
-       " -y       direct-lookup names of extended capabilities",
+       ," -x       print extended capabilities"
+       ," -y       direct-lookup names of extended capabilities"
 #endif
     };
     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 ch;
     int n;
     int repeat;
     char *name;
     int r_opt = 1;
     char *input_name = 0;
 
-    while ((n = getopt(argc, argv, "abd:e:fi:nqr:sxy")) != -1) {
-       switch (n) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "abd:e:fi:nqr:sxy")) != -1) {
+       switch (ch) {
        case 'a':
            a_opt = TRUE;
            break;
@@ -805,27 +845,32 @@ main(int argc, char *argv[])
            break;
        case 'r':
            if ((r_opt = atoi(optarg)) <= 0)
-               usage();
+               usage(FALSE);
            break;
        case 's':
            s_opt = TRUE;
            break;
-#ifdef NCURSES_VERSION
        case 'x':
+#ifdef NCURSES_VERSION
            x_opt = TRUE;
+#endif
            break;
+#ifdef NCURSES_VERSION
        case 'y':
            y_opt = TRUE;
            x_opt = TRUE;
            break;
 #endif
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           break;
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
 
-#if NCURSES_XNAMES
+#if HAVE_USE_EXTENDED_NAMES
     use_extended_names(x_opt);
 #endif
 
@@ -879,29 +924,42 @@ main(int argc, char *argv[])
        }
     }
 
-    printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
-          total_values, total_b_values, total_n_values, total_s_values);
+#define PLURAL(n) n, (n != 1) ? "s" : ""
+    printf("%ld value%s (%ld boolean%s, %ld number%s, %ld string%s)\n",
+          PLURAL(total_values),
+          PLURAL(total_b_values),
+          PLURAL(total_n_values),
+          PLURAL(total_s_values));
 
-#ifdef NO_LEAKS
+#if NO_LEAKS
     free_dblist();
-    if (my_blob != 0) {
-       free(my_blob);
-       free(my_boolcodes);
-       free(my_numcodes);
-       free(my_numvalues);
-       free(my_strcodes);
-       free(my_strvalues);
+    if (input_name != 0) {
+       if (my_blob != 0) {
+           free(my_blob);
+           free(my_boolcodes);
+           free(my_numcodes);
+           free(my_numvalues);
+           free(my_strcodes);
+           free(my_strvalues);
+       }
+    }
+#if USE_CODE_LISTS
+    else {
+       free_code_list(my_boolcodes);
+       free_code_list(my_numcodes);
+       free_code_list(my_strcodes);
     }
 #endif
+#endif /* NO_LEAKS */
 
     ExitProgram(EXIT_SUCCESS);
 }
 
 #else /* !HAVE_TIGETSTR */
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(void)
 {
-    printf("This program requires the terminfo functions such as tigetstr\n");
+    failed("This program requires the terminfo functions such as tigetstr");
     ExitProgram(EXIT_FAILURE);
 }
 #endif /* HAVE_TIGETSTR */