xref: /minix3/lib/libedit/keymacro.c (revision 3e1db26a5a6252fcff0898d4cb0c3fa16ccf561d)
1*3e1db26aSLionel Sambuc /*	$NetBSD: keymacro.c,v 1.7 2011/08/16 16:25:15 christos Exp $	*/
2*3e1db26aSLionel Sambuc 
3*3e1db26aSLionel Sambuc /*-
4*3e1db26aSLionel Sambuc  * Copyright (c) 1992, 1993
5*3e1db26aSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*3e1db26aSLionel Sambuc  *
7*3e1db26aSLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*3e1db26aSLionel Sambuc  * Christos Zoulas of Cornell University.
9*3e1db26aSLionel Sambuc  *
10*3e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*3e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
12*3e1db26aSLionel Sambuc  * are met:
13*3e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*3e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*3e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*3e1db26aSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*3e1db26aSLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*3e1db26aSLionel Sambuc  *    without specific prior written permission.
21*3e1db26aSLionel Sambuc  *
22*3e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*3e1db26aSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*3e1db26aSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*3e1db26aSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*3e1db26aSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*3e1db26aSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*3e1db26aSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*3e1db26aSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*3e1db26aSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*3e1db26aSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*3e1db26aSLionel Sambuc  * SUCH DAMAGE.
33*3e1db26aSLionel Sambuc  */
34*3e1db26aSLionel Sambuc 
35*3e1db26aSLionel Sambuc #include "config.h"
36*3e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*3e1db26aSLionel Sambuc #if 0
38*3e1db26aSLionel Sambuc static char sccsid[] = "@(#)key.c	8.1 (Berkeley) 6/4/93";
39*3e1db26aSLionel Sambuc #else
40*3e1db26aSLionel Sambuc __RCSID("$NetBSD: keymacro.c,v 1.7 2011/08/16 16:25:15 christos Exp $");
41*3e1db26aSLionel Sambuc #endif
42*3e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
43*3e1db26aSLionel Sambuc 
44*3e1db26aSLionel Sambuc /*
45*3e1db26aSLionel Sambuc  * keymacro.c: This module contains the procedures for maintaining
46*3e1db26aSLionel Sambuc  *	       the extended-key map.
47*3e1db26aSLionel Sambuc  *
48*3e1db26aSLionel Sambuc  *      An extended-key (key) is a sequence of keystrokes introduced
49*3e1db26aSLionel Sambuc  *	with a sequence introducer and consisting of an arbitrary
50*3e1db26aSLionel Sambuc  *	number of characters.  This module maintains a map (the
51*3e1db26aSLionel Sambuc  *	el->el_keymacro.map)
52*3e1db26aSLionel Sambuc  *	to convert these extended-key sequences into input strs
53*3e1db26aSLionel Sambuc  *	(XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
54*3e1db26aSLionel Sambuc  *
55*3e1db26aSLionel Sambuc  *      Warning:
56*3e1db26aSLionel Sambuc  *	  If key is a substr of some other keys, then the longer
57*3e1db26aSLionel Sambuc  *	  keys are lost!!  That is, if the keys "abcd" and "abcef"
58*3e1db26aSLionel Sambuc  *	  are in el->el_keymacro.map, adding the key "abc" will cause
59*3e1db26aSLionel Sambuc  *	  the first two definitions to be lost.
60*3e1db26aSLionel Sambuc  *
61*3e1db26aSLionel Sambuc  *      Restrictions:
62*3e1db26aSLionel Sambuc  *      -------------
63*3e1db26aSLionel Sambuc  *      1) It is not possible to have one key that is a
64*3e1db26aSLionel Sambuc  *	   substr of another.
65*3e1db26aSLionel Sambuc  */
66*3e1db26aSLionel Sambuc #include <string.h>
67*3e1db26aSLionel Sambuc #include <stdlib.h>
68*3e1db26aSLionel Sambuc 
69*3e1db26aSLionel Sambuc #include "el.h"
70*3e1db26aSLionel Sambuc 
71*3e1db26aSLionel Sambuc /*
72*3e1db26aSLionel Sambuc  * The Nodes of the el->el_keymacro.map.  The el->el_keymacro.map is a
73*3e1db26aSLionel Sambuc  * linked list of these node elements
74*3e1db26aSLionel Sambuc  */
75*3e1db26aSLionel Sambuc struct keymacro_node_t {
76*3e1db26aSLionel Sambuc 	Char		 ch;		/* single character of key 	 */
77*3e1db26aSLionel Sambuc 	int		 type;		/* node type			 */
78*3e1db26aSLionel Sambuc 	keymacro_value_t val;		/* command code or pointer to str,  */
79*3e1db26aSLionel Sambuc 					/* if this is a leaf 		 */
80*3e1db26aSLionel Sambuc 	struct keymacro_node_t *next;	/* ptr to next char of this key  */
81*3e1db26aSLionel Sambuc 	struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/
82*3e1db26aSLionel Sambuc };
83*3e1db26aSLionel Sambuc 
84*3e1db26aSLionel Sambuc private int		 node_trav(EditLine *, keymacro_node_t *, Char *,
85*3e1db26aSLionel Sambuc     keymacro_value_t *);
86*3e1db26aSLionel Sambuc private int		 node__try(EditLine *, keymacro_node_t *, const Char *,
87*3e1db26aSLionel Sambuc     keymacro_value_t *, int);
88*3e1db26aSLionel Sambuc private keymacro_node_t	*node__get(Int);
89*3e1db26aSLionel Sambuc private void		 node__free(keymacro_node_t *);
90*3e1db26aSLionel Sambuc private void		 node__put(EditLine *, keymacro_node_t *);
91*3e1db26aSLionel Sambuc private int		 node__delete(EditLine *, keymacro_node_t **,
92*3e1db26aSLionel Sambuc     const Char *);
93*3e1db26aSLionel Sambuc private int		 node_lookup(EditLine *, const Char *,
94*3e1db26aSLionel Sambuc     keymacro_node_t *, size_t);
95*3e1db26aSLionel Sambuc private int		 node_enum(EditLine *, keymacro_node_t *, size_t);
96*3e1db26aSLionel Sambuc 
97*3e1db26aSLionel Sambuc #define	KEY_BUFSIZ	EL_BUFSIZ
98*3e1db26aSLionel Sambuc 
99*3e1db26aSLionel Sambuc 
100*3e1db26aSLionel Sambuc /* keymacro_init():
101*3e1db26aSLionel Sambuc  *	Initialize the key maps
102*3e1db26aSLionel Sambuc  */
103*3e1db26aSLionel Sambuc protected int
keymacro_init(EditLine * el)104*3e1db26aSLionel Sambuc keymacro_init(EditLine *el)
105*3e1db26aSLionel Sambuc {
106*3e1db26aSLionel Sambuc 
107*3e1db26aSLionel Sambuc 	el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
108*3e1db26aSLionel Sambuc 	    sizeof(*el->el_keymacro.buf));
109*3e1db26aSLionel Sambuc 	if (el->el_keymacro.buf == NULL)
110*3e1db26aSLionel Sambuc 		return -1;
111*3e1db26aSLionel Sambuc 	el->el_keymacro.map = NULL;
112*3e1db26aSLionel Sambuc 	keymacro_reset(el);
113*3e1db26aSLionel Sambuc 	return 0;
114*3e1db26aSLionel Sambuc }
115*3e1db26aSLionel Sambuc 
116*3e1db26aSLionel Sambuc /* keymacro_end():
117*3e1db26aSLionel Sambuc  *	Free the key maps
118*3e1db26aSLionel Sambuc  */
119*3e1db26aSLionel Sambuc protected void
keymacro_end(EditLine * el)120*3e1db26aSLionel Sambuc keymacro_end(EditLine *el)
121*3e1db26aSLionel Sambuc {
122*3e1db26aSLionel Sambuc 
123*3e1db26aSLionel Sambuc 	el_free(el->el_keymacro.buf);
124*3e1db26aSLionel Sambuc 	el->el_keymacro.buf = NULL;
125*3e1db26aSLionel Sambuc 	node__free(el->el_keymacro.map);
126*3e1db26aSLionel Sambuc }
127*3e1db26aSLionel Sambuc 
128*3e1db26aSLionel Sambuc 
129*3e1db26aSLionel Sambuc /* keymacro_map_cmd():
130*3e1db26aSLionel Sambuc  *	Associate cmd with a key value
131*3e1db26aSLionel Sambuc  */
132*3e1db26aSLionel Sambuc protected keymacro_value_t *
keymacro_map_cmd(EditLine * el,int cmd)133*3e1db26aSLionel Sambuc keymacro_map_cmd(EditLine *el, int cmd)
134*3e1db26aSLionel Sambuc {
135*3e1db26aSLionel Sambuc 
136*3e1db26aSLionel Sambuc 	el->el_keymacro.val.cmd = (el_action_t) cmd;
137*3e1db26aSLionel Sambuc 	return &el->el_keymacro.val;
138*3e1db26aSLionel Sambuc }
139*3e1db26aSLionel Sambuc 
140*3e1db26aSLionel Sambuc 
141*3e1db26aSLionel Sambuc /* keymacro_map_str():
142*3e1db26aSLionel Sambuc  *	Associate str with a key value
143*3e1db26aSLionel Sambuc  */
144*3e1db26aSLionel Sambuc protected keymacro_value_t *
keymacro_map_str(EditLine * el,Char * str)145*3e1db26aSLionel Sambuc keymacro_map_str(EditLine *el, Char *str)
146*3e1db26aSLionel Sambuc {
147*3e1db26aSLionel Sambuc 
148*3e1db26aSLionel Sambuc 	el->el_keymacro.val.str = str;
149*3e1db26aSLionel Sambuc 	return &el->el_keymacro.val;
150*3e1db26aSLionel Sambuc }
151*3e1db26aSLionel Sambuc 
152*3e1db26aSLionel Sambuc 
153*3e1db26aSLionel Sambuc /* keymacro_reset():
154*3e1db26aSLionel Sambuc  *	Takes all nodes on el->el_keymacro.map and puts them on free list.
155*3e1db26aSLionel Sambuc  *	Then initializes el->el_keymacro.map with arrow keys
156*3e1db26aSLionel Sambuc  *	[Always bind the ansi arrow keys?]
157*3e1db26aSLionel Sambuc  */
158*3e1db26aSLionel Sambuc protected void
keymacro_reset(EditLine * el)159*3e1db26aSLionel Sambuc keymacro_reset(EditLine *el)
160*3e1db26aSLionel Sambuc {
161*3e1db26aSLionel Sambuc 
162*3e1db26aSLionel Sambuc 	node__put(el, el->el_keymacro.map);
163*3e1db26aSLionel Sambuc 	el->el_keymacro.map = NULL;
164*3e1db26aSLionel Sambuc 	return;
165*3e1db26aSLionel Sambuc }
166*3e1db26aSLionel Sambuc 
167*3e1db26aSLionel Sambuc 
168*3e1db26aSLionel Sambuc /* keymacro_get():
169*3e1db26aSLionel Sambuc  *	Calls the recursive function with entry point el->el_keymacro.map
170*3e1db26aSLionel Sambuc  *      Looks up *ch in map and then reads characters until a
171*3e1db26aSLionel Sambuc  *      complete match is found or a mismatch occurs. Returns the
172*3e1db26aSLionel Sambuc  *      type of the match found (XK_STR, XK_CMD, or XK_EXE).
173*3e1db26aSLionel Sambuc  *      Returns NULL in val.str and XK_STR for no match.
174*3e1db26aSLionel Sambuc  *      The last character read is returned in *ch.
175*3e1db26aSLionel Sambuc  */
176*3e1db26aSLionel Sambuc protected int
keymacro_get(EditLine * el,Char * ch,keymacro_value_t * val)177*3e1db26aSLionel Sambuc keymacro_get(EditLine *el, Char *ch, keymacro_value_t *val)
178*3e1db26aSLionel Sambuc {
179*3e1db26aSLionel Sambuc 
180*3e1db26aSLionel Sambuc 	return node_trav(el, el->el_keymacro.map, ch, val);
181*3e1db26aSLionel Sambuc }
182*3e1db26aSLionel Sambuc 
183*3e1db26aSLionel Sambuc 
184*3e1db26aSLionel Sambuc /* keymacro_add():
185*3e1db26aSLionel Sambuc  *      Adds key to the el->el_keymacro.map and associates the value in
186*3e1db26aSLionel Sambuc  *	val with it. If key is already is in el->el_keymacro.map, the new
187*3e1db26aSLionel Sambuc  *	code is applied to the existing key. Ntype specifies if code is a
188*3e1db26aSLionel Sambuc  *	command, an out str or a unix command.
189*3e1db26aSLionel Sambuc  */
190*3e1db26aSLionel Sambuc protected void
keymacro_add(EditLine * el,const Char * key,keymacro_value_t * val,int ntype)191*3e1db26aSLionel Sambuc keymacro_add(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
192*3e1db26aSLionel Sambuc {
193*3e1db26aSLionel Sambuc 
194*3e1db26aSLionel Sambuc 	if (key[0] == '\0') {
195*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile,
196*3e1db26aSLionel Sambuc 		    "keymacro_add: Null extended-key not allowed.\n");
197*3e1db26aSLionel Sambuc 		return;
198*3e1db26aSLionel Sambuc 	}
199*3e1db26aSLionel Sambuc 	if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
200*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile,
201*3e1db26aSLionel Sambuc 		    "keymacro_add: sequence-lead-in command not allowed\n");
202*3e1db26aSLionel Sambuc 		return;
203*3e1db26aSLionel Sambuc 	}
204*3e1db26aSLionel Sambuc 	if (el->el_keymacro.map == NULL)
205*3e1db26aSLionel Sambuc 		/* tree is initially empty.  Set up new node to match key[0] */
206*3e1db26aSLionel Sambuc 		el->el_keymacro.map = node__get(key[0]);
207*3e1db26aSLionel Sambuc 			/* it is properly initialized */
208*3e1db26aSLionel Sambuc 
209*3e1db26aSLionel Sambuc 	/* Now recurse through el->el_keymacro.map */
210*3e1db26aSLionel Sambuc 	(void) node__try(el, el->el_keymacro.map, key, val, ntype);
211*3e1db26aSLionel Sambuc 	return;
212*3e1db26aSLionel Sambuc }
213*3e1db26aSLionel Sambuc 
214*3e1db26aSLionel Sambuc 
215*3e1db26aSLionel Sambuc /* keymacro_clear():
216*3e1db26aSLionel Sambuc  *
217*3e1db26aSLionel Sambuc  */
218*3e1db26aSLionel Sambuc protected void
keymacro_clear(EditLine * el,el_action_t * map,const Char * in)219*3e1db26aSLionel Sambuc keymacro_clear(EditLine *el, el_action_t *map, const Char *in)
220*3e1db26aSLionel Sambuc {
221*3e1db26aSLionel Sambuc #ifdef WIDECHAR
222*3e1db26aSLionel Sambuc         if (*in > N_KEYS) /* can't be in the map */
223*3e1db26aSLionel Sambuc                 return;
224*3e1db26aSLionel Sambuc #endif
225*3e1db26aSLionel Sambuc 	if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
226*3e1db26aSLionel Sambuc 	    ((map == el->el_map.key &&
227*3e1db26aSLionel Sambuc 	    el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
228*3e1db26aSLionel Sambuc 	    (map == el->el_map.alt &&
229*3e1db26aSLionel Sambuc 	    el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
230*3e1db26aSLionel Sambuc 		(void) keymacro_delete(el, in);
231*3e1db26aSLionel Sambuc }
232*3e1db26aSLionel Sambuc 
233*3e1db26aSLionel Sambuc 
234*3e1db26aSLionel Sambuc /* keymacro_delete():
235*3e1db26aSLionel Sambuc  *      Delete the key and all longer keys staring with key, if
236*3e1db26aSLionel Sambuc  *      they exists.
237*3e1db26aSLionel Sambuc  */
238*3e1db26aSLionel Sambuc protected int
keymacro_delete(EditLine * el,const Char * key)239*3e1db26aSLionel Sambuc keymacro_delete(EditLine *el, const Char *key)
240*3e1db26aSLionel Sambuc {
241*3e1db26aSLionel Sambuc 
242*3e1db26aSLionel Sambuc 	if (key[0] == '\0') {
243*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile,
244*3e1db26aSLionel Sambuc 		    "keymacro_delete: Null extended-key not allowed.\n");
245*3e1db26aSLionel Sambuc 		return -1;
246*3e1db26aSLionel Sambuc 	}
247*3e1db26aSLionel Sambuc 	if (el->el_keymacro.map == NULL)
248*3e1db26aSLionel Sambuc 		return 0;
249*3e1db26aSLionel Sambuc 
250*3e1db26aSLionel Sambuc 	(void) node__delete(el, &el->el_keymacro.map, key);
251*3e1db26aSLionel Sambuc 	return 0;
252*3e1db26aSLionel Sambuc }
253*3e1db26aSLionel Sambuc 
254*3e1db26aSLionel Sambuc 
255*3e1db26aSLionel Sambuc /* keymacro_print():
256*3e1db26aSLionel Sambuc  *	Print the binding associated with key key.
257*3e1db26aSLionel Sambuc  *	Print entire el->el_keymacro.map if null
258*3e1db26aSLionel Sambuc  */
259*3e1db26aSLionel Sambuc protected void
keymacro_print(EditLine * el,const Char * key)260*3e1db26aSLionel Sambuc keymacro_print(EditLine *el, const Char *key)
261*3e1db26aSLionel Sambuc {
262*3e1db26aSLionel Sambuc 
263*3e1db26aSLionel Sambuc 	/* do nothing if el->el_keymacro.map is empty and null key specified */
264*3e1db26aSLionel Sambuc 	if (el->el_keymacro.map == NULL && *key == 0)
265*3e1db26aSLionel Sambuc 		return;
266*3e1db26aSLionel Sambuc 
267*3e1db26aSLionel Sambuc 	el->el_keymacro.buf[0] = '"';
268*3e1db26aSLionel Sambuc 	if (node_lookup(el, key, el->el_keymacro.map, (size_t)1) <= -1)
269*3e1db26aSLionel Sambuc 		/* key is not bound */
270*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile, "Unbound extended key \"" FSTR
271*3e1db26aSLionel Sambuc 		    "\"\n", key);
272*3e1db26aSLionel Sambuc 	return;
273*3e1db26aSLionel Sambuc }
274*3e1db26aSLionel Sambuc 
275*3e1db26aSLionel Sambuc 
276*3e1db26aSLionel Sambuc /* node_trav():
277*3e1db26aSLionel Sambuc  *	recursively traverses node in tree until match or mismatch is
278*3e1db26aSLionel Sambuc  * 	found.  May read in more characters.
279*3e1db26aSLionel Sambuc  */
280*3e1db26aSLionel Sambuc private int
node_trav(EditLine * el,keymacro_node_t * ptr,Char * ch,keymacro_value_t * val)281*3e1db26aSLionel Sambuc node_trav(EditLine *el, keymacro_node_t *ptr, Char *ch, keymacro_value_t *val)
282*3e1db26aSLionel Sambuc {
283*3e1db26aSLionel Sambuc 
284*3e1db26aSLionel Sambuc 	if (ptr->ch == *ch) {
285*3e1db26aSLionel Sambuc 		/* match found */
286*3e1db26aSLionel Sambuc 		if (ptr->next) {
287*3e1db26aSLionel Sambuc 			/* key not complete so get next char */
288*3e1db26aSLionel Sambuc 			if (FUN(el,getc)(el, ch) != 1) {/* if EOF or error */
289*3e1db26aSLionel Sambuc 				val->cmd = ED_END_OF_FILE;
290*3e1db26aSLionel Sambuc 				return XK_CMD;
291*3e1db26aSLionel Sambuc 				/* PWP: Pretend we just read an end-of-file */
292*3e1db26aSLionel Sambuc 			}
293*3e1db26aSLionel Sambuc 			return node_trav(el, ptr->next, ch, val);
294*3e1db26aSLionel Sambuc 		} else {
295*3e1db26aSLionel Sambuc 			*val = ptr->val;
296*3e1db26aSLionel Sambuc 			if (ptr->type != XK_CMD)
297*3e1db26aSLionel Sambuc 				*ch = '\0';
298*3e1db26aSLionel Sambuc 			return ptr->type;
299*3e1db26aSLionel Sambuc 		}
300*3e1db26aSLionel Sambuc 	} else {
301*3e1db26aSLionel Sambuc 		/* no match found here */
302*3e1db26aSLionel Sambuc 		if (ptr->sibling) {
303*3e1db26aSLionel Sambuc 			/* try next sibling */
304*3e1db26aSLionel Sambuc 			return node_trav(el, ptr->sibling, ch, val);
305*3e1db26aSLionel Sambuc 		} else {
306*3e1db26aSLionel Sambuc 			/* no next sibling -- mismatch */
307*3e1db26aSLionel Sambuc 			val->str = NULL;
308*3e1db26aSLionel Sambuc 			return XK_STR;
309*3e1db26aSLionel Sambuc 		}
310*3e1db26aSLionel Sambuc 	}
311*3e1db26aSLionel Sambuc }
312*3e1db26aSLionel Sambuc 
313*3e1db26aSLionel Sambuc 
314*3e1db26aSLionel Sambuc /* node__try():
315*3e1db26aSLionel Sambuc  * 	Find a node that matches *str or allocate a new one
316*3e1db26aSLionel Sambuc  */
317*3e1db26aSLionel Sambuc private int
node__try(EditLine * el,keymacro_node_t * ptr,const Char * str,keymacro_value_t * val,int ntype)318*3e1db26aSLionel Sambuc node__try(EditLine *el, keymacro_node_t *ptr, const Char *str,
319*3e1db26aSLionel Sambuc     keymacro_value_t *val, int ntype)
320*3e1db26aSLionel Sambuc {
321*3e1db26aSLionel Sambuc 
322*3e1db26aSLionel Sambuc 	if (ptr->ch != *str) {
323*3e1db26aSLionel Sambuc 		keymacro_node_t *xm;
324*3e1db26aSLionel Sambuc 
325*3e1db26aSLionel Sambuc 		for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
326*3e1db26aSLionel Sambuc 			if (xm->sibling->ch == *str)
327*3e1db26aSLionel Sambuc 				break;
328*3e1db26aSLionel Sambuc 		if (xm->sibling == NULL)
329*3e1db26aSLionel Sambuc 			xm->sibling = node__get(*str);	/* setup new node */
330*3e1db26aSLionel Sambuc 		ptr = xm->sibling;
331*3e1db26aSLionel Sambuc 	}
332*3e1db26aSLionel Sambuc 	if (*++str == '\0') {
333*3e1db26aSLionel Sambuc 		/* we're there */
334*3e1db26aSLionel Sambuc 		if (ptr->next != NULL) {
335*3e1db26aSLionel Sambuc 			node__put(el, ptr->next);
336*3e1db26aSLionel Sambuc 				/* lose longer keys with this prefix */
337*3e1db26aSLionel Sambuc 			ptr->next = NULL;
338*3e1db26aSLionel Sambuc 		}
339*3e1db26aSLionel Sambuc 		switch (ptr->type) {
340*3e1db26aSLionel Sambuc 		case XK_CMD:
341*3e1db26aSLionel Sambuc 		case XK_NOD:
342*3e1db26aSLionel Sambuc 			break;
343*3e1db26aSLionel Sambuc 		case XK_STR:
344*3e1db26aSLionel Sambuc 		case XK_EXE:
345*3e1db26aSLionel Sambuc 			if (ptr->val.str)
346*3e1db26aSLionel Sambuc 				el_free(ptr->val.str);
347*3e1db26aSLionel Sambuc 			break;
348*3e1db26aSLionel Sambuc 		default:
349*3e1db26aSLionel Sambuc 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
350*3e1db26aSLionel Sambuc 			    ptr->type));
351*3e1db26aSLionel Sambuc 			break;
352*3e1db26aSLionel Sambuc 		}
353*3e1db26aSLionel Sambuc 
354*3e1db26aSLionel Sambuc 		switch (ptr->type = ntype) {
355*3e1db26aSLionel Sambuc 		case XK_CMD:
356*3e1db26aSLionel Sambuc 			ptr->val = *val;
357*3e1db26aSLionel Sambuc 			break;
358*3e1db26aSLionel Sambuc 		case XK_STR:
359*3e1db26aSLionel Sambuc 		case XK_EXE:
360*3e1db26aSLionel Sambuc 			if ((ptr->val.str = Strdup(val->str)) == NULL)
361*3e1db26aSLionel Sambuc 				return -1;
362*3e1db26aSLionel Sambuc 			break;
363*3e1db26aSLionel Sambuc 		default:
364*3e1db26aSLionel Sambuc 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
365*3e1db26aSLionel Sambuc 			break;
366*3e1db26aSLionel Sambuc 		}
367*3e1db26aSLionel Sambuc 	} else {
368*3e1db26aSLionel Sambuc 		/* still more chars to go */
369*3e1db26aSLionel Sambuc 		if (ptr->next == NULL)
370*3e1db26aSLionel Sambuc 			ptr->next = node__get(*str);	/* setup new node */
371*3e1db26aSLionel Sambuc 		(void) node__try(el, ptr->next, str, val, ntype);
372*3e1db26aSLionel Sambuc 	}
373*3e1db26aSLionel Sambuc 	return 0;
374*3e1db26aSLionel Sambuc }
375*3e1db26aSLionel Sambuc 
376*3e1db26aSLionel Sambuc 
377*3e1db26aSLionel Sambuc /* node__delete():
378*3e1db26aSLionel Sambuc  *	Delete node that matches str
379*3e1db26aSLionel Sambuc  */
380*3e1db26aSLionel Sambuc private int
node__delete(EditLine * el,keymacro_node_t ** inptr,const Char * str)381*3e1db26aSLionel Sambuc node__delete(EditLine *el, keymacro_node_t **inptr, const Char *str)
382*3e1db26aSLionel Sambuc {
383*3e1db26aSLionel Sambuc 	keymacro_node_t *ptr;
384*3e1db26aSLionel Sambuc 	keymacro_node_t *prev_ptr = NULL;
385*3e1db26aSLionel Sambuc 
386*3e1db26aSLionel Sambuc 	ptr = *inptr;
387*3e1db26aSLionel Sambuc 
388*3e1db26aSLionel Sambuc 	if (ptr->ch != *str) {
389*3e1db26aSLionel Sambuc 		keymacro_node_t *xm;
390*3e1db26aSLionel Sambuc 
391*3e1db26aSLionel Sambuc 		for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
392*3e1db26aSLionel Sambuc 			if (xm->sibling->ch == *str)
393*3e1db26aSLionel Sambuc 				break;
394*3e1db26aSLionel Sambuc 		if (xm->sibling == NULL)
395*3e1db26aSLionel Sambuc 			return 0;
396*3e1db26aSLionel Sambuc 		prev_ptr = xm;
397*3e1db26aSLionel Sambuc 		ptr = xm->sibling;
398*3e1db26aSLionel Sambuc 	}
399*3e1db26aSLionel Sambuc 	if (*++str == '\0') {
400*3e1db26aSLionel Sambuc 		/* we're there */
401*3e1db26aSLionel Sambuc 		if (prev_ptr == NULL)
402*3e1db26aSLionel Sambuc 			*inptr = ptr->sibling;
403*3e1db26aSLionel Sambuc 		else
404*3e1db26aSLionel Sambuc 			prev_ptr->sibling = ptr->sibling;
405*3e1db26aSLionel Sambuc 		ptr->sibling = NULL;
406*3e1db26aSLionel Sambuc 		node__put(el, ptr);
407*3e1db26aSLionel Sambuc 		return 1;
408*3e1db26aSLionel Sambuc 	} else if (ptr->next != NULL &&
409*3e1db26aSLionel Sambuc 	    node__delete(el, &ptr->next, str) == 1) {
410*3e1db26aSLionel Sambuc 		if (ptr->next != NULL)
411*3e1db26aSLionel Sambuc 			return 0;
412*3e1db26aSLionel Sambuc 		if (prev_ptr == NULL)
413*3e1db26aSLionel Sambuc 			*inptr = ptr->sibling;
414*3e1db26aSLionel Sambuc 		else
415*3e1db26aSLionel Sambuc 			prev_ptr->sibling = ptr->sibling;
416*3e1db26aSLionel Sambuc 		ptr->sibling = NULL;
417*3e1db26aSLionel Sambuc 		node__put(el, ptr);
418*3e1db26aSLionel Sambuc 		return 1;
419*3e1db26aSLionel Sambuc 	} else {
420*3e1db26aSLionel Sambuc 		return 0;
421*3e1db26aSLionel Sambuc 	}
422*3e1db26aSLionel Sambuc }
423*3e1db26aSLionel Sambuc 
424*3e1db26aSLionel Sambuc 
425*3e1db26aSLionel Sambuc /* node__put():
426*3e1db26aSLionel Sambuc  *	Puts a tree of nodes onto free list using free(3).
427*3e1db26aSLionel Sambuc  */
428*3e1db26aSLionel Sambuc private void
node__put(EditLine * el,keymacro_node_t * ptr)429*3e1db26aSLionel Sambuc node__put(EditLine *el, keymacro_node_t *ptr)
430*3e1db26aSLionel Sambuc {
431*3e1db26aSLionel Sambuc 	if (ptr == NULL)
432*3e1db26aSLionel Sambuc 		return;
433*3e1db26aSLionel Sambuc 
434*3e1db26aSLionel Sambuc 	if (ptr->next != NULL) {
435*3e1db26aSLionel Sambuc 		node__put(el, ptr->next);
436*3e1db26aSLionel Sambuc 		ptr->next = NULL;
437*3e1db26aSLionel Sambuc 	}
438*3e1db26aSLionel Sambuc 	node__put(el, ptr->sibling);
439*3e1db26aSLionel Sambuc 
440*3e1db26aSLionel Sambuc 	switch (ptr->type) {
441*3e1db26aSLionel Sambuc 	case XK_CMD:
442*3e1db26aSLionel Sambuc 	case XK_NOD:
443*3e1db26aSLionel Sambuc 		break;
444*3e1db26aSLionel Sambuc 	case XK_EXE:
445*3e1db26aSLionel Sambuc 	case XK_STR:
446*3e1db26aSLionel Sambuc 		if (ptr->val.str != NULL)
447*3e1db26aSLionel Sambuc 			el_free(ptr->val.str);
448*3e1db26aSLionel Sambuc 		break;
449*3e1db26aSLionel Sambuc 	default:
450*3e1db26aSLionel Sambuc 		EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
451*3e1db26aSLionel Sambuc 		break;
452*3e1db26aSLionel Sambuc 	}
453*3e1db26aSLionel Sambuc 	el_free(ptr);
454*3e1db26aSLionel Sambuc }
455*3e1db26aSLionel Sambuc 
456*3e1db26aSLionel Sambuc 
457*3e1db26aSLionel Sambuc /* node__get():
458*3e1db26aSLionel Sambuc  *	Returns pointer to a keymacro_node_t for ch.
459*3e1db26aSLionel Sambuc  */
460*3e1db26aSLionel Sambuc private keymacro_node_t *
node__get(Int ch)461*3e1db26aSLionel Sambuc node__get(Int ch)
462*3e1db26aSLionel Sambuc {
463*3e1db26aSLionel Sambuc 	keymacro_node_t *ptr;
464*3e1db26aSLionel Sambuc 
465*3e1db26aSLionel Sambuc 	ptr = el_malloc(sizeof(*ptr));
466*3e1db26aSLionel Sambuc 	if (ptr == NULL)
467*3e1db26aSLionel Sambuc 		return NULL;
468*3e1db26aSLionel Sambuc 	ptr->ch = ch;
469*3e1db26aSLionel Sambuc 	ptr->type = XK_NOD;
470*3e1db26aSLionel Sambuc 	ptr->val.str = NULL;
471*3e1db26aSLionel Sambuc 	ptr->next = NULL;
472*3e1db26aSLionel Sambuc 	ptr->sibling = NULL;
473*3e1db26aSLionel Sambuc 	return ptr;
474*3e1db26aSLionel Sambuc }
475*3e1db26aSLionel Sambuc 
476*3e1db26aSLionel Sambuc private void
node__free(keymacro_node_t * k)477*3e1db26aSLionel Sambuc node__free(keymacro_node_t *k)
478*3e1db26aSLionel Sambuc {
479*3e1db26aSLionel Sambuc 	if (k == NULL)
480*3e1db26aSLionel Sambuc 		return;
481*3e1db26aSLionel Sambuc 	node__free(k->sibling);
482*3e1db26aSLionel Sambuc 	node__free(k->next);
483*3e1db26aSLionel Sambuc 	el_free(k);
484*3e1db26aSLionel Sambuc }
485*3e1db26aSLionel Sambuc 
486*3e1db26aSLionel Sambuc /* node_lookup():
487*3e1db26aSLionel Sambuc  *	look for the str starting at node ptr.
488*3e1db26aSLionel Sambuc  *	Print if last node
489*3e1db26aSLionel Sambuc  */
490*3e1db26aSLionel Sambuc private int
node_lookup(EditLine * el,const Char * str,keymacro_node_t * ptr,size_t cnt)491*3e1db26aSLionel Sambuc node_lookup(EditLine *el, const Char *str, keymacro_node_t *ptr, size_t cnt)
492*3e1db26aSLionel Sambuc {
493*3e1db26aSLionel Sambuc 	ssize_t used;
494*3e1db26aSLionel Sambuc 
495*3e1db26aSLionel Sambuc 	if (ptr == NULL)
496*3e1db26aSLionel Sambuc 		return -1;	/* cannot have null ptr */
497*3e1db26aSLionel Sambuc 
498*3e1db26aSLionel Sambuc 	if (!str || *str == 0) {
499*3e1db26aSLionel Sambuc 		/* no more chars in str.  node_enum from here. */
500*3e1db26aSLionel Sambuc 		(void) node_enum(el, ptr, cnt);
501*3e1db26aSLionel Sambuc 		return 0;
502*3e1db26aSLionel Sambuc 	} else {
503*3e1db26aSLionel Sambuc 		/* If match put this char into el->el_keymacro.buf.  Recurse */
504*3e1db26aSLionel Sambuc 		if (ptr->ch == *str) {
505*3e1db26aSLionel Sambuc 			/* match found */
506*3e1db26aSLionel Sambuc 			used = ct_visual_char(el->el_keymacro.buf + cnt,
507*3e1db26aSLionel Sambuc 			    KEY_BUFSIZ - cnt, ptr->ch);
508*3e1db26aSLionel Sambuc 			if (used == -1)
509*3e1db26aSLionel Sambuc 				return -1; /* ran out of buffer space */
510*3e1db26aSLionel Sambuc 			if (ptr->next != NULL)
511*3e1db26aSLionel Sambuc 				/* not yet at leaf */
512*3e1db26aSLionel Sambuc 				return (node_lookup(el, str + 1, ptr->next,
513*3e1db26aSLionel Sambuc 				    (size_t)used + cnt));
514*3e1db26aSLionel Sambuc 			else {
515*3e1db26aSLionel Sambuc 			    /* next node is null so key should be complete */
516*3e1db26aSLionel Sambuc 				if (str[1] == 0) {
517*3e1db26aSLionel Sambuc 					size_t px = cnt + (size_t)used;
518*3e1db26aSLionel Sambuc 					el->el_keymacro.buf[px] = '"';
519*3e1db26aSLionel Sambuc 					el->el_keymacro.buf[px + 1] = '\0';
520*3e1db26aSLionel Sambuc 					keymacro_kprint(el, el->el_keymacro.buf,
521*3e1db26aSLionel Sambuc 					    &ptr->val, ptr->type);
522*3e1db26aSLionel Sambuc 					return 0;
523*3e1db26aSLionel Sambuc 				} else
524*3e1db26aSLionel Sambuc 					return -1;
525*3e1db26aSLionel Sambuc 					/* mismatch -- str still has chars */
526*3e1db26aSLionel Sambuc 			}
527*3e1db26aSLionel Sambuc 		} else {
528*3e1db26aSLionel Sambuc 			/* no match found try sibling */
529*3e1db26aSLionel Sambuc 			if (ptr->sibling)
530*3e1db26aSLionel Sambuc 				return (node_lookup(el, str, ptr->sibling,
531*3e1db26aSLionel Sambuc 				    cnt));
532*3e1db26aSLionel Sambuc 			else
533*3e1db26aSLionel Sambuc 				return -1;
534*3e1db26aSLionel Sambuc 		}
535*3e1db26aSLionel Sambuc 	}
536*3e1db26aSLionel Sambuc }
537*3e1db26aSLionel Sambuc 
538*3e1db26aSLionel Sambuc 
539*3e1db26aSLionel Sambuc /* node_enum():
540*3e1db26aSLionel Sambuc  *	Traverse the node printing the characters it is bound in buffer
541*3e1db26aSLionel Sambuc  */
542*3e1db26aSLionel Sambuc private int
node_enum(EditLine * el,keymacro_node_t * ptr,size_t cnt)543*3e1db26aSLionel Sambuc node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
544*3e1db26aSLionel Sambuc {
545*3e1db26aSLionel Sambuc         ssize_t used;
546*3e1db26aSLionel Sambuc 
547*3e1db26aSLionel Sambuc 	if (cnt >= KEY_BUFSIZ - 5) {	/* buffer too small */
548*3e1db26aSLionel Sambuc 		el->el_keymacro.buf[++cnt] = '"';
549*3e1db26aSLionel Sambuc 		el->el_keymacro.buf[++cnt] = '\0';
550*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile,
551*3e1db26aSLionel Sambuc 		    "Some extended keys too long for internal print buffer");
552*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile, " \"" FSTR "...\"\n",
553*3e1db26aSLionel Sambuc 		    el->el_keymacro.buf);
554*3e1db26aSLionel Sambuc 		return 0;
555*3e1db26aSLionel Sambuc 	}
556*3e1db26aSLionel Sambuc 	if (ptr == NULL) {
557*3e1db26aSLionel Sambuc #ifdef DEBUG_EDIT
558*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_errfile,
559*3e1db26aSLionel Sambuc 		    "node_enum: BUG!! Null ptr passed\n!");
560*3e1db26aSLionel Sambuc #endif
561*3e1db26aSLionel Sambuc 		return -1;
562*3e1db26aSLionel Sambuc 	}
563*3e1db26aSLionel Sambuc 	/* put this char at end of str */
564*3e1db26aSLionel Sambuc         used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
565*3e1db26aSLionel Sambuc 	    ptr->ch);
566*3e1db26aSLionel Sambuc 	if (ptr->next == NULL) {
567*3e1db26aSLionel Sambuc 		/* print this key and function */
568*3e1db26aSLionel Sambuc 		el->el_keymacro.buf[cnt + (size_t)used   ] = '"';
569*3e1db26aSLionel Sambuc 		el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0';
570*3e1db26aSLionel Sambuc 		keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
571*3e1db26aSLionel Sambuc 	} else
572*3e1db26aSLionel Sambuc 		(void) node_enum(el, ptr->next, cnt + (size_t)used);
573*3e1db26aSLionel Sambuc 
574*3e1db26aSLionel Sambuc 	/* go to sibling if there is one */
575*3e1db26aSLionel Sambuc 	if (ptr->sibling)
576*3e1db26aSLionel Sambuc 		(void) node_enum(el, ptr->sibling, cnt);
577*3e1db26aSLionel Sambuc 	return 0;
578*3e1db26aSLionel Sambuc }
579*3e1db26aSLionel Sambuc 
580*3e1db26aSLionel Sambuc 
581*3e1db26aSLionel Sambuc /* keymacro_kprint():
582*3e1db26aSLionel Sambuc  *	Print the specified key and its associated
583*3e1db26aSLionel Sambuc  *	function specified by val
584*3e1db26aSLionel Sambuc  */
585*3e1db26aSLionel Sambuc protected void
keymacro_kprint(EditLine * el,const Char * key,keymacro_value_t * val,int ntype)586*3e1db26aSLionel Sambuc keymacro_kprint(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
587*3e1db26aSLionel Sambuc {
588*3e1db26aSLionel Sambuc 	el_bindings_t *fp;
589*3e1db26aSLionel Sambuc 	char unparsbuf[EL_BUFSIZ];
590*3e1db26aSLionel Sambuc 	static const char fmt[] = "%-15s->  %s\n";
591*3e1db26aSLionel Sambuc 
592*3e1db26aSLionel Sambuc 	if (val != NULL)
593*3e1db26aSLionel Sambuc 		switch (ntype) {
594*3e1db26aSLionel Sambuc 		case XK_STR:
595*3e1db26aSLionel Sambuc 		case XK_EXE:
596*3e1db26aSLionel Sambuc 			(void) keymacro__decode_str(val->str, unparsbuf,
597*3e1db26aSLionel Sambuc 			    sizeof(unparsbuf),
598*3e1db26aSLionel Sambuc 			    ntype == XK_STR ? "\"\"" : "[]");
599*3e1db26aSLionel Sambuc 			(void) fprintf(el->el_outfile, fmt,
600*3e1db26aSLionel Sambuc 			    ct_encode_string(key, &el->el_scratch), unparsbuf);
601*3e1db26aSLionel Sambuc 			break;
602*3e1db26aSLionel Sambuc 		case XK_CMD:
603*3e1db26aSLionel Sambuc 			for (fp = el->el_map.help; fp->name; fp++)
604*3e1db26aSLionel Sambuc 				if (val->cmd == fp->func) {
605*3e1db26aSLionel Sambuc                     ct_wcstombs(unparsbuf, fp->name, sizeof(unparsbuf));
606*3e1db26aSLionel Sambuc                     unparsbuf[sizeof(unparsbuf) -1] = '\0';
607*3e1db26aSLionel Sambuc 					(void) fprintf(el->el_outfile, fmt,
608*3e1db26aSLionel Sambuc                         ct_encode_string(key, &el->el_scratch), unparsbuf);
609*3e1db26aSLionel Sambuc 					break;
610*3e1db26aSLionel Sambuc 				}
611*3e1db26aSLionel Sambuc #ifdef DEBUG_KEY
612*3e1db26aSLionel Sambuc 			if (fp->name == NULL)
613*3e1db26aSLionel Sambuc 				(void) fprintf(el->el_outfile,
614*3e1db26aSLionel Sambuc 				    "BUG! Command not found.\n");
615*3e1db26aSLionel Sambuc #endif
616*3e1db26aSLionel Sambuc 
617*3e1db26aSLionel Sambuc 			break;
618*3e1db26aSLionel Sambuc 		default:
619*3e1db26aSLionel Sambuc 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
620*3e1db26aSLionel Sambuc 			break;
621*3e1db26aSLionel Sambuc 		}
622*3e1db26aSLionel Sambuc 	else
623*3e1db26aSLionel Sambuc 		(void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
624*3e1db26aSLionel Sambuc 		    &el->el_scratch), "no input");
625*3e1db26aSLionel Sambuc }
626*3e1db26aSLionel Sambuc 
627*3e1db26aSLionel Sambuc 
628*3e1db26aSLionel Sambuc #define ADDC(c) \
629*3e1db26aSLionel Sambuc 	if (b < eb) \
630*3e1db26aSLionel Sambuc 		*b++ = c; \
631*3e1db26aSLionel Sambuc 	else \
632*3e1db26aSLionel Sambuc 		b++
633*3e1db26aSLionel Sambuc /* keymacro__decode_str():
634*3e1db26aSLionel Sambuc  *	Make a printable version of the ey
635*3e1db26aSLionel Sambuc  */
636*3e1db26aSLionel Sambuc protected size_t
keymacro__decode_str(const Char * str,char * buf,size_t len,const char * sep)637*3e1db26aSLionel Sambuc keymacro__decode_str(const Char *str, char *buf, size_t len, const char *sep)
638*3e1db26aSLionel Sambuc {
639*3e1db26aSLionel Sambuc 	char *b = buf, *eb = b + len;
640*3e1db26aSLionel Sambuc 	const Char *p;
641*3e1db26aSLionel Sambuc 
642*3e1db26aSLionel Sambuc 	b = buf;
643*3e1db26aSLionel Sambuc 	if (sep[0] != '\0') {
644*3e1db26aSLionel Sambuc 		ADDC(sep[0]);
645*3e1db26aSLionel Sambuc 	}
646*3e1db26aSLionel Sambuc 	if (*str == '\0') {
647*3e1db26aSLionel Sambuc 		ADDC('^');
648*3e1db26aSLionel Sambuc 		ADDC('@');
649*3e1db26aSLionel Sambuc 		goto add_endsep;
650*3e1db26aSLionel Sambuc 	}
651*3e1db26aSLionel Sambuc 	for (p = str; *p != 0; p++) {
652*3e1db26aSLionel Sambuc 		Char dbuf[VISUAL_WIDTH_MAX];
653*3e1db26aSLionel Sambuc 		Char *p2 = dbuf;
654*3e1db26aSLionel Sambuc 		ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
655*3e1db26aSLionel Sambuc 		while (l-- > 0) {
656*3e1db26aSLionel Sambuc 			ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
657*3e1db26aSLionel Sambuc 			if (n == -1) /* ran out of space */
658*3e1db26aSLionel Sambuc 				goto add_endsep;
659*3e1db26aSLionel Sambuc 			else
660*3e1db26aSLionel Sambuc 				b += n;
661*3e1db26aSLionel Sambuc 		}
662*3e1db26aSLionel Sambuc 	}
663*3e1db26aSLionel Sambuc add_endsep:
664*3e1db26aSLionel Sambuc 	if (sep[0] != '\0' && sep[1] != '\0') {
665*3e1db26aSLionel Sambuc 		ADDC(sep[1]);
666*3e1db26aSLionel Sambuc 	}
667*3e1db26aSLionel Sambuc 	ADDC('\0');
668*3e1db26aSLionel Sambuc 	if ((size_t)(b - buf) >= len)
669*3e1db26aSLionel Sambuc 	    buf[len - 1] = '\0';
670*3e1db26aSLionel Sambuc 	return (size_t)(b - buf);
671*3e1db26aSLionel Sambuc }
672