1 /* $NetBSD: vis.c,v 1.4 2003/08/07 09:15:32 agc Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1999 The NetBSD Foundation, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 RCSID("$Id: vis.c 21005 2007-06-08 01:54:35Z lha $");
72 #define _DIAGASSERT(X)
75 #include <sys/cdefs.h>
77 __RCSID("$NetBSD: vis.c,v 1.4 2003/08/07 09:15:32 agc Exp $");
82 #include "namespace.h"
84 #include <sys/types.h>
95 __weak_alias(strsvis,_strsvis)
96 __weak_alias(strsvisx,_strsvisx)
97 __weak_alias(strvis,_strvis)
98 __weak_alias(strvisx,_strvisx)
99 __weak_alias(svis,_svis)
100 __weak_alias(vis,_vis)
105 #if defined(__STDC__)
111 char ROKEN_LIB_FUNCTION
112 *rk_vis (char *, int, int, int);
113 char ROKEN_LIB_FUNCTION
114 *rk_svis (char *, int, int, int, const char *);
115 int ROKEN_LIB_FUNCTION
116 rk_strvis (char *, const char *, int);
117 int ROKEN_LIB_FUNCTION
118 rk_strsvis (char *, const char *, int, const char *);
119 int ROKEN_LIB_FUNCTION
120 rk_strvisx (char *, const char *, size_t, int);
121 int ROKEN_LIB_FUNCTION
122 rk_strsvisx (char *, const char *, size_t, int, const char *);
125 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
126 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
127 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
132 #define MAKEEXTRALIST(flag, extra) \
134 char *pextra = extra; \
135 if (flag & VIS_SP) *pextra++ = ' '; \
136 if (flag & VIS_TAB) *pextra++ = '\t'; \
137 if (flag & VIS_NL) *pextra++ = '\n'; \
138 if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\'; \
140 } while (/*CONSTCOND*/0)
143 * This is SVIS, the central macro of vis.
144 * dst: Pointer to the destination buffer
145 * c: Character to encode
147 * nextc: The character following 'c'
148 * extra: Pointer to the list of extra characters to be
149 * backslash-protected.
151 #define SVIS(dst, c, flag, nextc, extra) \
154 isextra = strchr(extra, c) != NULL; \
156 isascii((unsigned char)c) && \
157 (isgraph((unsigned char)c) || iswhite(c) || \
158 ((flag & VIS_SAFE) && issafe(c)))) { \
163 if (flag & VIS_CSTYLE) { \
166 isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
169 isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
172 isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
175 isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
178 isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
181 isc = 1; *dst++ = '\\'; *dst++ = 't'; \
184 isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
187 isc = 1; *dst++ = '\\'; *dst++ = 's'; \
190 isc = 1; *dst++ = '\\'; *dst++ = '0'; \
191 if (isoctal(nextc)) { \
198 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
200 *dst++ = (u_char)(((unsigned)(u_char)c >> 6) & 03) + '0'; \
201 *dst++ = (u_char)(((unsigned)(u_char)c >> 3) & 07) + '0'; \
202 *dst++ = (c & 07) + '0'; \
204 if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
206 c &= 0177; *dst++ = 'M'; \
208 if (iscntrl((unsigned char)c)) { \
215 *dst++ = '-'; *dst++ = c; \
218 } while (/*CONSTCOND*/0)
222 * svis - visually encode characters, also encoding the characters
223 * pointed to by `extra'
226 char * ROKEN_LIB_FUNCTION
227 rk_svis(char *dst, int c, int flag, int nextc, const char *extra)
229 _DIAGASSERT(dst != NULL);
230 _DIAGASSERT(extra != NULL);
232 SVIS(dst, c, flag, nextc, extra);
239 * strsvis, strsvisx - visually encode characters from src into dst
241 * Extra is a pointer to a \0-terminated list of characters to
242 * be encoded, too. These functions are useful e. g. to
243 * encode strings in such a way so that they are not interpreted
246 * Dst must be 4 times the size of src to account for possible
247 * expansion. The length of dst, not including the trailing NULL,
250 * Strsvisx encodes exactly len bytes from src into dst.
251 * This is useful for encoding a block of data.
254 int ROKEN_LIB_FUNCTION
255 rk_strsvis(char *dst, const char *src, int flag, const char *extra)
260 _DIAGASSERT(dst != NULL);
261 _DIAGASSERT(src != NULL);
262 _DIAGASSERT(extra != NULL);
264 for (start = dst; (c = *src++) != '\0'; /* empty */)
265 SVIS(dst, c, flag, *src, extra);
267 return (dst - start);
271 int ROKEN_LIB_FUNCTION
272 rk_strsvisx(char *dst, const char *src, size_t len, int flag, const char *extra)
277 _DIAGASSERT(dst != NULL);
278 _DIAGASSERT(src != NULL);
279 _DIAGASSERT(extra != NULL);
281 for (start = dst; len > 0; len--) {
283 SVIS(dst, c, flag, len ? *src : '\0', extra);
286 return (dst - start);
291 * vis - visually encode characters
293 char * ROKEN_LIB_FUNCTION
294 rk_vis(char *dst, int c, int flag, int nextc)
296 char extra[MAXEXTRAS];
298 _DIAGASSERT(dst != NULL);
300 MAKEEXTRALIST(flag, extra);
301 SVIS(dst, c, flag, nextc, extra);
308 * strvis, strvisx - visually encode characters from src into dst
310 * Dst must be 4 times the size of src to account for possible
311 * expansion. The length of dst, not including the trailing NULL,
314 * Strvisx encodes exactly len bytes from src into dst.
315 * This is useful for encoding a block of data.
318 int ROKEN_LIB_FUNCTION
319 rk_strvis(char *dst, const char *src, int flag)
321 char extra[MAXEXTRAS];
323 MAKEEXTRALIST(flag, extra);
324 return (rk_strsvis(dst, src, flag, extra));
328 int ROKEN_LIB_FUNCTION
329 rk_strvisx(char *dst, const char *src, size_t len, int flag)
331 char extra[MAXEXTRAS];
333 MAKEEXTRALIST(flag, extra);
334 return (rk_strsvisx(dst, src, len, flag, extra));