Home | History | Annotate | Line # | Download | only in libedit
keymacro.c revision 1.20
      1 /*	$NetBSD: keymacro.c,v 1.20 2016/04/12 00:16:06 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.20 2016/04/12 00:16:06 christos Exp $");
     41 #endif
     42 #endif /* not lint && not SCCSID */
     43 
     44 /*
     45  * keymacro.c: This module contains the procedures for maintaining
     46  *	       the extended-key map.
     47  *
     48  *      An extended-key (key) is a sequence of keystrokes introduced
     49  *	with a sequence introducer and consisting of an arbitrary
     50  *	number of characters.  This module maintains a map (the
     51  *	el->el_keymacro.map)
     52  *	to convert these extended-key sequences into input strs
     53  *	(XK_STR) or editor functions (XK_CMD).
     54  *
     55  *      Warning:
     56  *	  If key is a substr of some other keys, then the longer
     57  *	  keys are lost!!  That is, if the keys "abcd" and "abcef"
     58  *	  are in el->el_keymacro.map, adding the key "abc" will cause
     59  *	  the first two definitions to be lost.
     60  *
     61  *      Restrictions:
     62  *      -------------
     63  *      1) It is not possible to have one key that is a
     64  *	   substr of another.
     65  */
     66 #include <stdlib.h>
     67 #include <string.h>
     68 
     69 #include "el.h"
     70 
     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 static int		 node_trav(EditLine *, keymacro_node_t *, wchar_t *,
     85     keymacro_value_t *);
     86 static int		 node__try(EditLine *, keymacro_node_t *,
     87     const wchar_t *, keymacro_value_t *, int);
     88 static keymacro_node_t	*node__get(wint_t);
     89 static void		 node__free(keymacro_node_t *);
     90 static void		 node__put(EditLine *, keymacro_node_t *);
     91 static int		 node__delete(EditLine *, keymacro_node_t **,
     92     const wchar_t *);
     93 static int		 node_lookup(EditLine *, const wchar_t *,
     94     keymacro_node_t *, size_t);
     95 static 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 or XK_CMD).
    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 static 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 static 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 			if (ptr->val.str)
    345 				el_free(ptr->val.str);
    346 			break;
    347 		default:
    348 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
    349 			    ptr->type));
    350 			break;
    351 		}
    352 
    353 		switch (ptr->type = ntype) {
    354 		case XK_CMD:
    355 			ptr->val = *val;
    356 			break;
    357 		case XK_STR:
    358 			if ((ptr->val.str = wcsdup(val->str)) == NULL)
    359 				return -1;
    360 			break;
    361 		default:
    362 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
    363 			break;
    364 		}
    365 	} else {
    366 		/* still more chars to go */
    367 		if (ptr->next == NULL)
    368 			ptr->next = node__get(*str);	/* setup new node */
    369 		(void) node__try(el, ptr->next, str, val, ntype);
    370 	}
    371 	return 0;
    372 }
    373 
    374 
    375 /* node__delete():
    376  *	Delete node that matches str
    377  */
    378 static int
    379 node__delete(EditLine *el, keymacro_node_t **inptr, const wchar_t *str)
    380 {
    381 	keymacro_node_t *ptr;
    382 	keymacro_node_t *prev_ptr = NULL;
    383 
    384 	ptr = *inptr;
    385 
    386 	if (ptr->ch != *str) {
    387 		keymacro_node_t *xm;
    388 
    389 		for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
    390 			if (xm->sibling->ch == *str)
    391 				break;
    392 		if (xm->sibling == NULL)
    393 			return 0;
    394 		prev_ptr = xm;
    395 		ptr = xm->sibling;
    396 	}
    397 	if (*++str == '\0') {
    398 		/* we're there */
    399 		if (prev_ptr == NULL)
    400 			*inptr = ptr->sibling;
    401 		else
    402 			prev_ptr->sibling = ptr->sibling;
    403 		ptr->sibling = NULL;
    404 		node__put(el, ptr);
    405 		return 1;
    406 	} else if (ptr->next != NULL &&
    407 	    node__delete(el, &ptr->next, str) == 1) {
    408 		if (ptr->next != NULL)
    409 			return 0;
    410 		if (prev_ptr == NULL)
    411 			*inptr = ptr->sibling;
    412 		else
    413 			prev_ptr->sibling = ptr->sibling;
    414 		ptr->sibling = NULL;
    415 		node__put(el, ptr);
    416 		return 1;
    417 	} else {
    418 		return 0;
    419 	}
    420 }
    421 
    422 
    423 /* node__put():
    424  *	Puts a tree of nodes onto free list using free(3).
    425  */
    426 static void
    427 node__put(EditLine *el, keymacro_node_t *ptr)
    428 {
    429 	if (ptr == NULL)
    430 		return;
    431 
    432 	if (ptr->next != NULL) {
    433 		node__put(el, ptr->next);
    434 		ptr->next = NULL;
    435 	}
    436 	node__put(el, ptr->sibling);
    437 
    438 	switch (ptr->type) {
    439 	case XK_CMD:
    440 	case XK_NOD:
    441 		break;
    442 	case XK_STR:
    443 		if (ptr->val.str != NULL)
    444 			el_free(ptr->val.str);
    445 		break;
    446 	default:
    447 		EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
    448 		break;
    449 	}
    450 	el_free(ptr);
    451 }
    452 
    453 
    454 /* node__get():
    455  *	Returns pointer to a keymacro_node_t for ch.
    456  */
    457 static keymacro_node_t *
    458 node__get(wint_t ch)
    459 {
    460 	keymacro_node_t *ptr;
    461 
    462 	ptr = el_malloc(sizeof(*ptr));
    463 	if (ptr == NULL)
    464 		return NULL;
    465 	ptr->ch = ch;
    466 	ptr->type = XK_NOD;
    467 	ptr->val.str = NULL;
    468 	ptr->next = NULL;
    469 	ptr->sibling = NULL;
    470 	return ptr;
    471 }
    472 
    473 static void
    474 node__free(keymacro_node_t *k)
    475 {
    476 	if (k == NULL)
    477 		return;
    478 	node__free(k->sibling);
    479 	node__free(k->next);
    480 	el_free(k);
    481 }
    482 
    483 /* node_lookup():
    484  *	look for the str starting at node ptr.
    485  *	Print if last node
    486  */
    487 static int
    488 node_lookup(EditLine *el, const wchar_t *str, keymacro_node_t *ptr,
    489     size_t cnt)
    490 {
    491 	ssize_t used;
    492 
    493 	if (ptr == NULL)
    494 		return -1;	/* cannot have null ptr */
    495 
    496 	if (!str || *str == 0) {
    497 		/* no more chars in str.  node_enum from here. */
    498 		(void) node_enum(el, ptr, cnt);
    499 		return 0;
    500 	} else {
    501 		/* If match put this char into el->el_keymacro.buf.  Recurse */
    502 		if (ptr->ch == *str) {
    503 			/* match found */
    504 			used = ct_visual_char(el->el_keymacro.buf + cnt,
    505 			    KEY_BUFSIZ - cnt, ptr->ch);
    506 			if (used == -1)
    507 				return -1; /* ran out of buffer space */
    508 			if (ptr->next != NULL)
    509 				/* not yet at leaf */
    510 				return (node_lookup(el, str + 1, ptr->next,
    511 				    (size_t)used + cnt));
    512 			else {
    513 			    /* next node is null so key should be complete */
    514 				if (str[1] == 0) {
    515 					size_t px = cnt + (size_t)used;
    516 					el->el_keymacro.buf[px] = '"';
    517 					el->el_keymacro.buf[px + 1] = '\0';
    518 					keymacro_kprint(el, el->el_keymacro.buf,
    519 					    &ptr->val, ptr->type);
    520 					return 0;
    521 				} else
    522 					return -1;
    523 					/* mismatch -- str still has chars */
    524 			}
    525 		} else {
    526 			/* no match found try sibling */
    527 			if (ptr->sibling)
    528 				return (node_lookup(el, str, ptr->sibling,
    529 				    cnt));
    530 			else
    531 				return -1;
    532 		}
    533 	}
    534 }
    535 
    536 
    537 /* node_enum():
    538  *	Traverse the node printing the characters it is bound in buffer
    539  */
    540 static int
    541 node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
    542 {
    543         ssize_t used;
    544 
    545 	if (cnt >= KEY_BUFSIZ - 5) {	/* buffer too small */
    546 		el->el_keymacro.buf[++cnt] = '"';
    547 		el->el_keymacro.buf[++cnt] = '\0';
    548 		(void) fprintf(el->el_errfile,
    549 		    "Some extended keys too long for internal print buffer");
    550 		(void) fprintf(el->el_errfile, " \"%ls...\"\n",
    551 		    el->el_keymacro.buf);
    552 		return 0;
    553 	}
    554 	if (ptr == NULL) {
    555 #ifdef DEBUG_EDIT
    556 		(void) fprintf(el->el_errfile,
    557 		    "node_enum: BUG!! Null ptr passed\n!");
    558 #endif
    559 		return -1;
    560 	}
    561 	/* put this char at end of str */
    562         used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
    563 	    ptr->ch);
    564 	if (ptr->next == NULL) {
    565 		/* print this key and function */
    566 		el->el_keymacro.buf[cnt + (size_t)used   ] = '"';
    567 		el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0';
    568 		keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
    569 	} else
    570 		(void) node_enum(el, ptr->next, cnt + (size_t)used);
    571 
    572 	/* go to sibling if there is one */
    573 	if (ptr->sibling)
    574 		(void) node_enum(el, ptr->sibling, cnt);
    575 	return 0;
    576 }
    577 
    578 
    579 /* keymacro_kprint():
    580  *	Print the specified key and its associated
    581  *	function specified by val
    582  */
    583 protected void
    584 keymacro_kprint(EditLine *el, const wchar_t *key, keymacro_value_t *val,
    585     int ntype)
    586 {
    587 	el_bindings_t *fp;
    588 	char unparsbuf[EL_BUFSIZ];
    589 	static const char fmt[] = "%-15s->  %s\n";
    590 
    591 	if (val != NULL)
    592 		switch (ntype) {
    593 		case XK_STR:
    594 			(void) keymacro__decode_str(val->str, unparsbuf,
    595 			    sizeof(unparsbuf),
    596 			    ntype == XK_STR ? "\"\"" : "[]");
    597 			(void) fprintf(el->el_outfile, fmt,
    598 			    ct_encode_string(key, &el->el_scratch), unparsbuf);
    599 			break;
    600 		case XK_CMD:
    601 			for (fp = el->el_map.help; fp->name; fp++)
    602 				if (val->cmd == fp->func) {
    603                     wcstombs(unparsbuf, fp->name, sizeof(unparsbuf));
    604                     unparsbuf[sizeof(unparsbuf) -1] = '\0';
    605 					(void) fprintf(el->el_outfile, fmt,
    606                         ct_encode_string(key, &el->el_scratch), unparsbuf);
    607 					break;
    608 				}
    609 #ifdef DEBUG_KEY
    610 			if (fp->name == NULL)
    611 				(void) fprintf(el->el_outfile,
    612 				    "BUG! Command not found.\n");
    613 #endif
    614 
    615 			break;
    616 		default:
    617 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
    618 			break;
    619 		}
    620 	else
    621 		(void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
    622 		    &el->el_scratch), "no input");
    623 }
    624 
    625 
    626 #define ADDC(c) \
    627 	if (b < eb) \
    628 		*b++ = c; \
    629 	else \
    630 		b++
    631 /* keymacro__decode_str():
    632  *	Make a printable version of the ey
    633  */
    634 protected size_t
    635 keymacro__decode_str(const wchar_t *str, char *buf, size_t len,
    636     const char *sep)
    637 {
    638 	char *b = buf, *eb = b + len;
    639 	const wchar_t *p;
    640 
    641 	b = buf;
    642 	if (sep[0] != '\0') {
    643 		ADDC(sep[0]);
    644 	}
    645 	if (*str == '\0') {
    646 		ADDC('^');
    647 		ADDC('@');
    648 		goto add_endsep;
    649 	}
    650 	for (p = str; *p != 0; p++) {
    651 		wchar_t dbuf[VISUAL_WIDTH_MAX];
    652 		wchar_t *p2 = dbuf;
    653 		ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
    654 		while (l-- > 0) {
    655 			ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
    656 			if (n == -1) /* ran out of space */
    657 				goto add_endsep;
    658 			else
    659 				b += n;
    660 		}
    661 	}
    662 add_endsep:
    663 	if (sep[0] != '\0' && sep[1] != '\0') {
    664 		ADDC(sep[1]);
    665 	}
    666 	ADDC('\0');
    667 	if ((size_t)(b - buf) >= len)
    668 	    buf[len - 1] = '\0';
    669 	return (size_t)(b - buf);
    670 }
    671