|
|
1.1 root 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 are permitted provided
6: * that: (1) source distributions retain this entire copyright notice and
7: * comment, and (2) distributions including binaries display the following
8: * acknowledgement: ``This product includes software developed by the
9: * University of California, Berkeley and its contributors'' in the
10: * documentation or other materials provided with the distribution and in
11: * all advertising materials mentioning features or use of this software.
12: * Neither the name of the University nor the names of its contributors may
13: * be used to endorse or promote products derived from this software without
14: * specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: static char sccsid[] = "@(#)vis.c 5.3 (Berkeley) 6/26/90";
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include <sys/types.h>
25: #include <ctype.h>
26: #include <vis.h>
27:
28: #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
29:
30: /*
31: * vis - visually encode characters
32: */
33: char *
34: vis(dst, c, flag, nextc)
35: register char *dst, c;
36: char nextc;
37: register int flag;
38: {
39: if (isascii(c) && isgraph(c) ||
40: ((flag & VIS_SP) == 0 && c == ' ') ||
41: ((flag & VIS_TAB) == 0 && c == '\t') ||
42: ((flag & VIS_NL) == 0 && c == '\n') ||
43: ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
44: *dst++ = c;
45: if (c == '\\' && (flag & VIS_NOSLASH) == 0)
46: *dst++ = '\\';
47: *dst = '\0';
48: return (dst);
49: }
50:
51: if (flag & VIS_CSTYLE) {
52: switch(c) {
53: case '\n':
54: *dst++ = '\\';
55: *dst++ = 'n';
56: goto done;
57: case '\r':
58: *dst++ = '\\';
59: *dst++ = 'r';
60: goto done;
61: case '\b':
62: *dst++ = '\\';
63: *dst++ = 'b';
64: goto done;
65: case '\007': /* waiting for ansi compiler */
66: *dst++ = '\\';
67: *dst++ = 'a';
68: goto done;
69: case '\v':
70: *dst++ = '\\';
71: *dst++ = 'v';
72: goto done;
73: case '\t':
74: *dst++ = '\\';
75: *dst++ = 't';
76: goto done;
77: case '\f':
78: *dst++ = '\\';
79: *dst++ = 'f';
80: goto done;
81: case ' ':
82: *dst++ = '\\';
83: *dst++ = 's';
84: goto done;
85: case '\0':
86: *dst++ = '\\';
87: *dst++ = '0';
88: if (isoctal(nextc)) {
89: *dst++ = '0';
90: *dst++ = '0';
91: }
92: goto done;
93: }
94: }
95: if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
96: *dst++ = '\\';
97: *dst++ = ((u_char)c >> 6 & 07) + '0';
98: *dst++ = ((u_char)c >> 3 & 07) + '0';
99: *dst++ = ((u_char)c & 07) + '0';
100: goto done;
101: }
102: if ((flag & VIS_NOSLASH) == 0)
103: *dst++ = '\\';
104: if (c & 0200) {
105: c &= 0177;
106: *dst++ = 'M';
107: }
108: if (iscntrl(c)) {
109: *dst++ = '^';
110: if (c == 0177)
111: *dst++ = '?';
112: else
113: *dst++ = c + '@';
114: } else {
115: *dst++ = '-';
116: *dst++ = c;
117: }
118: done:
119: *dst = '\0';
120: return (dst);
121: }
122:
123: /*
124: * strvis, strvisx - visually encode characters from src into dst
125: *
126: * Dst must be 4 times the size of src to account for possible
127: * expansion. The length of dst, not including the trailing NULL,
128: * is returned.
129: *
130: * Strvisx encodes exactly len bytes from src into dst.
131: * This is useful for encoding a block of data.
132: */
133: strvis(dst, src, flag)
134: register char *dst, *src;
135: {
136: register char c;
137: char *start = dst;
138:
139: for (;c = *src; src++)
140: dst = vis(dst, c, flag, *(src+1));
141:
142: return (dst - start);
143: }
144:
145: strvisx(dst, src, len, flag)
146: register char *dst, *src;
147: register int len;
148: {
149: char *start = dst;
150:
151: while (len > 1) {
152: dst = vis(dst, *src, flag, *(src+1));
153: len--;
154: }
155: if (len)
156: dst = vis(dst, *src, flag, '\0');
157:
158: return (dst - start);
159: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.