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 * @(#)key.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: key.c,v 1.16 2005/07/06 21:13:02 christos Exp $
34 * $DragonFly: src/lib/libedit/key.c,v 1.5 2005/11/13 11:58:30 corecode Exp $
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
66 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
67 * of these node elements
70 char ch; /* single character of key */
71 int type; /* node type */
72 key_value_t val; /* command code or pointer to str, */
73 /* if this is a leaf */
74 struct key_node_t *next; /* ptr to next char of this key */
75 struct key_node_t *sibling; /* ptr to another key with same prefix*/
78 private int node_trav(EditLine *, key_node_t *, char *,
80 private int node__try(EditLine *, key_node_t *, const char *,
82 private key_node_t *node__get(int);
83 private void node__free(key_node_t *);
84 private void node__put(EditLine *, key_node_t *);
85 private int node__delete(EditLine *, key_node_t **, const char *);
86 private int node_lookup(EditLine *, const char *, key_node_t *,
88 private int node_enum(EditLine *, key_node_t *, int);
89 private int key__decode_char(char *, int, int);
91 #define KEY_BUFSIZ EL_BUFSIZ
95 * Initialize the key maps
98 key_init(EditLine *el)
101 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
102 if (el->el_key.buf == NULL)
104 el->el_key.map = NULL;
113 key_end(EditLine *el)
116 el_free((ptr_t) el->el_key.buf);
117 el->el_key.buf = NULL;
118 node__free(el->el_key.map);
123 * Associate cmd with a key value
125 protected key_value_t *
126 key_map_cmd(EditLine *el, int cmd)
129 el->el_key.val.cmd = (el_action_t) cmd;
130 return (&el->el_key.val);
135 * Associate str with a key value
137 protected key_value_t *
138 key_map_str(EditLine *el, char *str)
141 el->el_key.val.str = str;
142 return (&el->el_key.val);
147 * Takes all nodes on el->el_key.map and puts them on free list. Then
148 * initializes el->el_key.map with arrow keys
149 * [Always bind the ansi arrow keys?]
152 key_reset(EditLine *el)
155 node__put(el, el->el_key.map);
156 el->el_key.map = NULL;
162 * Calls the recursive function with entry point el->el_key.map
163 * Looks up *ch in map and then reads characters until a
164 * complete match is found or a mismatch occurs. Returns the
165 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
166 * Returns NULL in val.str and XK_STR for no match.
167 * The last character read is returned in *ch.
170 key_get(EditLine *el, char *ch, key_value_t *val)
173 return (node_trav(el, el->el_key.map, ch, val));
178 * Adds key to the el->el_key.map and associates the value in val with it.
179 * If key is already is in el->el_key.map, the new code is applied to the
180 * existing key. Ntype specifies if code is a command, an
181 * out str or a unix command.
184 key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
187 if (key[0] == '\0') {
188 (void) fprintf(el->el_errfile,
189 "key_add: Null extended-key not allowed.\n");
192 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
193 (void) fprintf(el->el_errfile,
194 "key_add: sequence-lead-in command not allowed\n");
197 if (el->el_key.map == NULL)
198 /* tree is initially empty. Set up new node to match key[0] */
199 el->el_key.map = node__get(key[0]);
200 /* it is properly initialized */
202 /* Now recurse through el->el_key.map */
203 (void) node__try(el, el->el_key.map, key, val, ntype);
212 key_clear(EditLine *el, el_action_t *map, const char *in)
215 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
216 ((map == el->el_map.key &&
217 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
218 (map == el->el_map.alt &&
219 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
220 (void) key_delete(el, in);
225 * Delete the key and all longer keys staring with key, if
229 key_delete(EditLine *el, const char *key)
232 if (key[0] == '\0') {
233 (void) fprintf(el->el_errfile,
234 "key_delete: Null extended-key not allowed.\n");
237 if (el->el_key.map == NULL)
240 (void) node__delete(el, &el->el_key.map, key);
246 * Print the binding associated with key key.
247 * Print entire el->el_key.map if null
250 key_print(EditLine *el, const char *key)
253 /* do nothing if el->el_key.map is empty and null key specified */
254 if (el->el_key.map == NULL && *key == 0)
257 el->el_key.buf[0] = '"';
258 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
259 /* key is not bound */
260 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
267 * recursively traverses node in tree until match or mismatch is
268 * found. May read in more characters.
271 node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
274 if (ptr->ch == *ch) {
277 /* key not complete so get next char */
278 if (el_getc(el, ch) != 1) { /* if EOF or error */
279 val->cmd = ED_END_OF_FILE;
281 /* PWP: Pretend we just read an end-of-file */
283 return (node_trav(el, ptr->next, ch, val));
286 if (ptr->type != XK_CMD)
291 /* no match found here */
293 /* try next sibling */
294 return (node_trav(el, ptr->sibling, ch, val));
296 /* no next sibling -- mismatch */
305 * Find a node that matches *str or allocate a new one
308 node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
311 if (ptr->ch != *str) {
314 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
315 if (xm->sibling->ch == *str)
317 if (xm->sibling == NULL)
318 xm->sibling = node__get(*str); /* setup new node */
321 if (*++str == '\0') {
323 if (ptr->next != NULL) {
324 node__put(el, ptr->next);
325 /* lose longer keys with this prefix */
335 el_free((ptr_t) ptr->val.str);
338 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
343 switch (ptr->type = ntype) {
349 if ((ptr->val.str = el_strdup(val->str)) == NULL)
353 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
357 /* still more chars to go */
358 if (ptr->next == NULL)
359 ptr->next = node__get(*str); /* setup new node */
360 (void) node__try(el, ptr->next, str, val, ntype);
367 * Delete node that matches str
370 node__delete(EditLine *el, key_node_t **inptr, const char *str)
373 key_node_t *prev_ptr = NULL;
377 if (ptr->ch != *str) {
380 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
381 if (xm->sibling->ch == *str)
383 if (xm->sibling == NULL)
388 if (*++str == '\0') {
390 if (prev_ptr == NULL)
391 *inptr = ptr->sibling;
393 prev_ptr->sibling = ptr->sibling;
397 } else if (ptr->next != NULL &&
398 node__delete(el, &ptr->next, str) == 1) {
399 if (ptr->next != NULL)
401 if (prev_ptr == NULL)
402 *inptr = ptr->sibling;
404 prev_ptr->sibling = ptr->sibling;
415 * Puts a tree of nodes onto free list using free(3).
418 node__put(EditLine *el, key_node_t *ptr)
423 if (ptr->next != NULL) {
424 node__put(el, ptr->next);
427 node__put(el, ptr->sibling);
435 if (ptr->val.str != NULL)
436 el_free((ptr_t) ptr->val.str);
439 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
442 el_free((ptr_t) ptr);
447 * Returns pointer to an key_node_t for ch.
454 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
466 node__free(key_node_t *k)
470 node__free(k->sibling);
476 * look for the str starting at node ptr.
480 node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt)
485 return (-1); /* cannot have null ptr */
488 /* no more chars in str. node_enum from here. */
489 (void) node_enum(el, ptr, cnt);
492 /* If match put this char into el->el_key.buf. Recurse */
493 if (ptr->ch == *str) {
495 ncnt = key__decode_char(el->el_key.buf, cnt,
496 (unsigned char) ptr->ch);
497 if (ptr->next != NULL)
498 /* not yet at leaf */
499 return (node_lookup(el, str + 1, ptr->next,
502 /* next node is null so key should be complete */
504 el->el_key.buf[ncnt + 1] = '"';
505 el->el_key.buf[ncnt + 2] = '\0';
506 key_kprint(el, el->el_key.buf,
507 &ptr->val, ptr->type);
511 /* mismatch -- str still has chars */
514 /* no match found try sibling */
516 return (node_lookup(el, str, ptr->sibling,
526 * Traverse the node printing the characters it is bound in buffer
529 node_enum(EditLine *el, key_node_t *ptr, int cnt)
533 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
534 el->el_key.buf[++cnt] = '"';
535 el->el_key.buf[++cnt] = '\0';
536 (void) fprintf(el->el_errfile,
537 "Some extended keys too long for internal print buffer");
538 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
543 (void) fprintf(el->el_errfile,
544 "node_enum: BUG!! Null ptr passed\n!");
548 /* put this char at end of str */
549 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
550 if (ptr->next == NULL) {
551 /* print this key and function */
552 el->el_key.buf[ncnt + 1] = '"';
553 el->el_key.buf[ncnt + 2] = '\0';
554 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
556 (void) node_enum(el, ptr->next, ncnt + 1);
558 /* go to sibling if there is one */
560 (void) node_enum(el, ptr->sibling, cnt);
566 * Print the specified key and its associated
567 * function specified by val
570 key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
573 char unparsbuf[EL_BUFSIZ];
574 static const char fmt[] = "%-15s-> %s\n";
580 (void) fprintf(el->el_outfile, fmt, key,
581 key__decode_str(val->str, unparsbuf,
582 ntype == XK_STR ? "\"\"" : "[]"));
585 for (fp = el->el_map.help; fp->name; fp++)
586 if (val->cmd == fp->func) {
587 (void) fprintf(el->el_outfile, fmt,
592 if (fp->name == NULL)
593 (void) fprintf(el->el_outfile,
594 "BUG! Command not found.\n");
599 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
603 (void) fprintf(el->el_outfile, fmt, key, "no input");
607 /* key__decode_char():
608 * Put a printable form of char in buf.
611 key__decode_char(char *buf, int cnt, int ch)
623 buf[cnt] = ch | 0100;
624 } else if (ch == '^') {
627 } else if (ch == '\\') {
630 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
634 buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
635 buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
636 buf[cnt] = (ch & 7) + '0';
642 /* key__decode_str():
643 * Make a printable version of the ey
646 key__decode_str(const char *str, char *buf, const char *sep)
657 if (sep[0] != '\0' && sep[1] != '\0')
662 for (p = str; *p != 0; p++) {
663 if (iscntrl((unsigned char) *p)) {
669 } else if (*p == '^' || *p == '\\') {
672 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
673 !isspace((unsigned char) *p))) {
677 *b++ = (((unsigned int) *p >> 6) & 7) + '0';
678 *b++ = (((unsigned int) *p >> 3) & 7) + '0';
679 *b++ = (*p & 7) + '0';
682 if (sep[0] != '\0' && sep[1] != '\0')
685 return (buf); /* should check for overflow */