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