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

Annotation of mandoc/chars.c, Revision 1.39

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

CVSweb