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
40 * key.c: This module contains the procedures for maintaining
41 * the extended-key map.
43 * An extended-key (key) is a sequence of keystrokes introduced
44 * with an sequence introducer and consisting of an arbitrary
45 * number of characters. This module maintains a map (the el->el_key.map)
46 * to convert these extended-key sequences into input strs
47 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
50 * If key is a substr of some other keys, then the longer
51 * keys are lost!! That is, if the keys "abcd" and "abcef"
52 * are in el->el_key.map, adding the key "abc" will cause the first two
53 * definitions to be lost.
57 * 1) It is not possible to have one key that is a
67 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
68 * of these node elements
71 char ch; /* single character of key */
72 int type; /* node type */
73 key_value_t val; /* command code or pointer to str, */
74 /* if this is a leaf */
75 struct key_node_t *next; /* ptr to next char of this key */
76 struct key_node_t *sibling; /* ptr to another key with same prefix */
79 private int node_trav __P((EditLine *, key_node_t *, char *,
81 private int node__try __P((key_node_t *, char *,
83 private key_node_t *node__get __P((int));
84 private void node__put __P((key_node_t *));
85 private int node__delete __P((key_node_t **, char *));
86 private int node_lookup __P((EditLine *, char *, key_node_t *,
88 private int node_enum __P((EditLine *, key_node_t *, int));
89 private int key__decode_char __P((char *, int, int));
91 #define KEY_BUFSIZ EL_BUFSIZ
95 * Initialize the key maps
101 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
102 el->el_key.map = NULL;
115 el_free((ptr_t) el->el_key.buf);
116 el->el_key.buf = NULL;
117 /* XXX: provide a function to clear the keys */
118 el->el_key.map = NULL;
123 * Associate cmd with a key value
125 protected key_value_t *
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 *
143 el->el_key.val.str = str;
144 return &el->el_key.val;
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?]
157 node__put(el->el_key.map);
158 el->el_key.map = NULL;
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.
177 return node_trav(el, el->el_key.map, ch, val);
183 * Adds key to the el->el_key.map and associates the value in val with it.
184 * If key is already is in el->el_key.map, the new code is applied to the
185 * existing key. Ntype specifies if code is a command, an
186 * out str or a unix command.
189 key_add(el, key, val, ntype)
195 if (key[0] == '\0') {
196 (void) fprintf(el->el_errfile,
197 "key_add: Null extended-key not allowed.\n");
201 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
202 (void) fprintf(el->el_errfile,
203 "key_add: sequence-lead-in command not allowed\n");
207 if (el->el_key.map == NULL)
208 /* tree is initially empty. Set up new node to match key[0] */
209 el->el_key.map = node__get(key[0]); /* it is properly initialized */
211 /* Now recurse through el->el_key.map */
212 (void) node__try(el->el_key.map, key, val, ntype);
221 key_clear(el, map, in)
226 if ((map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN) &&
227 ((map == el->el_map.key &&
228 el->el_map.alt[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN) ||
229 (map == el->el_map.alt &&
230 el->el_map.key[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN)))
231 (void) key_delete(el, in);
236 * Delete the key and all longer keys staring with key, if
244 if (key[0] == '\0') {
245 (void) fprintf(el->el_errfile,
246 "key_delete: Null extended-key not allowed.\n");
250 if (el->el_key.map == NULL)
253 (void) node__delete(&el->el_key.map, key);
259 * Print the binding associated with key key.
260 * Print entire el->el_key.map if null
267 /* do nothing if el->el_key.map is empty and null key specified */
268 if (el->el_key.map == NULL && *key == 0)
271 el->el_key.buf[0] = '"';
272 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
273 /* key is not bound */
274 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n", key);
280 * recursively traverses node in tree until match or mismatch is
281 * found. May read in more characters.
284 node_trav(el, ptr, ch, val)
290 if (ptr->ch == *ch) {
293 /* key not complete so get next char */
294 if (el_getc(el, ch) != 1) { /* if EOF or error */
295 val->cmd = ED_END_OF_FILE;
296 return XK_CMD;/* PWP: Pretend we just read an end-of-file */
298 return node_trav(el, ptr->next, ch, val);
302 if (ptr->type != XK_CMD)
308 /* no match found here */
310 /* try next sibling */
311 return node_trav(el, ptr->sibling, ch, val);
314 /* no next sibling -- mismatch */
323 * Find a node that matches *str or allocate a new one
326 node__try(ptr, str, val, ntype)
332 if (ptr->ch != *str) {
335 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
336 if (xm->sibling->ch == *str)
338 if (xm->sibling == NULL)
339 xm->sibling = node__get(*str); /* setup new node */
343 if (*++str == '\0') {
345 if (ptr->next != NULL) {
346 node__put(ptr->next); /* lose longer keys with this prefix */
356 el_free((ptr_t) ptr->val.str);
363 switch (ptr->type = ntype) {
369 ptr->val.str = strdup(val->str);
377 /* still more chars to go */
378 if (ptr->next == NULL)
379 ptr->next = node__get(*str); /* setup new node */
380 (void) node__try(ptr->next, str, val, ntype);
387 * Delete node that matches str
390 node__delete(inptr, str)
395 key_node_t *prev_ptr = NULL;
399 if (ptr->ch != *str) {
402 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
403 if (xm->sibling->ch == *str)
405 if (xm->sibling == NULL)
411 if (*++str == '\0') {
413 if (prev_ptr == NULL)
414 *inptr = ptr->sibling;
416 prev_ptr->sibling = ptr->sibling;
421 else if (ptr->next != NULL && node__delete(&ptr->next, str) == 1) {
422 if (ptr->next != NULL)
424 if (prev_ptr == NULL)
425 *inptr = ptr->sibling;
427 prev_ptr->sibling = ptr->sibling;
438 * Puts a tree of nodes onto free list using free(3).
447 if (ptr->next != NULL) {
448 node__put(ptr->next);
452 node__put(ptr->sibling);
460 if (ptr->val.str != NULL)
461 el_free((ptr_t) ptr->val.str);
467 el_free((ptr_t) ptr);
472 * Returns pointer to an key_node_t for ch.
480 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
492 * look for the str starting at node ptr.
496 node_lookup(el, str, ptr, cnt)
505 return -1; /* cannot have null ptr */
508 /* no more chars in str. node_enum from here. */
509 (void) node_enum(el, ptr, cnt);
513 /* If match put this char into el->el_key.buf. Recurse */
514 if (ptr->ch == *str) {
516 ncnt = key__decode_char(el->el_key.buf, cnt,
517 (unsigned char) ptr->ch);
518 if (ptr->next != NULL)
519 /* not yet at leaf */
520 return node_lookup(el, str + 1, ptr->next, ncnt + 1);
522 /* next node is null so key should be complete */
524 el->el_key.buf[ncnt + 1] = '"';
525 el->el_key.buf[ncnt + 2] = '\0';
526 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
530 return -1;/* mismatch -- str still has chars */
534 /* no match found try sibling */
536 return node_lookup(el, str, ptr->sibling, cnt);
545 * Traverse the node printing the characters it is bound in buffer
548 node_enum(el, ptr, cnt)
555 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
556 el->el_key.buf[++cnt] = '"';
557 el->el_key.buf[++cnt] = '\0';
558 (void) fprintf(el->el_errfile,
559 "Some extended keys too long for internal print buffer");
560 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
566 (void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");
571 /* put this char at end of str */
572 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
573 if (ptr->next == NULL) {
574 /* print this key and function */
575 el->el_key.buf[ncnt + 1] = '"';
576 el->el_key.buf[ncnt + 2] = '\0';
577 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
580 (void) node_enum(el, ptr->next, ncnt + 1);
582 /* go to sibling if there is one */
584 (void) node_enum(el, ptr->sibling, cnt);
590 * Print the specified key and its associated
591 * function specified by val
594 key_kprint(el, key, val, ntype)
601 char unparsbuf[EL_BUFSIZ];
602 static char *fmt = "%-15s-> %s\n";
608 (void) fprintf(el->el_errfile, fmt, key,
609 key__decode_str(val->str, unparsbuf,
610 ntype == XK_STR ? "\"\"" : "[]"));
613 for (fp = el->el_map.help; fp->name; fp++)
614 if (val->cmd == fp->func) {
615 (void) fprintf(el->el_errfile, fmt, key, fp->name);
619 if (fp->name == NULL)
620 (void) fprintf(el->el_errfile, "BUG! Command not found.\n");
629 (void) fprintf(el->el_errfile, fmt, key, "no input");
633 /* key__decode_char():
634 * Put a printable form of char in buf.
637 key__decode_char(buf, cnt, ch)
641 ch = (unsigned char)ch;
654 buf[cnt] = toascii(ch) | 0100;
656 else if (ch == '^') {
660 else if (ch == '\\') {
664 else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
669 buf[cnt++] = ((ch >> 6) & 7) + '0';
670 buf[cnt++] = ((ch >> 3) & 7) + '0';
671 buf[cnt] = (ch & 7) + '0';
676 /* key__decode_str():
677 * Make a printable version of the ey
680 key__decode_str(str, buf, sep)
693 if (sep[0] != '\0' && sep[1] != '\0')
699 for (p = str; *p != 0; p++) {
700 if (iscntrl((unsigned char) *p)) {
705 *b++ = toascii(*p) | 0100;
707 else if (*p == '^' || *p == '\\') {
711 else if (*p == ' ' || (isprint((unsigned char) *p) &&
712 !isspace((unsigned char) *p))) {
717 *b++ = ((*p >> 6) & 7) + '0';
718 *b++ = ((*p >> 3) & 7) + '0';
719 *b++ = (*p & 7) + '0';
722 if (sep[0] != '\0' && sep[1] != '\0')
725 return buf; /* should check for overflow */