Home | History | Annotate | Line # | Download | only in gen
unvis.c revision 1.17
      1 /*	$NetBSD: unvis.c,v 1.17 1999/12/01 22:03:43 wennmach 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.17 1999/12/01 22:03:43 wennmach Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #define __LIBC12_SOURCE__
     46 
     47 #include "namespace.h"
     48 #include <sys/types.h>
     49 
     50 #include <assert.h>
     51 #include <ctype.h>
     52 #include <stdio.h>
     53 #include <vis.h>
     54 
     55 #ifdef __weak_alias
     56 __weak_alias(strunvis,_strunvis);
     57 __weak_alias(unvis,_unvis);
     58 #endif
     59 
     60 __warn_references(unvis,
     61     "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
     62 
     63 /*
     64  * decode driven by state machine
     65  */
     66 #define	S_GROUND	0	/* haven't seen escape char */
     67 #define	S_START		1	/* start decoding special sequence */
     68 #define	S_META		2	/* metachar started (M) */
     69 #define	S_META1		3	/* metachar more, regular char (-) */
     70 #define	S_CTRL		4	/* control char started (^) */
     71 #define	S_OCTAL2	5	/* octal digit 2 */
     72 #define	S_OCTAL3	6	/* octal digit 3 */
     73 
     74 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     75 
     76 int
     77 unvis(cp, c, astate, flag)
     78 	char *cp;
     79 	int c;
     80 	int *astate, flag;
     81 {
     82 	return __unvis13(cp, (int)c, astate, flag);
     83 }
     84 
     85 /*
     86  * unvis - decode characters previously encoded by vis
     87  */
     88 int
     89 __unvis13(cp, c, astate, flag)
     90 	char *cp;
     91 	int c;
     92 	int *astate, 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 = (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 		default:
    184 			/*
    185 			 * backslash escaped character
    186 			 */
    187 			*astate = S_GROUND;
    188 			if (isascii(c) && isgraph(c)) {
    189 				*cp = c;
    190 				return(UNVIS_VALID);
    191 			} else
    192 				return(UNVIS_SYNBAD);
    193 		}
    194 
    195 	case S_META:
    196 		if (c == '-')
    197 			*astate = S_META1;
    198 		else if (c == '^')
    199 			*astate = S_CTRL;
    200 		else {
    201 			*astate = S_GROUND;
    202 			return (UNVIS_SYNBAD);
    203 		}
    204 		return (0);
    205 
    206 	case S_META1:
    207 		*astate = S_GROUND;
    208 		*cp |= c;
    209 		return (UNVIS_VALID);
    210 
    211 	case S_CTRL:
    212 		if (c == '?')
    213 			*cp |= 0177;
    214 		else
    215 			*cp |= c & 037;
    216 		*astate = S_GROUND;
    217 		return (UNVIS_VALID);
    218 
    219 	case S_OCTAL2:	/* second possible octal digit */
    220 		if (isoctal(c)) {
    221 			/*
    222 			 * yes - and maybe a third
    223 			 */
    224 			*cp = (*cp << 3) + (c - '0');
    225 			*astate = S_OCTAL3;
    226 			return (0);
    227 		}
    228 		/*
    229 		 * no - done with current sequence, push back passed char
    230 		 */
    231 		*astate = S_GROUND;
    232 		return (UNVIS_VALIDPUSH);
    233 
    234 	case S_OCTAL3:	/* third possible octal digit */
    235 		*astate = S_GROUND;
    236 		if (isoctal(c)) {
    237 			*cp = (*cp << 3) + (c - '0');
    238 			return (UNVIS_VALID);
    239 		}
    240 		/*
    241 		 * we were done, push back passed char
    242 		 */
    243 		return (UNVIS_VALIDPUSH);
    244 
    245 	default:
    246 		/*
    247 		 * decoder in unknown state - (probably uninitialized)
    248 		 */
    249 		*astate = S_GROUND;
    250 		return (UNVIS_SYNBAD);
    251 	}
    252 }
    253 
    254 /*
    255  * strunvis - decode src into dst
    256  *
    257  *	Number of chars decoded into dst is returned, -1 on error.
    258  *	Dst is null terminated.
    259  */
    260 
    261 int
    262 strunvis(dst, src)
    263 	char *dst;
    264 	const char *src;
    265 {
    266 	char c;
    267 	char *start = dst;
    268 	int state = 0;
    269 
    270 	_DIAGASSERT(src != NULL);
    271 	_DIAGASSERT(dst != NULL);
    272 
    273 	while ((c = *src++) != '\0') {
    274 	again:
    275 		switch (__unvis13(dst, c, &state, 0)) {
    276 		case UNVIS_VALID:
    277 			dst++;
    278 			break;
    279 		case UNVIS_VALIDPUSH:
    280 			dst++;
    281 			goto again;
    282 		case 0:
    283 		case UNVIS_NOCHAR:
    284 			break;
    285 		default:
    286 			return (-1);
    287 		}
    288 	}
    289 	if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID)
    290 		dst++;
    291 	*dst = '\0';
    292 	return (dst - start);
    293 }
    294