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

Annotation of mandoc/chars.c, Revision 1.40

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

CVSweb