1 /* $NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
41 RCSID("$Id: vis.c,v 1.5 2001/09/03 05:37:23 assar Exp $");
45 #define _DIAGASSERT(X)
48 #include <sys/cdefs.h>
50 __RCSID("$NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $");
55 #include "namespace.h"
57 #include <sys/types.h>
68 __weak_alias(strsvis,_strsvis)
69 __weak_alias(strsvisx,_strsvisx)
70 __weak_alias(strvis,_strvis)
71 __weak_alias(strvisx,_strvisx)
72 __weak_alias(svis,_svis)
73 __weak_alias(vis,_vis)
84 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
85 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
86 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
91 #define MAKEEXTRALIST(flag, extra) \
93 char *pextra = extra; \
94 if (flag & VIS_SP) *pextra++ = ' '; \
95 if (flag & VIS_TAB) *pextra++ = '\t'; \
96 if (flag & VIS_NL) *pextra++ = '\n'; \
97 if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\'; \
99 } while (/*CONSTCOND*/0)
102 * This is SVIS, the central macro of vis.
103 * dst: Pointer to the destination buffer
104 * c: Character to encode
106 * nextc: The character following 'c'
107 * extra: Pointer to the list of extra characters to be
108 * backslash-protected.
110 #define SVIS(dst, c, flag, nextc, extra) \
113 isextra = strchr(extra, c) != NULL; \
115 isascii((unsigned char)c) && \
116 (isgraph((unsigned char)c) || iswhite(c) || \
117 ((flag & VIS_SAFE) && issafe(c)))) { \
122 if (flag & VIS_CSTYLE) { \
125 isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
128 isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
131 isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
134 isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
137 isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
140 isc = 1; *dst++ = '\\'; *dst++ = 't'; \
143 isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
146 isc = 1; *dst++ = '\\'; *dst++ = 's'; \
149 isc = 1; *dst++ = '\\'; *dst++ = '0'; \
150 if (isoctal(nextc)) { \
157 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
159 *dst++ = (u_char)(((unsigned)(u_char)c >> 6) & 03) + '0'; \
160 *dst++ = (u_char)(((unsigned)(u_char)c >> 3) & 07) + '0'; \
161 *dst++ = (c & 07) + '0'; \
163 if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
165 c &= 0177; *dst++ = 'M'; \
167 if (iscntrl((unsigned char)c)) { \
174 *dst++ = '-'; *dst++ = c; \
177 } while (/*CONSTCOND*/0)
181 * svis - visually encode characters, also encoding the characters
182 * pointed to by `extra'
186 svis(char *dst, int c, int flag, int nextc, const char *extra)
188 _DIAGASSERT(dst != NULL);
189 _DIAGASSERT(extra != NULL);
191 SVIS(dst, c, flag, nextc, extra);
199 * strsvis, strsvisx - visually encode characters from src into dst
201 * Extra is a pointer to a \0-terminated list of characters to
202 * be encoded, too. These functions are useful e. g. to
203 * encode strings in such a way so that they are not interpreted
206 * Dst must be 4 times the size of src to account for possible
207 * expansion. The length of dst, not including the trailing NULL,
210 * Strsvisx encodes exactly len bytes from src into dst.
211 * This is useful for encoding a block of data.
215 strsvis(char *dst, const char *src, int flag, const char *extra)
220 _DIAGASSERT(dst != NULL);
221 _DIAGASSERT(src != NULL);
222 _DIAGASSERT(extra != NULL);
224 for (start = dst; (c = *src++) != '\0'; /* empty */)
225 SVIS(dst, c, flag, *src, extra);
227 return (dst - start);
234 strsvisx(char *dst, const char *src, size_t len, int flag, const char *extra)
239 _DIAGASSERT(dst != NULL);
240 _DIAGASSERT(src != NULL);
241 _DIAGASSERT(extra != NULL);
243 for (start = dst; len > 0; len--) {
245 SVIS(dst, c, flag, len ? *src : '\0', extra);
248 return (dst - start);
254 * vis - visually encode characters
258 vis(char *dst, int c, int flag, int nextc)
260 char extra[MAXEXTRAS];
262 _DIAGASSERT(dst != NULL);
264 MAKEEXTRALIST(flag, extra);
265 SVIS(dst, c, flag, nextc, extra);
273 * strvis, strvisx - visually encode characters from src into dst
275 * Dst must be 4 times the size of src to account for possible
276 * expansion. The length of dst, not including the trailing NULL,
279 * Strvisx encodes exactly len bytes from src into dst.
280 * This is useful for encoding a block of data.
284 strvis(char *dst, const char *src, int flag)
286 char extra[MAXEXTRAS];
288 MAKEEXTRALIST(flag, extra);
289 return (strsvis(dst, src, flag, extra));
296 strvisx(char *dst, const char *src, size_t len, int flag)
298 char extra[MAXEXTRAS];
300 MAKEEXTRALIST(flag, extra);
301 return (strsvisx(dst, src, len, flag, extra));