blob: 58085304c90cac865a907926338957d21763876c [file] [log] [blame]
micky3879b9f5e72025-07-08 18:04:53 -04001/****************************************************************************
2 * Copyright 2022 Thomas E. Dickey *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28/*
29 * $Id: test_unget_wch.c,v 1.4 2022/12/10 23:31:31 tom Exp $
30 *
31 * Demonstrate the unget_wch and unget functions.
32 */
33
34#include <test.priv.h>
35
36#if USE_WIDEC_SUPPORT && HAVE_UNGET_WCH
37static void
38usage(int ok)
39{
40 static const char *msg[] =
41 {
42 "Usage: test_unget_wch [options]"
43 ,""
44 ,USAGE_COMMON
45 };
46 size_t n;
47
48 for (n = 0; n < SIZEOF(msg); n++)
49 fprintf(stderr, "%s\n", msg[n]);
50
51 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
52}
53/* *INDENT-OFF* */
54VERSION_COMMON()
55/* *INDENT-ON* */
56
57int
58main(int argc, char *argv[])
59{
60 int step = 0;
61 int ch;
62
63 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
64 switch (ch) {
65 case OPTS_VERSION:
66 show_version(argv);
67 ExitProgram(EXIT_SUCCESS);
68 default:
69 usage(ch == OPTS_USAGE);
70 /* NOTREACHED */
71 }
72 }
73 if (optind < argc)
74 usage(FALSE);
75
76 setlocale(LC_ALL, "");
77 initscr();
78 keypad(stdscr, TRUE);
79 cbreak();
80 noecho();
81 scrollok(stdscr, TRUE);
82
83 for (;;) {
84 wint_t widechar;
85 int rc = get_wch(&widechar);
86 if (rc == KEY_CODE_YES) {
87 printw("KEY[%d] %s\n", ++step, keyname((int) widechar));
88 ungetch((int) widechar);
89 printw("...[%d] %s\n", step, keyname(getch()));
90 } else if (widechar == QUIT || widechar == ESCAPE) {
91 break;
92 } else {
93 printw("CHR[%d] %s\n", ++step, key_name((wchar_t) widechar));
94 unget_wch((wchar_t) widechar);
95 rc = get_wch(&widechar);
96 printw("%s[%d] %s\n",
97 ((rc == KEY_CODE_YES)
98 ? "???"
99 : "..."),
100 step, key_name((wchar_t) widechar));
101 }
102 }
103
104 endwin();
105
106 ExitProgram(EXIT_SUCCESS);
107}
108
109#else
110int
111main(void)
112{
113 printf("This program requires the wide-ncurses library\n");
114 ExitProgram(EXIT_FAILURE);
115}
116#endif