vis.c revision 1.12 1 1.12 lukem /* $NetBSD: vis.c,v 1.12 1999/09/16 11:45:07 lukem Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.6 cgd * Copyright (c) 1989, 1993
5 1.6 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.7 christos #include <sys/cdefs.h>
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.6 cgd #if 0
39 1.6 cgd static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 7/19/93";
40 1.6 cgd #else
41 1.12 lukem __RCSID("$NetBSD: vis.c,v 1.12 1999/09/16 11:45:07 lukem Exp $");
42 1.6 cgd #endif
43 1.1 cgd #endif /* LIBC_SCCS and not lint */
44 1.1 cgd
45 1.8 jtc #include "namespace.h"
46 1.1 cgd #include <sys/types.h>
47 1.12 lukem
48 1.12 lukem #include <assert.h>
49 1.12 lukem #include <ctype.h>
50 1.2 cgd #include <limits.h>
51 1.12 lukem #include <stdio.h>
52 1.1 cgd #include <vis.h>
53 1.8 jtc
54 1.8 jtc #ifdef __weak_alias
55 1.8 jtc __weak_alias(strvis,_strvis);
56 1.8 jtc __weak_alias(strvisx,_strvisx);
57 1.8 jtc __weak_alias(vis,_vis);
58 1.8 jtc #endif
59 1.1 cgd
60 1.1 cgd #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
61 1.1 cgd
62 1.1 cgd /*
63 1.1 cgd * vis - visually encode characters
64 1.1 cgd */
65 1.1 cgd char *
66 1.1 cgd vis(dst, c, flag, nextc)
67 1.9 perry char *dst;
68 1.3 mycroft int c, nextc;
69 1.9 perry int flag;
70 1.1 cgd {
71 1.12 lukem
72 1.12 lukem _DIAGASSERT(dst != NULL);
73 1.12 lukem #ifdef _DIAGNOSTIC
74 1.12 lukem if (dst == NULL)
75 1.12 lukem return (NULL);
76 1.12 lukem #endif
77 1.12 lukem
78 1.7 christos if (((u_int)c <= UCHAR_MAX && isascii(c) && isgraph(c)) ||
79 1.1 cgd ((flag & VIS_SP) == 0 && c == ' ') ||
80 1.1 cgd ((flag & VIS_TAB) == 0 && c == '\t') ||
81 1.1 cgd ((flag & VIS_NL) == 0 && c == '\n') ||
82 1.1 cgd ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
83 1.1 cgd *dst++ = c;
84 1.1 cgd if (c == '\\' && (flag & VIS_NOSLASH) == 0)
85 1.1 cgd *dst++ = '\\';
86 1.1 cgd *dst = '\0';
87 1.1 cgd return (dst);
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd if (flag & VIS_CSTYLE) {
91 1.1 cgd switch(c) {
92 1.1 cgd case '\n':
93 1.1 cgd *dst++ = '\\';
94 1.1 cgd *dst++ = 'n';
95 1.1 cgd goto done;
96 1.1 cgd case '\r':
97 1.1 cgd *dst++ = '\\';
98 1.1 cgd *dst++ = 'r';
99 1.1 cgd goto done;
100 1.1 cgd case '\b':
101 1.1 cgd *dst++ = '\\';
102 1.1 cgd *dst++ = 'b';
103 1.1 cgd goto done;
104 1.1 cgd #if __STDC__
105 1.1 cgd case '\a':
106 1.1 cgd #else
107 1.1 cgd case '\007':
108 1.1 cgd #endif
109 1.1 cgd *dst++ = '\\';
110 1.1 cgd *dst++ = 'a';
111 1.1 cgd goto done;
112 1.1 cgd case '\v':
113 1.1 cgd *dst++ = '\\';
114 1.1 cgd *dst++ = 'v';
115 1.1 cgd goto done;
116 1.1 cgd case '\t':
117 1.1 cgd *dst++ = '\\';
118 1.1 cgd *dst++ = 't';
119 1.1 cgd goto done;
120 1.1 cgd case '\f':
121 1.1 cgd *dst++ = '\\';
122 1.1 cgd *dst++ = 'f';
123 1.1 cgd goto done;
124 1.1 cgd case ' ':
125 1.1 cgd *dst++ = '\\';
126 1.1 cgd *dst++ = 's';
127 1.1 cgd goto done;
128 1.1 cgd case '\0':
129 1.1 cgd *dst++ = '\\';
130 1.1 cgd *dst++ = '0';
131 1.1 cgd if (isoctal(nextc)) {
132 1.1 cgd *dst++ = '0';
133 1.1 cgd *dst++ = '0';
134 1.1 cgd }
135 1.1 cgd goto done;
136 1.1 cgd }
137 1.1 cgd }
138 1.1 cgd if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
139 1.1 cgd *dst++ = '\\';
140 1.11 christos *dst++ = ((((u_int32_t)c) >> 6) & 07) + '0';
141 1.11 christos *dst++ = ((((u_int32_t)c) >> 3) & 07) + '0';
142 1.10 christos *dst++ = (((u_char)c) & 07) + '0';
143 1.1 cgd goto done;
144 1.1 cgd }
145 1.1 cgd if ((flag & VIS_NOSLASH) == 0)
146 1.1 cgd *dst++ = '\\';
147 1.1 cgd if (c & 0200) {
148 1.1 cgd c &= 0177;
149 1.1 cgd *dst++ = 'M';
150 1.1 cgd }
151 1.1 cgd if (iscntrl(c)) {
152 1.1 cgd *dst++ = '^';
153 1.1 cgd if (c == 0177)
154 1.1 cgd *dst++ = '?';
155 1.1 cgd else
156 1.1 cgd *dst++ = c + '@';
157 1.1 cgd } else {
158 1.1 cgd *dst++ = '-';
159 1.1 cgd *dst++ = c;
160 1.1 cgd }
161 1.1 cgd done:
162 1.1 cgd *dst = '\0';
163 1.1 cgd return (dst);
164 1.1 cgd }
165 1.1 cgd
166 1.1 cgd /*
167 1.1 cgd * strvis, strvisx - visually encode characters from src into dst
168 1.1 cgd *
169 1.1 cgd * Dst must be 4 times the size of src to account for possible
170 1.1 cgd * expansion. The length of dst, not including the trailing NULL,
171 1.1 cgd * is returned.
172 1.1 cgd *
173 1.1 cgd * Strvisx encodes exactly len bytes from src into dst.
174 1.1 cgd * This is useful for encoding a block of data.
175 1.1 cgd */
176 1.1 cgd int
177 1.1 cgd strvis(dst, src, flag)
178 1.9 perry char *dst;
179 1.9 perry const char *src;
180 1.1 cgd int flag;
181 1.1 cgd {
182 1.9 perry char c;
183 1.2 cgd char *start;
184 1.1 cgd
185 1.12 lukem _DIAGASSERT(dst != NULL);
186 1.12 lukem _DIAGASSERT(src != NULL);
187 1.12 lukem #ifdef _DIAGNOSTIC
188 1.12 lukem if (dst == NULL || src == NULL)
189 1.12 lukem return (0);
190 1.12 lukem #endif
191 1.12 lukem
192 1.7 christos for (start = dst; (c = *src) != '\0';)
193 1.2 cgd dst = vis(dst, c, flag, *++src);
194 1.2 cgd *dst = '\0';
195 1.1 cgd return (dst - start);
196 1.1 cgd }
197 1.1 cgd
198 1.1 cgd int
199 1.1 cgd strvisx(dst, src, len, flag)
200 1.9 perry char *dst;
201 1.9 perry const char *src;
202 1.9 perry size_t len;
203 1.1 cgd int flag;
204 1.1 cgd {
205 1.9 perry char c;
206 1.4 mycroft char *start;
207 1.12 lukem
208 1.12 lukem _DIAGASSERT(dst != NULL);
209 1.12 lukem _DIAGASSERT(src != NULL);
210 1.12 lukem #ifdef _DIAGNOSTIC
211 1.12 lukem if (dst == NULL || src == NULL)
212 1.12 lukem return (0);
213 1.12 lukem #endif
214 1.1 cgd
215 1.4 mycroft for (start = dst; len > 1; len--) {
216 1.4 mycroft c = *src;
217 1.4 mycroft dst = vis(dst, c, flag, *++src);
218 1.1 cgd }
219 1.1 cgd if (len)
220 1.1 cgd dst = vis(dst, *src, flag, '\0');
221 1.4 mycroft *dst = '\0';
222 1.1 cgd
223 1.1 cgd return (dst - start);
224 1.1 cgd }
225