GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/key-string.c Lines: 0 125 0.0 %
Date: 2017-11-13 Branches: 0 119 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: key-string.c,v 1.47 2017/06/23 15:36:52 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
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.
9
 *
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.
17
 */
18
19
#include <sys/types.h>
20
21
#include <string.h>
22
23
#include "tmux.h"
24
25
static key_code	key_string_search_table(const char *);
26
static key_code	key_string_get_modifiers(const char **);
27
28
static const struct {
29
	const char     *string;
30
	key_code	key;
31
} key_string_table[] = {
32
	/* Function keys. */
33
	{ "F1",		KEYC_F1 },
34
	{ "F2",		KEYC_F2 },
35
	{ "F3",		KEYC_F3 },
36
	{ "F4",		KEYC_F4 },
37
	{ "F5",		KEYC_F5 },
38
	{ "F6",		KEYC_F6 },
39
	{ "F7",		KEYC_F7 },
40
	{ "F8",		KEYC_F8 },
41
	{ "F9",		KEYC_F9 },
42
	{ "F10",	KEYC_F10 },
43
	{ "F11",	KEYC_F11 },
44
	{ "F12",	KEYC_F12 },
45
	{ "IC",		KEYC_IC },
46
	{ "DC",		KEYC_DC },
47
	{ "Home",	KEYC_HOME },
48
	{ "End",	KEYC_END },
49
	{ "NPage",	KEYC_NPAGE },
50
	{ "PageDown",	KEYC_NPAGE },
51
	{ "PgDn",	KEYC_NPAGE },
52
	{ "PPage",	KEYC_PPAGE },
53
	{ "PageUp",	KEYC_PPAGE },
54
	{ "PgUp",	KEYC_PPAGE },
55
	{ "Tab",	'\011' },
56
	{ "BTab",	KEYC_BTAB },
57
	{ "Space",	' ' },
58
	{ "BSpace",	KEYC_BSPACE },
59
	{ "Enter",	'\r' },
60
	{ "Escape",	'\033' },
61
62
	/* Arrow keys. */
63
	{ "Up",		KEYC_UP },
64
	{ "Down",	KEYC_DOWN },
65
	{ "Left",	KEYC_LEFT },
66
	{ "Right",	KEYC_RIGHT },
67
68
	/* Numeric keypad. */
69
	{ "KP/", 	KEYC_KP_SLASH },
70
	{ "KP*",	KEYC_KP_STAR },
71
	{ "KP-",	KEYC_KP_MINUS },
72
	{ "KP7",	KEYC_KP_SEVEN },
73
	{ "KP8",	KEYC_KP_EIGHT },
74
	{ "KP9",	KEYC_KP_NINE },
75
	{ "KP+",	KEYC_KP_PLUS },
76
	{ "KP4",	KEYC_KP_FOUR },
77
	{ "KP5",	KEYC_KP_FIVE },
78
	{ "KP6",	KEYC_KP_SIX },
79
	{ "KP1",	KEYC_KP_ONE },
80
	{ "KP2",	KEYC_KP_TWO },
81
	{ "KP3",	KEYC_KP_THREE },
82
	{ "KPEnter",	KEYC_KP_ENTER },
83
	{ "KP0",	KEYC_KP_ZERO },
84
	{ "KP.",	KEYC_KP_PERIOD },
85
86
	/* Mouse keys. */
87
	KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
88
	KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
89
	KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
90
	KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
91
	KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
92
	KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
93
	KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
94
	KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
95
	KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
96
	KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
97
	KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
98
	KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
99
	KEYC_MOUSE_STRING(WHEELUP, WheelUp),
100
	KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
101
	KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
102
	KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
103
	KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
104
	KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
105
	KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
106
	KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
107
};
108
109
/* Find key string in table. */
110
static key_code
111
key_string_search_table(const char *string)
112
{
113
	u_int	i, user;
114
115
	for (i = 0; i < nitems(key_string_table); i++) {
116
		if (strcasecmp(string, key_string_table[i].string) == 0)
117
			return (key_string_table[i].key);
118
	}
119
120
	if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
121
		return (KEYC_USER + user);
122
123
	return (KEYC_UNKNOWN);
124
}
125
126
/* Find modifiers. */
127
static key_code
128
key_string_get_modifiers(const char **string)
129
{
130
	key_code	modifiers;
131
132
	modifiers = 0;
133
	while (((*string)[0] != '\0') && (*string)[1] == '-') {
134
		switch ((*string)[0]) {
135
		case 'C':
136
		case 'c':
137
			modifiers |= KEYC_CTRL;
138
			break;
139
		case 'M':
140
		case 'm':
141
			modifiers |= KEYC_ESCAPE;
142
			break;
143
		case 'S':
144
		case 's':
145
			modifiers |= KEYC_SHIFT;
146
			break;
147
		default:
148
			*string = NULL;
149
			return (0);
150
		}
151
		*string += 2;
152
	}
153
	return (modifiers);
154
}
155
156
/* Lookup a string and convert to a key value. */
157
key_code
158
key_string_lookup_string(const char *string)
159
{
160
	static const char	*other = "!#()+,-.0123456789:;<=>?'\r\t";
161
	key_code		 key;
162
	u_int			 u;
163
	key_code		 modifiers;
164
	struct utf8_data	 ud;
165
	u_int			 i;
166
	enum utf8_state		 more;
167
	wchar_t			 wc;
168
169
	/* Is this no key? */
170
	if (strcasecmp(string, "None") == 0)
171
		return (KEYC_NONE);
172
173
	/* Is this a hexadecimal value? */
174
	if (string[0] == '0' && string[1] == 'x') {
175
	        if (sscanf(string + 2, "%x", &u) != 1)
176
	                return (KEYC_UNKNOWN);
177
		if (u > 0x1fffff)
178
	                return (KEYC_UNKNOWN);
179
	        return (u);
180
	}
181
182
	/* Check for modifiers. */
183
	modifiers = 0;
184
	if (string[0] == '^' && string[1] != '\0') {
185
		modifiers |= KEYC_CTRL;
186
		string++;
187
	}
188
	modifiers |= key_string_get_modifiers(&string);
189
	if (string == NULL || string[0] == '\0')
190
		return (KEYC_UNKNOWN);
191
192
	/* Is this a standard ASCII key? */
193
	if (string[1] == '\0' && (u_char)string[0] <= 127) {
194
		key = (u_char)string[0];
195
		if (key < 32 || key == 127)
196
			return (KEYC_UNKNOWN);
197
	} else {
198
		/* Try as a UTF-8 key. */
199
		if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
200
			if (strlen(string) != ud.size)
201
				return (KEYC_UNKNOWN);
202
			for (i = 1; i < ud.size; i++)
203
				more = utf8_append(&ud, (u_char)string[i]);
204
			if (more != UTF8_DONE)
205
				return (KEYC_UNKNOWN);
206
			if (utf8_combine(&ud, &wc) != UTF8_DONE)
207
				return (KEYC_UNKNOWN);
208
			return (wc | modifiers);
209
		}
210
211
		/* Otherwise look the key up in the table. */
212
		key = key_string_search_table(string);
213
		if (key == KEYC_UNKNOWN)
214
			return (KEYC_UNKNOWN);
215
	}
