1 /* $NetBSD: vis.c,v 1.7 1997/07/13 19:43:00 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.7 1997/07/13 19:43:00 christos Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include <sys/types.h> 46 #include <limits.h> 47 #include <ctype.h> 48 #include <vis.h> 49 50 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 51 52 /* 53 * vis - visually encode characters 54 */ 55 char * 56 vis(dst, c, flag, nextc) 57 register char *dst; 58 int c, nextc; 59 register int flag; 60 { 61 if (((u_int)c <= UCHAR_MAX && isascii(c) && isgraph(c)) || 62 ((flag & VIS_SP) == 0 && c == ' ') || 63 ((flag & VIS_TAB) == 0 && c == '\t') || 64 ((flag & VIS_NL) == 0 && c == '\n') || 65 ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) { 66 *dst++ = c; 67 if (c == '\\' && (flag & VIS_NOSLASH) == 0) 68 *dst++ = '\\'; 69 *dst = '\0'; 70 return (dst); 71 } 72 73 if (flag & VIS_CSTYLE) { 74 switch(c) { 75 case '\n': 76 *dst++ = '\\'; 77 *dst++ = 'n'; 78 goto done; 79 case '\r': 80 *dst++ = '\\'; 81 *dst++ = 'r'; 82 goto done; 83 case '\b': 84 *dst++ = '\\'; 85 *dst++ = 'b'; 86 goto done; 87 #if __STDC__ 88 case '\a': 89 #else 90 case '\007': 91 #endif 92 *dst++ = '\\'; 93 *dst++ = 'a'; 94 goto done; 95 case '\v': 96 *dst++ = '\\'; 97 *dst++ = 'v'; 98 goto done; 99 case '\t': 100 *dst++ = '\\'; 101 *dst++ = 't'; 102 goto done; 103 case '\f': 104 *dst++ = '\\'; 105 *dst++ = 'f'; 106 goto done; 107 case ' ': 108 *dst++ = '\\'; 109 *dst++ = 's'; 110 goto done; 111 case '\0': 112 *dst++ = '\\'; 113 *dst++ = '0'; 114 if (isoctal(nextc)) { 115 *dst++ = '0'; 116 *dst++ = '0'; 117 } 118 goto done; 119 } 120 } 121 if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) { 122 *dst++ = '\\'; 123 *dst++ = ((u_char)c >> 6 & 07) + '0'; 124 *dst++ = ((u_char)c >> 3 & 07) + '0'; 125 *dst++ = ((u_char)c & 07) + '0'; 126 goto done; 127 } 128 if ((flag & VIS_NOSLASH) == 0) 129 *dst++ = '\\'; 130 if (c & 0200) { 131 c &= 0177; 132 *dst++ = 'M'; 133 } 134 if (iscntrl(c)) { 135 *dst++ = '^'; 136 if (c == 0177) 137 *dst++ = '?'; 138 else 139 *dst++ = c + '@'; 140 } else { 141 *dst++ = '-'; 142 *dst++ = c; 143 } 144 done: 145 *dst = '\0'; 146 return (dst); 147 } 148 149 /* 150 * strvis, strvisx - visually encode characters from src into dst 151 * 152 * Dst must be 4 times the size of src to account for possible 153 * expansion. The length of dst, not including the trailing NULL, 154 * is returned. 155 * 156 * Strvisx encodes exactly len bytes from src into dst. 157 * This is useful for encoding a block of data. 158 */ 159 int 160 strvis(dst, src, flag) 161 register char *dst; 162 register const char *src; 163 int flag; 164 { 165 register char c; 166 char *start; 167 168 for (start = dst; (c = *src) != '\0';) 169 dst = vis(dst, c, flag, *++src); 170 *dst = '\0'; 171 return (dst - start); 172 } 173 174 int 175 strvisx(dst, src, len, flag) 176 register char *dst; 177 register const char *src; 178 register size_t len; 179 int flag; 180 { 181 register char c; 182 char *start; 183 184 for (start = dst; len > 1; len--) { 185 c = *src; 186 dst = vis(dst, c, flag, *++src); 187 } 188 if (len) 189 dst = vis(dst, *src, flag, '\0'); 190 *dst = '\0'; 191 192 return (dst - start); 193 } 194