vis.c revision 1.15 1 1.15 wennmach /* $NetBSD: vis.c,v 1.15 1999/11/25 16:50:06 wennmach Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.15 wennmach * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.6 cgd * Copyright (c) 1989, 1993
6 1.15 wennmach * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.15 wennmach * This product includes software developed by the University of
19 1.15 wennmach * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.15 wennmach
38 1.7 christos #include <sys/cdefs.h>
39 1.15 wennmach #if !defined(lint)
40 1.15 wennmach __RCSID("$NetBSD: vis.c,v 1.15 1999/11/25 16:50:06 wennmach Exp $");
41 1.15 wennmach #endif /* not lint */
42 1.1 cgd
43 1.8 jtc #include "namespace.h"
44 1.1 cgd #include <sys/types.h>
45 1.12 lukem
46 1.12 lukem #include <assert.h>
47 1.12 lukem #include <ctype.h>
48 1.2 cgd #include <limits.h>
49 1.12 lukem #include <stdio.h>
50 1.15 wennmach #include <string.h>
51 1.1 cgd #include <vis.h>
52 1.8 jtc
53 1.8 jtc #ifdef __weak_alias
54 1.15 wennmach __weak_alias(strsvis,_strsvis);
55 1.15 wennmach __weak_alias(strsvisx,_strsvisx);
56 1.8 jtc __weak_alias(strvis,_strvis);
57 1.8 jtc __weak_alias(strvisx,_strvisx);
58 1.15 wennmach __weak_alias(svis,_svis);
59 1.8 jtc __weak_alias(vis,_vis);
60 1.8 jtc #endif
61 1.1 cgd
62 1.15 wennmach #undef BELL
63 1.15 wennmach #if defined(__STDC__)
64 1.15 wennmach #define BELL '\a'
65 1.15 wennmach #else
66 1.15 wennmach #define BELL '\007'
67 1.15 wennmach #endif
68 1.15 wennmach
69 1.15 wennmach #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
70 1.15 wennmach #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
71 1.15 wennmach #define issafe(c) (c == '\b' || c == BELL || c == '\r')
72 1.15 wennmach
73 1.15 wennmach
74 1.15 wennmach #define MAKEEXTRALIST(flag, extra) \
75 1.15 wennmach do { \
76 1.15 wennmach char *pextra = extra; \
77 1.15 wennmach if (flag & VIS_SP) *pextra++ = ' '; \
78 1.15 wennmach if (flag & VIS_TAB) *pextra++ = '\t'; \
79 1.15 wennmach if (flag & VIS_NL) *pextra++ = '\n'; \
80 1.15 wennmach if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\'; \
81 1.15 wennmach } while (0)
82 1.15 wennmach
83 1.15 wennmach /*
84 1.15 wennmach * This is SVIS, the central macro of vis.
85 1.15 wennmach * dst: Pointer to the destination buffer
86 1.15 wennmach * c: Character to encode
87 1.15 wennmach * flag: Flag word
88 1.15 wennmach * nextc: The character following 'c'
89 1.15 wennmach * extra: Pointer to the list of extra characters to be
90 1.15 wennmach * backslash-protected.
91 1.15 wennmach */
92 1.15 wennmach #define SVIS(dst, c, flag, nextc, extra) \
93 1.15 wennmach do { \
94 1.15 wennmach int isextra, isc; \
95 1.15 wennmach isextra = strchr(extra, c) != NULL; \
96 1.15 wennmach if (isascii(c) && isgraph(c)) { \
97 1.15 wennmach if (isextra) *dst++ = '\\'; \
98 1.15 wennmach *dst++ = c; \
99 1.15 wennmach break; \
100 1.15 wennmach } \
101 1.15 wennmach if (iswhite(c) && !isextra) { \
102 1.15 wennmach *dst++ = c; \
103 1.15 wennmach break; \
104 1.15 wennmach } \
105 1.15 wennmach if ((flag & VIS_SAFE) && issafe(c)) { \
106 1.15 wennmach *dst++ = c; \
107 1.15 wennmach break; \
108 1.15 wennmach } \
109 1.15 wennmach isc = 0; \
110 1.15 wennmach if (flag & VIS_CSTYLE) { \
111 1.15 wennmach switch (c) { \
112 1.15 wennmach case '\n': \
113 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
114 1.15 wennmach break; \
115 1.15 wennmach case '\r': \
116 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
117 1.15 wennmach break; \
118 1.15 wennmach case '\b': \
119 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
120 1.15 wennmach break; \
121 1.15 wennmach case BELL: \
122 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
123 1.15 wennmach break; \
124 1.15 wennmach case '\v': \
125 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
126 1.15 wennmach break; \
127 1.15 wennmach case '\t': \
128 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 't'; \
129 1.15 wennmach break; \
130 1.15 wennmach case '\f': \
131 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
132 1.15 wennmach break; \
133 1.15 wennmach case ' ': \
134 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = 's'; \
135 1.15 wennmach break; \
136 1.15 wennmach case '\0': \
137 1.15 wennmach isc = 1; *dst++ = '\\'; *dst++ = '0'; \
138 1.15 wennmach if (isoctal(nextc)) { \
139 1.15 wennmach *dst++ = '0'; \
140 1.15 wennmach *dst++ = '0'; \
141 1.15 wennmach } \
142 1.15 wennmach } \
143 1.15 wennmach } \
144 1.15 wennmach if (isc) break; \
145 1.15 wennmach if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
146 1.15 wennmach *dst++ = '\\'; \
147 1.15 wennmach *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \
148 1.15 wennmach *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \
149 1.15 wennmach *dst++ = (c & 07) + '0'; \
150 1.15 wennmach } else { \
151 1.15 wennmach if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
152 1.15 wennmach if (c & 0200) { \
153 1.15 wennmach c &= 0177; *dst++ = 'M'; \
154 1.15 wennmach } \
155 1.15 wennmach if (iscntrl(c)) { \
156 1.15 wennmach *dst++ = '^'; \
157 1.15 wennmach if (c == 0177) \
158 1.15 wennmach *dst++ = '?'; \
159 1.15 wennmach else \
160 1.15 wennmach *dst++ = c + '@'; \
161 1.15 wennmach } else { \
162 1.15 wennmach *dst++ = '-'; *dst++ = c; \
163 1.15 wennmach } \
164 1.15 wennmach } \
165 1.15 wennmach } while(0)
166 1.15 wennmach
167 1.15 wennmach
168 1.15 wennmach /*
169 1.15 wennmach * vis - visually encode characters, backslash-escaping the characters
170 1.15 wennmach * pointed to by extra
171 1.15 wennmach */
172 1.15 wennmach char *
173 1.15 wennmach svis(dst, c, flag, nextc, extra)
174 1.15 wennmach char *dst;
175 1.15 wennmach int c, flag, nextc;
176 1.15 wennmach const char *extra;
177 1.15 wennmach {
178 1.15 wennmach _DIAGASSERT(dst != NULL);
179 1.15 wennmach _DIAGASSERT(extra != NULL);
180 1.15 wennmach
181 1.15 wennmach SVIS(dst, c, flag, nextc, extra);
182 1.15 wennmach *dst = '\0';
183 1.15 wennmach return(dst);
184 1.15 wennmach }
185 1.15 wennmach
186 1.15 wennmach
187 1.15 wennmach /*
188 1.15 wennmach * strsvis, strsvisx - visually encode characters from src into dst
189 1.15 wennmach *
190 1.15 wennmach * Extra is a pointer to a \0-terminated list of characters to
191 1.15 wennmach * be backslash-escaped. These functions are useful e. g. to
192 1.15 wennmach * encode strings in such a way that they are not interpreted
193 1.15 wennmach * by a shell.
194 1.15 wennmach *
195 1.15 wennmach * Dst must be 4 times the size of src to account for possible
196 1.15 wennmach * expansion. The length of dst, not including the trailing NULL,
197 1.15 wennmach * is returned.
198 1.15 wennmach *
199 1.15 wennmach * Strsvisx encodes exactly len bytes from src into dst.
200 1.15 wennmach * This is useful for encoding a block of data.
201 1.15 wennmach */
202 1.15 wennmach int
203 1.15 wennmach strsvis(dst, src, flag, extra)
204 1.15 wennmach char *dst;
205 1.15 wennmach const char *src;
206 1.15 wennmach int flag;
207 1.15 wennmach const char *extra;
208 1.15 wennmach {
209 1.15 wennmach char c;
210 1.15 wennmach char *start;
211 1.15 wennmach
212 1.15 wennmach _DIAGASSERT(dst != NULL);
213 1.15 wennmach _DIAGASSERT(src != NULL);
214 1.15 wennmach _DIAGASSERT(extra != NULL);
215 1.15 wennmach
216 1.15 wennmach for (start = dst; (c = *src++) != '\0'; /* empty */)
217 1.15 wennmach SVIS(dst, c, flag, *src, extra);
218 1.15 wennmach *dst = '\0';
219 1.15 wennmach return (dst - start);
220 1.15 wennmach }
221 1.15 wennmach
222 1.15 wennmach
223 1.15 wennmach int
224 1.15 wennmach strsvisx(dst, src, len, flag, extra)
225 1.15 wennmach char *dst;
226 1.15 wennmach const char *src;
227 1.15 wennmach size_t len;
228 1.15 wennmach int flag;
229 1.15 wennmach const char *extra;
230 1.15 wennmach {
231 1.15 wennmach char c;
232 1.15 wennmach char *start;
233 1.15 wennmach
234 1.15 wennmach _DIAGASSERT(dst != NULL);
235 1.15 wennmach _DIAGASSERT(src != NULL);
236 1.15 wennmach _DIAGASSERT(extra != NULL);
237 1.15 wennmach
238 1.15 wennmach for (start = dst; len > 0; len--) {
239 1.15 wennmach c = *src++;
240 1.15 wennmach SVIS(dst, c, flag, len ? *src : '\0', extra);
241 1.15 wennmach }
242 1.15 wennmach *dst = '\0';
243 1.15 wennmach return (dst - start);
244 1.15 wennmach }
245 1.15 wennmach
246 1.1 cgd
247 1.1 cgd /*
248 1.1 cgd * vis - visually encode characters
249 1.1 cgd */
250 1.1 cgd char *
251 1.1 cgd vis(dst, c, flag, nextc)
252 1.15 wennmach char *dst;
253 1.15 wennmach int c, flag, nextc;
254 1.15 wennmach
255 1.15 wennmach {
256 1.15 wennmach char extra[] = {'\0', '\0', '\0', '\0', '\0'};
257 1.15 wennmach
258 1.15 wennmach _DIAGASSERT(dst != NULL);
259 1.15 wennmach
260 1.15 wennmach MAKEEXTRALIST(flag, extra);
261 1.15 wennmach SVIS(dst, c, flag, nextc, extra);
262 1.15 wennmach *dst = '\0';
263 1.15 wennmach return (dst);
264 1.1 cgd }
265 1.1 cgd
266 1.15 wennmach
267 1.1 cgd /*
268 1.1 cgd * strvis, strvisx - visually encode characters from src into dst
269 1.15 wennmach *
270 1.15 wennmach * Dst must be 4 times the size of src to account for possible
271 1.15 wennmach * expansion. The length of dst, not including the trailing NULL,
272 1.15 wennmach * is returned.
273 1.1 cgd *
274 1.15 wennmach * Strvisx encodes exactly len bytes from src into dst.
275 1.15 wennmach * This is useful for encoding a block of data.
276 1.1 cgd */
277 1.1 cgd int
278 1.1 cgd strvis(dst, src, flag)
279 1.15 wennmach char *dst;
280 1.15 wennmach const char *src;
281 1.15 wennmach int flag;
282 1.15 wennmach {
283 1.15 wennmach char extra[] = {'\0', '\0', '\0', '\0', '\0'};
284 1.15 wennmach
285 1.15 wennmach MAKEEXTRALIST(flag, extra);
286 1.15 wennmach return (strsvis(dst, src, flag, extra));
287 1.1 cgd }
288 1.1 cgd
289 1.15 wennmach
290 1.1 cgd int
291 1.1 cgd strvisx(dst, src, len, flag)
292 1.15 wennmach char *dst;
293 1.15 wennmach const char *src;
294 1.15 wennmach size_t len;
295 1.15 wennmach int flag;
296 1.15 wennmach {
297 1.15 wennmach char extra[] = {'\0', '\0', '\0', '\0', '\0'};
298 1.1 cgd
299 1.15 wennmach MAKEEXTRALIST(flag, extra);
300 1.15 wennmach return (strsvisx(dst, src, len, flag, extra));
301 1.1 cgd }
302