HAMMER Utilities: Sync with 60F
[dragonfly.git] / contrib / ncurses-5.4 / test / demo_defkey.c
blobab120636b7ff6dd82206f51667a068a912c02513
1 /*
2 * $Id: demo_defkey.c,v 1.13 2004/01/04 00:01:13 tom Exp $
4 * Demonstrate the define_key() function.
5 * Thomas Dickey - 2002/11/23
6 */
8 #include <test.priv.h>
10 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
12 #include <term.h>
14 #define MY_LOGFILE "demo_defkey.log"
17 * Log the most recently-written line to our logfile
19 static void
20 log_last_line(WINDOW *win)
22 FILE *fp;
23 int y, x, n;
24 char temp[256];
26 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
27 getyx(win, y, x);
28 wmove(win, y - 1, 0);
29 n = winnstr(win, temp, sizeof(temp));
30 while (n-- > 0) {
31 if (isspace(UChar(temp[n])))
32 temp[n] = '\0';
33 else
34 break;
36 wmove(win, y, x);
37 fprintf(fp, "%s\n", temp);
38 fclose(fp);
43 * Convert a character to visible form.
45 static char *
46 visichar(int ch)
48 static char temp[10];
50 ch = UChar(ch);
51 if (ch == '\\') {
52 strcpy(temp, "\\\\");
53 } else if (ch == '\033') {
54 strcpy(temp, "\\E");
55 } else if (ch < ' ') {
56 sprintf(temp, "\\%03o", ch);
57 } else if (ch >= 127) {
58 sprintf(temp, "\\%03o", ch);
59 } else {
60 sprintf(temp, "%c", ch);
62 return temp;
66 * Convert a string to visible form.
68 static char *
69 visible(const char *string)
71 char *result = 0;
72 unsigned need = 1;
73 int pass;
74 int n;
76 if (string != 0 && *string != '\0') {
77 for (pass = 0; pass < 2; ++pass) {
78 for (n = 0; string[n] != '\0'; ++n) {
79 char temp[80];
80 strcpy(temp, visichar(string[n]));
81 if (pass)
82 strcat(result, temp);
83 else
84 need += strlen(temp);
86 if (!pass)
87 result = (char *) calloc(need, 1);
89 } else {
90 result = (char *) calloc(1, 1);
92 return result;
95 static void
96 really_define_key(WINDOW *win, const char *new_string, int code)
98 int rc;
99 const char *code_name = keyname(code);
100 char *old_string;
101 char *vis_string = 0;
102 char temp[80];
104 if (code_name == 0) {
105 sprintf(temp, "Keycode %d", code);
106 code_name = temp;
109 if ((old_string = keybound(code, 0)) != 0) {
110 wprintw(win, "%s is %s\n",
111 code_name,
112 vis_string = visible(old_string));
113 } else {
114 wprintw(win, "%s is not bound\n",
115 code_name);
117 log_last_line(win);
118 if (vis_string != 0) {
119 free(vis_string);
120 vis_string = 0;
123 vis_string = visible(new_string);
124 if ((rc = key_defined(new_string)) > 0) {
125 wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
126 log_last_line(win);
127 } else if (new_string != 0 && rc < 0) {
128 wprintw(win, "%s conflicts with longer strings\n", vis_string);
129 log_last_line(win);
131 rc = define_key(new_string, code);
132 if (rc == ERR) {
133 wprintw(win, "%s unchanged\n", code_name);
134 log_last_line(win);
135 } else if (new_string != 0) {
136 wprintw(win, "%s is now bound to %s\n",
137 vis_string,
138 code_name);
139 log_last_line(win);
140 } else if (old_string != 0) {
141 wprintw(win, "%s deleted\n", code_name);
142 log_last_line(win);
144 if (vis_string != 0 && *vis_string != 0)
145 free(vis_string);
146 if (old_string != 0)
147 free(old_string);
150 static void
151 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
153 char *value = tigetstr(name);
155 if (value != 0) {
156 const char *prefix = 0;
157 char temp[BUFSIZ];
159 if (!strncmp(value, "\033[", 2)) {
160 prefix = "\033O";
161 } else if (!strncmp(value, "\033O", 2)) {
162 prefix = "\033[";
164 if (prefix != 0) {
165 sprintf(temp, "%s%s", prefix, value + 2);
166 really_define_key(win, temp, code);
171 static void
172 redefine(WINDOW *win, char *string, int code)
174 really_define_key(win, string, code);
177 static void
178 remove_definition(WINDOW *win, int code)
180 really_define_key(win, 0, code);
184 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
186 char *fkeys[12];
187 int n;
188 int ch;
189 WINDOW *win;
191 unlink(MY_LOGFILE);
193 initscr();
194 (void) cbreak(); /* take input chars one at a time, no wait for \n */
195 (void) noecho(); /* don't echo input */
197 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
198 printw("adds duplicate definitions for cursor application and normal modes,\n");
199 printw("and removes any definitions for the mini keypad. Type any of those:\n");
200 refresh();
202 win = newwin(LINES - 3, COLS, 3, 0);
203 scrollok(win, TRUE);
204 keypad(win, TRUE);
205 wmove(win, 0, 0);
207 /* we do the define_key() calls after keypad(), since the first call to
208 * keypad() initializes the corresponding data.
210 for (n = 0; n < 12; ++n) {
211 char name[10];
212 sprintf(name, "kf%d", n + 1);
213 fkeys[n] = tigetstr(name);
215 for (n = 0; n < 12; ++n) {
216 redefine(win, fkeys[11 - n], KEY_F(n + 1));
219 duplicate(win, "kcub1", KEY_LEFT);
220 duplicate(win, "kcuu1", KEY_UP);
221 duplicate(win, "kcud1", KEY_DOWN);
222 duplicate(win, "kcuf1", KEY_RIGHT);
224 remove_definition(win, KEY_A1);
225 remove_definition(win, KEY_A3);
226 remove_definition(win, KEY_B2);
227 remove_definition(win, KEY_C1);
228 remove_definition(win, KEY_C3);
230 really_define_key(win, "\033O", 1023);
232 while ((ch = wgetch(win)) != ERR) {
233 const char *name = keyname(ch);
234 wprintw(win, "Keycode %d, name %s\n",
236 name != 0 ? name : "<null>");
237 log_last_line(win);
238 wclrtoeol(win);
240 endwin();
241 return EXIT_SUCCESS;
243 #else
245 main(void)
247 printf("This program requires the ncurses library\n");
248 ExitProgram(EXIT_FAILURE);
250 #endif