-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkey.c
688 lines (607 loc) · 17.3 KB
/
key.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* key.c
*
* Anthony's Editor January 93
*
* Public Domain 1991, 1993 by Anthony Howe. No warranty.
*/
#include <ctype.h>
#include <string.h>
#include "header.h"
#include "key.h"
/* Variable length structure. */
typedef struct input_t {
struct input_t *next;
char *ptr;
char buf[1];
} input_t;
static input_t *istack = NULL;
static char blank[] = " \t\r\n";
static int k_default _((FILE *, char *, keymap_t *));
static int k_define _((FILE *, char *, keymap_t *));
static int k_erase _((FILE *, char *, keymap_t *));
static int k_help _((FILE *, char *, keymap_t *));
static int k_itself _((FILE *, char *, keymap_t *));
static int k_kill _((FILE *, char *, keymap_t *));
static int k_token _((FILE *, char *, keymap_t *));
static keymap_t *growkey _((keymap_t *, size_t));
static int ipush _((char *));
static int ipop _((void));
static void iflush _((void));
keyinit_t keywords[] = {
{ K_INSERT_ENTER, ".insert_enter", k_default },
{ K_INSERT_EXIT, ".insert_exit", k_default },
{ K_DELETE_LEFT, ".delete_left", k_default },
{ K_DELETE_RIGHT, ".delete_right", k_default },
{ K_BLOCK, ".block", k_default },
{ K_CUT, ".cut", k_default },
{ K_PASTE, ".paste", k_default },
{ K_UNDO, ".undo", k_default },
{ K_CURSOR_UP, ".cursor_up", k_default },
{ K_CURSOR_DOWN, ".cursor_down", k_default },
{ K_CURSOR_LEFT, ".cursor_left", k_default },
{ K_CURSOR_RIGHT, ".cursor_right", k_default },
{ K_PAGE_UP, ".page_up", k_default },
{ K_PAGE_DOWN, ".page_down", k_default },
{ K_WORD_LEFT, ".word_left", k_default },
{ K_WORD_RIGHT, ".word_right", k_default },
{ K_LINE_LEFT, ".line_left", k_default },
{ K_LINE_RIGHT, ".line_right", k_default },
{ K_FILE_TOP, ".file_top", k_default },
{ K_FILE_BOTTOM, ".file_bottom", k_default },
{ K_HELP, ".help", k_default },
{ K_HELP_OFF, ".help_off", k_token },
{ K_HELP_TEXT, ".help_text", k_help },
{ K_MACRO, ".macro", k_default },
{ K_MACRO_DEFINE, ".macro_define", k_define },
{ K_QUIT, ".quit", k_default },
{ K_QUIT_ASK, ".quit_ask", k_default },
{ K_FILE_READ, ".file_read", k_default },
{ K_FILE_WRITE, ".file_write", k_default },
{ K_STTY_ERASE, ".stty_erase", k_erase },
{ K_STTY_KILL, ".stty_kill", k_kill },
{ K_ITSELF, ".itself", k_itself },
{ K_REDRAW, ".redraw", k_default },
{ K_SHOW_VERSION, ".show_version", k_default },
{ K_LITERAL, ".literal", k_default },
{ K_ERROR, NULL, NULL }
};
/*
* Encode the given string into the supplied buffer that will be
* large enough to contain the string and the terminating '\0'.
* Return length of string string encoded in buffer; or 0 for
* an error.
*/
size_t
encodekey(str, buf)
char *str, *buf;
{
long number;
char *ptr, *ctrl;
static char escmap[] = "bfnrst";
static char escvalue[] = "\b\f\n\r \t";
static char control[] = "@abcdefghijklmnopqrstuvwxyz[\\]^_";
for (ptr = buf; *str != '\0'; ++ptr) {
switch (*str) {
case '^':
++str;
if (*str == '?') {
/* ^? equals ASCII DEL character. */
*ptr = 0x7f;
} else {
/* Non-ASCII dependant control key mapping. */
if (isupper(*str))
*str = tolower(*str);
if ((ctrl = strchr(control, *str)) == NULL)
return (0);
*ptr = (char) (ctrl - control);
}
++str;
break;
case '\\':
/* Escapes. */
++str;
if (isdigit(*str)) {
/* Numeric escapes allow for
* octal \0nnn
* hex \0xnn
* decimal \nnn
*/
number = strtol(str, &str, 0);
if (255 < number)
return (0);
*ptr = (char) number;
break;
}
if ((ctrl = strchr(escmap, *str)) != NULL) {
*ptr = escvalue[ctrl - escmap];
++str;
break;
}
/* Literal escapes. */
default:
/* Character. */
*ptr = *str++;
}
}
*ptr = '\0';
return ((size_t) (ptr - buf));
}
/*
* Read a configuration file from either the current directory or
* the user's home directory. Return an error status. Pass-back
* either a pointer to a key mapping table, or NULL if an error
* occured.
*/
int
initkey(fn, keys)
char *fn;
keymap_t **keys;
{
FILE *fp;
int error;
keyinit_t *kp;
keymap_t *array;
char *buf, *token;
size_t len, count;
*keys = NULL;
if ((fp = openrc(fn)) == NULL)
return (INITKEY_OPEN);
/* Allocate an array big enough to hold at least one of everything. */
if ((array = growkey(NULL, len = K_MAX_CODES)) == NULL) {
error = INITKEY_ALLOC;
goto error1;
}
count = 0;
while ((error = getblock(fp, "\n", &buf)) != GETBLOCK_EOF) {
if (error == GETBLOCK_ALLOC) {
error = INITKEY_ALLOC;
goto error1;
}
if (buf[0] != '.'
|| (token = strtok(buf, blank)) == NULL
|| (kp = findikey(keywords, strlwr(token))) == NULL) {
free(buf);
continue;
}
array[count].code = kp->code;
if (kp->fn != NULL) {
if (!(*kp->fn)(fp, buf, &array[count])) {
error = INITKEY_ERROR;
goto error1;
}
}
++count;
if (len <= count) {
keymap_t *new;
len += K_MAX_CODES;
if ((new = growkey(array, len)) == NULL) {
error = INITKEY_ALLOC;
goto error1;
}
array = new;
}
}
error = INITKEY_OK;
*keys = array;
error1:
(void) fclose(fp);
array[count].code = K_ERROR;
if (error != INITKEY_OK)
finikey(array);
return (error);
}
void
finikey(keys)
keymap_t *keys;
{
keymap_t *kp;
if (keys != NULL) {
for (kp = keys; kp->code != K_ERROR; ++kp) {
if (kp->lhs != NULL)
free(kp->lhs);
}
free(keys);
}
}
/*
* .help_text
* line(s) of text
* .end
*
* Fetch a block of text.
*/
static int
k_help(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
free(buf);
return (getblock(fp, ".end\n", &kp->lhs) == GETBLOCK_OK);
}
/*
* .function string
*
* Encode a key sequence for the given function.
*/
static int
k_default(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
char *tok;
size_t lhs;
if ((tok = strtok(NULL, blank)) == NULL
|| (lhs = encodekey(tok, buf)) == 0
|| (kp->lhs = (char *) realloc(buf, lhs)) == NULL) {
free(buf);
return (FALSE);
}
return (TRUE);
}
/*
* .macro_define
* .macro_define lhs rhs
*
* The first case is used as a place holder to reserve macro
* space. The second case actual defines a macro.
*/
static int
k_define(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
char *tok;
size_t lhs, rhs;
if ((tok = strtok(NULL, blank)) == NULL) {
return (k_token(fp, buf, kp));
} else if ((lhs = encodekey(tok, buf)) != 0
&& (tok = strtok(NULL, blank)) != NULL
&& (rhs = encodekey(tok, buf+lhs+1)) != 0
&& (kp->lhs = realloc(buf, lhs+rhs+2)) != NULL) {
kp->rhs = kp->lhs + lhs + 1;
return (TRUE);
}
free(buf);
return (FALSE);
}
/*
* .token
*/
static int
k_token(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
free(buf);
kp->lhs = kp->rhs = NULL;
return (TRUE);
}
/*
* .itself character
*/
static int
k_itself(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
buf[1] = '\0';
kp->code = *(unsigned char *) buf;
return ((kp->lhs = (char *) realloc(buf, 2)) != NULL);
}
/*
* .stty_erase
*/
static int
k_erase(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
buf[0] = erasechar();
buf[1] = '\0';
return ((kp->lhs = (char *) realloc(buf, 2)) != NULL);
}
/*
* .stty_kill
*/
static int
k_kill(fp, buf, kp)
FILE *fp;
char *buf;
keymap_t *kp;
{
buf[0] = killchar();
buf[1] = '\0';
return ((kp->lhs = (char *) realloc(buf, 2)) != NULL);
}
/*
* Find token and return corresponding table entry; else NULL.
*/
keymap_t *
findkey(kp, token)
keymap_t *kp;
char *token;
{
for (; kp->code != K_ERROR; ++kp)
if (kp->lhs != NULL && strcmp(token, kp->lhs) == 0)
return (kp);
return (NULL);
}
keyinit_t *
findikey(kp, token)
keyinit_t *kp;
char *token;
{
for (; kp->code != K_ERROR; ++kp)
if (kp->lhs != NULL && strcmp(token, kp->lhs) == 0)
return (kp);
return (NULL);
}
/*
*
*/
static keymap_t *
growkey(array, len)
keymap_t *array;
size_t len;
{
keymap_t *new;
if (len == 0)
return (NULL);
len *= sizeof (keymap_t);
if (array == NULL)
return ((keymap_t *) malloc(len));
return ((keymap_t *) realloc(array, len));
}
int
getkey(keys)
keymap_t *keys;
{
keymap_t *k;
int submatch;
static char buffer[K_BUFFER_LENGTH];
static char *record = buffer;
/* If recorded bytes remain, return next recorded byte. */
if (*record != '\0')
return (*(unsigned char *)record++);
/* Reset record buffer. */
record = buffer;
do {
if (K_BUFFER_LENGTH < record - buffer) {
record = buffer;
buffer[0] = '\0';
return (K_ERROR);
}
/* Read and record one byte. */
*record++ = getliteral();
*record = '\0';
/* If recorded bytes match any multi-byte sequence... */
for (k = keys, submatch = 0; k->code != K_ERROR; ++k) {
char *p, *q;
if (k->lhs == NULL
|| k->code == K_DISABLED || k->code == K_HELP_TEXT)
continue;
for (p = buffer, q = k->lhs; *p == *q; ++p, ++q) {
if (*q == '\0') {
if (k->code == K_LITERAL)
return (getliteral());
if (k->code != K_MACRO_DEFINE) {
/* Return extended key code. */
return (k->code);
}
if (k->rhs != NULL)
(void) ipush(k->rhs);
break;
}
}
if (*p == '\0' && *q != '\0') {
/* Recorded bytes match anchored substring. */
submatch = 1;
}
}
/* If recorded bytes matched an anchored substring, loop. */
} while (submatch);
/* Return first recorded byte. */
record = buffer;
return (*(unsigned char *)record++);
}
int
getliteral()
{
int ch;
ch = ipop();
if (ch == EOF)
return ((unsigned) getch());
return (ch);
}
/*
* Return true if a new input string was pushed onto the stack,
* else false if there was no more memory for the stack.
*/
static int
ipush(buf)
char *buf;
{
input_t *new;
new = (input_t *) malloc(sizeof (input_t) + strlen (buf));
if (new == NULL)
return (FALSE);
(void) strcpy(new->buf, buf);
new->ptr = new->buf;
new->next = istack;
istack = new;
return (TRUE);
}
/*
* Pop and return a character off the input stack. Return EOF if
* the stack is empty. If the end of an input string is reached,
* then free the node. This will allow clean tail recursion that
* won't blow the stack.
*/
static int
ipop()
{
int ch;
input_t *node;
if (istack == NULL)
return (EOF);
ch = (unsigned) *istack->ptr++;
if (*istack->ptr == '\0') {
node = istack;
istack = istack->next;
free(node);
}
return (ch);
}
/*
* Flush the entire input stack.
*/
static void
iflush()
{
input_t *node;
while (input != NULL) {
node = istack;
istack = istack->next;
free(node);
}
}
int
ismacro()
{
return (istack != NULL);
}
/*
* Field input.
*/
typedef struct key_entry_t {
int code;
int (*func) _((void));
} key_entry_t;
static int fld_done _((void));
static int fld_erase _((void));
static int fld_kill _((void));
static int fld_left _((void));
static int fld_insert _((void));
#define ERASE_KEY 0
#define KILL_KEY 1
static key_entry_t ktable[] = {
{ K_STTY_ERASE, fld_erase },
{ K_STTY_KILL, fld_kill },
{ '\r', fld_done },
{ '\n', fld_done },
{ '\b', fld_erase },
{ -1, fld_insert }
};
static int fld_row;
static int fld_col;
static int fld_key;
static int fld_echo;
static int fld_index;
static int fld_length;
static char *fld_buffer;
#ifndef getmaxyx
#define getmaxyx(w,r,c) (r=LINES,c=COLS)
#endif
int
getinput(buf, len, echoing)
char *buf;
int len, echoing;
{
int first;
key_entry_t *k;
fld_buffer = buf;
fld_index = (int) strlen(fld_buffer);
fld_length = len < 0 ? COLS : len;
if (--fld_length < 1)
return (FALSE);
ktable[ERASE_KEY].code = erasechar();
ktable[KILL_KEY].code = killchar();
fld_echo = echoing;
getyx(stdscr, fld_row, fld_col);
addstr(fld_buffer);
move(fld_row, fld_col);
for (first = TRUE;; first = FALSE) {
refresh();
fld_key = getliteral();
for (k = ktable; k->code != -1 && k->code != fld_key; ++k)
;
if (first && k->func == fld_insert)
fld_kill();
if (k->func != NULL && !(*k->func)()) {
fld_buffer[fld_index] = '\0';
break;
}
}
return (TRUE);
}
static int
fld_done()
{
return (FALSE);
}
static int
fld_left()
{
int row, col, max_row, max_col;
getyx(stdscr, row, col);
getmaxyx(stdscr, max_row, max_col);
if (0 < fld_index) {
--fld_index;
/* Assume that if 0 < fld_index then fld_row <= row
* and fld_col < col. So when fld_index == 0, then
* fld_row == row and fld_col == col.
*/
if (0 < col) {
--col;
} else if (0 < row) {
/* Handle reverse line wrap. */
--row;
col = max_col-1;
}
move(row, col);
}
return (TRUE);
}
static int
fld_erase()
{
int row, col;
if (0 < fld_index) {
fld_left();
getyx(stdscr, row, col);
addch(' ');
move(row, col);
fld_buffer[fld_index] = '\0';
}
return (TRUE);
}
static int
fld_kill()
{
move(fld_row, fld_col);
while (0 < fld_index--)
addch(' ');
move(fld_row, fld_col);
fld_buffer[0] = '\0';
fld_index = 0;
return (TRUE);
}
static int
fld_insert()
{
if (fld_index < fld_length) {
if (!ISFUNCKEY(fld_key)) {
fld_buffer[fld_index++] = fld_key;
if (fld_echo)
addch(fld_key);
}
}
return (fld_index < fld_length);
}