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