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
36 * $NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $
43 RCSID("$Id: vis.c,v 1.5 2001/09/03 05:37:23 assar Exp $");
47 #define _DIAGASSERT(X)
50 #include <sys/cdefs.h>
54 #include "namespace.h"
56 #include <sys/types.h>
67 __weak_alias(strsvis,_strsvis)
68 __weak_alias(strsvisx,_strsvisx)
69 __weak_alias(strvis,_strvis)
70 __weak_alias(strvisx,_strvisx)
71 __weak_alias(svis,_svis)
72 __weak_alias(vis,_vis)
83 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
84 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
85 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
90 #define MAKEEXTRALIST(flag, extra) \
92 char *pextra = extra; \
93 if (flag & VIS_SP) *pextra++ = ' '; \
94 if (flag & VIS_TAB) *pextra++ = '\t'; \
95 if (flag & VIS_NL) *pextra++ = '\n'; \
96 if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\'; \
98 } while (/*CONSTCOND*/0)
101 * This is SVIS, the central macro of vis.
102 * dst: Pointer to the destination buffer
103 * c: Character to encode
105 * nextc: The character following 'c'
106 * extra: Pointer to the list of extra characters to be
107 * backslash-protected.
109 #define SVIS(dst, c, flag, nextc, extra) \
112 isextra = strchr(extra, c) != NULL; \
114 isascii((unsigned char)c) && \
115 (isgraph((unsigned char)c) || iswhite(c) || \
116 ((flag & VIS_SAFE) && issafe(c)))) { \
121 if (flag & VIS_CSTYLE) { \
124 isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
127 isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
130 isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
133 isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
136 isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
139 isc = 1; *dst++ = '\\'; *dst++ = 't'; \
142 isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
145 isc = 1; *dst++ = '\\'; *dst++ = 's'; \
148 isc = 1; *dst++ = '\\'; *dst++ = '0'; \
149 if (isoctal(nextc)) { \
156 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
158 *dst++ = (u_char)(((unsigned)(u_char)c >> 6) & 03) + '0'; \
159 *dst++ = (u_char)(((unsigned)(u_char)c >> 3) & 07) + '0'; \
160 *dst++ = (c & 07) + '0'; \
162 if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
164 c &= 0177; *dst++ = 'M'; \
166 if (iscntrl((unsigned char)c)) { \
173 *dst++ = '-'; *dst++ = c; \
176 } while (/*CONSTCOND*/0)
180 * svis - visually encode characters, also encoding the characters
181 * pointed to by `extra'
185 svis(char *dst, int c, int flag, int nextc, const char *extra)
187 _DIAGASSERT(dst != NULL);
188 _DIAGASSERT(extra != NULL);
190 SVIS(dst, c, flag, nextc, extra);
198 * strsvis, strsvisx - visually encode characters from src into dst
200 * Extra is a pointer to a \0-terminated list of characters to
201 * be encoded, too. These functions are useful e. g. to
202 * encode strings in such a way so that they are not interpreted
205 * Dst must be 4 times the size of src to account for possible
206 * expansion. The length of dst, not including the trailing NULL,
209 * Strsvisx encodes exactly len bytes from src into dst.
210 * This is useful for encoding a block of data.
214 strsvis(char *dst, const char *src, int flag, const char *extra)
219 _DIAGASSERT(dst != NULL);
220 _DIAGASSERT(src != NULL);
221 _DIAGASSERT(extra != NULL);
223 for (start = dst; (c = *src++) != '\0'; /* empty */)
224 SVIS(dst, c, flag, *src, extra);
226 return (dst - start);
233 strsvisx(char *dst, const char *src, size_t len, int flag, const char *extra)
238 _DIAGASSERT(dst != NULL);
239 _DIAGASSERT(src != NULL);
240 _DIAGASSERT(extra != NULL);
242 for (start = dst; len > 0; len--) {
244 SVIS(dst, c, flag, len ? *src : '\0', extra);
247 return (dst - start);
253 * vis - visually encode characters
257 vis(char *dst, int c, int flag, int nextc)
259 char extra[MAXEXTRAS];
261 _DIAGASSERT(dst != NULL);
263 MAKEEXTRALIST(flag, extra);
264 SVIS(dst, c, flag, nextc, extra);
272 * strvis, strvisx - visually encode characters from src into dst
274 * Dst must be 4 times the size of src to account for possible
275 * expansion. The length of dst, not including the trailing NULL,
278 * Strvisx encodes exactly len bytes from src into dst.
279 * This is useful for encoding a block of data.
283 strvis(char *dst, const char *src, int flag)
285 char extra[MAXEXTRAS];
287 MAKEEXTRALIST(flag, extra);
288 return (strsvis(dst, src, flag, extra));
295 strvisx(char *dst, const char *src, size_t len, int flag)
297 char extra[MAXEXTRAS];
299 MAKEEXTRALIST(flag, extra);
300 return (strsvisx(dst, src, len, flag, extra));