Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / lib / libedit / key.c
blob0b9d05d337b320de8ead119e3cb82ff40b9c12ed
1 /*-
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
10 * are met:
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
30 * SUCH DAMAGE.
32 * $NetBSD: key.c,v 1.19 2006/03/23 20:22:51 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * key.c: This module contains the procedures for maintaining
43 * the extended-key map.
45 * An extended-key (key) is a sequence of keystrokes introduced
46 * with a sequence introducer and consisting of an arbitrary
47 * number of characters. This module maintains a map (the el->el_key.map)
48 * to convert these extended-key sequences into input strs
49 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
51 * Warning:
52 * If key is a substr of some other keys, then the longer
53 * keys are lost!! That is, if the keys "abcd" and "abcef"
54 * are in el->el_key.map, adding the key "abc" will cause the first two
55 * definitions to be lost.
57 * Restrictions:
58 * -------------
59 * 1) It is not possible to have one key that is a
60 * substr of another.
62 #include <string.h>
63 #include <stdlib.h>
65 #include "el.h"
68 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
69 * of these node elements
71 struct key_node_t {
72 char ch; /* single character of key */
73 int type; /* node type */
74 key_value_t val; /* command code or pointer to str, */
75 /* if this is a leaf */
76 struct key_node_t *next; /* ptr to next char of this key */
77 struct key_node_t *sibling; /* ptr to another key with same prefix*/
80 private int node_trav(EditLine *, key_node_t *, char *,
81 key_value_t *);
82 private int node__try(EditLine *, key_node_t *, const char *,
83 key_value_t *, int);
84 private key_node_t *node__get(int);
85 private void node__free(key_node_t *);
86 private void node__put(EditLine *, key_node_t *);
87 private int node__delete(EditLine *, key_node_t **, const char *);
88 private int node_lookup(EditLine *, const char *, key_node_t *,
89 int);
90 private int node_enum(EditLine *, key_node_t *, int);
92 #define KEY_BUFSIZ EL_BUFSIZ
95 /* key_init():
96 * Initialize the key maps
98 protected int
99 key_init(EditLine *el)
102 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
103 if (el->el_key.buf == NULL)
104 return (-1);
105 el->el_key.map = NULL;
106 key_reset(el);
107 return (0);
110 /* key_end():
111 * Free the key maps
113 protected void
114 key_end(EditLine *el)
117 el_free((ptr_t) el->el_key.buf);
118 el->el_key.buf = NULL;
119 node__free(el->el_key.map);
123 /* key_map_cmd():
124 * Associate cmd with a key value
126 protected key_value_t *
127 key_map_cmd(EditLine *el, int cmd)
130 el->el_key.val.cmd = (el_action_t) cmd;
131 return (&el->el_key.val);
135 /* key_map_str():
136 * Associate str with a key value
138 protected key_value_t *
139 key_map_str(EditLine *el, char *str)
142 el->el_key.val.str = str;
143 return (&el->el_key.val);
147 /* key_reset():
148 * Takes all nodes on el->el_key.map and puts them on free list. Then
149 * initializes el->el_key.map with arrow keys
150 * [Always bind the ansi arrow keys?]
152 protected void
153 key_reset(EditLine *el)
156 node__put(el, el->el_key.map);
157 el->el_key.map = NULL;
158 return;
162 /* key_get():
163 * Calls the recursive function with entry point el->el_key.map
164 * Looks up *ch in map and then reads characters until a
165 * complete match is found or a mismatch occurs. Returns the
166 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
167 * Returns NULL in val.str and XK_STR for no match.
168 * The last character read is returned in *ch.
170 protected int
171 key_get(EditLine *el, char *ch, key_value_t *val)
174 return (node_trav(el, el->el_key.map, ch, val));
178 /* key_add():
179 * Adds key to the el->el_key.map and associates the value in val with it.
180 * If key is already is in el->el_key.map, the new code is applied to the
181 * existing key. Ntype specifies if code is a command, an
182 * out str or a unix command.
184 protected void
185 key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
188 if (key[0] == '\0') {
189 (void) fprintf(el->el_errfile,
190 "key_add: Null extended-key not allowed.\n");
191 return;
193 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
194 (void) fprintf(el->el_errfile,
195 "key_add: sequence-lead-in command not allowed\n");
196 return;
198 if (el->el_key.map == NULL)
199 /* tree is initially empty. Set up new node to match key[0] */
200 el->el_key.map = node__get(key[0]);
201 /* it is properly initialized */
203 /* Now recurse through el->el_key.map */
204 (void) node__try(el, el->el_key.map, key, val, ntype);
205 return;
209 /* key_clear():
212 protected void
213 key_clear(EditLine *el, el_action_t *map, const char *in)
216 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
217 ((map == el->el_map.key &&
218 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
219 (map == el->el_map.alt &&
220 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
221 (void) key_delete(el, in);
225 /* key_delete():
226 * Delete the key and all longer keys staring with key, if
227 * they exists.
229 protected int
230 key_delete(EditLine *el, const char *key)
233 if (key[0] == '\0') {
234 (void) fprintf(el->el_errfile,
235 "key_delete: Null extended-key not allowed.\n");
236 return (-1);
238 if (el->el_key.map == NULL)
239 return (0);
241 (void) node__delete(el, &el->el_key.map, key);
242 return (0);
246 /* key_print():
247 * Print the binding associated with key key.
248 * Print entire el->el_key.map if null
250 protected void
251 key_print(EditLine *el, const char *key)
254 /* do nothing if el->el_key.map is empty and null key specified */
255 if (el->el_key.map == NULL && *key == 0)
256 return;
258 el->el_key.buf[0] = '"';
259 if (node_lookup(el, key, el->el_key.map, 1) <= -1)
260 /* key is not bound */
261 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
262 key);
263 return;
267 /* node_trav():
268 * recursively traverses node in tree until match or mismatch is
269 * found. May read in more characters.
271 private int
272 node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
275 if (ptr->ch == *ch) {
276 /* match found */
277 if (ptr->next) {
278 /* key not complete so get next char */
279 if (el_getc(el, ch) != 1) { /* if EOF or error */
280 val->cmd = ED_END_OF_FILE;
281 return (XK_CMD);
282 /* PWP: Pretend we just read an end-of-file */
284 return (node_trav(el, ptr->next, ch, val));
285 } else {
286 *val = ptr->val;
287 if (ptr->type != XK_CMD)
288 *ch = '\0';
289 return (ptr->type);
291 } else {
292 /* no match found here */
293 if (ptr->sibling) {
294 /* try next sibling */
295 return (node_trav(el, ptr->sibling, ch, val));
296 } else {
297 /* no next sibling -- mismatch */
298 val->str = NULL;
299 return (XK_STR);
305 /* node__try():
306 * Find a node that matches *str or allocate a new one
308 private int
309 node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
312 if (ptr->ch != *str) {
313 key_node_t *xm;
315 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
316 if (xm->sibling->ch == *str)
317 break;
318 if (xm->sibling == NULL)
319 xm->sibling = node__get(*str); /* setup new node */
320 ptr = xm->sibling;
322 if (*++str == '\0') {
323 /* we're there */
324 if (ptr->next != NULL) {
325 node__put(el, ptr->next);
326 /* lose longer keys with this prefix */
327 ptr->next = NULL;
329 switch (ptr->type) {
330 case XK_CMD:
331 case XK_NOD:
332 break;
333 case XK_STR:
334 case XK_EXE:
335 if (ptr->val.str)
336 el_free((ptr_t) ptr->val.str);
337 break;
338 default:
339 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
340 ptr->type));
341 break;
344 switch (ptr->type = ntype) {
345 case XK_CMD:
346 ptr->val = *val;
347 break;
348 case XK_STR:
349 case XK_EXE:
350 if ((ptr->val.str = el_strdup(val->str)) == NULL)
351 return -1;
352 break;
353 default:
354 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
355 break;
357 } else {
358 /* still more chars to go */
359 if (ptr->next == NULL)
360 ptr->next = node__get(*str); /* setup new node */
361 (void) node__try(el, ptr->next, str, val, ntype);
363 return (0);
367 /* node__delete():
368 * Delete node that matches str
370 private int
371 node__delete(EditLine *el, key_node_t **inptr, const char *str)
373 key_node_t *ptr;
374 key_node_t *prev_ptr = NULL;
376 ptr = *inptr;
378 if (ptr->ch != *str) {
379 key_node_t *xm;
381 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
382 if (xm->sibling->ch == *str)
383 break;
384 if (xm->sibling == NULL)
385 return (0);
386 prev_ptr = xm;
387 ptr = xm->sibling;
389 if (*++str == '\0') {
390 /* we're there */
391 if (prev_ptr == NULL)
392 *inptr = ptr->sibling;
393 else
394 prev_ptr->sibling = ptr->sibling;
395 ptr->sibling = NULL;
396 node__put(el, ptr);
397 return (1);
398 } else if (ptr->next != NULL &&
399 node__delete(el, &ptr->next, str) == 1) {
400 if (ptr->next != NULL)
401 return (0);
402 if (prev_ptr == NULL)
403 *inptr = ptr->sibling;
404 else
405 prev_ptr->sibling = ptr->sibling;
406 ptr->sibling = NULL;
407 node__put(el, ptr);
408 return (1);
409 } else {
410 return (0);
415 /* node__put():
416 * Puts a tree of nodes onto free list using free(3).
418 private void
419 node__put(EditLine *el, key_node_t *ptr)
421 if (ptr == NULL)
422 return;
424 if (ptr->next != NULL) {
425 node__put(el, ptr->next);
426 ptr->next = NULL;
428 node__put(el, ptr->sibling);
430 switch (ptr->type) {
431 case XK_CMD:
432 case XK_NOD:
433 break;
434 case XK_EXE:
435 case XK_STR:
436 if (ptr->val.str != NULL)
437 el_free((ptr_t) ptr->val.str);
438 break;
439 default:
440 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
441 break;
443 el_free((ptr_t) ptr);
447 /* node__get():
448 * Returns pointer to a key_node_t for ch.
450 private key_node_t *
451 node__get(int ch)
453 key_node_t *ptr;
455 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
456 if (ptr == NULL)
457 return NULL;
458 ptr->ch = ch;
459 ptr->type = XK_NOD;
460 ptr->val.str = NULL;
461 ptr->next = NULL;
462 ptr->sibling = NULL;
463 return (ptr);
466 private void
467 node__free(key_node_t *k)
469 if (k == NULL)
470 return;
471 node__free(k->sibling);
472 node__free(k->next);
473 el_free((ptr_t) k);
476 /* node_lookup():
477 * look for the str starting at node ptr.
478 * Print if last node
480 private int
481 node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt)
483 int ncnt;
485 if (ptr == NULL)
486 return (-1); /* cannot have null ptr */
488 if (*str == 0) {
489 /* no more chars in str. node_enum from here. */
490 (void) node_enum(el, ptr, cnt);
491 return (0);
492 } else {
493 /* If match put this char into el->el_key.buf. Recurse */
494 if (ptr->ch == *str) {
495 /* match found */
496 ncnt = key__decode_char(el->el_key.buf, KEY_BUFSIZ, 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,
501 ncnt + 1));
502 else {
503 /* next node is null so key should be complete */
504 if (str[1] == 0) {
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);
509 return (0);
510 } else
511 return (-1);
512 /* mismatch -- str still has chars */
514 } else {
515 /* no match found try sibling */
516 if (ptr->sibling)
517 return (node_lookup(el, str, ptr->sibling,
518 cnt));
519 else
520 return (-1);
526 /* node_enum():
527 * Traverse the node printing the characters it is bound in buffer
529 private int
530 node_enum(EditLine *el, key_node_t *ptr, int cnt)
532 int ncnt;
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);
540 return (0);
542 if (ptr == NULL) {
543 #ifdef DEBUG_EDIT
544 (void) fprintf(el->el_errfile,
545 "node_enum: BUG!! Null ptr passed\n!");
546 #endif
547 return (-1);
549 /* put this char at end of str */
550 ncnt = key__decode_char(el->el_key.buf, KEY_BUFSIZ, cnt,
551 (unsigned char)ptr->ch);
552 if (ptr->next == NULL) {
553 /* print this key and function */
554 el->el_key.buf[ncnt + 1] = '"';
555 el->el_key.buf[ncnt + 2] = '\0';
556 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
557 } else
558 (void) node_enum(el, ptr->next, ncnt + 1);
560 /* go to sibling if there is one */
561 if (ptr->sibling)
562 (void) node_enum(el, ptr->sibling, cnt);
563 return (0);
567 /* key_kprint():
568 * Print the specified key and its associated
569 * function specified by val
571 protected void
572 key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
574 el_bindings_t *fp;
575 char unparsbuf[EL_BUFSIZ];
576 static const char fmt[] = "%-15s-> %s\n";
578 if (val != NULL)
579 switch (ntype) {
580 case XK_STR:
581 case XK_EXE:
582 (void) key__decode_str(val->str, unparsbuf,
583 sizeof(unparsbuf),
584 ntype == XK_STR ? "\"\"" : "[]");
585 (void) fprintf(el->el_outfile, fmt, key, unparsbuf);
586 break;
587 case XK_CMD:
588 for (fp = el->el_map.help; fp->name; fp++)
589 if (val->cmd == fp->func) {
590 (void) fprintf(el->el_outfile, fmt,
591 key, fp->name);
592 break;
594 #ifdef DEBUG_KEY
595 if (fp->name == NULL)
596 (void) fprintf(el->el_outfile,
597 "BUG! Command not found.\n");
598 #endif
600 break;
601 default:
602 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
603 break;
605 else
606 (void) fprintf(el->el_outfile, fmt, key, "no input");
610 #define ADDC(c) \
611 if (b < eb) \
612 *b++ = c; \
613 else \
615 /* key__decode_char():
616 * Put a printable form of char in buf.
618 protected int
619 key__decode_char(char *buf, int cnt, int off, int ch)
621 char *sb = buf + off;
622 char *eb = buf + cnt;
623 char *b = sb;
625 ch = (unsigned char)ch;
626 if (ch == 0) {
627 ADDC('^');
628 ADDC('@');
629 return b - sb;
631 if (iscntrl(ch)) {
632 ADDC('^');
633 if (ch == '\177')
634 ADDC('?');
635 else
636 ADDC(toascii(ch) | 0100);
637 } else if (ch == '^') {
638 ADDC('\\');
639 ADDC('^');
640 } else if (ch == '\\') {
641 ADDC('\\');
642 ADDC('\\');
643 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
644 ADDC(ch);
645 } else {
646 ADDC('\\');
647 ADDC((((unsigned int) ch >> 6) & 7) + '0');
648 ADDC((((unsigned int) ch >> 3) & 7) + '0');
649 ADDC((ch & 7) + '0');
651 return b - sb;
655 /* key__decode_str():
656 * Make a printable version of the ey
658 protected int
659 key__decode_str(const char *str, char *buf, int len, const char *sep)
661 char *b = buf, *eb = b + len;
662 const char *p;
664 b = buf;
665 if (sep[0] != '\0') {
666 ADDC(sep[0]);
668 if (*str == '\0') {
669 ADDC('^');
670 ADDC('@');
671 if (sep[0] != '\0' && sep[1] != '\0') {
672 ADDC(sep[1]);
674 goto done;
676 for (p = str; *p != 0; p++) {
677 if (iscntrl((unsigned char) *p)) {
678 ADDC('^');
679 if (*p == '\177') {
680 ADDC('?');
681 } else {
682 ADDC(toascii(*p) | 0100);
684 } else if (*p == '^' || *p == '\\') {
685 ADDC('\\');
686 ADDC(*p);
687 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
688 !isspace((unsigned char) *p))) {
689 ADDC(*p);
690 } else {
691 ADDC('\\');
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') {
698 ADDC(sep[1]);
700 done:
701 ADDC('\0');
702 if (b - buf >= len)
703 buf[len - 1] = '\0';
704 return b - buf;