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