Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.53
      1  1.53  christos /*	$NetBSD: vis.c,v 1.53 2013/02/15 00:28:10 christos Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*-
      4   1.6       cgd  * Copyright (c) 1989, 1993
      5  1.16  wennmach  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.29     lukem  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.26       agc /*-
     33  1.31     lukem  * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
     34  1.30     lukem  * All rights reserved.
     35  1.26       agc  *
     36  1.26       agc  * Redistribution and use in source and binary forms, with or without
     37  1.26       agc  * modification, are permitted provided that the following conditions
     38  1.26       agc  * are met:
     39  1.26       agc  * 1. Redistributions of source code must retain the above copyright
     40  1.26       agc  *    notice, this list of conditions and the following disclaimer.
     41  1.26       agc  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.26       agc  *    notice, this list of conditions and the following disclaimer in the
     43  1.26       agc  *    documentation and/or other materials provided with the distribution.
     44  1.26       agc  *
     45  1.30     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     46  1.30     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     47  1.30     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     48  1.30     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     49  1.30     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     50  1.30     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     51  1.30     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     52  1.30     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     53  1.30     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     54  1.30     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     55  1.30     lukem  * POSSIBILITY OF SUCH DAMAGE.
     56  1.26       agc  */
     57  1.26       agc 
     58   1.7  christos #include <sys/cdefs.h>
     59  1.21        tv #if defined(LIBC_SCCS) && !defined(lint)
     60  1.53  christos __RCSID("$NetBSD: vis.c,v 1.53 2013/02/15 00:28:10 christos Exp $");
     61  1.21        tv #endif /* LIBC_SCCS and not lint */
     62  1.46  christos #ifdef __FBSDID
     63  1.46  christos __FBSDID("$FreeBSD$");
     64  1.46  christos #define	_DIAGASSERT(x)	assert(x)
     65  1.46  christos #endif
     66   1.1       cgd 
     67   1.8       jtc #include "namespace.h"
     68   1.1       cgd #include <sys/types.h>
     69  1.12     lukem 
     70  1.12     lukem #include <assert.h>
     71   1.1       cgd #include <vis.h>
     72  1.44  christos #include <errno.h>
     73  1.22  christos #include <stdlib.h>
     74  1.46  christos #include <wchar.h>
     75  1.46  christos #include <wctype.h>
     76   1.8       jtc 
     77   1.8       jtc #ifdef __weak_alias
     78  1.18   mycroft __weak_alias(strvisx,_strvisx)
     79  1.20        tv #endif
     80  1.20        tv 
     81  1.24     pooka #if !HAVE_VIS || !HAVE_SVIS
     82  1.20        tv #include <ctype.h>
     83  1.20        tv #include <limits.h>
     84  1.20        tv #include <stdio.h>
     85  1.20        tv #include <string.h>
     86   1.1       cgd 
     87  1.47  christos /*
     88  1.47  christos  * The reason for going through the trouble to deal with character encodings
     89  1.47  christos  * in vis(3), is that we use this to safe encode output of commands. This
     90  1.47  christos  * safe encoding varies depending on the character set. For example if we
     91  1.47  christos  * display ps output in French, we don't want to display French characters
     92  1.47  christos  * as M-foo.
     93  1.47  christos  */
     94  1.47  christos 
     95  1.48     pooka static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
     96  1.37       dsl 
     97  1.15  wennmach #undef BELL
     98  1.46  christos #define BELL L'\a'
     99  1.15  wennmach 
    100  1.46  christos #define iswoctal(c)	(((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
    101  1.46  christos #define iswwhite(c)	(c == L' ' || c == L'\t' || c == L'\n')
    102  1.46  christos #define iswsafe(c)	(c == L'\b' || c == BELL || c == L'\r')
    103  1.46  christos #define xtoa(c)		L"0123456789abcdef"[c]
    104  1.46  christos #define XTOA(c)		L"0123456789ABCDEF"[c]
    105  1.16  wennmach 
    106  1.45  christos #define MAXEXTRAS	9
    107  1.15  wennmach 
    108  1.34    martin #define MAKEEXTRALIST(flag, extra, orig_str)				      \
    109  1.16  wennmach do {									      \
    110  1.46  christos 	const wchar_t *orig = orig_str;					      \
    111  1.46  christos 	const wchar_t *o = orig;					      \
    112  1.46  christos 	wchar_t *e;							      \
    113  1.22  christos 	while (*o++)							      \
    114  1.22  christos 		continue;						      \
    115  1.46  christos 	extra = calloc((size_t)((o - orig) + MAXEXTRAS), sizeof(*extra));    \
    116  1.31     lukem 	if (!extra) break;						      \
    117  1.46  christos 	for (o = orig, e = extra; (*e++ = *o++) != L'\0';)		      \
    118  1.22  christos 		continue;						      \
    119  1.22  christos 	e--;								      \
    120  1.45  christos 	if (flag & VIS_GLOB) {						      \
    121  1.46  christos 		*e++ = L'*';						      \
    122  1.46  christos 		*e++ = L'?';						      \
    123  1.46  christos 		*e++ = L'[';						      \
    124  1.46  christos 		*e++ = L'#';						      \
    125  1.45  christos 	}								      \
    126  1.46  christos 	if (flag & VIS_SP) *e++ = L' ';					      \
    127  1.46  christos 	if (flag & VIS_TAB) *e++ = L'\t';				      \
    128  1.46  christos 	if (flag & VIS_NL) *e++ = L'\n';				      \
    129  1.46  christos 	if ((flag & VIS_NOSLASH) == 0) *e++ = L'\\';			      \
    130  1.46  christos 	*e = L'\0';							      \
    131  1.19   mycroft } while (/*CONSTCOND*/0)
    132  1.15  wennmach 
    133  1.22  christos /*
    134  1.37       dsl  * This is do_hvis, for HTTP style (RFC 1808)
    135  1.22  christos  */
    136  1.46  christos static wchar_t *
    137  1.46  christos do_hvis(wchar_t *dst, wint_t c, int flag, wint_t nextc, const wchar_t *extra)
    138  1.37       dsl {
    139  1.46  christos 	if (iswalnum(c)
    140  1.41    plunky 	    /* safe */
    141  1.46  christos 	    || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
    142  1.41    plunky 	    /* extra */
    143  1.46  christos 	    || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
    144  1.46  christos 	    || c == L',')
    145  1.46  christos 		dst = do_svis(dst, c, flag, nextc, extra);
    146  1.46  christos 	else {
    147  1.46  christos 		*dst++ = L'%';
    148  1.37       dsl 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
    149  1.37       dsl 		*dst++ = xtoa((unsigned int)c & 0xf);
    150  1.37       dsl 	}
    151  1.41    plunky 
    152  1.37       dsl 	return dst;
    153  1.37       dsl }
    154  1.27     enami 
    155  1.15  wennmach /*
    156  1.39  christos  * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
    157  1.39  christos  * NB: No handling of long lines or CRLF.
    158  1.39  christos  */
    159  1.46  christos static wchar_t *
    160  1.46  christos do_mvis(wchar_t *dst, wint_t c, int flag, wint_t nextc, const wchar_t *extra)
    161  1.39  christos {
    162  1.46  christos 	if ((c != L'\n') &&
    163  1.39  christos 	    /* Space at the end of the line */
    164  1.46  christos 	    ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
    165  1.39  christos 	    /* Out of range */
    166  1.46  christos 	    (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
    167  1.39  christos 	    /* Specific char to be escaped */
    168  1.46  christos 	    wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
    169  1.46  christos 		*dst++ = L'=';
    170  1.39  christos 		*dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
    171  1.39  christos 		*dst++ = XTOA((unsigned int)c & 0xf);
    172  1.46  christos 	} else
    173  1.46  christos 		dst = do_svis(dst, c, flag, nextc, extra);
    174  1.39  christos 	return dst;
    175  1.39  christos }
    176  1.39  christos 
    177  1.39  christos /*
    178  1.37       dsl  * This is do_vis, the central code of vis.
    179  1.16  wennmach  * dst:	      Pointer to the destination buffer
    180  1.16  wennmach  * c:	      Character to encode
    181  1.15  wennmach  * flag:      Flag word
    182  1.15  wennmach  * nextc:     The character following 'c'
    183  1.15  wennmach  * extra:     Pointer to the list of extra characters to be
    184  1.16  wennmach  *	      backslash-protected.
    185  1.15  wennmach  */
    186  1.46  christos static wchar_t *
    187  1.46  christos do_svis(wchar_t *dst, wint_t c, int flag, wint_t nextc, const wchar_t *extra)
    188  1.37       dsl {
    189  1.46  christos 	int iswextra;
    190  1.43  christos 
    191  1.46  christos 	iswextra = wcschr(extra, c) != NULL;
    192  1.46  christos 	if (!iswextra && (iswgraph(c) || iswwhite(c) ||
    193  1.46  christos 	    ((flag & VIS_SAFE) && iswsafe(c)))) {
    194  1.37       dsl 		*dst++ = c;
    195  1.37       dsl 		return dst;
    196  1.37       dsl 	}
    197  1.37       dsl 	if (flag & VIS_CSTYLE) {
    198  1.37       dsl 		switch (c) {
    199  1.46  christos 		case L'\n':
    200  1.46  christos 			*dst++ = L'\\'; *dst++ = L'n';
    201  1.37       dsl 			return dst;
    202  1.46  christos 		case L'\r':
    203  1.46  christos 			*dst++ = L'\\'; *dst++ = L'r';
    204  1.37       dsl 			return dst;
    205  1.46  christos 		case L'\b':
    206  1.46  christos 			*dst++ = L'\\'; *dst++ = L'b';
    207  1.37       dsl 			return dst;
    208  1.37       dsl 		case BELL:
    209  1.46  christos 			*dst++ = L'\\'; *dst++ = L'a';
    210  1.37       dsl 			return dst;
    211  1.46  christos 		case L'\v':
    212  1.46  christos 			*dst++ = L'\\'; *dst++ = L'v';
    213  1.37       dsl 			return dst;
    214  1.46  christos 		case L'\t':
    215  1.46  christos 			*dst++ = L'\\'; *dst++ = L't';
    216  1.37       dsl 			return dst;
    217  1.46  christos 		case L'\f':
    218  1.46  christos 			*dst++ = L'\\'; *dst++ = L'f';
    219  1.37       dsl 			return dst;
    220  1.46  christos 		case L' ':
    221  1.46  christos 			*dst++ = L'\\'; *dst++ = L's';
    222  1.37       dsl 			return dst;
    223  1.46  christos 		case L'\0':
    224  1.46  christos 			*dst++ = L'\\'; *dst++ = L'0';
    225  1.46  christos 			if (iswoctal(nextc)) {
    226  1.46  christos 				*dst++ = L'0';
    227  1.46  christos 				*dst++ = L'0';
    228  1.37       dsl 			}
    229  1.37       dsl 			return dst;
    230  1.37       dsl 		default:
    231  1.46  christos 			if (iswgraph(c)) {
    232  1.46  christos 				*dst++ = L'\\';
    233  1.46  christos 				*dst++ = c;
    234  1.37       dsl 				return dst;
    235  1.37       dsl 			}
    236  1.37       dsl 		}
    237  1.37       dsl 	}
    238  1.46  christos 	if (iswextra || ((c & 0177) == L' ') || (flag & VIS_OCTAL)) {
    239  1.46  christos 		*dst++ = L'\\';
    240  1.46  christos 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
    241  1.46  christos 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
    242  1.46  christos 		*dst++ =			     (c	      & 07) + L'0';
    243  1.37       dsl 	} else {
    244  1.46  christos 		if ((flag & VIS_NOSLASH) == 0)
    245  1.46  christos 			*dst++ = L'\\';
    246  1.42  christos 
    247  1.37       dsl 		if (c & 0200) {
    248  1.46  christos 			c &= 0177;
    249  1.46  christos 			*dst++ = L'M';
    250  1.37       dsl 		}
    251  1.42  christos 
    252  1.46  christos 		if (iswcntrl(c)) {
    253  1.46  christos 			*dst++ = L'^';
    254  1.37       dsl 			if (c == 0177)
    255  1.46  christos 				*dst++ = L'?';
    256  1.37       dsl 			else
    257  1.46  christos 				*dst++ = c + L'@';
    258  1.37       dsl 		} else {
    259  1.46  christos 			*dst++ = L'-';
    260  1.46  christos 			*dst++ = c;
    261  1.37       dsl 		}
    262  1.37       dsl 	}
    263  1.37       dsl 	return dst;
    264  1.37       dsl }
    265  1.15  wennmach 
    266  1.46  christos typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
    267  1.39  christos 
    268  1.39  christos /*
    269  1.39  christos  * Return the appropriate encoding function depending on the flags given.
    270  1.39  christos  */
    271  1.39  christos static visfun_t
    272  1.39  christos getvisfun(int flag)
    273  1.39  christos {
    274  1.39  christos 	if (flag & VIS_HTTPSTYLE)
    275  1.39  christos 		return do_hvis;
    276  1.40  christos 	if (flag & VIS_MIMESTYLE)
    277  1.39  christos 		return do_mvis;
    278  1.39  christos 	return do_svis;
    279  1.39  christos }
    280  1.15  wennmach 
    281  1.15  wennmach /*
    282  1.46  christos  * istrsnvisx()
    283  1.46  christos  * 	The main internal function.
    284  1.46  christos  *	All user-visible functions call this one.
    285  1.15  wennmach  */
    286  1.46  christos static int
    287  1.46  christos istrsnvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
    288  1.46  christos     int flag, const char *mbextra)
    289  1.15  wennmach {
    290  1.46  christos 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra, *nextra;
    291  1.50  christos 	size_t len, olen;
    292  1.46  christos 	wint_t c;
    293  1.39  christos 	visfun_t f;
    294  1.46  christos 	int clen, error = -1;
    295  1.50  christos 	ssize_t mbslength;
    296  1.46  christos 
    297  1.46  christos 	_DIAGASSERT(mbdst != NULL);
    298  1.46  christos 	_DIAGASSERT(mbsrc != NULL);
    299  1.46  christos 	_DIAGASSERT(mbextra != NULL);
    300  1.46  christos 
    301  1.53  christos 	/*
    302  1.53  christos 	 * Input (mbsrc) is a char string considered to be multibyte
    303  1.53  christos 	 * characters.  The input loop will read this string pulling
    304  1.53  christos 	 * one character, possibly multiple bytes, from mbsrc and
    305  1.53  christos 	 * converting each to wchar_t in src.
    306  1.53  christos 	 *
    307  1.53  christos 	 * The vis conversion will be done using the wide char
    308  1.53  christos 	 * wchar_t string.
    309  1.53  christos 	 *
    310  1.53  christos 	 * This will then be converted back to a multibyte string to
    311  1.53  christos 	 * return to the caller.
    312  1.53  christos 	 */
    313  1.53  christos 
    314  1.53  christos 	/* Allocate space for the wide char strings */
    315  1.46  christos 	psrc = pdst = extra = nextra = NULL;
    316  1.46  christos 	if (!mblength)
    317  1.46  christos 		mblength = strlen(mbsrc);
    318  1.31     lukem 
    319  1.46  christos 	if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
    320  1.46  christos 		return -1;
    321  1.46  christos 	if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
    322  1.46  christos 		goto out;
    323  1.46  christos 	if ((extra = calloc((strlen(mbextra) + 1), sizeof(*extra))) == NULL)
    324  1.46  christos 		goto out;
    325  1.46  christos 
    326  1.46  christos 	dst = pdst;
    327  1.46  christos 	src = psrc;
    328  1.46  christos 
    329  1.53  christos 	/*
    330  1.53  christos 	 * Input loop.
    331  1.53  christos 	 * Handle up to mblength characters (not bytes).  We do not
    332  1.53  christos 	 * stop at NULs because we may be processing a block of data
    333  1.53  christos 	 * that includes NULs.  We process one more than the character
    334  1.53  christos 	 * count so that we also get the next character of input which
    335  1.53  christos 	 * is needed under some circumstances as a look-ahead character.
    336  1.53  christos 	 */
    337  1.50  christos 	mbslength = (ssize_t)mblength;
    338  1.53  christos 	/*
    339  1.53  christos 	 * When inputing a single character, must also read in the
    340  1.53  christos 	 * next character for nextc, the look-ahead character.
    341  1.53  christos 	 */
    342  1.53  christos 	if (mbslength == 1)
    343  1.53  christos 		mbslength++;
    344  1.53  christos 	while (mbslength > 0) {
    345  1.53  christos 		/* Convert one multibyte character to wchar_t. */
    346  1.50  christos 		clen = mbtowc(src, mbsrc, MB_LEN_MAX);
    347  1.50  christos 		if (clen < 0) {
    348  1.53  christos 			/* Conversion error, process as a byte instead. */
    349  1.53  christos 			*src = (wint_t)*mbsrc;
    350  1.50  christos 			clen = 1;
    351  1.51  christos 		}
    352  1.50  christos 		if (clen == 0)
    353  1.53  christos 			/*
    354  1.53  christos 			 * NUL in input gives 0 return value. process
    355  1.53  christos 			 * as single NUL byte.
    356  1.53  christos 			 */
    357  1.50  christos 			clen = 1;
    358  1.53  christos 		/* Advance output pointer if we still have input left. */
    359  1.50  christos 		src++;
    360  1.53  christos 		/* Advance input pointer by number of bytes read. */
    361  1.50  christos 		mbsrc += clen;
    362  1.53  christos 		/* Decrement input count */
    363  1.50  christos 		mbslength -= clen;
    364  1.46  christos 	}
    365  1.50  christos 	len = src - psrc;
    366  1.50  christos 	src = psrc;
    367  1.53  christos 	/*
    368  1.53  christos 	 * In the single character input case, we will have actually
    369  1.53  christos 	 * processed two characters, c and nextc.  Reset len back to
    370  1.53  christos 	 * just a single character.
    371  1.53  christos 	 */
    372  1.53  christos 	if (mblength < len)
    373  1.53  christos 		len = mblength;
    374  1.46  christos 
    375  1.53  christos 	/* Convert extra argument to list of characters for this mode. */
    376  1.46  christos 	mbstowcs(extra, mbextra, strlen(mbextra));
    377  1.22  christos 	MAKEEXTRALIST(flag, nextra, extra);
    378  1.31     lukem 	if (!nextra) {
    379  1.44  christos 		if (dlen && *dlen == 0) {
    380  1.44  christos 			errno = ENOSPC;
    381  1.46  christos 			goto out;
    382  1.44  christos 		}
    383  1.46  christos 		*mbdst = '\0';		/* can't create nextra, return "" */
    384  1.46  christos 		error = 0;
    385  1.46  christos 		goto out;
    386  1.31     lukem 	}
    387  1.46  christos 
    388  1.53  christos 	/* Look up which processing function to call. */
    389  1.39  christos 	f = getvisfun(flag);
    390  1.46  christos 
    391  1.53  christos 	/*
    392  1.53  christos 	 * Main processing loop.
    393  1.53  christos 	 * Call do_Xvis processing function one character at a time
    394  1.53  christos 	 * with next character available for look-ahead.
    395  1.53  christos 	 */
    396  1.46  christos 	for (start = dst; len > 0; len--) {
    397  1.46  christos 		c = *src++;
    398  1.46  christos 		dst = (*f)(dst, c, flag, len >= 1 ? *src : L'\0', nextra);
    399  1.46  christos 		if (dst == NULL) {
    400  1.46  christos 			errno = ENOSPC;
    401  1.46  christos 			goto out;
    402  1.46  christos 		}
    403  1.46  christos 	}
    404  1.46  christos 
    405  1.53  christos 	/* Terminate the output string. */
    406  1.46  christos 	*dst = L'\0';
    407  1.46  christos 
    408  1.53  christos 	/* Convert wchar_t string back to multibyte output string. */
    409  1.46  christos 	len = dlen ? *dlen : ((wcslen(start) + 1) * MB_LEN_MAX);
    410  1.46  christos 	olen = wcstombs(mbdst, start, len * sizeof(*mbdst));
    411  1.46  christos 
    412  1.46  christos 	free(nextra);
    413  1.46  christos 	free(extra);
    414  1.46  christos 	free(pdst);
    415  1.46  christos 	free(psrc);
    416  1.46  christos 
    417  1.46  christos 	return (int)olen;
    418  1.46  christos out:
    419  1.31     lukem 	free(nextra);
    420  1.46  christos 	free(extra);
    421  1.46  christos 	free(pdst);
    422  1.46  christos 	free(psrc);
    423  1.46  christos 	return error;
    424  1.15  wennmach }
    425  1.46  christos #endif
    426  1.46  christos 
    427  1.46  christos #if !HAVE_SVIS
    428  1.46  christos /*
    429  1.46  christos  *	The "svis" variants all take an "extra" arg that is a pointer
    430  1.46  christos  *	to a NUL-terminated list of characters to be encoded, too.
    431  1.46  christos  *	These functions are useful e. g. to encode strings in such a
    432  1.46  christos  *	way so that they are not interpreted by a shell.
    433  1.46  christos  */
    434  1.15  wennmach 
    435  1.44  christos char *
    436  1.46  christos svis(char *mbdst, int c, int flag, int nextc, const char *mbextra)
    437  1.44  christos {
    438  1.46  christos 	char cc[2];
    439  1.46  christos 	int ret;
    440  1.46  christos 
    441  1.46  christos 	cc[0] = c;
    442  1.46  christos 	cc[1] = nextc;
    443  1.46  christos 
    444  1.46  christos 	ret = istrsnvisx(mbdst, NULL, cc, 1, flag, mbextra);
    445  1.46  christos 	if (ret < 0)
    446  1.46  christos 		return NULL;
    447  1.46  christos 	return mbdst + ret;
    448  1.44  christos }
    449  1.44  christos 
    450  1.44  christos char *
    451  1.46  christos snvis(char *mbdst, size_t dlen, int c, int flag, int nextc, const char *mbextra)
    452  1.44  christos {
    453  1.46  christos 	char cc[2];
    454  1.46  christos 	int ret;
    455  1.44  christos 
    456  1.46  christos 	cc[0] = c;
    457  1.46  christos 	cc[1] = nextc;
    458  1.15  wennmach 
    459  1.46  christos 	ret = istrsnvisx(mbdst, &dlen, cc, 1, flag, mbextra);
    460  1.46  christos 	if (ret < 0)
    461  1.46  christos 		return NULL;
    462  1.46  christos 	return mbdst + ret;
    463  1.15  wennmach }
    464  1.15  wennmach 
    465  1.44  christos int
    466  1.46  christos strsvis(char *mbdst, const char *mbsrc, int flag, const char *mbextra)
    467  1.44  christos {
    468  1.46  christos 	return istrsnvisx(mbdst, NULL, mbsrc, 0, flag, mbextra);
    469  1.44  christos }
    470  1.15  wennmach 
    471  1.15  wennmach int
    472  1.46  christos strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flag, const char *mbextra)
    473  1.44  christos {
    474  1.46  christos 	return istrsnvisx(mbdst, &dlen, mbsrc, 0, flag, mbextra);
    475  1.15  wennmach }
    476  1.44  christos 
    477  1.44  christos int
    478  1.46  christos strsvisx(char *mbdst, const char *mbsrc, size_t len, int flag, const char *mbextra)
    479  1.44  christos {
    480  1.46  christos 	return istrsnvisx(mbdst, NULL, mbsrc, len, flag, mbextra);
    481  1.44  christos }
    482  1.44  christos 
    483  1.44  christos int
    484  1.46  christos strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flag,
    485  1.46  christos     const char *mbextra)
    486  1.44  christos {
    487  1.46  christos 	return istrsnvisx(mbdst, &dlen, mbsrc, len, flag, mbextra);
    488  1.44  christos }
    489  1.24     pooka #endif
    490  1.15  wennmach 
    491  1.24     pooka #if !HAVE_VIS
    492   1.1       cgd /*
    493   1.1       cgd  * vis - visually encode characters
    494   1.1       cgd  */
    495  1.46  christos char *
    496  1.46  christos vis(char *mbdst, int c, int flag, int nextc)
    497  1.15  wennmach {
    498  1.46  christos 	char cc[2];
    499  1.46  christos 	int ret;
    500  1.15  wennmach 
    501  1.46  christos 	cc[0] = c;
    502  1.46  christos 	cc[1] = nextc;
    503  1.15  wennmach 
    504  1.46  christos 	ret = istrsnvisx(mbdst, NULL, cc, 1, flag, "");
    505  1.46  christos 	if (ret < 0)
    506  1.44  christos 		return NULL;
    507  1.46  christos 	return mbdst + ret;
    508   1.1       cgd }
    509   1.1       cgd 
    510  1.44  christos char *
    511  1.46  christos nvis(char *mbdst, size_t dlen, int c, int flag, int nextc)
    512  1.44  christos {
    513  1.46  christos 	char cc[2];
    514  1.46  christos 	int ret;
    515  1.46  christos 
    516  1.46  christos 	cc[0] = c;
    517  1.46  christos 	cc[1] = nextc;
    518  1.44  christos 
    519  1.46  christos 	ret = istrsnvisx(mbdst, &dlen, cc, 1, flag, "");
    520  1.46  christos 	if (ret < 0)
    521  1.46  christos 		return NULL;
    522  1.46  christos 	return mbdst + ret;
    523  1.44  christos }
    524  1.44  christos 
    525   1.1       cgd /*
    526  1.46  christos  * strvis - visually encode characters from src into dst
    527  1.27     enami  *
    528  1.16  wennmach  *	Dst must be 4 times the size of src to account for possible
    529  1.16  wennmach  *	expansion.  The length of dst, not including the trailing NULL,
    530  1.27     enami  *	is returned.
    531   1.1       cgd  */
    532   1.1       cgd 
    533  1.44  christos int
    534  1.46  christos strvis(char *mbdst, const char *mbsrc, int flag)
    535  1.44  christos {
    536  1.46  christos 	return istrsnvisx(mbdst, NULL, mbsrc, 0, flag, "");
    537  1.44  christos }
    538  1.15  wennmach 
    539   1.1       cgd int
    540  1.46  christos strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flag)
    541  1.44  christos {
    542  1.46  christos 	return istrsnvisx(mbdst, &dlen, mbsrc, 0, flag, "");
    543  1.44  christos }
    544  1.44  christos 
    545  1.46  christos /*
    546  1.46  christos  * strvisx - visually encode characters from src into dst
    547  1.46  christos  *
    548  1.46  christos  *	Dst must be 4 times the size of src to account for possible
    549  1.46  christos  *	expansion.  The length of dst, not including the trailing NULL,
    550  1.46  christos  *	is returned.
    551  1.46  christos  *
    552  1.46  christos  *	Strvisx encodes exactly len characters from src into dst.
    553  1.46  christos  *	This is useful for encoding a block of data.
    554  1.46  christos  */
    555  1.44  christos 
    556  1.44  christos int
    557  1.46  christos strvisx(char *mbdst, const char *mbsrc, size_t len, int flag)
    558  1.44  christos {
    559  1.46  christos 	return istrsnvisx(mbdst, NULL, mbsrc, len, flag, "");
    560  1.44  christos }
    561  1.44  christos 
    562  1.44  christos int
    563  1.46  christos strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flag)
    564  1.44  christos {
    565  1.46  christos 	return istrsnvisx(mbdst, &dlen, mbsrc, len, flag, "");
    566  1.44  christos }
    567  1.20        tv #endif
    568