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