[BACK]Return to chars.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/chars.c, Revision 1.16

1.16    ! schwarze    1: /*     $Id: chars.c,v 1.15 2011/01/04 22:28:17 schwarze Exp $ */
1.1       schwarze    2: /*
1.9       schwarze    3:  * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.16    ! schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    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:  */
                     18: #include <assert.h>
1.3       schwarze   19: #include <stdio.h>
1.1       schwarze   20: #include <stdlib.h>
                     21: #include <string.h>
                     22:
1.7       schwarze   23: #include "mandoc.h"
1.1       schwarze   24: #include "chars.h"
                     25:
                     26: #define        PRINT_HI         126
                     27: #define        PRINT_LO         32
                     28:
                     29: struct ln {
                     30:        struct ln        *next;
                     31:        const char       *code;
                     32:        const char       *ascii;
1.9       schwarze   33:        int               unicode;
1.1       schwarze   34:        int               type;
                     35: #define        CHARS_CHAR       (1 << 0)
                     36: #define        CHARS_STRING     (1 << 1)
1.4       schwarze   37: #define CHARS_BOTH      (CHARS_CHAR | CHARS_STRING)
1.1       schwarze   38: };
                     39:
1.14      schwarze   40: #define        LINES_MAX         351
1.1       schwarze   41:
1.10      schwarze   42: #define CHAR(in, ch, code) \
                     43:        { NULL, (in), (ch), (code), CHARS_CHAR },
                     44: #define STRING(in, ch, code) \
                     45:        { NULL, (in), (ch), (code), CHARS_STRING },
                     46: #define BOTH(in, ch, code) \
                     47:        { NULL, (in), (ch), (code), CHARS_BOTH },
1.1       schwarze   48:
1.4       schwarze   49: #define        CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
                     50: #define        CHAR_TBL_END      };
                     51:
1.1       schwarze   52: #include "chars.in"
                     53:
1.15      schwarze   54: struct ctab {
1.1       schwarze   55:        enum chars        type;
                     56:        struct ln       **htab;
                     57: };
                     58:
                     59: static inline int        match(const struct ln *,
                     60:                                const char *, size_t, int);
1.15      schwarze   61: static const struct ln  *find(struct ctab *, const char *, size_t, int);
1.1       schwarze   62:
                     63:
                     64: void
                     65: chars_free(void *arg)
                     66: {
1.15      schwarze   67:        struct ctab     *tab;
1.1       schwarze   68:
1.15      schwarze   69:        tab = (struct ctab *)arg;
1.1       schwarze   70:
                     71:        free(tab->htab);
                     72:        free(tab);
                     73: }
                     74:
                     75:
                     76: void *
                     77: chars_init(enum chars type)
                     78: {
1.15      schwarze   79:        struct ctab      *tab;
1.1       schwarze   80:        struct ln       **htab;
                     81:        struct ln        *pp;
                     82:        int               i, hash;
                     83:
                     84:        /*
                     85:         * Constructs a very basic chaining hashtable.  The hash routine
                     86:         * is simply the integral value of the first character.
                     87:         * Subsequent entries are chained in the order they're processed
                     88:         * (they're in-line re-ordered during lookup).
                     89:         */
                     90:
1.15      schwarze   91:        tab = malloc(sizeof(struct ctab));
1.3       schwarze   92:        if (NULL == tab) {
                     93:                perror(NULL);
1.14      schwarze   94:                exit((int)MANDOCLEVEL_SYSERR);
1.3       schwarze   95:        }
1.1       schwarze   96:
                     97:        htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
1.3       schwarze   98:        if (NULL == htab) {
                     99:                perror(NULL);
1.14      schwarze  100:                exit((int)MANDOCLEVEL_SYSERR);
1.3       schwarze  101:        }
1.1       schwarze  102:
                    103:        for (i = 0; i < LINES_MAX; i++) {
                    104:                hash = (int)lines[i].code[0] - PRINT_LO;
                    105:
                    106:                if (NULL == (pp = htab[hash])) {
                    107:                        htab[hash] = &lines[i];
                    108:                        continue;
                    109:                }
                    110:
                    111:                for ( ; pp->next; pp = pp->next)
                    112:                        /* Scan ahead. */ ;
                    113:                pp->next = &lines[i];
                    114:        }
                    115:
                    116:        tab->htab = htab;
1.3       schwarze  117:        tab->type = type;
1.1       schwarze  118:        return(tab);
                    119: }
                    120:
                    121:
