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