216
217
	/* Convert the standard control keys. */
218
	if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
219
		if (key >= 97 && key <= 122)
220
			key -= 96;
221
		else if (key >= 64 && key <= 95)
222
			key -= 64;
223
		else if (key == 32)
224
			key = 0;
225
		else if (key == 63)
226
			key = KEYC_BSPACE;
227
		else
228
			return (KEYC_UNKNOWN);
229
		modifiers &= ~KEYC_CTRL;
230
	}
231
232
	return (key | modifiers);
233
}
234
235
/* Convert a key code into string format, with prefix if necessary. */
236
const char *
237
key_string_lookup_key(key_code key)
238
{
239
	static char		out[32];
240
	char			tmp[8];
241
	u_int			i;
242
	struct utf8_data	ud;
243
	size_t			off;
244
245
	*out = '\0';
246
247
	/* Handle no key. */
248
	if (key == KEYC_NONE)
249
		return ("None");
250
251
	/* Handle special keys. */
252
	if (key == KEYC_UNKNOWN)
253
		return ("Unknown");
254
	if (key == KEYC_FOCUS_IN)
255
		return ("FocusIn");
256
	if (key == KEYC_FOCUS_OUT)
257
		return ("FocusOut");
258
	if (key == KEYC_PASTE_START)
259
		return ("PasteStart");
260
	if (key == KEYC_PASTE_END)
261
		return ("PasteEnd");
262
	if (key == KEYC_MOUSE)
263
		return ("Mouse");
264
	if (key == KEYC_DRAGGING)
265
		return ("Dragging");
266
	if (key == KEYC_MOUSEMOVE_PANE)
267
		return ("MouseMovePane");
268
	if (key == KEYC_MOUSEMOVE_STATUS)
269
		return ("MouseMoveStatus");
270
	if (key == KEYC_MOUSEMOVE_BORDER)
271
		return ("MouseMoveBorder");
272
	if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
273
		snprintf(out, sizeof out, "User%u", (u_int)(key - KEYC_USER));
274
		return (out);
275
	}
276
277
	/*
278
	 * Special case: display C-@ as C-Space. Could do this below in
279
	 * the (key >= 0 && key <= 32), but this way we let it be found
280
	 * in key_string_table, for the unlikely chance that we might
281
	 * change its name.
282
	 */
283
	if ((key & KEYC_MASK_KEY) == 0)
284
	    key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
285
286
	/* Fill in the modifiers. */
287
	if (key & KEYC_CTRL)
288
		strlcat(out, "C-", sizeof out);
289
	if (key & KEYC_ESCAPE)
290
		strlcat(out, "M-", sizeof out);
291
	if (key & KEYC_SHIFT)
292
		strlcat(out, "S-", sizeof out);
293
	key &= KEYC_MASK_KEY;
294
295
	/* Try the key against the string table. */
296
	for (i = 0; i < nitems(key_string_table); i++) {
297
		if (key == key_string_table[i].key)
298
			break;
299
	}
300
	if (i != nitems(key_string_table)) {
301
		strlcat(out, key_string_table[i].string, sizeof out);
302
		return (out);
303
	}
304
305
	/* Is this a UTF-8 key? */
306
	if (key > 127 && key < KEYC_BASE) {
307
		if (utf8_split(key, &ud) == UTF8_DONE) {
308
			off = strlen(out);
309
			memcpy(out + off, ud.data, ud.size);
310
			out[off + ud.size] = '\0';
311
			return (out);
312
		}
313
	}
314
315
	/* Invalid keys are errors. */
316
	if (key == 127 || key > 255) {
317
		snprintf(out, sizeof out, "Invalid#%llx", key);
318
		return (out);
319
	}
320
321
	/* Check for standard or control key. */
322
	if (key <= 32) {
323
		if (key == 0 || key > 26)
324
			xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
325
		else
326
			xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
327
	} else if (key >= 32 && key <= 126) {
328
		tmp[0] = key;
329
		tmp[1] = '\0';
330
	} else if (key >= 128)
331
		xsnprintf(tmp, sizeof tmp, "\\%llo", key);
332
333
	strlcat(out, tmp, sizeof out);
334
	return (out);
335
}