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