1 /* $NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if 1 33 #include <config.h> 34 #include "roken.h" 35 #ifndef _DIAGASSERT 36 #define _DIAGASSERT(X) 37 #endif 38 #else 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; 43 #else 44 __RCSID("$NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #define __LIBC12_SOURCE__ 49 50 #include "namespace.h" 51 #endif 52 #include <sys/types.h> 53 54 #include <assert.h> 55 #include <ctype.h> 56 #include <stdio.h> 57 #include <vis.h> 58 59 #if 0 60 #ifdef __weak_alias 61 __weak_alias(strunvis,_strunvis) 62 __weak_alias(unvis,_unvis) 63 #endif 64 65 __warn_references(unvis, 66 "warning: reference to compatibility unvis(); include <vis.h> for correct reference") 67 #endif 68 69 /* 70 * decode driven by state machine 71 */ 72 #define S_GROUND 0 /* haven't seen escape char */ 73 #define S_START 1 /* start decoding special sequence */ 74 #define S_META 2 /* metachar started (M) */ 75 #define S_META1 3 /* metachar more, regular char (-) */ 76 #define S_CTRL 4 /* control char started (^) */ 77 #define S_OCTAL2 5 /* octal digit 2 */ 78 #define S_OCTAL3 6 /* octal digit 3 */ 79 80 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 81 82 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 83 rk_strunvis (char *, const char *); 84 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 85 rk_unvis (char *, int, int *, int); 86 87 /* 88 * unvis - decode characters previously encoded by vis 89 */ 90 91 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 92 rk_unvis(char *cp, int c, int *astate, int flag) 93 { 94 95 _DIAGASSERT(cp != NULL); 96 _DIAGASSERT(astate != NULL); 97 98 if (flag & UNVIS_END) { 99 if (*astate == S_OCTAL2 || *astate == S_OCTAL3) { 100 *astate = S_GROUND; 101 return (UNVIS_VALID); 102 } 103 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); 104 } 105 106 switch (*astate) { 107 108 case S_GROUND: 109 *cp = 0; 110 if (c == '\\') { 111 *astate = S_START; 112 return (0); 113 } 114 *cp = c; 115 return (UNVIS_VALID); 116 117 case S_START: 118 switch(c) { 119 case '\\': 120 *cp = c; 121 *astate = S_GROUND; 122 return (UNVIS_VALID); 123 case '0': case '1': case '2': case '3': 124 case '4': case '5': case '6': case '7': 125 *cp = (c - '0'); 126 *astate = S_OCTAL2; 127 return (0); 128 case 'M': 129 *cp = (u_char)0200; 130 *astate = S_META; 131 return (0); 132 case '^': 133 *astate = S_CTRL; 134 return (0); 135 case 'n': 136 *cp = '\n'; 137 *astate = S_GROUND; 138 return (UNVIS_VALID); 139 case 'r': 140 *cp = '\r'; 141 *astate = S_GROUND; 142 return (UNVIS_VALID); 143 case 'b': 144 *cp = '\b'; 145 *astate = S_GROUND; 146 return (UNVIS_VALID); 147 case 'a': 148 *cp = '\007'; 149 *astate = S_GROUND; 150 return (UNVIS_VALID); 151 case 'v': 152 *cp = '\v'; 153 *astate = S_GROUND; 154 return (UNVIS_VALID); 155 case 't': 156 *cp = '\t'; 157 *astate = S_GROUND; 158 return (UNVIS_VALID); 159 case 'f': 160 *cp = '\f'; 161 *astate = S_GROUND; 162 return (UNVIS_VALID); 163 case 's': 164 *cp = ' '; 165 *astate = S_GROUND; 166 return (UNVIS_VALID); 167 case 'E': 168 *cp = '\033'; 169 *astate = S_GROUND; 170 return (UNVIS_VALID); 171 case '\n': 172 /* 173 * hidden newline 174 */ 175 *astate = S_GROUND; 176 return (UNVIS_NOCHAR); 177 case '$': 178 /* 179 * hidden marker 180 */ 181 *astate = S_GROUND; 182 return (UNVIS_NOCHAR); 183 } 184 *astate = S_GROUND; 185 return (UNVIS_SYNBAD); 186 187 case S_META: 188 if (c == '-') 189 *astate = S_META1; 190 else if (c == '^') 191 *astate = S_CTRL; 192 else { 193 *astate = S_GROUND; 194 return (UNVIS_SYNBAD); 195 } 196 return (0); 197 198 case S_META1: 199 *astate = S_GROUND; 200 *cp |= c; 201 return (UNVIS_VALID); 202 203 case S_CTRL: 204 if (c == '?') 205 *cp |= 0177; 206 else 207 *cp |= c & 037; 208 *astate = S_GROUND; 209 return (UNVIS_VALID); 210 211 case S_OCTAL2: /* second possible octal digit */ 212 if (isoctal(c)) { 213 /* 214 * yes - and maybe a third 215 */ 216 *cp = (*cp << 3) + (c - '0'); 217 *astate = S_OCTAL3; 218 return (0); 219 } 220 /* 221 * no - done with current sequence, push back passed char 222 */ 223 *astate = S_GROUND; 224 return (UNVIS_VALIDPUSH); 225 226 case S_OCTAL3: /* third possible octal digit */ 227 *astate = S_GROUND; 228 if (isoctal(c)) { 229 *cp = (*cp << 3) + (c - '0'); 230 return (UNVIS_VALID); 231 } 232 /* 233 * we were done, push back passed char 234 */ 235 return (UNVIS_VALIDPUSH); 236 237 default: 238 /* 239 * decoder in unknown state - (probably uninitialized) 240 */ 241 *astate = S_GROUND; 242 return (UNVIS_SYNBAD); 243 } 244 } 245 246 /* 247 * strunvis - decode src into dst 248 * 249 * Number of chars decoded into dst is returned, -1 on error. 250 * Dst is null terminated. 251 */ 252 253 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 254 rk_strunvis(char *dst, const char *src) 255 { 256 char c; 257 char *start = dst; 258 int state = 0; 259 260 _DIAGASSERT(src != NULL); 261 _DIAGASSERT(dst != NULL); 262 263 while ((c = *src++) != '\0') { 264 again: 265 switch (rk_unvis(dst, (unsigned char)c, &state, 0)) { 266 case UNVIS_VALID: 267 dst++; 268 break; 269 case UNVIS_VALIDPUSH: 270 dst++; 271 goto again; 272 case 0: 273 case UNVIS_NOCHAR: 274 break; 275 default: 276 return (-1); 277 } 278 } 279 if (unvis(dst, (unsigned char)c, &state, UNVIS_END) == UNVIS_VALID) 280 dst++; 281 *dst = '\0'; 282 return (dst - start); 283 } 284