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
37 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
39 #endif /* not lint && not SCCSID */
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 an 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
69 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
70 * of these node elements
73 char ch; /* single character of key */
74 int type; /* node type */
75 key_value_t val; /* command code or pointer to str, */
76 /* if this is a leaf */
77 struct key_node_t *next; /* ptr to next char of this key */
78 struct key_node_t *sibling; /* ptr to another key with same prefix */
81 private int node_trav __P((EditLine *, key_node_t *, char *,
83 private int node__try __P((key_node_t *, char *,
85 private key_node_t *node__get __P((int));
86 private void node__put __P((key_node_t *));
87 private int node__delete __P((key_node_t **, char *));
88 private int node_lookup __P((EditLine *, char *, key_node_t *,
90 private int node_enum __P((EditLine *, key_node_t *, int));
91 private int key__decode_char __P((char *, int, int));
93 #define KEY_BUFSIZ EL_BUFSIZ
97 * Initialize the key maps
103 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
104 el->el_key.map = NULL;
117 el_free((ptr_t) el->el_key.buf);
118 el->el_key.buf = NULL;
119 /* XXX: provide a function to clear the keys */
120 el->el_key.map = NULL;
125 * Associate cmd with a key value
127 protected key_value_t *
132 el->el_key.val.cmd = (el_action_t) cmd;
133 return &el->el_key.val;
138 * Associate str with a key value
140 protected key_value_t *
145 el->el_key.val.str = str;
146 return &el->el_key.val;
151 * Takes all nodes on el->el_key.map and puts them on free list. Then
152 * initializes el->el_key.map with arrow keys
153 * [Always bind the ansi arrow keys?]
159 node__put(el->el_key.map);
160 el->el_key.map = NULL;
166 * Calls the recursive function with entry point el->el_key.map
167 * Looks up *ch in map and then reads characters until a
168 * complete match is found or a mismatch occurs. Returns the
169 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
170 * Returns NULL in val.str and XK_STR for no match.
171 * The last character read is returned in *ch.
179 return node_trav(el, el->el_key.map, ch, val);
185 * Adds key to the el->el_key.map and associates the value in val with it.
186 * If key is already is in el->el_key.map, the new code is applied to the
187 * existing key. Ntype specifies if code is a command, an
188 * out str or a unix command.
191 key_add(el, key, val, ntype)
197 if (key[0] == '\0') {
198 (void) fprintf(el->el_errfile,
199 "key_add: Null extended-key not allowed.\n");
203 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
204 (void) fprintf(el->el_errfile,
205 "key_add: sequence-lead-in command not allowed\n");
209 if (el->el_key.map == NULL)
210 /* tree is initially empty. Set up new node to match key[0] */
211 el->el_key.map = node__get(key[0]); /* it is properly initialized */
213 /* Now recurse through el->el_key.map */
214 (void) node__try(el->el_key.map, key, val, ntype);
223 key_clear(el, map, in)
228 if ((map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN) &&
229 ((map == el->el_map.key &&
230 el->el_map.alt[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN) ||
231 (map == el->el_map.alt &&
232 el->el_map.key[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN)))
233 (void) key_delete(el, in);
238 * Delete the key and all longer keys staring with key, if
246 if (key[0] == '\0') {
247 (void) fprintf(el->el_errfile,
248 "key_delete: Null extended-key not allowed.\n");
252 if (el->el_key.map == NULL)
255 (void) node__delete(&el->el_key.map, key);
261 * Print the binding associated with key key.
262 * Print entire el->el_key.map if null
269 /* do nothing if el->el_key.map is empty and null key specified */
270 if (el->el_key.map == NULL && *key == 0)
273 el->el_key.buf[0] = '"';
274 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
275 /* key is not bound */
276 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n", key);
282 * recursively traverses node in tree until match or mismatch is
283 * found. May read in more characters.
286 node_trav(el, ptr, ch, val)
292 if (ptr->ch == *ch) {
295 /* key not complete so get next char */
296 if (el_getc(el, ch) != 1) { /* if EOF or error */
297 val->cmd = ED_END_OF_FILE;
298 return XK_CMD;/* PWP: Pretend we just read an end-of-file */
300 return node_trav(el, ptr->next, ch, val);
304 if (ptr->type != XK_CMD)
310 /* no match found here */
312 /* try next sibling */
313 return node_trav(el, ptr->sibling, ch, val);
316 /* no next sibling -- mismatch */
325 * Find a node that matches *str or allocate a new one
328 node__try(ptr, str, val, ntype)
334 if (ptr->ch != *str) {
337 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
338 if (xm->sibling->ch == *str)
340 if (xm->sibling == NULL)
341 xm->sibling = node__get(*str); /* setup new node */
345 if (*++str == '\0') {
347 if (ptr->next != NULL) {
348 node__put(ptr->next); /* lose longer keys with this prefix */
358 el_free((ptr_t) ptr->val.str);
365 switch (ptr->type = ntype) {
371 ptr->val.str = strdup(val->str);
379 /* still more chars to go */
380 if (ptr->next == NULL)
381 ptr->next = node__get(*str); /* setup new node */
382 (void) node__try(ptr->next, str, val, ntype);
389 * Delete node that matches str
392 node__delete(inptr, str)
397 key_node_t *prev_ptr = NULL;
401 if (ptr->ch != *str) {
404 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
405 if (xm->sibling->ch == *str)
407 if (xm->sibling == NULL)
413 if (*++str == '\0') {
415 if (prev_ptr == NULL)
416 *inptr = ptr->sibling;
418 prev_ptr->sibling = ptr->sibling;
423 else if (ptr->next != NULL && node__delete(&ptr->next, str) == 1) {
424 if (ptr->next != NULL)
426 if (prev_ptr == NULL)
427 *inptr = ptr->sibling;
429 prev_ptr->sibling = ptr->sibling;
440 * Puts a tree of nodes onto free list using free(3).
449 if (ptr->next != NULL) {
450 node__put(ptr->next);
454 node__put(ptr->sibling);
462 if (ptr->val.str != NULL)
463 el_free((ptr_t) ptr->val.str);
469 el_free((ptr_t) ptr);
474 * Returns pointer to an key_node_t for ch.
482 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
494 * look for the str starting at node ptr.
498 node_lookup(el, str, ptr, cnt)
507 return -1; /* cannot have null ptr */
510 /* no more chars in str. node_enum from here. */
511 (void) node_enum(el, ptr, cnt);
515 /* If match put this char into el->el_key.buf. Recurse */
516 if (ptr->ch == *str) {
518 ncnt = key__decode_char(el->el_key.buf, cnt,
519 (unsigned char) ptr->ch);
520 if (ptr->next != NULL)
521 /* not yet at leaf */
522 return node_lookup(el, str + 1, ptr->next, ncnt + 1);
524 /* next node is null so key should be complete */
526 el->el_key.buf[ncnt + 1] = '"';
527 el->el_key.buf[ncnt + 2] = '\0';
528 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
532 return -1;/* mismatch -- str still has chars */
536 /* no match found try sibling */
538 return node_lookup(el, str, ptr->sibling, cnt);
547 * Traverse the node printing the characters it is bound in buffer
550 node_enum(el, ptr, cnt)
557 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
558 el->el_key.buf[++cnt] = '"';
559 el->el_key.buf[++cnt] = '\0';
560 (void) fprintf(el->el_errfile,
561 "Some extended keys too long for internal print buffer");
562 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
568 (void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");
573 /* put this char at end of str */
574 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
575 if (ptr->next == NULL) {
576 /* print this key and function */
577 el->el_key.buf[ncnt + 1] = '"';
578 el->el_key.buf[ncnt + 2] = '\0';
579 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
582 (void) node_enum(el, ptr->next, ncnt + 1);
584 /* go to sibling if there is one */
586 (void) node_enum(el, ptr->sibling, cnt);
592 * Print the specified key and its associated
593 * function specified by val
596 key_kprint(el, key, val, ntype)
603 char unparsbuf[EL_BUFSIZ];
604 static char *fmt = "%-15s-> %s\n";
610 (void) fprintf(el->el_errfile, fmt, key,
611 key__decode_str(val->str, unparsbuf,
612 ntype == XK_STR ? "\"\"" : "[]"));
615 for (fp = el->el_map.help; fp->name; fp++)
616 if (val->cmd == fp->func) {
617 (void) fprintf(el->el_errfile, fmt, key, fp->name);
621 if (fp->name == NULL)
622 (void) fprintf(el->el_errfile, "BUG! Command not found.\n");
631 (void) fprintf(el->el_errfile, fmt, key, "no input");
635 /* key__decode_char():
636 * Put a printable form of char in buf.
639 key__decode_char(buf, cnt, ch)
643 ch = (unsigned char)ch;
656 buf[cnt] = toascii(ch) | 0100;
658 else if (ch == '^') {
662 else if (ch == '\\') {
666 else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
671 buf[cnt++] = ((ch >> 6) & 7) + '0';
672 buf[cnt++] = ((ch >> 3) & 7) + '0';
673 buf[cnt] = (ch & 7) + '0';
678 /* key__decode_str():
679 * Make a printable version of the ey
682 key__decode_str(str, buf, sep)
695 if (sep[0] != '\0' && sep[1] != '\0')
701 for (p = str; *p != 0; p++) {
702 if (iscntrl((unsigned char) *p)) {
707 *b++ = toascii(*p) | 0100;
709 else if (*p == '^' || *p == '\\') {
713 else if (*p == ' ' || (isprint((unsigned char) *p) &&
714 !isspace((unsigned char) *p))) {
719 *b++ = ((*p >> 6) & 7) + '0';
720 *b++ = ((*p >> 3) & 7) + '0';
721 *b++ = (*p & 7) + '0';
724 if (sep[0] != '\0' && sep[1] != '\0')
727 return buf; /* should check for overflow */