xref: /openbsd-src/usr.bin/tmux/key-string.c (revision 6f05df2d9be0954bec42d51d943d77bd250fb664)
1 /* $OpenBSD: key-string.c,v 1.25 2014/07/21 10:25:48 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 int	key_string_search_table(const char *);
26 int	key_string_get_modifiers(const char **);
27 
28 const struct {
29 	const char     *string;
30 	int	 	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 
87 /* Find key string in table. */
88 int
89 key_string_search_table(const char *string)
90 {
91 	u_int	i;
92 
93 	for (i = 0; i < nitems(key_string_table); i++) {
94 		if (strcasecmp(string, key_string_table[i].string) == 0)
95 			return (key_string_table[i].key);
96 	}
97 	return (KEYC_NONE);
98 }
99 
100 /* Find modifiers. */
101 int
102 key_string_get_modifiers(const char **string)
103 {
104 	int	modifiers;
105 
106 	modifiers = 0;
107 	while (((*string)[0] != '\0') && (*string)[1] == '-') {
108 		switch ((*string)[0]) {
109 		case 'C':
110 		case 'c':
111 			modifiers |= KEYC_CTRL;
112 			break;
113 		case 'M':
114 		case 'm':
115 			modifiers |= KEYC_ESCAPE;
116 			break;
117 		case 'S':
118 		case 's':
119 			modifiers |= KEYC_SHIFT;
120 			break;
121 		}
122 		*string += 2;
123 	}
124 	return (modifiers);
125 }
126 
127 /* Lookup a string and convert to a key value. */
128 int
129 key_string_lookup_string(const char *string)
130 {
131 	static const char	*other = "!#()+,-.0123456789:;<=>?'\r\t";
132 	int			 key, modifiers;
133 	u_short			 u;
134 	int			 size;
135 
136 	/* Is this a hexadecimal value? */
137 	if (string[0] == '0' && string[1] == 'x') {
138 	        if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4)
139 	                return (KEYC_NONE);
140 	        return (u);
141 	}
142 
143 	/* Check for modifiers. */
144 	modifiers = 0;
145 	if (string[0] == '^' && string[1] != '\0') {
146 		modifiers |= KEYC_CTRL;
147 		string++;
148 	}
149 	modifiers |= key_string_get_modifiers(&string);
150 	if (string[0] == '\0')
151 		return (KEYC_NONE);
152 
153 	/* Is this a standard ASCII key? */
154 	if (string[1] == '\0') {
155 		key = (u_char) string[0];
156 		if (key < 32 || key == 127 || key > 255)
157 			return (KEYC_NONE);
158 	} else {
159 		/* Otherwise look the key up in the table. */
160 		key = key_string_search_table(string);
161 		if (key == KEYC_NONE)
162 			return (KEYC_NONE);
163 	}
164 
165 	/* Convert the standard control keys. */
166 	if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
167 		if (key >= 97 && key <= 122)
168 			key -= 96;
169 		else if (key >= 64 && key <= 95)
170 			key -= 64;
171 		else if (key == 32)
172 			key = 0;
173 		else if (key == 63)
174 			key = KEYC_BSPACE;
175 		else
176 			return (KEYC_NONE);
177 		modifiers &= ~KEYC_CTRL;
178 	}
179 
180 	return (key | modifiers);
181 }
182 
183 /* Convert a key code into string format, with prefix if necessary. */
184 const char *
185 key_string_lookup_key(int key)
186 {
187 	static char	out[24];
188 	char		tmp[8];
189 	u_int		i;
190 
191 	*out = '\0';
192 
193 	/* Handle no key. */
194 	if (key == KEYC_NONE)
195 		return ("none");
196 
197 	/*
198 	 * Special case: display C-@ as C-Space. Could do this below in
199 	 * the (key >= 0 && key <= 32), but this way we let it be found
200 	 * in key_string_table, for the unlikely chance that we might
201 	 * change its name.
202 	 */
203 	if ((key & KEYC_MASK_KEY) == 0)
204 	    key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
205 
206 	/* Fill in the modifiers. */
207 	if (key & KEYC_CTRL)
208 		strlcat(out, "C-", sizeof out);
209 	if (key & KEYC_ESCAPE)
210 		strlcat(out, "M-", sizeof out);
211 	if (key & KEYC_SHIFT)
212 		strlcat(out, "S-", sizeof out);
213 	key &= KEYC_MASK_KEY;
214 
215 	/* Try the key against the string table. */
216 	for (i = 0; i < nitems(key_string_table); i++) {
217 		if (key == key_string_table[i].key)
218 			break;
219 	}
220 	if (i != nitems(key_string_table)) {
221 		strlcat(out, key_string_table[i].string, sizeof out);
222 		return (out);
223 	}
224 
225 	/* Invalid keys are errors. */
226 	if (key == 127 || key > 255)
227 		return (NULL);
228 
229 	/* Check for standard or control key. */
230 	if (key >= 0 && key <= 32) {
231 		if (key == 0 || key > 26)
232 			xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
233 		else
234 			xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
235 	} else if (key >= 32 && key <= 126) {
236 		tmp[0] = key;
237 		tmp[1] = '\0';
238 	} else if (key >= 128)
239 		xsnprintf(tmp, sizeof tmp, "\\%o", key);
240 
241 	strlcat(out, tmp, sizeof out);
242 	return (out);
243 }
244