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