1 /* $NetBSD: keymacro.c,v 1.23 2016/05/24 15:00:45 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.23 2016/05/24 15:00:45 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * keymacro.c: This module contains the procedures for maintaining
46 * the extended-key map.
48 * An extended-key (key) is a sequence of keystrokes introduced
49 * with a sequence introducer and consisting of an arbitrary
50 * number of characters. This module maintains a map (the
51 * el->el_keymacro.map)
52 * to convert these extended-key sequences into input strs
53 * (XK_STR) or editor functions (XK_CMD).
56 * If key is a substr of some other keys, then the longer
57 * keys are lost!! That is, if the keys "abcd" and "abcef"
58 * are in el->el_keymacro.map, adding the key "abc" will cause
59 * the first two definitions to be lost.
63 * 1) It is not possible to have one key that is a
73 * The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a
74 * linked list of these node elements
76 struct keymacro_node_t
{
77 wchar_t ch
; /* single character of key */
78 int type
; /* node type */
79 keymacro_value_t val
; /* command code or pointer to str, */
80 /* if this is a leaf */
81 struct keymacro_node_t
*next
; /* ptr to next char of this key */
82 struct keymacro_node_t
*sibling
;/* ptr to another key with same prefix*/
85 static int node_trav(EditLine
*, keymacro_node_t
*, wchar_t *,
87 static int node__try(EditLine
*, keymacro_node_t
*,
88 const wchar_t *, keymacro_value_t
*, int);
89 static keymacro_node_t
*node__get(wint_t);
90 static void node__free(keymacro_node_t
*);
91 static void node__put(EditLine
*, keymacro_node_t
*);
92 static int node__delete(EditLine
*, keymacro_node_t
**,
94 static int node_lookup(EditLine
*, const wchar_t *,
95 keymacro_node_t
*, size_t);
96 static int node_enum(EditLine
*, keymacro_node_t
*, size_t);
98 #define KEY_BUFSIZ EL_BUFSIZ
102 * Initialize the key maps
105 keymacro_init(EditLine
*el
)
108 el
->el_keymacro
.buf
= el_malloc(KEY_BUFSIZ
*
109 sizeof(*el
->el_keymacro
.buf
));
110 if (el
->el_keymacro
.buf
== NULL
)
112 el
->el_keymacro
.map
= NULL
;
121 keymacro_end(EditLine
*el
)
124 el_free(el
->el_keymacro
.buf
);
125 el
->el_keymacro
.buf
= NULL
;
126 node__free(el
->el_keymacro
.map
);
130 /* keymacro_map_cmd():
131 * Associate cmd with a key value
133 libedit_private keymacro_value_t
*
134 keymacro_map_cmd(EditLine
*el
, int cmd
)
137 el
->el_keymacro
.val
.cmd
= (el_action_t
) cmd
;
138 return &el
->el_keymacro
.val
;
142 /* keymacro_map_str():
143 * Associate str with a key value
145 libedit_private keymacro_value_t
*
146 keymacro_map_str(EditLine
*el
, wchar_t *str
)
149 el
->el_keymacro
.val
.str
= str
;
150 return &el
->el_keymacro
.val
;
155 * Takes all nodes on el->el_keymacro.map and puts them on free list.
156 * Then initializes el->el_keymacro.map with arrow keys
157 * [Always bind the ansi arrow keys?]
160 keymacro_reset(EditLine
*el
)
163 node__put(el
, el
->el_keymacro
.map
);
164 el
->el_keymacro
.map
= NULL
;
170 * Calls the recursive function with entry point el->el_keymacro.map
171 * Looks up *ch in map and then reads characters until a
172 * complete match is found or a mismatch occurs. Returns the
173 * type of the match found (XK_STR or XK_CMD).
174 * Returns NULL in val.str and XK_STR for no match.
175 * Returns XK_NOD for end of file or read error.
176 * The last character read is returned in *ch.
179 keymacro_get(EditLine
*el
, wchar_t *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 wchar_t *key
, keymacro_value_t
*val
,
197 if (key
[0] == '\0') {
198 (void) fprintf(el
->el_errfile
,
199 "keymacro_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 "keymacro_add: sequence-lead-in command not allowed\n");
207 if (el
->el_keymacro
.map
== NULL
)
208 /* tree is initially empty. Set up new node to match key[0] */
209 el
->el_keymacro
.map
= node__get(key
[0]);
210 /* it is properly initialized */
212 /* Now recurse through el->el_keymacro.map */
213 (void) node__try(el
, el
->el_keymacro
.map
, key
, val
, ntype
);
222 keymacro_clear(EditLine
*el
, el_action_t
*map
, const wchar_t *in
)
224 if (*in
> N_KEYS
) /* can't be in the map */
226 if ((map
[(unsigned char)*in
] == ED_SEQUENCE_LEAD_IN
) &&
227 ((map
== el
->el_map
.key
&&
228 el
->el_map
.alt
[(unsigned char)*in
] != ED_SEQUENCE_LEAD_IN
) ||
229 (map
== el
->el_map
.alt
&&
230 el
->el_map
.key
[(unsigned char)*in
] != ED_SEQUENCE_LEAD_IN
)))
231 (void) keymacro_delete(el
, in
);
235 /* keymacro_delete():
236 * Delete the key and all longer keys staring with key, if
240 keymacro_delete(EditLine
*el
, const wchar_t *key
)
243 if (key
[0] == '\0') {
244 (void) fprintf(el
->el_errfile
,
245 "keymacro_delete: Null extended-key not allowed.\n");
248 if (el
->el_keymacro
.map
== NULL
)
251 (void) node__delete(el
, &el
->el_keymacro
.map
, key
);
257 * Print the binding associated with key key.
258 * Print entire el->el_keymacro.map if null
261 keymacro_print(EditLine
*el
, const wchar_t *key
)
264 /* do nothing if el->el_keymacro.map is empty and null key specified */
265 if (el
->el_keymacro
.map
== NULL
&& *key
== 0)
268 el
->el_keymacro
.buf
[0] = '"';
269 if (node_lookup(el
, key
, el
->el_keymacro
.map
, (size_t)1) <= -1)
270 /* key is not bound */
271 (void) fprintf(el
->el_errfile
, "Unbound extended key \"%ls"
278 * recursively traverses node in tree until match or mismatch is
279 * found. May read in more characters.
282 node_trav(EditLine
*el
, keymacro_node_t
*ptr
, wchar_t *ch
,
283 keymacro_value_t
*val
)
286 if (ptr
->ch
== *ch
) {
289 /* key not complete so get next char */
290 if (el_wgetc(el
, ch
) != 1)
292 return node_trav(el
, ptr
->next
, ch
, val
);
295 if (ptr
->type
!= XK_CMD
)
300 /* no match found here */
302 /* try next sibling */
303 return node_trav(el
, ptr
->sibling
, ch
, val
);
305 /* no next sibling -- mismatch */
314 * Find a node that matches *str or allocate a new one
317 node__try(EditLine
*el
, keymacro_node_t
*ptr
, const wchar_t *str
,
318 keymacro_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 */
344 el_free(ptr
->val
.str
);
347 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n",
352 switch (ptr
->type
= ntype
) {
357 if ((ptr
->val
.str
= wcsdup(val
->str
)) == NULL
)
361 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ntype
));
365 /* still more chars to go */
366 if (ptr
->next
== NULL
)
367 ptr
->next
= node__get(*str
); /* setup new node */
368 (void) node__try(el
, ptr
->next
, str
, val
, ntype
);
375 * Delete node that matches str
378 node__delete(EditLine
*el
, keymacro_node_t
**inptr
, const wchar_t *str
)
380 keymacro_node_t
*ptr
;
381 keymacro_node_t
*prev_ptr
= NULL
;
385 if (ptr
->ch
!= *str
) {
388 for (xm
= ptr
; xm
->sibling
!= NULL
; xm
= xm
->sibling
)
389 if (xm
->sibling
->ch
== *str
)
391 if (xm
->sibling
== NULL
)
396 if (*++str
== '\0') {
398 if (prev_ptr
== NULL
)
399 *inptr
= ptr
->sibling
;
401 prev_ptr
->sibling
= ptr
->sibling
;
405 } else if (ptr
->next
!= NULL
&&
406 node__delete(el
, &ptr
->next
, str
) == 1) {
407 if (ptr
->next
!= NULL
)
409 if (prev_ptr
== NULL
)
410 *inptr
= ptr
->sibling
;
412 prev_ptr
->sibling
= ptr
->sibling
;
423 * Puts a tree of nodes onto free list using free(3).
426 node__put(EditLine
*el
, keymacro_node_t
*ptr
)
431 if (ptr
->next
!= NULL
) {
432 node__put(el
, ptr
->next
);
435 node__put(el
, ptr
->sibling
);
442 if (ptr
->val
.str
!= NULL
)
443 el_free(ptr
->val
.str
);
446 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ptr
->type
));
454 * Returns pointer to a keymacro_node_t for ch.
456 static keymacro_node_t
*
459 keymacro_node_t
*ptr
;
461 ptr
= el_malloc(sizeof(*ptr
));
473 node__free(keymacro_node_t
*k
)
477 node__free(k
->sibling
);
483 * look for the str starting at node ptr.
487 node_lookup(EditLine
*el
, const wchar_t *str
, keymacro_node_t
*ptr
,
493 return -1; /* cannot have null ptr */
495 if (!str
|| *str
== 0) {
496 /* no more chars in str. node_enum from here. */
497 (void) node_enum(el
, ptr
, cnt
);
500 /* If match put this char into el->el_keymacro.buf. Recurse */
501 if (ptr
->ch
== *str
) {
503 used
= ct_visual_char(el
->el_keymacro
.buf
+ cnt
,
504 KEY_BUFSIZ
- cnt
, ptr
->ch
);
506 return -1; /* ran out of buffer space */
507 if (ptr
->next
!= NULL
)
508 /* not yet at leaf */
509 return (node_lookup(el
, str
+ 1, ptr
->next
,
510 (size_t)used
+ cnt
));
512 /* next node is null so key should be complete */
514 size_t px
= cnt
+ (size_t)used
;
515 el
->el_keymacro
.buf
[px
] = '"';
516 el
->el_keymacro
.buf
[px
+ 1] = '\0';
517 keymacro_kprint(el
, el
->el_keymacro
.buf
,
518 &ptr
->val
, ptr
->type
);
522 /* mismatch -- str still has chars */
525 /* no match found try sibling */
527 return (node_lookup(el
, str
, ptr
->sibling
,
537 * Traverse the node printing the characters it is bound in buffer
540 node_enum(EditLine
*el
, keymacro_node_t
*ptr
, size_t cnt
)
544 if (cnt
>= KEY_BUFSIZ
- 5) { /* buffer too small */
545 el
->el_keymacro
.buf
[++cnt
] = '"';
546 el
->el_keymacro
.buf
[++cnt
] = '\0';
547 (void) fprintf(el
->el_errfile
,
548 "Some extended keys too long for internal print buffer");
549 (void) fprintf(el
->el_errfile
, " \"%ls...\"\n",
550 el
->el_keymacro
.buf
);
555 (void) fprintf(el
->el_errfile
,
556 "node_enum: BUG!! Null ptr passed\n!");
560 /* put this char at end of str */
561 used
= ct_visual_char(el
->el_keymacro
.buf
+ cnt
, KEY_BUFSIZ
- cnt
,
563 if (ptr
->next
== NULL
) {
564 /* print this key and function */
565 el
->el_keymacro
.buf
[cnt
+ (size_t)used
] = '"';
566 el
->el_keymacro
.buf
[cnt
+ (size_t)used
+ 1] = '\0';
567 keymacro_kprint(el
, el
->el_keymacro
.buf
, &ptr
->val
, ptr
->type
);
569 (void) node_enum(el
, ptr
->next
, cnt
+ (size_t)used
);
571 /* go to sibling if there is one */
573 (void) node_enum(el
, ptr
->sibling
, cnt
);
578 /* keymacro_kprint():
579 * Print the specified key and its associated
580 * function specified by val
583 keymacro_kprint(EditLine
*el
, const wchar_t *key
, keymacro_value_t
*val
,
587 char unparsbuf
[EL_BUFSIZ
];
588 static const char fmt
[] = "%-15s-> %s\n";
593 (void) keymacro__decode_str(val
->str
, unparsbuf
,
595 ntype
== XK_STR
? "\"\"" : "[]");
596 (void) fprintf(el
->el_outfile
, fmt
,
597 ct_encode_string(key
, &el
->el_scratch
), unparsbuf
);
600 for (fp
= el
->el_map
.help
; fp
->name
; fp
++)
601 if (val
->cmd
== fp
->func
) {
602 wcstombs(unparsbuf
, fp
->name
, sizeof(unparsbuf
));
603 unparsbuf
[sizeof(unparsbuf
) -1] = '\0';
604 (void) fprintf(el
->el_outfile
, fmt
,
605 ct_encode_string(key
, &el
->el_scratch
), unparsbuf
);
609 if (fp
->name
== NULL
)
610 (void) fprintf(el
->el_outfile
,
611 "BUG! Command not found.\n");
616 EL_ABORT((el
->el_errfile
, "Bad XK_ type %d\n", ntype
));
620 (void) fprintf(el
->el_outfile
, fmt
, ct_encode_string(key
,
621 &el
->el_scratch
), "no input");
630 /* keymacro__decode_str():
631 * Make a printable version of the ey
633 libedit_private
size_t
634 keymacro__decode_str(const wchar_t *str
, char *buf
, size_t len
,
637 char *b
= buf
, *eb
= b
+ len
;
641 if (sep
[0] != '\0') {
649 for (p
= str
; *p
!= 0; p
++) {
650 wchar_t dbuf
[VISUAL_WIDTH_MAX
];
652 ssize_t l
= ct_visual_char(dbuf
, VISUAL_WIDTH_MAX
, *p
);
654 ssize_t n
= ct_encode_char(b
, (size_t)(eb
- b
), *p2
++);
655 if (n
== -1) /* ran out of space */
662 if (sep
[0] != '\0' && sep
[1] != '\0') {
666 if ((size_t)(b
- buf
) >= len
)
668 return (size_t)(b
- buf
);