Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.10
      1 /*	$NetBSD: vis.c,v 1.10 1998/11/13 12:31:53 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)vis.c	8.1 (Berkeley) 7/19/93";
     40 #else
     41 __RCSID("$NetBSD: vis.c,v 1.10 1998/11/13 12:31:53 christos Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include "namespace.h"
     46 #include <sys/types.h>
     47 #include <limits.h>
     48 #include <ctype.h>
     49 #include <vis.h>
     50 
     51 #ifdef __weak_alias
     52 __weak_alias(strvis,_strvis);
     53 __weak_alias(strvisx,_strvisx);
     54 __weak_alias(vis,_vis);
     55 #endif
     56 
     57 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     58 
     59 /*
     60  * vis - visually encode characters
     61  */
     62 char *
     63 vis(dst, c, flag, nextc)
     64 	char *dst;
     65 	int c, nextc;
     66 	int flag;
     67 {
     68 	if (((u_int)c <= UCHAR_MAX && isascii(c) && isgraph(c)) ||
     69 	   ((flag & VIS_SP) == 0 && c == ' ') ||
     70 	   ((flag & VIS_TAB) == 0 && c == '\t') ||
     71 	   ((flag & VIS_NL) == 0 && c == '\n') ||
     72 	   ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
     73 		*dst++ = c;
     74 		if (c == '\\' && (flag & VIS_NOSLASH) == 0)
     75 			*dst++ = '\\';
     76 		*dst = '\0';
     77 		return (dst);
     78 	}
     79 
     80 	if (flag & VIS_CSTYLE) {
     81 		switch(c) {
     82 		case '\n':
     83 			*dst++ = '\\';
     84 			*dst++ = 'n';
     85 			goto done;
     86 		case '\r':
     87 			*dst++ = '\\';
     88 			*dst++ = 'r';
     89 			goto done;
     90 		case '\b':
     91 			*dst++ = '\\';
     92 			*dst++ = 'b';
     93 			goto done;
     94 #if __STDC__
     95 		case '\a':
     96 #else
     97 		case '\007':
     98 #endif
     99 			*dst++ = '\\';
    100 			*dst++ = 'a';
    101 			goto done;
    102 		case '\v':
    103 			*dst++ = '\\';
    104 			*dst++ = 'v';
    105 			goto done;
    106 		case '\t':
    107 			*dst++ = '\\';
    108 			*dst++ = 't';
    109 			goto done;
    110 		case '\f':
    111 			*dst++ = '\\';
    112 			*dst++ = 'f';
    113 			goto done;
    114 		case ' ':
    115 			*dst++ = '\\';
    116 			*dst++ = 's';
    117 			goto done;
    118 		case '\0':
    119 			*dst++ = '\\';
    120 			*dst++ = '0';
    121 			if (isoctal(nextc)) {
    122 				*dst++ = '0';
    123 				*dst++ = '0';
    124 			}
    125 			goto done;
    126 		}
    127 	}
    128 	if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
    129 		*dst++ = '\\';
    130 		*dst++ = ((u_char)(((u_char)c) >> 6) & 07) + '0';
    131 		*dst++ = ((u_char)(((u_char)c) >> 3) & 07) + '0';
    132 		*dst++ = (((u_char)c) & 07) + '0';
    133 		goto done;
    134 	}
    135 	if ((flag & VIS_NOSLASH) == 0)
    136 		*dst++ = '\\';
    137 	if (c & 0200) {
    138 		c &= 0177;
    139 		*dst++ = 'M';
    140 	}
    141 	if (iscntrl(c)) {
    142 		*dst++ = '^';
    143 		if (c == 0177)
    144 			*dst++ = '?';
    145 		else
    146 			*dst++ = c + '@';
    147 	} else {
    148 		*dst++ = '-';
    149 		*dst++ = c;
    150 	}
    151 done:
    152 	*dst = '\0';
    153 	return (dst);
    154 }
    155 
    156 /*
    157  * strvis, strvisx - visually encode characters from src into dst
    158  *
    159  *	Dst must be 4 times the size of src to account for possible
    160  *	expansion.  The length of dst, not including the trailing NULL,
    161  *	is returned.
    162  *
    163  *	Strvisx encodes exactly len bytes from src into dst.
    164  *	This is useful for encoding a block of data.
    165  */
    166 int
    167 strvis(dst, src, flag)
    168 	char *dst;
    169 	const char *src;
    170 	int flag;
    171 {
    172 	char c;
    173 	char *start;
    174 
    175 	for (start = dst; (c = *src) != '\0';)
    176 		dst = vis(dst, c, flag, *++src);
    177 	*dst = '\0';
    178 	return (dst - start);
    179 }
    180 
    181 int
    182 strvisx(dst, src, len, flag)
    183 	char *dst;
    184 	const char *src;
    185 	size_t len;
    186 	int flag;
    187 {
    188 	char c;
    189 	char *start;
    190 
    191 	for (start = dst; len > 1; len--) {
    192 		c = *src;
    193 		dst = vis(dst, c, flag, *++src);
    194 	}
    195 	if (len)
    196 		dst = vis(dst, *src, flag, '\0');
    197 	*dst = '\0';
    198 
    199 	return (dst - start);
    200 }
    201