summaryrefslogtreecommitdiffstats
path: root/lib/libedit/key.c
blob: 302b6bd68d543cc40d985dbe81c2f65f5ed40cd9 (plain)
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Christos Zoulas of Cornell University.
 *
 * 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 */

#if !defined(lint) && !defined(SCCSID)
static char sccsid[] = "@(#)key.c	8.1 (Berkeley) 6/4/93";
#endif /* not lint && not SCCSID */

/*
 * key.c: This module contains the procedures for maintaining
 *	  the extended-key map.
 *
 *      An extended-key (key) is a sequence of keystrokes introduced
 *	with an sequence introducer and consisting of an arbitrary
 *	number of characters.  This module maintains a map (the el->el_key.map)
 *	to convert these extended-key sequences into input strs
 *	(XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
 *
 *      Warning:
 *	  If key is a substr of some other keys, then the longer
 *	  keys are lost!!  That is, if the keys "abcd" and "abcef"
 *	  are in el->el_key.map, adding the key "abc" will cause the first two
 *	  definitions to be lost.
 *
 *      Restrictions:
 *      -------------
 *      1) It is not possible to have one key that is a
 *	   substr of another.
 */
#include "sys.h"
#include <string.h>
#include <stdlib.h>

#include "el.h"

/*
 * The Nodes of the el->el_key.map.  The el->el_key.map is a linked list
 * of these node elements
 */
struct key_node_t {
    char        ch;		/* single character of key 		*/
    int         type;		/* node type				*/
    key_value_t val; 		/* command code or pointer to str, 	*/
				/* if this is a leaf 			*/
    struct key_node_t *next;	/* ptr to next char of this key 	*/
    struct key_node_t *sibling;	/* ptr to another key with same prefix 	*/
};

private	int            node_trav	__P((EditLine *, key_node_t *, char *,
					     key_value_t *));
private	int            node__try	__P((key_node_t *, char *,
					     key_value_t *, int));
private	key_node_t    *node__get	__P((int));
private	void	       node__put	__P((key_node_t *));
private	int	       node__delete	__P((key_node_t **, char *));
private	int	       node_lookup 	__P((EditLine *, char *, key_node_t *,
					     int));
private	int	       node_enum	__P((EditLine *, key_node_t *, int));
private	int	       key__decode_char	__P((char *, int, int));

#define KEY_BUFSIZ	EL_BUFSIZ


/* key_init():
 *	Initialize the key maps
 */
protected int
key_init(el)
    EditLine *el;
{
    el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
    el->el_key.map = NULL;
    key_reset(el);
    return 0;
}


/* key_end():
 *	Free the key maps
 */
protected void
key_end(el)
    EditLine *el;
{
    el_free((ptr_t) el->el_key.buf);
    el->el_key.buf = NULL;
    /* XXX: provide a function to clear the keys */
    el->el_key.map = NULL;
}


/* key_map_cmd():
 *	Associate cmd with a key value
 */
protected key_value_t *
key_map_cmd(el, cmd)
    EditLine *el;
    int cmd;
{
    el->el_key.val.cmd = (el_action_t) cmd;
    return &el->el_key.val;
}


/* key_map_str():
 *	Associate str with a key value
 */
protected key_value_t *
key_map_str(el, str)
    EditLine *el;
    char  *str;
{
    el->el_key.val.str = str;
    return &el->el_key.val;
}


/* key_reset():
 *	Takes all nodes on el->el_key.map and puts them on free list.  Then
 *	initializes el->el_key.map with arrow keys
 *	[Always bind the ansi arrow keys?]
 */
protected void
key_reset(el)
    EditLine *el;
{
    node__put(el->el_key.map);
    el->el_key.map = NULL;
    return;
}


/* key_get():
 *	Calls the recursive function with entry point el->el_key.map
 *      Looks up *ch in map and then reads characters until a
 *      complete match is found or a mismatch occurs. Returns the
 *      type of the match found (XK_STR, XK_CMD, or XK_EXE).
 *      Returns NULL in val.str and XK_STR for no match.
 *      The last character read is returned in *ch.
 */
protected int
key_get(el, ch, val)
    EditLine    *el;
    char        *ch;
    key_value_t *val;
{
    return node_trav(el, el->el_key.map, ch, val);
}



/* key_add():
 *      Adds key to the el->el_key.map and associates the value in val with it.
 *      If key is already is in el->el_key.map, the new code is applied to the
 *      existing key. Ntype specifies if code is a command, an
 *      out str or a unix command.
 */
