keymacro.c revision 1.18 1 /* $NetBSD: keymacro.c,v 1.18 2016/04/11 00:50:13 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.18 2016/04/11 00:50:13 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), editor functions (XK_CMD), or unix commands (XK_EXE).
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
71 /*
72 * The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a
73 * linked list of these node elements
74 */
75 struct keymacro_node_t {
76 wchar_t ch; /* single character of key */
77 int type; /* node type */
78 keymacro_value_t val; /* command code or pointer to str, */
79 /* if this is a leaf */
80 struct keymacro_node_t *next; /* ptr to next char of this key */
81 struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/
82 };
83
84 private int node_trav(EditLine *, keymacro_node_t *, wchar_t *,
85 keymacro_value_t *);
86 private int node__try(EditLine *, keymacro_node_t *,
87 const wchar_t *, keymacro_value_t *, int);
88 private keymacro_node_t *node__get(wint_t);
89 private void node__free(keymacro_node_t *);
90 private void node__put(EditLine *, keymacro_node_t *);
91 private int node__delete(EditLine *, keymacro_node_t **,
92 const wchar_t *);
93 private int node_lookup(EditLine *, const wchar_t *,
94 keymacro_node_t *, size_t);
95 private int node_enum(EditLine *, keymacro_node_t *, size_t);
96
97 #define KEY_BUFSIZ EL_BUFSIZ
98
99
100 /* keymacro_init():
101 * Initialize the key maps
102 */
103 protected int
104 keymacro_init(EditLine *el)
105 {
106
107 el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
108 sizeof(*el->el_keymacro.buf));
109 if (el->el_keymacro.buf == NULL)
110 return -1;
111 el->el_keymacro.map = NULL;
112 keymacro_reset(el);
113 return 0;
114 }
115
116 /* keymacro_end():
117 * Free the key maps
118 */
119 protected void
120 keymacro_end(EditLine *el)
121 {
122
123 el_free(el->el_keymacro.buf);
124 el->el_keymacro.buf = NULL;
125 node__free(el->el_keymacro.map);
126 }
127
128
129 /* keymacro_map_cmd():
130 * Associate cmd with a key value
131 */
132 protected keymacro_value_t *
133 keymacro_map_cmd(EditLine *el, int cmd)
134 {
135
136 el->el_keymacro.val.cmd = (el_action_t) cmd;
137 return &el->el_keymacro.val;
138 }
139
140
141 /* keymacro_map_str():
142 * Associate str with a key value
143 */
144 protected keymacro_value_t *
145 keymacro_map_str(EditLine *el, wchar_t *str)
146 {
147
148 el->el_keymacro.val.str = str;
149 return &el->el_keymacro.val;
150 }
151
152
153 /* keymacro_reset():
154 * Takes all nodes on el->el_keymacro.map and puts them on free list.
155 * Then initializes el->el_keymacro.map with arrow keys
156 * [Always bind the ansi arrow keys?]
157 */
158 protected void
159 keymacro_reset(EditLine *el)
160 {
161
162 node__put(el, el->el_keymacro.map);
163 el->el_keymacro.map = NULL;
164 return;
165 }
166
167
168 /* keymacro_get():
169 * Calls the recursive function with entry point el->el_keymacro.map
170 * Looks up *ch in map and then reads characters until a
171 * complete match is found or a mismatch occurs. Returns the
172 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
173 * Returns NULL in val.str and XK_STR for no match.
174 * The last character read is returned in *ch.
175 */
176 protected int
177 keymacro_get(EditLine *el, wchar_t *ch, keymacro_value_t *val)
178 {
179
180 return node_trav(el, el->el_keymacro.map, ch, val);
181 }
182
183
184 /* keymacro_add():
185 * Adds key to the el->el_keymacro.map and associates the value in
186 * val with it. If key is already is in el->el_keymacro.map, the new
187 * code is applied to the existing key. Ntype specifies if code is a
188 * command, an out str or a unix command.
189 */
190 protected void
191 keymacro_add(EditLine *el, const wchar_t *key, keymacro_value_t *val,
192 int ntype)
193 {
194
195 if (key[0] == '\0') {
196 (void) fprintf(el->el_errfile,
197 "keymacro_add: Null extended-key not allowed.\n");
198 return;
199 }
200 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
201 (void) fprintf(el->el_errfile,
202 "keymacro_add: sequence-lead-in command not allowed\n");
203 return;
204 }
205 if (el->el_keymacro.map == NULL)
206 /* tree is initially empty. Set up new node to match key[0] */
207 el->el_keymacro.map = node__get(key[0]);
208 /* it is properly initialized */
209
210 /* Now recurse through el->el_keymacro.map */
211 (void) node__try(el, el->el_keymacro.map, key, val, ntype);
212 return;
213 }
214
215
216 /* keymacro_clear():
217 *
218 */
219 protected void
220 keymacro_clear(EditLine *el, el_action_t *map, const wchar_t *in)
221 {
222 if (*in > N_KEYS) /* can't be in the map */
223 return;
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);
230 }
231
232
233 /* keymacro_delete():
234 * Delete the key and all longer keys staring with key, if
235 * they exists.
236 */
237 protected int
238 keymacro_delete(EditLine *el, const wchar_t *key)
239 {
240
241 if (key[0] == '\0') {
242 (void) fprintf(el->el_errfile,
243 "keymacro_delete: Null extended-key not allowed.\n");
244 return -1;
245 }
246 if (el->el_keymacro.map == NULL)
247 return 0;
248
249 (void) node__delete(el, &el->el_keymacro.map, key);
250 return 0;
251 }
252
253
254 /* keymacro_print():
255 * Print the binding associated with key key.
256 * Print entire el->el_keymacro.map if null
257 */
258 protected void
259 keymacro_print(EditLine *el, const wchar_t *key)
260 {
261
262 /* do nothing if el->el_keymacro.map is empty and null key specified */
263 if (el->el_keymacro.map == NULL && *key == 0)
264 return;
265
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 \"%ls"
270 "\"\n", key);
271 return;
272 }
273
274
275 /* node_trav():
276 * recursively traverses node in tree until match or mismatch is
277 * found. May read in more characters.
278 */
279 private int
280 node_trav(EditLine *el, keymacro_node_t *ptr, wchar_t *ch,
281 keymacro_value_t *val)
282 {
283
284 if (ptr->ch == *ch) {
285 /* match found */
286 if (ptr->next) {
287 /* key not complete so get next char */
288 if (el_wgetc(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 */
292 }
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;
299 }
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;
309 }
310 }
311 }
312
313
314 /* node__try():
315 * Find a node that matches *str or allocate a new one
316 */
317 private int
318 node__try(EditLine *el, keymacro_node_t *ptr, const wchar_t *str,
319 keymacro_value_t *val, int ntype)
320 {
321
322 if (ptr->ch != *str) {
323 keymacro_node_t *xm;
324
325 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
326 if (xm->sibling->ch == *str)
327 break;
328 if (xm->sibling == NULL)
329 xm->sibling = node__get(*str); /* setup new node */
330 ptr = xm->sibling;
331 }
332 if (*++str == '\0') {
333 /* we're there */
334 if (ptr->next != NULL) {
335 node__put(el, ptr->next);
336 /* lose longer keys with this prefix */
337 ptr->next = NULL;
338 }
339 switch (ptr->type) {
340 case XK_CMD:
341 case XK_NOD:
342 break;
343 case XK_STR:
344 case XK_EXE:
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 case XK_EXE:
360 if ((ptr->val.str = wcsdup(val->str)) == NULL)
361 return -1;
362 break;
363 default:
364 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
365 break;
366 }
367 } else {
368 /* still more chars to go */
369 if (ptr->next == NULL)
370 ptr->next = node__get(*str); /* setup new node */
371 (void) node__try(el, ptr->next, str, val, ntype);
372 }
373 return 0;
374 }
375
376
377 /* node__delete():
378 * Delete node that matches str
379 */
380 private int
381 node__delete(EditLine *el, keymacro_node_t **inptr, const wchar_t *str)
382 {
383 keymacro_node_t *ptr;
384 keymacro_node_t *prev_ptr = NULL;
385
386 ptr = *inptr;
387
388 if (ptr->ch != *str) {
389 keymacro_node_t *xm;
390
391 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
392 if (xm->sibling->ch == *str)
393 break;
394 if (xm->sibling == NULL)
395 return 0;
396 prev_ptr = xm;
397 ptr = xm->sibling;
398 }
399 if (*++str == '\0') {
400 /* we're there */
401 if (prev_ptr == NULL)
402 *inptr = ptr->sibling;
403 else
404 prev_ptr->sibling = ptr->sibling;
405 ptr->sibling = NULL;
406 node__put(el, ptr);
407 return 1;
408 } else if (ptr->next != NULL &&
409 node__delete(el, &ptr->next, str) == 1) {
410 if (ptr->next != NULL)
411 return 0;
412 if (prev_ptr == NULL)
413 *inptr = ptr->sibling;
414 else
415 prev_ptr->sibling = ptr->sibling;
416 ptr->sibling = NULL;
417 node__put(el, ptr);
418 return 1;
419 } else {
420 return 0;
421 }
422 }
423
424
425 /* node__put():
426 * Puts a tree of nodes onto free list using free(3).
427 */
428 private void
429 node__put(EditLine *el, keymacro_node_t *ptr)
430 {
431 if (ptr == NULL)
432 return;
433
434 if (ptr->next != NULL) {
435 node__put(el, ptr->next);
436 ptr->next = NULL;
437 }
438 node__put(el, ptr->sibling);
439
440 switch (ptr->type) {
441 case XK_CMD:
442 case XK_NOD:
443 break;
444 case XK_EXE:
445 case XK_STR:
446 if (ptr->val.str != NULL)
447 el_free(ptr->val.str);
448 break;
449 default:
450 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
451 break;
452 }
453 el_free(ptr);
454 }
455
456
457 /* node__get():
458 * Returns pointer to a keymacro_node_t for ch.
459 */
460 private keymacro_node_t *
461 node__get(wint_t ch)
462 {
463 keymacro_node_t *ptr;
464
465 ptr = el_malloc(sizeof(*ptr));
466 if (ptr == NULL)
467 return NULL;
468 ptr->ch = ch;
469 ptr->type = XK_NOD;
470 ptr->val.str = NULL;
471 ptr->next = NULL;
472 ptr->sibling = NULL;
473 return ptr;
474 }
475
476 private void
477 node__free(keymacro_node_t *k)
478 {
479 if (k == NULL)
480 return;
481 node__free(k->sibling);
482 node__free(k->next);
483 el_free(k);
484 }
485
486 /* node_lookup():
487 * look for the str starting at node ptr.
488 * Print if last node
489 */
490 private int
491 node_lookup(EditLine *el, const wchar_t *str, keymacro_node_t *ptr,
492 size_t cnt)
493 {
494 ssize_t used;
495
496 if (ptr == NULL)
497 return -1; /* cannot have null ptr */
498
499 if (!str || *str == 0) {
500 /* no more chars in str. node_enum from here. */
501 (void) node_enum(el, ptr, cnt);
502 return 0;
503 } else {
504 /* If match put this char into el->el_keymacro.buf. Recurse */
505 if (ptr->ch == *str) {
506 /* match found */
507 used = ct_visual_char(el->el_keymacro.buf + cnt,
508 KEY_BUFSIZ - cnt, ptr->ch);
509 if (used == -1)
510 return -1; /* ran out of buffer space */
511 if (ptr->next != NULL)
512 /* not yet at leaf */
513 return (node_lookup(el, str + 1, ptr->next,
514 (size_t)used + cnt));
515 else {
516 /* next node is null so key should be complete */
517 if (str[1] == 0) {
518 size_t px = cnt + (size_t)used;
519 el->el_keymacro.buf[px] = '"';
520 el->el_keymacro.buf[px + 1] = '\0';
521 keymacro_kprint(el, el->el_keymacro.buf,
522 &ptr->val, ptr->type);
523 return 0;
524 } else
525 return -1;
526 /* mismatch -- str still has chars */
527 }
528 } else {
529 /* no match found try sibling */
530 if (ptr->sibling)
531 return (node_lookup(el, str, ptr->sibling,
532 cnt));
533 else
534 return -1;
535 }
536 }
537 }
538
539
540 /* node_enum():
541 * Traverse the node printing the characters it is bound in buffer
542 */
543 private int
544 node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
545 {
546 ssize_t used;
547
548 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
549 el->el_keymacro.buf[++cnt] = '"';
550 el->el_keymacro.buf[++cnt] = '\0';
551 (void) fprintf(el->el_errfile,
552 "Some extended keys too long for internal print buffer");
553 (void) fprintf(el->el_errfile, " \"%ls...\"\n",
554 el->el_keymacro.buf);
555 return 0;
556 }
557 if (ptr == NULL) {
558 #ifdef DEBUG_EDIT
559 (void) fprintf(el->el_errfile,
560 "node_enum: BUG!! Null ptr passed\n!");
561 #endif
562 return -1;
563 }
564 /* put this char at end of str */
565 used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
566 ptr->ch);
567 if (ptr->next == NULL) {
568 /* print this key and function */
569 el->el_keymacro.buf[cnt + (size_t)used ] = '"';
570 el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0';
571 keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
572 } else
573 (void) node_enum(el, ptr->next, cnt + (size_t)used);
574
575 /* go to sibling if there is one */
576 if (ptr->sibling)
577 (void) node_enum(el, ptr->sibling, cnt);
578 return 0;
579 }
580
581
582 /* keymacro_kprint():
583 * Print the specified key and its associated
584 * function specified by val
585 */
586 protected void
587 keymacro_kprint(EditLine *el, const wchar_t *key, keymacro_value_t *val,
588 int ntype)
589 {
590 el_bindings_t *fp;
591 char unparsbuf[EL_BUFSIZ];
592 static const char fmt[] = "%-15s-> %s\n";
593
594 if (val != NULL)
595 switch (ntype) {
596 case XK_STR:
597 case XK_EXE:
598 (void) keymacro__decode_str(val->str, unparsbuf,
599 sizeof(unparsbuf),
600 ntype == XK_STR ? "\"\"" : "[]");
601 (void) fprintf(el->el_outfile, fmt,
602 ct_encode_string(key, &el->el_scratch), unparsbuf);
603 break;
604 case XK_CMD:
605 for (fp = el->el_map.help; fp->name; fp++)
606 if (val->cmd == fp->func) {
607 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);
611 break;
612 }
613 #ifdef DEBUG_KEY
614 if (fp->name == NULL)
615 (void) fprintf(el->el_outfile,
616 "BUG! Command not found.\n");
617 #endif
618
619 break;
620 default:
621 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
622 break;
623 }
624 else
625 (void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
626 &el->el_scratch), "no input");
627 }
628
629
630 #define ADDC(c) \
631 if (b < eb) \
632 *b++ = c; \
633 else \
634 b++
635 /* keymacro__decode_str():
636 * Make a printable version of the ey
637 */
638 protected size_t
639 keymacro__decode_str(const wchar_t *str, char *buf, size_t len,
640 const char *sep)
641 {
642 char *b = buf, *eb = b + len;
643 const wchar_t *p;
644
645 b = buf;
646 if (sep[0] != '\0') {
647 ADDC(sep[0]);
648 }
649 if (*str == '\0') {
650 ADDC('^');
651 ADDC('@');
652 goto add_endsep;
653 }
654 for (p = str; *p != 0; p++) {
655 wchar_t dbuf[VISUAL_WIDTH_MAX];
656 wchar_t *p2 = dbuf;
657 ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
658 while (l-- > 0) {
659 ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
660 if (n == -1) /* ran out of space */
661 goto add_endsep;
662 else
663 b += n;
664 }
665 }
666 add_endsep:
667 if (sep[0] != '\0' && sep[1] != '\0') {
668 ADDC(sep[1]);
669 }
670 ADDC('\0');
671 if ((size_t)(b - buf) >= len)
672 buf[len - 1] = '\0';
673 return (size_t)(b - buf);
674 }
675