Didn't really think the else behaviour through - requiring argv to
[tmux-openbsd.git] / key-string.c
blob29b6a844e8ade47a9c2efd45c4e884d6cbbf1983
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
23 #include "tmux.h"
25 int key_string_search_table(const char *);
26 int key_string_get_modifiers(const char **);
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 { "F13", KEYC_F13 },
46 { "F14", KEYC_F14 },
47 { "F15", KEYC_F15 },
48 { "F16", KEYC_F16 },
49 { "F17", KEYC_F17 },
50 { "F18", KEYC_F18 },
51 { "F19", KEYC_F19 },
52 { "F20", KEYC_F20 },
53 { "IC", KEYC_IC },
54 { "DC", KEYC_DC },
55 { "Home", KEYC_HOME },
56 { "End", KEYC_END },
57 { "NPage", KEYC_NPAGE },
58 { "PageDown", KEYC_NPAGE },
59 { "PgDn", KEYC_NPAGE },
60 { "PPage", KEYC_PPAGE },
61 { "PageUp", KEYC_PPAGE },
62 { "PgUp", KEYC_PPAGE },
63 { "Tab", '\011' },
64 { "BTab", KEYC_BTAB },
65 { "Space", ' ' },
66 { "BSpace", KEYC_BSPACE },
67 { "Enter", '\r' },
68 { "Escape", '\033' },
70 /* Arrow keys. */
71 { "Up", KEYC_UP },
72 { "Down", KEYC_DOWN },
73 { "Left", KEYC_LEFT },
74 { "Right", KEYC_RIGHT },
76 /* Numeric keypad. */
77 { "KP/", KEYC_KP_SLASH },
78 { "KP*", KEYC_KP_STAR },
79 { "KP-", KEYC_KP_MINUS },
80 { "KP7", KEYC_KP_SEVEN },
81 { "KP8", KEYC_KP_EIGHT },
82 { "KP9", KEYC_KP_NINE },
83 { "KP+", KEYC_KP_PLUS },
84 { "KP4", KEYC_KP_FOUR },
85 { "KP5", KEYC_KP_FIVE },
86 { "KP6", KEYC_KP_SIX },
87 { "KP1", KEYC_KP_ONE },
88 { "KP2", KEYC_KP_TWO },
89 { "KP3", KEYC_KP_THREE },
90 { "KPEnter", KEYC_KP_ENTER },
91 { "KP0", KEYC_KP_ZERO },
92 { "KP.", KEYC_KP_PERIOD },
95 /* Find key string in table. */
96 int
97 key_string_search_table(const char *string)
99 u_int i;
101 for (i = 0; i < nitems(key_string_table); i++) {
102 if (strcasecmp(string, key_string_table[i].string) == 0)
103 return (key_string_table[i].key);
105 return (KEYC_NONE);
108 /* Find modifiers. */
110 key_string_get_modifiers(const char **string)
112 int modifiers;
114 modifiers = 0;
115 while (((*string)[0] != '\0') && (*string)[1] == '-') {
116 switch ((*string)[0]) {
117 case 'C':
118 case 'c':
119 modifiers |= KEYC_CTRL;
120 break;
121 case 'M':
122 case 'm':
123 modifiers |= KEYC_ESCAPE;
124 break;
125 case 'S':
126 case 's':
127 modifiers |= KEYC_SHIFT;
128 break;
130 *string += 2;
132 return (modifiers);
135 /* Lookup a string and convert to a key value. */
137 key_string_lookup_string(const char *string)
139 int key, modifiers;
141 /* Check for modifiers. */
142 modifiers = 0;
143 if (string[0] == '^' && string[1] != '\0') {
144 modifiers |= KEYC_CTRL;
145 string++;
147 modifiers |= key_string_get_modifiers(&string);
148 if (string[0] == '\0')
149 return (KEYC_NONE);
151 /* Is this a standard ASCII key? */
152 if (string[1] == '\0') {
153 key = (u_char) string[0];
154 if (key < 32 || key == 127 || key > 255)
155 return (KEYC_NONE);
156 } else {
157 /* Otherwise look the key up in the table. */
158 key = key_string_search_table(string);
159 if (key == KEYC_NONE)
160 return (KEYC_NONE);
163 /* Convert the standard control keys. */
164 if (key < KEYC_BASE && (modifiers & KEYC_CTRL)) {
165 if (key >= 97 && key <= 122)
166 key -= 96;
167 else if (key >= 64 && key <= 95)
168 key -= 64;
169 else if (key == 32)
170 key = 0;
171 else if (key == 63)
172 key = KEYC_BSPACE;
173 else
174 return (KEYC_NONE);
175 modifiers &= ~KEYC_CTRL;
178 return (key | modifiers);
181 /* Convert a key code into string format, with prefix if necessary. */
182 const char *
183 key_string_lookup_key(int key)
185 static char out[24];
186 char tmp[8];
187 u_int i;
189 *out = '\0';
192 * Special case: display C-@ as C-Space. Could do this below in
193 * the (key >= 0 && key <= 32), but this way we let it be found
194 * in key_string_table, for the unlikely chance that we might
195 * change its name.
197 if ((key & KEYC_MASK_KEY) == 0)
198 key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
200 /* Fill in the modifiers. */
201 if (key & KEYC_CTRL)
202 strlcat(out, "C-", sizeof out);
203 if (key & KEYC_ESCAPE)
204 strlcat(out, "M-", sizeof out);
205 if (key & KEYC_SHIFT)
206 strlcat(out, "S-", sizeof out);
207 key &= KEYC_MASK_KEY;
209 /* Try the key against the string table. */
210 for (i = 0; i < nitems(key_string_table); i++) {
211 if (key == key_string_table[i].key)
212 break;
214 if (i != nitems(key_string_table)) {
215 strlcat(out, key_string_table[i].string, sizeof out);
216 return (out);
219 /* Invalid keys are errors. */
220 if (key == 127 || key > 255)
221 return (NULL);
223 /* Check for standard or control key. */
224 if (key >= 0 && key <= 32) {
225 if (key == 0 || key > 26)
226 xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
227 else
228 xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
229 } else if (key >= 32 && key <= 126) {
230 tmp[0] = key;
231 tmp[1] = '\0';
232 } else if (key >= 128)
233 xsnprintf(tmp, sizeof tmp, "\\%o", key);
235 strlcat(out, tmp, sizeof out);
236 return (out);