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