]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/demo_keyok.c
ncurses 6.4 - patch 20230418
[ncurses.git] / test / demo_keyok.c
index 670bd54f2649d5ffa3d283922bda816f2b6eec11..8f4ae906bd061391a9cc615b17e2a86b7ff94723 100644 (file)
@@ -1,5 +1,33 @@
+/****************************************************************************
+ * Copyright 2020,2022 Thomas E. Dickey                                     *
+ * Copyright 2002-2006,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            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
 /*
- * $Id: demo_keyok.c,v 1.3 2003/05/17 23:18:34 tom Exp $
+ * $Id: demo_keyok.c,v 1.9 2022/12/10 23:31:31 tom Exp $
  *
  * Demonstrate the keyok() function.
  * Thomas Dickey - 2002/11/23
 #include <test.priv.h>
 
 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
+static void
+usage(int ok)
+{
+    static const char *msg[] =
+    {
+       "Usage: demo_keyok [options]"
+       ,""
+       ,USAGE_COMMON
+    };
+    size_t n;
+
+    for (n = 0; n < SIZEOF(msg); n++)
+       fprintf(stderr, "%s\n", msg[n]);
+
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
+
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(int argc, char *argv[])
 {
     int lastch = ERR;
+    int prior = ERR;
     int ch;
     WINDOW *win;
 
+    while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
+       switch (ch) {
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
+       default:
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
+       }
+    }
+    if (optind < argc)
+       usage(FALSE);
+
+    setlocale(LC_ALL, "");
     initscr();
     (void) cbreak();           /* take input chars one at a time, no wait for \n */
     (void) noecho();           /* don't echo input */
@@ -30,6 +93,9 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 
     while ((ch = wgetch(win)) != ERR) {
        const char *name = keyname(ch);
+       if (ch == ESCAPE && prior == ch)
+           break;
+       prior = ch;
        wprintw(win, "Keycode %d, name %s\n",
                ch,
                name != 0 ? name : "<null>");
@@ -43,7 +109,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        }
     }
     endwin();
-    return EXIT_SUCCESS;
+    ExitProgram(EXIT_SUCCESS);
 }
 #else
 int