Support for withheld or missing CID string
[asterisk-bristuff.git] / main / editline / key.c
blob0dcdf4191ff9fe6fa888fe609bb51ed7136c43cc
1 /* $NetBSD: key.c,v 1.13 2002/03/18 16:00:55 christos Exp $ */
3 /*-
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
12 * are met:
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
36 * SUCH DAMAGE.
39 #include "config.h"
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: key.c,v 1.13 2002/03/18 16:00:55 christos Exp $");
45 #endif
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).
58 * Warning:
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.
64 * Restrictions:
65 * -------------
66 * 1) It is not possible to have one key that is a
67 * substr of another.
69 #include <string.h>
70 #include <stdlib.h>
72 #include "el.h"
75 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
76 * of these node elements
78 struct key_node_t {
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 *,
88 key_value_t *);
89 private int node__try(EditLine *, key_node_t *, const char *,
90 key_value_t *, int);
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 *,
95 int);
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
102 /* key_init():
103 * Initialize the key maps
105 protected int
106 key_init(EditLine *el)
109 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
110 if (el->el_key.buf == NULL)
111 return (-1);
112 el->el_key.map = NULL;
113 key_reset(el);
114 return (0);
118 /* key_end():
119 * Free the key maps
121 protected void
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;
132 /* key_map_cmd():
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);
144 /* key_map_str():
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);
156 /* key_reset():
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?]
161 protected void
162 key_reset(EditLine *el)
165 node__put(el, el->el_key.map);
166 el->el_key.map = NULL;
167 return;
171 /* key_get():
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.
179 protected int
180 key_get(EditLine *el, char *ch, key_value_t *val)
183 return (node_trav(el, el->el_key.map, ch, val));
187 /* key_add():
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.
193 protected void
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");
200 return;
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");
205 return;
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);
214 return;
218 /* key_clear():
221 protected void
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);
234 /* key_delete():
235 * Delete the key and all longer keys staring with key, if
236 * they exists.
238 protected int
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");
245 return (-1);
247 if (el->el_key.map == NULL)
248 return (0);
250 (void) node__delete(el, &el->el_key.map, key);
251 return (0);
255 /* key_print():
256 * Print the binding associated with key key.
257 * Print entire el->el_key.map if null
259 protected void
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)
265 return;
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",
271 key);
272 return;
276 /* node_trav():
277 * recursively traverses node in tree until match or mismatch is
278 * found. May read in more characters.
280 private int
281 node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
284 if (ptr->ch == *ch) {
285 /* match found */
286 if (ptr->next) {
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;
290 return (XK_CMD);
291 /* PWP: Pretend we just read an end-of-file */
293 return (node_trav(el, ptr->next, ch, val));
294 } else {
295 *val = ptr->val;
296 if (ptr->type != XK_CMD)
297 *ch = '\0';
298 return (ptr->type);
300 } else {
301 /* no match found here */
302 if (ptr->sibling) {
303 /* try next sibling */
304 return (node_trav(el, ptr->sibling, ch, val));
305 } else {
306 /* no next sibling -- mismatch */
307 val->str = NULL;
308 return (XK_STR);
314 /* node__try():
315 * Find a node that matches *str or allocate a new one
317 private int
318 node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
321 if (ptr->ch != *str) {
322 key_node_t *xm;
324 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
325 if (xm->sibling->ch == *str)
326 break;
327 if (xm->sibling == NULL)
328 xm->sibling = node__get(*str); /* setup new node */
329 ptr = xm->sibling;
331 if (*++str == '\0') {
332 /* we're there */
333 if (ptr->next != NULL) {
334 node__put(el, ptr->next);
335 /* lose longer keys with this prefix */
336 ptr->next = NULL;
338 switch (ptr->type) {
339 case XK_CMD:
340 case XK_NOD:
341 break;
342 case XK_STR:
343 case XK_EXE:
344 if (ptr->val.str)
345 el_free((ptr_t) ptr->val.str);
346 break;
347 default:
348 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
349 ptr->type));
350 break;
353 switch (ptr->type = ntype) {
354 case XK_CMD:
355 ptr->val = *val;
356 break;
357 case XK_STR:
358 case XK_EXE:
359 ptr->val.str = strdup(val->str);
360 break;
361 default:
362 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
363 break;
365 } else {
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);
371 return (0);
375 /* node__delete():
376 * Delete node that matches str
378 private int
379 node__delete(EditLine *el, key_node_t **inptr, const char *str)
381 key_node_t *ptr;
382 key_node_t *prev_ptr = NULL;
384 ptr = *inptr;
386 if (ptr->ch != *str) {
387 key_node_t *xm;
389 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
390 if (xm->sibling->ch == *str)
391 break;
392 if (xm->sibling == NULL)
393 return (0);
394 prev_ptr = xm;
395 ptr = xm->sibling;
397 if (*++str == '\0') {
398 /* we're there */
399 if (prev_ptr == NULL)
400 *inptr = ptr->sibling;
401 else
402 prev_ptr->sibling = ptr->sibling;
403 ptr->sibling = NULL;
404 node__put(el, ptr);
405 return (1);
406 } else if (ptr->next != NULL &&
407 node__delete(el, &ptr->next, str) == 1) {
408 if (ptr->next != NULL)
409 return (0);
410 if (prev_ptr == NULL)
411 *inptr = ptr->sibling;
412 else
413 prev_ptr->sibling = ptr->sibling;
414 ptr->sibling = NULL;
415 node__put(el, ptr);
416 return (1);
417 } else {
418 return (0);
423 /* node__put():
424 * Puts a tree of nodes onto free list using free(3).
426 private void
427 node__put(EditLine *el, key_node_t *ptr)
429 if (ptr == NULL)
430 return;
432 if (ptr->next != NULL) {
433 node__put(el, ptr->next);
434 ptr->next = NULL;
436 node__put(el, ptr->sibling);
438 switch (ptr->type) {
439 case XK_CMD:
440 case XK_NOD:
441 break;
442 case XK_EXE:
443 case XK_STR:
444 if (ptr->val.str != NULL)
445 el_free((ptr_t) ptr->val.str);
446 break;
447 default:
448 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
449 break;
451 el_free((ptr_t) ptr);
455 /* node__get():
456 * Returns pointer to an key_node_t for ch.
458 private key_node_t *
459 node__get(int ch)
461 key_node_t *ptr;
463 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
464 if (ptr == NULL)
465 return NULL;
466 ptr->ch = ch;
467 ptr->type = XK_NOD;
468 ptr->val.str = NULL;
469 ptr->next = NULL;
470 ptr->sibling = NULL;
471 return (ptr);
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, 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, 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);
556 } else
557 (void) node_enum(el, ptr->next, ncnt + 1);
559 /* go to sibling if there is one */
560 if (ptr->sibling)
561 (void) node_enum(el, ptr->sibling, cnt);
562 return (0);
566 /* key_kprint():
567 * Print the specified key and its associated
568 * function specified by val
570 protected void
571 key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
573 el_bindings_t *fp;
574 char unparsbuf[EL_BUFSIZ];
575 static const char fmt[] = "%-15s-> %s\n";
577 if (val != NULL)
578 switch (ntype) {
579 case XK_STR:
580 case XK_EXE:
581 (void) fprintf(el->el_outfile, fmt, key,
582 key__decode_str(val->str, unparsbuf,
583 ntype == XK_STR ? "\"\"" : "[]"));
584 break;
585 case XK_CMD:
586 for (fp = el->el_map.help; fp->name; fp++)
587 if (val->cmd == fp->func) {
588 (void) fprintf(el->el_outfile, fmt,
589 key, fp->name);
590 break;
592 #ifdef DEBUG_KEY
593 if (fp->name == NULL)
594 (void) fprintf(el->el_outfile,
595 "BUG! Command not found.\n");
596 #endif
598 break;
599 default:
600 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
601 break;
603 else
604 (void) fprintf(el->el_outfile, fmt, key, "no input");
608 /* key__decode_char():
609 * Put a printable form of char in buf.
611 private int
612 key__decode_char(char *buf, int cnt, int ch)
614 if (ch == 0) {
615 buf[cnt++] = '^';
616 buf[cnt] = '@';
617 return (cnt);
619 if (iscntrl(ch)) {
620 buf[cnt++] = '^';
621 if (ch == '\177')
622 buf[cnt] = '?';
623 else
624 buf[cnt] = ch | 0100;
625 } else if (ch == '^') {
626 buf[cnt++] = '\\';
627 buf[cnt] = '^';
628 } else if (ch == '\\') {
629 buf[cnt++] = '\\';
630 buf[cnt] = '\\';
631 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
632 buf[cnt] = ch;
633 } else {
634 buf[cnt++] = '\\';
635 buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
636 buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
637 buf[cnt] = (ch & 7) + '0';
639 return (cnt);
643 /* key__decode_str():
644 * Make a printable version of the ey
646 protected char *
647 key__decode_str(const char *str, char *buf, const char *sep)
649 char *b;
650 const char *p;
652 b = buf;
653 if (sep[0] != '\0')
654 *b++ = sep[0];
655 if (*str == 0) {
656 *b++ = '^';
657 *b++ = '@';
658 if (sep[0] != '\0' && sep[1] != '\0')
659 *b++ = sep[1];
660 *b++ = 0;
661 return (buf);
663 for (p = str; *p != 0; p++) {
664 if (iscntrl((unsigned char) *p)) {
665 *b++ = '^';
666 if (*p == '\177')
667 *b++ = '?';
668 else
669 *b++ = *p | 0100;
670 } else if (*p == '^' || *p == '\\') {
671 *b++ = '\\';
672 *b++ = *p;
673 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
674 !isspace((unsigned char) *p))) {
675 *b++ = *p;
676 } else {
677 *b++ = '\\';
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')
684 *b++ = sep[1];
685 *b++ = 0;
686 return (buf); /* should check for overflow */