GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/xterm-keys.c Lines: 0 62 0.0 %
Date: 2016-12-06 Branches: 0 48 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: xterm-keys.c,v 1.18 2016/01/19 15:59:12 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 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
/*
26
 * xterm-style function keys append one of the following values before the last
27
 * character:
28
 *
29
 * 2 Shift
30
 * 3 Alt
31
 * 4 Shift + Alt
32
 * 5 Ctrl
33
 * 6 Shift + Ctrl
34
 * 7 Alt + Ctrl
35
 * 8 Shift + Alt + Ctrl
36
 *
37
 * Rather than parsing them, just match against a table.
38
 *
39
 * There are three forms for F1-F4 (\\033O_P and \\033O1;_P and \\033[1;_P).
40
 * We accept any but always output the latter (it comes first in the table).
41
 */
42
43
int	xterm_keys_match(const char *, const char *, size_t, size_t *,
44
	    key_code *);
45
int	xterm_keys_modifiers(const char *, size_t, size_t *, key_code *);
46
47
struct xterm_keys_entry {
48
	key_code	 key;
49
	const char	*template;
50
};
51
52
const struct xterm_keys_entry xterm_keys_table[] = {
53
	{ KEYC_F1,	"\033[1;_P" },
54
	{ KEYC_F1,	"\033O1;_P" },
55
	{ KEYC_F1,	"\033O_P" },
56
	{ KEYC_F2,	"\033[1;_Q" },
57
	{ KEYC_F2,	"\033O1;_Q" },
58
	{ KEYC_F2,	"\033O_Q" },
59
	{ KEYC_F3,	"\033[1;_R" },
60
	{ KEYC_F3,	"\033O1;_R" },
61
	{ KEYC_F3,	"\033O_R" },
62
	{ KEYC_F4,	"\033[1;_S" },
63
	{ KEYC_F4,	"\033O1;_S" },
64
	{ KEYC_F4,	"\033O_S" },
65
	{ KEYC_F5,	"\033[15;_~" },
66
	{ KEYC_F6,	"\033[17;_~" },
67
	{ KEYC_F7,	"\033[18;_~" },
68
	{ KEYC_F8,	"\033[19;_~" },
69
	{ KEYC_F9,	"\033[20;_~" },
70
	{ KEYC_F10,	"\033[21;_~" },
71
	{ KEYC_F11,	"\033[23;_~" },
72
	{ KEYC_F12,	"\033[24;_~" },
73
	{ KEYC_UP,	"\033[1;_A" },
74
	{ KEYC_DOWN,	"\033[1;_B" },
75
	{ KEYC_RIGHT,	"\033[1;_C" },
76
	{ KEYC_LEFT,	"\033[1;_D" },
77
	{ KEYC_HOME,	"\033[1;_H" },
78
	{ KEYC_END,	"\033[1;_F" },
79
	{ KEYC_PPAGE,	"\033[5;_~" },
80
	{ KEYC_NPAGE,	"\033[6;_~" },
81
	{ KEYC_IC,	"\033[2;_~" },
82
	{ KEYC_DC,	"\033[3;_~" },
83
84
	{ '!',          "\033[27;_;33~" },
85
	{ '#',		"\033[27;_;35~" },
86
	{ '(',		"\033[27;_;40~" },
87
	{ ')',		"\033[27;_;41~" },
88
	{ '+',		"\033[27;_;43~" },
89
	{ ',',		"\033[27;_;44~" },
90
	{ '-',		"\033[27;_;45~" },
91
	{ '.',		"\033[27;_;46~" },
92
	{ '0',		"\033[27;_;48~" },
93
	{ '1',		"\033[27;_;49~" },
94
	{ '2',		"\033[27;_;50~" },
95
	{ '3',		"\033[27;_;51~" },
96
	{ '4',		"\033[27;_;52~" },
97
	{ '5',		"\033[27;_;53~" },
98
	{ '6',		"\033[27;_;54~" },
99
	{ '7',		"\033[27;_;55~" },
100
	{ '8',		"\033[27;_;56~" },
101
	{ '9',		"\033[27;_;57~" },
102
	{ ':',		"\033[27;_;58~" },
103
	{ ';',		"\033[27;_;59~" },
104
	{ '<',		"\033[27;_;60~" },
105
	{ '=',		"\033[27;_;61~" },
106
	{ '>',		"\033[27;_;62~" },
107
	{ '?',		"\033[27;_;63~" },
108
	{ '\'',		"\033[27;_;39~" },
109
	{ '\r',		"\033[27;_;13~" },
110
	{ '\t',		"\033[27;_;9~" },
111
};
112
113
/*
114
 * Match key against buffer, treating _ as a wildcard. Return -1 for no match,
115
 * 0 for match, 1 if the end of the buffer is reached (need more data).
116
 */
