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 static const char rcsid[] =
41 #endif /* not lint && not SCCSID */
44 * key.c: This module contains the procedures for maintaining
45 * the extended-key map.
47 * An extended-key (key) is a sequence of keystrokes introduced
48 * with an sequence introducer and consisting of an arbitrary
49 * number of characters. This module maintains a map (the el->el_key.map)
50 * to convert these extended-key sequences into input strs
51 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
54 * If key is a substr of some other keys, then the longer
55 * keys are lost!! That is, if the keys "abcd" and "abcef"
56 * are in el->el_key.map, adding the key "abc" will cause the first two
57 * definitions to be lost.
61 * 1) It is not possible to have one key that is a
71 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
72 * of these node elements
75 char ch; /* single character of key */
76 int type; /* node type */
77 key_value_t val; /* command code or pointer to str, */
78 /* if this is a leaf */
79 struct key_node_t *next; /* ptr to next char of this key */
80 struct key_node_t *sibling; /* ptr to another key with same prefix */
83 private int node_trav __P((EditLine *, key_node_t *, char *,
85 private int node__try __P((key_node_t *, char *,
87 private key_node_t *node__get __P((int));
88 private void node__put __P((key_node_t *));
89 private int node__delete __P((key_node_t **, char *));
90 private int node_lookup __P((EditLine *, char *, key_node_t *,
92 private int node_enum __P((EditLine *, key_node_t *, int));
93 private int key__decode_char __P((char *, int, int));
95 #define KEY_BUFSIZ EL_BUFSIZ
99 * Initialize the key maps
105 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
106 el->el_key.map = NULL;
119 el_free((ptr_t) el->el_key.buf);
120 el->el_key.buf = NULL;
121 /* XXX: provide a function to clear the keys */
122 el->el_key.map = NULL;
127 * Associate cmd with a key value
129 protected key_value_t *
134 el->el_key.val.cmd = (el_action_t) cmd;
135 return &el->el_key.val;
140 * Associate str with a key value
142 protected key_value_t *
147 el->el_key.val.str = str;
148 return &el->el_key.val;
153 * Takes all nodes on el->el_key.map and puts them on free list. Then
154 * initializes el->el_key.map with arrow keys
155 * [Always bind the ansi arrow keys?]
161 node__put(el->el_key.map);
162 el->el_key.map = NULL;
168 * Calls the recursive function with entry point el->el_key.map
169 * Looks up *ch in map and then reads characters until a
170 * complete match is found or a mismatch occurs. Returns the
171 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
172 * Returns NULL in val.str and XK_STR for no match.
173 * The last character read is returned in *ch.
181 return node_trav(el, el->el_key.map, ch, val);
187 * Adds key to the el->el_key.map and associates the value in val with it.
188 * If key is already is in el->el_key.map, the new code is applied to the
189 * existing key. Ntype specifies if code is a command, an
190 * out str or a unix command.
193 key_add(el, key, val, ntype)
199 if (key[0] == '\0') {
200 (void) fprintf(el->el_errfile,
201 "key_add: Null extended-key not allowed.\n");
205 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
206 (void) fprintf(el->el_errfile,
207 "key_add: sequence-lead-in command not allowed\n");
211 if (el->el_key.map == NULL)
212 /* tree is initially empty. Set up new node to match key[0] */
213 el->el_key.map = node__get(key[0]); /* it is properly initialized */
215 /* Now recurse through el->el_key.map */
216 (void) node__try(el->el_key.map, key, val, ntype);
225 key_clear(el, map, in)
230 if ((map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN) &&
231 ((map == el->el_map.key &&
232 el->el_map.alt[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN) ||
233 (map == el->el_map.alt &&
234 el->el_map.key[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN)))
235 (void) key_delete(el, in);
240 * Delete the key and all longer keys staring with key, if
248 if (key[0] == '\0') {
249 (void) fprintf(el->el_errfile,
250 "key_delete: Null extended-key not allowed.\n");
254 if (el->el_key.map == NULL)
257 (void) node__delete(&el->el_key.map, key);
263 * Print the binding associated with key key.
264 * Print entire el->el_key.map if null
271 /* do nothing if el->el_key.map is empty and null key specified */
272 if (el->el_key.map == NULL && *key == 0)
275 el->el_key.buf[0] = '"';
276 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
277 /* key is not bound */
278 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n", key);
284 * recursively traverses node in tree until match or mismatch is
285 * found. May read in more characters.
288 node_trav(el, ptr, ch, val)
294 if (ptr->ch == *ch) {
297 /* key not complete so get next char */
298 if (el_getc(el, ch) != 1) { /* if EOF or error */
299 val->cmd = ED_END_OF_FILE;
300 return XK_CMD;/* PWP: Pretend we just read an end-of-file */
302 return node_trav(el, ptr->next, ch, val);
306 if (ptr->type != XK_CMD)
312 /* no match found here */
314 /* try next sibling */
315 return node_trav(el, ptr->sibling, ch, val);
318 /* no next sibling -- mismatch */
327 * Find a node that matches *str or allocate a new one
330 node__try(ptr, str, val, ntype)
336 if (ptr->ch != *str) {
339 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
340 if (xm->sibling->ch == *str)
342 if (xm->sibling == NULL)
343 xm->sibling = node__get(*str); /* setup new node */
347 if (*++str == '\0') {
349 if (ptr->next != NULL) {
350 node__put(ptr->next); /* lose longer keys with this prefix */
360 el_free((ptr_t) ptr->val.str);
367 switch (ptr->type = ntype) {
373 ptr->val.str = strdup(val->str);
381 /* still more chars to go */
382 if (ptr->next == NULL)
383 ptr->next = node__get(*str); /* setup new node */
384 (void) node__try(ptr->next, str, val, ntype);
391 * Delete node that matches str
394 node__delete(inptr, str)
399 key_node_t *prev_ptr = NULL;
403 if (ptr->ch != *str) {
406 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
407 if (xm->sibling->ch == *str)
409 if (xm->sibling == NULL)
415 if (*++str == '\0') {
417 if (prev_ptr == NULL)
418 *inptr = ptr->sibling;
420 prev_ptr->sibling = ptr->sibling;
425 else if (ptr->next != NULL && node__delete(&ptr->next, str) == 1) {
426 if (ptr->next != NULL)
428 if (prev_ptr == NULL)
429 *inptr = ptr->sibling;
431 prev_ptr->sibling = ptr->sibling;
442 * Puts a tree of nodes onto free list using free(3).
451 if (ptr->next != NULL) {
452 node__put(ptr->next);
456 node__put(ptr->sibling);
464 if (ptr->val.str != NULL)
465 el_free((ptr_t) ptr->val.str);
471 el_free((ptr_t) ptr);
476 * Returns pointer to an key_node_t for ch.
484 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
496 * look for the str starting at node ptr.
500 node_lookup(el, str, ptr, cnt)
509 return -1; /* cannot have null ptr */
512 /* no more chars in str. node_enum from here. */
513 (void) node_enum(el, ptr, cnt);
517 /* If match put this char into el->el_key.buf. Recurse */
518 if (ptr->ch == *str) {
520 ncnt = key__decode_char(el->el_key.buf, cnt,
521 (unsigned char) ptr->ch);
522 if (ptr->next != NULL)
523 /* not yet at leaf */
524 return node_lookup(el, str + 1, ptr->next, ncnt + 1);
526 /* next node is null so key should be complete */
528 el->el_key.buf[ncnt + 1] = '"';
529 el->el_key.buf[ncnt + 2] = '\0';
530 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
534 return -1;/* mismatch -- str still has chars */
538 /* no match found try sibling */
540 return node_lookup(el, str, ptr->sibling, cnt);
549 * Traverse the node printing the characters it is bound in buffer
552 node_enum(el, ptr, cnt)
559 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
560 el->el_key.buf[++cnt] = '"';
561 el->el_key.buf[++cnt] = '\0';
562 (void) fprintf(el->el_errfile,
563 "Some extended keys too long for internal print buffer");
564 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
570 (void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");
575 /* put this char at end of str */
576 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
577 if (ptr->next == NULL) {
578 /* print this key and function */
579 el->el_key.buf[ncnt + 1] = '"';
580 el->el_key.buf[ncnt + 2] = '\0';
581 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
584 (void) node_enum(el, ptr->next, ncnt + 1);
586 /* go to sibling if there is one */
588 (void) node_enum(el, ptr->sibling, cnt);
594 * Print the specified key and its associated
595 * function specified by val
598 key_kprint(el, key, val, ntype)
605 char unparsbuf[EL_BUFSIZ];
606 static const char fmt[] = "%-15s-> %s\n";
612 (void) fprintf(el->el_errfile, fmt, key,
613 key__decode_str(val->str, unparsbuf,
614 ntype == XK_STR ? "\"\"" : "[]"));
617 for (fp = el->el_map.help; fp->name; fp++)
618 if (val->cmd == fp->func) {
619 (void) fprintf(el->el_errfile, fmt, key, fp->name);
623 if (fp->name == NULL)
624 (void) fprintf(el->el_errfile, "BUG! Command not found.\n");
633 (void) fprintf(el->el_errfile, fmt, key, "no input");
637 /* key__decode_char():
638 * Put a printable form of char in buf.
641 key__decode_char(buf, cnt, ch)
645 ch = (unsigned char)ch;
658 buf[cnt] = toascii(ch) | 0100;
660 else if (ch == '^') {
664 else if (ch == '\\') {
668 else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
673 buf[cnt++] = ((ch >> 6) & 7) + '0';
674 buf[cnt++] = ((ch >> 3) & 7) + '0';
675 buf[cnt] = (ch & 7) + '0';
680 /* key__decode_str():
681 * Make a printable version of the ey
684 key__decode_str(str, buf, sep)
697 if (sep[0] != '\0' && sep[1] != '\0')
703 for (p = str; *p != 0; p++) {
704 if (iscntrl((unsigned char) *p)) {
709 *b++ = toascii(*p) | 0100;
711 else if (*p == '^' || *p == '\\') {
715 else if (*p == ' ' || (isprint((unsigned char) *p) &&
716 !isspace((unsigned char) *p))) {
721 *b++ = ((*p >> 6) & 7) + '0';
722 *b++ = ((*p >> 3) & 7) + '0';
723 *b++ = (*p & 7) + '0';
726 if (sep[0] != '\0' && sep[1] != '\0')
729 return buf; /* should check for overflow */