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