vm: fix a null dereference on out-of-memory
[minix.git] / lib / libcurses / keyname.c
blob2adef1d017323aa40fa032eebc41b84636c4bdcb
1 /* $NetBSD: keyname.c,v 1.6 2008/04/28 20:23:01 martin Exp $ */
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julian Coleman.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: keyname.c,v 1.6 2008/04/28 20:23:01 martin Exp $");
35 #endif /* not lint */
37 #include <stdlib.h>
38 #include <string.h>
40 #include "curses.h"
41 #include "curses_private.h"
43 #define KEYNAMEMAX (size_t) 14 /* "KEY_BACKSPACE\0" */
44 static char name[KEYNAMEMAX + 1];
47 * keyname --
48 * Return name of key or NULL;
50 char *
51 keyname(int key)
53 /* We don't bother with the large keyname table if SMALL is defined. */
54 #ifdef SMALL
55 strcpy(name, "-1\0");
56 return name;
57 #else
58 if (key < 0) {
59 strcpy(name, "-1\0");
60 return name;
63 /* No name. */
64 if (key == 0x100) {
65 strcpy(name, "-1\0");
66 return name;
69 /* Control codes */
70 if (key < 0x20) {
71 name[0] = '^';
72 name[1] = (char) (key + 64); /* Offset of '@' */
73 name[2] = '\0';
74 return name;
77 /* "Normal" keys */
78 if (key < 0x7F) {
79 name[0] = (char) key;
80 name[1] = '\0';
81 return name;
84 /* Delete key */
85 if (key == 0x7F) {
86 strcpy(name, "^?\0");
87 return name;
90 /* Meta + control codes */
91 if (key < 0x9F) {
92 strcpy(name, "M-^");
93 name[3] = (char) (key - 64); /* Offset of '@' */
94 name[4] = '\0';
95 return name;
98 /* Meta + "normal" keys */
99 if (key < 0xFF) {
100 strcpy (name, "M-");
101 name[2] = (char) (key - 128);
102 name[3] = '\0';
103 return name;
106 /* Meta + delete key */
107 if (key == 0xFF) {
108 strcpy(name, "M-^?\0");
109 return name;
112 /* Key names. Synchronise this with curses.h. */
113 if (key == 0x101) {
114 strncpy(name, "KEY_BREAK\0", KEYNAMEMAX);
115 return name;
117 if (key == 0x102) {
118 strncpy(name, "KEY_DOWN\0", KEYNAMEMAX);
119 return name;
121 if (key == 0x103) {
122 strncpy(name, "KEY_UP\0", KEYNAMEMAX);
123 return name;
125 if (key == 0x104) {
126 strncpy(name, "KEY_LEFT\0", KEYNAMEMAX);
127 return name;
129 if (key == 0x105) {
130 strncpy(name, "KEY_RIGHT\0", KEYNAMEMAX);
131 return name;
133 if (key == 0x106) {
134 strncpy(name, "KEY_HOME\0", KEYNAMEMAX);
135 return name;
137 if (key == 0x107) {
138 strncpy(name, "KEY_BACKSPACE\0", KEYNAMEMAX);
139 return name;
141 /* Function key block (64 keys). */
142 if (key < 0x148) {
143 int i;
145 strcpy(name, "KEY_F(");
146 i = snprintf(&name[6], (size_t) 3, "%d", key - 0x108);
147 name[6 + i] = ')';
148 name[7 + i] = '\0';
149 return name;
151 if (key == 0x148) {
152 strncpy(name, "KEY_DL\0", KEYNAMEMAX);
153 return name;
155 if (key == 0x149) {
156 strncpy(name, "KEY_IL\0", KEYNAMEMAX);
157 return name;
159 if (key == 0x14A) {
160 strncpy(name, "KEY_DC\0", KEYNAMEMAX);
161 return name;
163 if (key == 0x14B) {
164 strncpy(name, "KEY_IC\0", KEYNAMEMAX);
165 return name;
167 if (key == 0x14C) {
168 strncpy(name, "KEY_EIC\0", KEYNAMEMAX);
169 return name;
171 if (key == 0x14D) {
172 strncpy(name, "KEY_CLEAR\0", KEYNAMEMAX);
173 return name;
175 if (key == 0x14E) {
176 strncpy(name, "KEY_EOS\0", KEYNAMEMAX);
177 return name;
179 if (key == 0x14F) {
180 strncpy(name, "KEY_EOL\0", KEYNAMEMAX);
181 return name;
183 if (key == 0x150) {
184 strncpy(name, "KEY_SF\0", KEYNAMEMAX);
185 return name;
187 if (key == 0x151) {
188 strncpy(name, "KEY_SR\0", KEYNAMEMAX);
189 return name;
191 if (key == 0x152) {
192 strncpy(name, "KEY_NPAGE\0", KEYNAMEMAX);
193 return name;
195 if (key == 0x153) {
196 strncpy(name, "KEY_PPAGE\0", KEYNAMEMAX);
197 return name;
199 if (key == 0x154) {
200 strncpy(name, "KEY_STAB\0", KEYNAMEMAX);
201 return name;
203 if (key == 0x155) {
204 strncpy(name, "KEY_CTAB\0", KEYNAMEMAX);
205 return name;
207 if (key == 0x156) {
208 strncpy(name, "KEY_CATAB\0", KEYNAMEMAX);
209 return name;
211 if (key == 0x157) {
212 strncpy(name, "KEY_ENTER\0", KEYNAMEMAX);
213 return name;
215 if (key == 0x158) {
216 strncpy(name, "KEY_SRESET\0", KEYNAMEMAX);
217 return name;
219 if (key == 0x159) {
220 strncpy(name, "KEY_RESET\0", KEYNAMEMAX);
221 return name;
223 if (key == 0x15A) {
224 strncpy(name, "KEY_PRINT\0", KEYNAMEMAX);
225 return name;
227 if (key == 0x15B) {
228 strncpy(name, "KEY_LL\0", KEYNAMEMAX);
229 return name;
231 if (key == 0x15C) {
232 strncpy(name, "KEY_A1\0", KEYNAMEMAX);
233 return name;
235 if (key == 0x15D) {
236 strncpy(name, "KEY_A3\0", KEYNAMEMAX);
237 return name;
239 if (key == 0x15E) {
240 strncpy(name, "KEY_B2\0", KEYNAMEMAX);
241 return name;
243 if (key == 0x15F) {
244 strncpy(name, "KEY_C1\0", KEYNAMEMAX);
245 return name;
247 if (key == 0x160) {
248 strncpy(name, "KEY_C3\0", KEYNAMEMAX);
249 return name;
251 if (key == 0x161) {
252 strncpy(name, "KEY_BTAB\0", KEYNAMEMAX);
253 return name;
255 if (key == 0x162) {
256 strncpy(name, "KEY_BEG\0", KEYNAMEMAX);
257 return name;
259 if (key == 0x163) {
260 strncpy(name, "KEY_CANCEL\0", KEYNAMEMAX);
261 return name;
263 if (key == 0x164) {
264 strncpy(name, "KEY_CLOSE\0", KEYNAMEMAX);
265 return name;
267 if (key == 0x165) {
268 strncpy(name, "KEY_COMMAND\0", KEYNAMEMAX);
269 return name;
271 if (key == 0x166) {
272 strncpy(name, "KEY_COPY\0", KEYNAMEMAX);
273 return name;
275 if (key == 0x167) {
276 strncpy(name, "KEY_CREATE\0", KEYNAMEMAX);
277 return name;
279 if (key == 0x168) {
280 strncpy(name, "KEY_END\0", KEYNAMEMAX);
281 return name;
283 if (key == 0x169) {
284 strncpy(name, "KEY_EXIT\0", KEYNAMEMAX);
285 return name;
287 if (key == 0x16A) {
288 strncpy(name, "KEY_FIND\0", KEYNAMEMAX);
289 return name;
291 if (key == 0x16B) {
292 strncpy(name, "KEY_HELP\0", KEYNAMEMAX);
293 return name;
295 if (key == 0x16C) {
296 strncpy(name, "KEY_MARK\0", KEYNAMEMAX);
297 return name;
299 if (key == 0x16D) {
300 strncpy(name, "KEY_MESSAGE\0", KEYNAMEMAX);
301 return name;
303 if (key == 0x16E) {
304 strncpy(name, "KEY_MOVE\0", KEYNAMEMAX);
305 return name;
307 if (key == 0x16F) {
308 strncpy(name, "KEY_NEXT\0", KEYNAMEMAX);
309 return name;
311 if (key == 0x170) {
312 strncpy(name, "KEY_OPEN\0", KEYNAMEMAX);
313 return name;
315 if (key == 0x171) {
316 strncpy(name, "KEY_OPTIONS\0", KEYNAMEMAX);
317 return name;
319 if (key == 0x172) {
320 strncpy(name, "KEY_PREVIOUS\0", KEYNAMEMAX);
321 return name;
323 if (key == 0x173) {
324 strncpy(name, "KEY_REDO\0", KEYNAMEMAX);
325 return name;
327 if (key == 0x174) {
328 strncpy(name, "KEY_REFERENCE\0", KEYNAMEMAX);
329 return name;
331 if (key == 0x175) {
332 strncpy(name, "KEY_REFRESH\0", KEYNAMEMAX);
333 return name;
335 if (key == 0x176) {
336 strncpy(name, "KEY_REPLACE\0", KEYNAMEMAX);
337 return name;
339 if (key == 0x177) {
340 strncpy(name, "KEY_RESTART\0", KEYNAMEMAX);
341 return name;
343 if (key == 0x178) {
344 strncpy(name, "KEY_RESUME\0", KEYNAMEMAX);
345 return name;
347 if (key == 0x179) {
348 strncpy(name, "KEY_SAVE\0", KEYNAMEMAX);
349 return name;
351 if (key == 0x17A) {
352 strncpy(name, "KEY_SBEG\0", KEYNAMEMAX);
353 return name;
355 if (key == 0x17B) {
356 strncpy(name, "KEY_SCANCEL\0", KEYNAMEMAX);
357 return name;
359 if (key == 0x17C) {
360 strncpy(name, "KEY_SCOMMAND\0", KEYNAMEMAX);
361 return name;
363 if (key == 0x17D) {
364 strncpy(name, "KEY_SCOPY\0", KEYNAMEMAX);
365 return name;
367 if (key == 0x17E) {
368 strncpy(name, "KEY_SCREATE\0", KEYNAMEMAX);
369 return name;
371 if (key == 0x17F) {
372 strncpy(name, "KEY_SDC\0", KEYNAMEMAX);
373 return name;
375 if (key == 0x180) {
376 strncpy(name, "KEY_SDL\0", KEYNAMEMAX);
377 return name;
379 if (key == 0x181) {
380 strncpy(name, "KEY_SELECT\0", KEYNAMEMAX);
381 return name;
383 if (key == 0x182) {
384 strncpy(name, "KEY_SEND\0", KEYNAMEMAX);
385 return name;
387 if (key == 0x183) {
388 strncpy(name, "KEY_SEOL\0", KEYNAMEMAX);
389 return name;
391 if (key == 0x184) {
392 strncpy(name, "KEY_SEXIT\0", KEYNAMEMAX);
393 return name;
395 if (key == 0x185) {
396 strncpy(name, "KEY_SFIND\0", KEYNAMEMAX);
397 return name;
399 if (key == 0x186) {
400 strncpy(name, "KEY_SHELP\0", KEYNAMEMAX);
401 return name;
403 if (key == 0x187) {
404 strncpy(name, "KEY_SHOME\0", KEYNAMEMAX);
405 return name;
407 if (key == 0x188) {
408 strncpy(name, "KEY_SIC\0", KEYNAMEMAX);
409 return name;
411 if (key == 0x189) {
412 strncpy(name, "KEY_SLEFT\0", KEYNAMEMAX);
413 return name;
415 if (key == 0x18A) {
416 strncpy(name, "KEY_SMESSAGE\0", KEYNAMEMAX);
417 return name;
419 if (key == 0x18B) {
420 strncpy(name, "KEY_SMOVE\0", KEYNAMEMAX);
421 return name;
423 if (key == 0x18C) {
424 strncpy(name, "KEY_SNEXT\0", KEYNAMEMAX);
425 return name;
427 if (key == 0x18D) {
428 strncpy(name, "KEY_SOPTIONS\0", KEYNAMEMAX);
429 return name;
431 if (key == 0x18E) {
432 strncpy(name, "KEY_SPREVIOUS\0", KEYNAMEMAX);
433 return name;
435 if (key == 0x18F) {
436 strncpy(name, "KEY_SPRINT\0", KEYNAMEMAX);
437 return name;
439 if (key == 0x190) {
440 strncpy(name, "KEY_SREDO\0", KEYNAMEMAX);
441 return name;
443 if (key == 0x191) {
444 strncpy(name, "KEY_SREPLACE\0", KEYNAMEMAX);
445 return name;
447 if (key == 0x192) {
448 strncpy(name, "KEY_SRIGHT\0", KEYNAMEMAX);
449 return name;
451 if (key == 0x193) {
452 strncpy(name, "KEY_SRSUME\0", KEYNAMEMAX);
453 return name;
455 if (key == 0x194) {
456 strncpy(name, "KEY_SSAVE\0", KEYNAMEMAX);
457 return name;
459 if (key == 0x195) {
460 strncpy(name, "KEY_SSUSPEND\0", KEYNAMEMAX);
461 return name;
463 if (key == 0x196) {
464 strncpy(name, "KEY_SUNDO\0", KEYNAMEMAX);
465 return name;
467 if (key == 0x197) {
468 strncpy(name, "KEY_SUSPEND\0", KEYNAMEMAX);
469 return name;
471 if (key == 0x198) {
472 strncpy(name, "KEY_UNDO\0", KEYNAMEMAX);
473 return name;
475 if (key == 0x199) {
476 strncpy(name, "KEY_MOUSE\0", KEYNAMEMAX);
477 return name;
479 if (key == 0x200) {
480 strncpy(name, "KEY_RESIZE\0", KEYNAMEMAX);
481 return name;
483 /* No more names. */
484 strncpy(name, "UNKOWN KEY\0", KEYNAMEMAX);
485 return name;
486 #endif
489 * key_name --
490 * Return name of key or NULL;
492 char *
493 key_name(wchar_t key)
495 #ifndef HAVE_WCHAR
496 return NULL;
497 #else
498 (void) keyname((int) key);
500 if (!strncmp(name, "M-", 2)) {
501 /* Remove the "M-" */
502 name[0] = name[2];
503 name[1] = '\0';
505 return name;
506 #endif /* HAVE_WCHAR */