2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * $NetBSD: key.c,v 1.19 2006/03/23 20:22:51 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid
[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * key.c: This module contains the procedures for maintaining
43 * the extended-key map.
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).
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.
59 * 1) It is not possible to have one key that is a
68 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
69 * of these node elements
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*/
80 private int node_trav(EditLine
*, key_node_t
*, char *,
82 private int node__try(EditLine
*, key_node_t
*, const char *,
84 private key_node_t
*node__get(int);
85 private void node__free(key_node_t
*);
86 private void node__put(EditLine
*, key_node_t
*);
87 private int node__delete(EditLine
*, key_node_t
**, const char *);
88 private int node_lookup(EditLine
*, const char *, key_node_t
*,
90 private int node_enum(EditLine
*, key_node_t
*, int);
92 #define KEY_BUFSIZ EL_BUFSIZ
96 * Initialize the key maps
99 key_init(EditLine
*el
)
102 el
->el_key
.buf
= (char *) el_malloc(KEY_BUFSIZ
);
103 if (el
->el_key
.buf
== NULL
)
105 el
->el_key
.map
= NULL
;
114 key_end(EditLine
*el
)
117 el_free((ptr_t
) el
->el_key
.buf
);
118 el
->el_key
.buf
= NULL
;
119 node__free(el
->el_key
.map
);
124 * Associate cmd with a key value
126 protected key_value_t
*
127 key_map_cmd(EditLine
*el
, int cmd
)
130 el
->el_key
.val
.cmd
= (el_action_t
) cmd
;
131 return (&el
->el_key
.val
);
136 * Associate str with a key value
138 protected key_value_t
*
139 key_map_str(EditLine
*el
, char *str
)
142 el
->el_key
.val
.str
= str
;
143 return (&el
->el_key
.val
);
148 * Takes all nodes on el->el_key.map and puts them on free list. Then
149 * initializes el->el_key.map with arrow keys
150 * [Always bind the ansi arrow keys?]
153 key_reset(EditLine
*el
)
156 node__put(el
, el
->el_key
.map
);
157 el
->el_key
.map
= NULL
;
163 * Calls the recursive function with entry point el->el_key.map
164 * Looks up *ch in map and then reads characters until a
165 * complete match is found or a mismatch occurs. Returns the
166 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
167 * Returns NULL in val.str and XK_STR for no match.
168 * The last character read is returned in *ch.
171 key_get(EditLine
*el
, char *ch
, key_value_t
*val
)
174 return (node_trav(el
, el
->el_key
.map
, ch
, val
));
179 * Adds key to the el->el_key.map and associates the value in val with it.
180 * If key is already is in el->el_key.map, the new code is applied to the
181 * existing key. Ntype specifies if code is a command, an
182 * out str or a unix command.
185 key_add(EditLine
*el
, const char *key
, key_value_t
*val
, int ntype
)
188 if (key
[0] == '\0') {
189 (void) fprintf(el
->el_errfile
,
190 "key_add: Null extended-key not allowed.\n");
193 if (ntype
== XK_CMD
&& val
->cmd
== ED_SEQUENCE_LEAD_IN
) {
194 (void) fprintf(el
->el_errfile
,
195 "key_add: sequence-lead-in command not allowed\n");
198 if (el
->el_key
.map
== NULL
)
199 /* tree is initially empty. Set up new node to match key[0] */
200 el
->el_key
.map
= node__get(key
[0]);
201 /* it is properly initialized */
203 /* Now recurse through el->el_key.map */
204 (void) node__try(el
, el
->el_key
.map
, key
, val
, ntype
);
213 key_clear(EditLine
*el
, el_action_t
*map
, const char *in
)
216 if ((map
[(unsigned char)*in
] == ED_SEQUENCE_LEAD_IN
) &&
217 ((map
== el
->el_map
.key
&&
218 el
->el_map
.alt
[(unsigned char)*in
] != ED_SEQUENCE_LEAD_IN
) ||
219 (map
== el
->el_map
.alt
&&
220 el
->el_map
.key
[(unsigned char)*in
] != ED_SEQUENCE_LEAD_IN
)))
221 (void) key_delete(el
, in
);
226 * Delete the key and all longer keys staring with key, if
230 key_delete(EditLine
*el
, const char *key
)
233 if (key
[0] == '\0') {
234 (void) fprintf(el
->el_errfile
,
235 "key_delete: Null extended-key not allowed.\n");
238 if (el
->el_key
.map
== NULL
)
241 (void) node__delete(el
, &el
->el_key
.map
, key
);
247 * Print the binding associated with key key.
248 * Print entire el->el_key.map if null
251 key_print(EditLine
*el
, const char *key
)
254 /* do nothing if el->el_key.map is empty and null key specified */
255 if (el
->el_key
.map
== NULL
&& *key
== 0)
258 el
->el_key
.buf
[0] = '"';
259 if (node_lookup(el
, key
, el
->el_key
.map
, 1) <= -1)
260 /* key is not bound */
261 (void) fprintf(el
->el_errfile
, "Unbound extended key \"%s\"\n",
268 * recursively traverses node in tree until match or mismatch is
269 * found. May read in more characters.
272 node_trav(EditLine
*el
, key_node_t
*ptr
, char *ch
, key_value_t
*val
)
275 if (ptr
->ch
== *ch
) {
278 /* key not complete so get next char */
279 if (el_getc(el
, ch
) != 1) { /* if EOF or error */
280 val
->cmd
= ED_END_OF_FILE
;
282 /* PWP: Pretend we just read an end-of-file */
284 return (node_trav(el
, ptr
->next
, ch
, val
));
287 if (ptr
->type
!= XK_CMD
)
292 /* no match found here */
294 /* try next sibling */
295 return (node_trav(el
, ptr
->sibling
, ch
, val
));
297 /* no next sibling -- mismatch */
306 * Find a node that matches *str or allocate a new one
309 node__try(EditLine
*el
, key_node_t
*ptr
, const char *str
, key_value_t
*val
, int ntype
)
312 if (ptr
->ch
!= *str
) {
315 for (xm
= ptr
; xm
->sibling
!= NULL
; xm
= xm
->sibling
)
316 if (xm
->sibling
->ch
== *str
)
318 if (xm
->sibling
== NULL
)
319 xm
->sibling
= node__get(*str
); /* setup new node */
322 if (*++str
== '\0') {
324 if (ptr
->next
!= NULL
) {
325 node__put(el
, ptr
->next
);
326 /* lose longer keys with this prefix */
336 el_free((ptr_t
) ptr
->val
.str
);
339 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n",
344 switch (ptr
->type
= ntype
) {
350 if ((ptr
->val
.str
= el_strdup(val
->str
)) == NULL
)
354 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ntype
));
358 /* still more chars to go */
359 if (ptr
->next
== NULL
)
360 ptr
->next
= node__get(*str
); /* setup new node */
361 (void) node__try(el
, ptr
->next
, str
, val
, ntype
);
368 * Delete node that matches str
371 node__delete(EditLine
*el
, key_node_t
**inptr
, const char *str
)
374 key_node_t
*prev_ptr
= NULL
;
378 if (ptr
->ch
!= *str
) {
381 for (xm
= ptr
; xm
->sibling
!= NULL
; xm
= xm
->sibling
)
382 if (xm
->sibling
->ch
== *str
)
384 if (xm
->sibling
== NULL
)
389 if (*++str
== '\0') {
391 if (prev_ptr
== NULL
)
392 *inptr
= ptr
->sibling
;
394 prev_ptr
->sibling
= ptr
->sibling
;
398 } else if (ptr
->next
!= NULL
&&
399 node__delete(el
, &ptr
->next
, str
) == 1) {
400 if (ptr
->next
!= NULL
)
402 if (prev_ptr
== NULL
)
403 *inptr
= ptr
->sibling
;
405 prev_ptr
->sibling
= ptr
->sibling
;
416 * Puts a tree of nodes onto free list using free(3).
419 node__put(EditLine
*el
, key_node_t
*ptr
)
424 if (ptr
->next
!= NULL
) {
425 node__put(el
, ptr
->next
);
428 node__put(el
, ptr
->sibling
);
436 if (ptr
->val
.str
!= NULL
)
437 el_free((ptr_t
) ptr
->val
.str
);
440 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ptr
->type
));
443 el_free((ptr_t
) ptr
);
448 * Returns pointer to a key_node_t for ch.
455 ptr
= (key_node_t
*) el_malloc((size_t) sizeof(key_node_t
));
467 node__free(key_node_t
*k
)
471 node__free(k
->sibling
);
477 * look for the str starting at node ptr.
481 node_lookup(EditLine
*el
, const char *str
, key_node_t
*ptr
, int cnt
)
486 return (-1); /* cannot have null ptr */
489 /* no more chars in str. node_enum from here. */
490 (void) node_enum(el
, ptr
, cnt
);
493 /* If match put this char into el->el_key.buf. Recurse */
494 if (ptr
->ch
== *str
) {
496 ncnt
= key__decode_char(el
->el_key
.buf
, KEY_BUFSIZ
, cnt
,
497 (unsigned char) ptr
->ch
);
498 if (ptr
->next
!= NULL
)
499 /* not yet at leaf */
500 return (node_lookup(el
, str
+ 1, ptr
->next
,
503 /* next node is null so key should be complete */
505 el
->el_key
.buf
[ncnt
+ 1] = '"';
506 el
->el_key
.buf
[ncnt
+ 2] = '\0';
507 key_kprint(el
, el
->el_key
.buf
,
508 &ptr
->val
, ptr
->type
);
512 /* mismatch -- str still has chars */
515 /* no match found try sibling */
517 return (node_lookup(el
, str
, ptr
->sibling
,
527 * Traverse the node printing the characters it is bound in buffer
530 node_enum(EditLine
*el
, key_node_t
*ptr
, int cnt
)
534 if (cnt
>= KEY_BUFSIZ
- 5) { /* buffer too small */
535 el
->el_key
.buf
[++cnt
] = '"';
536 el
->el_key
.buf
[++cnt
] = '\0';
537 (void) fprintf(el
->el_errfile
,
538 "Some extended keys too long for internal print buffer");
539 (void) fprintf(el
->el_errfile
, " \"%s...\"\n", el
->el_key
.buf
);
544 (void) fprintf(el
->el_errfile
,
545 "node_enum: BUG!! Null ptr passed\n!");
549 /* put this char at end of str */
550 ncnt
= key__decode_char(el
->el_key
.buf
, KEY_BUFSIZ
, cnt
,
551 (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
);
558 (void) node_enum(el
, ptr
->next
, ncnt
+ 1);
560 /* go to sibling if there is one */
562 (void) node_enum(el
, ptr
->sibling
, cnt
);
568 * Print the specified key and its associated
569 * function specified by val
572 key_kprint(EditLine
*el
, const char *key
, key_value_t
*val
, int ntype
)
575 char unparsbuf
[EL_BUFSIZ
];
576 static const char fmt
[] = "%-15s-> %s\n";
582 (void) key__decode_str(val
->str
, unparsbuf
,
584 ntype
== XK_STR
? "\"\"" : "[]");
585 (void) fprintf(el
->el_outfile
, fmt
, key
, unparsbuf
);
588 for (fp
= el
->el_map
.help
; fp
->name
; fp
++)
589 if (val
->cmd
== fp
->func
) {
590 (void) fprintf(el
->el_outfile
, fmt
,
595 if (fp
->name
== NULL
)
596 (void) fprintf(el
->el_outfile
,
597 "BUG! Command not found.\n");
602 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ntype
));
606 (void) fprintf(el
->el_outfile
, fmt
, key
, "no input");
615 /* key__decode_char():
616 * Put a printable form of char in buf.
619 key__decode_char(char *buf
, int cnt
, int off
, int ch
)
621 char *sb
= buf
+ off
;
622 char *eb
= buf
+ cnt
;
625 ch
= (unsigned char)ch
;
636 ADDC(toascii(ch
) | 0100);
637 } else if (ch
== '^') {
640 } else if (ch
== '\\') {
643 } else if (ch
== ' ' || (isprint(ch
) && !isspace(ch
))) {
647 ADDC((((unsigned int) ch
>> 6) & 7) + '0');
648 ADDC((((unsigned int) ch
>> 3) & 7) + '0');
649 ADDC((ch
& 7) + '0');
655 /* key__decode_str():
656 * Make a printable version of the ey
659 key__decode_str(const char *str
, char *buf
, int len
, const char *sep
)
661 char *b
= buf
, *eb
= b
+ len
;
665 if (sep
[0] != '\0') {
671 if (sep
[0] != '\0' && sep
[1] != '\0') {
676 for (p
= str
; *p
!= 0; p
++) {
677 if (iscntrl((unsigned char) *p
)) {
682 ADDC(toascii(*p
) | 0100);
684 } else if (*p
== '^' || *p
== '\\') {
687 } else if (*p
== ' ' || (isprint((unsigned char) *p
) &&
688 !isspace((unsigned char) *p
))) {
692 ADDC((((unsigned int) *p
>> 6) & 7) + '0');
693 ADDC((((unsigned int) *p
>> 3) & 7) + '0');
694 ADDC((*p
& 7) + '0');
697 if (sep
[0] != '\0' && sep
[1] != '\0') {