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