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";
41
#endif /* not lint && not SCCSID */
44
* keymacro.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
50
* el->el_keymacro.map)
51
* to convert these extended-key sequences into input strs
52
* (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
55
* If key is a substr of some other keys, then the longer
56
* keys are lost!! That is, if the keys "abcd" and "abcef"
57
* are in el->el_keymacro.map, adding the key "abc" will cause
58
* the first two definitions to be lost.
62
* 1) It is not possible to have one key that is a
71
* The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a
72
* linked list of these node elements
74
struct keymacro_node_t {
75
Char ch; /* single character of key */
76
int type; /* node type */
77
keymacro_value_t val; /* command code or pointer to str, */
78
/* if this is a leaf */
79
struct keymacro_node_t *next; /* ptr to next char of this key */
80
struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/
83
private int node_trav(EditLine *, keymacro_node_t *, Char *,
85
private int node__try(EditLine *, keymacro_node_t *, const Char *,
86
keymacro_value_t *, int);
87
private keymacro_node_t *node__get(Int);
88
private void node__free(keymacro_node_t *);
89
private void node__put(EditLine *, keymacro_node_t *);
90
private int node__delete(EditLine *, keymacro_node_t **,
92
private int node_lookup(EditLine *, const Char *,
93
keymacro_node_t *, size_t);
94
private int node_enum(EditLine *, keymacro_node_t *, size_t);
96
#define KEY_BUFSIZ EL_BUFSIZ
100
* Initialize the key maps
103
keymacro_init(EditLine *el)
106
el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
107
sizeof(*el->el_keymacro.buf));
108
if (el->el_keymacro.buf == NULL)
110
el->el_keymacro.map = NULL;
119
keymacro_end(EditLine *el)
122
el_free(el->el_keymacro.buf);
123
el->el_keymacro.buf = NULL;
124
node__free(el->el_keymacro.map);
128
/* keymacro_map_cmd():
129
* Associate cmd with a key value
131
protected keymacro_value_t *
132
keymacro_map_cmd(EditLine *el, int cmd)
135
el->el_keymacro.val.cmd = (el_action_t) cmd;
136
return &el->el_keymacro.val;
140
/* keymacro_map_str():
141
* Associate str with a key value
143
protected keymacro_value_t *
144
keymacro_map_str(EditLine *el, Char *str)
147
el->el_keymacro.val.str = str;
148
return &el->el_keymacro.val;
153
* Takes all nodes on el->el_keymacro.map and puts them on free list.
154
* Then initializes el->el_keymacro.map with arrow keys
155
* [Always bind the ansi arrow keys?]
158
keymacro_reset(EditLine *el)
161
node__put(el, el->el_keymacro.map);
162
el->el_keymacro.map = NULL;
168
* Calls the recursive function with entry point el->el_keymacro.map
169
* Looks up *ch in map and then reads characters until a
170
* complete match is found or a mismatch occurs. Returns the
171
* type of the match found (XK_STR, XK_CMD, or XK_EXE).
172
* Returns NULL in val.str and XK_STR for no match.
173
* The last character read is returned in *ch.
176
keymacro_get(EditLine *el, Char *ch, keymacro_value_t *val)
179
return node_trav(el, el->el_keymacro.map, ch, val);
184
* Adds key to the el->el_keymacro.map and associates the value in
185
* val with it. If key is already is in el->el_keymacro.map, the new
186
* code is applied to the existing key. Ntype specifies if code is a
187
* command, an out str or a unix command.
190
keymacro_add(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
193
if (key[0] == '\0') {
194
(void) fprintf(el->el_errfile,
195
"keymacro_add: Null extended-key not allowed.\n");
198
if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
199
(void) fprintf(el->el_errfile,
200
"keymacro_add: sequence-lead-in command not allowed\n");
203
if (el->el_keymacro.map == NULL)
204
/* tree is initially empty. Set up new node to match key[0] */
205
el->el_keymacro.map = node__get(key[0]);
206
/* it is properly initialized */
208
/* Now recurse through el->el_keymacro.map */
209
(void) node__try(el, el->el_keymacro.map, key, val, ntype);
218
keymacro_clear(EditLine *el, el_action_t *map, const Char *in)
221
if (*in > N_KEYS) /* can't be in the map */
224
if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
225
((map == el->el_map.key &&
226
el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
227
(map == el->el_map.alt &&
228
el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
229
(void) keymacro_delete(el, in);
233
/* keymacro_delete():
234
* Delete the key and all longer keys staring with key, if
238
keymacro_delete(EditLine *el, const Char *key)
241
if (key[0] == '\0') {
242
(void) fprintf(el->el_errfile,
243
"keymacro_delete: Null extended-key not allowed.\n");
246
if (el->el_keymacro.map == NULL)
249
(void) node__delete(el, &el->el_keymacro.map, key);
255
* Print the binding associated with key key.
256
* Print entire el->el_keymacro.map if null
259
keymacro_print(EditLine *el, const Char *key)
262
/* do nothing if el->el_keymacro.map is empty and null key specified */
263
if (el->el_keymacro.map == NULL && *key == 0)
266
el->el_keymacro.buf[0] = '"';
267
if (node_lookup(el, key, el->el_keymacro.map, (size_t)1) <= -1)
268
/* key is not bound */
269
(void) fprintf(el->el_errfile, "Unbound extended key \"" FSTR
276
* recursively traverses node in tree until match or mismatch is
277
* found. May read in more characters.
280
node_trav(EditLine *el, keymacro_node_t *ptr, Char *ch, keymacro_value_t *val)
283
if (ptr->ch == *ch) {
286
/* key not complete so get next char */
287
if (FUN(el,getc)(el, ch) != 1) {/* if EOF or error */
288
val->cmd = ED_END_OF_FILE;
290
/* PWP: Pretend we just read an end-of-file */
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 Char *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 */
345
el_free(ptr->val.str);
348
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
353
switch (ptr->type = ntype) {
359
if ((ptr->val.str = Strdup(val->str)) == NULL)
363
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
367
/* still more chars to go */
368
if (ptr->next == NULL)
369
ptr->next = node__get(*str); /* setup new node */
370
(void) node__try(el, ptr->next, str, val, ntype);
377
* Delete node that matches str
380
node__delete(EditLine *el, keymacro_node_t **inptr, const Char *str)
382
keymacro_node_t *ptr;
383
keymacro_node_t *prev_ptr = NULL;
387
if (ptr->ch != *str) {
390
for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
391
if (xm->sibling->ch == *str)
393
if (xm->sibling == NULL)
398
if (*++str == '\0') {
400
if (prev_ptr == NULL)
401
*inptr = ptr->sibling;
403
prev_ptr->sibling = ptr->sibling;
407
} else if (ptr->next != NULL &&
408
node__delete(el, &ptr->next, str) == 1) {
409
if (ptr->next != NULL)
411
if (prev_ptr == NULL)
412
*inptr = ptr->sibling;
414
prev_ptr->sibling = ptr->sibling;
425
* Puts a tree of nodes onto free list using free(3).
428
node__put(EditLine *el, keymacro_node_t *ptr)
433
if (ptr->next != NULL) {
434
node__put(el, ptr->next);
437
node__put(el, ptr->sibling);
445
if (ptr->val.str != NULL)
446
el_free(ptr->val.str);
449
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
457
* Returns pointer to a keymacro_node_t for ch.
459
private keymacro_node_t *
462
keymacro_node_t *ptr;
464
ptr = el_malloc(sizeof(*ptr));
476
node__free(keymacro_node_t *k)
480
node__free(k->sibling);
486
* look for the str starting at node ptr.
490
node_lookup(EditLine *el, const Char *str, keymacro_node_t *ptr, size_t cnt)
495
return -1; /* cannot have null ptr */
497
if (!str || *str == 0) {
498
/* no more chars in str. node_enum from here. */
499
(void) node_enum(el, ptr, cnt);
502
/* If match put this char into el->el_keymacro.buf. Recurse */
503
if (ptr->ch == *str) {
505
used = ct_visual_char(el->el_keymacro.buf + cnt,
506
KEY_BUFSIZ - cnt, ptr->ch);
508
return -1; /* ran out of buffer space */
509
if (ptr->next != NULL)
510
/* not yet at leaf */
511
return (node_lookup(el, str + 1, ptr->next,
512
(size_t)used + cnt));
514
/* next node is null so key should be complete */
516
size_t px = cnt + (size_t)used;
517
el->el_keymacro.buf[px] = '"';
518
el->el_keymacro.buf[px + 1] = '\0';
519
keymacro_kprint(el, el->el_keymacro.buf,
520
&ptr->val, ptr->type);
524
/* mismatch -- str still has chars */
527
/* no match found try sibling */
529
return (node_lookup(el, str, ptr->sibling,
539
* Traverse the node printing the characters it is bound in buffer
542
node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
546
if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
547
el->el_keymacro.buf[++cnt] = '"';
548
el->el_keymacro.buf[++cnt] = '\0';
549
(void) fprintf(el->el_errfile,
550
"Some extended keys too long for internal print buffer");
551
(void) fprintf(el->el_errfile, " \"" FSTR "...\"\n",
552
el->el_keymacro.buf);
557
(void) fprintf(el->el_errfile,
558
"node_enum: BUG!! Null ptr passed\n!");
562
/* put this char at end of str */
563
used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
565
if (ptr->next == NULL) {
566
/* print this key and function */
567
el->el_keymacro.buf[cnt + (size_t)used ] = '"';
568
el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0';
569
keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
571
(void) node_enum(el, ptr->next, cnt + (size_t)used);
573
/* go to sibling if there is one */
575
(void) node_enum(el, ptr->sibling, cnt);
580
/* keymacro_kprint():
581
* Print the specified key and its associated
582
* function specified by val
585
keymacro_kprint(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
588
char unparsbuf[EL_BUFSIZ];
589
static const char fmt[] = "%-15s-> %s\n";
592
memset(&state, 0, sizeof(mbstate_t));
597
(void) keymacro__decode_str(val->str, unparsbuf,
599
ntype == XK_STR ? "\"\"" : "[]");
600
(void) fprintf(el->el_outfile, fmt,
601
ct_encode_string(key, &el->el_scratch), unparsbuf);
604
for (fp = el->el_map.help; fp->name; fp++)
605
if (val->cmd == fp->func) {
606
memset(&state, 0, sizeof(mbstate_t));
607
wcsrtombs(unparsbuf, (const wchar_t **) &fp->name,
608
sizeof(unparsbuf), &state);
609
unparsbuf[sizeof(unparsbuf) -1] = '\0';
610
(void) fprintf(el->el_outfile, fmt,
611
ct_encode_string(key, &el->el_scratch), unparsbuf);
615
if (fp->name == NULL)
616
(void) fprintf(el->el_outfile,
617
"BUG! Command not found.\n");
622
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
626
(void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
627
&el->el_scratch), "no input");
636
/* keymacro__decode_str():
637
* Make a printable version of the ey
640
keymacro__decode_str(const Char *str, char *buf, size_t len, const char *sep)
642
char *b = buf, *eb = b + len;
646
memset(&state, 0, sizeof(mbstate_t));
648
if (sep[0] != '\0') {
656
for (p = str; *p != 0; p++) {
657
Char dbuf[VISUAL_WIDTH_MAX];
659
ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
661
ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++,
663
if (n == -1) /* ran out of space */
670
if (sep[0] != '\0' && sep[1] != '\0') {
674
if ((size_t)(b - buf) >= len)
676
return (size_t)(b - buf);