protected void
key_add(el, key, val, ntype)
    EditLine    *el;
    char        *key;
    key_value_t *val;
    int          ntype;
{
    if (key[0] == '\0') {
	(void) fprintf(el->el_errfile,
		       "key_add: Null extended-key not allowed.\n");
	return;
    }

    if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
	(void) fprintf(el->el_errfile,
		       "key_add: sequence-lead-in command not allowed\n");
	return;
    }

    if (el->el_key.map == NULL)
	/* tree is initially empty.  Set up new node to match key[0] */
	el->el_key.map = node__get(key[0]);	/* it is properly initialized */

    /* Now recurse through el->el_key.map */
    (void) node__try(el->el_key.map, key, val, ntype);
    return;
}


/* key_clear():
 *
 */
protected void
key_clear(el, map, in)
    EditLine *el;
    el_action_t *map;
    char   *in;
{
    if ((map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN) &&
	((map == el->el_map.key &&
	  el->el_map.alt[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN) ||
	 (map == el->el_map.alt &&
	  el->el_map.key[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN)))
	(void) key_delete(el, in);
}


/* key_delete():
 *      Delete the key and all longer keys staring with key, if
 *      they exists.
 */
protected int
key_delete(el, key)
    EditLine *el;
    char   *key;
{
    if (key[0] == '\0') {
	(void) fprintf(el->el_errfile,
		       "key_delete: Null extended-key not allowed.\n");
	return -1;
    }

    if (el->el_key.map == NULL)
	return 0;

    (void) node__delete(&el->el_key.map, key);
    return 0;
}


/* key_print():
 *	Print the binding associated with key key.
 *	Print entire el->el_key.map if null
 */
protected void
key_print(el, key)
    EditLine *el;
    char   *key;
{
    /* do nothing if el->el_key.map is empty and null key specified */
    if (el->el_key.map == NULL && *key == 0)
	return;

    el->el_key.buf[0] =  '"';
    if (node_lookup(el, key, el->el_key.map, 1) <= -1)
	/* key is not bound */
	(void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n", key);
    return;
}


/* node_trav():
 *	recursively traverses node in tree until match or mismatch is
 * 	found.  May read in more characters.
 */
private int
node_trav(el, ptr, ch, val)
    EditLine     *el;
    key_node_t   *ptr;
    char         *ch;
    key_value_t  *val;
{
    if (ptr->ch == *ch) {
	/* match found */
	if (ptr->next) {
	    /* key not complete so get next char */
	    if (el_getc(el, ch) != 1) {	/* if EOF or error */
		val->cmd = ED_END_OF_FILE;
		return XK_CMD;/* PWP: Pretend we just read an end-of-file */
	    }
	    return node_trav(el, ptr->next, ch, val);
	}
	else {
	    *val = ptr->val;
	    if (ptr->type != XK_CMD)
		*ch = '\0';
	    return ptr->type;
	}
    }
    else {
	/* no match found here */
	if (ptr->sibling) {
	    /* try next sibling */
	    return node_trav(el, ptr->sibling, ch, val);
	}
	else {
	    /* no next sibling -- mismatch */
	    val->str = NULL;
	    return XK_STR;
	}
    }
}


/* node__try():
 * 	Find a node that matches *str or allocate a new one
 */
private int
node__try(ptr, str, val, ntype)
    key_node_t   *ptr;
    char         *str;
    key_value_t  *val;
    int           ntype;
{
    if (ptr->ch != *str) {
	key_node_t *xm;

	for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
	    if (xm->sibling->ch == *str)
		break;
	if (xm->sibling == NULL)
	    xm->sibling = node__get(*str);	/* setup new node */
	ptr = xm->sibling;
    }

    if (*++str == '\0') {
	/* we're there */
	if (ptr->next != NULL) {
	    node__put(ptr->next);	/* lose longer keys with this prefix */
	    ptr->next = NULL;
	}
	switch (ptr->type) {
	case XK_CMD:
	case XK_NOD:
	    break;
	case XK_STR:
	case XK_EXE:
	    if (ptr->val.str)
		el_free((ptr_t) ptr->val.str);
	    break;
	default:
	    abort();
	    break;
	}

	switch (ptr->type = ntype) {
	case XK_CMD:
	    ptr->val = *val;
	    break;
	case XK_STR:
	case XK_EXE:
	    ptr->val.str = strdup(val->str);
	    break;
	default:
	    abort();
	    break;
	}
    }
    else {
	/* still more chars to go */
	if (ptr->next == NULL)
	    ptr->next = node__get(*str);	/* setup new node */
	(void) node__try(ptr->next, str, val, ntype);
    }
    return 0;
}


/* node__delete():
 *	Delete node that matches str
 */
private int
node__delete(inptr, str)
    key_node_t **inptr;
    char   *str;
{
    key_node_t *ptr;
    key_node_t *prev_ptr = NULL;

    ptr = *inptr;

    if (ptr->ch != *str) {
	key_node_t *xm;

	for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
	    if (xm->sibling->ch == *str)
		break;
	if (xm->sibling == NULL)
	    return 0;
	prev_ptr = xm;
	ptr = xm->sibling;
    }

    if (*++str == '\0') {
	/* we're there */
	if (prev_ptr == NULL)
	    *inptr = ptr->sibling;
	else
	    prev_ptr->sibling = ptr->sibling;
	ptr->sibling = NULL;
	node__put(ptr);
	return 1;
    }
    else if (ptr->next != NULL && node__delete(&ptr->next, str) == 1) {
	if (ptr->next != NULL)
	    return 0;
	if (prev_ptr == NULL)
	    *inptr = ptr->sibling;
	else
	    prev_ptr->sibling = ptr->sibling;
	ptr->sibling = NULL;
	node__put(ptr);
	return 1;
    }
    else {
	return 0;
    }
}

/* node__put():
 *	Puts a tree of nodes onto free list using free(3).
 */
private void
node__put(ptr)
    key_node_t *ptr;
{
    if (ptr == NULL)
	return;

    if (ptr->next != NULL) {
	node__put(ptr->next);
	ptr->next = NULL;
    }

    node__put(ptr->sibling);

    switch (ptr->type) {
    case XK_CMD:
    case XK_NOD:
	break;
    case XK_EXE:
    case XK_STR:
	if (ptr->val.str != NULL)
	    el_free((ptr_t) ptr->val.str);
	break;
    default:
	abort();
	break;
    }
    el_free((ptr_t) ptr);
}


/* node__get():
 *	Returns pointer to an key_node_t for ch.
 */
private key_node_t *
node__get(ch)
    int    ch;
{
    key_node_t *ptr;

    ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
    ptr->ch = ch;
    ptr->type = XK_NOD;
    ptr->val.str = NULL;
    ptr->next = NULL;
    ptr->sibling = NULL;
    return ptr;
}



/* node_lookup():
 *	look for the str starting at node ptr.
 *	Print if last node
 */
private int
node_lookup(el, str, ptr, cnt)
    EditLine   *el;
    char       *str;
    key_node_t *ptr;
    int         cnt;
{
    int     ncnt;

    if (ptr == NULL)
	return -1;		/* cannot have null ptr */

    if (*str == 0) {
	/* no more chars in str.  node_enum from here. */
	(void) node_enum(el, ptr, cnt);
	return 0;
    }
    else {
	/* If match put this char into el->el_key.buf.  Recurse */
	if (ptr->ch == *str) {
	    /* match found */
	    ncnt = key__decode_char(el->el_key.buf, cnt,
				    (unsigned char) ptr->ch);
	    if (ptr->next != NULL)
		/* not yet at leaf */
		return node_lookup(el, str + 1, ptr->next, ncnt + 1);
	    else {
		/* next node is null so key should be complete */
		if (str[1] == 0) {
		    el->el_key.buf[ncnt + 1] = '"';
		    el->el_key.buf[ncnt + 2] = '\0';
		    key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
		    return 0;
		}
		else
		    return -1;/* mismatch -- str still has chars */
	    }
	}
	else {
	    /* no match found try sibling */
	    if (ptr->sibling)
		return node_lookup(el, str, ptr->sibling, cnt);
	    else
		return -1;
	}
    }
}


/* node_enum():
 *	Traverse the node printing the characters it is bound in buffer
 */
private int
node_enum(el, ptr, cnt)
    EditLine *el;
    key_node_t *ptr;
    int     cnt;
{
    int     ncnt;

    if (cnt >= KEY_BUFSIZ - 5) {	/* buffer too small */
	el->el_key.buf[++cnt] = '"';
	el->el_key.buf[++cnt] = '\0';
	(void) fprintf(el->el_errfile,
		    "Some extended keys too long for internal print buffer");
	(void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
	return 0;
    }

    if (ptr == NULL) {
#ifdef DEBUG_EDIT
	(void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");
#endif
	return -1;
    }

    /* put this char at end of str */
    ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
    if (ptr->next == NULL) {
	/* print this key and function */
	el->el_key.buf[ncnt + 1] = '"';
	el->el_key.buf[ncnt + 2] = '\0';
	key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
    }
    else
	(void) node_enum(el, ptr->next, ncnt + 1);

    /* go to sibling if there is one */
    if (ptr->sibling)
	(void) node_enum(el, ptr->sibling, cnt);
    return 0;
}


/* key_kprint():
 *	Print the specified key and its associated
 *	function specified by val
 */
protected void
key_kprint(el, key, val, ntype)
    EditLine      *el;
    char          *key;
    key_value_t   *val;
    int            ntype;
{
    el_bindings_t *fp;
    char unparsbuf[EL_BUFSIZ];
    static char *fmt = "%-15s->  %s\n";

    if (val != NULL)
	switch (ntype) {
	case XK_STR:
	case XK_EXE:
	    (void) fprintf(el->el_errfile, fmt, key,
			   key__decode_str(val->str, unparsbuf,
					      ntype == XK_STR ? "\"\"" : "[]"));
	    break;
	case XK_CMD:
	    for (fp = el->el_map.help; fp->name; fp++)
		if (val->cmd == fp->func) {
		    (void) fprintf(el->el_errfile, fmt, key, fp->name);
		    break;
		}
#ifdef DEBUG_KEY
	    if (fp->name == NULL)
		(void) fprintf(el->el_errfile, "BUG! Command not found.\n");
#endif

	    break;
	default:
	    abort();
	    break;
	}
    else
	(void) fprintf(el->el_errfile, fmt, key, "no input");
}


/* key__decode_char():
 *	Put a printable form of char in buf.
 */
private int
key__decode_char(buf, cnt, ch)
    char *buf;
    int   cnt, ch;
{
    if (ch == 0) {
	buf[cnt++] = '^';
	buf[cnt] = '@';
	return cnt;
    }

    if (iscntrl(ch)) {
	buf[cnt++] = '^';
	if (ch == '\177')
	    buf[cnt] = '?';
	else
	    buf[cnt] = ch | 0100;
    }
    else if (ch == '^') {
	buf[cnt++] = '\\';
	buf[cnt] = '^';
    }
    else if (ch == '\\') {
	buf[cnt++] = '\\';
	buf[cnt] = '\\';
    }
    else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
	buf[cnt] = ch;
    }
    else {
	buf[cnt++] = '\\';
	buf[cnt++] = ((ch >> 6) & 7) + '0';
	buf[cnt++] = ((ch >> 3) & 7) + '0';
	buf[cnt] = (ch & 7) + '0';
    }
    return cnt;
}

/* key__decode_str():
 *	Make a printable version of the ey
 */
protected char *
key__decode_str(str, buf, sep)
    char   *str;
    char   *buf;
    char   *sep;
{
    char *b, *p;

    b = buf;
    if (sep[0] != '\0')
	*b++ = sep[0];
    if (*str == 0) {
	*b++ = '^';
	*b++ = '@';
	if (sep[0] != '\0' && sep[1] != '\0')
	    *b++ = sep[1];
	*b++ = 0;
	return buf;
    }

    for (p = str; *p != 0; p++) {
	if (iscntrl((unsigned char) *p)) {
	    *b++ = '^';
	    if (*p == '\177')
		*b++ = '?';
	    else
		*b++ = *p | 0100;
	}
	else if (*p == '^' || *p == '\\') {
	    *b++ = '\\';
	    *b++ = *p;
	}
	else if (*p == ' ' || (isprint((unsigned char) *p) &&
			       !isspace((unsigned char) *p))) {
	    *b++ = *p;
	}
	else {
	    *b++ = '\\';
	    *b++ = ((*p >> 6) & 7) + '0';
	    *b++ = ((*p >> 3) & 7) + '0';
	    *b++ = (*p & 7) + '0';
	}
    }
    if (sep[0] != '\0' && sep[1] != '\0')
	*b++ = sep[1];
    *b++ = 0;
    return buf;			/* should check for overflow */
}
OpenPOWER on IntegriCloud