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