117
int
118
xterm_keys_match(const char *template, const char *buf, size_t len,
119
    size_t *size, key_code *modifiers)
120
{
121
	size_t	pos;
122
	int	retval;
123
124
	*modifiers = 0;
125
126
	if (len == 0)
127
		return (0);
128
129
	pos = 0;
130
	do {
131
		if (*template == '_') {
132
			retval = xterm_keys_modifiers(buf, len, &pos,
133
			    modifiers);
134
			if (retval != 0)
135
				return (retval);
136
			continue;
137
		}
138
		if (buf[pos] != *template)
139
			return (-1);
140
		pos++;
141
	} while (*++template != '\0' && pos != len);
142
143
	if (*template != '\0')	/* partial */
144
		return (1);
145
146
	*size = pos;
147
	return (0);
148
}
149
150
/* Find modifiers from buffer. */
151
int
152
xterm_keys_modifiers(const char *buf, size_t len, size_t *pos,
153
    key_code *modifiers)
154
{
155
	u_int	flags;
156
157
	if (len - *pos < 2)
158
		return (1);
159
160
	if (buf[*pos] < '0' || buf[*pos] > '9')
161
		return (-1);
162
	flags = buf[(*pos)++] - '0';
163
	if (buf[*pos] >= '0' && buf[*pos] <= '9')
164
		flags = (flags * 10) + (buf[(*pos)++] - '0');
165
	flags -= 1;
166
167
	*modifiers = 0;
168
	if (flags & 1)
169
		*modifiers |= KEYC_SHIFT;
170
	if (flags & 2)
171
		*modifiers |= KEYC_ESCAPE;
172
	if (flags & 4)
173
		*modifiers |= KEYC_CTRL;
174
	if (flags & 8)
175
		*modifiers |= KEYC_ESCAPE;
176
	return (0);
177
}
178
179
/*
180
 * Lookup key from a buffer against the table. Returns 0 for found (and the
181
 * key), -1 for not found, 1 for partial match.
182
 */
183
int
184
xterm_keys_find(const char *buf, size_t len, size_t *size, key_code *key)
185
{
186
	const struct xterm_keys_entry	*entry;
187
	u_int				 i;
188
	int				 matched;
189
	key_code			 modifiers;
190
191
	for (i = 0; i < nitems(xterm_keys_table); i++) {
192
		entry = &xterm_keys_table[i];
193
194
		matched = xterm_keys_match(entry->template, buf, len, size,
195
		    &modifiers);
196
		if (matched == -1)
197
			continue;
198
		if (matched == 0)
199
			*key = entry->key | modifiers;
200
		return (matched);
201
	}
202
	return (-1);
203
}
204
205
/* Lookup a key number from the table. */
206
char *
207
xterm_keys_lookup(key_code key)
208
{
209
	const struct xterm_keys_entry	*entry;
210
	u_int				 i;
211
	key_code			 modifiers;
212
	char				*out;
213
214
	modifiers = 1;
215
	if (key & KEYC_SHIFT)
216
		modifiers += 1;
217
	if (key & KEYC_ESCAPE)
218
		modifiers += 2;
219
	if (key & KEYC_CTRL)
220
		modifiers += 4;
221
222
	/*
223
	 * If the key has no modifiers, return NULL and let it fall through to
224
	 * the normal lookup.
225
	 */
226
	if (modifiers == 1)
227
		return (NULL);
228
229
	/* Otherwise, find the key in the table. */
230
	key &= ~(KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL);
231
	for (i = 0; i < nitems(xterm_keys_table); i++) {
232
		entry = &xterm_keys_table[i];
233
		if (key == entry->key)
234
			break;
235
	}
236
	if (i == nitems(xterm_keys_table))
237
		return (NULL);
238
239
	/* Copy the template and replace the modifier. */
240
	out = xstrdup(entry->template);
241
	out[strcspn(out, "_")] = '0' + modifiers;
242
	return (out);
243
}