[BACK]Return to chars.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/chars.c, Revision 1.62

1.62    ! schwarze    1: /*     $Id: chars.c,v 1.61 2014/10/26 18:07:28 schwarze Exp $ */
1.1       kristaps    2: /*
1.51      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.60      schwarze    4:  * Copyright (c) 2011, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.14      kristaps   18: #include "config.h"
1.59      schwarze   19:
                     20: #include <sys/types.h>
1.14      kristaps   21:
1.1       kristaps   22: #include <assert.h>
1.40      kristaps   23: #include <ctype.h>
1.1       kristaps   24: #include <stdlib.h>
                     25: #include <string.h>
                     26:
1.18      kristaps   27: #include "mandoc.h"
1.56      schwarze   28: #include "mandoc_aux.h"
1.43      kristaps   29: #include "libmandoc.h"
1.1       kristaps   30:
1.2       kristaps   31: #define        PRINT_HI         126
                     32: #define        PRINT_LO         32
1.1       kristaps   33:
                     34: struct ln {
                     35:        struct ln        *next;
                     36:        const char       *code;
1.2       kristaps   37:        const char       *ascii;
1.21      kristaps   38:        int               unicode;
1.1       kristaps   39: };
                     40:
1.55      schwarze   41: #define        LINES_MAX         330
1.1       kristaps   42:
1.24      kristaps   43: #define CHAR(in, ch, code) \
1.45      kristaps   44:        { NULL, (in), (ch), (code) },
1.1       kristaps   45:
1.13      kristaps   46: #define        CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
                     47: #define        CHAR_TBL_END      };
                     48:
1.1       kristaps   49: #include "chars.in"
                     50:
1.36      kristaps   51: struct mchars {
1.1       kristaps   52:        struct ln       **htab;
                     53: };
                     54:
1.57      schwarze   55: static const struct ln  *find(const struct mchars *,
1.52      kristaps   56:                                const char *, size_t);
1.1       kristaps   57:
1.57      schwarze   58:
1.1       kristaps   59: void
1.36      kristaps   60: mchars_free(struct mchars *arg)
1.1       kristaps   61: {
                     62:
1.36      kristaps   63:        free(arg->htab);
                     64:        free(arg);
1.1       kristaps   65: }
                     66:
1.36      kristaps   67: struct mchars *
1.38      kristaps   68: mchars_alloc(void)
1.1       kristaps   69: {
1.36      kristaps   70:        struct mchars    *tab;
1.1       kristaps   71:        struct ln       **htab;
                     72:        struct ln        *pp;
                     73:        int               i, hash;
                     74:
                     75:        /*
                     76:         * Constructs a very basic chaining hashtable.  The hash routine
                     77:         * is simply the integral value of the first character.
1.47      kristaps   78:         * Subsequent entries are chained in the order they're processed.
1.1       kristaps   79:         */
                     80:
1.36      kristaps   81:        tab = mandoc_malloc(sizeof(struct mchars));
1.53      schwarze   82:        htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln *));
1.1       kristaps   83:
                     84:        for (i = 0; i < LINES_MAX; i++) {
1.2       kristaps   85:                hash = (int)lines[i].code[0] - PRINT_LO;
1.1       kristaps   86:
                     87:                if (NULL == (pp = htab[hash])) {
                     88:                        htab[hash] = &lines[i];
                     89:                        continue;
                     90:                }
                     91:
                     92:                for ( ; pp->next; pp = pp->next)
                     93:                        /* Scan ahead. */ ;
                     94:                pp->next = &lines[i];
                     95:        }
                     96:
                     97:        tab->htab = htab;
                     98:        return(tab);
                     99: }
                    100:
1.21      kristaps  101: int
1.52      kristaps  102: mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz)
1.21      kristaps  103: {
                    104:        const struct ln *ln;
                    105:
1.45      kristaps  106:        ln = find(arg, p, sz);
1.62    ! schwarze  107:        return(ln != NULL ? ln->unicode : sz == 1 ? *p : 0xFFFD);
1.21      kristaps  108: }
                    109:
1.39      kristaps  110: char
1.36      kristaps  111: mchars_num2char(const char *p, size_t sz)
1.32      schwarze  112: {
1.57      schwarze  113:        int       i;
1.32      schwarze  114:
1.48      kristaps  115:        if ((i = mandoc_strntoi(p, sz, 10)) < 0)
1.39      kristaps  116:                return('\0');
1.57      schwarze  117:
                    118:        return(i > 0 && i < 256 && isprint(i) ? i : '\0');
1.44      kristaps  119: }
                    120:
                    121: int
                    122: mchars_num2uc(const char *p, size_t sz)
                    123: {
1.57      schwarze  124:        int      i;
1.39      kristaps  125:
1.48      kristaps  126:        if ((i = mandoc_strntoi(p, sz, 16)) < 0)
1.60      schwarze  127:                return(0xFFFD);
1.58      schwarze  128:
                    129:        /*
                    130:         * XXX Code is missing here to exclude bogus ranges.
                    131:         */
                    132:
1.60      schwarze  133:        return(i <= 0x10FFFF ? i : 0xFFFD);
1.21      kristaps  134: }
                    135:
1.1       kristaps  136: const char *
1.57      schwarze  137: mchars_spec2str(const struct mchars *arg,
1.52      kristaps  138:                const char *p, size_t sz, size_t *rsz)
1.1       kristaps  139: {
1.21      kristaps  140:        const struct ln *ln;
1.1       kristaps  141:
1.45      kristaps  142:        ln = find(arg, p, sz);
1.60      schwarze  143:        if (ln == NULL) {
1.50      schwarze  144:                *rsz = 1;
1.60      schwarze  145:                return(sz == 1 ? p : NULL);
1.50      schwarze  146:        }
1.21      kristaps  147:
1.24      kristaps  148:        *rsz = strlen(ln->ascii);
1.21      kristaps  149:        return(ln->ascii);
1.61      schwarze  150: }
                    151:
                    152: const char *
                    153: mchars_uc2str(int uc)
                    154: {
                    155:        int      i;
                    156:
                    157:        for (i = 0; i < LINES_MAX; i++)
                    158:                if (uc == lines[i].unicode)
                    159:                        return(lines[i].ascii);
                    160:        return("<?>");
1.1       kristaps  161: }
                    162:
1.21      kristaps  163: static const struct ln *
1.52      kristaps  164: find(const struct mchars *tab, const char *p, size_t sz)
1.1       kristaps  165: {
1.52      kristaps  166:        const struct ln  *pp;
1.1       kristaps  167:        int               hash;
                    168:
                    169:        assert(p);
                    170:
1.47      kristaps  171:        if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI)
1.1       kristaps  172:                return(NULL);
                    173:
1.2       kristaps  174:        hash = (int)p[0] - PRINT_LO;
1.1       kristaps  175:
1.47      kristaps  176:        for (pp = tab->htab[hash]; pp; pp = pp->next)
1.57      schwarze  177:                if (0 == strncmp(pp->code, p, sz) &&
                    178:                    '\0' == pp->code[(int)sz])
1.47      kristaps  179:                        return(pp);
1.1       kristaps  180:
                    181:        return(NULL);
                    182: }

CVSweb