|
|
1.1 root 1: #include <stdio.h>
2:
3: #include <ioctl.h>
4:
5: #include <string.h>
6:
7:
8:
9: #define MAXKEY 32
10:
11: #ifndef TIOCGXKEY
12:
13: #define TIOCGXKEY (('T'<< 8) | 13)
14:
15: #define TIOCSXKEY (('T'<< 8) | 14)
16:
17: #endif
18:
19:
20:
21: struct xkey {
22:
23: short xk_num;
24:
25: char xk_def[8];
26:
27: } xk;
28:
29:
30:
31: char *kname[MAXKEY] = {
32:
33: "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10",
34:
35: "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
36:
37: "up", "down", "right", "left", "help", "undo", "insert", "home",
38:
39: "UP", "DOWN", "RIGHT", "LEFT"
40:
41: };
42:
43:
44:
45: char buf[80];
46:
47:
48:
49: char *unescape(char *string)
50:
51: {
52:
53: char *u = buf;
54:
55: char c;
56:
57:
58:
59: while (c = *string++) {
60:
61: if (c == '\\') {
62:
63: c = *string++;
64:
65: if (!c) break;
66:
67: }
68:
69: else if (c == '^') {
70:
71: c = *string++;
72:
73: if (!c) break;
74:
75: else if (c == '?') c = 127;
76:
77: else c &= 0x1f;
78:
79: }
80:
81: *u++ = c;
82:
83: }
84:
85: *u++ = 0;
86:
87: return buf;
88:
89: }
90:
91:
92:
93: char *escape(char *string)
94:
95: {
96:
97: char *e, c;
98:
99:
100:
101: e = buf;
102:
103: while ( c = *string++ ) {
104:
105: if (c < ' ' && c > 0) {
106:
107: *e++ = '^';
108:
109: c += '@';
110:
111: }
112:
113: else if (c == 127) {
114:
115: *e++ = '^';
116:
117: c = '?';
118:
119: }
120:
121: else if (c == '\\' || c == '^')
122:
123: *e++ = '\\';
124:
125: *e++ = c;
126:
127: }
128:
129: *e++ = 0;
130:
131: return buf;
132:
133: }
134:
135:
136:
137: void
138:
139: displaykeys()
140:
141: {
142:
143: int i, r;
144:
145: int col = 0, maxcol;
146:
147: char *e;
148:
149:
150:
151: maxcol = 80; /* really should do ioctl to get this */
152:
153: for (i = 0; i < MAXKEY; i++) {
154:
155: xk.xk_num = i;
156:
157: r = ioctl(0, TIOCGXKEY, &xk);
158:
159: if (r) {
160:
161: perror("ioctl");
162:
163: exit(1);
164:
165: }
166:
167: e = escape(xk.xk_def);
168:
169: col += (r = strlen(kname[i]) + strlen(e) + 2);
170:
171: if (col >= maxcol) {
172:
173: printf("\n");
174:
175: col = r;
176:
177: }
178:
179: printf("%s %s ", kname[i], e);
180:
181: }
182:
183: printf("\n");
184:
185: }
186:
187:
188:
189: void
190:
191: definekey(char *key, char *def)
192:
193: {
194:
195: int i;
196:
197:
198:
199: for (i = 0; i < MAXKEY; i++)
200:
201: if (!strcmp(kname[i], key)) break;
202:
203: if (i >= MAXKEY) {
204:
205: fprintf(stderr, "Unknown key: %s\n", key);
206:
207: return;
208:
209: }
210:
211: strncpy(xk.xk_def, unescape(def), 7);
212:
213: xk.xk_num = i;
214:
215: xk.xk_def[7] = 0;
216:
217: i = ioctl(0, TIOCSXKEY, &xk);
218:
219: if (i) {
220:
221: perror("ioctl");
222:
223: }
224:
225: }
226:
227:
228:
229: int
230:
231: main(int argc, char **argv)
232:
233: {
234:
235: char *progname;
236:
237: char *key, *def;
238:
239:
240:
241: progname = *argv++;
242:
243:
244:
245: if (!isatty(0)) {
246:
247: fprintf(stderr, "%s must be run on a terminal\n", progname);
248:
249: return 1;
250:
251: }
252:
253: if (!*argv) {
254:
255: displaykeys();
256:
257: return 0;
258:
259: }
260:
261: while (*argv) {
262:
263: key = *argv++;
264:
265: def = *argv++;
266:
267: if (!def) {
268:
269: fprintf(stderr, "Usage: %s [key def]...\n", progname);
270:
271: return 2;
272:
273: }
274:
275: definekey(key, def);
276:
277: }
278:
279: return 0;
280:
281: }
282:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.