Annotation of src/lib/libc/gen/vis.c, Revision 1.19.6.1
1.19.6.1! nathanw 1: /* $NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $ */
1.6 cgd 2:
1.1 cgd 3: /*-
1.15 wennmach 4: * Copyright (c) 1999 The NetBSD Foundation, Inc.
1.6 cgd 5: * Copyright (c) 1989, 1993
1.16 wennmach 6: * The Regents of the University of California. All rights reserved.
1.1 cgd 7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
1.16 wennmach 18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
1.1 cgd 20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
1.19.6.1! nathanw 37: #if HAVE_CONFIG_H
! 38: #include "config.h"
! 39: #else
1.7 christos 40: #include <sys/cdefs.h>
1.15 wennmach 41: #if !defined(lint)
1.19.6.1! nathanw 42: __RCSID("$NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $");
1.15 wennmach 43: #endif /* not lint */
1.1 cgd 44:
1.8 jtc 45: #include "namespace.h"
1.1 cgd 46: #include <sys/types.h>
1.12 lukem 47:
48: #include <assert.h>
1.1 cgd 49: #include <vis.h>
1.8 jtc 50:
51: #ifdef __weak_alias
1.18 mycroft 52: __weak_alias(strsvis,_strsvis)
53: __weak_alias(strsvisx,_strsvisx)
54: __weak_alias(strvis,_strvis)
55: __weak_alias(strvisx,_strvisx)
56: __weak_alias(svis,_svis)
57: __weak_alias(vis,_vis)
1.8 jtc 58: #endif
1.19.6.1! nathanw 59: #endif
! 60:
! 61: #if !HAVE_VIS_H
! 62: #include <ctype.h>
! 63: #include <limits.h>
! 64: #include <stdio.h>
! 65: #include <string.h>
1.1 cgd 66:
1.15 wennmach 67: #undef BELL
68: #if defined(__STDC__)
69: #define BELL '\a'
70: #else
71: #define BELL '\007'
72: #endif
73:
1.16 wennmach 74: #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
75: #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
76: #define issafe(c) (c == '\b' || c == BELL || c == '\r')
77:
78: #define MAXEXTRAS 5
1.15 wennmach 79:
80:
1.16 wennmach 81: #define MAKEEXTRALIST(flag, extra) \
82: do { \
83: char *pextra = extra; \
84: if (flag & VIS_SP) *pextra++ = ' '; \
85: if (flag & VIS_TAB) *pextra++ = '\t'; \
86: if (flag & VIS_NL) *pextra++ = '\n'; \
87: if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\'; \
88: *pextra = '\0'; \
1.19 mycroft 89: } while (/*CONSTCOND*/0)
1.15 wennmach 90:
91: /*
92: * This is SVIS, the central macro of vis.
1.16 wennmach 93: * dst: Pointer to the destination buffer
94: * c: Character to encode
1.15 wennmach 95: * flag: Flag word
96: * nextc: The character following 'c'
97: * extra: Pointer to the list of extra characters to be
1.16 wennmach 98: * backslash-protected.
1.15 wennmach 99: */
1.16 wennmach 100: #define SVIS(dst, c, flag, nextc, extra) \
101: do { \
102: int isextra, isc; \
103: isextra = strchr(extra, c) != NULL; \
1.17 wennmach 104: if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || \
105: ((flag & VIS_SAFE) && issafe(c)))) { \
1.16 wennmach 106: *dst++ = c; \
107: break; \
108: } \
109: isc = 0; \
110: if (flag & VIS_CSTYLE) { \
111: switch (c) { \
112: case '\n': \
113: isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
114: break; \
115: case '\r': \
116: isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
117: break; \
118: case '\b': \
119: isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
120: break; \
121: case BELL: \
122: isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
123: break; \
124: case '\v': \
125: isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
126: break; \
127: case '\t': \
128: isc = 1; *dst++ = '\\'; *dst++ = 't'; \
129: break; \
130: case '\f': \
131: isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
132: break; \
133: case ' ': \
134: isc = 1; *dst++ = '\\'; *dst++ = 's'; \
135: break; \
136: case '\0': \
137: isc = 1; *dst++ = '\\'; *dst++ = '0'; \
138: if (isoctal(nextc)) { \
139: *dst++ = '0'; \
140: *dst++ = '0'; \
141: } \
142: } \
143: } \
144: if (isc) break; \
1.17 wennmach 145: if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
1.16 wennmach 146: *dst++ = '\\'; \
147: *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \
148: *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \
149: *dst++ = (c & 07) + '0'; \
150: } else { \
151: if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
152: if (c & 0200) { \
153: c &= 0177; *dst++ = 'M'; \
154: } \
155: if (iscntrl(c)) { \
156: *dst++ = '^'; \
157: if (c == 0177) \
158: *dst++ = '?'; \
159: else \
160: *dst++ = c + '@'; \
161: } else { \
162: *dst++ = '-'; *dst++ = c; \
163: } \
164: } \
1.19 mycroft 165: } while (/*CONSTCOND*/0)
1.15 wennmach 166:
167:
168: /*
1.17 wennmach 169: * svis - visually encode characters, also encoding the characters
170: * pointed to by `extra'
1.15 wennmach 171: */
172: char *
173: svis(dst, c, flag, nextc, extra)
1.16 wennmach 174: char *dst;
175: int c, flag, nextc;
176: const char *extra;
1.15 wennmach 177: {
1.16 wennmach 178: _DIAGASSERT(dst != NULL);
179: _DIAGASSERT(extra != NULL);
1.15 wennmach 180:
1.16 wennmach 181: SVIS(dst, c, flag, nextc, extra);
182: *dst = '\0';
183: return(dst);
1.15 wennmach 184: }
185:
186:
187: /*
188: * strsvis, strsvisx - visually encode characters from src into dst
189: *
1.16 wennmach 190: * Extra is a pointer to a \0-terminated list of characters to
1.17 wennmach 191: * be encoded, too. These functions are useful e. g. to
192: * encode strings in such a way so that they are not interpreted
1.16 wennmach 193: * by a shell.
194: *
195: * Dst must be 4 times the size of src to account for possible
196: * expansion. The length of dst, not including the trailing NULL,
197: * is returned.
1.15 wennmach 198: *
1.16 wennmach 199: * Strsvisx encodes exactly len bytes from src into dst.
200: * This is useful for encoding a block of data.
1.15 wennmach 201: */
202: int
203: strsvis(dst, src, flag, extra)
1.16 wennmach 204: char *dst;
205: const char *src;
206: int flag;
207: const char *extra;
1.15 wennmach 208: {
1.16 wennmach 209: char c;
210: char *start;
1.15 wennmach 211:
1.16 wennmach 212: _DIAGASSERT(dst != NULL);
213: _DIAGASSERT(src != NULL);
214: _DIAGASSERT(extra != NULL);
215:
216: for (start = dst; (c = *src++) != '\0'; /* empty */)
217: SVIS(dst, c, flag, *src, extra);
218: *dst = '\0';
219: return (dst - start);
1.15 wennmach 220: }
221:
222:
223: int
224: strsvisx(dst, src, len, flag, extra)
1.16 wennmach 225: char *dst;
226: const char *src;
227: size_t len;
228: int flag;
229: const char *extra;
1.15 wennmach 230: {
1.16 wennmach 231: char c;
232: char *start;
1.15 wennmach 233:
1.16 wennmach 234: _DIAGASSERT(dst != NULL);
235: _DIAGASSERT(src != NULL);
236: _DIAGASSERT(extra != NULL);
237:
238: for (start = dst; len > 0; len--) {
239: c = *src++;
240: SVIS(dst, c, flag, len ? *src : '\0', extra);
241: }
242: *dst = '\0';
243: return (dst - start);
1.15 wennmach 244: }
245:
1.1 cgd 246:
247: /*
248: * vis - visually encode characters
249: */
250: char *
251: vis(dst, c, flag, nextc)
1.16 wennmach 252: char *dst;
253: int c, flag, nextc;
254:
1.15 wennmach 255: {
1.16 wennmach 256: char extra[MAXEXTRAS];
1.15 wennmach 257:
1.16 wennmach 258: _DIAGASSERT(dst != NULL);
1.15 wennmach 259:
1.16 wennmach 260: MAKEEXTRALIST(flag, extra);
261: SVIS(dst, c, flag, nextc, extra);
262: *dst = '\0';
263: return (dst);
1.1 cgd 264: }
265:
1.15 wennmach 266:
1.1 cgd 267: /*
268: * strvis, strvisx - visually encode characters from src into dst
1.16 wennmach 269: *
270: * Dst must be 4 times the size of src to account for possible
271: * expansion. The length of dst, not including the trailing NULL,
272: * is returned.
1.1 cgd 273: *
1.16 wennmach 274: * Strvisx encodes exactly len bytes from src into dst.
275: * This is useful for encoding a block of data.
1.1 cgd 276: */
277: int
278: strvis(dst, src, flag)
1.16 wennmach 279: char *dst;
280: const char *src;
281: int flag;
1.15 wennmach 282: {
1.16 wennmach 283: char extra[MAXEXTRAS];
1.15 wennmach 284:
1.16 wennmach 285: MAKEEXTRALIST(flag, extra);
286: return (strsvis(dst, src, flag, extra));
1.1 cgd 287: }
288:
1.15 wennmach 289:
1.1 cgd 290: int
291: strvisx(dst, src, len, flag)
1.16 wennmach 292: char *dst;
293: const char *src;
294: size_t len;
295: int flag;
1.15 wennmach 296: {
1.16 wennmach 297: char extra[MAXEXTRAS];
1.1 cgd 298:
1.16 wennmach 299: MAKEEXTRALIST(flag, extra);
300: return (strsvisx(dst, src, len, flag, extra));
1.1 cgd 301: }
1.19.6.1! nathanw 302: #endif
CVSweb <webmaster@jp.NetBSD.org>