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

Annotation of mandoc/chars.c, Revision 1.44

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

CVSweb