Deleted Added
full compact
keymacro.c (108470) keymacro.c (148834)
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
16 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
36 * $NetBSD: key.c,v 1.11 2001/01/23 15:55:30 jdolecek Exp $
32 * $NetBSD: key.c,v 1.16 2005/07/06 21:13:02 christos Exp $
37 */
38
39#if !defined(lint) && !defined(SCCSID)
40static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
41#endif /* not lint && not SCCSID */
42#include <sys/cdefs.h>
33 */
34
35#if !defined(lint) && !defined(SCCSID)
36static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
37#endif /* not lint && not SCCSID */
38#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/lib/libedit/key.c 108470 2002-12-30 21:18:15Z schweikh $");
39__FBSDID("$FreeBSD: head/lib/libedit/key.c 148834 2005-08-07 20:55:59Z stefanf $");
44
45/*
46 * key.c: This module contains the procedures for maintaining
47 * the extended-key map.
48 *
49 * An extended-key (key) is a sequence of keystrokes introduced
50 * with a sequence introducer and consisting of an arbitrary
51 * number of characters. This module maintains a map (the el->el_key.map)

--- 6 unchanged lines hidden (view full) ---

58 * are in el->el_key.map, adding the key "abc" will cause the first two
59 * 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 */
40
41/*
42 * key.c: This module contains the procedures for maintaining
43 * the extended-key map.
44 *
45 * An extended-key (key) is a sequence of keystrokes introduced
46 * with a sequence introducer and consisting of an arbitrary
47 * number of characters. This module maintains a map (the el->el_key.map)

--- 6 unchanged lines hidden (view full) ---

54 * are in el->el_key.map, adding the key "abc" will cause the first two
55 * definitions to be lost.
56 *
57 * Restrictions:
58 * -------------
59 * 1) It is not possible to have one key that is a
60 * substr of another.
61 */
66#include "sys.h"
67#include <string.h>
68#include <stdlib.h>
69
70#include "el.h"
71
72/*
73 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
74 * of these node elements

--- 7 unchanged lines hidden (view full) ---

82 struct key_node_t *sibling; /* ptr to another key with same prefix*/
83};
84
85private int node_trav(EditLine *, key_node_t *, char *,
86 key_value_t *);
87private int node__try(EditLine *, key_node_t *, const char *,
88 key_value_t *, int);
89private key_node_t *node__get(int);
62#include <string.h>
63#include <stdlib.h>
64
65#include "el.h"
66
67/*
68 * The Nodes of the el->el_key.map. The el->el_key.map is a linked list
69 * of these node elements

--- 7 unchanged lines hidden (view full) ---

77 struct key_node_t *sibling; /* ptr to another key with same prefix*/
78};
79
80private int node_trav(EditLine *, key_node_t *, char *,
81 key_value_t *);
82private int node__try(EditLine *, key_node_t *, const char *,
83 key_value_t *, int);
84private key_node_t *node__get(int);
85private void node__free(key_node_t *);
90private void node__put(EditLine *, key_node_t *);
86private void node__put(EditLine *, key_node_t *);
91private int node__delete(EditLine *, key_node_t **, char *);
92private int node_lookup(EditLine *, char *, key_node_t *, int);
87private int node__delete(EditLine *, key_node_t **, const char *);
88private int node_lookup(EditLine *, const char *, key_node_t *,
89 int);
93private int node_enum(EditLine *, key_node_t *, int);
94private int key__decode_char(char *, int, int);
95
96#define KEY_BUFSIZ EL_BUFSIZ
97
98
99/* key_init():
100 * Initialize the key maps

--- 5 unchanged lines hidden (view full) ---

106 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
107 if (el->el_key.buf == NULL)
108 return (-1);
109 el->el_key.map = NULL;
110 key_reset(el);
111 return (0);
112}
113
90private int node_enum(EditLine *, key_node_t *, int);
91private int key__decode_char(char *, int, int);
92
93#define KEY_BUFSIZ EL_BUFSIZ
94
95
96/* key_init():
97 * Initialize the key maps

--- 5 unchanged lines hidden (view full) ---

103 el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
104 if (el->el_key.buf == NULL)
105 return (-1);
106 el->el_key.map = NULL;
107 key_reset(el);
108 return (0);
109}
110
114
115/* key_end():
116 * Free the key maps
117 */
118protected void
119key_end(EditLine *el)
120{
121
122 el_free((ptr_t) el->el_key.buf);
123 el->el_key.buf = NULL;
111/* key_end():
112 * Free the key maps
113 */
114protected void
115key_end(EditLine *el)
116{
117
118 el_free((ptr_t) el->el_key.buf);
119 el->el_key.buf = NULL;
124 /* XXX: provide a function to clear the keys */
125 el->el_key.map = NULL;
120 node__free(el->el_key.map);
126}
127
128
129/* key_map_cmd():
130 * Associate cmd with a key value
131 */
132protected key_value_t *
133key_map_cmd(EditLine *el, int cmd)

--- 77 unchanged lines hidden (view full) ---

211 return;
212}
213
214
215/* key_clear():
216 *
217 */
218protected void
121}
122
123
124/* key_map_cmd():
125 * Associate cmd with a key value
126 */
127protected key_value_t *
128key_map_cmd(EditLine *el, int cmd)

