1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $NetBSD: key.c,v 1.16 2005/07/06 21:13:02 christos Exp $
33 */
34
35#if !defined(lint) && !defined(SCCSID)
36static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
37#endif /* not lint && not SCCSID */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libedit/key.c 148834 2005-08-07 20:55:59Z stefanf $");
40
41/*
42 * key.c: This module contains the procedures for maintaining
43 * the extended-key map.
44 *
45 * An extended-key (key) is a sequence of keystrokes introduced
46 * with a sequence introducer and consisting of an arbitrary
47 * number of characters. This module maintains a map (the el->el_key.map)
48 * to convert these extended-key sequences into input strs
49 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
50 *
51 * Warning:
52 * If key is a substr of some other keys, then the longer
53 * keys are lost!! That is, if the keys "abcd" and "abcef"
54 * are in el->el_key.map, adding the key "abc" will cause the first two
55 * definitions to be lost.
56 *
57 * Restrictions:
58 * -------------
59 * 1) It is not possible to have one key that is a
60 * substr of another.
61 */
62#include <string.h>
63#include <stdlib.h>
64
65#include "el.h"
66
67/*
68 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
69 * of these node elements
70 */
71struct key_node_t {
72 char ch; /* single character of key */
73 int type; /* node type */
74 key_value_t val; /* command code or pointer to str, */
75 /* if this is a leaf */
76 struct key_node_t *next; /* ptr to next char of this key */
77 struct key_node_t *sibling; /* ptr to another key with same prefix*/
78};
79
80private int node_trav(EditLine *, key_node_t *, char *,
81 key_value_t *);
82private int node__try(EditLine *, key_node_t *, const char *,
83 key_value_t *, int);
84private key_node_t *node__get(int);
85private void node__free(key_node_t *);
86private void node__put(EditLine *, key_node_t *);
87private int node__delete(EditLine *, key_node_t **, const char *);
88private int node_lookup(EditLine *, const char *, key_node_t *,
89 int);
90private int node_enum(EditLine *, key_node_t *, int);
91private int key__decode_char(char *, int, int);
92
93#define KEY_BUFSIZ EL_BUFSIZ
94
95
96/* key_init():
97 * Initialize the key maps
98 */
99protected int
100key_init(EditLine *el)
101{
102
103 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
104 if (el->el_key.buf == NULL)
105 return (-1);
106 el->el_key.map = NULL;
107 key_reset(el);
108 return (0);
109}
110
111/* key_end():
112 * Free the key maps
113 */
114protected void
115key_end(EditLine *el)
116{
117
118 el_free((ptr_t) el->el_key.buf);
119 el->el_key.buf = NULL;
120 node__free(el->el_key.map);
121}
122
123
124/* key_map_cmd():
125 * Associate cmd with a key value
126 */
127protected key_value_t *
128key_map_cmd(EditLine *el, int cmd)
129{
130
131 el->el_key.val.cmd = (el_action_t) cmd;
132 return (&el->el_key.val);
133}
134
135
136/* key_map_str():
137 * Associate str with a key value
138 */
139protected key_value_t *
140key_map_str(EditLine *el, char *str)
141{
142
143 el->el_key.val.str = str;
144 return (&el->el_key.val);
145}
146
147
148/* key_reset():
149 * Takes all nodes on el->el_key.map and puts them on free list. Then
150 * initializes el->el_key.map with arrow keys
151 * [Always bind the ansi arrow keys?]
152 */
153protected void
154key_reset(EditLine *el)
155{
156
157 node__put(el, el->el_key.map);
158 el->el_key.map = NULL;
159 return;
160}
161
162
163/* key_get():
164 * Calls the recursive function with entry point el->el_key.map
165 * Looks up *ch in map and then reads characters until a
166 * complete match is found or a mismatch occurs. Returns the
167 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
168 * Returns NULL in val.str and XK_STR for no match.
169 * The last character read is returned in *ch.
170 */
171protected int
172key_get(EditLine *el, char *ch, key_value_t *val)
173{
174
175 return (node_trav(el, el->el_key.map, ch, val));
176}
177
178
179/* key_add():
180 * Adds key to the el->el_key.map and associates the value in val with it.
181 * If key is already is in el->el_key.map, the new code is applied to the
182 * existing key. Ntype specifies if code is a command, an
183 * out str or a unix command.
184 */
185protected void
186key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
187{
188
189 if (key[0] == '\0') {
190 (void) fprintf(el->el_errfile,
191 "key_add: Null extended-key not allowed.\n");
192 return;
193 }
194 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
195 (void) fprintf(el->el_errfile,
196 "key_add: sequence-lead-in command not allowed\n");
197 return;
198 }
199 if (el->el_key.map == NULL)
200 /* tree is initially empty. Set up new node to match key[0] */
201 el->el_key.map = node__get(key[0]);
202 /* it is properly initialized */
203
204 /* Now recurse through el->el_key.map */
205 (void) node__try(el, el->el_key.map, key, val, ntype);
206 return;
207}
208
209
210/* key_clear():
211 *
212 */
213protected void
214key_clear(EditLine *el, el_action_t *map, const char *in)
215{
216
217 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
218 ((map == el->el_map.key &&
219 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
220 (map == el->el_map.alt &&
221 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
222 (void) key_delete(el, in);
223}
224
225
226/* key_delete():
227 * Delete the key and all longer keys staring with key, if
228 * they exists.
229 */
230protected int
231key_delete(EditLine *el, const char *key)
232{
233
234 if (key[0] == '\0') {
235 (void) fprintf(el->el_errfile,
236 "key_delete: Null extended-key not allowed.\n");
237 return (-1);
238 }
239 if (el->el_key.map == NULL)
240 return (0);
241
242 (void) node__delete(el, &el->el_key.map, key);
243 return (0);
244}
245
246
247/* key_print():
248 * Print the binding associated with key key.
249 * Print entire el->el_key.map if null
250 */
251protected void
252key_print(EditLine *el, const char *key)
253{
254
255 /* do nothing if el->el_key.map is empty and null key specified */
256 if (el->el_key.map == NULL && *key == 0)
257 return;
258
259 el->el_key.buf[0] = '"';
260 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
261 /* key is not bound */
262 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
263 key);
264 return;
265}
266
267
268/* node_trav():
269 * recursively traverses node in tree until match or mismatch is
270 * found. May read in more characters.
271 */
272private int
273node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
274{
275
276 if (ptr->ch == *ch) {
277 /* match found */
278 if (ptr->next) {
279 /* key not complete so get next char */
280 if (el_getc(el, ch) != 1) { /* if EOF or error */
281 val->cmd = ED_END_OF_FILE;
282 return (XK_CMD);
283 /* PWP: Pretend we just read an end-of-file */
284 }
285 return (node_trav(el, ptr->next, ch, val));
286 } else {
287 *val = ptr->val;
288 if (ptr->type != XK_CMD)
289 *ch = '\0';
290 return (ptr->type);
291 }
292 } else {
293 /* no match found here */
294 if (ptr->sibling) {
295 /* try next sibling */
296 return (node_trav(el, ptr->sibling, ch, val));
297 } else {
298 /* no next sibling -- mismatch */
299 val->str = NULL;
300 return (XK_STR);
301 }
302 }
303}
304
305
306/* node__try():
307 * Find a node that matches *str or allocate a new one
308 */
309private int
310node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
311{
312
313 if (ptr->ch != *str) {
314 key_node_t *xm;
315
316 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
317 if (xm->sibling->ch == *str)
318 break;
319 if (xm->sibling == NULL)
320 xm->sibling = node__get(*str); /* setup new node */
321 ptr = xm->sibling;
322 }
323 if (*++str == '\0') {
324 /* we're there */
325 if (ptr->next != NULL) {
326 node__put(el, ptr->next);
327 /* lose longer keys with this prefix */
328 ptr->next = NULL;
329 }
330 switch (ptr->type) {
331 case XK_CMD:
332 case XK_NOD:
333 break;
334 case XK_STR:
335 case XK_EXE:
336 if (ptr->val.str)
337 el_free((ptr_t) ptr->val.str);
338 break;
339 default:
340 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
341 ptr->type));
342 break;
343 }
344
345 switch (ptr->type = ntype) {
346 case XK_CMD:
347 ptr->val = *val;
348 break;
349 case XK_STR:
350 case XK_EXE:
351 if ((ptr->val.str = el_strdup(val->str)) == NULL)
352 return -1;
353 break;
354 default:
355 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
356 break;
357 }
358 } else {
359 /* still more chars to go */
360 if (ptr->next == NULL)
361 ptr->next = node__get(*str); /* setup new node */
362 (void) node__try(el, ptr->next, str, val, ntype);
363 }
364 return (0);
365}
366
367
368/* node__delete():
369 * Delete node that matches str
370 */
371private int
372node__delete(EditLine *el, key_node_t **inptr, const char *str)
373{
374 key_node_t *ptr;
375 key_node_t *prev_ptr = NULL;
376
377 ptr = *inptr;
378
379 if (ptr->ch != *str) {
380 key_node_t *xm;
381
382 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
383 if (xm->sibling->ch == *str)
384 break;
385 if (xm->sibling == NULL)
386 return (0);
387 prev_ptr = xm;
388 ptr = xm->sibling;
389 }
390 if (*++str == '\0') {
391 /* we're there */
392 if (prev_ptr == NULL)
393 *inptr = ptr->sibling;
394 else
395 prev_ptr->sibling = ptr->sibling;
396 ptr->sibling = NULL;
397 node__put(el, ptr);
398 return (1);
399 } else if (ptr->next != NULL &&
400 node__delete(el, &ptr->next, str) == 1) {
401 if (ptr->next != NULL)
402 return (0);
403 if (prev_ptr == NULL)
404 *inptr = ptr->sibling;
405 else
406 prev_ptr->sibling = ptr->sibling;
407 ptr->sibling = NULL;
408 node__put(el, ptr);
409 return (1);
410 } else {
411 return (0);
412 }
413}
414
415
416/* node__put():
417 * Puts a tree of nodes onto free list using free(3).
418 */
419private void
420node__put(EditLine *el, key_node_t *ptr)
421{
422 if (ptr == NULL)
423 return;
424
425 if (ptr->next != NULL) {
426 node__put(el, ptr->next);
427 ptr->next = NULL;
428 }
429 node__put(el, ptr->sibling);
430
431 switch (ptr->type) {
432 case XK_CMD:
433 case XK_NOD:
434 break;
435 case XK_EXE:
436 case XK_STR:
437 if (ptr->val.str != NULL)
438 el_free((ptr_t) ptr->val.str);
439 break;
440 default:
441 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
442 break;
443 }
444 el_free((ptr_t) ptr);
445}
446
447
448/* node__get():
449 * Returns pointer to a key_node_t for ch.
450 */
451private key_node_t *
452node__get(int ch)
453{
454 key_node_t *ptr;
455
456 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
457 if (ptr == NULL)
458 return NULL;
459 ptr->ch = ch;
460 ptr->type = XK_NOD;
461 ptr->val.str = NULL;
462 ptr->next = NULL;
463 ptr->sibling = NULL;
464 return (ptr);
465}
466
467private void
468node__free(key_node_t *k)
469{
470 if (k == NULL)
471 return;
472 node__free(k->sibling);
473 node__free(k->next);
474 el_free((ptr_t) k);
475}
476
477/* node_lookup():
478 * look for the str starting at node ptr.
479 * Print if last node
480 */
481private int
482node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt)
483{
484 int ncnt;
485
486 if (ptr == NULL)
487 return (-1); /* cannot have null ptr */
488
489 if (*str == 0) {
490 /* no more chars in str. node_enum from here. */
491 (void) node_enum(el, ptr, cnt);
492 return (0);
493 } else {
494 /* If match put this char into el->el_key.buf. Recurse */
495 if (ptr->ch == *str) {
496 /* match found */
497 ncnt = key__decode_char(el->el_key.buf, cnt,
498 (unsigned char) ptr->ch);
499 if (ptr->next != NULL)
500 /* not yet at leaf */
501 return (node_lookup(el, str + 1, ptr->next,
502 ncnt + 1));
503 else {
504 /* next node is null so key should be complete */
505 if (str[1] == 0) {
506 el->el_key.buf[ncnt + 1] = '"';
507 el->el_key.buf[ncnt + 2] = '\0';
508 key_kprint(el, el->el_key.buf,
509 &ptr->val, ptr->type);
510 return (0);
511 } else
512 return (-1);
513 /* mismatch -- str still has chars */
514 }
515 } else {
516 /* no match found try sibling */
517 if (ptr->sibling)
518 return (node_lookup(el, str, ptr->sibling,
519 cnt));
520 else
521 return (-1);
522 }
523 }
524}
525
526
527/* node_enum():
528 * Traverse the node printing the characters it is bound in buffer
529 */
530private int
531node_enum(EditLine *el, key_node_t *ptr, int cnt)
532{
533 int ncnt;
534
535 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
536 el->el_key.buf[++cnt] = '"';
537 el->el_key.buf[++cnt] = '\0';
538 (void) fprintf(el->el_errfile,
539 "Some extended keys too long for internal print buffer");
540 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
541 return (0);
542 }
543 if (ptr == NULL) {
544#ifdef DEBUG_EDIT
545 (void) fprintf(el->el_errfile,
546 "node_enum: BUG!! Null ptr passed\n!");
547#endif
548 return (-1);
549 }
550 /* put this char at end of str */
551 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
552 if (ptr->next == NULL) {
553 /* print this key and function */
554 el->el_key.buf[ncnt + 1] = '"';
555 el->el_key.buf[ncnt + 2] = '\0';
556 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
557 } else
558 (void) node_enum(el, ptr->next, ncnt + 1);
559
560 /* go to sibling if there is one */
561 if (ptr->sibling)
562 (void) node_enum(el, ptr->sibling, cnt);
563 return (0);
564}
565
566
567/* key_kprint():
568 * Print the specified key and its associated
569 * function specified by val
570 */
571protected void
572key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
573{
574 el_bindings_t *fp;
575 char unparsbuf[EL_BUFSIZ];
576 static const char fmt[] = "%-15s-> %s\n";
577
578 if (val != NULL)
579 switch (ntype) {
580 case XK_STR:
581 case XK_EXE:
582 (void) fprintf(el->el_outfile, fmt, key,
583 key__decode_str(val->str, unparsbuf,
584 ntype == XK_STR ? "\"\"" : "[]"));
585 break;
586 case XK_CMD:
587 for (fp = el->el_map.help; fp->name; fp++)
588 if (val->cmd == fp->func) {
589 (void) fprintf(el->el_outfile, fmt,
590 key, fp->name);
591 break;
592 }
593#ifdef DEBUG_KEY
594 if (fp->name == NULL)
595 (void) fprintf(el->el_outfile,
596 "BUG! Command not found.\n");
597#endif
598
599 break;
600 default:
601 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
602 break;
603 }
604 else
605 (void) fprintf(el->el_outfile, fmt, key, "no input");
606}
607
608
609/* key__decode_char():
610 * Put a printable form of char in buf.
611 */
612private int
613key__decode_char(char *buf, int cnt, int ch)
614{
615 ch = (unsigned char)ch;
616
617 if (ch == 0) {
618 buf[cnt++] = '^';
619 buf[cnt] = '@';
620 return (cnt);
621 }
622 if (iscntrl(ch)) {
623 buf[cnt++] = '^';
624 if (ch == 0177)
625 buf[cnt] = '?';
626 else
627 buf[cnt] = toascii(ch) | 0100;
628 } else if (ch == '^') {
629 buf[cnt++] = '\\';
630 buf[cnt] = '^';
631 } else if (ch == '\\') {
632 buf[cnt++] = '\\';
633 buf[cnt] = '\\';
634 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
635 buf[cnt] = ch;
636 } else {
637 buf[cnt++] = '\\';
638 buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
639 buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
640 buf[cnt] = (ch & 7) + '0';
641 }
642 return (cnt);
643}
644
645
646/* key__decode_str():
647 * Make a printable version of the ey
648 */
649protected char *
650key__decode_str(const char *str, char *buf, const char *sep)
651{
652 char *b;
653 const char *p;
654
655 b = buf;
656 if (sep[0] != '\0')
657 *b++ = sep[0];
658 if (*str == 0) {
659 *b++ = '^';
660 *b++ = '@';
661 if (sep[0] != '\0' && sep[1] != '\0')
662 *b++ = sep[1];
663 *b++ = 0;
664 return (buf);
665 }
666 for (p = str; *p != 0; p++) {
667 if (iscntrl((unsigned char) *p)) {
668 *b++ = '^';
669 if (*p == '\177')
670 *b++ = '?';
671 else
672 *b++ = toascii(*p) | 0100;
673 } else if (*p == '^' || *p == '\\') {
674 *b++ = '\\';
675 *b++ = *p;
676 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
677 !isspace((unsigned char) *p))) {
678 *b++ = *p;
679 } else {
680 *b++ = '\\';
681 *b++ = (((unsigned int) *p >> 6) & 7) + '0';
682 *b++ = (((unsigned int) *p >> 3) & 7) + '0';
683 *b++ = (*p & 7) + '0';
684 }
685 }
686 if (sep[0] != '\0' && sep[1] != '\0')
687 *b++ = sep[1];
688 *b++ = 0;
689 return (buf); /* should check for overflow */
690}
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $NetBSD: key.c,v 1.16 2005/07/06 21:13:02 christos Exp $
33 */
34
35#if !defined(lint) && !defined(SCCSID)
36static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
37#endif /* not lint && not SCCSID */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libedit/key.c 148834 2005-08-07 20:55:59Z stefanf $");
40
41/*
42 * key.c: This module contains the procedures for maintaining
43 * the extended-key map.
44 *
45 * An extended-key (key) is a sequence of keystrokes introduced
46 * with a sequence introducer and consisting of an arbitrary
47 * number of characters. This module maintains a map (the el->el_key.map)
48 * to convert these extended-key sequences into input strs
49 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
50 *
51 * Warning:
52 * If key is a substr of some other keys, then the longer
53 * keys are lost!! That is, if the keys "abcd" and "abcef"
54 * are in el->el_key.map, adding the key "abc" will cause the first two
55 * definitions to be lost.
56 *
57 * Restrictions:
58 * -------------
59 * 1) It is not possible to have one key that is a
60 * substr of another.
61 */
62#include <string.h>
63#include <stdlib.h>
64
65#include "el.h"
66
67/*
68 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
69 * of these node elements
70 */
71struct key_node_t {
72 char ch; /* single character of key */
73 int type; /* node type */
74 key_value_t val; /* command code or pointer to str, */
75 /* if this is a leaf */
76 struct key_node_t *next; /* ptr to next char of this key */
77 struct key_node_t *sibling; /* ptr to another key with same prefix*/
78};
79
80private int node_trav(EditLine *, key_node_t *, char *,
81 key_value_t *);
82private int node__try(EditLine *, key_node_t *, const char *,
83 key_value_t *, int);
84private key_node_t *node__get(int);
85private void node__free(key_node_t *);
86private void node__put(EditLine *, key_node_t *);
87private int node__delete(EditLine *, key_node_t **, const char *);
88private int node_lookup(EditLine *, const char *, key_node_t *,
89 int);
90private int node_enum(EditLine *, key_node_t *, int);
91private int key__decode_char(char *, int, int);
92
93#define KEY_BUFSIZ EL_BUFSIZ
94
95
96/* key_init():
97 * Initialize the key maps
98 */
99protected int
100key_init(EditLine *el)
101{
102
103 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
104 if (el->el_key.buf == NULL)
105 return (-1);
106 el->el_key.map = NULL;
107 key_reset(el);
108 return (0);
109}
110
111/* key_end():
112 * Free the key maps
113 */
114protected void
115key_end(EditLine *el)
116{
117
118 el_free((ptr_t) el->el_key.buf);
119 el->el_key.buf = NULL;
120 node__free(el->el_key.map);
121}
122
123
124/* key_map_cmd():
125 * Associate cmd with a key value
126 */
127protected key_value_t *
128key_map_cmd(EditLine *el, int cmd)
129{
130
131 el->el_key.val.cmd = (el_action_t) cmd;
132 return (&el->el_key.val);
133}
134
135
136/* key_map_str():
137 * Associate str with a key value
138 */
139protected key_value_t *
140key_map_str(EditLine *el, char *str)
141{
142
143 el->el_key.val.str = str;
144 return (&el->el_key.val);
145}
146
147
148/* key_reset():
149 * Takes all nodes on el->el_key.map and puts them on free list. Then
150 * initializes el->el_key.map with arrow keys
151 * [Always bind the ansi arrow keys?]
152 */
153protected void
154key_reset(EditLine *el)
155{
156
157 node__put(el, el->el_key.map);
158 el->el_key.map = NULL;
159 return;
160}
161
162
163/* key_get():
164 * Calls the recursive function with entry point el->el_key.map
165 * Looks up *ch in map and then reads characters until a
166 * complete match is found or a mismatch occurs. Returns the
167 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
168 * Returns NULL in val.str and XK_STR for no match.
169 * The last character read is returned in *ch.
170 */
171protected int
172key_get(EditLine *el, char *ch, key_value_t *val)
173{
174
175 return (node_trav(el, el->el_key.map, ch, val));
176}
177
178
179/* key_add():
180 * Adds key to the el->el_key.map and associates the value in val with it.
181 * If key is already is in el->el_key.map, the new code is applied to the
182 * existing key. Ntype specifies if code is a command, an
183 * out str or a unix command.
184 */
185protected void
186key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
187{
188
189 if (key[0] == '\0') {
190 (void) fprintf(el->el_errfile,
191 "key_add: Null extended-key not allowed.\n");
192 return;
193 }
194 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
195 (void) fprintf(el->el_errfile,
196 "key_add: sequence-lead-in command not allowed\n");
197 return;
198 }
199 if (el->el_key.map == NULL)
200 /* tree is initially empty. Set up new node to match key[0] */
201 el->el_key.map = node__get(key[0]);
202 /* it is properly initialized */
203
204 /* Now recurse through el->el_key.map */
205 (void) node__try(el, el->el_key.map, key, val, ntype);
206 return;
207}
208
209
210/* key_clear():
211 *
212 */
213protected void
214key_clear(EditLine *el, el_action_t *map, const char *in)
215{
216
217 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
218 ((map == el->el_map.key &&
219 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
220 (map == el->el_map.alt &&
221 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
222 (void) key_delete(el, in);
223}
224
225
226/* key_delete():
227 * Delete the key and all longer keys staring with key, if
228 * they exists.
229 */
230protected int
231key_delete(EditLine *el, const char *key)
232{
233
234 if (key[0] == '\0') {
235 (void) fprintf(el->el_errfile,
236 "key_delete: Null extended-key not allowed.\n");
237 return (-1);
238 }
239 if (el->el_key.map == NULL)
240 return (0);
241
242 (void) node__delete(el, &el->el_key.map, key);
243 return (0);
244}
245
246
247/* key_print():
248 * Print the binding associated with key key.
249 * Print entire el->el_key.map if null
250 */
251protected void
252key_print(EditLine *el, const char *key)
253{
254
255 /* do nothing if el->el_key.map is empty and null key specified */
256 if (el->el_key.map == NULL && *key == 0)
257 return;
258
259 el->el_key.buf[0] = '"';
260 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
261 /* key is not bound */
262 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
263 key);
264 return;
265}
266
267
268/* node_trav():
269 * recursively traverses node in tree until match or mismatch is
270 * found. May read in more characters.
271 */
272private int
273node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
274{
275
276 if (ptr->ch == *ch) {
277 /* match found */
278 if (ptr->next) {
279 /* key not complete so get next char */
280 if (el_getc(el, ch) != 1) { /* if EOF or error */
281 val->cmd = ED_END_OF_FILE;
282 return (XK_CMD);
283 /* PWP: Pretend we just read an end-of-file */
284 }
285 return (node_trav(el, ptr->next, ch, val));
286 } else {
287 *val = ptr->val;
288 if (ptr->type != XK_CMD)
289 *ch = '\0';
290 return (ptr->type);
291 }
292 } else {
293 /* no match found here */
294 if (ptr->sibling) {
295 /* try next sibling */
296 return (node_trav(el, ptr->sibling, ch, val));
297 } else {
298 /* no next sibling -- mismatch */
299 val->str = NULL;
300 return (XK_STR);
301 }
302 }
303}
304
305
306/* node__try():
307 * Find a node that matches *str or allocate a new one
308 */
309private int
310node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
311{
312
313 if (ptr->ch != *str) {
314 key_node_t *xm;
315
316 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
317 if (xm->sibling->ch == *str)
318 break;
319 if (xm->sibling == NULL)
320 xm->sibling = node__get(*str); /* setup new node */
321 ptr = xm->sibling;
322 }
323 if (*++str == '\0') {
324 /* we're there */
325 if (ptr->next != NULL) {
326 node__put(el, ptr->next);
327 /* lose longer keys with this prefix */
328 ptr->next = NULL;
329 }
330 switch (ptr->type) {
331 case XK_CMD:
332 case XK_NOD:
333 break;
334 case XK_STR:
335 case XK_EXE:
336 if (ptr->val.str)
337 el_free((ptr_t) ptr->val.str);
338 break;
339 default:
340 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
341 ptr->type));
342 break;
343 }
344
345 switch (ptr->type = ntype) {
346 case XK_CMD:
347 ptr->val = *val;
348 break;
349 case XK_STR:
350 case XK_EXE:
351 if ((ptr->val.str = el_strdup(val->str)) == NULL)
352 return -1;
353 break;
354 default:
355 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
356 break;
357 }
358 } else {
359 /* still more chars to go */
360 if (ptr->next == NULL)
361 ptr->next = node__get(*str); /* setup new node */
362 (void) node__try(el, ptr->next, str, val, ntype);
363 }
364 return (0);
365}
366
367
368/* node__delete():
369 * Delete node that matches str
370 */
371private int
372node__delete(EditLine *el, key_node_t **inptr, const char *str)
373{
374 key_node_t *ptr;
375 key_node_t *prev_ptr = NULL;
376
377 ptr = *inptr;
378
379 if (ptr->ch != *str) {
380 key_node_t *xm;
381
382 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
383 if (xm->sibling->ch == *str)
384 break;
385 if (xm->sibling == NULL)
386 return (0);
387 prev_ptr = xm;
388 ptr = xm->sibling;
389 }
390 if (*++str == '\0') {
391 /* we're there */
392 if (prev_ptr == NULL)
393 *inptr = ptr->sibling;
394 else
395 prev_ptr->sibling = ptr->sibling;
396 ptr->sibling = NULL;
397 node__put(el, ptr);
398 return (1);
399 } else if (ptr->next != NULL &&
400 node__delete(el, &ptr->next, str) == 1) {
401 if (ptr->next != NULL)
402 return (0);
403 if (prev_ptr == NULL)
404 *inptr = ptr->sibling;
405 else
406 prev_ptr->sibling = ptr->sibling;
407 ptr->sibling = NULL;
408 node__put(el, ptr);
409 return (1);
410 } else {
411 return (0);
412 }
413}
414
415
416/* node__put():
417 * Puts a tree of nodes onto free list using free(3).
418 */
419private void
420node__put(EditLine *el, key_node_t *ptr)
421{
422 if (ptr == NULL)
423 return;
424
425 if (ptr->next != NULL) {
426 node__put(el, ptr->next);
427 ptr->next = NULL;
428 }
429 node__put(el, ptr->sibling);
430
431 switch (ptr->type) {
432 case XK_CMD:
433 case XK_NOD:
434 break;
435 case XK_EXE:
436 case XK_STR:
437 if (ptr->val.str != NULL)
438 el_free((ptr_t) ptr->val.str);
439 break;
440 default:
441 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
442 break;
443 }
444 el_free((ptr_t) ptr);
445}
446
447
448/* node__get():
449 * Returns pointer to a key_node_t for ch.
450 */
451private key_node_t *
452node__get(int ch)
453{
454 key_node_t *ptr;
455
456 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
457 if (ptr == NULL)
458 return NULL;
459 ptr->ch = ch;
460 ptr->type = XK_NOD;
461 ptr->val.str = NULL;
462 ptr->next = NULL;
463 ptr->sibling = NULL;
464 return (ptr);
465}
466
467private void
468node__free(key_node_t *k)
469{
470 if (k == NULL)
471 return;
472 node__free(k->sibling);
473 node__free(k->next);
474 el_free((ptr_t) k);
475}
476
477/* node_lookup():
478 * look for the str starting at node ptr.
479 * Print if last node
480 */
481private int
482node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt)
483{
484 int ncnt;
485
486 if (ptr == NULL)
487 return (-1); /* cannot have null ptr */
488
489 if (*str == 0) {
490 /* no more chars in str. node_enum from here. */
491 (void) node_enum(el, ptr, cnt);
492 return (0);
493 } else {
494 /* If match put this char into el->el_key.buf. Recurse */
495 if (ptr->ch == *str) {
496 /* match found */
497 ncnt = key__decode_char(el->el_key.buf, cnt,
498 (unsigned char) ptr->ch);
499 if (ptr->next != NULL)
500 /* not yet at leaf */
501 return (node_lookup(el, str + 1, ptr->next,
502 ncnt + 1));
503 else {
504 /* next node is null so key should be complete */
505 if (str[1] == 0) {
506 el->el_key.buf[ncnt + 1] = '"';
507 el->el_key.buf[ncnt + 2] = '\0';
508 key_kprint(el, el->el_key.buf,
509 &ptr->val, ptr->type);
510 return (0);
511 } else
512 return (-1);
513 /* mismatch -- str still has chars */
514 }
515 } else {
516 /* no match found try sibling */
517 if (ptr->sibling)
518 return (node_lookup(el, str, ptr->sibling,
519 cnt));
520 else
521 return (-1);
522 }
523 }
524}
525
526
527/* node_enum():
528 * Traverse the node printing the characters it is bound in buffer
529 */
530private int
531node_enum(EditLine *el, key_node_t *ptr, int cnt)
532{
533 int ncnt;
534
535 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
536 el->el_key.buf[++cnt] = '"';
537 el->el_key.buf[++cnt] = '\0';
538 (void) fprintf(el->el_errfile,
539 "Some extended keys too long for internal print buffer");
540 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
541 return (0);
542 }
543 if (ptr == NULL) {
544#ifdef DEBUG_EDIT
545 (void) fprintf(el->el_errfile,
546 "node_enum: BUG!! Null ptr passed\n!");
547#endif
548 return (-1);
549 }
550 /* put this char at end of str */
551 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
552 if (ptr->next == NULL) {
553 /* print this key and function */
554 el->el_key.buf[ncnt + 1] = '"';
555 el->el_key.buf[ncnt + 2] = '\0';
556 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
557 } else
558 (void) node_enum(el, ptr->next, ncnt + 1);
559
560 /* go to sibling if there is one */
561 if (ptr->sibling)
562 (void) node_enum(el, ptr->sibling, cnt);
563 return (0);
564}
565
566
567/* key_kprint():
568 * Print the specified key and its associated
569 * function specified by val
570 */
571protected void
572key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
573{
574 el_bindings_t *fp;
575 char unparsbuf[EL_BUFSIZ];
576 static const char fmt[] = "%-15s-> %s\n";
577
578 if (val != NULL)
579 switch (ntype) {
580 case XK_STR:
581 case XK_EXE:
582 (void) fprintf(el->el_outfile, fmt, key,
583 key__decode_str(val->str, unparsbuf,
584 ntype == XK_STR ? "\"\"" : "[]"));
585 break;
586 case XK_CMD:
587 for (fp = el->el_map.help; fp->name; fp++)
588 if (val->cmd == fp->func) {
589 (void) fprintf(el->el_outfile, fmt,
590 key, fp->name);
591 break;
592 }
593#ifdef DEBUG_KEY
594 if (fp->name == NULL)
595 (void) fprintf(el->el_outfile,
596 "BUG! Command not found.\n");
597#endif
598
599 break;
600 default:
601 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
602 break;
603 }
604 else
605 (void) fprintf(el->el_outfile, fmt, key, "no input");
606}
607
608
609/* key__decode_char():
610 * Put a printable form of char in buf.
611 */
612private int
613key__decode_char(char *buf, int cnt, int ch)
614{
615 ch = (unsigned char)ch;
616
617 if (ch == 0) {
618 buf[cnt++] = '^';
619 buf[cnt] = '@';
620 return (cnt);
621 }
622 if (iscntrl(ch)) {
623 buf[cnt++] = '^';
624 if (ch == 0177)
625 buf[cnt] = '?';
626 else
627 buf[cnt] = toascii(ch) | 0100;
628 } else if (ch == '^') {
629 buf[cnt++] = '\\';
630 buf[cnt] = '^';
631 } else if (ch == '\\') {
632 buf[cnt++] = '\\';
633 buf[cnt] = '\\';
634 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
635 buf[cnt] = ch;
636 } else {
637 buf[cnt++] = '\\';
638 buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
639 buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
640 buf[cnt] = (ch & 7) + '0';
641 }
642 return (cnt);
643}
644
645
646/* key__decode_str():
647 * Make a printable version of the ey
648 */
649protected char *
650key__decode_str(const char *str, char *buf, const char *sep)
651{
652 char *b;
653 const char *p;
654
655 b = buf;
656 if (sep[0] != '\0')
657 *b++ = sep[0];
658 if (*str == 0) {
659 *b++ = '^';
660 *b++ = '@';
661 if (sep[0] != '\0' && sep[1] != '\0')
662 *b++ = sep[1];
663 *b++ = 0;
664 return (buf);
665 }
666 for (p = str; *p != 0; p++) {
667 if (iscntrl((unsigned char) *p)) {
668 *b++ = '^';
669 if (*p == '\177')
670 *b++ = '?';
671 else
672 *b++ = toascii(*p) | 0100;
673 } else if (*p == '^' || *p == '\\') {
674 *b++ = '\\';
675 *b++ = *p;
676 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
677 !isspace((unsigned char) *p))) {
678 *b++ = *p;
679 } else {
680 *b++ = '\\';
681 *b++ = (((unsigned int) *p >> 6) & 7) + '0';
682 *b++ = (((unsigned int) *p >> 3) & 7) + '0';
683 *b++ = (*p & 7) + '0';
684 }
685 }
686 if (sep[0] != '\0' && sep[1] != '\0')
687 *b++ = sep[1];
688 *b++ = 0;
689 return (buf); /* should check for overflow */
690}