sbin/hammer: Make volume count test optional for blkdevs based commands
[dragonfly.git] / lib / libc / gen / unvis.c
blob4195781bf824bb9d3ca152d143d73894e4466192
1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * $FreeBSD: src/lib/libc/gen/unvis.c,v 1.4.8.1 2000/08/17 08:25:54 jhb Exp $
30 * $DragonFly: src/lib/libc/gen/unvis.c,v 1.5 2005/11/19 22:32:53 swildner Exp $
32 * @(#)unvis.c 8.1 (Berkeley) 6/4/93
35 #include <sys/types.h>
36 #include <ctype.h>
37 #include <vis.h>
40 * decode driven by state machine
42 #define S_GROUND 0 /* haven't seen escape char */
43 #define S_START 1 /* start decoding special sequence */
44 #define S_META 2 /* metachar started (M) */
45 #define S_META1 3 /* metachar more, regular char (-) */
46 #define S_CTRL 4 /* control char started (^) */
47 #define S_OCTAL2 5 /* octal digit 2 */
48 #define S_OCTAL3 6 /* octal digit 3 */
49 #define S_HEX2 7 /* hex digit 2 */
51 #define S_HTTP 0x080 /* %HEXHEX escape */
53 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
54 #define ishex(c) ((((u_char)(c)) >= '0' && ((u_char)(c)) <= '9') || (((u_char)(c)) >= 'a' && ((u_char)(c)) <= 'f'))
57 * unvis - decode characters previously encoded by vis
59 int
60 unvis(char *cp, int c, int *astate, int flag)
63 if (flag & UNVIS_END) {
64 if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
65 *astate = S_GROUND;
66 return (UNVIS_VALID);
68 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
71 switch (*astate & ~S_HTTP) {
73 case S_GROUND:
74 *cp = 0;
75 if (c == '\\') {
76 *astate = S_START;
77 return (0);
79 if (flag & VIS_HTTPSTYLE && c == '%') {
80 *astate = S_START | S_HTTP;
81 return (0);
83 *cp = c;
84 return (UNVIS_VALID);
86 case S_START:
87 if (*astate & S_HTTP) {
88 if (ishex(tolower(c))) {
89 *cp = isdigit(c) ? (c - '0') : (tolower(c) - 'a');
90 *astate = S_HEX2;
91 return (0);
94 switch(c) {
95 case '\\':
96 *cp = c;
97 *astate = S_GROUND;
98 return (UNVIS_VALID);
99 case '0': case '1': case '2': case '3':
100 case '4': case '5': case '6': case '7':
101 *cp = (c - '0');
102 *astate = S_OCTAL2;
103 return (0);
104 case 'M':
105 *cp = (char)0200;
106 *astate = S_META;
107 return (0);
108 case '^':
109 *astate = S_CTRL;
110 return (0);
111 case 'n':
112 *cp = '\n';
113 *astate = S_GROUND;
114 return (UNVIS_VALID);
115 case 'r':
116 *cp = '\r';
117 *astate = S_GROUND;
118 return (UNVIS_VALID);
119 case 'b':
120 *cp = '\b';
121 *astate = S_GROUND;
122 return (UNVIS_VALID);
123 case 'a':
124 *cp = '\007';
125 *astate = S_GROUND;
126 return (UNVIS_VALID);
127 case 'v':
128 *cp = '\v';
129 *astate = S_GROUND;
130 return (UNVIS_VALID);
131 case 't':
132 *cp = '\t';
133 *astate = S_GROUND;
134 return (UNVIS_VALID);
135 case 'f':
136 *cp = '\f';
137 *astate = S_GROUND;
138 return (UNVIS_VALID);
139 case 's':
140 *cp = ' ';
141 *astate = S_GROUND;
142 return (UNVIS_VALID);
143 case 'E':
144 *cp = '\033';
145 *astate = S_GROUND;
146 return (UNVIS_VALID);
147 case '\n':
149 * hidden newline
151 *astate = S_GROUND;
152 return (UNVIS_NOCHAR);
153 case '$':
155 * hidden marker
157 *astate = S_GROUND;
158 return (UNVIS_NOCHAR);
160 *astate = S_GROUND;
161 return (UNVIS_SYNBAD);
163 case S_META:
164 if (c == '-')
165 *astate = S_META1;
166 else if (c == '^')
167 *astate = S_CTRL;
168 else {
169 *astate = S_GROUND;
170 return (UNVIS_SYNBAD);
172 return (0);
174 case S_META1:
175 *astate = S_GROUND;
176 *cp |= c;
177 return (UNVIS_VALID);
179 case S_CTRL:
180 if (c == '?')
181 *cp |= 0177;
182 else
183 *cp |= c & 037;
184 *astate = S_GROUND;
185 return (UNVIS_VALID);
187 case S_OCTAL2: /* second possible octal digit */
188 if (isoctal(c)) {
190 * yes - and maybe a third
192 *cp = (*cp << 3) + (c - '0');
193 *astate = S_OCTAL3;
194 return (0);
197 * no - done with current sequence, push back passed char
199 *astate = S_GROUND;
200 return (UNVIS_VALIDPUSH);
202 case S_OCTAL3: /* third possible octal digit */
203 *astate = S_GROUND;
204 if (isoctal(c)) {
205 *cp = (*cp << 3) + (c - '0');
206 return (UNVIS_VALID);
209 * we were done, push back passed char
211 return (UNVIS_VALIDPUSH);
213 case S_HEX2: /* second mandatory hex digit */
214 if (ishex(tolower(c))) {
215 *cp = (isdigit(c) ? (*cp << 4) + (c - '0') : (*cp << 4) + (tolower(c) - 'a' + 10));
217 *astate = S_GROUND;
218 return (UNVIS_VALID);
220 default:
222 * decoder in unknown state - (probably uninitialized)
224 *astate = S_GROUND;
225 return (UNVIS_SYNBAD);
230 * strunvis - decode src into dst
232 * Number of chars decoded into dst is returned, -1 on error.
233 * Dst is null terminated.
237 strunvis(char *dst, const char *src)
239 char c;
240 char *start = dst;
241 int state = 0;
243 while ( (c = *src++) ) {
244 again:
245 switch (unvis(dst, c, &state, 0)) {
246 case UNVIS_VALID:
247 dst++;
248 break;
249 case UNVIS_VALIDPUSH:
250 dst++;
251 goto again;
252 case 0:
253 case UNVIS_NOCHAR:
254 break;
255 default:
256 return (-1);
259 if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
260 dst++;
261 *dst = '\0';
262 return (dst - start);
266 strunvisx(char *dst, const char *src, int flag)
268 char c;
269 char *start = dst;
270 int state = 0;
272 while ( (c = *src++) ) {
273 again:
274 switch (unvis(dst, c, &state, flag)) {
275 case UNVIS_VALID:
276 dst++;
277 break;
278 case UNVIS_VALIDPUSH:
279 dst++;
280 goto again;
281 case 0:
282 case UNVIS_NOCHAR:
283 break;
284 default:
285 return (-1);
288 if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
289 dst++;
290 *dst = '\0';
291 return (dst - start);