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