1
/* $NetBSD: key.c,v 1.19 2006/03/23 20:22:51 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. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#if !defined(lint) && !defined(SCCSID)
38
static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
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 a 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
70
* The Nodes of the el->el_key.map. The el->el_key.map is a linked list
71
* of these node elements
74
char ch; /* single character of key */
75
int type; /* node type */
76
key_value_t val; /* command code or pointer to str, */
77
/* if this is a leaf */
78
struct key_node_t *next; /* ptr to next char of this key */
79
struct key_node_t *sibling; /* ptr to another key with same prefix*/
82
private int node_trav(EditLine *, key_node_t *, char *,
84
private int node__try(EditLine *, key_node_t *, const char *,
86
private key_node_t *node__get(int);
87
private void node__free(key_node_t *);
88
private void node__put(EditLine *, key_node_t *);
89
private int node__delete(EditLine *, key_node_t **, const char *);
90
private int node_lookup(EditLine *, const char *, key_node_t *,
92
private int node_enum(EditLine *, key_node_t *, int);
94
#define KEY_BUFSIZ EL_BUFSIZ
98
* Initialize the key maps
101
key_init(EditLine *el)
104
el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
105
if (el->el_key.buf == NULL)
107
el->el_key.map = NULL;
116
key_end(EditLine *el)
119
el_free((ptr_t) el->el_key.buf);
120
el->el_key.buf = NULL;
121
node__free(el->el_key.map);
126
* Associate cmd with a key value
128
protected key_value_t *
129
key_map_cmd(EditLine *el, int cmd)
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 *
141
key_map_str(EditLine *el, char *str)
144
el->el_key.val.str = str;
145
return (&el->el_key.val);
150
* Takes all nodes on el->el_key.map and puts them on free list. Then
151
* initializes el->el_key.map with arrow keys
152
* [Always bind the ansi arrow keys?]
155
key_reset(EditLine *el)
158
node__put(el, el->el_key.map);
159
el->el_key.map = NULL;
165
* Calls the recursive function with entry point el->el_key.map
166
* Looks up *ch in map and then reads characters until a
167
* complete match is found or a mismatch occurs. Returns the
168
* type of the match found (XK_STR, XK_CMD, or XK_EXE).
169
* Returns NULL in val.str and XK_STR for no match.
170
* The last character read is returned in *ch.
173
key_get(EditLine *el, char *ch, key_value_t *val)
176
return (node_trav(el, el->el_key.map, ch, val));
181
* Adds key to the el->el_key.map and associates the value in val with it.
182
* If key is already is in el->el_key.map, the new code is applied to the
183
* existing key. Ntype specifies if code is a command, an
184
* out str or a unix command.
187
key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
190
if (key[0] == '\0') {
191
(void) fprintf(el->el_errfile,
192
"key_add: Null extended-key not allowed.\n");
195
if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
196
(void) fprintf(el->el_errfile,
197
"key_add: sequence-lead-in command not allowed\n");
200
if (el->el_key.map == NULL)
201
/* tree is initially empty. Set up new node to match key[0] */
202
el->el_key.map = node__get(key[0]);
203
/* it is properly initialized */
205
/* Now recurse through el->el_key.map */
206
(void) node__try(el, el->el_key.map, key, val, ntype);
215
key_clear(EditLine *el, el_action_t *map, const char *in)
218
if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
219
((map == el->el_map.key &&
220
el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
221
(map == el->el_map.alt &&
222
el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
223
(void) key_delete(el, in);
228
* Delete the key and all longer keys staring with key, if
232
key_delete(EditLine *el, const char *key)
235
if (key[0] == '\0') {
236
(void) fprintf(el->el_errfile,
237
"key_delete: Null extended-key not allowed.\n");
240
if (el->el_key.map == NULL)
243
(void) node__delete(el, &el->el_key.map, key);
249
* Print the binding associated with key key.
250
* Print entire el->el_key.map if null
253
key_print(EditLine *el, const char *key)
256
/* do nothing if el->el_key.map is empty and null key specified */
257
if (el->el_key.map == NULL && *key == 0)
260
el->el_key.buf[0] = '"';
261
if (node_lookup(el, key, el->el_key.map, 1) <= -1)
262
/* key is not bound */
263
(void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
270
* recursively traverses node in tree until match or mismatch is
271
* found. May read in more characters.
274
node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
277
if (ptr->ch == *ch) {
280
/* key not complete so get next char */
281
if (el_getc(el, ch) != 1) { /* if EOF or error */
282
val->cmd = ED_END_OF_FILE;
284
/* PWP: Pretend we just read an end-of-file */
286
return (node_trav(el, ptr->next, ch, val));
289
if (ptr->type != XK_CMD)
294
/* no match found here */
296
/* try next sibling */
297
return (node_trav(el, ptr->sibling, ch, val));
299
/* no next sibling -- mismatch */
308
* Find a node that matches *str or allocate a new one
311
node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
314
if (ptr->ch != *str) {
317
for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
318
if (xm->sibling->ch == *str)
320
if (xm->sibling == NULL)
321
xm->sibling = node__get(*str); /* setup new node */
324
if (*++str == '\0') {
326
if (ptr->next != NULL) {
327
node__put(el, ptr->next);
328
/* lose longer keys with this prefix */
338
el_free((ptr_t) ptr->val.str);
341
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
346
switch (ptr->type = ntype) {
352
if ((ptr->val.str = el_strdup(val->str)) == NULL)
356
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
360
/* still more chars to go */
361
if (ptr->next == NULL)
362
ptr->next = node__get(*str); /* setup new node */
363
(void) node__try(el, ptr->next, str, val, ntype);
370
* Delete node that matches str
373
node__delete(EditLine *el, key_node_t **inptr, const char *str)
376
key_node_t *prev_ptr = NULL;
380
if (ptr->ch != *str) {
383
for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
384
if (xm->sibling->ch == *str)
386
if (xm->sibling == NULL)
391
if (*++str == '\0') {
393
if (prev_ptr == NULL)
394
*inptr = ptr->sibling;
396
prev_ptr->sibling = ptr->sibling;
400
} else if (ptr->next != NULL &&
401
node__delete(el, &ptr->next, str) == 1) {
402
if (ptr->next != NULL)
404
if (prev_ptr == NULL)
405
*inptr = ptr->sibling;
407
prev_ptr->sibling = ptr->sibling;
418
* Puts a tree of nodes onto free list using free(3).
421
node__put(EditLine *el, key_node_t *ptr)
426
if (ptr->next != NULL) {
427
node__put(el, ptr->next);
430
node__put(el, ptr->sibling);
438
if (ptr->val.str != NULL)
439
el_free((ptr_t) ptr->val.str);
442
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
445
el_free((ptr_t) ptr);
450
* Returns pointer to a key_node_t for ch.
457
ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
469
node__free(key_node_t *k)
473
node__free(k->sibling);
479
* look for the str starting at node ptr.
483
node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt)
488
return (-1); /* cannot have null ptr */
491
/* no more chars in str. node_enum from here. */
492
(void) node_enum(el, ptr, cnt);
495
/* If match put this char into el->el_key.buf. Recurse */
496
if (ptr->ch == *str) {
498
ncnt = key__decode_char(el->el_key.buf, KEY_BUFSIZ, cnt,
499
(unsigned char) ptr->ch);
500
if (ptr->next != NULL)
501
/* not yet at leaf */
502
return (node_lookup(el, str + 1, ptr->next,
505
/* next node is null so key should be complete */
507
el->el_key.buf[ncnt + 1] = '"';
508
el->el_key.buf[ncnt + 2] = '\0';
509
key_kprint(el, el->el_key.buf,
510
&ptr->val, ptr->type);
514
/* mismatch -- str still has chars */
517
/* no match found try sibling */
519
return (node_lookup(el, str, ptr->sibling,
529
* Traverse the node printing the characters it is bound in buffer
532
node_enum(EditLine *el, key_node_t *ptr, int cnt)
536
if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
537
el->el_key.buf[++cnt] = '"';
538
el->el_key.buf[++cnt] = '\0';
539
(void) fprintf(el->el_errfile,
540
"Some extended keys too long for internal print buffer");
541
(void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
546
(void) fprintf(el->el_errfile,
547
"node_enum: BUG!! Null ptr passed\n!");
551
/* put this char at end of str */
552
ncnt = key__decode_char(el->el_key.buf, KEY_BUFSIZ, cnt,
553
(unsigned char)ptr->ch);
554
if (ptr->next == NULL) {
555
/* print this key and function */
556
el->el_key.buf[ncnt + 1] = '"';
557
el->el_key.buf[ncnt + 2] = '\0';
558
key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
560
(void) node_enum(el, ptr->next, ncnt + 1);
562
/* go to sibling if there is one */
564
(void) node_enum(el, ptr->sibling, cnt);
570
* Print the specified key and its associated
571
* function specified by val
574
key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
577
char unparsbuf[EL_BUFSIZ];
578
static const char fmt[] = "%-15s-> %s\n";
584
(void) key__decode_str(val->str, unparsbuf,
586
ntype == XK_STR ? "\"\"" : "[]");
587
(void) fprintf(el->el_outfile, fmt, key, unparsbuf);
590
for (fp = el->el_map.help; fp->name; fp++)
591
if (val->cmd == fp->func) {
592
(void) fprintf(el->el_outfile, fmt,
597
if (fp->name == NULL)
598
(void) fprintf(el->el_outfile,
599
"BUG! Command not found.\n");
604
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
608
(void) fprintf(el->el_outfile, fmt, key, "no input");
617
/* key__decode_char():
618
* Put a printable form of char in buf.
621
key__decode_char(char *buf, int cnt, int off, int ch)
623
char *sb = buf + off;
624
char *eb = buf + cnt;
637
} else if (ch == '^') {
640
} else if (ch == '\\') {
643
} else if (ch == ' ' || (el_isprint(ch) && !isspace(ch))) {
647
ADDC((((unsigned int) ch >> 6) & 7) + '0');
648
ADDC((((unsigned int) ch >> 3) & 7) + '0');
649
ADDC((ch & 7) + '0');
655
/* key__decode_str():
656
* Make a printable version of the ey
659
key__decode_str(const char *str, char *buf, int len, const char *sep)
661
char *b = buf, *eb = b + len;
665
if (sep[0] != '\0') {
671
if (sep[0] != '\0' && sep[1] != '\0') {
676
for (p = str; *p != 0; p++) {
677
if (iscntrl((unsigned char) *p)) {
684
} else if (*p == '^' || *p == '\\') {
687
} else if (*p == ' ' || (el_isprint((unsigned char) *p) &&
688
!isspace((unsigned char) *p))) {
692
ADDC((((unsigned int) *p >> 6) & 7) + '0');
693
ADDC((((unsigned int) *p >> 3) & 7) + '0');
694
ADDC((*p & 7) + '0');
697
if (sep[0] != '\0' && sep[1] != '\0') {