Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.16
      1 /*	$NetBSD: vis.c,v 1.16 1999/11/28 22:51:37 wennmach Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * Copyright (c) 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      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
     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  */
     36 
     37 
     38 #include <sys/cdefs.h>
     39 #if !defined(lint)
     40 __RCSID("$NetBSD: vis.c,v 1.16 1999/11/28 22:51:37 wennmach Exp $");
     41 #endif /* not lint */
     42 
     43 #include "namespace.h"
     44 #include <sys/types.h>
     45 
     46 #include <assert.h>
     47 #include <ctype.h>
     48 #include <limits.h>
     49 #include <stdio.h>
     50 #include <string.h>
     51 #include <vis.h>
     52 
     53 #ifdef __weak_alias
     54 __weak_alias(strsvis,_strsvis);
     55 __weak_alias(strsvisx,_strsvisx);
     56 __weak_alias(strvis,_strvis);
     57 __weak_alias(strvisx,_strvisx);
     58 __weak_alias(svis,_svis);
     59 __weak_alias(vis,_vis);
     60 #endif
     61 
     62 #undef BELL
     63 #if defined(__STDC__)
     64 #define BELL '\a'
     65 #else
     66 #define BELL '\007'
     67 #endif
     68 
     69 #define isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     70 #define iswhite(c)	(c == ' ' || c == '\t' || c == '\n')
     71 #define issafe(c)	(c == '\b' || c == BELL || c == '\r')
     72 
     73 #define MAXEXTRAS       5
     74 
     75 
     76 #define MAKEEXTRALIST(flag, extra)					      \
     77 do {									      \
     78 	char *pextra = extra;						      \
     79 	if (flag & VIS_SP) *pextra++ = ' ';				      \
     80 	if (flag & VIS_TAB) *pextra++ = '\t';				      \
     81 	if (flag & VIS_NL) *pextra++ = '\n';				      \
     82 	if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\';		      \
     83 	*pextra = '\0';							      \
     84 } while (0)
     85 
     86 /*
     87  * This is SVIS, the central macro of vis.
     88  * dst:	      Pointer to the destination buffer
     89  * c:	      Character to encode
     90  * flag:      Flag word
     91  * nextc:     The character following 'c'
     92  * extra:     Pointer to the list of extra characters to be
     93  *	      backslash-protected.
     94  */
     95 #define SVIS(dst, c, flag, nextc, extra)				      \
     96 do {									      \
     97 	int isextra, isc;						      \
     98 	isextra = strchr(extra, c) != NULL;				      \
     99 	if (isascii(c) && isgraph(c)) {					      \
    100 		if (isextra) *dst++ = '\\';				      \
    101 		*dst++ = c;						      \
    102 		break;							      \
    103 	}								      \
    104 	if (iswhite(c) && !isextra) {					      \
    105 		*dst++ = c;						      \
    106 		break;							      \
    107 	}								      \
    108 	if ((flag & VIS_SAFE) && issafe(c)) {				      \
    109 		*dst++ = c;						      \
    110 		break;							      \
    111 	}								      \
    112 	isc = 0;							      \
    113 	if (flag & VIS_CSTYLE) {					      \
    114 		switch (c) {						      \
    115 		case '\n':						      \
    116 			isc = 1; *dst++ = '\\'; *dst++ = 'n';		      \
    117 			break;						      \
    118 		case '\r':						      \
    119 			isc = 1; *dst++ = '\\'; *dst++ = 'r';		      \
    120 			break;						      \
    121 		case '\b':						      \
    122 			isc = 1; *dst++ = '\\'; *dst++ = 'b';		      \
    123 			break;						      \
    124 		case BELL:						      \
    125 			isc = 1; *dst++ = '\\'; *dst++ = 'a';		      \
    126 			break;						      \
    127 		case '\v':						      \
    128 			isc = 1; *dst++ = '\\'; *dst++ = 'v';		      \
    129 			break;						      \
    130 		case '\t':						      \
    131 			isc = 1; *dst++ = '\\'; *dst++ = 't';		      \
    132 			break;						      \
    133 		case '\f':						      \
    134 			isc = 1; *dst++ = '\\'; *dst++ = 'f';		      \
    135 			break;						      \
    136 		case ' ':						      \
    137 			isc = 1; *dst++ = '\\'; *dst++ = 's';		      \
    138 			break;						      \
    139 		case '\0':						      \
    140 			isc = 1; *dst++ = '\\'; *dst++ = '0';		      \
    141 			if (isoctal(nextc)) {				      \
    142 				*dst++ = '0';				      \
    143 				*dst++ = '0';				      \
    144 			}						      \
    145 		}							      \
    146 	}								      \
    147 	if (isc) break;							      \
    148 	if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {		      \
    149 		*dst++ = '\\';						      \
    150 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';    \
    151 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';    \
    152 		*dst++ =			     (c	      & 07) + '0';    \
    153 	} else {							      \
    154 		if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';		      \
    155 		if (c & 0200) {						      \
    156 			c &= 0177; *dst++ = 'M';			      \
    157 		}							      \
    158 		if (iscntrl(c)) {					      \
    159 			*dst++ = '^';					      \
    160 			if (c == 0177)					      \
    161 				*dst++ = '?';				      \
    162 			else						      \
    163 				*dst++ = c + '@';			      \
    164 		} else {						      \
    165 			*dst++ = '-'; *dst++ = c;			      \
    166 		}							      \
    167 	}								      \
    168 } while(0)
    169 
    170 
    171 /*
    172  * vis - visually encode characters, backslash-escaping the characters
    173  *	 pointed to by extra
    174  */
    175 char *
    176 svis(dst, c, flag, nextc, extra)
    177 	char *dst;
    178 	int c, flag, nextc;
    179 	const char *extra;
    180 {
    181 	_DIAGASSERT(dst != NULL);
    182 	_DIAGASSERT(extra != NULL);
    183 
    184 	SVIS(dst, c, flag, nextc, extra);
    185 	*dst = '\0';
    186 	return(dst);
    187 }
    188 
    189 
    190 /*
    191  * strsvis, strsvisx - visually encode characters from src into dst
    192  *
    193  *	Extra is a pointer to a \0-terminated list of characters to
    194  *	be backslash-escaped. These functions are useful e. g. to
    195  *	encode strings in such a way that they are not interpreted
    196  *	by a shell.
    197  *
    198  *	Dst must be 4 times the size of src to account for possible
    199  *	expansion.  The length of dst, not including the trailing NULL,
    200  *	is returned.
    201  *
    202  *	Strsvisx encodes exactly len bytes from src into dst.
    203  *	This is useful for encoding a block of data.
    204  */
    205 int
    206 strsvis(dst, src, flag, extra)
    207 	char *dst;
    208 	const char *src;
    209 	int flag;
    210 	const char *extra;
    211 {
    212 	char c;
    213 	char *start;
    214 
    215 	_DIAGASSERT(dst != NULL);
    216 	_DIAGASSERT(src != NULL);
    217 	_DIAGASSERT(extra != NULL);
    218 
    219 	for (start = dst; (c = *src++) != '\0'; /* empty */)
    220 	    SVIS(dst, c, flag, *src, extra);
    221 	*dst = '\0';
    222 	return (dst - start);
    223 }
    224 
    225 
    226 int
    227 strsvisx(dst, src, len, flag, extra)
    228 	char *dst;
    229 	const char *src;
    230 	size_t len;
    231 	int flag;
    232 	const char *extra;
    233 {
    234 	char c;
    235 	char *start;
    236 
    237 	_DIAGASSERT(dst != NULL);
    238 	_DIAGASSERT(src != NULL);
    239 	_DIAGASSERT(extra != NULL);
    240 
    241 	for (start = dst; len > 0; len--) {
    242 		c = *src++;
    243 		SVIS(dst, c, flag, len ? *src : '\0', extra);
    244 	}
    245 	*dst = '\0';
    246 	return (dst - start);
    247 }
    248 
    249 
    250 /*
    251  * vis - visually encode characters
    252  */
    253 char *
    254 vis(dst, c, flag, nextc)
    255 	char *dst;
    256 	int c, flag, nextc;
    257 
    258 {
    259 	char extra[MAXEXTRAS];
    260 
    261 	_DIAGASSERT(dst != NULL);
    262 
    263 	MAKEEXTRALIST(flag, extra);
    264 	SVIS(dst, c, flag, nextc, extra);
    265 	*dst = '\0';
    266 	return (dst);
    267 }
    268 
    269 
    270 /*
    271  * strvis, strvisx - visually encode characters from src into dst
    272  *
    273  *	Dst must be 4 times the size of src to account for possible
    274  *	expansion.  The length of dst, not including the trailing NULL,
    275  *	is returned.
    276  *
    277  *	Strvisx encodes exactly len bytes from src into dst.
    278  *	This is useful for encoding a block of data.
    279  */
    280 int
    281 strvis(dst, src, flag)
    282 	char *dst;
    283 	const char *src;
    284 	int flag;
    285 {
    286 	char extra[MAXEXTRAS];
    287 
    288 	MAKEEXTRALIST(flag, extra);
    289 	return (strsvis(dst, src, flag, extra));
    290 }
    291 
    292 
    293 int
    294 strvisx(dst, src, len, flag)
    295 	char *dst;
    296 	const char *src;
    297 	size_t len;
    298 	int flag;
    299 {
    300 	char extra[MAXEXTRAS];
    301 
    302 	MAKEEXTRALIST(flag, extra);
    303 	return (strsvisx(dst, src, len, flag, extra));
    304 }
    305