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