1 /* $NetBSD: key.c,v 1.13 2002/03/18 16:00:55 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if !defined(lint) && !defined(SCCSID)
42 static char sccsid
[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
44 __RCSID("$NetBSD: key.c,v 1.13 2002/03/18 16:00:55 christos Exp $");
46 #endif /* not lint && not SCCSID */
49 * key.c: This module contains the procedures for maintaining
50 * the extended-key map.
52 * An extended-key (key) is a sequence of keystrokes introduced
53 * with an sequence introducer and consisting of an arbitrary
54 * number of characters. This module maintains a map (the el->el_key.map)
55 * to convert these extended-key sequences into input strs
56 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
59 * If key is a substr of some other keys, then the longer
60 * keys are lost!! That is, if the keys "abcd" and "abcef"
61 * are in el->el_key.map, adding the key "abc" will cause the first two
62 * definitions to be lost.
66 * 1) It is not possible to have one key that is a
75 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
76 * of these node elements
79 char ch
; /* single character of key */
80 int type
; /* node type */
81 key_value_t val
; /* command code or pointer to str, */
82 /* if this is a leaf */
83 struct key_node_t
*next
; /* ptr to next char of this key */
84 struct key_node_t
*sibling
; /* ptr to another key with same prefix*/
87 private int node_trav(EditLine
*, key_node_t
*, char *,
89 private int node__try(EditLine
*, key_node_t
*, const char *,
91 private key_node_t
*node__get(int);
92 private void node__put(EditLine
*, key_node_t
*);
93 private int node__delete(EditLine
*, key_node_t
**, const char *);
94 private int node_lookup(EditLine
*, const char *, key_node_t
*,
96 private int node_enum(EditLine
*, key_node_t
*, int);
97 private int key__decode_char(char *, int, int);
99 #define KEY_BUFSIZ EL_BUFSIZ
103 * Initialize the key maps
106 key_init(EditLine
*el
)
109 el
->el_key
.buf
= (char *) el_malloc(KEY_BUFSIZ
);
110 if (el
->el_key
.buf
== NULL
)
112 el
->el_key
.map
= NULL
;
122 key_end(EditLine
*el
)
125 el_free((ptr_t
) el
->el_key
.buf
);
126 el
->el_key
.buf
= NULL
;
127 node__put(el
, el
->el_key
.map
);
128 el
->el_key
.map
= NULL
;
133 * Associate cmd with a key value
135 protected key_value_t
*
136 key_map_cmd(EditLine
*el
, int cmd
)
139 el
->el_key
.val
.cmd
= (el_action_t
) cmd
;
140 return (&el
->el_key
.val
);
145 * Associate str with a key value
147 protected key_value_t
*
148 key_map_str(EditLine
*el
, char *str
)
151 el
->el_key
.val
.str
= str
;
152 return (&el
->el_key
.val
);
157 * Takes all nodes on el->el_key.map and puts them on free list. Then
158 * initializes el->el_key.map with arrow keys
159 * [Always bind the ansi arrow keys?]
162 key_reset(EditLine
*el
)
165 node__put(el
, el
->el_key
.map
);
166 el
->el_key
.map
= NULL
;
172 * Calls the recursive function with entry point el->el_key.map
173 * Looks up *ch in map and then reads characters until a
174 * complete match is found or a mismatch occurs. Returns the
175 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
176 * Returns NULL in val.str and XK_STR for no match.
177 * The last character read is returned in *ch.
180 key_get(EditLine
*el
, char *ch
, key_value_t
*val
)
183 return (node_trav(el
, el
->el_key
.map
, ch
, val
));
188 * Adds key to the el->el_key.map and associates the value in val with it.
189 * If key is already is in el->el_key.map, the new code is applied to the
190 * existing key. Ntype specifies if code is a command, an
191 * out str or a unix command.
194 key_add(EditLine
*el
, const char *key
, key_value_t
*val
, int ntype
)
197 if (key
[0] == '\0') {
198 (void) fprintf(el
->el_errfile
,
199 "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");
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]);
210 /* it is properly initialized */
212 /* Now recurse through el->el_key.map */
213 (void) node__try(el
, el
->el_key
.map
, key
, val
, ntype
);
222 key_clear(EditLine
*el
, el_action_t
*map
, const char *in
)
225 if ((map
[(unsigned char)*in
] == ED_SEQUENCE_LEAD_IN
) &&
226 ((map
== el
->el_map
.key
&&
227 el
->el_map
.alt
[(unsigned char)*in
] != ED_SEQUENCE_LEAD_IN
) ||
228 (map
== el
->el_map
.alt
&&
229 el
->el_map
.key
[(unsigned char)*in
] != ED_SEQUENCE_LEAD_IN
)))
230 (void) key_delete(el
, in
);
235 * Delete the key and all longer keys staring with key, if
239 key_delete(EditLine
*el
, const char *key
)
242 if (key
[0] == '\0') {
243 (void) fprintf(el
->el_errfile
,
244 "key_delete: Null extended-key not allowed.\n");
247 if (el
->el_key
.map
== NULL
)
250 (void) node__delete(el
, &el
->el_key
.map
, key
);
256 * Print the binding associated with key key.
257 * Print entire el->el_key.map if null
260 key_print(EditLine
*el
, const char *key
)
263 /* do nothing if el->el_key.map is empty and null key specified */
264 if (el
->el_key
.map
== NULL
&& *key
== 0)
267 el
->el_key
.buf
[0] = '"';
268 if (node_lookup(el
, key
, el
->el_key
.map
, 1) <= -1)
269 /* key is not bound */
270 (void) fprintf(el
->el_errfile
, "Unbound extended key \"%s\"\n",
277 * recursively traverses node in tree until match or mismatch is
278 * found. May read in more characters.
281 node_trav(EditLine
*el
, key_node_t
*ptr
, char *ch
, key_value_t
*val
)
284 if (ptr
->ch
== *ch
) {
287 /* key not complete so get next char */
288 if (el_getc(el
, ch
) != 1) { /* if EOF or error */
289 val
->cmd
= ED_END_OF_FILE
;
291 /* PWP: Pretend we just read an end-of-file */
293 return (node_trav(el
, ptr
->next
, ch
, val
));
296 if (ptr
->type
!= XK_CMD
)
301 /* no match found here */
303 /* try next sibling */
304 return (node_trav(el
, ptr
->sibling
, ch
, val
));
306 /* no next sibling -- mismatch */
315 * Find a node that matches *str or allocate a new one
318 node__try(EditLine
*el
, key_node_t
*ptr
, const char *str
, key_value_t
*val
, int ntype
)
321 if (ptr
->ch
!= *str
) {
324 for (xm
= ptr
; xm
->sibling
!= NULL
; xm
= xm
->sibling
)
325 if (xm
->sibling
->ch
== *str
)
327 if (xm
->sibling
== NULL
)
328 xm
->sibling
= node__get(*str
); /* setup new node */
331 if (*++str
== '\0') {
333 if (ptr
->next
!= NULL
) {
334 node__put(el
, ptr
->next
);
335 /* lose longer keys with this prefix */
345 el_free((ptr_t
) ptr
->val
.str
);
348 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n",
353 switch (ptr
->type
= ntype
) {
359 ptr
->val
.str
= strdup(val
->str
);
362 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ntype
));
366 /* still more chars to go */
367 if (ptr
->next
== NULL
)
368 ptr
->next
= node__get(*str
); /* setup new node */
369 (void) node__try(el
, ptr
->next
, str
, val
, ntype
);
376 * Delete node that matches str
379 node__delete(EditLine
*el
, key_node_t
**inptr
, const char *str
)
382 key_node_t
*prev_ptr
= NULL
;
386 if (ptr
->ch
!= *str
) {
389 for (xm
= ptr
; xm
->sibling
!= NULL
; xm
= xm
->sibling
)
390 if (xm
->sibling
->ch
== *str
)
392 if (xm
->sibling
== NULL
)
397 if (*++str
== '\0') {
399 if (prev_ptr
== NULL
)
400 *inptr
= ptr
->sibling
;
402 prev_ptr
->sibling
= ptr
->sibling
;
406 } else if (ptr
->next
!= NULL
&&
407 node__delete(el
, &ptr
->next
, str
) == 1) {
408 if (ptr
->next
!= NULL
)
410 if (prev_ptr
== NULL
)
411 *inptr
= ptr
->sibling
;
413 prev_ptr
->sibling
= ptr
->sibling
;
424 * Puts a tree of nodes onto free list using free(3).
427 node__put(EditLine
*el
, key_node_t
*ptr
)
432 if (ptr
->next
!= NULL
) {
433 node__put(el
, ptr
->next
);
436 node__put(el
, ptr
->sibling
);
444 if (ptr
->val
.str
!= NULL
)
445 el_free((ptr_t
) ptr
->val
.str
);
448 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ptr
->type
));
451 el_free((ptr_t
) ptr
);
456 * Returns pointer to an key_node_t for ch.
463 ptr
= (key_node_t
*) el_malloc((size_t) sizeof(key_node_t
));
477 * look for the str starting at node ptr.
481 node_lookup(EditLine
*el
, const char *str
, key_node_t
*ptr
, int cnt
)
486 return (-1); /* cannot have null ptr */
489 /* no more chars in str. node_enum from here. */
490 (void) node_enum(el
, ptr
, cnt
);
493 /* If match put this char into el->el_key.buf. Recurse */
494 if (ptr
->ch
== *str
) {
496 ncnt
= key__decode_char(el
->el_key
.buf
, cnt
,
497 (unsigned char) ptr
->ch
);
498 if (ptr
->next
!= NULL
)
499 /* not yet at leaf */
500 return (node_lookup(el
, str
+ 1, ptr
->next
,
503 /* next node is null so key should be complete */
505 el
->el_key
.buf
[ncnt
+ 1] = '"';
506 el
->el_key
.buf
[ncnt
+ 2] = '\0';
507 key_kprint(el
, el
->el_key
.buf
,
508 &ptr
->val
, ptr
->type
);
512 /* mismatch -- str still has chars */
515 /* no match found try sibling */
517 return (node_lookup(el
, str
, ptr
->sibling
,
527 * Traverse the node printing the characters it is bound in buffer
530 node_enum(EditLine
*el
, key_node_t
*ptr
, int cnt
)
534 if (cnt
>= KEY_BUFSIZ
- 5) { /* buffer too small */
535 el
->el_key
.buf
[++cnt
] = '"';
536 el
->el_key
.buf
[++cnt
] = '\0';
537 (void) fprintf(el
->el_errfile
,
538 "Some extended keys too long for internal print buffer");
539 (void) fprintf(el
->el_errfile
, " \"%s...\"\n", el
->el_key
.buf
);
544 (void) fprintf(el
->el_errfile
,
545 "node_enum: BUG!! Null ptr passed\n!");
549 /* put this char at end of str */
550 ncnt
= key__decode_char(el
->el_key
.buf
, cnt
, (unsigned char) ptr
->ch
);
551 if (ptr
->next
== NULL
) {
552 /* print this key and function */
553 el
->el_key
.buf
[ncnt
+ 1] = '"';
554 el
->el_key
.buf
[ncnt
+ 2] = '\0';
555 key_kprint(el
, el
->el_key
.buf
, &ptr
->val
, ptr
->type
);
557 (void) node_enum(el
, ptr
->next
, ncnt
+ 1);
559 /* go to sibling if there is one */
561 (void) node_enum(el
, ptr
->sibling
, cnt
);
567 * Print the specified key and its associated
568 * function specified by val
571 key_kprint(EditLine
*el
, const char *key
, key_value_t
*val
, int ntype
)
574 char unparsbuf
[EL_BUFSIZ
];
575 static const char fmt
[] = "%-15s-> %s\n";
581 (void) fprintf(el
->el_outfile
, fmt
, key
,
582 key__decode_str(val
->str
, unparsbuf
,
583 ntype
== XK_STR
? "\"\"" : "[]"));
586 for (fp
= el
->el_map
.help
; fp
->name
; fp
++)
587 if (val
->cmd
== fp
->func
) {
588 (void) fprintf(el
->el_outfile
, fmt
,
593 if (fp
->name
== NULL
)
594 (void) fprintf(el
->el_outfile
,
595 "BUG! Command not found.\n");
600 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ntype
));
604 (void) fprintf(el
->el_outfile
, fmt
, key
, "no input");
608 /* key__decode_char():
609 * Put a printable form of char in buf.
612 key__decode_char(char *buf
, int cnt
, int ch
)
624 buf
[cnt
] = ch
| 0100;
625 } else if (ch
== '^') {
628 } else if (ch
== '\\') {
631 } else if (ch
== ' ' || (isprint(ch
) && !isspace(ch
))) {
635 buf
[cnt
++] = (((unsigned int) ch
>> 6) & 7) + '0';
636 buf
[cnt
++] = (((unsigned int) ch
>> 3) & 7) + '0';
637 buf
[cnt
] = (ch
& 7) + '0';
643 /* key__decode_str():
644 * Make a printable version of the ey
647 key__decode_str(const char *str
, char *buf
, const char *sep
)
658 if (sep
[0] != '\0' && sep
[1] != '\0')
663 for (p
= str
; *p
!= 0; p
++) {
664 if (iscntrl((unsigned char) *p
)) {
670 } else if (*p
== '^' || *p
== '\\') {
673 } else if (*p
== ' ' || (isprint((unsigned char) *p
) &&
674 !isspace((unsigned char) *p
))) {
678 *b
++ = (((unsigned int) *p
>> 6) & 7) + '0';
679 *b
++ = (((unsigned int) *p
>> 3) & 7) + '0';
680 *b
++ = (*p
& 7) + '0';
683 if (sep
[0] != '\0' && sep
[1] != '\0')
686 return (buf
); /* should check for overflow */