Annotation of pkgsrc/pkgtools/libnbcompat/files/vis.c, revision 1.2

1.2     ! jlam        1: /*     $NetBSD: vis.c,v 1.1.1.1 2003/03/31 05:02:59 grant Exp $        */
1.1       grant       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: 
1.2     ! jlam       37: #if HAVE_CONFIG_H
        !            38: #include "nbconfig.h"
1.1       grant      39: #endif
                     40: 
                     41: #if HAVE_SYS_CDEFS_H
                     42: #include <sys/cdefs.h>
                     43: #endif
                     44: 
                     45: #if defined(LIBC_SCCS) && !defined(lint)
1.2     ! jlam       46: __RCSID("$NetBSD: vis.c,v 1.1.1.1 2003/03/31 05:02:59 grant Exp $");
1.1       grant      47: #endif /* LIBC_SCCS and not lint */
                     48: 
                     49: #include "namespace.h"
                     50: #include <sys/types.h>
                     51: 
1.2     ! jlam       52: #if HAVE_ASSERT_H
1.1       grant      53: #include <assert.h>
                     54: #endif
                     55: 
                     56: #include "vis.h"
                     57: #include <stdlib.h>
                     58: 
                     59: #include <nbtypes.h>
                     60: 
                     61: #ifndef __ELF__
                     62: #define _strsvis       strsvis
                     63: #define _strsvisx      strsvisx
                     64: #define _strvis        strvis
                     65: #define _strvisx       strvisx
                     66: #define _svis  svis
                     67: #define _vis   vis
                     68: #else
                     69: #ifdef __weak_alias
                     70: __weak_alias(strsvis,_strsvis)
                     71: __weak_alias(strsvisx,_strsvisx)
                     72: __weak_alias(strvis,_strvis)
                     73: __weak_alias(strvisx,_strvisx)
                     74: __weak_alias(svis,_svis)
                     75: __weak_alias(vis,_vis)
                     76: #endif
                     77: #endif
                     78: 
                     79: #include <ctype.h>
                     80: #include <limits.h>
                     81: #include <stdio.h>
                     82: #include <string.h>
                     83: 
                     84: #undef BELL
                     85: #if defined(__STDC__)
                     86: #define BELL '\a'
                     87: #else
                     88: #define BELL '\007'
                     89: #endif
                     90: 
                     91: #define isoctal(c)     (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
                     92: #define iswhite(c)     (c == ' ' || c == '\t' || c == '\n')
                     93: #define issafe(c)      (c == '\b' || c == BELL || c == '\r')
                     94: #define xtoa(c)                "0123456789abcdef"[c]
                     95: 
                     96: #define MAXEXTRAS       5
                     97: 
                     98: 
                     99: #define MAKEEXTRALIST(flag, extra, orig)                                     \
                    100: do {                                                                         \
                    101:        const char *o = orig;                                                 \
                    102:        char *e;                                                              \
                    103:        while (*o++)                                                          \
                    104:                continue;                                                     \
                    105:        extra = malloc((size_t)((o - orig) + MAXEXTRAS));                     \
                    106:        for (o = orig, e = extra; (*e++ = *o++) != '\0';)                     \
                    107:                continue;                                                     \
                    108:        e--;                                                                  \
                    109:        if (flag & VIS_SP) *e++ = ' ';                                        \
                    110:        if (flag & VIS_TAB) *e++ = '\t';                                      \
                    111:        if (flag & VIS_NL) *e++ = '\n';                                       \
                    112:        if ((flag & VIS_NOSLASH) == 0) *e++ = '\\';                           \
                    113:        *e = '\0';                                                            \
                    114: } while (/*CONSTCOND*/0)
                    115: 
                    116: 
                    117: /*
                    118:  * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
                    119:  */
                    120: #define HVIS(dst, c, flag, nextc, extra)                                     \
                    121: do                                                                           \
                    122:        if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
                    123:                *dst++ = '%';                                                 \
                    124:                *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);                  \
                    125:                *dst++ = xtoa((unsigned int)c & 0xf);                         \
                    126:        } else {                                                              \
                    127:                SVIS(dst, c, flag, nextc, extra);                             \
                    128:        }                                                                     \
                    129: while (/*CONSTCOND*/0)
                    130:        
                    131: /*
                    132:  * This is SVIS, the central macro of vis.
                    133:  * dst:              Pointer to the destination buffer
                    134:  * c:        Character to encode
                    135:  * flag:      Flag word
                    136:  * nextc:     The character following 'c'
                    137:  * extra:     Pointer to the list of extra characters to be
                    138:  *           backslash-protected.
                    139:  */
                    140: #define SVIS(dst, c, flag, nextc, extra)                                     \
                    141: do {                                                                         \
                    142:        int isextra, isc;                                                     \
                    143:        isextra = strchr(extra, c) != NULL;                                   \
                    144:        if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||            \
                    145:            ((flag & VIS_SAFE) && issafe(c)))) {                              \
                    146:                *dst++ = c;                                                   \
                    147:                break;                                                        \
                    148:        }                                                                     \
                    149:        isc = 0;                                                              \
                    150:        if (flag & VIS_CSTYLE) {                                              \
                    151:                switch (c) {                                                  \
                    152:                case '\n':                                                    \
                    153:                        isc = 1; *dst++ = '\\'; *dst++ = 'n';                 \
                    154:                        break;                                                \
                    155:                case '\r':                                                    \
                    156:                        isc = 1; *dst++ = '\\'; *dst++ = 'r';                 \
                    157:                        break;                                                \
                    158:                case '\b':                                                    \
                    159:                        isc = 1; *dst++ = '\\'; *dst++ = 'b';                 \
                    160:                        break;                                                \
                    161:                case BELL:                                                    \
                    162:                        isc = 1; *dst++ = '\\'; *dst++ = 'a';                 \
                    163:                        break;                                                \
                    164:                case '\v':                                                    \
                    165:                        isc = 1; *dst++ = '\\'; *dst++ = 'v';                 \
                    166:                        break;                                                \
                    167:                case '\t':                                                    \
                    168:                        isc = 1; *dst++ = '\\'; *dst++ = 't';                 \
                    169:                        break;                                                \
                    170:                case '\f':                                                    \
                    171:                        isc = 1; *dst++ = '\\'; *dst++ = 'f';                 \
                    172:                        break;                                                \
                    173:                case ' ':                                                     \
                    174:                        isc = 1; *dst++ = '\\'; *dst++ = 's';                 \
                    175:                        break;                                                \
                    176:                case '\0':                                                    \
                    177:                        isc = 1; *dst++ = '\\'; *dst++ = '0';                 \
                    178:                        if (isoctal(nextc)) {                                 \
                    179:                                *dst++ = '0';                                 \
                    180:                                *dst++ = '0';                                 \
                    181:                        }                                                     \
                    182:                }                                                             \
                    183:        }                                                                     \
                    184:        if (isc) break;                                                       \
                    185:        if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {           \
                    186:                *dst++ = '\\';                                                \
                    187:                *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';    \
                    188:                *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';    \
                    189:                *dst++ =                             (c       & 07) + '0';    \
                    190:        } else {                                                              \
                    191:                if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';                 \
                    192:                if (c & 0200) {                                               \
                    193:                        c &= 0177; *dst++ = 'M';                              \
                    194:                }                                                             \
                    195:                if (iscntrl(c)) {                                             \
                    196:                        *dst++ = '^';                                         \
                    197:                        if (c == 0177)                                        \
                    198:                                *dst++ = '?';                                 \
                    199:                        else                                                  \
                    200:                                *dst++ = c + '@';                             \
                    201:                } else {                                                      \
                    202:                        *dst++ = '-'; *dst++ = c;                             \
                    203:                }                                                             \
                    204:        }                                                                     \
                    205: } while (/*CONSTCOND*/0)
                    206: 
                    207: 
                    208: /*
                    209:  * svis - visually encode characters, also encoding the characters
                    210:  *       pointed to by `extra'
                    211:  */
                    212: char *
                    213: svis(dst, c, flag, nextc, extra)
                    214:        char *dst;
                    215:        int c, flag, nextc;
                    216:        const char *extra;
                    217: {
                    218:        char *nextra;
                    219:        _DIAGASSERT(dst != NULL);
                    220:        _DIAGASSERT(extra != NULL);
                    221:        MAKEEXTRALIST(flag, nextra, extra);
                    222:        if (flag & VIS_HTTPSTYLE)
                    223:                HVIS(dst, c, flag, nextc, nextra);
                    224:        else
                    225:                SVIS(dst, c, flag, nextc, nextra);
                    226:        *dst = '\0';
                    227:        free(nextra);
                    228:        return(dst);
                    229: }
                    230: 
                    231: #ifndef HAVE_STRSVIS
                    232: /*
                    233:  * strsvis, strsvisx - visually encode characters from src into dst
                    234:  *
                    235:  *     Extra is a pointer to a \0-terminated list of characters to
                    236:  *     be encoded, too. These functions are useful e. g. to
                    237:  *     encode strings in such a way so that they are not interpreted
                    238:  *     by a shell.
                    239:  *     
                    240:  *     Dst must be 4 times the size of src to account for possible
                    241:  *     expansion.  The length of dst, not including the trailing NULL,
                    242:  *     is returned. 
                    243:  *
                    244:  *     Strsvisx encodes exactly len bytes from src into dst.
                    245:  *     This is useful for encoding a block of data.
                    246:  */
                    247: int
                    248: strsvis(dst, src, flag, extra)
                    249:        char *dst;
                    250:        const char *src;
                    251:        int flag;
                    252:        const char *extra;
                    253: {
                    254:        char c;
                    255:        char *start;
                    256:        char *nextra;
                    257: 
                    258:        _DIAGASSERT(dst != NULL);
                    259:        _DIAGASSERT(src != NULL);
                    260:        _DIAGASSERT(extra != NULL);
                    261:        MAKEEXTRALIST(flag, nextra, extra);
                    262:        if (flag & VIS_HTTPSTYLE) {
                    263:                for (start = dst; (c = *src++) != '\0'; /* empty */)
                    264:                        HVIS(dst, c, flag, *src, nextra);
                    265:        } else {
                    266:                for (start = dst; (c = *src++) != '\0'; /* empty */)
                    267:                        SVIS(dst, c, flag, *src, nextra);
                    268:        }
                    269:        *dst = '\0';
                    270:        free(nextra);
                    271:        return (dst - start);
                    272: }
                    273: 
                    274: 
                    275: int
                    276: strsvisx(dst, src, len, flag, extra)
                    277:        char *dst;
                    278:        const char *src;
                    279:        size_t len;
                    280:        int flag;
                    281:        const char *extra;
                    282: {
                    283:        char c;
                    284:        char *start;
                    285:        char *nextra;
                    286: 
                    287:        _DIAGASSERT(dst != NULL);
                    288:        _DIAGASSERT(src != NULL);
                    289:        _DIAGASSERT(extra != NULL);
                    290:        MAKEEXTRALIST(flag, nextra, extra);
                    291: 
                    292:        if (flag & VIS_HTTPSTYLE) {
                    293:                for (start = dst; len > 0; len--) {
                    294:                        c = *src++;
                    295:                        HVIS(dst, c, flag, len ? *src : '\0', nextra);
                    296:                }
                    297:        } else {
                    298:                for (start = dst; len > 0; len--) {
                    299:                        c = *src++;
                    300:                        SVIS(dst, c, flag, len ? *src : '\0', nextra);
                    301:                }
                    302:        }
                    303:        *dst = '\0';
                    304:        free(nextra);
                    305:        return (dst - start);
                    306: }
                    307: #endif
                    308: 
                    309: 
                    310: /*
                    311:  * vis - visually encode characters
                    312:  */
                    313: char *
                    314: vis(dst, c, flag, nextc)
                    315:        char *dst;
                    316:        int c, flag, nextc;
                    317:        
                    318: {
                    319:        char *extra;
                    320: 
                    321:        _DIAGASSERT(dst != NULL);
                    322: 
                    323:        MAKEEXTRALIST(flag, extra, "");
                    324:        if (flag & VIS_HTTPSTYLE)
                    325:            HVIS(dst, c, flag, nextc, extra);
                    326:        else
                    327:            SVIS(dst, c, flag, nextc, extra);
                    328:        *dst = '\0';
                    329:        free(extra);
                    330:        return (dst);
                    331: }
                    332: 
1.2     ! jlam      333: #if HAVE_STRVIS
1.1       grant     334: /*
                    335:  * strvis, strvisx - visually encode characters from src into dst
                    336:  *     
                    337:  *     Dst must be 4 times the size of src to account for possible
                    338:  *     expansion.  The length of dst, not including the trailing NULL,
                    339:  *     is returned. 
                    340:  *
                    341:  *     Strvisx encodes exactly len bytes from src into dst.
                    342:  *     This is useful for encoding a block of data.
                    343:  */
                    344: int
                    345: strvis(dst, src, flag)
                    346:        char *dst;
                    347:        const char *src;
                    348:        int flag;
                    349: {
                    350:        char *extra;
                    351:        int ret;
                    352: 
                    353:        MAKEEXTRALIST(flag, extra, "");
                    354:        ret = strsvis(dst, src, flag, extra);
                    355:        free(extra);
                    356:        return(ret);
                    357: }
                    358: 
                    359: 
                    360: int
                    361: strvisx(dst, src, len, flag)
                    362:        char *dst;
                    363:        const char *src;
                    364:        size_t len;
                    365:        int flag;
                    366: {
                    367:        char *extra;
                    368:        int ret;
                    369: 
                    370:        MAKEEXTRALIST(flag, extra, "");
                    371:        ret = strsvisx(dst, src, len, flag, extra);
                    372:        free(extra);
                    373:        return(ret);
                    374: }
                    375: #endif

CVSweb <webmaster@jp.NetBSD.org>