1 /* $NetBSD: keymacro.c,v 1.7 2011/08/16 16:25:15 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";
40 __RCSID("$NetBSD: keymacro.c,v 1.7 2011/08/16 16:25:15 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * keymacro.c: This module contains the procedures for maintaining
48 * the extended-key map.
50 * An extended-key (key) is a sequence of keystrokes introduced
51 * with a sequence introducer and consisting of an arbitrary
52 * number of characters. This module maintains a map (the
53 * el->el_keymacro.map)
54 * to convert these extended-key sequences into input strs
55 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
58 * If key is a substr of some other keys, then the longer
59 * keys are lost!! That is, if the keys "abcd" and "abcef"
60 * are in el->el_keymacro.map, adding the key "abc" will cause
61 * the first two definitions to be lost.
65 * 1) It is not possible to have one key that is a
74 * The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a
75 * linked list of these node elements
77 struct keymacro_node_t {
78 Char ch; /* single character of key */
79 int type; /* node type */
80 keymacro_value_t val; /* command code or pointer to str, */
81 /* if this is a leaf */
82 struct keymacro_node_t *next; /* ptr to next char of this key */
83 struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/
86 private int node_trav(EditLine *, keymacro_node_t *, Char *,
88 private int node__try(EditLine *, keymacro_node_t *, const Char *,
89 keymacro_value_t *, int);
90 private keymacro_node_t *node__get(Int);
91 private void node__free(keymacro_node_t *);
92 private void node__put(EditLine *, keymacro_node_t *);
93 private int node__delete(EditLine *, keymacro_node_t **,
95 private int node_lookup(EditLine *, const Char *,
96 keymacro_node_t *, size_t);
97 private int node_enum(EditLine *, keymacro_node_t *, size_t);
99 #define KEY_BUFSIZ EL_BUFSIZ
103 * Initialize the key maps
106 keymacro_init(EditLine *el)
109 el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
110 sizeof(*el->el_keymacro.buf));
111 if (el->el_keymacro.buf == NULL)
113 el->el_keymacro.map = NULL;
122 keymacro_end(EditLine *el)
125 el_free(el->el_keymacro.buf);
126 el->el_keymacro.buf = NULL;
127 node__free(el->el_keymacro.map);
131 /* keymacro_map_cmd():
132 * Associate cmd with a key value
134 protected keymacro_value_t *
135 keymacro_map_cmd(EditLine *el, int cmd)
138 el->el_keymacro.val.cmd = (el_action_t) cmd;
139 return &el->el_keymacro.val;
143 /* keymacro_map_str():
144 * Associate str with a key value
146 protected keymacro_value_t *
147 keymacro_map_str(EditLine *el, Char *str)
150 el->el_keymacro.val.str = str;
151 return &el->el_keymacro.val;
156 * Takes all nodes on el->el_keymacro.map and puts them on free list.
157 * Then initializes el->el_keymacro.map with arrow keys
158 * [Always bind the ansi arrow keys?]
161 keymacro_reset(EditLine *el)
164 node__put(el, el->el_keymacro.map);
165 el->el_keymacro.map = NULL;
171 * Calls the recursive function with entry point el->el_keymacro.map
172 * Looks up *ch in map and then reads characters until a
173 * complete match is found or a mismatch occurs. Returns the
174 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
175 * Returns NULL in val.str and XK_STR for no match.
176 * The last character read is returned in *ch.
179 keymacro_get(EditLine *el, Char *ch, keymacro_value_t *val)
182 return node_trav(el, el->el_keymacro.map, ch, val);
187 * Adds key to the el->el_keymacro.map and associates the value in
188 * val with it. If key is already is in el->el_keymacro.map, the new
189 * code is applied to the existing key. Ntype specifies if code is a
190 * command, an out str or a unix command.
193 keymacro_add(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
196 if (key[0] == '\0') {
197 (void) fprintf(el->el_errfile,
198 "keymacro_add: Null extended-key not allowed.\n");
201 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
202 (void) fprintf(el->el_errfile,
203 "keymacro_add: sequence-lead-in command not allowed\n");
206 if (el->el_keymacro.map == NULL)
207 /* tree is initially empty. Set up new node to match key[0] */
208 el->el_keymacro.map = node__get(key[0]);
209 /* it is properly initialized */
211 /* Now recurse through el->el_keymacro.map */
212 (void) node__try(el, el->el_keymacro.map, key, val, ntype);
221 keymacro_clear(EditLine *el, el_action_t *map, const Char *in)
224 if (*in > N_KEYS) /* can't be in the map */
227 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
228 ((map == el->el_map.key &&
229 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
230 (map == el->el_map.alt &&
231 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
232 (void) keymacro_delete(el, in);
236 /* keymacro_delete():
237 * Delete the key and all longer keys staring with key, if
241 keymacro_delete(EditLine *el, const Char *key)
244 if (key[0] == '\0') {
245 (void) fprintf(el->el_errfile,
246 "keymacro_delete: Null extended-key not allowed.\n");
249 if (el->el_keymacro.map == NULL)
252 (void) node__delete(el, &el->el_keymacro.map, key);
258 * Print the binding associated with key key.
259 * Print entire el->el_keymacro.map if null
262 keymacro_print(EditLine *el, const Char *key)
265 /* do nothing if el->el_keymacro.map is empty and null key specified */
266 if (el->el_keymacro.map == NULL && *key == 0)
269 el->el_keymacro.buf[0] = '"';
270 if (node_lookup(el, key, el->el_keymacro.map, (size_t)1) <= -1)
271 /* key is not bound */
272 (void) fprintf(el->el_errfile, "Unbound extended key \"" FSTR
279 * recursively traverses node in tree until match or mismatch is
280 * found. May read in more characters.
283 node_trav(EditLine *el, keymacro_node_t *ptr, Char *ch, keymacro_value_t *val)
286 if (ptr->ch == *ch) {
289 /* key not complete so get next char */
290 if (FUN(el,getc)(el, ch) != 1) {/* if EOF or error */
291 val->cmd = ED_END_OF_FILE;
293 /* PWP: Pretend we just read an end-of-file */
295 return node_trav(el, ptr->next, ch, val);
298 if (ptr->type != XK_CMD)
303 /* no match found here */
305 /* try next sibling */
306 return node_trav(el, ptr->sibling, ch, val);
308 /* no next sibling -- mismatch */
317 * Find a node that matches *str or allocate a new one
320 node__try(EditLine *el, keymacro_node_t *ptr, const Char *str,
321 keymacro_value_t *val, int ntype)
324 if (ptr->ch != *str) {
327 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
328 if (xm->sibling->ch == *str)
330 if (xm->sibling == NULL)
331 xm->sibling = node__get(*str); /* setup new node */
334 if (*++str == '\0') {
336 if (ptr->next != NULL) {
337 node__put(el, ptr->next);
338 /* lose longer keys with this prefix */
348 el_free(ptr->val.str);
351 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
356 switch (ptr->type = ntype) {
362 if ((ptr->val.str = Strdup(val->str)) == NULL)
366 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
370 /* still more chars to go */
371 if (ptr->next == NULL)
372 ptr->next = node__get(*str); /* setup new node */
373 (void) node__try(el, ptr->next, str, val, ntype);
380 * Delete node that matches str
383 node__delete(EditLine *el, keymacro_node_t **inptr, const Char *str)
385 keymacro_node_t *ptr;
386 keymacro_node_t *prev_ptr = NULL;
390 if (ptr->ch != *str) {
393 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
394 if (xm->sibling->ch == *str)
396 if (xm->sibling == NULL)
401 if (*++str == '\0') {
403 if (prev_ptr == NULL)
404 *inptr = ptr->sibling;
406 prev_ptr->sibling = ptr->sibling;
410 } else if (ptr->next != NULL &&
411 node__delete(el, &ptr->next, str) == 1) {
412 if (ptr->next != NULL)
414 if (prev_ptr == NULL)
415 *inptr = ptr->sibling;
417 prev_ptr->sibling = ptr->sibling;
428 * Puts a tree of nodes onto free list using free(3).
431 node__put(EditLine *el, keymacro_node_t *ptr)
436 if (ptr->next != NULL) {
437 node__put(el, ptr->next);
440 node__put(el, ptr->sibling);
448 if (ptr->val.str != NULL)
449 el_free(ptr->val.str);
452 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
460 * Returns pointer to a keymacro_node_t for ch.
462 private keymacro_node_t *
465 keymacro_node_t *ptr;
467 ptr = el_malloc(sizeof(*ptr));
479 node__free(keymacro_node_t *k)
483 node__free(k->sibling);
489 * look for the str starting at node ptr.
493 node_lookup(EditLine *el, const Char *str, keymacro_node_t *ptr, size_t cnt)
498 return -1; /* cannot have null ptr */
500 if (!str || *str == 0) {
501 /* no more chars in str. node_enum from here. */
502 (void) node_enum(el, ptr, cnt);
505 /* If match put this char into el->el_keymacro.buf. Recurse */
506 if (ptr->ch == *str) {
508 used = ct_visual_char(el->el_keymacro.buf + cnt,
509 KEY_BUFSIZ - cnt, ptr->ch);
511 return -1; /* ran out of buffer space */
512 if (ptr->next != NULL)
513 /* not yet at leaf */
514 return (node_lookup(el, str + 1, ptr->next,
515 (size_t)used + cnt));
517 /* next node is null so key should be complete */
519 size_t px = cnt + (size_t)used;
520 el->el_keymacro.buf[px] = '"';
521 el->el_keymacro.buf[px + 1] = '\0';
522 keymacro_kprint(el, el->el_keymacro.buf,
523 &ptr->val, ptr->type);
527 /* mismatch -- str still has chars */
530 /* no match found try sibling */
532 return (node_lookup(el, str, ptr->sibling,
542 * Traverse the node printing the characters it is bound in buffer
545 node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
549 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
550 el->el_keymacro.buf[++cnt] = '"';
551 el->el_keymacro.buf[++cnt] = '\0';
552 (void) fprintf(el->el_errfile,
553 "Some extended keys too long for internal print buffer");
554 (void) fprintf(el->el_errfile, " \"" FSTR "...\"\n",
555 el->el_keymacro.buf);
560 (void) fprintf(el->el_errfile,
561 "node_enum: BUG!! Null ptr passed\n!");
565 /* put this char at end of str */
566 used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
568 if (ptr->next == NULL) {
569 /* print this key and function */
570 el->el_keymacro.buf[cnt + (size_t)used ] = '"';
571 el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0';
572 keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
574 (void) node_enum(el, ptr->next, cnt + (size_t)used);
576 /* go to sibling if there is one */
578 (void) node_enum(el, ptr->sibling, cnt);
583 /* keymacro_kprint():
584 * Print the specified key and its associated
585 * function specified by val
588 keymacro_kprint(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
591 char unparsbuf[EL_BUFSIZ];
592 static const char fmt[] = "%-15s-> %s\n";
598 (void) keymacro__decode_str(val->str, unparsbuf,
600 ntype == XK_STR ? "\"\"" : "[]");
601 (void) fprintf(el->el_outfile, fmt,
602 ct_encode_string(key, &el->el_scratch), unparsbuf);
605 for (fp = el->el_map.help; fp->name; fp++)
606 if (val->cmd == fp->func) {
607 ct_wcstombs(unparsbuf, fp->name, sizeof(unparsbuf));
608 unparsbuf[sizeof(unparsbuf) -1] = '\0';
609 (void) fprintf(el->el_outfile, fmt,
610 ct_encode_string(key, &el->el_scratch), unparsbuf);
614 if (fp->name == NULL)
615 (void) fprintf(el->el_outfile,
616 "BUG! Command not found.\n");
621 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
625 (void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
626 &el->el_scratch), "no input");
635 /* keymacro__decode_str():
636 * Make a printable version of the ey
639 keymacro__decode_str(const Char *str, char *buf, size_t len, const char *sep)
641 char *b = buf, *eb = b + len;
645 if (sep[0] != '\0') {
653 for (p = str; *p != 0; p++) {
654 Char dbuf[VISUAL_WIDTH_MAX];
656 ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
658 ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
659 if (n == -1) /* ran out of space */
666 if (sep[0] != '\0' && sep[1] != '\0') {
670 if ((size_t)(b - buf) >= len)
672 return (size_t)(b - buf);