--- 77 unchanged lines hidden (view full) ---

206 return;
207}
208
209
210/* key_clear():
211 *
212 */
213protected void
219key_clear(EditLine *el, el_action_t *map, char *in)
214key_clear(EditLine *el, el_action_t *map, const char *in)
220{
221
222 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
223 ((map == el->el_map.key &&
224 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
225 (map == el->el_map.alt &&
226 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
227 (void) key_delete(el, in);
228}
229
230
231/* key_delete():
232 * Delete the key and all longer keys staring with key, if
233 * they exists.
234 */
235protected int
215{
216
217 if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
218 ((map == el->el_map.key &&
219 el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
220 (map == el->el_map.alt &&
221 el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
222 (void) key_delete(el, in);
223}
224
225
226/* key_delete():
227 * Delete the key and all longer keys staring with key, if
228 * they exists.
229 */
230protected int
236key_delete(EditLine *el, char *key)
231key_delete(EditLine *el, const char *key)
237{
238
239 if (key[0] == '\0') {
240 (void) fprintf(el->el_errfile,
241 "key_delete: Null extended-key not allowed.\n");
242 return (-1);
243 }
244 if (el->el_key.map == NULL)

--- 4 unchanged lines hidden (view full) ---

249}
250
251
252/* key_print():
253 * Print the binding associated with key key.
254 * Print entire el->el_key.map if null
255 */
256protected void
232{
233
234 if (key[0] == '\0') {
235 (void) fprintf(el->el_errfile,
236 "key_delete: Null extended-key not allowed.\n");
237 return (-1);
238 }
239 if (el->el_key.map == NULL)

--- 4 unchanged lines hidden (view full) ---

244}
245
246
247/* key_print():
248 * Print the binding associated with key key.
249 * Print entire el->el_key.map if null
250 */
251protected void
257key_print(EditLine *el, char *key)
252key_print(EditLine *el, const char *key)
258{
259
260 /* do nothing if el->el_key.map is empty and null key specified */
261 if (el->el_key.map == NULL && *key == 0)
262 return;
263
264 el->el_key.buf[0] = '"';
265 if (node_lookup(el, key, el->el_key.map, 1) <= -1)

--- 82 unchanged lines hidden (view full) ---

348 }
349
350 switch (ptr->type = ntype) {
351 case XK_CMD:
352 ptr->val = *val;
353 break;
354 case XK_STR:
355 case XK_EXE:
253{
254
255 /* do nothing if el->el_key.map is empty and null key specified */
256 if (el->el_key.map == NULL && *key == 0)
257 return;
258
259 el->el_key.buf[0] = '"';
260 if (node_lookup(el, key, el->el_key.map, 1) <= -1)

--- 82 unchanged lines hidden (view full) ---

343 }
344
345 switch (ptr->type = ntype) {
346 case XK_CMD:
347 ptr->val = *val;
348 break;
349 case XK_STR:
350 case XK_EXE:
356 ptr->val.str = strdup(val->str);
351 if ((ptr->val.str = el_strdup(val->str)) == NULL)
352 return -1;
357 break;
358 default:
359 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
360 break;
361 }
362 } else {
363 /* still more chars to go */
364 if (ptr->next == NULL)
365 ptr->next = node__get(*str); /* setup new node */
366 (void) node__try(el, ptr->next, str, val, ntype);
367 }
368 return (0);
369}
370
371
372/* node__delete():
373 * Delete node that matches str
374 */
375private int
353 break;
354 default:
355 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
356 break;
357 }
358 } else {
359 /* still more chars to go */
360 if (ptr->next == NULL)
361 ptr->next = node__get(*str); /* setup new node */
362 (void) node__try(el, ptr->next, str, val, ntype);
363 }
364 return (0);
365}
366
367
368/* node__delete():
369 * Delete node that matches str
370 */
371private int
376node__delete(EditLine *el, key_node_t **inptr, char *str)
372node__delete(EditLine *el, key_node_t **inptr, const char *str)
377{
378 key_node_t *ptr;
379 key_node_t *prev_ptr = NULL;
380
381 ptr = *inptr;
382
383 if (ptr->ch != *str) {
384 key_node_t *xm;

--- 78 unchanged lines hidden (view full) ---

463 ptr->ch = ch;
464 ptr->type = XK_NOD;
465 ptr->val.str = NULL;
466 ptr->next = NULL;
467 ptr->sibling = NULL;
468 return (ptr);
469}
470
373{
374 key_node_t *ptr;
375 key_node_t *prev_ptr = NULL;
376
377 ptr = *inptr;
378
379 if (ptr->ch != *str) {
380 key_node_t *xm;

--- 78 unchanged lines hidden (view full) ---

459 ptr->ch = ch;
460 ptr->type = XK_NOD;
461 ptr->val.str = NULL;
462 ptr->next = NULL;
463 ptr->sibling = NULL;
464 return (ptr);
465}
466
467private void
468node__free(key_node_t *k)
469{
470 if (k == NULL)
471 return;
472 node__free(k->sibling);
473 node__free(k->next);
474 el_free((ptr_t) k);
475}
471
476
472
473/* node_lookup():
474 * look for the str starting at node ptr.
475 * Print if last node
476 */
477private int
477/* node_lookup():
478 * look for the str starting at node ptr.
479 * Print if last node
480 */
481private int
478node_lookup(EditLine *el, char *str, key_node_t *ptr, int cnt)
482node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt)
479{
480 int ncnt;
481
482 if (ptr == NULL)
483 return (-1); /* cannot have null ptr */
484
485 if (*str == 0) {
486 /* no more chars in str. node_enum from here. */

--- 73 unchanged lines hidden (view full) ---

560}
561
562
563/* key_kprint():
564 * Print the specified key and its associated
565 * function specified by val
566 */
567protected void
483{
484 int ncnt;
485
486 if (ptr == NULL)
487 return (-1); /* cannot have null ptr */
488
489 if (*str == 0) {
490 /* no more chars in str. node_enum from here. */

--- 73 unchanged lines hidden (view full) ---

564}
565
566
567/* key_kprint():
568 * Print the specified key and its associated
569 * function specified by val
570 */
571protected void
568key_kprint(EditLine *el, char *key, key_value_t *val, int ntype)
572key_kprint(EditLine *el, const char *key, key_value_t *val, int ntype)
569{
570 el_bindings_t *fp;
571 char unparsbuf[EL_BUFSIZ];
572 static const char fmt[] = "%-15s-> %s\n";
573
574 if (val != NULL)
575 switch (ntype) {
576 case XK_STR:

--- 56 unchanged lines hidden (view full) ---

633 buf[cnt++] = '\\';
634 buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
635 buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
636 buf[cnt] = (ch & 7) + '0';
637 }
638 return (cnt);
639}
640
573{
574 el_bindings_t *fp;
575 char unparsbuf[EL_BUFSIZ];
576 static const char fmt[] = "%-15s-> %s\n";
577
578 if (val != NULL)
579 switch (ntype) {
580 case XK_STR:

--- 56 unchanged lines hidden (view full) ---

637 buf[cnt++] = '\\';
638 buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
639 buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
640 buf[cnt] = (ch & 7) + '0';
641 }
642 return (cnt);
643}
644
645
641/* key__decode_str():
642 * Make a printable version of the ey
643 */
644protected char *
646/* key__decode_str():
647 * Make a printable version of the ey
648 */
649protected char *
645key__decode_str(char *str, char *buf, char *sep)
650key__decode_str(const char *str, char *buf, const char *sep)
646{
651{
647 char *b, *p;
652 char *b;
653 const char *p;
648
649 b = buf;
650 if (sep[0] != '\0')
651 *b++ = sep[0];
652 if (*str == 0) {
653 *b++ = '^';
654 *b++ = '@';
655 if (sep[0] != '\0' && sep[1] != '\0')

--- 29 unchanged lines hidden ---
654
655 b = buf;
656 if (sep[0] != '\0')
657 *b++ = sep[0];
658 if (*str == 0) {
659 *b++ = '^';
660 *b++ = '@';
661 if (sep[0] != '\0' && sep[1] != '\0')

--- 29 unchanged lines hidden ---