Home | History | Annotate | Line # | Download | only in gen
unvis.c revision 1.10
      1 /*	$NetBSD: unvis.c,v 1.10 1997/10/22 00:55:33 fvdl 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.10 1997/10/22 00:55:33 fvdl 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 #include <ctype.h>
     50 #include <vis.h>
     51 
     52 #ifdef __weak_alias
     53 __weak_alias(strunvis,_strunvis);
     54 __weak_alias(unvis,_unvis);
     55 __weak_alias(__unvis13,___unvis13);
     56 #endif
     57 
     58 /*
     59  * decode driven by state machine
     60  */
     61 #define	S_GROUND	0	/* haven't seen escape char */
     62 #define	S_START		1	/* start decoding special sequence */
     63 #define	S_META		2	/* metachar started (M) */
     64 #define	S_META1		3	/* metachar more, regular char (-) */
     65 #define	S_CTRL		4	/* control char started (^) */
     66 #define	S_OCTAL2	5	/* octal digit 2 */
     67 #define	S_OCTAL3	6	/* octal digit 3 */
     68 
     69 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     70 
     71 int
     72 unvis(cp, c, astate, flag)
     73 	char *cp;
     74 	char c;
     75 	int *astate, flag;
     76 {
     77 	return __unvis13(cp, (int)c, astate, flag);
     78 }
     79 
     80 /*
     81  * unvis - decode characters previously encoded by vis
     82  */
     83 int
     84 __unvis13(cp, c, astate, flag)
     85 	char *cp;
     86 	int c;
     87 	int *astate, flag;
     88 {
     89 
     90 	if (flag & UNVIS_END) {
     91 		if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
     92 			*astate = S_GROUND;
     93 			return (UNVIS_VALID);
     94 		}
     95 		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
     96 	}
     97 
     98 	switch (*astate) {
     99 
    100 	case S_GROUND:
    101 		*cp = 0;
    102 		if (c == '\\') {
    103 			*astate = S_START;
    104 			return (0);
    105 		}
    106 		*cp = c;
    107 		return (UNVIS_VALID);
    108 
    109 	case S_START:
    110 		switch(c) {
    111 		case '\\':
    112 			*cp = c;
    113 			*astate = S_GROUND;
    114 			return (UNVIS_VALID);
    115 		case '0': case '1': case '2': case '3':
    116 		case '4': case '5': case '6': case '7':
    117 			*cp = (c - '0');
    118 			*astate = S_OCTAL2;
    119 			return (0);
    120 		case 'M':
    121 			*cp = 0200;
    122 			*astate = S_META;
    123 			return (0);
    124 		case '^':
    125 			*astate = S_CTRL;
    126 			return (0);
    127 		case 'n':
    128 			*cp = '\n';
    129 			*astate = S_GROUND;
    130 			return (UNVIS_VALID);
    131 		case 'r':
    132 			*cp = '\r';
    133 			*astate = S_GROUND;
    134 			return (UNVIS_VALID);
    135 		case 'b':
    136 			*cp = '\b';
    137 			*astate = S_GROUND;
    138 			return (UNVIS_VALID);
    139 		case 'a':
    140 			*cp = '\007';
    141 			*astate = S_GROUND;
    142 			return (UNVIS_VALID);
    143 		case 'v':
    144 			*cp = '\v';
    145 			*astate = S_GROUND;
    146 			return (UNVIS_VALID);
    147 		case 't':
    148 			*cp = '\t';
    149 			*astate = S_GROUND;
    150 			return (UNVIS_VALID);
    151 		case 'f':
    152 			*cp = '\f';
    153 			*astate = S_GROUND;
    154 			return (UNVIS_VALID);
    155 		case 's':
    156 			*cp = ' ';
    157 			*astate = S_GROUND;
    158 			return (UNVIS_VALID);
    159 		case 'E':
    160 			*cp = '\033';
    161 			*astate = S_GROUND;
    162 			return (UNVIS_VALID);
    163 		case '\n':
    164 			/*
    165 			 * hidden newline
    166 			 */
    167 			*astate = S_GROUND;
    168 			return (UNVIS_NOCHAR);
    169 		case '$':
    170 			/*
    171 			 * hidden marker
    172 			 */
    173 			*astate = S_GROUND;
    174 			return (UNVIS_NOCHAR);
    175 		}
    176 		*astate = S_GROUND;
    177 		return (UNVIS_SYNBAD);
    178 
    179 	case S_META:
    180 		if (c == '-')
    181 			*astate = S_META1;
    182 		else if (c == '^')
    183 			*astate = S_CTRL;
    184 		else {
    185 			*astate = S_GROUND;
    186 			return (UNVIS_SYNBAD);
    187 		}
    188 		return (0);
    189 
    190 	case S_META1:
    191 		*astate = S_GROUND;
    192 		*cp |= c;
    193 		return (UNVIS_VALID);
    194 
    195 	case S_CTRL:
    196 		if (c == '?')
    197 			*cp |= 0177;
    198 		else
    199 			*cp |= c & 037;
    200 		*astate = S_GROUND;
    201 		return (UNVIS_VALID);
    202 
    203 	case S_OCTAL2:	/* second possible octal digit */
    204 		if (isoctal(c)) {
    205 			/*
    206 			 * yes - and maybe a third
    207 			 */
    208 			*cp = (*cp << 3) + (c - '0');
    209 			*astate = S_OCTAL3;
    210 			return (0);
    211 		}
    212 		/*
    213 		 * no - done with current sequence, push back passed char
    214 		 */
    215 		*astate = S_GROUND;
    216 		return (UNVIS_VALIDPUSH);
    217 
    218 	case S_OCTAL3:	/* third possible octal digit */
    219 		*astate = S_GROUND;
    220 		if (isoctal(c)) {
    221 			*cp = (*cp << 3) + (c - '0');
    222 			return (UNVIS_VALID);
    223 		}
    224 		/*
    225 		 * we were done, push back passed char
    226 		 */
    227 		return (UNVIS_VALIDPUSH);
    228 
    229 	default:
    230 		/*
    231 		 * decoder in unknown state - (probably uninitialized)
    232 		 */
    233 		*astate = S_GROUND;
    234 		return (UNVIS_SYNBAD);
    235 	}
    236 }
    237 
    238 /*
    239  * strunvis - decode src into dst
    240  *
    241  *	Number of chars decoded into dst is returned, -1 on error.
    242  *	Dst is null terminated.
    243  */
    244 
    245 int
    246 strunvis(dst, src)
    247 	register char *dst;
    248 	register const char *src;
    249 {
    250 	register char c;
    251 	char *start = dst;
    252 	int state = 0;
    253 
    254 	while ((c = *src++) != '\0') {
    255 	again:
    256 		switch (__unvis13(dst, c, &state, 0)) {
    257 		case UNVIS_VALID:
    258 			dst++;
    259 			break;
    260 		case UNVIS_VALIDPUSH:
    261 			dst++;
    262 			goto again;
    263 		case 0:
    264 		case UNVIS_NOCHAR:
    265 			break;
    266 		default:
    267 			return (-1);
    268 		}
    269 	}
    270 	if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID)
    271 		dst++;
    272 	*dst = '\0';
    273 	return (dst - start);
    274 }
    275