etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / roken / unvis.c
blob8a415a53ace6e6e8800dc22e68fd8e7db0f66d73
1 /* $NetBSD: unvis.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
3 /* NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp */
5 /*-
6 * Copyright (c) 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if 1
35 #include <config.h>
36 #include <krb5/roken.h>
37 #ifndef _DIAGASSERT
38 #define _DIAGASSERT(X)
39 #endif
40 #else
41 #include <sys/cdefs.h>
42 #if defined(LIBC_SCCS) && !defined(lint)
43 #if 0
44 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
45 #else
46 __RCSID("NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp");
47 #endif
48 #endif /* LIBC_SCCS and not lint */
50 #define __LIBC12_SOURCE__
52 #include "namespace.h"
53 #endif
54 #include <sys/types.h>
56 #include <assert.h>
57 #include <ctype.h>
58 #include <stdio.h>
59 #include <vis.h>
61 #if 0
62 #ifdef __weak_alias
63 __weak_alias(strunvis,_strunvis)
64 __weak_alias(unvis,_unvis)
65 #endif
67 __warn_references(unvis,
68 "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
69 #endif
72 * decode driven by state machine
74 #define S_GROUND 0 /* haven't seen escape char */
75 #define S_START 1 /* start decoding special sequence */
76 #define S_META 2 /* metachar started (M) */
77 #define S_META1 3 /* metachar more, regular char (-) */
78 #define S_CTRL 4 /* control char started (^) */
79 #define S_OCTAL2 5 /* octal digit 2 */
80 #define S_OCTAL3 6 /* octal digit 3 */
82 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
84 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
85 rk_strunvis (char *, const char *);
86 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
87 rk_unvis (char *, int, int *, int);
90 * unvis - decode characters previously encoded by vis
93 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
94 rk_unvis(char *cp, int c, int *astate, int flag)
97 _DIAGASSERT(cp != NULL);
98 _DIAGASSERT(astate != NULL);
100 if (flag & UNVIS_END) {
101 if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
102 *astate = S_GROUND;
103 return (UNVIS_VALID);
105 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
108 switch (*astate) {
110 case S_GROUND:
111 *cp = 0;
112 if (c == '\\') {
113 *astate = S_START;
114 return (0);
116 *cp = c;
117 return (UNVIS_VALID);
119 case S_START:
120 switch(c) {
121 case '\\':
122 *cp = c;
123 *astate = S_GROUND;
124 return (UNVIS_VALID);
125 case '0': case '1': case '2': case '3':
126 case '4': case '5': case '6': case '7':
127 *cp = (c - '0');
128 *astate = S_OCTAL2;
129 return (0);
130 case 'M':
131 *cp = (u_char)0200;
132 *astate = S_META;
133 return (0);
134 case '^':
135 *astate = S_CTRL;
136 return (0);
137 case 'n':
138 *cp = '\n';
139 *astate = S_GROUND;
140 return (UNVIS_VALID);
141 case 'r':
142 *cp = '\r';
143 *astate = S_GROUND;
144 return (UNVIS_VALID);
145 case 'b':
146 *cp = '\b';
147 *astate = S_GROUND;
148 return (UNVIS_VALID);
149 case 'a':
150 *cp = '\007';
151 *astate = S_GROUND;
152 return (UNVIS_VALID);
153 case 'v':
154 *cp = '\v';
155 *astate = S_GROUND;
156 return (UNVIS_VALID);
157 case 't':
158 *cp = '\t';
159 *astate = S_GROUND;
160 return (UNVIS_VALID);
161 case 'f':
162 *cp = '\f';
163 *astate = S_GROUND;
164 return (UNVIS_VALID);
165 case 's':
166 *cp = ' ';
167 *astate = S_GROUND;
168 return (UNVIS_VALID);
169 case 'E':
170 *cp = '\033';
171 *astate = S_GROUND;
172 return (UNVIS_VALID);
173 case '\n':
175 * hidden newline
177 *astate = S_GROUND;
178 return (UNVIS_NOCHAR);
179 case '$':
181 * hidden marker
183 *astate = S_GROUND;
184 return (UNVIS_NOCHAR);
186 *astate = S_GROUND;
187 return (UNVIS_SYNBAD);
189 case S_META:
190 if (c == '-')
191 *astate = S_META1;
192 else if (c == '^')
193 *astate = S_CTRL;
194 else {
195 *astate = S_GROUND;
196 return (UNVIS_SYNBAD);
198 return (0);
200 case S_META1:
201 *astate = S_GROUND;
202 *cp |= c;
203 return (UNVIS_VALID);
205 case S_CTRL:
206 if (c == '?')
207 *cp |= 0177;
208 else
209 *cp |= c & 037;
210 *astate = S_GROUND;
211 return (UNVIS_VALID);
213 case S_OCTAL2: /* second possible octal digit */
214 if (isoctal(c)) {
216 * yes - and maybe a third
218 *cp = (*cp << 3) + (c - '0');
219 *astate = S_OCTAL3;
220 return (0);
223 * no - done with current sequence, push back passed char
225 *astate = S_GROUND;
226 return (UNVIS_VALIDPUSH);
228 case S_OCTAL3: /* third possible octal digit */
229 *astate = S_GROUND;
230 if (isoctal(c)) {
231 *cp = (*cp << 3) + (c - '0');
232 return (UNVIS_VALID);
235 * we were done, push back passed char
237 return (UNVIS_VALIDPUSH);
239 default:
241 * decoder in unknown state - (probably uninitialized)
243 *astate = S_GROUND;
244 return (UNVIS_SYNBAD);
249 * strunvis - decode src into dst
251 * Number of chars decoded into dst is returned, -1 on error.
252 * Dst is null terminated.
255 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
256 rk_strunvis(char *dst, const char *src)
258 char c;
259 char *start = dst;
260 int state = 0;
262 _DIAGASSERT(src != NULL);
263 _DIAGASSERT(dst != NULL);
265 while ((c = *src++) != '\0') {
266 again:
267 switch (rk_unvis(dst, (unsigned char)c, &state, 0)) {
268 case UNVIS_VALID:
269 dst++;
270 break;
271 case UNVIS_VALIDPUSH:
272 dst++;
273 goto again;
274 case 0:
275 case UNVIS_NOCHAR:
276 break;
277 default:
278 return (-1);
281 if (unvis(dst, (unsigned char)c, &state, UNVIS_END) == UNVIS_VALID)
282 dst++;
283 *dst = '\0';
284 return (dst - start);