-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.c
189 lines (166 loc) · 5.32 KB
/
keys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
$NiH: keys.c,v 1.10 2001/12/11 14:37:34 dillo Exp $
keys.c -- parse and print key specifications
Copyright (C) 1996-2002 Dieter Baron
This file is part of cftp, a fullscreen ftp client
The author can be contacted at <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "keys.h"
struct keyname key_name[] = {
{ 8, "bs", "backspace" },
{ 9, "tab", NULL },
{ 13, "ret", "return" },
{ 27, "esc", "escape" },
{ 32, "spc", "space" },
{ 127, "del", "delete" },
{ 256, NULL, NULL }
};
struct fnkey fnkey[] = {
{ "left", "cursor left", "kl", NULL },
{ "right", "cursor right", "kr", NULL },
{ "up", "cursor up", "ku", NULL },
{ "down", "cursor down", "kd", NULL },
{ "home", "home", "kh", NULL },
{ "num7", "number block 7", "K1", NULL },
{ "num9", "number block 9", "K2", NULL },
{ "num5", "number block 5", "K3", NULL },
{ "num1", "number block 1", "K4", NULL },
{ "num3", "number block 3", "K5", NULL },
{ "f10", "function key 10 (or 0)", "k0", NULL },
{ "f1", "function key 1", "k1", NULL },
{ "f2", "function key 2", "k2", NULL },
{ "f3", "function key 3", "k3", NULL },
{ "f4", "function key 4", "k4", NULL },
{ "f5", "function key 5", "k5", NULL },
{ "f6", "function key 6", "k6", NULL },
{ "f7", "function key 7", "k7", NULL },
{ "f8", "function key 8", "k8", NULL },
{ "f9", "function key 9", "k9", NULL },
{ "clratab", "clear all tabs", "ka", NULL },
{ "clrtab", "clear tab this column", "kt", NULL },
{ "clr", "clear screen", "kC", NULL },
{ "delline", "delete line", "kL", NULL },
{ "exins", "exit insert mode", "kM", NULL },
{ "clreol", "clear to end of line", "kE", NULL },
{ "clreos", "clear to end of screen", "kS", NULL },
{ "ins", "insert", "kI", NULL },
{ "insline", "insert line", "kA", NULL },
{ "pgdown", "next page", "kN", NULL },
{ "pgup", "previous page", "kP", NULL },
{ "scrup", "scroll forward", "kF", NULL },
{ "scrdown", "scroll reverse", "kR", NULL },
{ "settab", "set tab stop this column", "kT", NULL }
};
int max_fnkey = sizeof(fnkey)/sizeof(struct fnkey);
char *
print_key(int key, int longp)
{
int i;
static char buf[64];
if (key < 256) {
for (i=0; (key&127)>key_name[i].key; i++)
;
if ((key&127) == key_name[i].key) {
if (key > 128) {
sprintf(buf, "%s%s",
(longp ? "meta " : "M-"),
((longp && key_name[i].longname) ?
key_name[i].longname
: key_name[i].name));
return buf;
}
else
return ((longp && key_name[i].longname) ?
key_name[i].longname
: key_name[i].name);
}
if ((key&127) < 32) {
sprintf(buf, "%s%s%c",
(key > 128 ? (longp ?
"meta " : "M-") :
""),
(longp ? "control " : "C-"),
tolower((key&127)+96));
return buf;
}
else {
sprintf(buf, "%s%c",
(key > 128 ? (longp ?
"meta " : "M-") :
""),
key&127);
return buf;
}
}
else
return ((longp && fnkey[key-256].longname) ?
fnkey[key-256].longname : fnkey[key-256].name);
}
int
parse_key(char *kn)
{
int k, i;
if (strlen(kn) == 1)
return kn[0];
i = 0;
if (tolower(kn[0]) == 'c' && kn[1] == '-')
i = 2;
if (kn[0] == '^')
i = 1;
if (strncasecmp(kn, "control ", 8) == 0)
i = 8;
if (i) {
if ((k=parse_key(kn+i)) > 256)
return -1;
return (k & 0x9f);
}
i = 0;
if (tolower(kn[0]) == 'm' && kn[1] == '-')
i = 2;
if (strncasecmp(kn, "meta ", 5) == 0)
i = 5;
if (i) {
if ((k=parse_key(kn+i)) > 256)
return -1;
return (k | 0x80);
}
for (i=0; key_name[i].key < 255; i++) {
if (strcasecmp(kn, key_name[i].name) == 0
|| (key_name[i].longname
&& strcasecmp(kn, key_name[i].longname) == 0))
return key_name[i].key;
}
for (i=0; i<max_fnkey; i++) {
if (strcasecmp(kn, fnkey[i].name) == 0
|| (fnkey[i].longname
&& strcasecmp(kn, fnkey[i].longname) == 0))
return i+256;
}
return -1;
}