Report error if creating socket fails with -D.
[tmux-openbsd.git] / key-string.c
blob0ca913069a5fcbd4495f2033c0a4194fc3af9fce
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
25 #include "tmux.h"
27 static key_code key_string_search_table(const char *);
28 static key_code key_string_get_modifiers(const char **);
30 static const struct {
31 const char *string;
32 key_code key;
33 } key_string_table[] = {
34 /* Function keys. */
35 { "F1", KEYC_F1|KEYC_IMPLIED_META },
36 { "F2", KEYC_F2|KEYC_IMPLIED_META },
37 { "F3", KEYC_F3|KEYC_IMPLIED_META },
38 { "F4", KEYC_F4|KEYC_IMPLIED_META },
39 { "F5", KEYC_F5|KEYC_IMPLIED_META },
40 { "F6", KEYC_F6|KEYC_IMPLIED_META },
41 { "F7", KEYC_F7|KEYC_IMPLIED_META },
42 { "F8", KEYC_F8|KEYC_IMPLIED_META },
43 { "F9", KEYC_F9|KEYC_IMPLIED_META },
44 { "F10", KEYC_F10|KEYC_IMPLIED_META },
45 { "F11", KEYC_F11|KEYC_IMPLIED_META },
46 { "F12", KEYC_F12|KEYC_IMPLIED_META },
47 { "IC", KEYC_IC|KEYC_IMPLIED_META },
48 { "Insert", KEYC_IC|KEYC_IMPLIED_META },
49 { "DC", KEYC_DC|KEYC_IMPLIED_META },
50 { "Delete", KEYC_DC|KEYC_IMPLIED_META },
51 { "Home", KEYC_HOME|KEYC_IMPLIED_META },
52 { "End", KEYC_END|KEYC_IMPLIED_META },
53 { "NPage", KEYC_NPAGE|KEYC_IMPLIED_META },
54 { "PageDown", KEYC_NPAGE|KEYC_IMPLIED_META },
55 { "PgDn", KEYC_NPAGE|KEYC_IMPLIED_META },
56 { "PPage", KEYC_PPAGE|KEYC_IMPLIED_META },
57 { "PageUp", KEYC_PPAGE|KEYC_IMPLIED_META },
58 { "PgUp", KEYC_PPAGE|KEYC_IMPLIED_META },
59 { "Tab", '\011' },
60 { "BTab", KEYC_BTAB },
61 { "Space", ' ' },
62 { "BSpace", KEYC_BSPACE },
63 { "Enter", '\r' },
64 { "Escape", '\033' },
66 /* Arrow keys. */
67 { "Up", KEYC_UP|KEYC_CURSOR|KEYC_IMPLIED_META },
68 { "Down", KEYC_DOWN|KEYC_CURSOR|KEYC_IMPLIED_META },
69 { "Left", KEYC_LEFT|KEYC_CURSOR|KEYC_IMPLIED_META },
70 { "Right", KEYC_RIGHT|KEYC_CURSOR|KEYC_IMPLIED_META },
72 /* Numeric keypad. */
73 { "KP/", KEYC_KP_SLASH|KEYC_KEYPAD },
74 { "KP*", KEYC_KP_STAR|KEYC_KEYPAD },
75 { "KP-", KEYC_KP_MINUS|KEYC_KEYPAD },
76 { "KP7", KEYC_KP_SEVEN|KEYC_KEYPAD },
77 { "KP8", KEYC_KP_EIGHT|KEYC_KEYPAD },
78 { "KP9", KEYC_KP_NINE|KEYC_KEYPAD },
79 { "KP+", KEYC_KP_PLUS|KEYC_KEYPAD },
80 { "KP4", KEYC_KP_FOUR|KEYC_KEYPAD },
81 { "KP5", KEYC_KP_FIVE|KEYC_KEYPAD },
82 { "KP6", KEYC_KP_SIX|KEYC_KEYPAD },
83 { "KP1", KEYC_KP_ONE|KEYC_KEYPAD },
84 { "KP2", KEYC_KP_TWO|KEYC_KEYPAD },
85 { "KP3", KEYC_KP_THREE|KEYC_KEYPAD },
86 { "KPEnter", KEYC_KP_ENTER|KEYC_KEYPAD },
87 { "KP0", KEYC_KP_ZERO|KEYC_KEYPAD },
88 { "KP.", KEYC_KP_PERIOD|KEYC_KEYPAD },
90 /* Mouse keys. */
91 KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
92 KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
93 KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
94 KEYC_MOUSE_STRING(MOUSEDOWN6, MouseDown6),
95 KEYC_MOUSE_STRING(MOUSEDOWN7, MouseDown7),
96 KEYC_MOUSE_STRING(MOUSEDOWN8, MouseDown8),
97 KEYC_MOUSE_STRING(MOUSEDOWN9, MouseDown9),
98 KEYC_MOUSE_STRING(MOUSEDOWN10, MouseDown10),
99 KEYC_MOUSE_STRING(MOUSEDOWN11, MouseDown11),
100 KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
101 KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
102 KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
103 KEYC_MOUSE_STRING(MOUSEUP6, MouseUp6),
104 KEYC_MOUSE_STRING(MOUSEUP7, MouseUp7),
105 KEYC_MOUSE_STRING(MOUSEUP8, MouseUp8),
106 KEYC_MOUSE_STRING(MOUSEUP9, MouseUp9),
107 KEYC_MOUSE_STRING(MOUSEUP10, MouseUp10),
108 KEYC_MOUSE_STRING(MOUSEUP11, MouseUp11),
109 KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
110 KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
111 KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
112 KEYC_MOUSE_STRING(MOUSEDRAG6, MouseDrag6),
113 KEYC_MOUSE_STRING(MOUSEDRAG7, MouseDrag7),
114 KEYC_MOUSE_STRING(MOUSEDRAG8, MouseDrag8),
115 KEYC_MOUSE_STRING(MOUSEDRAG9, MouseDrag9),
116 KEYC_MOUSE_STRING(MOUSEDRAG10, MouseDrag10),
117 KEYC_MOUSE_STRING(MOUSEDRAG11, MouseDrag11),
118 KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
119 KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
120 KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
121 KEYC_MOUSE_STRING(MOUSEDRAGEND6, MouseDragEnd6),
122 KEYC_MOUSE_STRING(MOUSEDRAGEND7, MouseDragEnd7),
123 KEYC_MOUSE_STRING(MOUSEDRAGEND8, MouseDragEnd8),
124 KEYC_MOUSE_STRING(MOUSEDRAGEND9, MouseDragEnd9),
125 KEYC_MOUSE_STRING(MOUSEDRAGEND10, MouseDragEnd10),
126 KEYC_MOUSE_STRING(MOUSEDRAGEND11, MouseDragEnd11),
127 KEYC_MOUSE_STRING(WHEELUP, WheelUp),
128 KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
129 KEYC_MOUSE_STRING(SECONDCLICK1, SecondClick1),
130 KEYC_MOUSE_STRING(SECONDCLICK2, SecondClick2),
131 KEYC_MOUSE_STRING(SECONDCLICK3, SecondClick3),
132 KEYC_MOUSE_STRING(SECONDCLICK6, SecondClick6),
133 KEYC_MOUSE_STRING(SECONDCLICK7, SecondClick7),
134 KEYC_MOUSE_STRING(SECONDCLICK8, SecondClick8),
135 KEYC_MOUSE_STRING(SECONDCLICK9, SecondClick9),
136 KEYC_MOUSE_STRING(SECONDCLICK10, SecondClick10),
137 KEYC_MOUSE_STRING(SECONDCLICK11, SecondClick11),
138 KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
139 KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
140 KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
141 KEYC_MOUSE_STRING(DOUBLECLICK6, DoubleClick6),
142 KEYC_MOUSE_STRING(DOUBLECLICK7, DoubleClick7),
143 KEYC_MOUSE_STRING(DOUBLECLICK8, DoubleClick8),
144 KEYC_MOUSE_STRING(DOUBLECLICK9, DoubleClick9),
145 KEYC_MOUSE_STRING(DOUBLECLICK10, DoubleClick10),
146 KEYC_MOUSE_STRING(DOUBLECLICK11, DoubleClick11),
147 KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
148 KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
149 KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
150 KEYC_MOUSE_STRING(TRIPLECLICK6, TripleClick6),
151 KEYC_MOUSE_STRING(TRIPLECLICK7, TripleClick7),
152 KEYC_MOUSE_STRING(TRIPLECLICK8, TripleClick8),
153 KEYC_MOUSE_STRING(TRIPLECLICK9, TripleClick9),
154 KEYC_MOUSE_STRING(TRIPLECLICK10, TripleClick10),
155 KEYC_MOUSE_STRING(TRIPLECLICK11, TripleClick11)
158 /* Find key string in table. */
159 static key_code
160 key_string_search_table(const char *string)
162 u_int i, user;
164 for (i = 0; i < nitems(key_string_table); i++) {
165 if (strcasecmp(string, key_string_table[i].string) == 0)
166 return (key_string_table[i].key);
169 if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
170 return (KEYC_USER + user);
172 return (KEYC_UNKNOWN);
175 /* Find modifiers. */
176 static key_code
177 key_string_get_modifiers(const char **string)
179 key_code modifiers;
181 modifiers = 0;
182 while (((*string)[0] != '\0') && (*string)[1] == '-') {
183 switch ((*string)[0]) {
184 case 'C':
185 case 'c':
186 modifiers |= KEYC_CTRL;
187 break;
188 case 'M':
189 case 'm':
190 modifiers |= KEYC_META;
191 break;
192 case 'S':
193 case 's':
194 modifiers |= KEYC_SHIFT;
195 break;
196 default:
197 *string = NULL;
198 return (0);
200 *string += 2;
202 return (modifiers);
205 /* Lookup a string and convert to a key value. */
206 key_code
207 key_string_lookup_string(const char *string)
209 static const char *other = "!#()+,-.0123456789:;<=>'\r\t\177`/";
210 key_code key, modifiers;
211 u_int u, i;
212 struct utf8_data ud, *udp;
213 enum utf8_state more;
214 utf8_char uc;
215 char m[MB_LEN_MAX + 1];
216 int mlen;
218 /* Is this no key or any key? */
219 if (strcasecmp(string, "None") == 0)
220 return (KEYC_NONE);
221 if (strcasecmp(string, "Any") == 0)
222 return (KEYC_ANY);
224 /* Is this a hexadecimal value? */
225 if (string[0] == '0' && string[1] == 'x') {
226 if (sscanf(string + 2, "%x", &u) != 1)
227 return (KEYC_UNKNOWN);
228 if (u < 32)
229 return (u);
230 mlen = wctomb(m, u);
231 if (mlen <= 0 || mlen > MB_LEN_MAX)
232 return (KEYC_UNKNOWN);
233 m[mlen] = '\0';
235 udp = utf8_fromcstr(m);
236 if (udp == NULL ||
237 udp[0].size == 0 ||
238 udp[1].size != 0 ||
239 utf8_from_data(&udp[0], &uc) != UTF8_DONE) {
240 free(udp);
241 return (KEYC_UNKNOWN);
243 free(udp);
244 return (uc);
247 /* Check for modifiers. */
248 modifiers = 0;
249 if (string[0] == '^' && string[1] != '\0') {
250 modifiers |= KEYC_CTRL;
251 string++;
253 modifiers |= key_string_get_modifiers(&string);
254 if (string == NULL || string[0] == '\0')
255 return (KEYC_UNKNOWN);
257 /* Is this a standard ASCII key? */
258 if (string[1] == '\0' && (u_char)string[0] <= 127) {
259 key = (u_char)string[0];
260 if (key < 32)
261 return (KEYC_UNKNOWN);
262 } else {
263 /* Try as a UTF-8 key. */
264 if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
265 if (strlen(string) != ud.size)
266 return (KEYC_UNKNOWN);
267 for (i = 1; i < ud.size; i++)
268 more = utf8_append(&ud, (u_char)string[i]);
269 if (more != UTF8_DONE)
270 return (KEYC_UNKNOWN);
271 if (utf8_from_data(&ud, &uc) != UTF8_DONE)
272 return (KEYC_UNKNOWN);
273 return (uc|modifiers);
276 /* Otherwise look the key up in the table. */
277 key = key_string_search_table(string);
278 if (key == KEYC_UNKNOWN)
279 return (KEYC_UNKNOWN);
280 if (~modifiers & KEYC_META)
281 key &= ~KEYC_IMPLIED_META;
284 /* Convert the standard control keys. */
285 if (key <= 127 &&
286 (modifiers & KEYC_CTRL) &&
287 strchr(other, key) == NULL &&
288 key != 9 &&
289 key != 13 &&
290 key != 27) {
291 if (key >= 97 && key <= 122)
292 key -= 96;
293 else if (key >= 64 && key <= 95)
294 key -= 64;
295 else if (key == 32)
296 key = 0;
297 else if (key == 63)
298 key = 127;
299 else
300 return (KEYC_UNKNOWN);
301 modifiers &= ~KEYC_CTRL;
304 return (key|modifiers);
307 /* Convert a key code into string format, with prefix if necessary. */
308 const char *
309 key_string_lookup_key(key_code key, int with_flags)
311 key_code saved = key;
312 static char out[64];
313 char tmp[8];
314 const char *s;
315 u_int i;
316 struct utf8_data ud;
317 size_t off;
319 *out = '\0';
321 /* Literal keys are themselves. */
322 if (key & KEYC_LITERAL) {
323 snprintf(out, sizeof out, "%c", (int)(key & 0xff));
324 goto out;
327 /* Display C-@ as C-Space. */
328 if ((key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) == 0)
329 key = ' '|KEYC_CTRL;
331 /* Fill in the modifiers. */
332 if (key & KEYC_CTRL)
333 strlcat(out, "C-", sizeof out);
334 if (key & KEYC_META)
335 strlcat(out, "M-", sizeof out);
336 if (key & KEYC_SHIFT)
337 strlcat(out, "S-", sizeof out);
338 key &= KEYC_MASK_KEY;
340 /* Handle no key. */
341 if (key == KEYC_NONE) {
342 s = "None";
343 goto append;
346 /* Handle special keys. */
347 if (key == KEYC_UNKNOWN) {
348 s = "Unknown";
349 goto append;
351 if (key == KEYC_ANY) {
352 s = "Any";
353 goto append;
355 if (key == KEYC_FOCUS_IN) {
356 s = "FocusIn";
357 goto append;
359 if (key == KEYC_FOCUS_OUT) {
360 s = "FocusOut";
361 goto append;
363 if (key == KEYC_PASTE_START) {
364 s = "PasteStart";
365 goto append;
367 if (key == KEYC_PASTE_END) {
368 s = "PasteEnd";
369 goto append;
371 if (key == KEYC_MOUSE) {
372 s = "Mouse";
373 goto append;
375 if (key == KEYC_DRAGGING) {
376 s = "Dragging";
377 goto append;
379 if (key == KEYC_MOUSEMOVE_PANE) {
380 s = "MouseMovePane";
381 goto append;
383 if (key == KEYC_MOUSEMOVE_STATUS) {
384 s = "MouseMoveStatus";
385 goto append;
387 if (key == KEYC_MOUSEMOVE_STATUS_LEFT) {
388 s = "MouseMoveStatusLeft";
389 goto append;
391 if (key == KEYC_MOUSEMOVE_STATUS_RIGHT) {
392 s = "MouseMoveStatusRight";
393 goto append;
395 if (key == KEYC_MOUSEMOVE_BORDER) {
396 s = "MouseMoveBorder";
397 goto append;
399 if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
400 snprintf(tmp, sizeof tmp, "User%u", (u_int)(key - KEYC_USER));
401 strlcat(out, tmp, sizeof out);
402 goto out;
405 /* Try the key against the string table. */
406 for (i = 0; i < nitems(key_string_table); i++) {
407 if (key == (key_string_table[i].key & KEYC_MASK_KEY))
408 break;
410 if (i != nitems(key_string_table)) {
411 strlcat(out, key_string_table[i].string, sizeof out);
412 goto out;
415 /* Is this a Unicode key? */
416 if (KEYC_IS_UNICODE(key)) {
417 utf8_to_data(key, &ud);
418 off = strlen(out);
419 memcpy(out + off, ud.data, ud.size);
420 out[off + ud.size] = '\0';
421 goto out;
424 /* Invalid keys are errors. */
425 if (key > 255) {
426 snprintf(out, sizeof out, "Invalid#%llx", saved);
427 goto out;
430 /* Check for standard or control key. */
431 if (key <= 32) {
432 if (key == 0 || key > 26)
433 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
434 else
435 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
436 } else if (key >= 32 && key <= 126) {
437 tmp[0] = key;
438 tmp[1] = '\0';
439 } else if (key == 127)
440 xsnprintf(tmp, sizeof tmp, "C-?");
441 else if (key >= 128)
442 xsnprintf(tmp, sizeof tmp, "\\%llo", key);
444 strlcat(out, tmp, sizeof out);
445 goto out;
447 append:
448 strlcat(out, s, sizeof out);
450 out:
451 if (with_flags && (saved & KEYC_MASK_FLAGS) != 0) {
452 strlcat(out, "[", sizeof out);
453 if (saved & KEYC_LITERAL)
454 strlcat(out, "L", sizeof out);
455 if (saved & KEYC_KEYPAD)
456 strlcat(out, "K", sizeof out);
457 if (saved & KEYC_CURSOR)
458 strlcat(out, "C", sizeof out);
459 if (saved & KEYC_IMPLIED_META)
460 strlcat(out, "I", sizeof out);
461 if (saved & KEYC_BUILD_MODIFIERS)
462 strlcat(out, "B", sizeof out);
463 strlcat(out, "]", sizeof out);
465 return (out);