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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)key.c 8.1 (Berkeley) 6/4/93
37 * $DragonFly: src/lib/libedit/key.c,v 1.4 2003/11/12 20:21:29 eirikn Exp $
41 * key.c: This module contains the procedures for maintaining
42 * the extended-key map.
44 * An extended-key (key) is a sequence of keystrokes introduced
45 * with an sequence introducer and consisting of an arbitrary
46 * number of characters. This module maintains a map (the el->el_key.map)
47 * to convert these extended-key sequences into input strs
48 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
51 * If key is a substr of some other keys, then the longer
52 * keys are lost!! That is, if the keys "abcd" and "abcef"
53 * are in el->el_key.map, adding the key "abc" will cause the first two
54 * definitions to be lost.
58 * 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 (key_node_t *, char *,
84 private key_node_t *node__get (int);
85 private void node__put (key_node_t *);
86 private int node__delete (key_node_t **, char *);
87 private int node_lookup (EditLine *, char *, key_node_t *,
89 private int node_enum (EditLine *, key_node_t *, int);
90 private int key__decode_char (char *, int, int);
92 #define KEY_BUFSIZ EL_BUFSIZ
96 * Initialize the key maps
102 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
103 el->el_key.map = NULL;
116 el_free((ptr_t) el->el_key.buf);
117 el->el_key.buf = NULL;
118 /* XXX: provide a function to clear the keys */
119 el->el_key.map = NULL;
124 * Associate cmd with a key value
126 protected key_value_t *
131 el->el_key.val.cmd = (el_action_t) cmd;
132 return &el->el_key.val;
137 * Associate str with a key value
139 protected key_value_t *
144 el->el_key.val.str = str;
145 return &el->el_key.val;
150 * Takes all nodes on el->el_key.map and puts them on free list. Then
151 * initializes el->el_key.map with arrow keys
152 * [Always bind the ansi arrow keys?]
158 node__put(el->el_key.map);
159 el->el_key.map = NULL;
165 * Calls the recursive function with entry point el->el_key.map
166 * Looks up *ch in map and then reads characters until a
167 * complete match is found or a mismatch occurs. Returns the
168 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
169 * Returns NULL in val.str and XK_STR for no match.
170 * The last character read is returned in *ch.
178 return node_trav(el, el->el_key.map, ch, val);
184 * Adds key to the el->el_key.map and associates the value in val with it.
185 * If key is already is in el->el_key.map, the new code is applied to the
186 * existing key. Ntype specifies if code is a command, an
187 * out str or a unix command.
190 key_add(el, key, val, ntype)
196 if (key[0] == '\0') {
197 (void) fprintf(el->el_errfile,
198 "key_add: Null extended-key not allowed.\n");
202 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
203 (void) fprintf(el->el_errfile,
204 "key_add: sequence-lead-in command not allowed\n");
208 if (el->el_key.map == NULL)
209 /* tree is initially empty. Set up new node to match key[0] */
210 el->el_key.map = node__get(key[0]); /* it is properly initialized */
212 /* Now recurse through el->el_key.map */
213 (void) node__try(el->el_key.map, key, val, ntype);
222 key_clear(el, map, in)
227 if ((map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN) &&
228 ((map == el->el_map.key &&
229 el->el_map.alt[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN) ||
230 (map == el->el_map.alt &&
231 el->el_map.key[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN)))
232 (void) key_delete(el, in);
237 * Delete the key and all longer keys staring with key, if
245 if (key[0] == '\0') {
246 (void) fprintf(el->el_errfile,
247 "key_delete: Null extended-key not allowed.\n");
251 if (el->el_key.map == NULL)
254 (void) node__delete(&el->el_key.map, key);
260 * Print the binding associated with key key.
261 * Print entire el->el_key.map if null
268 /* do nothing if el->el_key.map is empty and null key specified */
269 if (el->el_key.map == NULL && *key == 0)
272 el->el_key.buf[0] = '"';
273 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
274 /* key is not bound */
275 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n", key);
281 * recursively traverses node in tree until match or mismatch is
282 * found. May read in more characters.
285 node_trav(el, ptr, ch, val)
291 if (ptr->ch == *ch) {
294 /* key not complete so get next char */
295 if (el_getc(el, ch) != 1) { /* if EOF or error */
296 val->cmd = ED_END_OF_FILE;
297 return XK_CMD;/* PWP: Pretend we just read an end-of-file */
299 return node_trav(el, ptr->next, ch, val);
303 if (ptr->type != XK_CMD)
309 /* no match found here */
311 /* try next sibling */
312 return node_trav(el, ptr->sibling, ch, val);
315 /* no next sibling -- mismatch */
324 * Find a node that matches *str or allocate a new one
327 node__try(ptr, str, val, ntype)
333 if (ptr->ch != *str) {
336 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
337 if (xm->sibling->ch == *str)
339 if (xm->sibling == NULL)
340 xm->sibling = node__get(*str); /* setup new node */
344 if (*++str == '\0') {
346 if (ptr->next != NULL) {
347 node__put(ptr->next); /* lose longer keys with this prefix */
357 el_free((ptr_t) ptr->val.str);
364 switch (ptr->type = ntype) {
370 ptr->val.str = strdup(val->str);
378 /* still more chars to go */
379 if (ptr->next == NULL)
380 ptr->next = node__get(*str); /* setup new node */
381 (void) node__try(ptr->next, str, val, ntype);
388 * Delete node that matches str
391 node__delete(inptr, str)
396 key_node_t *prev_ptr = NULL;
400 if (ptr->ch != *str) {
403 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
404 if (xm->sibling->ch == *str)
406 if (xm->sibling == NULL)
412 if (*++str == '\0') {
414 if (prev_ptr == NULL)
415 *inptr = ptr->sibling;
417 prev_ptr->sibling = ptr->sibling;
422 else if (ptr->next != NULL && node__delete(&ptr->next, str) == 1) {
423 if (ptr->next != NULL)
425 if (prev_ptr == NULL)
426 *inptr = ptr->sibling;
428 prev_ptr->sibling = ptr->sibling;
439 * Puts a tree of nodes onto free list using free(3).
448 if (ptr->next != NULL) {
449 node__put(ptr->next);
453 node__put(ptr->sibling);
461 if (ptr->val.str != NULL)
462 el_free((ptr_t) ptr->val.str);
468 el_free((ptr_t) ptr);
473 * Returns pointer to an key_node_t for ch.
481 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
493 * look for the str starting at node ptr.
497 node_lookup(el, str, ptr, cnt)
506 return -1; /* cannot have null ptr */
509 /* no more chars in str. node_enum from here. */
510 (void) node_enum(el, ptr, cnt);
514 /* If match put this char into el->el_key.buf. Recurse */
515 if (ptr->ch == *str) {
517 ncnt = key__decode_char(el->el_key.buf, cnt,
518 (unsigned char) ptr->ch);
519 if (ptr->next != NULL)
520 /* not yet at leaf */
521 return node_lookup(el, str + 1, ptr->next, ncnt + 1);
523 /* next node is null so key should be complete */
525 el->el_key.buf[ncnt + 1] = '"';
526 el->el_key.buf[ncnt + 2] = '\0';
527 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
531 return -1;/* mismatch -- str still has chars */
535 /* no match found try sibling */
537 return node_lookup(el, str, ptr->sibling, cnt);
546 * Traverse the node printing the characters it is bound in buffer
549 node_enum(el, ptr, cnt)
556 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
557 el->el_key.buf[++cnt] = '"';
558 el->el_key.buf[++cnt] = '\0';
559 (void) fprintf(el->el_errfile,
560 "Some extended keys too long for internal print buffer");
561 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
567 (void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");
572 /* put this char at end of str */
573 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
574 if (ptr->next == NULL) {
575 /* print this key and function */
576 el->el_key.buf[ncnt + 1] = '"';
577 el->el_key.buf[ncnt + 2] = '\0';
578 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
581 (void) node_enum(el, ptr->next, ncnt + 1);
583 /* go to sibling if there is one */
585 (void) node_enum(el, ptr->sibling, cnt);
591 * Print the specified key and its associated
592 * function specified by val
595 key_kprint(el, key, val, ntype)
602 char unparsbuf[EL_BUFSIZ];
603 static char *fmt = "%-15s-> %s\n";
609 (void) fprintf(el->el_errfile, fmt, key,
610 key__decode_str(val->str, unparsbuf,
611 ntype == XK_STR ? "\"\"" : "[]"));
614 for (fp = el->el_map.help; fp->name; fp++)
615 if (val->cmd == fp->func) {
616 (void) fprintf(el->el_errfile, fmt, key, fp->name);
620 if (fp->name == NULL)
621 (void) fprintf(el->el_errfile, "BUG! Command not found.\n");
630 (void) fprintf(el->el_errfile, fmt, key, "no input");
634 /* key__decode_char():
635 * Put a printable form of char in buf.
638 key__decode_char(buf, cnt, ch)
642 ch = (unsigned char)ch;
655 buf[cnt] = toascii(ch) | 0100;
657 else if (ch == '^') {
661 else if (ch == '\\') {
665 else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
670 buf[cnt++] = ((ch >> 6) & 7) + '0';
671 buf[cnt++] = ((ch >> 3) & 7) + '0';
672 buf[cnt] = (ch & 7) + '0';
677 /* key__decode_str():
678 * Make a printable version of the ey
681 key__decode_str(str, buf, sep)
694 if (sep[0] != '\0' && sep[1] != '\0')
700 for (p = str; *p != 0; p++) {
701 if (iscntrl((unsigned char) *p)) {
706 *b++ = toascii(*p) | 0100;
708 else if (*p == '^' || *p == '\\') {
712 else if (*p == ' ' || (isprint((unsigned char) *p) &&
713 !isspace((unsigned char) *p))) {
718 *b++ = ((*p >> 6) & 7) + '0';
719 *b++ = ((*p >> 3) & 7) + '0';
720 *b++ = (*p & 7) + '0';
723 if (sep[0] != '\0' && sep[1] != '\0')
726 return buf; /* should check for overflow */