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