1 /* $OpenBSD: unvis.c,v 1.12 2005/08/08 08:05:34 espie Exp $ */
2 /*-
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <ctype.h>
33 #include <vis.h>
34
35 __RCSID("$MirOS: src/lib/libc/gen/unvis.c,v 1.3 2005/09/22 20:40:01 tg Exp $");
36
37 /*
38 * decode driven by state machine
39 */
40 #define S_GROUND 0 /* haven't seen escape char */
41 #define S_START 1 /* start decoding special sequence */
42 #define S_META 2 /* metachar started (M) */
43 #define S_META1 3 /* metachar more, regular char (-) */
44 #define S_CTRL 4 /* control char started (^) */
45 #define S_OCTAL2 5 /* octal digit 2 */
46 #define S_OCTAL3 6 /* octal digit 3 */
47
48 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
49
50 /*
51 * unvis - decode characters previously encoded by vis
52 */
53 int
unvis(char * cp,char c,int * astate,int flag)54 unvis(char *cp, char c, int *astate, int flag)
55 {
56
57 if (flag & UNVIS_END) {
58 if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
59 *astate = S_GROUND;
60 return (UNVIS_VALID);
61 }
62 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
63 }
64
65 switch (*astate) {
66
67 case S_GROUND:
68 *cp = 0;
69 if (c == '\\') {
70 *astate = S_START;
71 return (0);
72 }
73 *cp = c;
74 return (UNVIS_VALID);
75
76 case S_START:
77 switch(c) {
78 case '\\':
79 *cp = c;
80 *astate = S_GROUND;
81 return (UNVIS_VALID);
82 case '0': case '1': case '2': case '3':
83 case '4': case '5': case '6': case '7':
84 *cp = (c - '0');
85 *astate = S_OCTAL2;
86 return (0);
87 case 'M':
88 *cp = (char) 0200;
89 *astate = S_META;
90 return (0);
91 case '^':
92 *astate = S_CTRL;
93 return (0);
94 case 'n':
95 *cp = '\n';
96 *astate = S_GROUND;
97 return (UNVIS_VALID);
98 case 'r':
99 *cp = '\r';
100 *astate = S_GROUND;
101 return (UNVIS_VALID);
102 case 'b':
103 *cp = '\b';
104 *astate = S_GROUND;
105 return (UNVIS_VALID);
106 case 'a':
107 *cp = '\007';
108 *astate = S_GROUND;
109 return (UNVIS_VALID);
110 case 'v':
111 *cp = '\v';
112 *astate = S_GROUND;
113 return (UNVIS_VALID);
114 case 't':
115 *cp = '\t';
116 *astate = S_GROUND;
117 return (UNVIS_VALID);
118 case 'f':
119 *cp = '\f';
120 *astate = S_GROUND;
121 return (UNVIS_VALID);
122 case 's':
123 *cp = ' ';
124 *astate = S_GROUND;
125 return (UNVIS_VALID);
126 case 'E':
127 *cp = '\033';
128 *astate = S_GROUND;
129 return (UNVIS_VALID);
130 case '\n':
131 /*
132 * hidden newline
133 */
134 *astate = S_GROUND;
135 return (UNVIS_NOCHAR);
136 case '$':
137 /*
138 * hidden marker
139 */
140 *astate = S_GROUND;
141 return (UNVIS_NOCHAR);
142 }
143 *astate = S_GROUND;
144 return (UNVIS_SYNBAD);
145
146 case S_META:
147 if (c == '-')
148 *astate = S_META1;
149 else if (c == '^')
150 *astate = S_CTRL;
151 else {
152 *astate = S_GROUND;
153 return (UNVIS_SYNBAD);
154 }
155 return (0);
156
157 case S_META1:
158 *astate = S_GROUND;
159 *cp |= c;
160 return (UNVIS_VALID);
161
162 case S_CTRL:
163 if (c == '?')
164 *cp |= 0177;
165 else
166 *cp |= c & 037;
167 *astate = S_GROUND;
168 return (UNVIS_VALID);
169
170 case S_OCTAL2: /* second possible octal digit */
171 if (isoctal(c)) {
172 /*
173 * yes - and maybe a third
174 */
175 *cp = (*cp << 3) + (c - '0');
176 *astate = S_OCTAL3;
177 return (0);
178 }
179 /*
180 * no - done with current sequence, push back passed char
181 */
182 *astate = S_GROUND;
183 return (UNVIS_VALIDPUSH);
184
185 case S_OCTAL3: /* third possible octal digit */
186 *astate = S_GROUND;
187 if (isoctal(c)) {
188 *cp = (*cp << 3) + (c - '0');
189 return (UNVIS_VALID);
190 }
191 /*
192 * we were done, push back passed char
193 */
194 return (UNVIS_VALIDPUSH);
195
196 default:
197 /*
198 * decoder in unknown state - (probably uninitialized)
199 */
200 *astate = S_GROUND;
201 return (UNVIS_SYNBAD);
202 }
203 }
204
205 /*
206 * strunvis - decode src into dst
207 *
208 * Number of chars decoded into dst is returned, -1 on error.
209 * Dst is null terminated.
210 */
211
212 int
strunvis(char * dst,const char * src)213 strunvis(char *dst, const char *src)
214 {
215 char c;
216 char *start = dst;
217 int state = 0;
218
219 while ((c = *src++)) {
220 again:
221 switch (unvis(dst, c, &state, 0)) {
222 case UNVIS_VALID:
223 dst++;
224 break;
225 case UNVIS_VALIDPUSH:
226 dst++;
227 goto again;
228 case 0:
229 case UNVIS_NOCHAR:
230 break;
231 default:
232 *dst = '\0';
233 return (-1);
234 }
235 }
236 if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
237 dst++;
238 *dst = '\0';
239 return (dst - start);
240 }
241
242 ssize_t
strnunvis(char * dst,const char * src,size_t sz)243 strnunvis(char *dst, const char *src, size_t sz)
244 {
245 char c, p;
246 char *start = dst, *end = dst + sz - 1;
247 int state = 0;
248
249 if (sz > 0)
250 *end = '\0';
251 while ((c = *src++)) {
252 again:
253 switch (unvis(&p, c, &state, 0)) {
254 case UNVIS_VALID:
255 if (dst < end)
256 *dst = p;
257 dst++;
258 break;
259 case UNVIS_VALIDPUSH:
260 if (dst < end)
261 *dst = p;
262 dst++;
263 goto again;
264 case 0:
265 case UNVIS_NOCHAR:
266 break;
267 default:
268 if (dst <= end)
269 *dst = '\0';
270 return (-1);
271 }
272 }
273 if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) {
274 if (dst < end)
275 *dst = p;
276 dst++;
277 }
278 if (dst <= end)
279 *dst = '\0';
280 return (dst - start);
281 }
282