Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.19
      1  1.19   mycroft /*	$NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*-
      4  1.15  wennmach  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.6       cgd  * Copyright (c) 1989, 1993
      6  1.16  wennmach  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18  1.16  wennmach  *	This product includes software developed by the University of
     19  1.16  wennmach  *	California, Berkeley and its contributors.
     20   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1       cgd  *    may be used to endorse or promote products derived from this software
     22   1.1       cgd  *    without specific prior written permission.
     23   1.1       cgd  *
     24   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1       cgd  * SUCH DAMAGE.
     35   1.1       cgd  */
     36   1.1       cgd 
     37  1.15  wennmach 
     38   1.7  christos #include <sys/cdefs.h>
     39  1.15  wennmach #if !defined(lint)
     40  1.19   mycroft __RCSID("$NetBSD: vis.c,v 1.19 2000/01/22 22:42:45 mycroft Exp $");
     41  1.15  wennmach #endif /* not lint */
     42   1.1       cgd 
     43   1.8       jtc #include "namespace.h"
     44   1.1       cgd #include <sys/types.h>
     45  1.12     lukem 
     46  1.12     lukem #include <assert.h>
     47  1.12     lukem #include <ctype.h>
     48   1.2       cgd #include <limits.h>
     49  1.12     lukem #include <stdio.h>
     50  1.15  wennmach #include <string.h>
     51   1.1       cgd #include <vis.h>
     52   1.8       jtc 
     53   1.8       jtc #ifdef __weak_alias
     54  1.18   mycroft __weak_alias(strsvis,_strsvis)
     55  1.18   mycroft __weak_alias(strsvisx,_strsvisx)
     56  1.18   mycroft __weak_alias(strvis,_strvis)
     57  1.18   mycroft __weak_alias(strvisx,_strvisx)
     58  1.18   mycroft __weak_alias(svis,_svis)
     59  1.18   mycroft __weak_alias(vis,_vis)
     60   1.8       jtc #endif
     61   1.1       cgd 
     62  1.15  wennmach #undef BELL
     63  1.15  wennmach #if defined(__STDC__)
     64  1.15  wennmach #define BELL '\a'
     65  1.15  wennmach #else
     66  1.15  wennmach #define BELL '\007'
     67  1.15  wennmach #endif
     68  1.15  wennmach 
     69  1.16  wennmach #define isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     70  1.16  wennmach #define iswhite(c)	(c == ' ' || c == '\t' || c == '\n')
     71  1.16  wennmach #define issafe(c)	(c == '\b' || c == BELL || c == '\r')
     72  1.16  wennmach 
     73  1.16  wennmach #define MAXEXTRAS       5
     74  1.15  wennmach 
     75  1.15  wennmach 
     76  1.16  wennmach #define MAKEEXTRALIST(flag, extra)					      \
     77  1.16  wennmach do {									      \
     78  1.16  wennmach 	char *pextra = extra;						      \
     79  1.16  wennmach 	if (flag & VIS_SP) *pextra++ = ' ';				      \
     80  1.16  wennmach 	if (flag & VIS_TAB) *pextra++ = '\t';				      \
     81  1.16  wennmach 	if (flag & VIS_NL) *pextra++ = '\n';				      \
     82  1.16  wennmach 	if ((flag & VIS_NOSLASH) == 0) *pextra++ = '\\';		      \
     83  1.16  wennmach 	*pextra = '\0';							      \
     84  1.19   mycroft } while (/*CONSTCOND*/0)
     85  1.15  wennmach 
     86  1.15  wennmach /*
     87  1.15  wennmach  * This is SVIS, the central macro of vis.
     88  1.16  wennmach  * dst:	      Pointer to the destination buffer
     89  1.16  wennmach  * c:	      Character to encode
     90  1.15  wennmach  * flag:      Flag word
     91  1.15  wennmach  * nextc:     The character following 'c'
     92  1.15  wennmach  * extra:     Pointer to the list of extra characters to be
     93  1.16  wennmach  *	      backslash-protected.
     94  1.15  wennmach  */
     95  1.16  wennmach #define SVIS(dst, c, flag, nextc, extra)				      \
     96  1.16  wennmach do {									      \
     97  1.16  wennmach 	int isextra, isc;						      \
     98  1.16  wennmach 	isextra = strchr(extra, c) != NULL;				      \
     99  1.17  wennmach 	if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||	      \
    100  1.17  wennmach 	    ((flag & VIS_SAFE) && issafe(c)))) {			      \
    101  1.16  wennmach 		*dst++ = c;						      \
    102  1.16  wennmach 		break;							      \
    103  1.16  wennmach 	}								      \
    104  1.16  wennmach 	isc = 0;							      \
    105  1.16  wennmach 	if (flag & VIS_CSTYLE) {					      \
    106  1.16  wennmach 		switch (c) {						      \
    107  1.16  wennmach 		case '\n':						      \
    108  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 'n';		      \
    109  1.16  wennmach 			break;						      \
    110  1.16  wennmach 		case '\r':						      \
    111  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 'r';		      \
    112  1.16  wennmach 			break;						      \
    113  1.16  wennmach 		case '\b':						      \
    114  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 'b';		      \
    115  1.16  wennmach 			break;						      \
    116  1.16  wennmach 		case BELL:						      \
    117  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 'a';		      \
    118  1.16  wennmach 			break;						      \
    119  1.16  wennmach 		case '\v':						      \
    120  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 'v';		      \
    121  1.16  wennmach 			break;						      \
    122  1.16  wennmach 		case '\t':						      \
    123  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 't';		      \
    124  1.16  wennmach 			break;						      \
    125  1.16  wennmach 		case '\f':						      \
    126  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 'f';		      \
    127  1.16  wennmach 			break;						      \
    128  1.16  wennmach 		case ' ':						      \
    129  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = 's';		      \
    130  1.16  wennmach 			break;						      \
    131  1.16  wennmach 		case '\0':						      \
    132  1.16  wennmach 			isc = 1; *dst++ = '\\'; *dst++ = '0';		      \
    133  1.16  wennmach 			if (isoctal(nextc)) {				      \
    134  1.16  wennmach 				*dst++ = '0';				      \
    135  1.16  wennmach 				*dst++ = '0';				      \
    136  1.16  wennmach 			}						      \
    137  1.16  wennmach 		}							      \
    138  1.16  wennmach 	}								      \
    139  1.16  wennmach 	if (isc) break;							      \
    140  1.17  wennmach 	if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {	      \
    141  1.16  wennmach 		*dst++ = '\\';						      \
    142  1.16  wennmach 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';    \
    143  1.16  wennmach 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';    \
    144  1.16  wennmach 		*dst++ =			     (c	      & 07) + '0';    \
    145  1.16  wennmach 	} else {							      \
    146  1.16  wennmach 		if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';		      \
    147  1.16  wennmach 		if (c & 0200) {						      \
    148  1.16  wennmach 			c &= 0177; *dst++ = 'M';			      \
    149  1.16  wennmach 		}							      \
    150  1.16  wennmach 		if (iscntrl(c)) {					      \
    151  1.16  wennmach 			*dst++ = '^';					      \
    152  1.16  wennmach 			if (c == 0177)					      \
    153  1.16  wennmach 				*dst++ = '?';				      \
    154  1.16  wennmach 			else						      \
    155  1.16  wennmach 				*dst++ = c + '@';			      \
    156  1.16  wennmach 		} else {						      \
    157  1.16  wennmach 			*dst++ = '-'; *dst++ = c;			      \
    158  1.16  wennmach 		}							      \
    159  1.16  wennmach 	}								      \
    160  1.19   mycroft } while (/*CONSTCOND*/0)
    161  1.15  wennmach 
    162  1.15  wennmach 
    163  1.15  wennmach /*
    164  1.17  wennmach  * svis - visually encode characters, also encoding the characters
    165  1.17  wennmach  * 	  pointed to by `extra'
    166  1.15  wennmach  */
    167  1.15  wennmach char *
    168  1.15  wennmach svis(dst, c, flag, nextc, extra)
    169  1.16  wennmach 	char *dst;
    170  1.16  wennmach 	int c, flag, nextc;
    171  1.16  wennmach 	const char *extra;
    172  1.15  wennmach {
    173  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    174  1.16  wennmach 	_DIAGASSERT(extra != NULL);
    175  1.15  wennmach 
    176  1.16  wennmach 	SVIS(dst, c, flag, nextc, extra);
    177  1.16  wennmach 	*dst = '\0';
    178  1.16  wennmach 	return(dst);
    179  1.15  wennmach }
    180  1.15  wennmach 
    181  1.15  wennmach 
    182  1.15  wennmach /*
    183  1.15  wennmach  * strsvis, strsvisx - visually encode characters from src into dst
    184  1.15  wennmach  *
    185  1.16  wennmach  *	Extra is a pointer to a \0-terminated list of characters to
    186  1.17  wennmach  *	be encoded, too. These functions are useful e. g. to
    187  1.17  wennmach  *	encode strings in such a way so that they are not interpreted
    188  1.16  wennmach  *	by a shell.
    189  1.16  wennmach  *
    190  1.16  wennmach  *	Dst must be 4 times the size of src to account for possible
    191  1.16  wennmach  *	expansion.  The length of dst, not including the trailing NULL,
    192  1.16  wennmach  *	is returned.
    193  1.15  wennmach  *
    194  1.16  wennmach  *	Strsvisx encodes exactly len bytes from src into dst.
    195  1.16  wennmach  *	This is useful for encoding a block of data.
    196  1.15  wennmach  */
    197  1.15  wennmach int
    198  1.15  wennmach strsvis(dst, src, flag, extra)
    199  1.16  wennmach 	char *dst;
    200  1.16  wennmach 	const char *src;
    201  1.16  wennmach 	int flag;
    202  1.16  wennmach 	const char *extra;
    203  1.15  wennmach {
    204  1.16  wennmach 	char c;
    205  1.16  wennmach 	char *start;
    206  1.15  wennmach 
    207  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    208  1.16  wennmach 	_DIAGASSERT(src != NULL);
    209  1.16  wennmach 	_DIAGASSERT(extra != NULL);
    210  1.16  wennmach 
    211  1.16  wennmach 	for (start = dst; (c = *src++) != '\0'; /* empty */)
    212  1.16  wennmach 	    SVIS(dst, c, flag, *src, extra);
    213  1.16  wennmach 	*dst = '\0';
    214  1.16  wennmach 	return (dst - start);
    215  1.15  wennmach }
    216  1.15  wennmach 
    217  1.15  wennmach 
    218  1.15  wennmach int
    219  1.15  wennmach strsvisx(dst, src, len, flag, extra)
    220  1.16  wennmach 	char *dst;
    221  1.16  wennmach 	const char *src;
    222  1.16  wennmach 	size_t len;
    223  1.16  wennmach 	int flag;
    224  1.16  wennmach 	const char *extra;
    225  1.15  wennmach {
    226  1.16  wennmach 	char c;
    227  1.16  wennmach 	char *start;
    228  1.15  wennmach 
    229  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    230  1.16  wennmach 	_DIAGASSERT(src != NULL);
    231  1.16  wennmach 	_DIAGASSERT(extra != NULL);
    232  1.16  wennmach 
    233  1.16  wennmach 	for (start = dst; len > 0; len--) {
    234  1.16  wennmach 		c = *src++;
    235  1.16  wennmach 		SVIS(dst, c, flag, len ? *src : '\0', extra);
    236  1.16  wennmach 	}
    237  1.16  wennmach 	*dst = '\0';
    238  1.16  wennmach 	return (dst - start);
    239  1.15  wennmach }
    240  1.15  wennmach 
    241   1.1       cgd 
    242   1.1       cgd /*
    243   1.1       cgd  * vis - visually encode characters
    244   1.1       cgd  */
    245   1.1       cgd char *
    246   1.1       cgd vis(dst, c, flag, nextc)
    247  1.16  wennmach 	char *dst;
    248  1.16  wennmach 	int c, flag, nextc;
    249  1.16  wennmach 
    250  1.15  wennmach {
    251  1.16  wennmach 	char extra[MAXEXTRAS];
    252  1.15  wennmach 
    253  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    254  1.15  wennmach 
    255  1.16  wennmach 	MAKEEXTRALIST(flag, extra);
    256  1.16  wennmach 	SVIS(dst, c, flag, nextc, extra);
    257  1.16  wennmach 	*dst = '\0';
    258  1.16  wennmach 	return (dst);
    259   1.1       cgd }
    260   1.1       cgd 
    261  1.15  wennmach 
    262   1.1       cgd /*
    263   1.1       cgd  * strvis, strvisx - visually encode characters from src into dst
    264  1.16  wennmach  *
    265  1.16  wennmach  *	Dst must be 4 times the size of src to account for possible
    266  1.16  wennmach  *	expansion.  The length of dst, not including the trailing NULL,
    267  1.16  wennmach  *	is returned.
    268   1.1       cgd  *
    269  1.16  wennmach  *	Strvisx encodes exactly len bytes from src into dst.
    270  1.16  wennmach  *	This is useful for encoding a block of data.
    271   1.1       cgd  */
    272   1.1       cgd int
    273   1.1       cgd strvis(dst, src, flag)
    274  1.16  wennmach 	char *dst;
    275  1.16  wennmach 	const char *src;
    276  1.16  wennmach 	int flag;
    277  1.15  wennmach {
    278  1.16  wennmach 	char extra[MAXEXTRAS];
    279  1.15  wennmach 
    280  1.16  wennmach 	MAKEEXTRALIST(flag, extra);
    281  1.16  wennmach 	return (strsvis(dst, src, flag, extra));
    282   1.1       cgd }
    283   1.1       cgd 
    284  1.15  wennmach 
    285   1.1       cgd int
    286   1.1       cgd strvisx(dst, src, len, flag)
    287  1.16  wennmach 	char *dst;
    288  1.16  wennmach 	const char *src;
    289  1.16  wennmach 	size_t len;
    290  1.16  wennmach 	int flag;
    291  1.15  wennmach {
    292  1.16  wennmach 	char extra[MAXEXTRAS];
    293   1.1       cgd 
    294  1.16  wennmach 	MAKEEXTRALIST(flag, extra);
    295  1.16  wennmach 	return (strsvisx(dst, src, len, flag, extra));
    296   1.1       cgd }
    297