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)
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_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)
48 * to convert these extended-key sequences into input strs
49 * (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
50 *
51 * Warning:
52 * If key is a substr of some other keys, then the longer
53 * keys are lost!! That is, if the keys "abcd" and "abcef"
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
75 */
76struct key_node_t {
77 char ch; /* single character of key */
78 int type; /* node type */
79 key_value_t val; /* command code or pointer to str, */
80 /* if this is a leaf */
81 struct key_node_t *next; /* ptr to next char of this key */
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
70 */
71struct key_node_t {
72 char ch; /* single character of key */
73 int type; /* node type */
74 key_value_t val; /* command code or pointer to str, */
75 /* if this is a leaf */
76 struct key_node_t *next; /* ptr to next char of this key */
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
101 */
102protected int
103key_init(EditLine *el)
104{
105
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
98 */
99protected int
100key_init(EditLine *el)
101{
102
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)
134{
135
136 el->el_key.val.cmd = (el_action_t) cmd;
137 return (&el->el_key.val);
138}
139
140
141/* key_map_str():
142 * Associate str with a key value
143 */
144protected key_value_t *
145key_map_str(EditLine *el, char *str)
146{
147
148 el->el_key.val.str = str;
149 return (&el->el_key.val);
150}
151
152
153/* key_reset():
154 * Takes all nodes on el->el_key.map and puts them on free list. Then
155 * initializes el->el_key.map with arrow keys
156 * [Always bind the ansi arrow keys?]
157 */
158protected void
159key_reset(EditLine *el)
160{
161
162 node__put(el, el->el_key.map);
163 el->el_key.map = NULL;
164 return;
165}
166
167
168/* key_get():
169 * Calls the recursive function with entry point el->el_key.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 */
176protected int
177key_get(EditLine *el, char *ch, key_value_t *val)
178{
179
180 return (node_trav(el, el->el_key.map, ch, val));
181}
182
183
184/* key_add():
185 * Adds key to the el->el_key.map and associates the value in val with it.
186 * If key is already is in el->el_key.map, the new code is applied to the
187 * existing key. Ntype specifies if code is a command, an
188 * out str or a unix command.
189 */
190protected void
191key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
192{
193
194 if (key[0] == '\0') {
195 (void) fprintf(el->el_errfile,
196 "key_add: Null extended-key not allowed.\n");
197 return;
198 }
199 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
200 (void) fprintf(el->el_errfile,
201 "key_add: sequence-lead-in command not allowed\n");
202 return;
203 }
204 if (el->el_key.map == NULL)
205 /* tree is initially empty. Set up new node to match key[0] */
206 el->el_key.map = node__get(key[0]);
207 /* it is properly initialized */
208
209 /* Now recurse through el->el_key.map */
210 (void) node__try(el, el->el_key.map, key, val, ntype);
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)
129{
130
131 el->el_key.val.cmd = (el_action_t) cmd;
132 return (&el->el_key.val);
133}
134
135
136/* key_map_str():
137 * Associate str with a key value
138 */
139protected key_value_t *
140key_map_str(EditLine *el, char *str)
141{
142
143 el->el_key.val.str = str;
144 return (&el->el_key.val);
145}
146
147
148/* key_reset():
149 * Takes all nodes on el->el_key.map and puts them on free list. Then
150 * initializes el->el_key.map with arrow keys
151 * [Always bind the ansi arrow keys?]
152 */
153protected void
154key_reset(EditLine *el)
155{
156
157 node__put(el, el->el_key.map);
158 el->el_key.map = NULL;
159 return;
160}
161
162
163/* key_get():
164 * Calls the recursive function with entry point el->el_key.map
165 * Looks up *ch in map and then reads characters until a
166 * complete match is found or a mismatch occurs. Returns the
167 * type of the match found (XK_STR, XK_CMD, or XK_EXE).
168 * Returns NULL in val.str and XK_STR for no match.
169 * The last character read is returned in *ch.
170 */
171protected int
172key_get(EditLine *el, char *ch, key_value_t *val)
173{
174
175 return (node_trav(el, el->el_key.map, ch, val));
176}
177
178
179/* key_add():
180 * Adds key to the el->el_key.map and associates the value in val with it.
181 * If key is already is in el->el_key.map, the new code is applied to the
182 * existing key. Ntype specifies if code is a command, an
183 * out str or a unix command.
184 */
185protected void
186key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
187{
188
189 if (key[0] == '\0') {
190 (void) fprintf(el->el_errfile,
191 "key_add: Null extended-key not allowed.\n");
192 return;
193 }
194 if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
195 (void) fprintf(el->el_errfile,
196 "key_add: sequence-lead-in command not allowed\n");
197 return;
198 }
199 if (el->el_key.map == NULL)
200 /* tree is initially empty. Set up new node to match key[0] */
201 el->el_key.map = node__get(key[0]);
202 /* it is properly initialized */
203
204 /* Now recurse through el->el_key.map */
205 (void) node__try(el, el->el_key.map, key, val, ntype);
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)
245 return (0);
246
247 (void) node__delete(el, &el->el_key.map, key);
248 return (0);
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)
240 return (0);
241
242 (void) node__delete(el, &el->el_key.map, key);
243 return (0);
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)
266 /* key is not bound */
267 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
268 key);
269 return;
270}
271
272
273/* node_trav():
274 * recursively traverses node in tree until match or mismatch is
275 * found. May read in more characters.
276 */
277private int
278node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
279{
280
281 if (ptr->ch == *ch) {
282 /* match found */
283 if (ptr->next) {
284 /* key not complete so get next char */
285 if (el_getc(el, ch) != 1) { /* if EOF or error */
286 val->cmd = ED_END_OF_FILE;
287 return (XK_CMD);
288 /* PWP: Pretend we just read an end-of-file */
289 }
290 return (node_trav(el, ptr->next, ch, val));
291 } else {
292 *val = ptr->val;
293 if (ptr->type != XK_CMD)
294 *ch = '\0';
295 return (ptr->type);
296 }
297 } else {
298 /* no match found here */
299 if (ptr->sibling) {
300 /* try next sibling */
301 return (node_trav(el, ptr->sibling, ch, val));
302 } else {
303 /* no next sibling -- mismatch */
304 val->str = NULL;
305 return (XK_STR);
306 }
307 }
308}
309
310
311/* node__try():
312 * Find a node that matches *str or allocate a new one
313 */
314private int
315node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
316{
317
318 if (ptr->ch != *str) {
319 key_node_t *xm;
320
321 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
322 if (xm->sibling->ch == *str)
323 break;
324 if (xm->sibling == NULL)
325 xm->sibling = node__get(*str); /* setup new node */
326 ptr = xm->sibling;
327 }
328 if (*++str == '\0') {
329 /* we're there */
330 if (ptr->next != NULL) {
331 node__put(el, ptr->next);
332 /* lose longer keys with this prefix */
333 ptr->next = NULL;
334 }
335 switch (ptr->type) {
336 case XK_CMD:
337 case XK_NOD:
338 break;
339 case XK_STR:
340 case XK_EXE:
341 if (ptr->val.str)
342 el_free((ptr_t) ptr->val.str);
343 break;
344 default:
345 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
346 ptr->type));
347 break;
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)
261 /* key is not bound */
262 (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
263 key);
264 return;
265}
266
267
268/* node_trav():
269 * recursively traverses node in tree until match or mismatch is
270 * found. May read in more characters.
271 */
272private int
273node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
274{
275
276 if (ptr->ch == *ch) {
277 /* match found */
278 if (ptr->next) {
279 /* key not complete so get next char */
280 if (el_getc(el, ch) != 1) { /* if EOF or error */
281 val->cmd = ED_END_OF_FILE;
282 return (XK_CMD);
283 /* PWP: Pretend we just read an end-of-file */
284 }
285 return (node_trav(el, ptr->next, ch, val));
286 } else {
287 *val = ptr->val;
288 if (ptr->type != XK_CMD)
289 *ch = '\0';
290 return (ptr->type);
291 }
292 } else {
293 /* no match found here */
294 if (ptr->sibling) {
295 /* try next sibling */
296 return (node_trav(el, ptr->sibling, ch, val));
297 } else {
298 /* no next sibling -- mismatch */
299 val->str = NULL;
300 return (XK_STR);
301 }
302 }
303}
304
305
306/* node__try():
307 * Find a node that matches *str or allocate a new one
308 */
309private int
310node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
311{
312
313 if (ptr->ch != *str) {
314 key_node_t *xm;
315
316 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
317 if (xm->sibling->ch == *str)
318 break;
319 if (xm->sibling == NULL)
320 xm->sibling = node__get(*str); /* setup new node */
321 ptr = xm->sibling;
322 }
323 if (*++str == '\0') {
324 /* we're there */
325 if (ptr->next != NULL) {
326 node__put(el, ptr->next);
327 /* lose longer keys with this prefix */
328 ptr->next = NULL;
329 }
330 switch (ptr->type) {
331 case XK_CMD:
332 case XK_NOD:
333 break;
334 case XK_STR:
335 case XK_EXE:
336 if (ptr->val.str)
337 el_free((ptr_t) ptr->val.str);
338 break;
339 default:
340 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
341 ptr->type));
342 break;
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;
385
386 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
387 if (xm->sibling->ch == *str)
388 break;
389 if (xm->sibling == NULL)
390 return (0);
391 prev_ptr = xm;
392 ptr = xm->sibling;
393 }
394 if (*++str == '\0') {
395 /* we're there */
396 if (prev_ptr == NULL)
397 *inptr = ptr->sibling;
398 else
399 prev_ptr->sibling = ptr->sibling;
400 ptr->sibling = NULL;
401 node__put(el, ptr);
402 return (1);
403 } else if (ptr->next != NULL &&
404 node__delete(el, &ptr->next, str) == 1) {
405 if (ptr->next != NULL)
406 return (0);
407 if (prev_ptr == NULL)
408 *inptr = ptr->sibling;
409 else
410 prev_ptr->sibling = ptr->sibling;
411 ptr->sibling = NULL;
412 node__put(el, ptr);
413 return (1);
414 } else {
415 return (0);
416 }
417}
418
419
420/* node__put():
421 * Puts a tree of nodes onto free list using free(3).
422 */
423private void
424node__put(EditLine *el, key_node_t *ptr)
425{
426 if (ptr == NULL)
427 return;
428
429 if (ptr->next != NULL) {
430 node__put(el, ptr->next);
431 ptr->next = NULL;
432 }
433 node__put(el, ptr->sibling);
434
435 switch (ptr->type) {
436 case XK_CMD:
437 case XK_NOD:
438 break;
439 case XK_EXE:
440 case XK_STR:
441 if (ptr->val.str != NULL)
442 el_free((ptr_t) ptr->val.str);
443 break;
444 default:
445 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
446 break;
447 }
448 el_free((ptr_t) ptr);
449}
450
451
452/* node__get():
453 * Returns pointer to a key_node_t for ch.
454 */
455private key_node_t *
456node__get(int ch)
457{
458 key_node_t *ptr;
459
460 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
461 if (ptr == NULL)
462 return NULL;
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;
381
382 for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
383 if (xm->sibling->ch == *str)
384 break;
385 if (xm->sibling == NULL)
386 return (0);
387 prev_ptr = xm;
388 ptr = xm->sibling;
389 }
390 if (*++str == '\0') {
391 /* we're there */
392 if (prev_ptr == NULL)
393 *inptr = ptr->sibling;
394 else
395 prev_ptr->sibling = ptr->sibling;
396 ptr->sibling = NULL;
397 node__put(el, ptr);
398 return (1);
399 } else if (ptr->next != NULL &&
400 node__delete(el, &ptr->next, str) == 1) {
401 if (ptr->next != NULL)
402 return (0);
403 if (prev_ptr == NULL)
404 *inptr = ptr->sibling;
405 else
406 prev_ptr->sibling = ptr->sibling;
407 ptr->sibling = NULL;
408 node__put(el, ptr);
409 return (1);
410 } else {
411 return (0);
412 }
413}
414
415
416/* node__put():
417 * Puts a tree of nodes onto free list using free(3).
418 */
419private void
420node__put(EditLine *el, key_node_t *ptr)
421{
422 if (ptr == NULL)
423 return;
424
425 if (ptr->next != NULL) {
426 node__put(el, ptr->next);
427 ptr->next = NULL;
428 }
429 node__put(el, ptr->sibling);
430
431 switch (ptr->type) {
432 case XK_CMD:
433 case XK_NOD:
434 break;
435 case XK_EXE:
436 case XK_STR:
437 if (ptr->val.str != NULL)
438 el_free((ptr_t) ptr->val.str);
439 break;
440 default:
441 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
442 break;
443 }
444 el_free((ptr_t) ptr);
445}
446
447
448/* node__get():
449 * Returns pointer to a key_node_t for ch.
450 */
451private key_node_t *
452node__get(int ch)
453{
454 key_node_t *ptr;
455
456 ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
457 if (ptr == NULL)
458 return NULL;
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. */
487 (void) node_enum(el, ptr, cnt);
488 return (0);
489 } else {
490 /* If match put this char into el->el_key.buf. Recurse */
491 if (ptr->ch == *str) {
492 /* match found */
493 ncnt = key__decode_char(el->el_key.buf, cnt,
494 (unsigned char) ptr->ch);
495 if (ptr->next != NULL)
496 /* not yet at leaf */
497 return (node_lookup(el, str + 1, ptr->next,
498 ncnt + 1));
499 else {
500 /* next node is null so key should be complete */
501 if (str[1] == 0) {
502 el->el_key.buf[ncnt + 1] = '"';
503 el->el_key.buf[ncnt + 2] = '\0';
504 key_kprint(el, el->el_key.buf,
505 &ptr->val, ptr->type);
506 return (0);
507 } else
508 return (-1);
509 /* mismatch -- str still has chars */
510 }
511 } else {
512 /* no match found try sibling */
513 if (ptr->sibling)
514 return (node_lookup(el, str, ptr->sibling,
515 cnt));
516 else
517 return (-1);
518 }
519 }
520}
521
522
523/* node_enum():
524 * Traverse the node printing the characters it is bound in buffer
525 */
526private int
527node_enum(EditLine *el, key_node_t *ptr, int cnt)
528{
529 int ncnt;
530
531 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
532 el->el_key.buf[++cnt] = '"';
533 el->el_key.buf[++cnt] = '\0';
534 (void) fprintf(el->el_errfile,
535 "Some extended keys too long for internal print buffer");
536 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
537 return (0);
538 }
539 if (ptr == NULL) {
540#ifdef DEBUG_EDIT
541 (void) fprintf(el->el_errfile,
542 "node_enum: BUG!! Null ptr passed\n!");
543#endif
544 return (-1);
545 }
546 /* put this char at end of str */
547 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
548 if (ptr->next == NULL) {
549 /* print this key and function */
550 el->el_key.buf[ncnt + 1] = '"';
551 el->el_key.buf[ncnt + 2] = '\0';
552 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
553 } else
554 (void) node_enum(el, ptr->next, ncnt + 1);
555
556 /* go to sibling if there is one */
557 if (ptr->sibling)
558 (void) node_enum(el, ptr->sibling, cnt);
559 return (0);
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. */
491 (void) node_enum(el, ptr, cnt);
492 return (0);
493 } else {
494 /* If match put this char into el->el_key.buf. Recurse */
495 if (ptr->ch == *str) {
496 /* match found */
497 ncnt = key__decode_char(el->el_key.buf, cnt,
498 (unsigned char) ptr->ch);
499 if (ptr->next != NULL)
500 /* not yet at leaf */
501 return (node_lookup(el, str + 1, ptr->next,
502 ncnt + 1));
503 else {
504 /* next node is null so key should be complete */
505 if (str[1] == 0) {
506 el->el_key.buf[ncnt + 1] = '"';
507 el->el_key.buf[ncnt + 2] = '\0';
508 key_kprint(el, el->el_key.buf,
509 &ptr->val, ptr->type);
510 return (0);
511 } else
512 return (-1);
513 /* mismatch -- str still has chars */
514 }
515 } else {
516 /* no match found try sibling */
517 if (ptr->sibling)
518 return (node_lookup(el, str, ptr->sibling,
519 cnt));
520 else
521 return (-1);
522 }
523 }
524}
525
526
527/* node_enum():
528 * Traverse the node printing the characters it is bound in buffer
529 */
530private int
531node_enum(EditLine *el, key_node_t *ptr, int cnt)
532{
533 int ncnt;
534
535 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
536 el->el_key.buf[++cnt] = '"';
537 el->el_key.buf[++cnt] = '\0';
538 (void) fprintf(el->el_errfile,
539 "Some extended keys too long for internal print buffer");
540 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
541 return (0);
542 }
543 if (ptr == NULL) {
544#ifdef DEBUG_EDIT
545 (void) fprintf(el->el_errfile,
546 "node_enum: BUG!! Null ptr passed\n!");
547#endif
548 return (-1);
549 }
550 /* put this char at end of str */
551 ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
552 if (ptr->next == NULL) {
553 /* print this key and function */
554 el->el_key.buf[ncnt + 1] = '"';
555 el->el_key.buf[ncnt + 2] = '\0';
556 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
557 } else
558 (void) node_enum(el, ptr->next, ncnt + 1);
559
560 /* go to sibling if there is one */
561 if (ptr->sibling)
562 (void) node_enum(el, ptr->sibling, cnt);
563 return (0);
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:
577 case XK_EXE:
578 (void) fprintf(el->el_outfile, fmt, key,
579 key__decode_str(val->str, unparsbuf,
580 ntype == XK_STR ? "\"\"" : "[]"));
581 break;
582 case XK_CMD:
583 for (fp = el->el_map.help; fp->name; fp++)
584 if (val->cmd == fp->func) {
585 (void) fprintf(el->el_outfile, fmt,
586 key, fp->name);
587 break;
588 }
589#ifdef DEBUG_KEY
590 if (fp->name == NULL)
591 (void) fprintf(el->el_outfile,
592 "BUG! Command not found.\n");
593#endif
594
595 break;
596 default:
597 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
598 break;
599 }
600 else
601 (void) fprintf(el->el_outfile, fmt, key, "no input");
602}
603
604
605/* key__decode_char():
606 * Put a printable form of char in buf.
607 */
608private int
609key__decode_char(char *buf, int cnt, int ch)
610{
611 ch = (unsigned char)ch;
612
613 if (ch == 0) {
614 buf[cnt++] = '^';
615 buf[cnt] = '@';
616 return (cnt);
617 }
618 if (iscntrl(ch)) {
619 buf[cnt++] = '^';
620 if (ch == 0177)
621 buf[cnt] = '?';
622 else
623 buf[cnt] = toascii(ch) | 0100;
624 } else if (ch == '^') {
625 buf[cnt++] = '\\';
626 buf[cnt] = '^';
627 } else if (ch == '\\') {
628 buf[cnt++] = '\\';
629 buf[cnt] = '\\';
630 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
631 buf[cnt] = ch;
632 } else {
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:
581 case XK_EXE:
582 (void) fprintf(el->el_outfile, fmt, key,
583 key__decode_str(val->str, unparsbuf,
584 ntype == XK_STR ? "\"\"" : "[]"));
585 break;
586 case XK_CMD:
587 for (fp = el->el_map.help; fp->name; fp++)
588 if (val->cmd == fp->func) {
589 (void) fprintf(el->el_outfile, fmt,
590 key, fp->name);
591 break;
592 }
593#ifdef DEBUG_KEY
594 if (fp->name == NULL)
595 (void) fprintf(el->el_outfile,
596 "BUG! Command not found.\n");
597#endif
598
599 break;
600 default:
601 EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
602 break;
603 }
604 else
605 (void) fprintf(el->el_outfile, fmt, key, "no input");
606}
607
608
609/* key__decode_char():
610 * Put a printable form of char in buf.
611 */
612private int
613key__decode_char(char *buf, int cnt, int ch)
614{
615 ch = (unsigned char)ch;
616
617 if (ch == 0) {
618 buf[cnt++] = '^';
619 buf[cnt] = '@';
620 return (cnt);
621 }
622 if (iscntrl(ch)) {
623 buf[cnt++] = '^';
624 if (ch == 0177)
625 buf[cnt] = '?';
626 else
627 buf[cnt] = toascii(ch) | 0100;
628 } else if (ch == '^') {
629 buf[cnt++] = '\\';
630 buf[cnt] = '^';
631 } else if (ch == '\\') {
632 buf[cnt++] = '\\';
633 buf[cnt] = '\\';
634 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
635 buf[cnt] = ch;
636 } else {
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')
656 *b++ = sep[1];
657 *b++ = 0;
658 return (buf);
659 }
660 for (p = str; *p != 0; p++) {
661 if (iscntrl((unsigned char) *p)) {
662 *b++ = '^';
663 if (*p == '\177')
664 *b++ = '?';
665 else
666 *b++ = toascii(*p) | 0100;
667 } else if (*p == '^' || *p == '\\') {
668 *b++ = '\\';
669 *b++ = *p;
670 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
671 !isspace((unsigned char) *p))) {
672 *b++ = *p;
673 } else {
674 *b++ = '\\';
675 *b++ = (((unsigned int) *p >> 6) & 7) + '0';
676 *b++ = (((unsigned int) *p >> 3) & 7) + '0';
677 *b++ = (*p & 7) + '0';
678 }
679 }
680 if (sep[0] != '\0' && sep[1] != '\0')
681 *b++ = sep[1];
682 *b++ = 0;
683 return (buf); /* should check for overflow */
684}
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')
662 *b++ = sep[1];
663 *b++ = 0;
664 return (buf);
665 }
666 for (p = str; *p != 0; p++) {
667 if (iscntrl((unsigned char) *p)) {
668 *b++ = '^';
669 if (*p == '\177')
670 *b++ = '?';
671 else
672 *b++ = toascii(*p) | 0100;
673 } else if (*p == '^' || *p == '\\') {
674 *b++ = '\\';
675 *b++ = *p;
676 } else if (*p == ' ' || (isprint((unsigned char) *p) &&
677 !isspace((unsigned char) *p))) {
678 *b++ = *p;
679 } else {
680 *b++ = '\\';
681 *b++ = (((unsigned int) *p >> 6) & 7) + '0';
682 *b++ = (((unsigned int) *p >> 3) & 7) + '0';
683 *b++ = (*p & 7) + '0';
684 }
685 }
686 if (sep[0] != '\0' && sep[1] != '\0')
687 *b++ = sep[1];
688 *b++ = 0;
689 return (buf); /* should check for overflow */
690}