1 /* $NetBSD: keymacro.c,v 1.21 2016/04/18 17:01:19 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
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. 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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: keymacro.c,v 1.21 2016/04/18 17:01:19 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
43
44 /*
45 * keymacro.c: This module contains the procedures for maintaining
46 * the extended-key map.
47 *
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).
54 *
55 * Warning:
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.
60 *
61 * Restrictions:
62 * -------------
63 * 1) It is not possible to have one key that is a
64 * substr of another.
65 */
66 #include <stdlib.h>
67 #include <string.h>
68
69 #include "el.h"
70 #include "fcns.h"
71
72 /*
73 * The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a
74 * linked list of these node elements
75 */
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*/
83 };
84
85 static int node_trav(EditLine *, keymacro_node_t *, wchar_t *,
86 keymacro_value_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 **,
93 const wchar_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);
97
98 #define KEY_BUFSIZ EL_BUFSIZ
99
100
101 /* keymacro_init():
102 * Initialize the key maps
103 */
104 protected int
keymacro_init(EditLine * el)105 keymacro_init(EditLine *el)
106 {
107
108 el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
109 sizeof(*el->el_keymacro.buf));
110 if (el->el_keymacro.buf == NULL)
111 return -1;
112 el->el_keymacro.map = NULL;
113 keymacro_reset(el);
114 return 0;
115 }
116
117 /* keymacro_end():
118 * Free the key maps
119 */
120 protected void
keymacro_end(EditLine * el)121 keymacro_end(EditLine *el)
122 {
123
124 el_free(el->el_keymacro.buf);
125 el->el_keymacro.buf = NULL;
126 node__free(el->el_keymacro.map);
127 }
128
129
130 /* keymacro_map_cmd():
131 * Associate cmd with a key value
132 */
133 protected keymacro_value_t *
keymacro_map_cmd(EditLine * el,int cmd)134 keymacro_map_cmd(EditLine *el, int cmd)
135 {
136
137 el->el_keymacro.val.cmd = (el_action_t) cmd;
138 return &el->el_keymacro.val;
139 }
140
141
142 /* keymacro_map_str():
143 * Associate str with a key value
144 */
145 protected keymacro_value_t *
keymacro_map_str(EditLine * el,wchar_t * str)146 keymacro_map_str(EditLine *el, wchar_t *str)
147 {
148
149 el->el_keymacro.val.str = str;
150 return &el->el_keymacro.val;
151 }
152
153
154 /* keymacro_reset():
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?]
158 */
159 protected void
keymacro_reset(EditLine * el)160 keymacro_reset(EditLine *el)
161 {
162
163 node__put(el, el->el_keymacro.map);
164 el->el_keymacro.map = NULL;
165 return;
166 }
167
168
169 /* keymacro_get():
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 * The last character read is returned in *ch.
176 */
177 protected int
keymacro_get(EditLine * el,wchar_t * ch,keymacro_value_t * val)178 keymacro_get(EditLine *el, wchar_t *ch, keymacro_value_t *val)
179 {
180
181 return node_trav(el, el->el_keymacro.map, ch, val);
182 }
183
184
185 /* keymacro_add():
186 * Adds key to the el->el_keymacro.map and associates the value in
187 * val with it. If key is already is in el->el_keymacro.map, the new
188 * code is applied to the existing key. Ntype specifies if code is a
189 * command, an out str or a unix command.
190 */
191 protected void
keymacro_add(EditLine * el,const wchar_t * key,keymacro_value_t * val,int ntype)192 keymacro_add(EditLine *el, const wchar_t *key, keymacro_value_t *val,
193 int ntype)
194 {
195
196 if (key[0] == '\0') {
197 (void) fprintf(el->el_errfile,
198 "keymacro_add: Null extended-key not allowed.\n");
199 return;
200 }
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");
204 return;
205 }
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 */
210
211 /* Now recurse through el->el_keymacro.map */
212 (void) node__try(el, el->el_keymacro.map, key, val, ntype);
213 return;
214 }
215
216
217 /* keymacro_clear():
218 *
219 */
220 protected void
keymacro_clear(EditLine * el,el_action_t * map,const wchar_t * in)221 keymacro_clear(EditLine *el, el_action_t *map, const wchar_t *in)
222 {
223 if (*in > N_KEYS) /* can't be in the map */
224 return;
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) keymacro_delete(el, in);
231 }
232
233
234 /* keymacro_delete():
235 * Delete the key and all longer keys staring with key, if
236 * they exists.
237 */
238 protected int
keymacro_delete(EditLine * el,const wchar_t * key)239 keymacro_delete(EditLine *el, const wchar_t *key)
240 {
241
242 if (key[0] == '\0') {
243 (void) fprintf(el->el_errfile,
244 "keymacro_delete: Null extended-key not allowed.\n");
245 return -1;
246 }
247 if (el->el_keymacro.map == NULL)
248 return 0;
249
250 (void) node__delete(el, &el->el_keymacro.map, key);
251 return 0;
252 }
253
254
255 /* keymacro_print():
256 * Print the binding associated with key key.
257 * Print entire el->el_keymacro.map if null
258 */
259 protected void
keymacro_print(EditLine * el,const wchar_t * key)260 keymacro_print(EditLine *el, const wchar_t *key)
261 {
262
263 /* do nothing if el->el_keymacro.map is empty and null key specified */
264 if (el->el_keymacro.map == NULL && *key == 0)
265 return;
266
267 el->el_keymacro.buf[0] = '"';
268 if (node_lookup(el, key, el->el_keymacro.map, (size_t)1) <= -1)
269 /* key is not bound */
270 (void) fprintf(el->el_errfile, "Unbound extended key \"%ls"
271 "\"\n", key);
272 return;
273 }
274
275
276 /* node_trav():
277 * recursively traverses node in tree until match or mismatch is
278 * found. May read in more characters.
279 */
280 static int
node_trav(EditLine * el,keymacro_node_t * ptr,wchar_t * ch,keymacro_value_t * val)281 node_trav(EditLine *el, keymacro_node_t *ptr, wchar_t *ch,
282 keymacro_value_t *val)
283 {
284
285 if (ptr->ch == *ch) {
286 /* match found */
287 if (ptr->next) {
288 /* key not complete so get next char */
289 if (el_wgetc(el, ch) != 1) {/* if EOF or error */
290 val->cmd = ED_END_OF_FILE;
291 return XK_CMD;
292 /* PWP: Pretend we just read an end-of-file */
293 }
294 return node_trav(el, ptr->next, ch, val);
295 } else {
296 *val = ptr->val;
297 if (ptr->type != XK_CMD)
298 *ch = '\0';
299 return ptr->type;
300 }
301 } else {
302 /* no match found here */
303 if (ptr->sibling) {
304 /* try next sibling */
305 return node_trav(el, ptr->sibling, ch, val);
306 } else {
307 /* no next sibling -- mismatch */
308 val->str = NULL;
309 return XK_STR;
310 }
311 }
312 }
313
314
315 /* node__try():
316 * Find a node that matches *str or allocate a new one
317 */
318 static int
node__try(EditLine * el,keymacro_node_t * ptr,const wchar_t * str,keymacro_value_t * val,int ntype)319 node__try(EditLine *el, keymacro_node_t *ptr, const wchar_t *str,
320 keymacro_value_t *val, int ntype)
321 {
322
323 if (ptr->ch != *str) {
324 keymacro_node_t *xm;
325
326 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
327 if (xm->sibling->ch == *str)
328 break;
329 if (xm->sibling == NULL)
330 xm->sibling = node__get(*str); /* setup new node */
331 ptr = xm->sibling;
332 }
333 if (*++str == '\0') {
334 /* we're there */
335 if (ptr->next != NULL) {
336 node__put(el, ptr->next);
337 /* lose longer keys with this prefix */
338 ptr->next = NULL;
339 }
340 switch (ptr->type) {
341 case XK_CMD:
342 case XK_NOD:
343 break;
344 case XK_STR:
345 if (ptr->val.str)
346 el_free(ptr->val.str);
347 break;
348 default:
349 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
350 ptr->type));
351 break;
352 }
353
354 switch (ptr->type = ntype) {
355 case XK_CMD:
356 ptr->val = *val;
357 break;
358 case XK_STR:
359 if ((ptr->val.str = wcsdup(val->str)) == NULL)
360 return -1;
361 break;
362 default:
363 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
364 break;
365 }
366 } else {
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);
371 }
372 return 0;
373 }
374
375
376 /* node__delete():
377 * Delete node that matches str
378 */
379 static int
node__delete(EditLine * el,keymacro_node_t ** inptr,const wchar_t * str)380 node__delete(EditLine *el, keymacro_node_t **inptr, const wchar_t *str)
381 {
382 keymacro_node_t *ptr;
383 keymacro_node_t *prev_ptr = NULL;
384
385 ptr = *inptr;
386
387 if (ptr->ch != *str) {
388 keymacro_node_t *xm;
389
390 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
391 if (xm->sibling->ch == *str)
392 break;
393 if (xm->sibling == NULL)
394 return 0;
395 prev_ptr = xm;
396 ptr = xm->sibling;
397 }
398 if (*++str == '\0') {
399 /* we're there */
400 if (prev_ptr == NULL)
401 *inptr = ptr->sibling;
402 else
403 prev_ptr->sibling = ptr->sibling;
404 ptr->sibling = NULL;
405 node__put(el, ptr);
406 return 1;
407 } else if (ptr->next != NULL &&
408 node__delete(el, &ptr->next, str) == 1) {
409 if (ptr->next != NULL)
410 return 0;
411 if (prev_ptr == NULL)
412 *inptr = ptr->sibling;
413 else
414 prev_ptr->sibling = ptr->sibling;
415 ptr->sibling = NULL;
416 node__put(el, ptr);
417 return 1;
418 } else {
419 return 0;
420 }
421 }
422
423
424 /* node__put():
425 * Puts a tree of nodes onto free list using free(3).
426 */
427 static void
node__put(EditLine * el,keymacro_node_t * ptr)428 node__put(EditLine *el, keymacro_node_t *ptr)
429 {
430 if (ptr == NULL)
431 return;
432
433 if (ptr->next != NULL) {
434 node__put(el, ptr->next);
435 ptr->next = NULL;
436 }
437 node__put(el, ptr->sibling);
438
439 switch (ptr->type) {
440 case XK_CMD:
441 case XK_NOD:
442 break;
443 case XK_STR:
444 if (ptr->val.str != NULL)
445 el_free(ptr->val.str);
446 break;
447 default:
448 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
449 break;
450 }
451 el_free(ptr);
452 }
453
454
455 /* node__get():
456 * Returns pointer to a keymacro_node_t for ch.
457 */
458 static keymacro_node_t *
node__get(wint_t ch)459 node__get(wint_t ch)
460 {
461 keymacro_node_t *ptr;
462
463 ptr = el_malloc(sizeof(*ptr));
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;
472 }
473
474 static void
node__free(keymacro_node_t * k)475 node__free(keymacro_node_t *k)
476 {
477 if (k == NULL)
478 return;
479 node__free(k->sibling);
480 node__free(k->next);
481 el_free(k);
482 }
483
484 /* node_lookup():
485 * look for the str starting at node ptr.
486 * Print if last node
487 */
488 static int
node_lookup(EditLine * el,const wchar_t * str,keymacro_node_t * ptr,size_t cnt)489 node_lookup(EditLine *el, const wchar_t *str, keymacro_node_t *ptr,
490 size_t cnt)
491 {
492 ssize_t used;
493
494 if (ptr == NULL)
495 return -1; /* cannot have null ptr */
496
497 if (!str || *str == 0) {
498 /* no more chars in str. node_enum from here. */
499 (void) node_enum(el, ptr, cnt);
500 return 0;
501 } else {
502 /* If match put this char into el->el_keymacro.buf. Recurse */
503 if (ptr->ch == *str) {
504 /* match found */
505 used = ct_visual_char(el->el_keymacro.buf + cnt,
506 KEY_BUFSIZ - cnt, ptr->ch);
507 if (used == -1)
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));
513 else {
514 /* next node is null so key should be complete */
515 if (str[1] == 0) {
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);
521 return 0;
522 } else
523 return -1;
524 /* mismatch -- str still has chars */
525 }
526 } else {
527 /* no match found try sibling */
528 if (ptr->sibling)
529 return (node_lookup(el, str, ptr->sibling,
530 cnt));
531 else
532 return -1;
533 }
534 }
535 }
536
537
538 /* node_enum():
539 * Traverse the node printing the characters it is bound in buffer
540 */
541 static int
node_enum(EditLine * el,keymacro_node_t * ptr,size_t cnt)542 node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
543 {
544 ssize_t used;
545
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, " \"%ls...\"\n",
552 el->el_keymacro.buf);
553 return 0;
554 }
555 if (ptr == NULL) {
556 #ifdef DEBUG_EDIT
557 (void) fprintf(el->el_errfile,
558 "node_enum: BUG!! Null ptr passed\n!");
559 #endif
560 return -1;
561 }
562 /* put this char at end of str */
563 used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
564 ptr->ch);
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);
570 } else
571 (void) node_enum(el, ptr->next, cnt + (size_t)used);
572
573 /* go to sibling if there is one */
574 if (ptr->sibling)
575 (void) node_enum(el, ptr->sibling, cnt);
576 return 0;
577 }
578
579
580 /* keymacro_kprint():
581 * Print the specified key and its associated
582 * function specified by val
583 */
584 protected void
keymacro_kprint(EditLine * el,const wchar_t * key,keymacro_value_t * val,int ntype)585 keymacro_kprint(EditLine *el, const wchar_t *key, keymacro_value_t *val,
586 int ntype)
587 {
588 el_bindings_t *fp;
589 char unparsbuf[EL_BUFSIZ];
590 static const char fmt[] = "%-15s-> %s\n";
591
592 if (val != NULL)
593 switch (ntype) {
594 case XK_STR:
595 (void) keymacro__decode_str(val->str, unparsbuf,
596 sizeof(unparsbuf),
597 ntype == XK_STR ? "\"\"" : "[]");
598 (void) fprintf(el->el_outfile, fmt,
599 ct_encode_string(key, &el->el_scratch), unparsbuf);
600 break;
601 case XK_CMD:
602 for (fp = el->el_map.help; fp->name; fp++)
603 if (val->cmd == fp->func) {
604 wcstombs(unparsbuf, fp->name, sizeof(unparsbuf));
605 unparsbuf[sizeof(unparsbuf) -1] = '\0';
606 (void) fprintf(el->el_outfile, fmt,
607 ct_encode_string(key, &el->el_scratch), unparsbuf);
608 break;
609 }
610 #ifdef DEBUG_KEY
611 if (fp->name == NULL)
612 (void) fprintf(el->el_outfile,
613 "BUG! Command not found.\n");
614 #endif
615
616 break;
617 default:
618 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
619 break;
620 }
621 else
622 (void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
623 &el->el_scratch), "no input");
624 }
625
626
627 #define ADDC(c) \
628 if (b < eb) \
629 *b++ = c; \
630 else \
631 b++
632 /* keymacro__decode_str():
633 * Make a printable version of the ey
634 */
635 protected size_t
keymacro__decode_str(const wchar_t * str,char * buf,size_t len,const char * sep)636 keymacro__decode_str(const wchar_t *str, char *buf, size_t len,
637 const char *sep)
638 {
639 char *b = buf, *eb = b + len;
640 const wchar_t *p;
641
642 b = buf;
643 if (sep[0] != '\0') {
644 ADDC(sep[0]);
645 }
646 if (*str == '\0') {
647 ADDC('^');
648 ADDC('@');
649 goto add_endsep;
650 }
651 for (p = str; *p != 0; p++) {
652 wchar_t dbuf[VISUAL_WIDTH_MAX];
653 wchar_t *p2 = dbuf;
654 ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
655 while (l-- > 0) {
656 ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
657 if (n == -1) /* ran out of space */
658 goto add_endsep;
659 else
660 b += n;
661 }
662 }
663 add_endsep:
664 if (sep[0] != '\0' && sep[1] != '\0') {
665 ADDC(sep[1]);
666 }
667 ADDC('\0');
668 if ((size_t)(b - buf) >= len)
669 buf[len - 1] = '\0';
670 return (size_t)(b - buf);
671 }
672