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

Annotation of mandoc/mdoc_hash.c, Revision 1.28

1.28    ! schwarze    1: /*     $Id: mdoc_hash.c,v 1.27 2016/07/15 18:03:45 schwarze Exp $ */
1.1       kristaps    2: /*
1.16      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
1.28    ! schwarze    4:  * Copyright (c) 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.3       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.3       kristaps   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.
1.1       kristaps   17:  */
1.12      kristaps   18: #include "config.h"
                     19:
1.8       kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
                     23: #include <ctype.h>
1.9       kristaps   24: #include <limits.h>
1.1       kristaps   25: #include <stdlib.h>
                     26: #include <stdio.h>
                     27: #include <string.h>
                     28:
1.27      schwarze   29: #include "mandoc.h"
1.22      schwarze   30: #include "roff.h"
1.17      kristaps   31: #include "mdoc.h"
1.27      schwarze   32: #include "libmandoc.h"
1.1       kristaps   33: #include "libmdoc.h"
                     34:
1.18      kristaps   35: static unsigned char    table[27 * 12];
1.6       kristaps   36:
1.20      schwarze   37:
1.1       kristaps   38: void
1.9       kristaps   39: mdoc_hash_init(void)
1.1       kristaps   40: {
1.9       kristaps   41:        int              i, j, major;
                     42:        const char      *p;
1.24      schwarze   43:
                     44:        if (*table != '\0')
                     45:                return;
1.1       kristaps   46:
1.9       kristaps   47:        memset(table, UCHAR_MAX, sizeof(table));
1.1       kristaps   48:
1.28    ! schwarze   49:        for (i = 0; i < (int)(MDOC_MAX - MDOC_Dd); i++) {
        !            50:                p = roff_name[MDOC_Dd + i];
1.1       kristaps   51:
1.18      kristaps   52:                if (isalpha((unsigned char)p[1]))
                     53:                        major = 12 * (tolower((unsigned char)p[1]) - 97);
1.9       kristaps   54:                else
                     55:                        major = 12 * 26;
                     56:
                     57:                for (j = 0; j < 12; j++)
                     58:                        if (UCHAR_MAX == table[major + j]) {
1.18      kristaps   59:                                table[major + j] = (unsigned char)i;
1.9       kristaps   60:                                break;
                     61:                        }
1.1       kristaps   62:
1.9       kristaps   63:                assert(j < 12);
1.1       kristaps   64:        }
                     65: }
                     66:
1.28    ! schwarze   67: enum roff_tok
1.9       kristaps   68: mdoc_hash_find(const char *p)
1.1       kristaps   69: {
1.9       kristaps   70:        int               major, i, j;
1.1       kristaps   71:
1.9       kristaps   72:        if (0 == p[0])
1.26      schwarze   73:                return TOKEN_NONE;
1.18      kristaps   74:        if ( ! isalpha((unsigned char)p[0]) && '%' != p[0])
1.26      schwarze   75:                return TOKEN_NONE;
1.6       kristaps   76:
1.18      kristaps   77:        if (isalpha((unsigned char)p[1]))
                     78:                major = 12 * (tolower((unsigned char)p[1]) - 97);
1.9       kristaps   79:        else if ('1' == p[1])
                     80:                major = 12 * 26;
1.20      schwarze   81:        else
1.26      schwarze   82:                return TOKEN_NONE;
1.1       kristaps   83:
1.9       kristaps   84:        if (p[2] && p[3])
1.26      schwarze   85:                return TOKEN_NONE;
1.1       kristaps   86:
1.9       kristaps   87:        for (j = 0; j < 12; j++) {
                     88:                if (UCHAR_MAX == (i = table[major + j]))
                     89:                        break;
1.28    ! schwarze   90:                if (strcmp(p, roff_name[MDOC_Dd + i]) == 0)
        !            91:                        return MDOC_Dd + i;
1.1       kristaps   92:        }
                     93:
1.26      schwarze   94:        return TOKEN_NONE;
1.1       kristaps   95: }

CVSweb