1.9       schwarze  122: /*
                    123:  * Special character to Unicode codepoint.
                    124:  */
                    125: int
                    126: chars_spec2cp(void *arg, const char *p, size_t sz)
                    127: {
                    128:        const struct ln *ln;
                    129:
1.15      schwarze  130:        ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
1.9       schwarze  131:        if (NULL == ln)
                    132:                return(-1);
                    133:        return(ln->unicode);
                    134: }
                    135:
                    136:
                    137: /*
                    138:  * Reserved word to Unicode codepoint.
                    139:  */
                    140: int
                    141: chars_res2cp(void *arg, const char *p, size_t sz)
                    142: {
                    143:        const struct ln *ln;
                    144:
1.15      schwarze  145:        ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
1.9       schwarze  146:        if (NULL == ln)
                    147:                return(-1);
                    148:        return(ln->unicode);
1.16    ! schwarze  149: }
        !           150:
        !           151:
        !           152: /*
        !           153:  * Numbered character to literal character,
        !           154:  * represented as a null-terminated string for additional safety.
        !           155:  */
        !           156: const char *
        !           157: chars_num2char(const char *p, size_t sz)
        !           158: {
        !           159:        int               i;
        !           160:        static char       c[2];
        !           161:
        !           162:        if (sz > 3)
        !           163:                return(NULL);
        !           164:        i = atoi(p);
        !           165:        if (i < 0 || i > 255)
        !           166:                return(NULL);
        !           167:        c[0] = (char)i;
        !           168:        c[1] = '\0';
        !           169:        return(c);
1.9       schwarze  170: }
                    171:
                    172:
                    173: /*
                    174:  * Special character to string array.
                    175:  */
1.1       schwarze  176: const char *
1.9       schwarze  177: chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
1.1       schwarze  178: {
1.9       schwarze  179:        const struct ln *ln;
                    180:
1.15      schwarze  181:        ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
1.9       schwarze  182:        if (NULL == ln)
                    183:                return(NULL);
1.1       schwarze  184:
1.10      schwarze  185:        *rsz = strlen(ln->ascii);
1.9       schwarze  186:        return(ln->ascii);
1.1       schwarze  187: }
                    188:
                    189:
1.9       schwarze  190: /*
                    191:  * Reserved word to string array.
                    192:  */
1.1       schwarze  193: const char *
1.9       schwarze  194: chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
1.1       schwarze  195: {
1.9       schwarze  196:        const struct ln *ln;
                    197:
1.15      schwarze  198:        ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
1.9       schwarze  199:        if (NULL == ln)
                    200:                return(NULL);
1.1       schwarze  201:
1.10      schwarze  202:        *rsz = strlen(ln->ascii);
1.9       schwarze  203:        return(ln->ascii);
1.1       schwarze  204: }
                    205:
                    206:
1.9       schwarze  207: static const struct ln *
1.15      schwarze  208: find(struct ctab *tab, const char *p, size_t sz, int type)
1.1       schwarze  209: {
                    210:        struct ln        *pp, *prev;
                    211:        struct ln       **htab;
                    212:        int               hash;
                    213:
                    214:        assert(p);
1.9       schwarze  215:        if (0 == sz)
                    216:                return(NULL);
1.1       schwarze  217:
                    218:        if (p[0] < PRINT_LO || p[0] > PRINT_HI)
                    219:                return(NULL);
                    220:
                    221:        /*
                    222:         * Lookup the symbol in the symbol hash.  See ascii2htab for the
                    223:         * hashtable specs.  This dynamically re-orders the hash chain
                    224:         * to optimise for repeat hits.
                    225:         */
                    226:
                    227:        hash = (int)p[0] - PRINT_LO;
                    228:        htab = tab->htab;
                    229:
                    230:        if (NULL == (pp = htab[hash]))
                    231:                return(NULL);
                    232:
                    233:        for (prev = NULL; pp; pp = pp->next) {
                    234:                if ( ! match(pp, p, sz, type)) {
                    235:                        prev = pp;
                    236:                        continue;
                    237:                }
                    238:
                    239:                if (prev) {
                    240:                        prev->next = pp->next;
                    241:                        pp->next = htab[hash];
                    242:                        htab[hash] = pp;
                    243:                }
                    244:
1.9       schwarze  245:                return(pp);
1.1       schwarze  246:        }
                    247:
                    248:        return(NULL);
                    249: }
                    250:
                    251:
                    252: static inline int
                    253: match(const struct ln *ln, const char *p, size_t sz, int type)
                    254: {
                    255:
                    256:        if ( ! (ln->type & type))
                    257:                return(0);
1.9       schwarze  258:        if (strncmp(ln->code, p, sz))
1.1       schwarze  259:                return(0);
1.9       schwarze  260:        return('\0' == ln->code[(int)sz]);
1.1       schwarze  261: }