Mon Jul 6 23:33:38 2020 UTC ()
Pads are not to be automatically refreshed on input.

X/Open Curses says in the documentation for newpad():

  Automatic refreshes of pads (e.g., from scrolling or echoing of
  input) do not occur.

And in the documentation for get*():

  If the current or specified window is not a pad, and it has been
  moved or modified since the last refresh operation, then it will be
  refreshed before another character is read.

>From Michael Forney in PR lib/55457


(uwe)
diff -r1.23 -r1.24 src/lib/libcurses/get_wch.c
diff -r1.74 -r1.75 src/lib/libcurses/getch.c

cvs diff -r1.23 -r1.24 src/lib/libcurses/get_wch.c (switch to unified diff)

--- src/lib/libcurses/get_wch.c 2019/06/09 07:40:14 1.23
+++ src/lib/libcurses/get_wch.c 2020/07/06 23:33:38 1.24
@@ -1,666 +1,666 @@ @@ -1,666 +1,666 @@
1/* $NetBSD: get_wch.c,v 1.23 2019/06/09 07:40:14 blymn Exp $ */ 1/* $NetBSD: get_wch.c,v 1.24 2020/07/06 23:33:38 uwe Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005 The NetBSD Foundation Inc. 4 * Copyright (c) 2005 The NetBSD Foundation Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from code donated to the NetBSD Foundation 7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>. 8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9 * 9 *
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the NetBSD Foundation nor the names of its 19 * 3. Neither the name of the NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived 20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission. 21 * from this software without specific prior written permission.
22 * 22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 */ 35 */
36 36
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38#ifndef lint 38#ifndef lint
39__RCSID("$NetBSD: get_wch.c,v 1.23 2019/06/09 07:40:14 blymn Exp $"); 39__RCSID("$NetBSD: get_wch.c,v 1.24 2020/07/06 23:33:38 uwe Exp $");
40#endif /* not lint */ 40#endif /* not lint */
41 41
42#include <errno.h> 42#include <errno.h>
43#include <string.h> 43#include <string.h>
44#include <stdlib.h> 44#include <stdlib.h>
45#include <unistd.h> 45#include <unistd.h>
46#include <stdio.h> 46#include <stdio.h>
47#include "curses.h" 47#include "curses.h"
48#include "curses_private.h" 48#include "curses_private.h"
49#include "keymap.h" 49#include "keymap.h"
50 50
51static short wstate; /* state of the wcinkey function */ 51static short wstate; /* state of the wcinkey function */
52extern short _cursesi_state; /* storage declared in getch.c */ 52extern short _cursesi_state; /* storage declared in getch.c */
53 53
54/* prototypes for private functions */ 54/* prototypes for private functions */
55static int inkey(wchar_t *wc, int to, int delay); 55static int inkey(wchar_t *wc, int to, int delay);
56static wint_t __fgetwc_resize(FILE *infd, bool *resized); 56static wint_t __fgetwc_resize(FILE *infd, bool *resized);
57 57
58/* 58/*
59 * __init_get_wch - initialise all the pointers & structures needed to make 59 * __init_get_wch - initialise all the pointers & structures needed to make
60 * get_wch work in keypad mode. 60 * get_wch work in keypad mode.
61 * 61 *
62 */ 62 */
63void 63void
64__init_get_wch(SCREEN *screen) 64__init_get_wch(SCREEN *screen)
65{ 65{
66 wstate = INKEY_NORM; 66 wstate = INKEY_NORM;
67 memset(&screen->cbuf, 0, sizeof(screen->cbuf)); 67 memset(&screen->cbuf, 0, sizeof(screen->cbuf));
68 screen->cbuf_head = screen->cbuf_tail = screen->cbuf_cur = 0; 68 screen->cbuf_head = screen->cbuf_tail = screen->cbuf_cur = 0;
69} 69}
70 70
71 71
72/* 72/*
73 * inkey - do the work to process keyboard input, check for multi-key 73 * inkey - do the work to process keyboard input, check for multi-key
74 * sequences and return the appropriate symbol if we get a match. 74 * sequences and return the appropriate symbol if we get a match.
75 * 75 *
76 */ 76 */
77static int 77static int
78inkey(wchar_t *wc, int to, int delay) 78inkey(wchar_t *wc, int to, int delay)
79{ 79{
80 wchar_t k = 0; 80 wchar_t k = 0;
81 int c, mapping, ret = 0; 81 int c, mapping, ret = 0;
82 size_t mlen = 0; 82 size_t mlen = 0;
83 keymap_t *current = _cursesi_screen->base_keymap; 83 keymap_t *current = _cursesi_screen->base_keymap;
84 FILE *infd = _cursesi_screen->infd; 84 FILE *infd = _cursesi_screen->infd;
85 int *start = &_cursesi_screen->cbuf_head, 85 int *start = &_cursesi_screen->cbuf_head,
86 *working = &_cursesi_screen->cbuf_cur, 86 *working = &_cursesi_screen->cbuf_cur,
87 *end = &_cursesi_screen->cbuf_tail; 87 *end = &_cursesi_screen->cbuf_tail;
88 char *inbuf = &_cursesi_screen->cbuf[ 0 ]; 88 char *inbuf = &_cursesi_screen->cbuf[ 0 ];
89 89
90#ifdef DEBUG 90#ifdef DEBUG
91 __CTRACE(__CTRACE_INPUT, "inkey (%p, %d, %d)\n", wc, to, delay); 91 __CTRACE(__CTRACE_INPUT, "inkey (%p, %d, %d)\n", wc, to, delay);
92#endif 92#endif
93 for (;;) { /* loop until we get a complete key sequence */ 93 for (;;) { /* loop until we get a complete key sequence */
94 if (wstate == INKEY_NORM) { 94 if (wstate == INKEY_NORM) {
95 if (delay && __timeout(delay) == ERR) 95 if (delay && __timeout(delay) == ERR)
96 return ERR; 96 return ERR;
97 c = __fgetc_resize(infd); 97 c = __fgetc_resize(infd);
98 if (c == ERR || c == KEY_RESIZE) { 98 if (c == ERR || c == KEY_RESIZE) {
99 clearerr(infd); 99 clearerr(infd);
100 return c; 100 return c;
101 } 101 }
102 102
103 if (delay && (__notimeout() == ERR)) 103 if (delay && (__notimeout() == ERR))
104 return ERR; 104 return ERR;
105 105
106 k = (wchar_t)c; 106 k = (wchar_t)c;
107#ifdef DEBUG 107#ifdef DEBUG
108 __CTRACE(__CTRACE_INPUT, 108 __CTRACE(__CTRACE_INPUT,
109 "inkey (wstate normal) got '%s'\n", unctrl(k)); 109 "inkey (wstate normal) got '%s'\n", unctrl(k));
110#endif 110#endif
111 111
112 inbuf[*end] = k; 112 inbuf[*end] = k;
113 *end = (*end + 1) % MAX_CBUF_SIZE; 113 *end = (*end + 1) % MAX_CBUF_SIZE;
114 *working = *start; 114 *working = *start;
115 wstate = INKEY_ASSEMBLING; /* go to assembling state */ 115 wstate = INKEY_ASSEMBLING; /* go to assembling state */
116#ifdef DEBUG 116#ifdef DEBUG
117 __CTRACE(__CTRACE_INPUT, 117 __CTRACE(__CTRACE_INPUT,
118 "inkey: NORM=>ASSEMBLING: start(%d), " 118 "inkey: NORM=>ASSEMBLING: start(%d), "
119 "current(%d), end(%d)\n", *start, *working, *end); 119 "current(%d), end(%d)\n", *start, *working, *end);
120#endif /* DEBUG */ 120#endif /* DEBUG */
121 } else if (wstate == INKEY_BACKOUT) { 121 } else if (wstate == INKEY_BACKOUT) {
122 k = inbuf[*working]; 122 k = inbuf[*working];
123 *working = (*working + 1) % MAX_CBUF_SIZE; 123 *working = (*working + 1) % MAX_CBUF_SIZE;
124 if (*working == *end) { /* see if run out of keys */ 124 if (*working == *end) { /* see if run out of keys */
125 /* if so, switch to assembling */ 125 /* if so, switch to assembling */
126 wstate = INKEY_ASSEMBLING; 126 wstate = INKEY_ASSEMBLING;
127#ifdef DEBUG 127#ifdef DEBUG
128 __CTRACE(__CTRACE_INPUT, 128 __CTRACE(__CTRACE_INPUT,
129 "inkey: BACKOUT=>ASSEMBLING, start(%d), " 129 "inkey: BACKOUT=>ASSEMBLING, start(%d), "
130 "current(%d), end(%d)\n", 130 "current(%d), end(%d)\n",
131 *start, *working, *end); 131 *start, *working, *end);
132#endif /* DEBUG */ 132#endif /* DEBUG */
133 } 133 }
134 } else if (wstate == INKEY_ASSEMBLING) { 134 } else if (wstate == INKEY_ASSEMBLING) {
135 /* assembling a key sequence */ 135 /* assembling a key sequence */
136 if (delay) { 136 if (delay) {
137 if (__timeout(to ? (ESCDELAY / 100) : delay) 137 if (__timeout(to ? (ESCDELAY / 100) : delay)
138 == ERR) 138 == ERR)
139 return ERR; 139 return ERR;
140 } else { 140 } else {
141 if (to && (__timeout(ESCDELAY / 100) == ERR)) 141 if (to && (__timeout(ESCDELAY / 100) == ERR))
142 return ERR; 142 return ERR;
143 } 143 }
144 144
145 c = __fgetc_resize(infd); 145 c = __fgetc_resize(infd);
146 if (ferror(infd)) { 146 if (ferror(infd)) {
147 clearerr(infd); 147 clearerr(infd);
148 return c; 148 return c;
149 } 149 }
150 150
151 if ((to || delay) && (__notimeout() == ERR)) 151 if ((to || delay) && (__notimeout() == ERR))
152 return ERR; 152 return ERR;
153 153
154 k = (wchar_t)c; 154 k = (wchar_t)c;
155#ifdef DEBUG 155#ifdef DEBUG
156 __CTRACE(__CTRACE_INPUT, 156 __CTRACE(__CTRACE_INPUT,
157 "inkey (wstate assembling) got '%s'\n", unctrl(k)); 157 "inkey (wstate assembling) got '%s'\n", unctrl(k));
158#endif /* DEBUG */ 158#endif /* DEBUG */
159 if (feof(infd)) { /* inter-char T/O, start backout */ 159 if (feof(infd)) { /* inter-char T/O, start backout */
160 clearerr(infd); 160 clearerr(infd);
161 if (*start == *end) 161 if (*start == *end)
162 /* no chars in the buffer, restart */ 162 /* no chars in the buffer, restart */
163 continue; 163 continue;
164 164
165 k = inbuf[*start]; 165 k = inbuf[*start];
166 wstate = INKEY_TIMEOUT; 166 wstate = INKEY_TIMEOUT;
167#ifdef DEBUG 167#ifdef DEBUG
168 __CTRACE(__CTRACE_INPUT, 168 __CTRACE(__CTRACE_INPUT,
169 "inkey: ASSEMBLING=>TIMEOUT, start(%d), " 169 "inkey: ASSEMBLING=>TIMEOUT, start(%d), "
170 "current(%d), end(%d)\n", 170 "current(%d), end(%d)\n",
171 *start, *working, *end); 171 *start, *working, *end);
172#endif /* DEBUG */ 172#endif /* DEBUG */
173 } else { 173 } else {
174 inbuf[*end] = k; 174 inbuf[*end] = k;
175 *working = *end; 175 *working = *end;
176 *end = (*end + 1) % MAX_CBUF_SIZE; 176 *end = (*end + 1) % MAX_CBUF_SIZE;
177#ifdef DEBUG 177#ifdef DEBUG
178 __CTRACE(__CTRACE_INPUT, 178 __CTRACE(__CTRACE_INPUT,
179 "inkey: ASSEMBLING: start(%d), " 179 "inkey: ASSEMBLING: start(%d), "
180 "current(%d), end(%d)", 180 "current(%d), end(%d)",
181 *start, *working, *end); 181 *start, *working, *end);
182#endif /* DEBUG */ 182#endif /* DEBUG */
183 } 183 }
184 } else if (wstate == INKEY_WCASSEMBLING) { 184 } else if (wstate == INKEY_WCASSEMBLING) {
185 /* assembling a wide-char sequence */ 185 /* assembling a wide-char sequence */
186 if (delay) { 186 if (delay) {
187 if (__timeout(to ? (ESCDELAY / 100) : delay) 187 if (__timeout(to ? (ESCDELAY / 100) : delay)
188 == ERR) 188 == ERR)
189 return ERR; 189 return ERR;
190 } else { 190 } else {
191 if (to && (__timeout(ESCDELAY / 100) == ERR)) 191 if (to && (__timeout(ESCDELAY / 100) == ERR))
192 return ERR; 192 return ERR;
193 } 193 }
194 194
195 c = __fgetc_resize(infd); 195 c = __fgetc_resize(infd);
196 if (ferror(infd)) { 196 if (ferror(infd)) {
197 clearerr(infd); 197 clearerr(infd);
198 return c; 198 return c;
199 } 199 }
200 200
201 if ((to || delay) && (__notimeout() == ERR)) 201 if ((to || delay) && (__notimeout() == ERR))
202 return ERR; 202 return ERR;
203 203
204 k = (wchar_t)c; 204 k = (wchar_t)c;
205#ifdef DEBUG 205#ifdef DEBUG
206 __CTRACE(__CTRACE_INPUT, 206 __CTRACE(__CTRACE_INPUT,
207 "inkey (wstate wcassembling) got '%s'\n", 207 "inkey (wstate wcassembling) got '%s'\n",
208 unctrl(k)); 208 unctrl(k));
209#endif 209#endif
210 if (feof(infd)) { /* inter-char T/O, start backout */ 210 if (feof(infd)) { /* inter-char T/O, start backout */
211 clearerr(infd); 211 clearerr(infd);
212 if (*start == *end) 212 if (*start == *end)
213 /* no chars in the buffer, restart */ 213 /* no chars in the buffer, restart */
214 continue; 214 continue;
215 215
216 *wc = inbuf[*start]; 216 *wc = inbuf[*start];
217 *working = *start = (*start +1) % MAX_CBUF_SIZE; 217 *working = *start = (*start +1) % MAX_CBUF_SIZE;
218 if (*start == *end) { 218 if (*start == *end) {
219 _cursesi_state = wstate = INKEY_NORM; 219 _cursesi_state = wstate = INKEY_NORM;
220#ifdef DEBUG 220#ifdef DEBUG
221 __CTRACE(__CTRACE_INPUT, 221 __CTRACE(__CTRACE_INPUT,
222 "inkey: WCASSEMBLING=>NORM, " 222 "inkey: WCASSEMBLING=>NORM, "
223 "start(%d), current(%d), end(%d)", 223 "start(%d), current(%d), end(%d)",
224 *start, *working, *end); 224 *start, *working, *end);
225#endif /* DEBUG */ 225#endif /* DEBUG */
226 } else { 226 } else {
227 _cursesi_state = wstate = INKEY_BACKOUT; 227 _cursesi_state = wstate = INKEY_BACKOUT;
228#ifdef DEBUG 228#ifdef DEBUG
229 __CTRACE(__CTRACE_INPUT, 229 __CTRACE(__CTRACE_INPUT,
230 "inkey: WCASSEMBLING=>BACKOUT, " 230 "inkey: WCASSEMBLING=>BACKOUT, "
231 "start(%d), current(%d), end(%d)", 231 "start(%d), current(%d), end(%d)",
232 *start, *working, *end); 232 *start, *working, *end);
233#endif /* DEBUG */ 233#endif /* DEBUG */
234 } 234 }
235 return OK; 235 return OK;
236 } else { 236 } else {
237 /* assembling wide characters */ 237 /* assembling wide characters */
238 inbuf[*end] = k; 238 inbuf[*end] = k;
239 *working = *end; 239 *working = *end;
240 *end = (*end + 1) % MAX_CBUF_SIZE; 240 *end = (*end + 1) % MAX_CBUF_SIZE;
241#ifdef DEBUG 241#ifdef DEBUG
242 __CTRACE(__CTRACE_INPUT, 242 __CTRACE(__CTRACE_INPUT,
243 "inkey: WCASSEMBLING[head(%d), " 243 "inkey: WCASSEMBLING[head(%d), "
244 "urrent(%d), tail(%d)]\n", 244 "urrent(%d), tail(%d)]\n",
245 *start, *working, *end); 245 *start, *working, *end);
246#endif /* DEBUG */ 246#endif /* DEBUG */
247 ret = (int)mbrtowc(wc, inbuf + (*working), 1, 247 ret = (int)mbrtowc(wc, inbuf + (*working), 1,
248 &_cursesi_screen->sp); 248 &_cursesi_screen->sp);
249#ifdef DEBUG 249#ifdef DEBUG
250 __CTRACE(__CTRACE_INPUT, 250 __CTRACE(__CTRACE_INPUT,
251 "inkey: mbrtowc returns %d, wc(%x)\n", 251 "inkey: mbrtowc returns %d, wc(%x)\n",
252 ret, *wc); 252 ret, *wc);
253#endif /* DEBUG */ 253#endif /* DEBUG */
254 if (ret == -2) { 254 if (ret == -2) {
255 *working = (*working+1) % MAX_CBUF_SIZE; 255 *working = (*working+1) % MAX_CBUF_SIZE;
256 continue; 256 continue;
257 } 257 }
258 if ( ret == 0 ) 258 if ( ret == 0 )
259 ret = 1; 259 ret = 1;
260 if ( ret == -1 ) { 260 if ( ret == -1 ) {
261 /* return the 1st character we know */ 261 /* return the 1st character we know */
262 *wc = inbuf[*start]; 262 *wc = inbuf[*start];
263 *working = *start = (*start + 1) % MAX_CBUF_SIZE; 263 *working = *start = (*start + 1) % MAX_CBUF_SIZE;
264#ifdef DEBUG 264#ifdef DEBUG
265 __CTRACE(__CTRACE_INPUT, 265 __CTRACE(__CTRACE_INPUT,
266 "inkey: Invalid wide char(%x) " 266 "inkey: Invalid wide char(%x) "
267 "[head(%d), current(%d), " 267 "[head(%d), current(%d), "
268 "tail(%d)]\n", 268 "tail(%d)]\n",
269 *wc, *start, *working, *end); 269 *wc, *start, *working, *end);
270#endif /* DEBUG */ 270#endif /* DEBUG */
271 } else { /* > 0 */ 271 } else { /* > 0 */
272 /* return the wide character */ 272 /* return the wide character */
273 *start = *working 273 *start = *working
274 = (*working + ret)%MAX_CBUF_SIZE; 274 = (*working + ret)%MAX_CBUF_SIZE;
275#ifdef DEBUG 275#ifdef DEBUG
276 __CTRACE(__CTRACE_INPUT, 276 __CTRACE(__CTRACE_INPUT,
277 "inkey: Wide char found(%x) " 277 "inkey: Wide char found(%x) "
278 "[head(%d), current(%d), " 278 "[head(%d), current(%d), "
279 "tail(%d)]\n", 279 "tail(%d)]\n",
280 *wc, *start, *working, *end); 280 *wc, *start, *working, *end);
281#endif /* DEBUG */ 281#endif /* DEBUG */
282 } 282 }
283 283
284 if (*start == *end) { 284 if (*start == *end) {
285 /* only one char processed */ 285 /* only one char processed */
286 _cursesi_state = wstate = INKEY_NORM; 286 _cursesi_state = wstate = INKEY_NORM;
287#ifdef DEBUG 287#ifdef DEBUG
288 __CTRACE(__CTRACE_INPUT, 288 __CTRACE(__CTRACE_INPUT,
289 "inkey: WCASSEMBLING=>NORM, " 289 "inkey: WCASSEMBLING=>NORM, "
290 "start(%d), current(%d), end(%d)", 290 "start(%d), current(%d), end(%d)",
291 *start, *working, *end); 291 *start, *working, *end);
292#endif /* DEBUG */ 292#endif /* DEBUG */
293 } else { 293 } else {
294 /* otherwise we must have more than 294 /* otherwise we must have more than
295 * one char to backout */ 295 * one char to backout */
296 _cursesi_state = wstate = INKEY_BACKOUT; 296 _cursesi_state = wstate = INKEY_BACKOUT;
297#ifdef DEBUG 297#ifdef DEBUG
298 __CTRACE(__CTRACE_INPUT, 298 __CTRACE(__CTRACE_INPUT,
299 "inkey: WCASSEMBLING=>BACKOUT, " 299 "inkey: WCASSEMBLING=>BACKOUT, "
300 "start(%d), current(%d), end(%d)", 300 "start(%d), current(%d), end(%d)",
301 *start, *working, *end); 301 *start, *working, *end);
302#endif /* DEBUG */ 302#endif /* DEBUG */
303 } 303 }
304 return OK; 304 return OK;
305 } 305 }
306 } else { 306 } else {
307 fprintf(stderr, "Inkey wstate screwed - exiting!!!"); 307 fprintf(stderr, "Inkey wstate screwed - exiting!!!");
308 exit(2); 308 exit(2);
309 } 309 }
310 310
311 /* 311 /*
312 * Check key has no special meaning and we have not 312 * Check key has no special meaning and we have not
313 * timed out and the key has not been disabled 313 * timed out and the key has not been disabled
314 */ 314 */
315 mapping = current->mapping[k]; 315 mapping = current->mapping[k];
316 if (((wstate == INKEY_TIMEOUT) || (mapping < 0)) 316 if (((wstate == INKEY_TIMEOUT) || (mapping < 0))
317 || ((current->key[mapping]->type 317 || ((current->key[mapping]->type
318 == KEYMAP_LEAF) 318 == KEYMAP_LEAF)
319 && (current->key[mapping]->enable == FALSE))) 319 && (current->key[mapping]->enable == FALSE)))
320 { 320 {
321 /* wide-character specific code */ 321 /* wide-character specific code */
322#ifdef DEBUG 322#ifdef DEBUG
323 __CTRACE(__CTRACE_INPUT, 323 __CTRACE(__CTRACE_INPUT,
324 "inkey: Checking for wide char\n"); 324 "inkey: Checking for wide char\n");
325#endif /* DEBUG */ 325#endif /* DEBUG */
326 mbrtowc( NULL, NULL, 1, &_cursesi_screen->sp ); 326 mbrtowc( NULL, NULL, 1, &_cursesi_screen->sp );
327 *working = *start; 327 *working = *start;
328 mlen = *end > *working ? 328 mlen = *end > *working ?
329 *end - *working : MAX_CBUF_SIZE - *working; 329 *end - *working : MAX_CBUF_SIZE - *working;
330 if (!mlen) 330 if (!mlen)
331 return ERR; 331 return ERR;
332#ifdef DEBUG 332#ifdef DEBUG
333 __CTRACE(__CTRACE_INPUT, 333 __CTRACE(__CTRACE_INPUT,
334 "inkey: Check wide char[head(%d), " 334 "inkey: Check wide char[head(%d), "
335 "current(%d), tail(%d), mlen(%ld)]\n", 335 "current(%d), tail(%d), mlen(%ld)]\n",
336 *start, *working, *end, (long) mlen); 336 *start, *working, *end, (long) mlen);
337#endif /* DEBUG */ 337#endif /* DEBUG */
338 ret = (int)mbrtowc(wc, inbuf + (*working), mlen, 338 ret = (int)mbrtowc(wc, inbuf + (*working), mlen,
339 &_cursesi_screen->sp); 339 &_cursesi_screen->sp);
340#ifdef DEBUG 340#ifdef DEBUG
341 __CTRACE(__CTRACE_INPUT, 341 __CTRACE(__CTRACE_INPUT,
342 "inkey: mbrtowc returns %d, wc(%x)\n", ret, *wc); 342 "inkey: mbrtowc returns %d, wc(%x)\n", ret, *wc);
343#endif /* DEBUG */ 343#endif /* DEBUG */
344 if (ret == -2 && *end < *working) { 344 if (ret == -2 && *end < *working) {
345 /* second half of a wide character */ 345 /* second half of a wide character */
346 *working = 0; 346 *working = 0;
347 mlen = *end; 347 mlen = *end;
348 if (mlen) 348 if (mlen)
349 ret = (int)mbrtowc(wc, inbuf, mlen, 349 ret = (int)mbrtowc(wc, inbuf, mlen,
350 &_cursesi_screen->sp); 350 &_cursesi_screen->sp);
351 } 351 }
352 if (ret == -2 && wstate != INKEY_TIMEOUT) { 352 if (ret == -2 && wstate != INKEY_TIMEOUT) {
353 *working = (*working + (int) mlen) 353 *working = (*working + (int) mlen)
354 % MAX_CBUF_SIZE; 354 % MAX_CBUF_SIZE;
355 wstate = INKEY_WCASSEMBLING; 355 wstate = INKEY_WCASSEMBLING;
356 continue; 356 continue;
357 } 357 }
358 if (ret == 0) 358 if (ret == 0)
359 ret = 1; 359 ret = 1;
360 if (ret == -1) { 360 if (ret == -1) {
361 /* return the first key we know about */ 361 /* return the first key we know about */
362 *wc = inbuf[*start]; 362 *wc = inbuf[*start];
363 *working = *start 363 *working = *start
364 = (*start + 1) % MAX_CBUF_SIZE; 364 = (*start + 1) % MAX_CBUF_SIZE;
365#ifdef DEBUG 365#ifdef DEBUG
366 __CTRACE(__CTRACE_INPUT, 366 __CTRACE(__CTRACE_INPUT,
367 "inkey: Invalid wide char(%x)[head(%d), " 367 "inkey: Invalid wide char(%x)[head(%d), "
368 "current(%d), tail(%d)]\n", 368 "current(%d), tail(%d)]\n",
369 *wc, *start, *working, *end); 369 *wc, *start, *working, *end);
370#endif /* DEBUG */ 370#endif /* DEBUG */
371 } else { /* > 0 */ 371 } else { /* > 0 */
372 /* return the wide character */ 372 /* return the wide character */
373 *start = *working 373 *start = *working
374 = (*working + ret) % MAX_CBUF_SIZE; 374 = (*working + ret) % MAX_CBUF_SIZE;
375#ifdef DEBUG 375#ifdef DEBUG
376 __CTRACE(__CTRACE_INPUT, 376 __CTRACE(__CTRACE_INPUT,
377 "inkey: Wide char found(%x)[head(%d), " 377 "inkey: Wide char found(%x)[head(%d), "
378 "current(%d), tail(%d)]\n", 378 "current(%d), tail(%d)]\n",
379 *wc, *start, *working, *end); 379 *wc, *start, *working, *end);
380#endif /* DEBUG */ 380#endif /* DEBUG */
381 } 381 }
382 382
383 if (*start == *end) { /* only one char processed */ 383 if (*start == *end) { /* only one char processed */
384 _cursesi_state = wstate = INKEY_NORM; 384 _cursesi_state = wstate = INKEY_NORM;
385#ifdef DEBUG 385#ifdef DEBUG
386 __CTRACE(__CTRACE_INPUT, 386 __CTRACE(__CTRACE_INPUT,
387 "inkey: Empty cbuf=>NORM, " 387 "inkey: Empty cbuf=>NORM, "
388 "start(%d), current(%d), end(%d)\n", 388 "start(%d), current(%d), end(%d)\n",
389 *start, *working, *end); 389 *start, *working, *end);
390#endif /* DEBUG */ 390#endif /* DEBUG */
391 } else { 391 } else {
392 /* otherwise we must have more than one 392 /* otherwise we must have more than one
393 * char to backout */ 393 * char to backout */
394 _cursesi_state = wstate = INKEY_BACKOUT; 394 _cursesi_state = wstate = INKEY_BACKOUT;
395#ifdef DEBUG 395#ifdef DEBUG
396 __CTRACE(__CTRACE_INPUT, 396 __CTRACE(__CTRACE_INPUT,
397 "inkey: Non-empty cbuf=>BACKOUT, " 397 "inkey: Non-empty cbuf=>BACKOUT, "
398 "start(%d), current(%d), end(%d)\n", 398 "start(%d), current(%d), end(%d)\n",
399 *start, *working, *end); 399 *start, *working, *end);
400#endif /* DEBUG */ 400#endif /* DEBUG */
401 } 401 }
402 return OK; 402 return OK;
403 } else { /* must be part of a multikey sequence */ 403 } else { /* must be part of a multikey sequence */
404 /* check for completed key sequence */ 404 /* check for completed key sequence */
405 if (current->key[current->mapping[k]]->type 405 if (current->key[current->mapping[k]]->type
406 == KEYMAP_LEAF) { 406 == KEYMAP_LEAF) {
407 /* eat the key sequence in cbuf */ 407 /* eat the key sequence in cbuf */
408 *start = *working = ( *working + 1 ) 408 *start = *working = ( *working + 1 )
409 % MAX_CBUF_SIZE; 409 % MAX_CBUF_SIZE;
410 410
411 /* check if inbuf empty now */ 411 /* check if inbuf empty now */
412#ifdef DEBUG 412#ifdef DEBUG
413 __CTRACE(__CTRACE_INPUT, 413 __CTRACE(__CTRACE_INPUT,
414 "inkey: Key found(%s)\n", 414 "inkey: Key found(%s)\n",
415 key_name(current->key[mapping]->value.symbol)); 415 key_name(current->key[mapping]->value.symbol));
416#endif /* DEBUG */ 416#endif /* DEBUG */
417 if (*start == *end) { 417 if (*start == *end) {
418 /* if it is go back to normal */ 418 /* if it is go back to normal */
419 _cursesi_state = wstate = INKEY_NORM; 419 _cursesi_state = wstate = INKEY_NORM;
420#ifdef DEBUG 420#ifdef DEBUG
421 __CTRACE(__CTRACE_INPUT, 421 __CTRACE(__CTRACE_INPUT,
422 "[inkey]=>NORM, start(%d), " 422 "[inkey]=>NORM, start(%d), "
423 "current(%d), end(%d)", 423 "current(%d), end(%d)",
424 *start, *working, *end); 424 *start, *working, *end);
425#endif /* DEBUG */ 425#endif /* DEBUG */
426 } else { 426 } else {
427 /* otherwise go to backout state */ 427 /* otherwise go to backout state */
428 _cursesi_state = wstate = INKEY_BACKOUT; 428 _cursesi_state = wstate = INKEY_BACKOUT;
429#ifdef DEBUG 429#ifdef DEBUG
430 __CTRACE(__CTRACE_INPUT, 430 __CTRACE(__CTRACE_INPUT,
431 "[inkey]=>BACKOUT, start(%d), " 431 "[inkey]=>BACKOUT, start(%d), "
432 "current(%d), end(%d)", 432 "current(%d), end(%d)",
433 *start, *working, *end ); 433 *start, *working, *end );
434#endif /* DEBUG */ 434#endif /* DEBUG */
435 } 435 }
436 436
437 /* return the symbol */ 437 /* return the symbol */
438 *wc = current->key[mapping]->value.symbol; 438 *wc = current->key[mapping]->value.symbol;
439 return KEY_CODE_YES; 439 return KEY_CODE_YES;
440 } else { 440 } else {
441 /* Step to next part of multi-key sequence */ 441 /* Step to next part of multi-key sequence */
442 current = current->key[current->mapping[k]]->value.next; 442 current = current->key[current->mapping[k]]->value.next;
443 } 443 }
444 } 444 }
445 } 445 }
446} 446}
447 447
448/* 448/*
449 * get_wch -- 449 * get_wch --
450 * Read in a wide character from stdscr. 450 * Read in a wide character from stdscr.
451 */ 451 */
452int 452int
453get_wch(wint_t *ch) 453get_wch(wint_t *ch)
454{ 454{
455 return wget_wch(stdscr, ch); 455 return wget_wch(stdscr, ch);
456} 456}
457 457
458/* 458/*
459 * mvget_wch -- 459 * mvget_wch --
460 * Read in a character from stdscr at the given location. 460 * Read in a character from stdscr at the given location.
461 */ 461 */
462int 462int
463mvget_wch(int y, int x, wint_t *ch) 463mvget_wch(int y, int x, wint_t *ch)
464{ 464{
465 return mvwget_wch(stdscr, y, x, ch); 465 return mvwget_wch(stdscr, y, x, ch);
466} 466}
467 467
468/* 468/*
469 * mvwget_wch -- 469 * mvwget_wch --
470 * Read in a character from stdscr at the given location in the 470 * Read in a character from stdscr at the given location in the
471 * given window. 471 * given window.
472 */ 472 */
473int 473int
474mvwget_wch(WINDOW *win, int y, int x, wint_t *ch) 474mvwget_wch(WINDOW *win, int y, int x, wint_t *ch)
475{ 475{
476 if (wmove(win, y, x) == ERR) 476 if (wmove(win, y, x) == ERR)
477 return ERR; 477 return ERR;
478 478
479 return wget_wch(win, ch); 479 return wget_wch(win, ch);
480} 480}
481 481
482/* 482/*
483 * wget_wch -- 483 * wget_wch --
484 * Read in a wide character from the window. 484 * Read in a wide character from the window.
485 */ 485 */
486int 486int
487wget_wch(WINDOW *win, wint_t *ch) 487wget_wch(WINDOW *win, wint_t *ch)
488{ 488{
489 int ret, weset; 489 int ret, weset;
490 int c; 490 int c;
491 FILE *infd = _cursesi_screen->infd; 491 FILE *infd = _cursesi_screen->infd;
492 cchar_t wc; 492 cchar_t wc;
493 wchar_t inp, ws[2]; 493 wchar_t inp, ws[2];
494 494
495 if (!(win->flags & __SCROLLOK) 495 if (!(win->flags & __SCROLLOK)
496 && (win->flags & __FULLWIN) 496 && (win->flags & __FULLWIN)
497 && win->curx == win->maxx - 1 497 && win->curx == win->maxx - 1
498 && win->cury == win->maxy - 1 498 && win->cury == win->maxy - 1
499 && __echoit) 499 && __echoit)
500 return ERR; 500 return ERR;
501 501
502 if (is_wintouched(win)) 502 if (!(win->flags & __ISPAD) && is_wintouched(win))
503 wrefresh(win); 503 wrefresh(win);
504#ifdef DEBUG 504#ifdef DEBUG
505 __CTRACE(__CTRACE_INPUT, "wget_wch: __echoit = %d, " 505 __CTRACE(__CTRACE_INPUT, "wget_wch: __echoit = %d, "
506 "__rawmode = %d, __nl = %d, flags = %#.4x\n", 506 "__rawmode = %d, __nl = %d, flags = %#.4x\n",
507 __echoit, __rawmode, _cursesi_screen->nl, win->flags); 507 __echoit, __rawmode, _cursesi_screen->nl, win->flags);
508#endif 508#endif
509 if (_cursesi_screen->resized) { 509 if (_cursesi_screen->resized) {
510 resizeterm(LINES, COLS); 510 resizeterm(LINES, COLS);
511 _cursesi_screen->resized = 0; 511 _cursesi_screen->resized = 0;
512 *ch = KEY_RESIZE; 512 *ch = KEY_RESIZE;
513 return KEY_CODE_YES; 513 return KEY_CODE_YES;
514 } 514 }
515 if (_cursesi_screen->unget_pos) { 515 if (_cursesi_screen->unget_pos) {
516#ifdef DEBUG 516#ifdef DEBUG
517 __CTRACE(__CTRACE_INPUT, "wget_wch returning char at %d\n", 517 __CTRACE(__CTRACE_INPUT, "wget_wch returning char at %d\n",
518 _cursesi_screen->unget_pos); 518 _cursesi_screen->unget_pos);
519#endif 519#endif
520 _cursesi_screen->unget_pos--; 520 _cursesi_screen->unget_pos--;
521 *ch = _cursesi_screen->unget_list[_cursesi_screen->unget_pos]; 521 *ch = _cursesi_screen->unget_list[_cursesi_screen->unget_pos];
522 if (__echoit) { 522 if (__echoit) {
523 ws[0] = *ch, ws[1] = L'\0'; 523 ws[0] = *ch, ws[1] = L'\0';
524 setcchar(&wc, ws, win->wattr, 0, NULL); 524 setcchar(&wc, ws, win->wattr, 0, NULL);
525 wadd_wch(win, &wc); 525 wadd_wch(win, &wc);
526 } 526 }
527 return KEY_CODE_YES; 527 return KEY_CODE_YES;
528 } 528 }
529 if (__echoit && !__rawmode) { 529 if (__echoit && !__rawmode) {
530 cbreak(); 530 cbreak();
531 weset = 1; 531 weset = 1;
532 } else 532 } else
533 weset = 0; 533 weset = 0;
534 534
535 __save_termios(); 535 __save_termios();
536 536
537 if (win->flags & __KEYPAD) { 537 if (win->flags & __KEYPAD) {
538 switch (win->delay) { 538 switch (win->delay) {
539 case -1: 539 case -1:
540 ret = inkey(&inp, 540 ret = inkey(&inp,
541 win->flags & __NOTIMEOUT ? 0 : 1, 0); 541 win->flags & __NOTIMEOUT ? 0 : 1, 0);
542 break; 542 break;
543 case 0: 543 case 0:
544 if (__nodelay() == ERR) 544 if (__nodelay() == ERR)
545 return ERR; 545 return ERR;
546 ret = inkey(&inp, 0, 0); 546 ret = inkey(&inp, 0, 0);
547 break; 547 break;
548 default: 548 default:
549 ret = inkey(&inp, 549 ret = inkey(&inp,
550 win->flags & __NOTIMEOUT ? 0 : 1, 550 win->flags & __NOTIMEOUT ? 0 : 1,
551 win->delay); 551 win->delay);
552 break; 552 break;
553 } 553 }
554 if ( ret == ERR ) 554 if ( ret == ERR )
555 return ERR; 555 return ERR;
556 } else { 556 } else {
557 bool resized; 557 bool resized;
558 558
559 switch (win->delay) { 559 switch (win->delay) {
560 case -1: 560 case -1:
561 break; 561 break;
562 case 0: 562 case 0:
563 if (__nodelay() == ERR) 563 if (__nodelay() == ERR)
564 return ERR; 564 return ERR;
565 break; 565 break;
566 default: 566 default:
567 if (__timeout(win->delay) == ERR) 567 if (__timeout(win->delay) == ERR)
568 return ERR; 568 return ERR;
569 break; 569 break;
570 } 570 }
571 571
572 c = __fgetwc_resize(infd, &resized); 572 c = __fgetwc_resize(infd, &resized);
573 if (c == WEOF) { 573 if (c == WEOF) {
574 clearerr(infd); 574 clearerr(infd);
575 __restore_termios(); 575 __restore_termios();
576 if (resized) { 576 if (resized) {
577 *ch = KEY_RESIZE; 577 *ch = KEY_RESIZE;
578 return KEY_CODE_YES; 578 return KEY_CODE_YES;
579 } else 579 } else
580 return ERR; 580 return ERR;
581 } else { 581 } else {
582 ret = c; 582 ret = c;
583 inp = c; 583 inp = c;
584 } 584 }
585 } 585 }
586#ifdef DEBUG 586#ifdef DEBUG
587 if (inp > 255) 587 if (inp > 255)
588 /* we have a key symbol - treat it differently */ 588 /* we have a key symbol - treat it differently */
589 /* XXXX perhaps __unctrl should be expanded to include 589 /* XXXX perhaps __unctrl should be expanded to include
590 * XXXX the keysyms in the table.... 590 * XXXX the keysyms in the table....
591 */ 591 */
592 __CTRACE(__CTRACE_INPUT, "wget_wch assembled keysym 0x%x\n", 592 __CTRACE(__CTRACE_INPUT, "wget_wch assembled keysym 0x%x\n",
593 inp); 593 inp);
594 else 594 else
595 __CTRACE(__CTRACE_INPUT, "wget_wch got '%s'\n", unctrl(inp)); 595 __CTRACE(__CTRACE_INPUT, "wget_wch got '%s'\n", unctrl(inp));
596#endif 596#endif
597 if (win->delay > -1) { 597 if (win->delay > -1) {
598 if (__delay() == ERR) 598 if (__delay() == ERR)
599 return ERR; 599 return ERR;
600 } 600 }
601 601
602 __restore_termios(); 602 __restore_termios();
603 603
604 if (__echoit) { 604 if (__echoit) {
605 if ( ret == KEY_CODE_YES ) { 605 if ( ret == KEY_CODE_YES ) {
606 /* handle [DEL], [BS], and [LEFT] */ 606 /* handle [DEL], [BS], and [LEFT] */
607 if ( win->curx && 607 if ( win->curx &&
608 ( inp == KEY_DC || 608 ( inp == KEY_DC ||
609 inp == KEY_BACKSPACE || 609 inp == KEY_BACKSPACE ||
610 inp == KEY_LEFT )) { 610 inp == KEY_LEFT )) {
611 wmove( win, win->cury, win->curx - 1); 611 wmove( win, win->cury, win->curx - 1);
612 wdelch( win ); 612 wdelch( win );
613 } 613 }
614 } else { 614 } else {
615 ws[ 0 ] = inp, ws[ 1 ] = L'\0'; 615 ws[ 0 ] = inp, ws[ 1 ] = L'\0';
616 setcchar( &wc, ws, win->wattr, 0, NULL ); 616 setcchar( &wc, ws, win->wattr, 0, NULL );
617 wadd_wch( win, &wc ); 617 wadd_wch( win, &wc );
618 } 618 }
619 } 619 }
620 620
621 if (weset) 621 if (weset)
622 nocbreak(); 622 nocbreak();
623 623
624 if (_cursesi_screen->nl && inp == 13) 624 if (_cursesi_screen->nl && inp == 13)
625 inp = 10; 625 inp = 10;
626 626
627 *ch = inp; 627 *ch = inp;
628 628
629 if ( ret == KEY_CODE_YES ) 629 if ( ret == KEY_CODE_YES )
630 return KEY_CODE_YES; 630 return KEY_CODE_YES;
631 return inp < 0 ? ERR : OK; 631 return inp < 0 ? ERR : OK;
632} 632}
633 633
634/* 634/*
635 * unget_wch -- 635 * unget_wch --
636 * Put the wide character back into the input queue. 636 * Put the wide character back into the input queue.
637 */ 637 */
638int 638int
639unget_wch(const wchar_t c) 639unget_wch(const wchar_t c)
640{ 640{
641 return __unget((wint_t)c); 641 return __unget((wint_t)c);
642} 642}
643 643
644/* 644/*
645 * __fgetwc_resize -- 645 * __fgetwc_resize --
646 * Any call to fgetwc(3) should use this function instead. 646 * Any call to fgetwc(3) should use this function instead.
647 */ 647 */
648static wint_t 648static wint_t
649__fgetwc_resize(FILE *infd, bool *resized) 649__fgetwc_resize(FILE *infd, bool *resized)
650{ 650{
651 wint_t c; 651 wint_t c;
652 652
653 c = fgetwc(infd); 653 c = fgetwc(infd);
654 if (c != WEOF) 654 if (c != WEOF)
655 return c; 655 return c;
656 656
657 if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized) 657 if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized)
658 return ERR; 658 return ERR;
659#ifdef DEBUG 659#ifdef DEBUG
660 __CTRACE(__CTRACE_INPUT, "__fgetwc_resize returning KEY_RESIZE\n"); 660 __CTRACE(__CTRACE_INPUT, "__fgetwc_resize returning KEY_RESIZE\n");
661#endif 661#endif
662 resizeterm(LINES, COLS); 662 resizeterm(LINES, COLS);
663 _cursesi_screen->resized = 0; 663 _cursesi_screen->resized = 0;
664 *resized = true; 664 *resized = true;
665 return c; 665 return c;
666} 666}

