]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/vis.c
Libc-763.12.tar.gz
[apple/libc.git] / gen / FreeBSD / vis.c
CommitLineData
9385eb3d 1/*-
e9ce8d39
A
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. 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.
e9ce8d39
A
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
9385eb3d
A
30#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 7/19/93";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
1f2f436a 34__FBSDID("$FreeBSD: src/lib/libc/gen/vis.c,v 1.14 2007/01/09 00:27:56 imp Exp $");
e9ce8d39
A
35
36#include <sys/types.h>
37#include <limits.h>
38#include <ctype.h>
9385eb3d 39#include <stdio.h>
e9ce8d39
A
40#include <vis.h>
41
42#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
43
44/*
45 * vis - visually encode characters
46 */
47char *
48vis(dst, c, flag, nextc)
9385eb3d 49 char *dst;
e9ce8d39 50 int c, nextc;
9385eb3d 51 int flag;
e9ce8d39 52{
9385eb3d
A
53 c = (unsigned char)c;
54
55 if (flag & VIS_HTTPSTYLE) {
56 /* Described in RFC 1808 */
57 if (!(isalnum(c) /* alpha-numeric */
58 /* safe */
59 || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
60 /* extra */
61 || c == '!' || c == '*' || c == '\'' || c == '('
62 || c == ')' || c == ',')) {
63 *dst++ = '%';
64 snprintf(dst, 4, (c < 16 ? "0%X" : "%X"), c);
65 dst += 2;
66 goto done;
67 }
68 }
69
3d9156a7
A
70 if ((flag & VIS_GLOB) &&
71 (c == '*' || c == '?' || c == '[' || c == '#'))
72 ;
73 else if (isgraph(c) ||
e9ce8d39
A
74 ((flag & VIS_SP) == 0 && c == ' ') ||
75 ((flag & VIS_TAB) == 0 && c == '\t') ||
76 ((flag & VIS_NL) == 0 && c == '\n') ||
77 ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
78 *dst++ = c;
79 if (c == '\\' && (flag & VIS_NOSLASH) == 0)
80 *dst++ = '\\';
81 *dst = '\0';
82 return (dst);
83 }
84
85 if (flag & VIS_CSTYLE) {
86 switch(c) {
87 case '\n':
88 *dst++ = '\\';
89 *dst++ = 'n';
90 goto done;
91 case '\r':
92 *dst++ = '\\';
93 *dst++ = 'r';
94 goto done;
95 case '\b':
96 *dst++ = '\\';
97 *dst++ = 'b';
98 goto done;
e9ce8d39 99 case '\a':
e9ce8d39
A
100 *dst++ = '\\';
101 *dst++ = 'a';
102 goto done;
103 case '\v':
104 *dst++ = '\\';
105 *dst++ = 'v';
106 goto done;
107 case '\t':
108 *dst++ = '\\';
109 *dst++ = 't';
110 goto done;
111 case '\f':
112 *dst++ = '\\';
113 *dst++ = 'f';
114 goto done;
115 case ' ':
116 *dst++ = '\\';
117 *dst++ = 's';
118 goto done;
119 case '\0':
120 *dst++ = '\\';
121 *dst++ = '0';
122 if (isoctal(nextc)) {
123 *dst++ = '0';
124 *dst++ = '0';
125 }
126 goto done;
127 }
128 }
3d9156a7 129 if (((c & 0177) == ' ') || isgraph(c) || (flag & VIS_OCTAL)) {
e9ce8d39
A
130 *dst++ = '\\';
131 *dst++ = ((u_char)c >> 6 & 07) + '0';
132 *dst++ = ((u_char)c >> 3 & 07) + '0';
133 *dst++ = ((u_char)c & 07) + '0';
134 goto done;
135 }
136 if ((flag & VIS_NOSLASH) == 0)
137 *dst++ = '\\';
138 if (c & 0200) {
139 c &= 0177;
140 *dst++ = 'M';
141 }
142 if (iscntrl(c)) {
143 *dst++ = '^';
144 if (c == 0177)
145 *dst++ = '?';
146 else
147 *dst++ = c + '@';
148 } else {
149 *dst++ = '-';
150 *dst++ = c;
151 }
152done:
153 *dst = '\0';
154 return (dst);
155}
156
157/*
158 * strvis, strvisx - visually encode characters from src into dst
9385eb3d 159 *
e9ce8d39 160 * Dst must be 4 times the size of src to account for possible
9385eb3d
A
161 * expansion. The length of dst, not including the trailing NUL,
162 * is returned.
e9ce8d39
A
163 *
164 * Strvisx encodes exactly len bytes from src into dst.
165 * This is useful for encoding a block of data.
166 */
167int
168strvis(dst, src, flag)
9385eb3d
A
169 char *dst;
170 const char *src;
e9ce8d39
A
171 int flag;
172{
9385eb3d 173 char c;
e9ce8d39
A
174 char *start;
175
9385eb3d 176 for (start = dst; (c = *src); )
e9ce8d39
A
177 dst = vis(dst, c, flag, *++src);
178 *dst = '\0';
179 return (dst - start);
180}
181
182int
183strvisx(dst, src, len, flag)
9385eb3d
A
184 char *dst;
185 const char *src;
186 size_t len;
e9ce8d39
A
187 int flag;
188{
189 int c;
190 char *start;
191
192 for (start = dst; len > 1; len--) {
193 c = *src;
194 dst = vis(dst, c, flag, *++src);
195 }
196 if (len)
197 dst = vis(dst, *src, flag, '\0');
198 *dst = '\0';
199
200 return (dst - start);
201}