-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkbd.c
602 lines (486 loc) · 11.8 KB
/
kbd.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Terminal independent keyboard handling.
*/
#include "def.h"
#include "kbd.h"
#include "key.h"
#include "macro.h"
key_struct key; // The actual definition
INT negative_argument(INT f, INT n);
INT digit_argument(INT f, INT n);
INT insert(INT f, INT n);
INT extend(INT f, INT n);
static int
checkkeycount()
{
if (key.k_count >= MAXKEY - 1) {
ewprintf("Max Key count reached");
return 1;
}
return 0;
}
#ifdef BSMAP
INT bs_map = BSMAP;
/*
* Toggle backspace mapping
*/
INT
bsmap(INT f, INT n)
{
if(f & FFARG) bs_map = n > 0;
else bs_map = ! bs_map;
ewprintf("Backspace mapping %sabled", bs_map ? "en" : "dis");
return TRUE;
}
#endif
#ifndef NO_DPROMPT
#define PROMPTL ((MAXSTRKEY+1)*MAXKEY+1)
char prompt[PROMPTL], *promptp;
#endif
static INT pushed = FALSE;
static INT pushedc;
void
ungetkey(INT c)
{
pushedc = c;
pushed = TRUE;
}
/*
* Mg3a: Translate an 8-bit character to UCS-4.
* Leave alone if charset is NULL.
*/
INT
translate(charset_t charset, INT c)
{
if (c < 0 || c >= 256) {
return 0xfffd;
} else if (c < 128 || !charset) {
return c;
} else {
return charset[c - 128];
}
}
#if TESTCMD == 3
int revhit=0, revtotal=0, revmiss=0;
#endif
/*
* Mg3a: Reverse-translate from UCS-4 to an 8-bit charset. Use '?' as
* substitution character if no match. Leave alone if charset is NULL.
*/
INT
reverse_translate(charset_t charset, INT c)
{
INT i, ri;
static uint_least8_t reverse_cache[256];
if (c < 0 || c > 0x10ffff || c == 0xfffd) {
return '?';
} else if (c < 128) {
return c;
} else if (charset) {
ri = reverse_cache[c & 255] & 127;
#if TESTCMD == 3
revtotal++;
if (charset[ri] == c) revhit++;
#endif
if (charset[ri] == c) return ri + 128;
for (i = 0; i < 128; i++) {
if (c == charset[i]) {
reverse_cache[c & 255] = i;
return i + 128;
}
}
#if TESTCMD == 3
revmiss++;
#endif
} else if (c < 256) {
return c;
}
return '?';
}
/*
* Mg3a: Read an UTF-8 character from the keyboard.
* Return '?' on any error.
*/
static INT
getutf8()
{
INT c1, c2, c3, c4;
c1 = getkbd();
if ((c1 & 0x80) == 0) {
return c1;
} else if ((c1 & 0xe0) == 0xc0) {
c2 = getkbd();
if ((c2 & 0xc0) != 0x80) return '?';
return ((c1 & 0x1f) << 6) | (c2 & 0x3f);
} else if ((c1 & 0xf0) == 0xe0) {
c2 = getkbd();
if ((c2 & 0xc0) != 0x80) return '?';
c3 = getkbd();
if ((c3 & 0xc0) != 0x80) return '?';
return ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f);
} else if ((c1 & 0xf8) == 0xf0) {
c2 = getkbd();
if ((c2 & 0xc0) != 0x80) return '?';
c3 = getkbd();
if ((c3 & 0xc0) != 0x80) return '?';
c4 = getkbd();
if ((c4 & 0xc0) != 0x80) return '?';
return ((c1 & 0x07) << 18) | ((c2 & 0x3f) << 12) |
((c3 & 0x3f) << 6) | (c4 & 0x3f);
} else {
return '?';
}
}
/*
* Mg3a: getkey has been modified to return UCS-4
*/
INT
getkey(INT flag)
{
INT c;
if (pushed) {
pushed = FALSE;
return pushedc;
}
#ifndef NO_DPROMPT
if (flag) {
if(prompt[0]!='\0' && !waitforinput(2000)) {
ewprintf("%s", prompt); /* avoid problems with % */
update(); /* put the cursor back */
epresf = KPROMPT;
}
if(promptp > prompt) *(promptp-1) = ' ';
}
#endif
if (termisutf8) {
c = getutf8();
} else {
c = translate(termcharset, getkbd());
}
#ifdef BSMAP
if(bs_map) {
if(c==CCHR('H')) c=CCHR('?');
else if(c==CCHR('?')) c=CCHR('H');
}
#endif
#ifndef NO_DPROMPT
if (flag && promptp < &prompt[PROMPTL - MAXSTRKEY - 2]) {
promptp = keyname(promptp, c);
*promptp++ = '-';
*promptp = '\0';
}
#endif
return c;
}
/*
* doscan scans a keymap for a keyboard character and returns a
* pointer to the function associated with that character. Sets ele to
* the keymap element the keyboard was found in as a side effect.
*/
MAP_ELEMENT *ele;
PF
doscan(KEYMAP *map, INT c)
{
MAP_ELEMENT *elec = &map->map_element[0];
MAP_ELEMENT *last = &map->map_element[map->map_num];
while(elec < last && c > elec->k_num) elec++;
ele = elec; /* used by prefix and binding code */
if(elec >= last || c < elec->k_base)
return map->map_default;
return elec->k_funcp[c - elec->k_base];
}
INT
doin()
{
KEYMAP *curmap;
PF funct;
#ifndef NO_DPROMPT
*(promptp = prompt) = '\0';
#endif
curmap = mode_map(curbp->b_modes[curbp->b_nmodes]);
key.k_count = 0;
while((funct=doscan(curmap,(key.k_chars[key.k_count++]=getkey(TRUE))))
== prefix) {
if (checkkeycount()) return FALSE;
curmap = ele->k_prefmap;
}
#ifndef NO_MACRO
if(macrodef && macrocount < MAXMACRO) {
macro[macrocount++].m_funct = funct;
}
#endif
return execute(funct, 0, 1);
}
INT
rescan(INT f, INT n)
{
INT c;
KEYMAP *curmap;
INT i;
PF fp = NULL;
INT mode = curbp->b_nmodes;
for(;;) {
if(ucs_isupper(key.k_chars[key.k_count-1])) {
c = ucs_tolower(key.k_chars[key.k_count-1]);
curmap = mode_map(curbp->b_modes[mode]);
for(i=0; i < key.k_count-1; i++) {
if((fp=doscan(curmap,(key.k_chars[i]))) != prefix) break;
curmap = ele->k_prefmap;
}
if(fp==prefix) {
if((fp = doscan(curmap, c)) == prefix)
while((fp=doscan(curmap,key.k_chars[key.k_count++] =
getkey(TRUE))) == prefix) {
if (checkkeycount()) return FALSE;
curmap = ele->k_prefmap;
}
if(fp!=rescan) {
#ifndef NO_MACRO
if(macrodef && macrocount <= MAXMACRO)
macro[macrocount-1].m_funct = fp;
#endif
return execute(fp, f, n);
}
}
}
/* try previous mode */
if(--mode < 0) {
ewprintf("That key is not bound");
thisflag |= CFNOQUIT;
return ABORT;
}
curmap = mode_map(curbp->b_modes[mode]);
for(i=0; i < key.k_count; i++) {
if((fp=doscan(curmap,(key.k_chars[i]))) != prefix) break;
curmap = ele->k_prefmap;
}
if(fp==prefix) {
while((fp=doscan(curmap,key.k_chars[i++]=getkey(TRUE)))
== prefix) {
if (checkkeycount()) return FALSE;
curmap = ele->k_prefmap;
}
key.k_count = i;
}
if(fp!=rescan && i>=key.k_count-1) {
#ifndef NO_MACRO
if(macrodef && macrocount <= MAXMACRO)
macro[macrocount-1].m_funct = fp;
#endif
return execute(fp, f, n);
}
}
}
static INT
overflow()
{
ewprintf("Integer overflow in argument");
return FALSE;
}
static INT
repeatedrepeat()
{
ewprintf("Repeated repeat count");
return FALSE;
}
INT
universal_argument(INT f, INT n)
{
INT c, nn;
KEYMAP *curmap;
PF funct;
if (f & FFUNIV2) return repeatedrepeat(); // To guard against infinite recursion
if (f & FFNUM) { // ^U after a repeat count
f |= FFUNIV2;
nn = n;
} else if (f & FFUNIV) { // Repeated ^U
if (n >= MAXINT / 4 || n <= MININT / 4) return overflow();
nn = n * 4;
} else { // Default to 4
nn = 4;
}
key.k_chars[0] = c = getkey(TRUE);
key.k_count = 1;
if (!(f & FFUNIV2)) {
if (c == '-') return negative_argument(f, 1);
if (c >= '0' && c <= '9') return digit_argument(f, 1);
}
curmap = mode_map(curbp->b_modes[curbp->b_nmodes]);
while ((funct = doscan(curmap, c)) == prefix) {
curmap = ele->k_prefmap;
key.k_chars[key.k_count++] = c = getkey(TRUE);
if (checkkeycount()) return FALSE;
}
#ifndef NO_MACRO
if (macrodef && macrocount < MAXMACRO-2) {
if (f & FFARG) macrocount -= 3;
macro[macrocount++].m_flag = f ? f : FFUNIV; /* Mg3a: preserve flag */
macro[macrocount++].m_count = nn;
macro[macrocount++].m_funct = funct;
}
#endif
return execute(funct, f ? f : FFUNIV, nn); /* Mg3a: Pass along same flag, or FFUNIV */
}
INT
digit_argument(INT f, INT n)
{
INT nn, c;
KEYMAP *curmap;
PF funct;
if (f & (FFUNIV2|FFNUM)) return repeatedrepeat();
nn = key.k_chars[key.k_count-1] - '0';
if (nn < 0 || nn > 9) nn = 0;
for (;;) {
c = getkey(TRUE);
if(c < '0' || c > '9') break;
if (nn > (MAXINT - 9) / 10) return overflow();
nn *= 10;
nn += c - '0';
}
if (MININT + nn >= 0) return overflow(); // Truly paranoid
key.k_chars[0] = c;
key.k_count = 1;
curmap = mode_map(curbp->b_modes[curbp->b_nmodes]);
while ((funct = doscan(curmap, c)) == prefix) {
curmap = ele->k_prefmap;
key.k_chars[key.k_count++] = c = getkey(TRUE);
if (checkkeycount()) return FALSE;
}
#ifndef NO_MACRO
if (macrodef && macrocount < MAXMACRO-2) {
if (f & FFARG) macrocount -= 3;
else macro[macrocount-1].m_funct = universal_argument;
macro[macrocount++].m_flag = FFNUM; /* preserve flag */
macro[macrocount++].m_count = nn;
macro[macrocount++].m_funct = funct;
}
#endif
return execute(funct, FFNUM, nn);
}
INT
negative_argument(INT f, INT n)
{
INT nn = 0, c;
KEYMAP *curmap;
PF funct;
if (f & (FFUNIV2|FFNUM)) return repeatedrepeat();
for (;;) {
c = getkey(TRUE);
if(c < '0' || c > '9') break;
if (nn > (MAXINT - 9) / 10) return overflow();
nn *= 10;
nn += c - '0';
}
if (nn == 0) nn = 1;
else if (MININT + nn >= 0) return overflow();
nn = -nn;
key.k_chars[0] = c;
key.k_count = 1;
curmap = mode_map(curbp->b_modes[curbp->b_nmodes]);
while ((funct = doscan(curmap, c)) == prefix) {
curmap = ele->k_prefmap;
key.k_chars[key.k_count++] = c = getkey(TRUE);
if (checkkeycount()) return FALSE;
}
#ifndef NO_MACRO
if (macrodef && macrocount < MAXMACRO-2) {
if (f & FFARG) macrocount -= 3;
else macro[macrocount-1].m_funct = universal_argument;
macro[macrocount++].m_flag = FFNUM; /* preserve flag */
macro[macrocount++].m_count = nn;
macro[macrocount++].m_funct = funct;
}
#endif
return execute(funct, FFNUM, nn);
}
/*
* Insert a character. While defining a macro, create a "LINE"
* containing all inserted characters.
*/
INT
selfinsert(INT f, INT n)
{
INT c;
#ifndef NO_MACRO
LINE *lp;
INT len, linelen;
unsigned char buf[4];
#endif
inhibit_undo_boundary(f, selfinsert);
if (curbp->b_flag & BFREADONLY) return readonly();
if (n < 0) return FALSE;
if (n == 0) return TRUE;
c = key.k_chars[key.k_count-1];
#ifndef NO_MACRO
// Much simplified macro-add code. Append to fixed-size chunks,
// create new chunk if anything unusual.
if (macrodef && macrocount < MAXMACRO) {
ucs_chartostr(termcharset, c, buf, &len);
if ((f & FFARG) || !(lastflag & CFINS) || llength(maclcur) > lsize(maclcur) - len) {
if ((lp = lalloc(n == 1 ? 80 : len)) == NULL) return FALSE;
macro[macrocount-1].m_funct = insert;
insertafter(maclcur, lp);
maclcur = lp;
memcpy(ltext(maclcur), buf, len);
lsetlength(maclcur, len);
maclcur->l_flag = macrocount;
if (n == 1) thisflag |= CFINS;
// ewprintf("new n=%d, len=%d", n, len);
} else {
macrocount--;
linelen = llength(maclcur);
memcpy(<ext(maclcur)[linelen], buf, len);
lsetlength(maclcur, linelen + len);
thisflag |= CFINS;
// ewprintf("append n=%d, len=%d", n, linelen + len);
}
}
#endif
return linsert_overwrite(curbp->charset, n, c);
}
INT quoted_char_radix = 8; /* Radix of "quoted-insert" */
/*
* Mg3a: Rewritten a lot. Reads more digits like Emacs. Does not rely
* on selfinsert(). Always inserts.
*/
INT
quote(INT f, INT n)
{
INT c, num, base, digit;
INT s;
base = quoted_char_radix;
if (base < 2 || base > 10 + 'z' - 'a' + 1) {
ewprintf("Invalid base %d", base);
return FALSE;
}
if (curbp->b_flag & BFREADONLY) return readonly();
if (n < 0) return FALSE;
if (inmacro) {
if ((s = macro_get_INT(&c)) != TRUE) return s;
return linsert_ucs(curbp->charset, n, c);
}
c = getkey(TRUE);
digit = basedigit(c, base);
if (digit >= 0 && !(curbp->b_flag & BFOVERWRITE)) {
num = 0;
while (1) {
if (num > MAXINT / base || num * base > 0x10ffff - digit) {
return invalid_codepoint();
}
num = num * base + digit;
c = getkey(TRUE);
digit = basedigit(c, base);
if (c == '\r') {
break;
} else if (digit < 0) {
ungetkey(c);
break;
}
}
c = num;
}
if (macrodef) {
if ((s = macro_store_INT(c)) != TRUE) return s;
}
return linsert_ucs(curbp->charset, n, c);
}