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