unvis.c revision 1.13 1 /* $NetBSD: unvis.c,v 1.13 1998/11/13 12:31:53 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: unvis.c,v 1.13 1998/11/13 12:31:53 christos Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #define __LIBC12_SOURCE__
46
47 #include "namespace.h"
48 #include <sys/types.h>
49 #include <ctype.h>
50 #include <vis.h>
51
52 #ifdef __weak_alias
53 __weak_alias(strunvis,_strunvis);
54 __weak_alias(unvis,_unvis);
55 #error "XXX THIS IS NOT CORRECT!"
56 __weak_alias(__unvis13,___unvis13);
57 #endif
58
59 /*
60 * decode driven by state machine
61 */
62 #define S_GROUND 0 /* haven't seen escape char */
63 #define S_START 1 /* start decoding special sequence */
64 #define S_META 2 /* metachar started (M) */
65 #define S_META1 3 /* metachar more, regular char (-) */
66 #define S_CTRL 4 /* control char started (^) */
67 #define S_OCTAL2 5 /* octal digit 2 */
68 #define S_OCTAL3 6 /* octal digit 3 */
69
70 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
71
72 int
73 unvis(cp, c, astate, flag)
74 char *cp;
75 int c;
76 int *astate, flag;
77 {
78 return __unvis13(cp, (int)c, astate, flag);
79 }
80
81 /*
82 * unvis - decode characters previously encoded by vis
83 */
84 int
85 __unvis13(cp, c, astate, flag)
86 char *cp;
87 int c;
88 int *astate, flag;
89 {
90
91 if (flag & UNVIS_END) {
92 if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
93 *astate = S_GROUND;
94 return (UNVIS_VALID);
95 }
96 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
97 }
98
99 switch (*astate) {
100
101 case S_GROUND:
102 *cp = 0;
103 if (c == '\\') {
104 *astate = S_START;
105 return (0);
106 }
107 *cp = c;
108 return (UNVIS_VALID);
109
110 case S_START:
111 switch(c) {
112 case '\\':
113 *cp = c;
114 *astate = S_GROUND;
115 return (UNVIS_VALID);
116 case '0': case '1': case '2': case '3':
117 case '4': case '5': case '6': case '7':
118 *cp = (c - '0');
119 *astate = S_OCTAL2;
120 return (0);
121 case 'M':
122 *cp = (char)0200;
123 *astate = S_META;
124 return (0);
125 case '^':
126 *astate = S_CTRL;
127 return (0);
128 case 'n':
129 *cp = '\n';
130 *astate = S_GROUND;
131 return (UNVIS_VALID);
132 case 'r':
133 *cp = '\r';
134 *astate = S_GROUND;
135 return (UNVIS_VALID);
136 case 'b':
137 *cp = '\b';
138 *astate = S_GROUND;
139 return (UNVIS_VALID);
140 case 'a':
141 *cp = '\007';
142 *astate = S_GROUND;
143 return (UNVIS_VALID);
144 case 'v':
145 *cp = '\v';
146 *astate = S_GROUND;
147 return (UNVIS_VALID);
148 case 't':
149 *cp = '\t';
150 *astate = S_GROUND;
151 return (UNVIS_VALID);
152 case 'f':
153 *cp = '\f';
154 *astate = S_GROUND;
155 return (UNVIS_VALID);
156 case 's':
157 *cp = ' ';
158 *astate = S_GROUND;
159 return (UNVIS_VALID);
160 case 'E':
161 *cp = '\033';
162 *astate = S_GROUND;
163 return (UNVIS_VALID);
164 case '\n':
165 /*
166 * hidden newline
167 */
168 *astate = S_GROUND;
169 return (UNVIS_NOCHAR);
170 case '$':
171 /*
172 * hidden marker
173 */
174 *astate = S_GROUND;
175 return (UNVIS_NOCHAR);
176 }
177 *astate = S_GROUND;
178 return (UNVIS_SYNBAD);
179
180 case S_META:
181 if (c == '-')
182 *astate = S_META1;
183 else if (c == '^')
184 *astate = S_CTRL;
185 else {
186 *astate = S_GROUND;
187 return (UNVIS_SYNBAD);
188 }
189 return (0);
190
191 case S_META1:
192 *astate = S_GROUND;
193 *cp |= c;
194 return (UNVIS_VALID);
195
196 case S_CTRL:
197 if (c == '?')
198 *cp |= 0177;
199 else
200 *cp |= c & 037;
201 *astate = S_GROUND;
202 return (UNVIS_VALID);
203
204 case S_OCTAL2: /* second possible octal digit */
205 if (isoctal(c)) {
206 /*
207 * yes - and maybe a third
208 */
209 *cp = (*cp << 3) + (c - '0');
210 *astate = S_OCTAL3;
211 return (0);
212 }
213 /*
214 * no - done with current sequence, push back passed char
215 */
216 *astate = S_GROUND;
217 return (UNVIS_VALIDPUSH);
218
219 case S_OCTAL3: /* third possible octal digit */
220 *astate = S_GROUND;
221 if (isoctal(c)) {
222 *cp = (*cp << 3) + (c - '0');
223 return (UNVIS_VALID);
224 }
225 /*
226 * we were done, push back passed char
227 */
228 return (UNVIS_VALIDPUSH);
229
230 default:
231 /*
232 * decoder in unknown state - (probably uninitialized)
233 */
234 *astate = S_GROUND;
235 return (UNVIS_SYNBAD);
236 }
237 }
238
239 /*
240 * strunvis - decode src into dst
241 *
242 * Number of chars decoded into dst is returned, -1 on error.
243 * Dst is null terminated.
244 */
245
246 int
247 strunvis(dst, src)
248 char *dst;
249 const char *src;
250 {
251 char c;
252 char *start = dst;
253 int state = 0;
254
255 while ((c = *src++) != '\0') {
256 again:
257 switch (__unvis13(dst, c, &state, 0)) {
258 case UNVIS_VALID:
259 dst++;
260 break;
261 case UNVIS_VALIDPUSH:
262 dst++;
263 goto again;
264 case 0:
265 case UNVIS_NOCHAR:
266 break;
267 default:
268 return (-1);
269 }
270 }
271 if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID)
272 dst++;
273 *dst = '\0';
274 return (dst - start);
275 }
276