]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/list_keys.c
ncurses 6.4 - patch 20230624
[ncurses.git] / test / list_keys.c
index e3077de4de3c8e2c31bd2baf699f36cae7871f9f..96b02e14f355e29fcb448dc6ce003720a936dd0a 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright 2018-2020,2021 Thomas E. Dickey                                *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
  * Copyright 2016,2017 Free Software Foundation, Inc.                       *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
@@ -27,7 +27,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: list_keys.c,v 1.27 2021/03/27 23:41:21 tom Exp $
+ * $Id: list_keys.c,v 1.32 2023/06/24 13:57:11 tom Exp $
  *
  * Author: Thomas E Dickey
  *
@@ -71,6 +71,13 @@ typedef struct {
 #define Type(n) list[n].type
 #define Name(n) list[n].name
 
+static void
+failed(const char *msg)
+{
+    perror(msg);
+    ExitProgram(EXIT_FAILURE);
+}
+
 static const char *
 full_name(const char *name)
 {
@@ -338,8 +345,11 @@ list_keys(TERMINAL **terms, int count)
        widths1 = (int) strlen(modifier);
 
     for (k = 0; k < count; ++k) {
+       char *value;
        set_curterm(terms[k]);
-       check = (int) strlen(termname());
+       if ((value = termname()) == NULL)
+           failed("termname");
+       check = (int) strlen(value);
        if (widths2 < check)
            widths2 = check;
     }
@@ -417,37 +427,41 @@ list_keys(TERMINAL **terms, int count)
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
-       "Usage: list_keys [options] [terminal [terminal2 [...]]]",
-       "",
-       "Print capabilities for terminal special keys.",
-       "",
-       "Options:",
-       " -f       print full names",
-       " -m       print modifier-column for shift/control keys",
-       " -t       print result as CSV table",
+       "Usage: list_keys [options] [terminal [terminal2 [...]]]"
+       ,""
+       ,"Print capabilities for terminal special keys."
+       ,""
+       ,USAGE_COMMON
+       ,"Options:"
+       ," -f       print full names"
+       ," -m       print modifier-column for shift/control keys"
+       ," -t       print result as CSV table"
 #ifdef NCURSES_VERSION
-       " -x       print extended capabilities",
+       ," -x       print 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 n;
+    int ch;
     TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
 
-    while ((n = getopt(argc, argv, "fmtx")) != -1) {
-       switch (n) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "fmtx")) != -1) {
+       switch (ch) {
        case 'f':
            f_opt = TRUE;
            break;
@@ -462,9 +476,12 @@ main(int argc, char *argv[])
            x_opt = TRUE;
            break;
 #endif
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           break;
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
 
@@ -475,6 +492,7 @@ main(int argc, char *argv[])
     if (optind < argc) {
        int found = 0;
        int status;
+       int n;
        for (n = optind; n < argc; ++n) {
            setupterm((NCURSES_CONST char *) argv[n], 1, &status);
            if (status > 0 && cur_term != 0) {
@@ -496,7 +514,7 @@ main(int argc, char *argv[])
 
 #else
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(void)
 {
     printf("This program requires the terminfo arrays\n");
     ExitProgram(EXIT_FAILURE);
@@ -504,7 +522,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 #endif
 #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");
     ExitProgram(EXIT_FAILURE);