cvs diff -r1.74 -r1.75 src/lib/libcurses/getch.c (switch to unified diff)

--- src/lib/libcurses/getch.c 2020/05/14 11:50:04 1.74
+++ src/lib/libcurses/getch.c 2020/07/06 23:33:38 1.75
@@ -1,1042 +1,1042 @@ @@ -1,1042 +1,1042 @@
1/* $NetBSD: getch.c,v 1.74 2020/05/14 11:50:04 simonb Exp $ */ 1/* $NetBSD: getch.c,v 1.75 2020/07/06 23:33:38 uwe Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1981, 1993, 1994 4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors 15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software 16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission. 17 * without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34#if 0 34#if 0
35static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94"; 35static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
36#else 36#else
37__RCSID("$NetBSD: getch.c,v 1.74 2020/05/14 11:50:04 simonb Exp $"); 37__RCSID("$NetBSD: getch.c,v 1.75 2020/07/06 23:33:38 uwe Exp $");
38#endif 38#endif
39#endif /* not lint */ 39#endif /* not lint */
40 40
41#include <errno.h> 41#include <errno.h>
42#include <string.h> 42#include <string.h>
43#include <stdlib.h> 43#include <stdlib.h>
44#include <unistd.h> 44#include <unistd.h>
45#include <stdio.h> 45#include <stdio.h>
46#include "curses.h" 46#include "curses.h"
47#include "curses_private.h" 47#include "curses_private.h"
48#include "keymap.h" 48#include "keymap.h"
49 49
50short _cursesi_state; /* state of the inkey function */ 50short _cursesi_state; /* state of the inkey function */
51 51
52static const struct tcdata tc[] = { 52static const struct tcdata tc[] = {
53 {TICODE_kSAV, KEY_SSAVE}, 53 {TICODE_kSAV, KEY_SSAVE},
54 {TICODE_kSPD, KEY_SSUSPEND}, 54 {TICODE_kSPD, KEY_SSUSPEND},
55 {TICODE_kUND, KEY_SUNDO}, 55 {TICODE_kUND, KEY_SUNDO},
56 {TICODE_kHLP, KEY_SHELP}, 56 {TICODE_kHLP, KEY_SHELP},
57 {TICODE_kHOM, KEY_SHOME}, 57 {TICODE_kHOM, KEY_SHOME},
58 {TICODE_kIC, KEY_SIC}, 58 {TICODE_kIC, KEY_SIC},
59 {TICODE_kLFT, KEY_SLEFT}, 59 {TICODE_kLFT, KEY_SLEFT},
60 {TICODE_krdo, KEY_REDO}, 60 {TICODE_krdo, KEY_REDO},
61 {TICODE_khlp, KEY_HELP}, 61 {TICODE_khlp, KEY_HELP},
62 {TICODE_kmrk, KEY_MARK}, 62 {TICODE_kmrk, KEY_MARK},
63 {TICODE_kmsg, KEY_MESSAGE}, 63 {TICODE_kmsg, KEY_MESSAGE},
64 {TICODE_kmov, KEY_MOVE}, 64 {TICODE_kmov, KEY_MOVE},
65 {TICODE_knxt, KEY_NEXT}, 65 {TICODE_knxt, KEY_NEXT},
66 {TICODE_kopn, KEY_OPEN}, 66 {TICODE_kopn, KEY_OPEN},
67 {TICODE_kopt, KEY_OPTIONS}, 67 {TICODE_kopt, KEY_OPTIONS},
68 {TICODE_kprv, KEY_PREVIOUS}, 68 {TICODE_kprv, KEY_PREVIOUS},
69 {TICODE_kprt, KEY_PRINT}, 69 {TICODE_kprt, KEY_PRINT},
70 {TICODE_kMSG, KEY_SMESSAGE}, 70 {TICODE_kMSG, KEY_SMESSAGE},
71 {TICODE_kMOV, KEY_SMOVE}, 71 {TICODE_kMOV, KEY_SMOVE},
72 {TICODE_kNXT, KEY_SNEXT}, 72 {TICODE_kNXT, KEY_SNEXT},
73 {TICODE_kOPT, KEY_SOPTIONS}, 73 {TICODE_kOPT, KEY_SOPTIONS},
74 {TICODE_kPRV, KEY_SPREVIOUS}, 74 {TICODE_kPRV, KEY_SPREVIOUS},
75 {TICODE_kPRT, KEY_SPRINT}, 75 {TICODE_kPRT, KEY_SPRINT},
76 {TICODE_kRDO, KEY_SREDO}, 76 {TICODE_kRDO, KEY_SREDO},
77 {TICODE_kRPL, KEY_SREPLACE}, 77 {TICODE_kRPL, KEY_SREPLACE},
78 {TICODE_kRIT, KEY_SRIGHT}, 78 {TICODE_kRIT, KEY_SRIGHT},
79 {TICODE_kRES, KEY_SRSUME}, 79 {TICODE_kRES, KEY_SRSUME},
80 {TICODE_kCAN, KEY_SCANCEL}, 80 {TICODE_kCAN, KEY_SCANCEL},
81 {TICODE_kref, KEY_REFERENCE}, 81 {TICODE_kref, KEY_REFERENCE},
82 {TICODE_krfr, KEY_REFRESH}, 82 {TICODE_krfr, KEY_REFRESH},
83 {TICODE_krpl, KEY_REPLACE}, 83 {TICODE_krpl, KEY_REPLACE},
84 {TICODE_krst, KEY_RESTART}, 84 {TICODE_krst, KEY_RESTART},
85 {TICODE_kres, KEY_RESUME}, 85 {TICODE_kres, KEY_RESUME},
86 {TICODE_ksav, KEY_SAVE}, 86 {TICODE_ksav, KEY_SAVE},
87 {TICODE_kspd, KEY_SUSPEND}, 87 {TICODE_kspd, KEY_SUSPEND},
88 {TICODE_kund, KEY_UNDO}, 88 {TICODE_kund, KEY_UNDO},
89 {TICODE_kBEG, KEY_SBEG}, 89 {TICODE_kBEG, KEY_SBEG},
90 {TICODE_kFND, KEY_SFIND}, 90 {TICODE_kFND, KEY_SFIND},
91 {TICODE_kCMD, KEY_SCOMMAND}, 91 {TICODE_kCMD, KEY_SCOMMAND},
92 {TICODE_kCPY, KEY_SCOPY}, 92 {TICODE_kCPY, KEY_SCOPY},
93 {TICODE_kCRT, KEY_SCREATE}, 93 {TICODE_kCRT, KEY_SCREATE},
94 {TICODE_kDC, KEY_SDC}, 94 {TICODE_kDC, KEY_SDC},
95 {TICODE_kDL, KEY_SDL}, 95 {TICODE_kDL, KEY_SDL},
96 {TICODE_kslt, KEY_SELECT}, 96 {TICODE_kslt, KEY_SELECT},
97 {TICODE_kEND, KEY_SEND}, 97 {TICODE_kEND, KEY_SEND},
98 {TICODE_kEOL, KEY_SEOL}, 98 {TICODE_kEOL, KEY_SEOL},
99 {TICODE_kEXT, KEY_SEXIT}, 99 {TICODE_kEXT, KEY_SEXIT},
100 {TICODE_kfnd, KEY_FIND}, 100 {TICODE_kfnd, KEY_FIND},
101 {TICODE_kbeg, KEY_BEG}, 101 {TICODE_kbeg, KEY_BEG},
102 {TICODE_kcan, KEY_CANCEL}, 102 {TICODE_kcan, KEY_CANCEL},
103 {TICODE_kclo, KEY_CLOSE}, 103 {TICODE_kclo, KEY_CLOSE},
104 {TICODE_kcmd, KEY_COMMAND}, 104 {TICODE_kcmd, KEY_COMMAND},
105 {TICODE_kcpy, KEY_COPY}, 105 {TICODE_kcpy, KEY_COPY},
106 {TICODE_kcrt, KEY_CREATE}, 106 {TICODE_kcrt, KEY_CREATE},
107 {TICODE_kend, KEY_END}, 107 {TICODE_kend, KEY_END},
108 {TICODE_kent, KEY_ENTER}, 108 {TICODE_kent, KEY_ENTER},
109 {TICODE_kext, KEY_EXIT}, 109 {TICODE_kext, KEY_EXIT},
110 {TICODE_kf11, KEY_F(11)}, 110 {TICODE_kf11, KEY_F(11)},
111 {TICODE_kf12, KEY_F(12)}, 111 {TICODE_kf12, KEY_F(12)},
112 {TICODE_kf13, KEY_F(13)}, 112 {TICODE_kf13, KEY_F(13)},
113 {TICODE_kf14, KEY_F(14)}, 113 {TICODE_kf14, KEY_F(14)},
114 {TICODE_kf15, KEY_F(15)}, 114 {TICODE_kf15, KEY_F(15)},
115 {TICODE_kf16, KEY_F(16)}, 115 {TICODE_kf16, KEY_F(16)},
116 {TICODE_kf17, KEY_F(17)}, 116 {TICODE_kf17, KEY_F(17)},
117 {TICODE_kf18, KEY_F(18)}, 117 {TICODE_kf18, KEY_F(18)},
118 {TICODE_kf19, KEY_F(19)}, 118 {TICODE_kf19, KEY_F(19)},
119 {TICODE_kf20, KEY_F(20)}, 119 {TICODE_kf20, KEY_F(20)},
120 {TICODE_kf21, KEY_F(21)}, 120 {TICODE_kf21, KEY_F(21)},
121 {TICODE_kf22, KEY_F(22)}, 121 {TICODE_kf22, KEY_F(22)},
122 {TICODE_kf23, KEY_F(23)}, 122 {TICODE_kf23, KEY_F(23)},
123 {TICODE_kf24, KEY_F(24)}, 123 {TICODE_kf24, KEY_F(24)},
124 {TICODE_kf25, KEY_F(25)}, 124 {TICODE_kf25, KEY_F(25)},
125 {TICODE_kf26, KEY_F(26)}, 125 {TICODE_kf26, KEY_F(26)},
126 {TICODE_kf27, KEY_F(27)}, 126 {TICODE_kf27, KEY_F(27)},
127 {TICODE_kf28, KEY_F(28)}, 127 {TICODE_kf28, KEY_F(28)},
128 {TICODE_kf29, KEY_F(29)}, 128 {TICODE_kf29, KEY_F(29)},
129 {TICODE_kf30, KEY_F(30)}, 129 {TICODE_kf30, KEY_F(30)},
130 {TICODE_kf31, KEY_F(31)}, 130 {TICODE_kf31, KEY_F(31)},
131 {TICODE_kf32, KEY_F(32)}, 131 {TICODE_kf32, KEY_F(32)},
132 {TICODE_kf33, KEY_F(33)}, 132 {TICODE_kf33, KEY_F(33)},
133 {TICODE_kf34, KEY_F(34)}, 133 {TICODE_kf34, KEY_F(34)},
134 {TICODE_kf35, KEY_F(35)}, 134 {TICODE_kf35, KEY_F(35)},
135 {TICODE_kf36, KEY_F(36)}, 135 {TICODE_kf36, KEY_F(36)},
136 {TICODE_kf37, KEY_F(37)}, 136 {TICODE_kf37, KEY_F(37)},
137 {TICODE_kf38, KEY_F(38)}, 137 {TICODE_kf38, KEY_F(38)},
138 {TICODE_kf39, KEY_F(39)}, 138 {TICODE_kf39, KEY_F(39)},
139 {TICODE_kf40, KEY_F(40)}, 139 {TICODE_kf40, KEY_F(40)},
140 {TICODE_kf41, KEY_F(41)}, 140 {TICODE_kf41, KEY_F(41)},
141 {TICODE_kf42, KEY_F(42)}, 141 {TICODE_kf42, KEY_F(42)},
142 {TICODE_kf43, KEY_F(43)}, 142 {TICODE_kf43, KEY_F(43)},
143 {TICODE_kf44, KEY_F(44)}, 143 {TICODE_kf44, KEY_F(44)},
144 {TICODE_kf45, KEY_F(45)}, 144 {TICODE_kf45, KEY_F(45)},
145 {TICODE_kf46, KEY_F(46)}, 145 {TICODE_kf46, KEY_F(46)},
146 {TICODE_kf47, KEY_F(47)}, 146 {TICODE_kf47, KEY_F(47)},
147 {TICODE_kf48, KEY_F(48)}, 147 {TICODE_kf48, KEY_F(48)},
148 {TICODE_kf49, KEY_F(49)}, 148 {TICODE_kf49, KEY_F(49)},
149 {TICODE_kf50, KEY_F(50)}, 149 {TICODE_kf50, KEY_F(50)},
150 {TICODE_kf51, KEY_F(51)}, 150 {TICODE_kf51, KEY_F(51)},
151 {TICODE_kf52, KEY_F(52)}, 151 {TICODE_kf52, KEY_F(52)},
152 {TICODE_kf53, KEY_F(53)}, 152 {TICODE_kf53, KEY_F(53)},
153 {TICODE_kf54, KEY_F(54)}, 153 {TICODE_kf54, KEY_F(54)},
154 {TICODE_kf55, KEY_F(55)}, 154 {TICODE_kf55, KEY_F(55)},
155 {TICODE_kf56, KEY_F(56)}, 155 {TICODE_kf56, KEY_F(56)},
156 {TICODE_kf57, KEY_F(57)}, 156 {TICODE_kf57, KEY_F(57)},
157 {TICODE_kf58, KEY_F(58)}, 157 {TICODE_kf58, KEY_F(58)},
158 {TICODE_kf59, KEY_F(59)}, 158 {TICODE_kf59, KEY_F(59)},
159 {TICODE_kf60, KEY_F(60)}, 159 {TICODE_kf60, KEY_F(60)},
160 {TICODE_kf61, KEY_F(61)}, 160 {TICODE_kf61, KEY_F(61)},
161 {TICODE_kf62, KEY_F(62)}, 161 {TICODE_kf62, KEY_F(62)},
162 {TICODE_kf63, KEY_F(63)}, 162 {TICODE_kf63, KEY_F(63)},
163 {TICODE_ka1, KEY_A1}, 163 {TICODE_ka1, KEY_A1},
164 {TICODE_kb2, KEY_B2}, 164 {TICODE_kb2, KEY_B2},
165 {TICODE_ka3, KEY_A3}, 165 {TICODE_ka3, KEY_A3},
166 {TICODE_kc1, KEY_C1}, 166 {TICODE_kc1, KEY_C1},
167 {TICODE_kc3, KEY_C3}, 167 {TICODE_kc3, KEY_C3},
168 {TICODE_kmous, KEY_MOUSE}, 168 {TICODE_kmous, KEY_MOUSE},
169 {TICODE_kf0, KEY_F0}, 169 {TICODE_kf0, KEY_F0},
170 {TICODE_kf1, KEY_F(1)}, 170 {TICODE_kf1, KEY_F(1)},
171 {TICODE_kf2, KEY_F(2)}, 171 {TICODE_kf2, KEY_F(2)},
172 {TICODE_kf3, KEY_F(3)}, 172 {TICODE_kf3, KEY_F(3)},
173 {TICODE_kf4, KEY_F(4)}, 173 {TICODE_kf4, KEY_F(4)},
174 {TICODE_kf5, KEY_F(5)}, 174 {TICODE_kf5, KEY_F(5)},
175 {TICODE_kf6, KEY_F(6)}, 175 {TICODE_kf6, KEY_F(6)},
176 {TICODE_kf7, KEY_F(7)}, 176 {TICODE_kf7, KEY_F(7)},
177 {TICODE_kf8, KEY_F(8)}, 177 {TICODE_kf8, KEY_F(8)},
178 {TICODE_kf9, KEY_F(9)}, 178 {TICODE_kf9, KEY_F(9)},
179 {TICODE_kf10, KEY_F(10)}, 179 {TICODE_kf10, KEY_F(10)},
180 {TICODE_kil1, KEY_IL}, 180 {TICODE_kil1, KEY_IL},
181 {TICODE_ktbc, KEY_CATAB}, 181 {TICODE_ktbc, KEY_CATAB},
182 {TICODE_kcbt, KEY_BTAB}, 182 {TICODE_kcbt, KEY_BTAB},
183 {TICODE_kbs, KEY_BACKSPACE}, 183 {TICODE_kbs, KEY_BACKSPACE},
184 {TICODE_kclr, KEY_CLEAR}, 184 {TICODE_kclr, KEY_CLEAR},
185 {TICODE_kdch1, KEY_DC}, 185 {TICODE_kdch1, KEY_DC},
186 {TICODE_kcud1, KEY_DOWN}, 186 {TICODE_kcud1, KEY_DOWN},
187 {TICODE_kel, KEY_EOL}, 187 {TICODE_kel, KEY_EOL},
188 {TICODE_kind, KEY_SF}, 188 {TICODE_kind, KEY_SF},
189 {TICODE_kll, KEY_LL}, 189 {TICODE_kll, KEY_LL},
190 {TICODE_khome, KEY_HOME}, 190 {TICODE_khome, KEY_HOME},
191 {TICODE_kich1, KEY_IC}, 191 {TICODE_kich1, KEY_IC},
192 {TICODE_kdl1, KEY_DL}, 192 {TICODE_kdl1, KEY_DL},
193 {TICODE_kcub1, KEY_LEFT}, 193 {TICODE_kcub1, KEY_LEFT},
194 {TICODE_krmir, KEY_EIC}, 194 {TICODE_krmir, KEY_EIC},
195 {TICODE_knp, KEY_NPAGE}, 195 {TICODE_knp, KEY_NPAGE},
196 {TICODE_kpp, KEY_PPAGE}, 196 {TICODE_kpp, KEY_PPAGE},
197 {TICODE_kri, KEY_SR}, 197 {TICODE_kri, KEY_SR},
198 {TICODE_kcuf1, KEY_RIGHT}, 198 {TICODE_kcuf1, KEY_RIGHT},
199 {TICODE_ked, KEY_EOS}, 199 {TICODE_ked, KEY_EOS},
200 {TICODE_khts, KEY_STAB}, 200 {TICODE_khts, KEY_STAB},
201 {TICODE_kctab, KEY_CTAB}, 201 {TICODE_kctab, KEY_CTAB},
202 {TICODE_kcuu1, KEY_UP} 202 {TICODE_kcuu1, KEY_UP}
203}; 203};
204/* Number of TC entries .... */ 204/* Number of TC entries .... */
205static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata)); 205static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
206 206
207/* Key buffer */ 207/* Key buffer */
208#define INBUF_SZ 16 /* size of key buffer - must be larger than 208#define INBUF_SZ 16 /* size of key buffer - must be larger than
209 * longest multi-key sequence */ 209 * longest multi-key sequence */
210static wchar_t inbuf[INBUF_SZ]; 210static wchar_t inbuf[INBUF_SZ];
211static int start, end, working; /* pointers for manipulating inbuf data */ 211static int start, end, working; /* pointers for manipulating inbuf data */
212 212
213/* prototypes for private functions */ 213/* prototypes for private functions */
214static void add_key_sequence(SCREEN *screen, char *sequence, int key_type); 214static void add_key_sequence(SCREEN *screen, char *sequence, int key_type);
215static key_entry_t *add_new_key(keymap_t *current, char ch, int key_type, 215static key_entry_t *add_new_key(keymap_t *current, char ch, int key_type,
216 int symbol); 216 int symbol);
217static void delete_key_sequence(keymap_t *current, int key_type); 217static void delete_key_sequence(keymap_t *current, int key_type);
218static void do_keyok(keymap_t *current, int key_type, bool set, bool flag, 218static void do_keyok(keymap_t *current, int key_type, bool set, bool flag,
219 int *retval); 219 int *retval);
220static keymap_t *new_keymap(void); /* create a new keymap */ 220static keymap_t *new_keymap(void); /* create a new keymap */
221static key_entry_t *new_key(void); /* create a new key entry */ 221static key_entry_t *new_key(void); /* create a new key entry */
222static wchar_t inkey(int to, int delay); 222static wchar_t inkey(int to, int delay);
223 223
224/* 224/*
225 * Free the storage associated with the given keymap 225 * Free the storage associated with the given keymap
226 */ 226 */
227void 227void
228_cursesi_free_keymap(keymap_t *map) 228_cursesi_free_keymap(keymap_t *map)
229{ 229{
230 int i; 230 int i;
231 231
232 /* check for, and free, child keymaps */ 232 /* check for, and free, child keymaps */
233 for (i = 0; i < MAX_CHAR; i++) { 233 for (i = 0; i < MAX_CHAR; i++) {
234 if (map->mapping[i] >= 0) { 234 if (map->mapping[i] >= 0) {
235 if (map->key[map->mapping[i]]->type == KEYMAP_MULTI) 235 if (map->key[map->mapping[i]]->type == KEYMAP_MULTI)
236 _cursesi_free_keymap( 236 _cursesi_free_keymap(
237 map->key[map->mapping[i]]->value.next); 237 map->key[map->mapping[i]]->value.next);
238 } 238 }
239 } 239 }
240 240
241 /* now free any allocated keymap structs */ 241 /* now free any allocated keymap structs */
242 for (i = 0; i < map->count; i += KEYMAP_ALLOC_CHUNK) { 242 for (i = 0; i < map->count; i += KEYMAP_ALLOC_CHUNK) {
243 free(map->key[i]); 243 free(map->key[i]);
244 } 244 }
245 245
246 free(map->key); 246 free(map->key);
247 free(map); 247 free(map);
248} 248}
249 249
250 250
251/* 251/*
252 * Add a new key entry to the keymap pointed to by current. Entry 252 * Add a new key entry to the keymap pointed to by current. Entry
253 * contains the character to add to the keymap, type is the type of 253 * contains the character to add to the keymap, type is the type of
254 * entry to add (either multikey or leaf) and symbol is the symbolic 254 * entry to add (either multikey or leaf) and symbol is the symbolic
255 * value for a leaf type entry. The function returns a pointer to the 255 * value for a leaf type entry. The function returns a pointer to the
256 * new keymap entry. 256 * new keymap entry.
257 */ 257 */
258static key_entry_t * 258static key_entry_t *
259add_new_key(keymap_t *current, char chr, int key_type, int symbol) 259add_new_key(keymap_t *current, char chr, int key_type, int symbol)
260{ 260{
261 key_entry_t *the_key; 261 key_entry_t *the_key;
262 int i, ki; 262 int i, ki;
263 263
264#ifdef DEBUG 264#ifdef DEBUG
265 __CTRACE(__CTRACE_MISC, 265 __CTRACE(__CTRACE_MISC,
266 "Adding character %s of type %d, symbol 0x%x\n", 266 "Adding character %s of type %d, symbol 0x%x\n",
267 unctrl(chr), key_type, symbol); 267 unctrl(chr), key_type, symbol);
268#endif 268#endif
269 if (current->mapping[(unsigned char)chr] < 0) { 269 if (current->mapping[(unsigned char)chr] < 0) {
270 if (current->mapping[(unsigned char)chr] == MAPPING_UNUSED) { 270 if (current->mapping[(unsigned char)chr] == MAPPING_UNUSED) {
271 /* first time for this char */ 271 /* first time for this char */
272 current->mapping[(unsigned char)chr] = 272 current->mapping[(unsigned char)chr] =
273 current->count; /* map new entry */ 273 current->count; /* map new entry */
274 ki = current->count; 274 ki = current->count;
275 275
276 /* make sure we have room in the key array first */ 276 /* make sure we have room in the key array first */
277 if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0) 277 if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
278 { 278 {
279 if ((current->key = 279 if ((current->key =
280 realloc(current->key, 280 realloc(current->key,
281 ki * sizeof(key_entry_t *) 281 ki * sizeof(key_entry_t *)
282 + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) { 282 + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
283 fprintf(stderr, 283 fprintf(stderr,
284 "Could not malloc for key entry\n"); 284 "Could not malloc for key entry\n");
285 exit(1); 285 exit(1);
286 } 286 }
287 287
288 the_key = new_key(); 288 the_key = new_key();
289 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) { 289 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
290 current->key[ki + i] = &the_key[i]; 290 current->key[ki + i] = &the_key[i];
291 } 291 }
292 } 292 }
293 } else { 293 } else {
294 /* the mapping was used but freed, reuse it */ 294 /* the mapping was used but freed, reuse it */
295 ki = - current->mapping[(unsigned char) chr]; 295 ki = - current->mapping[(unsigned char) chr];
296 current->mapping[(unsigned char) chr] = ki; 296 current->mapping[(unsigned char) chr] = ki;
297 } 297 }
298 298
299 current->count++; 299 current->count++;
300 300
301 /* point at the current key array element to use */ 301 /* point at the current key array element to use */
302 the_key = current->key[ki]; 302 the_key = current->key[ki];
303 303
304 the_key->type = key_type; 304 the_key->type = key_type;
305 305
306 switch (key_type) { 306 switch (key_type) {
307 case KEYMAP_MULTI: 307 case KEYMAP_MULTI:
308 /* need for next key */ 308 /* need for next key */
309#ifdef DEBUG 309#ifdef DEBUG
310 __CTRACE(__CTRACE_MISC, "Creating new keymap\n"); 310 __CTRACE(__CTRACE_MISC, "Creating new keymap\n");
311#endif 311#endif
312 the_key->value.next = new_keymap(); 312 the_key->value.next = new_keymap();
313 the_key->enable = TRUE; 313 the_key->enable = TRUE;
314 break; 314 break;
315 315
316 case KEYMAP_LEAF: 316 case KEYMAP_LEAF:
317 /* the associated symbol for the key */ 317 /* the associated symbol for the key */
318#ifdef DEBUG 318#ifdef DEBUG
319 __CTRACE(__CTRACE_MISC, "Adding leaf key\n"); 319 __CTRACE(__CTRACE_MISC, "Adding leaf key\n");
320#endif 320#endif
321 the_key->value.symbol = symbol; 321 the_key->value.symbol = symbol;
322 the_key->enable = TRUE; 322 the_key->enable = TRUE;
323 break; 323 break;
324 324
325 default: 325 default:
326 fprintf(stderr, "add_new_key: bad type passed\n"); 326 fprintf(stderr, "add_new_key: bad type passed\n");
327 exit(1); 327 exit(1);
328 } 328 }
329 } else { 329 } else {
330 /* the key is already known - just return the address. */ 330 /* the key is already known - just return the address. */
331#ifdef DEBUG 331#ifdef DEBUG
332 __CTRACE(__CTRACE_MISC, "Keymap already known\n"); 332 __CTRACE(__CTRACE_MISC, "Keymap already known\n");
333#endif 333#endif
334 the_key = current->key[current->mapping[(unsigned char)chr]]; 334 the_key = current->key[current->mapping[(unsigned char)chr]];
335 } 335 }
336 336
337 return the_key; 337 return the_key;
338} 338}
339 339
340/* 340/*
341 * Delete the given key symbol from the key mappings for the screen. 341 * Delete the given key symbol from the key mappings for the screen.
342 * 342 *
343 */ 343 */
344static void 344static void
345delete_key_sequence(keymap_t *current, int key_type) 345delete_key_sequence(keymap_t *current, int key_type)
346{ 346{
347 key_entry_t *key; 347 key_entry_t *key;
348 int i; 348 int i;
349 349
350 /* 350 /*
351 * we need to iterate over all the keys as there may be 351 * we need to iterate over all the keys as there may be
352 * multiple instances of the leaf symbol. 352 * multiple instances of the leaf symbol.
353 */ 353 */
354 for (i = 0; i < MAX_CHAR; i++) { 354 for (i = 0; i < MAX_CHAR; i++) {
355 if (current->mapping[i] < 0) 355 if (current->mapping[i] < 0)
356 continue; /* no mapping for the key, next! */ 356 continue; /* no mapping for the key, next! */
357 357
358 key = current->key[current->mapping[i]]; 358 key = current->key[current->mapping[i]];
359 359
360 if (key->type == KEYMAP_MULTI) { 360 if (key->type == KEYMAP_MULTI) {
361 /* have not found the leaf, recurse down */ 361 /* have not found the leaf, recurse down */
362 delete_key_sequence(key->value.next, key_type); 362 delete_key_sequence(key->value.next, key_type);
363 /* if we deleted the last key in the map, free */ 363 /* if we deleted the last key in the map, free */
364 if (key->value.next->count == 0) 364 if (key->value.next->count == 0)
365 _cursesi_free_keymap(key->value.next); 365 _cursesi_free_keymap(key->value.next);
366 } else if ((key->type == KEYMAP_LEAF) 366 } else if ((key->type == KEYMAP_LEAF)
367 && (key->value.symbol == key_type)) { 367 && (key->value.symbol == key_type)) {
368#ifdef DEBUG 368#ifdef DEBUG
369 __CTRACE(__CTRACE_INPUT, "delete_key_sequence: found keysym %d, deleting\n", 369 __CTRACE(__CTRACE_INPUT, "delete_key_sequence: found keysym %d, deleting\n",
370 key_type); 370 key_type);
371#endif 371#endif
372 key->enable = FALSE; 372 key->enable = FALSE;
373 } 373 }
374 } 374 }
375} 375}
376 376
377/* 377/*
378 * Add the sequence of characters given in sequence as the key mapping 378 * Add the sequence of characters given in sequence as the key mapping
379 * for the given key symbol. 379 * for the given key symbol.
380 */ 380 */
381static void 381static void
382add_key_sequence(SCREEN *screen, char *sequence, int key_type) 382add_key_sequence(SCREEN *screen, char *sequence, int key_type)
383{ 383{
384 key_entry_t *tmp_key; 384 key_entry_t *tmp_key;
385 keymap_t *current; 385 keymap_t *current;
386 int length, j, key_ent; 386 int length, j, key_ent;
387 387
388#ifdef DEBUG 388#ifdef DEBUG
389 __CTRACE(__CTRACE_MISC, "add_key_sequence: add key sequence: %s(%s)\n", 389 __CTRACE(__CTRACE_MISC, "add_key_sequence: add key sequence: %s(%s)\n",
390 sequence, keyname(key_type)); 390 sequence, keyname(key_type));
391#endif /* DEBUG */ 391#endif /* DEBUG */
392 current = screen->base_keymap; /* always start with 392 current = screen->base_keymap; /* always start with
393 * base keymap. */ 393 * base keymap. */
394 length = (int)strlen(sequence); 394 length = (int)strlen(sequence);
395 395
396 /* 396 /*
397 * OK - we really should never get a zero length string here, either 397 * OK - we really should never get a zero length string here, either
398 * the terminfo entry is there and it has a value or we are not called 398 * the terminfo entry is there and it has a value or we are not called
399 * at all. Unfortunately, if someone assigns a terminfo string to the 399 * at all. Unfortunately, if someone assigns a terminfo string to the
400 * ^@ value we get passed a null string which messes up our length. 400 * ^@ value we get passed a null string which messes up our length.
401 * So, if we get a null string then just insert a leaf value in 401 * So, if we get a null string then just insert a leaf value in
402 * the 0th char position of the root keymap. Note that we are 402 * the 0th char position of the root keymap. Note that we are
403 * totally screwed if someone terminates a multichar sequence 403 * totally screwed if someone terminates a multichar sequence
404 * with ^@... oh well. 404 * with ^@... oh well.
405 */ 405 */
406 if (length == 0) 406 if (length == 0)
407 length = 1; 407 length = 1;
408 408
409 for (j = 0; j < length - 1; j++) { 409 for (j = 0; j < length - 1; j++) {
410 /* add the entry to the struct */ 410 /* add the entry to the struct */
411 tmp_key = add_new_key(current, sequence[j], KEYMAP_MULTI, 0); 411 tmp_key = add_new_key(current, sequence[j], KEYMAP_MULTI, 0);
412 412
413 /* index into the key array - it's 413 /* index into the key array - it's
414 clearer if we stash this */ 414 clearer if we stash this */
415 key_ent = current->mapping[(unsigned char) sequence[j]]; 415 key_ent = current->mapping[(unsigned char) sequence[j]];
416 416
417 current->key[key_ent] = tmp_key; 417 current->key[key_ent] = tmp_key;
418 418
419 /* next key uses this map... */ 419 /* next key uses this map... */
420 current = current->key[key_ent]->value.next; 420 current = current->key[key_ent]->value.next;
421 } 421 }
422 422
423 /* 423 /*
424 * This is the last key in the sequence (it may have been the 424 * This is the last key in the sequence (it may have been the
425 * only one but that does not matter) this means it is a leaf 425 * only one but that does not matter) this means it is a leaf
426 * key and should have a symbol associated with it. 426 * key and should have a symbol associated with it.
427 */ 427 */
428 tmp_key = add_new_key(current, sequence[length - 1], KEYMAP_LEAF, 428 tmp_key = add_new_key(current, sequence[length - 1], KEYMAP_LEAF,
429 key_type); 429 key_type);
430 current->key[current->mapping[(int)sequence[length - 1]]] = tmp_key; 430 current->key[current->mapping[(int)sequence[length - 1]]] = tmp_key;
431} 431}
432 432
433/* 433/*
434 * Init_getch - initialise all the pointers & structures needed to make 434 * Init_getch - initialise all the pointers & structures needed to make
435 * getch work in keypad mode. 435 * getch work in keypad mode.
436 * 436 *
437 */ 437 */
438void 438void
439__init_getch(SCREEN *screen) 439__init_getch(SCREEN *screen)
440{ 440{
441 char entry[1024], *p; 441 char entry[1024], *p;
442 const char *s; 442 const char *s;
443 int i; 443 int i;
444 size_t limit, l; 444 size_t limit, l;
445#ifdef DEBUG 445#ifdef DEBUG
446 int k, length; 446 int k, length;
447#endif 447#endif
448 448
449 /* init the inkey state variable */ 449 /* init the inkey state variable */
450 _cursesi_state = INKEY_NORM; 450 _cursesi_state = INKEY_NORM;
451 451
452 /* init the base keymap */ 452 /* init the base keymap */
453 screen->base_keymap = new_keymap(); 453 screen->base_keymap = new_keymap();
454 454
455 /* key input buffer pointers */ 455 /* key input buffer pointers */
456 start = end = working = 0; 456 start = end = working = 0;
457 457
458 /* now do the terminfo snarfing ... */ 458 /* now do the terminfo snarfing ... */
459 459
460 for (i = 0; i < num_tcs; i++) { 460 for (i = 0; i < num_tcs; i++) {
461 p = entry; 461 p = entry;
462 limit = 1023; 462 limit = 1023;
463 s = screen->term->strs[tc[i].code]; 463 s = screen->term->strs[tc[i].code];
464 if (s == NULL) 464 if (s == NULL)
465 continue; 465 continue;
466 l = strlen(s) + 1; 466 l = strlen(s) + 1;
467 if (limit < l) 467 if (limit < l)
468 continue; 468 continue;
469 strlcpy(p, s, limit); 469 strlcpy(p, s, limit);
470 p += l; 470 p += l;
471 limit -= l; 471 limit -= l;
472#ifdef DEBUG 472#ifdef DEBUG
473 __CTRACE(__CTRACE_INIT, 473 __CTRACE(__CTRACE_INIT,
474 "Processing terminfo entry %d, sequence ", 474 "Processing terminfo entry %d, sequence ",
475 tc[i].code); 475 tc[i].code);
476 length = (int) strlen(entry); 476 length = (int) strlen(entry);
477 for (k = 0; k <= length -1; k++) 477 for (k = 0; k <= length -1; k++)
478 __CTRACE(__CTRACE_INIT, "%s", unctrl(entry[k])); 478 __CTRACE(__CTRACE_INIT, "%s", unctrl(entry[k]));
479 __CTRACE(__CTRACE_INIT, "\n"); 479 __CTRACE(__CTRACE_INIT, "\n");
480#endif 480#endif
481 add_key_sequence(screen, entry, tc[i].symbol); 481 add_key_sequence(screen, entry, tc[i].symbol);
482 } 482 }
483} 483}
484 484
485 485
486/* 486/*
487 * new_keymap - allocates & initialises a new keymap structure. This 487 * new_keymap - allocates & initialises a new keymap structure. This
488 * function returns a pointer to the new keymap. 488 * function returns a pointer to the new keymap.
489 * 489 *
490 */ 490 */
491static keymap_t * 491static keymap_t *
492new_keymap(void) 492new_keymap(void)
493{ 493{
494 int i; 494 int i;
495 keymap_t *new_map; 495 keymap_t *new_map;
496 496
497 if ((new_map = malloc(sizeof(keymap_t))) == NULL) { 497 if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
498 perror("Inkey: Cannot allocate new keymap"); 498 perror("Inkey: Cannot allocate new keymap");
499 exit(2); 499 exit(2);
500 } 500 }
501 501
502 /* Initialise the new map */ 502 /* Initialise the new map */
503 new_map->count = 0; 503 new_map->count = 0;
504 for (i = 0; i < MAX_CHAR; i++) { 504 for (i = 0; i < MAX_CHAR; i++) {
505 new_map->mapping[i] = MAPPING_UNUSED; /* no mapping for char */ 505 new_map->mapping[i] = MAPPING_UNUSED; /* no mapping for char */
506 } 506 }
507 507
508 /* key array will be allocated when first key is added */ 508 /* key array will be allocated when first key is added */
509 new_map->key = NULL; 509 new_map->key = NULL;
510 510
511 return new_map; 511 return new_map;
512} 512}
513 513
514/* 514/*
515 * new_key - allocates & initialises a new key entry. This function returns 515 * new_key - allocates & initialises a new key entry. This function returns
516 * a pointer to the newly allocated key entry. 516 * a pointer to the newly allocated key entry.
517 * 517 *
518 */ 518 */
519static key_entry_t * 519static key_entry_t *
520new_key(void) 520new_key(void)
521{ 521{
522 key_entry_t *new_one; 522 key_entry_t *new_one;
523 int i; 523 int i;
524 524
525 new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)); 525 new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t));
526 if (new_one == NULL) { 526 if (new_one == NULL) {
527 perror("inkey: Cannot allocate new key entry chunk"); 527 perror("inkey: Cannot allocate new key entry chunk");
528 exit(2); 528 exit(2);
529 } 529 }
530 530
531 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) { 531 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
532 new_one[i].type = 0; 532 new_one[i].type = 0;
533 new_one[i].value.next = NULL; 533 new_one[i].value.next = NULL;
534 } 534 }
535 535
536 return new_one; 536 return new_one;
537} 537}
538 538
539/* 539/*
540 * inkey - do the work to process keyboard input, check for multi-key 540 * inkey - do the work to process keyboard input, check for multi-key
541 * sequences and return the appropriate symbol if we get a match. 541 * sequences and return the appropriate symbol if we get a match.
542 * 542 *
543 */ 543 */
544 544
545static wchar_t 545static wchar_t
546inkey(int to, int delay) 546inkey(int to, int delay)
547{ 547{
548 wchar_t k; 548 wchar_t k;
549 int c, mapping; 549 int c, mapping;
550 keymap_t *current = _cursesi_screen->base_keymap; 550 keymap_t *current = _cursesi_screen->base_keymap;
551 FILE *infd = _cursesi_screen->infd; 551 FILE *infd = _cursesi_screen->infd;
552 552
553 k = 0; /* XXX gcc -Wuninitialized */ 553 k = 0; /* XXX gcc -Wuninitialized */
554 554
555#ifdef DEBUG 555#ifdef DEBUG
556 __CTRACE(__CTRACE_INPUT, "inkey (%d, %d)\n", to, delay); 556 __CTRACE(__CTRACE_INPUT, "inkey (%d, %d)\n", to, delay);
557#endif 557#endif
558 for (;;) { /* loop until we get a complete key sequence */ 558 for (;;) { /* loop until we get a complete key sequence */
559reread: 559reread:
560 if (_cursesi_state == INKEY_NORM) { 560 if (_cursesi_state == INKEY_NORM) {
561 if (delay && __timeout(delay) == ERR) 561 if (delay && __timeout(delay) == ERR)
562 return ERR; 562 return ERR;
563 c = __fgetc_resize(infd); 563 c = __fgetc_resize(infd);
564 if (c == ERR || c == KEY_RESIZE) { 564 if (c == ERR || c == KEY_RESIZE) {
565 clearerr(infd); 565 clearerr(infd);
566 return c; 566 return c;
567 } 567 }
568 568
569 if (delay && (__notimeout() == ERR)) 569 if (delay && (__notimeout() == ERR))
570 return ERR; 570 return ERR;
571 571
572 k = (wchar_t)c; 572 k = (wchar_t)c;
573#ifdef DEBUG 573#ifdef DEBUG
574 __CTRACE(__CTRACE_INPUT, 574 __CTRACE(__CTRACE_INPUT,
575 "inkey (state normal) got '%s'\n", unctrl(k)); 575 "inkey (state normal) got '%s'\n", unctrl(k));
576#endif 576#endif
577 577
578 working = start; 578 working = start;
579 inbuf[working] = k; 579 inbuf[working] = k;
580 INC_POINTER(working); 580 INC_POINTER(working);
581 end = working; 581 end = working;
582 582
583 /* go to the assembling state now */ 583 /* go to the assembling state now */
584 _cursesi_state = INKEY_ASSEMBLING; 584 _cursesi_state = INKEY_ASSEMBLING;
585 585
586 } else if (_cursesi_state == INKEY_BACKOUT) { 586 } else if (_cursesi_state == INKEY_BACKOUT) {
587 k = inbuf[working]; 587 k = inbuf[working];
588 INC_POINTER(working); 588 INC_POINTER(working);
589 if (working == end) { /* see if we have run 589 if (working == end) { /* see if we have run
590 * out of keys in the 590 * out of keys in the
591 * backlog */ 591 * backlog */
592 592
593 /* if we have then switch to assembling */ 593 /* if we have then switch to assembling */
594 _cursesi_state = INKEY_ASSEMBLING; 594 _cursesi_state = INKEY_ASSEMBLING;
595 } 595 }
596 } else if (_cursesi_state == INKEY_ASSEMBLING) { 596 } else if (_cursesi_state == INKEY_ASSEMBLING) {
597 /* assembling a key sequence */ 597 /* assembling a key sequence */
598 if (delay) { 598 if (delay) {
599 if (__timeout(to ? (ESCDELAY / 100) : delay) 599 if (__timeout(to ? (ESCDELAY / 100) : delay)
600 == ERR) 600 == ERR)
601 return ERR; 601 return ERR;
602 } else { 602 } else {
603 if (to && (__timeout(ESCDELAY / 100) == ERR)) 603 if (to && (__timeout(ESCDELAY / 100) == ERR))
604 return ERR; 604 return ERR;
605 } 605 }
606 606
607 c = __fgetc_resize(infd); 607 c = __fgetc_resize(infd);
608 if (ferror(infd)) { 608 if (ferror(infd)) {
609 clearerr(infd); 609 clearerr(infd);
610 return c; 610 return c;
611 } 611 }
612 612
613 if ((to || delay) && (__notimeout() == ERR)) 613 if ((to || delay) && (__notimeout() == ERR))
614 return ERR; 614 return ERR;
615 615
616#ifdef DEBUG 616#ifdef DEBUG
617 __CTRACE(__CTRACE_INPUT, 617 __CTRACE(__CTRACE_INPUT,
618 "inkey (state assembling) got '%s'\n", unctrl(k)); 618 "inkey (state assembling) got '%s'\n", unctrl(k));
619#endif 619#endif
620 if (feof(infd) || c == -1) { /* inter-char timeout, 620 if (feof(infd) || c == -1) { /* inter-char timeout,
621 * start backing out */ 621 * start backing out */
622 clearerr(infd); 622 clearerr(infd);
623 if (start == end) 623 if (start == end)
624 /* no chars in the buffer, restart */ 624 /* no chars in the buffer, restart */
625 goto reread; 625 goto reread;
626 626
627 k = inbuf[start]; 627 k = inbuf[start];
628 _cursesi_state = INKEY_TIMEOUT; 628 _cursesi_state = INKEY_TIMEOUT;
629 } else { 629 } else {
630 k = (wchar_t) c; 630 k = (wchar_t) c;
631 inbuf[working] = k; 631 inbuf[working] = k;
632 INC_POINTER(working); 632 INC_POINTER(working);
633 end = working; 633 end = working;
634 } 634 }
635 } else { 635 } else {
636 fprintf(stderr, "Inkey state screwed - exiting!!!"); 636 fprintf(stderr, "Inkey state screwed - exiting!!!");
637 exit(2); 637 exit(2);
638 } 638 }
639 639
640 /* 640 /*
641 * Check key has no special meaning and we have not 641 * Check key has no special meaning and we have not
642 * timed out and the key has not been disabled 642 * timed out and the key has not been disabled
643 */ 643 */
644 mapping = current->mapping[k]; 644 mapping = current->mapping[k];
645 if (((_cursesi_state == INKEY_TIMEOUT) || (mapping < 0)) 645 if (((_cursesi_state == INKEY_TIMEOUT) || (mapping < 0))
646 || ((current->key[mapping]->type == KEYMAP_LEAF) 646 || ((current->key[mapping]->type == KEYMAP_LEAF)
647 && (current->key[mapping]->enable == FALSE))) { 647 && (current->key[mapping]->enable == FALSE))) {
648 /* return the first key we know about */ 648 /* return the first key we know about */
649 k = inbuf[start]; 649 k = inbuf[start];
650 650
651 INC_POINTER(start); 651 INC_POINTER(start);
652 working = start; 652 working = start;
653 653
654 if (start == end) { /* only one char processed */ 654 if (start == end) { /* only one char processed */
655 _cursesi_state = INKEY_NORM; 655 _cursesi_state = INKEY_NORM;
656 } else {/* otherwise we must have more than one char 656 } else {/* otherwise we must have more than one char
657 * to backout */ 657 * to backout */
658 _cursesi_state = INKEY_BACKOUT; 658 _cursesi_state = INKEY_BACKOUT;
659 } 659 }
660 return k; 660 return k;
661 } else { /* must be part of a multikey sequence */ 661 } else { /* must be part of a multikey sequence */
662 /* check for completed key sequence */ 662 /* check for completed key sequence */
663 if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) { 663 if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
664 start = working; /* eat the key sequence 664 start = working; /* eat the key sequence
665 * in inbuf */ 665 * in inbuf */
666 666
667 /* check if inbuf empty now */ 667 /* check if inbuf empty now */
668 if (start == end) { 668 if (start == end) {
669 /* if it is go back to normal */ 669 /* if it is go back to normal */
670 _cursesi_state = INKEY_NORM; 670 _cursesi_state = INKEY_NORM;
671 } else { 671 } else {
672 /* otherwise go to backout state */ 672 /* otherwise go to backout state */
673 _cursesi_state = INKEY_BACKOUT; 673 _cursesi_state = INKEY_BACKOUT;
674 } 674 }
675 675
676 /* return the symbol */ 676 /* return the symbol */
677 return current->key[current->mapping[k]]->value.symbol; 677 return current->key[current->mapping[k]]->value.symbol;
678 678
679 } else { 679 } else {
680 /* 680 /*
681 * Step on to next part of the multi-key 681 * Step on to next part of the multi-key
682 * sequence. 682 * sequence.
683 */ 683 */
684 current = current->key[current->mapping[k]]->value.next; 684 current = current->key[current->mapping[k]]->value.next;
685 } 685 }
686 } 686 }
687 } 687 }
688} 688}
689 689
690#ifndef _CURSES_USE_MACROS 690#ifndef _CURSES_USE_MACROS
691/* 691/*
692 * getch -- 692 * getch --
693 * Read in a character from stdscr. 693 * Read in a character from stdscr.
694 */ 694 */
695int 695int
696getch(void) 696getch(void)
697{ 697{
698 return wgetch(stdscr); 698 return wgetch(stdscr);
699} 699}
700 700
701/* 701/*
702 * mvgetch -- 702 * mvgetch --
703 * Read in a character from stdscr at the given location. 703 * Read in a character from stdscr at the given location.
704 */ 704 */
705int 705int
706mvgetch(int y, int x) 706mvgetch(int y, int x)
707{ 707{
708 return mvwgetch(stdscr, y, x); 708 return mvwgetch(stdscr, y, x);
709} 709}
710 710
711/* 711/*
712 * mvwgetch -- 712 * mvwgetch --
713 * Read in a character from stdscr at the given location in the 713 * Read in a character from stdscr at the given location in the
714 * given window. 714 * given window.
715 */ 715 */
716int 716int
717mvwgetch(WINDOW *win, int y, int x) 717mvwgetch(WINDOW *win, int y, int x)
718{ 718{
719 if (wmove(win, y, x) == ERR) 719 if (wmove(win, y, x) == ERR)
720 return ERR; 720 return ERR;
721 721
722 return wgetch(win); 722 return wgetch(win);
723} 723}
724 724
725#endif 725#endif
726 726
727/* 727/*
728 * keyok -- 728 * keyok --
729 * Set the enable flag for a keysym, if the flag is false then 729 * Set the enable flag for a keysym, if the flag is false then
730 * getch will not return this keysym even if the matching key sequence 730 * getch will not return this keysym even if the matching key sequence
731 * is seen. 731 * is seen.
732 */ 732 */
733int 733int
734keyok(int key_type, bool flag) 734keyok(int key_type, bool flag)
735{ 735{
736 int result = ERR; 736 int result = ERR;
737 737
738 if (_cursesi_screen != NULL) 738 if (_cursesi_screen != NULL)
739 do_keyok(_cursesi_screen->base_keymap, key_type, 739 do_keyok(_cursesi_screen->base_keymap, key_type,
740 true, flag, &result); 740 true, flag, &result);
741 return result; 741 return result;
742} 742}
743 743
744/* 744/*
745 * do_keyok -- 745 * do_keyok --
746 * Does the actual work for keyok, we need to recurse through the 746 * Does the actual work for keyok, we need to recurse through the
747 * keymaps finding the passed key symbol. 747 * keymaps finding the passed key symbol.
748 */ 748 */
749static void 749static void
750do_keyok(keymap_t *current, int key_type, bool set, bool flag, int *retval) 750do_keyok(keymap_t *current, int key_type, bool set, bool flag, int *retval)
751{ 751{
752 key_entry_t *key; 752 key_entry_t *key;
753 int i; 753 int i;
754 754
755 /* 755 /*
756 * we need to iterate over all the keys as there may be 756 * we need to iterate over all the keys as there may be
757 * multiple instances of the leaf symbol. 757 * multiple instances of the leaf symbol.
758 */ 758 */
759 for (i = 0; i < MAX_CHAR; i++) { 759 for (i = 0; i < MAX_CHAR; i++) {
760 if (current->mapping[i] < 0) 760 if (current->mapping[i] < 0)
761 continue; /* no mapping for the key, next! */ 761 continue; /* no mapping for the key, next! */
762 762
763 key = current->key[current->mapping[i]]; 763 key = current->key[current->mapping[i]];
764 764
765 if (key->type == KEYMAP_MULTI) 765 if (key->type == KEYMAP_MULTI)
766 do_keyok(key->value.next, key_type, set, flag, retval); 766 do_keyok(key->value.next, key_type, set, flag, retval);
767 else if ((key->type == KEYMAP_LEAF) 767 else if ((key->type == KEYMAP_LEAF)
768 && (key->value.symbol == key_type)) { 768 && (key->value.symbol == key_type)) {
769 if (set) 769 if (set)
770 key->enable = flag; 770 key->enable = flag;
771 *retval = OK; /* we found at least one instance, ok */ 771 *retval = OK; /* we found at least one instance, ok */
772 } 772 }
773 } 773 }
774} 774}
775 775
776/* 776/*
777 * define_key -- 777 * define_key --
778 * Add a custom mapping of a key sequence to key symbol. 778 * Add a custom mapping of a key sequence to key symbol.
779 * 779 *
780 */ 780 */
781int 781int
782define_key(char *sequence, int symbol) 782define_key(char *sequence, int symbol)
783{ 783{
784 784
785 if (symbol <= 0 || _cursesi_screen == NULL) 785 if (symbol <= 0 || _cursesi_screen == NULL)
786 return ERR; 786 return ERR;
787 787
788 if (sequence == NULL) { 788 if (sequence == NULL) {
789#ifdef DEBUG 789#ifdef DEBUG
790 __CTRACE(__CTRACE_INPUT, "define_key: deleting keysym %d\n", 790 __CTRACE(__CTRACE_INPUT, "define_key: deleting keysym %d\n",
791 symbol); 791 symbol);
792#endif 792#endif
793 delete_key_sequence(_cursesi_screen->base_keymap, symbol); 793 delete_key_sequence(_cursesi_screen->base_keymap, symbol);
794 } else 794 } else
795 add_key_sequence(_cursesi_screen, sequence, symbol); 795 add_key_sequence(_cursesi_screen, sequence, symbol);
796 796
797 return OK; 797 return OK;
798} 798}
799 799
800/* 800/*
801 * wgetch -- 801 * wgetch --
802 * Read in a character from the window. 802 * Read in a character from the window.
803 */ 803 */
804int 804int
805wgetch(WINDOW *win) 805wgetch(WINDOW *win)
806{ 806{
807 int inp, weset; 807 int inp, weset;
808 int c; 808 int c;
809 FILE *infd = _cursesi_screen->infd; 809 FILE *infd = _cursesi_screen->infd;
810 810
811#ifdef DEBUG 811#ifdef DEBUG
812 __CTRACE(__CTRACE_INPUT, "wgetch: win(%p)\n", win); 812 __CTRACE(__CTRACE_INPUT, "wgetch: win(%p)\n", win);
813#endif 813#endif
814 if (win == NULL) 814 if (win == NULL)
815 return ERR; 815 return ERR;
816 if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN) 816 if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
817 && win->curx == win->maxx - 1 && win->cury == win->maxy - 1 817 && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
818 && __echoit) 818 && __echoit)
819 return ERR; 819 return ERR;
820 820
821 if (is_wintouched(win)) 821 if (!(win->flags & __ISPAD)) {
822 wrefresh(win); 822 if (is_wintouched(win))
823 else { 823 wrefresh(win);
824 if ((_cursesi_screen->curscr->cury != (win->begy + win->cury)) 824 else if ((_cursesi_screen->curscr->cury != (win->begy + win->cury))
825 || (_cursesi_screen->curscr->curx != (win->begx + win->curx))) { 825 || (_cursesi_screen->curscr->curx != (win->begx + win->curx))) {
826#ifdef DEBUG 826#ifdef DEBUG
827 __CTRACE(__CTRACE_INPUT, "wgetch: curscr cury %d cury %d curscr curx %d curx %d\n", 827 __CTRACE(__CTRACE_INPUT, "wgetch: curscr cury %d cury %d curscr curx %d curx %d\n",
828 _cursesi_screen->curscr->cury, win->begy + win->cury, 828 _cursesi_screen->curscr->cury, win->begy + win->cury,
829 _cursesi_screen->curscr->curx, win->begx + win->curx); 829 _cursesi_screen->curscr->curx, win->begx + win->curx);
830#endif 830#endif
831 /* 831 /*
832 * Just in case the window is not dirty but the 832 * Just in case the window is not dirty but the
833 * cursor was moved, check and update the  833 * cursor was moved, check and update the
834 * cursor location. 834 * cursor location.
835 */ 835 */
836 mvcur(_cursesi_screen->curscr->cury, 836 mvcur(_cursesi_screen->curscr->cury,
837 _cursesi_screen->curscr->curx, 837 _cursesi_screen->curscr->curx,
838 win->cury + win->begy, win->curx + win->begx); 838 win->cury + win->begy, win->curx + win->begx);
839 _cursesi_screen->curscr->cury = 839 _cursesi_screen->curscr->cury =
840 win->cury + win->begy; 840 win->cury + win->begy;
841 _cursesi_screen->curscr->curx = 841 _cursesi_screen->curscr->curx =
842 win->curx + win->begx; 842 win->curx + win->begx;
843 } 843 }
844 } 844 }
845 845
846#ifdef DEBUG 846#ifdef DEBUG
847 __CTRACE(__CTRACE_INPUT, "wgetch: __echoit = %d, " 847 __CTRACE(__CTRACE_INPUT, "wgetch: __echoit = %d, "
848 "__rawmode = %d, __nl = %d, flags = %#.4x, delay = %d\n", 848 "__rawmode = %d, __nl = %d, flags = %#.4x, delay = %d\n",
849 __echoit, __rawmode, _cursesi_screen->nl, win->flags, win->delay); 849 __echoit, __rawmode, _cursesi_screen->nl, win->flags, win->delay);
850#endif 850#endif
851 if (_cursesi_screen->resized) { 851 if (_cursesi_screen->resized) {
852 resizeterm(LINES, COLS); 852 resizeterm(LINES, COLS);
853 _cursesi_screen->resized = 0; 853 _cursesi_screen->resized = 0;
854#ifdef DEBUG 854#ifdef DEBUG
855 __CTRACE(__CTRACE_INPUT, "wgetch returning KEY_RESIZE\n"); 855 __CTRACE(__CTRACE_INPUT, "wgetch returning KEY_RESIZE\n");
856#endif 856#endif
857 return KEY_RESIZE; 857 return KEY_RESIZE;
858 } 858 }
859 if (_cursesi_screen->unget_pos) { 859 if (_cursesi_screen->unget_pos) {
860#ifdef DEBUG 860#ifdef DEBUG
861 __CTRACE(__CTRACE_INPUT, "wgetch returning char at %d\n", 861 __CTRACE(__CTRACE_INPUT, "wgetch returning char at %d\n",
862 _cursesi_screen->unget_pos); 862 _cursesi_screen->unget_pos);
863#endif 863#endif
864 _cursesi_screen->unget_pos--; 864 _cursesi_screen->unget_pos--;
865 c = _cursesi_screen->unget_list[_cursesi_screen->unget_pos]; 865 c = _cursesi_screen->unget_list[_cursesi_screen->unget_pos];
866 if (__echoit) 866 if (__echoit)
867 waddch(win, (chtype) c); 867 waddch(win, (chtype) c);
868 return c; 868 return c;
869 } 869 }
870 if (__echoit && !__rawmode) { 870 if (__echoit && !__rawmode) {
871 cbreak(); 871 cbreak();
872 weset = 1; 872 weset = 1;
873 } else 873 } else
874 weset = 0; 874 weset = 0;
875 875
876 __save_termios(); 876 __save_termios();
877 877
878 if (win->flags & __KEYPAD) { 878 if (win->flags & __KEYPAD) {
879 switch (win->delay) { 879 switch (win->delay) {
880 case -1: 880 case -1:
881 inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0); 881 inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
882 break; 882 break;
883 case 0: 883 case 0:
884 if (__nodelay() == ERR) 884 if (__nodelay() == ERR)
885 return ERR; 885 return ERR;
886 inp = inkey(0, 0); 886 inp = inkey(0, 0);
887 break; 887 break;
888 default: 888 default:
889 inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay); 889 inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
890 break; 890 break;
891 } 891 }
892 } else { 892 } else {
893 switch (win->delay) { 893 switch (win->delay) {
894 case -1: 894 case -1:
895 if (__delay() == ERR) 895 if (__delay() == ERR)
896 return ERR; 896 return ERR;
897 break; 897 break;
898 case 0: 898 case 0:
899 if (__nodelay() == ERR) 899 if (__nodelay() == ERR)
900 return ERR; 900 return ERR;
901 break; 901 break;
902 default: 902 default:
903 if (__timeout(win->delay) == ERR) 903 if (__timeout(win->delay) == ERR)
904 return ERR; 904 return ERR;
905 break; 905 break;
906 } 906 }
907 907
908 inp = __fgetc_resize(infd); 908 inp = __fgetc_resize(infd);
909 if (inp == ERR || inp == KEY_RESIZE) { 909 if (inp == ERR || inp == KEY_RESIZE) {
910 clearerr(infd); 910 clearerr(infd);
911 __restore_termios(); 911 __restore_termios();
912 return inp; 912 return inp;
913 } 913 }
914 } 914 }
915#ifdef DEBUG 915#ifdef DEBUG
916 if (inp > 255) 916 if (inp > 255)
917 /* we have a key symbol - treat it differently */ 917 /* we have a key symbol - treat it differently */
918 /* XXXX perhaps __unctrl should be expanded to include 918 /* XXXX perhaps __unctrl should be expanded to include
919 * XXXX the keysyms in the table.... 919 * XXXX the keysyms in the table....
920 */ 920 */
921 __CTRACE(__CTRACE_INPUT, "wgetch assembled keysym 0x%x\n", inp); 921 __CTRACE(__CTRACE_INPUT, "wgetch assembled keysym 0x%x\n", inp);
922 else 922 else
923 __CTRACE(__CTRACE_INPUT, "wgetch got '%s'\n", unctrl(inp)); 923 __CTRACE(__CTRACE_INPUT, "wgetch got '%s'\n", unctrl(inp));
924#endif 924#endif
925 if (win->delay > -1) { 925 if (win->delay > -1) {
926 if (__delay() == ERR) 926 if (__delay() == ERR)
927 return ERR; 927 return ERR;
928 } 928 }
929 929
930 __restore_termios(); 930 __restore_termios();
931 931
932 if ((__echoit) && (inp < KEY_MIN)) 932 if ((__echoit) && (inp < KEY_MIN))
933 waddch(win, (chtype) inp); 933 waddch(win, (chtype) inp);
934 934
935 if (weset) 935 if (weset)
936 nocbreak(); 936 nocbreak();
937 937
938 if (_cursesi_screen->nl && inp == 13) 938 if (_cursesi_screen->nl && inp == 13)
939 inp = 10; 939 inp = 10;
940 940
941 return ((inp < 0) || (inp == ERR) ? ERR : inp); 941 return ((inp < 0) || (inp == ERR) ? ERR : inp);
942} 942}
943 943
944/* 944/*
945 * ungetch -- 945 * ungetch --
946 * Put the character back into the input queue. 946 * Put the character back into the input queue.
947 */ 947 */
948int 948int
949ungetch(int c) 949ungetch(int c)
950{ 950{
951 return __unget((wint_t)c); 951 return __unget((wint_t)c);
952} 952}
953 953
954/* 954/*
955 * __unget -- 955 * __unget --
956 * Do the work for ungetch() and unget_wch(); 956 * Do the work for ungetch() and unget_wch();
957 */ 957 */
958int 958int
959__unget(wint_t c) 959__unget(wint_t c)
960{ 960{
961 wchar_t *p; 961 wchar_t *p;
962 int len; 962 int len;
963 963
964#ifdef DEBUG 964#ifdef DEBUG
965 __CTRACE(__CTRACE_INPUT, "__unget(%x)\n", c); 965 __CTRACE(__CTRACE_INPUT, "__unget(%x)\n", c);
966#endif 966#endif
967 if (_cursesi_screen == NULL) 967 if (_cursesi_screen == NULL)
968 return ERR; 968 return ERR;
969 if (_cursesi_screen->unget_pos >= _cursesi_screen->unget_len) { 969 if (_cursesi_screen->unget_pos >= _cursesi_screen->unget_len) {
970 len = _cursesi_screen->unget_len + 32; 970 len = _cursesi_screen->unget_len + 32;
971 if ((p = realloc(_cursesi_screen->unget_list, 971 if ((p = realloc(_cursesi_screen->unget_list,
972 sizeof(wchar_t) * len)) == NULL) { 972 sizeof(wchar_t) * len)) == NULL) {
973 /* Can't realloc(), so just lose the oldest entry */ 973 /* Can't realloc(), so just lose the oldest entry */
974 memmove(_cursesi_screen->unget_list, 974 memmove(_cursesi_screen->unget_list,
975 _cursesi_screen->unget_list + sizeof(wchar_t), 975 _cursesi_screen->unget_list + sizeof(wchar_t),
976 _cursesi_screen->unget_len - 1); 976 _cursesi_screen->unget_len - 1);
977 _cursesi_screen->unget_list[_cursesi_screen->unget_len 977 _cursesi_screen->unget_list[_cursesi_screen->unget_len
978 - 1] = c; 978 - 1] = c;
979 _cursesi_screen->unget_pos = 979 _cursesi_screen->unget_pos =
980 _cursesi_screen->unget_len; 980 _cursesi_screen->unget_len;
981 return OK; 981 return OK;
982 } else { 982 } else {
983 _cursesi_screen->unget_pos = 983 _cursesi_screen->unget_pos =
984 _cursesi_screen->unget_len; 984 _cursesi_screen->unget_len;
985 _cursesi_screen->unget_len = len; 985 _cursesi_screen->unget_len = len;
986 _cursesi_screen->unget_list = p; 986 _cursesi_screen->unget_list = p;
987 } 987 }
988 } 988 }
989 _cursesi_screen->unget_list[_cursesi_screen->unget_pos] = c; 989 _cursesi_screen->unget_list[_cursesi_screen->unget_pos] = c;
990 _cursesi_screen->unget_pos++; 990 _cursesi_screen->unget_pos++;
991 return OK; 991 return OK;
992} 992}
993 993
994int 994int
995has_key(int key_type) 995has_key(int key_type)
996{ 996{
997 int result = ERR; 997 int result = ERR;
998 998
999 if (_cursesi_screen != NULL) 999 if (_cursesi_screen != NULL)
1000 do_keyok(_cursesi_screen->base_keymap, key_type, 1000 do_keyok(_cursesi_screen->base_keymap, key_type,
1001 false, false, &result); 1001 false, false, &result);
1002 return result; 1002 return result;
1003} 1003}
1004 1004
1005/* 1005/*
1006 * set_escdelay -- 1006 * set_escdelay --
1007 * Sets the escape delay for the current screen. 1007 * Sets the escape delay for the current screen.
1008 */ 1008 */
1009int 1009int
1010set_escdelay(int escdelay) 1010set_escdelay(int escdelay)
1011{ 1011{
1012 1012
1013 if (_cursesi_screen == NULL) 1013 if (_cursesi_screen == NULL)
1014 return ERR; 1014 return ERR;
1015 _cursesi_screen->ESCDELAY = escdelay; 1015 _cursesi_screen->ESCDELAY = escdelay;
1016 ESCDELAY = escdelay; 1016 ESCDELAY = escdelay;
1017 return OK; 1017 return OK;
1018} 1018}
1019 1019
1020/* 1020/*
1021 * __fgetc_resize -- 1021 * __fgetc_resize --
1022 * Any call to fgetc(3) should use this function instead 1022 * Any call to fgetc(3) should use this function instead
1023 * and test for the return value of KEY_RESIZE as well as ERR. 1023 * and test for the return value of KEY_RESIZE as well as ERR.
1024 */ 1024 */
1025int 1025int
1026__fgetc_resize(FILE *infd) 1026__fgetc_resize(FILE *infd)
1027{ 1027{
1028 int c; 1028 int c;
1029 1029
1030 c = fgetc(infd); 1030 c = fgetc(infd);
1031 if (c != EOF) 1031 if (c != EOF)
1032 return c; 1032 return c;
1033 1033
1034 if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized) 1034 if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized)
1035 return ERR; 1035 return ERR;
1036#ifdef DEBUG 1036#ifdef DEBUG
1037 __CTRACE(__CTRACE_INPUT, "__fgetc_resize returning KEY_RESIZE\n"); 1037 __CTRACE(__CTRACE_INPUT, "__fgetc_resize returning KEY_RESIZE\n");
1038#endif 1038#endif
1039 resizeterm(LINES, COLS); 1039 resizeterm(LINES, COLS);
1040 _cursesi_screen->resized = 0; 1040 _cursesi_screen->resized = 0;
1041 return KEY_RESIZE; 1041 return KEY_RESIZE;
1042} 1042}