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