1*95c635efSGarrett D'Amore /* $Id: mdoc_hash.c,v 1.18 2011/07/24 18:15:14 kristaps Exp $ */ 2*95c635efSGarrett D'Amore /* 3*95c635efSGarrett D'Amore * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv> 4*95c635efSGarrett D'Amore * 5*95c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any 6*95c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above 7*95c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies. 8*95c635efSGarrett D'Amore * 9*95c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10*95c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*95c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12*95c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*95c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*95c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15*95c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*95c635efSGarrett D'Amore */ 17*95c635efSGarrett D'Amore #ifdef HAVE_CONFIG_H 18*95c635efSGarrett D'Amore #include "config.h" 19*95c635efSGarrett D'Amore #endif 20*95c635efSGarrett D'Amore 21*95c635efSGarrett D'Amore #include <sys/types.h> 22*95c635efSGarrett D'Amore 23*95c635efSGarrett D'Amore #include <assert.h> 24*95c635efSGarrett D'Amore #include <ctype.h> 25*95c635efSGarrett D'Amore #include <limits.h> 26*95c635efSGarrett D'Amore #include <stdlib.h> 27*95c635efSGarrett D'Amore #include <stdio.h> 28*95c635efSGarrett D'Amore #include <string.h> 29*95c635efSGarrett D'Amore 30*95c635efSGarrett D'Amore #include "mdoc.h" 31*95c635efSGarrett D'Amore #include "mandoc.h" 32*95c635efSGarrett D'Amore #include "libmdoc.h" 33*95c635efSGarrett D'Amore 34*95c635efSGarrett D'Amore static unsigned char table[27 * 12]; 35*95c635efSGarrett D'Amore 36*95c635efSGarrett D'Amore /* 37*95c635efSGarrett D'Amore * XXX - this hash has global scope, so if intended for use as a library 38*95c635efSGarrett D'Amore * with multiple callers, it will need re-invocation protection. 39*95c635efSGarrett D'Amore */ 40*95c635efSGarrett D'Amore void 41*95c635efSGarrett D'Amore mdoc_hash_init(void) 42*95c635efSGarrett D'Amore { 43*95c635efSGarrett D'Amore int i, j, major; 44*95c635efSGarrett D'Amore const char *p; 45*95c635efSGarrett D'Amore 46*95c635efSGarrett D'Amore memset(table, UCHAR_MAX, sizeof(table)); 47*95c635efSGarrett D'Amore 48*95c635efSGarrett D'Amore for (i = 0; i < (int)MDOC_MAX; i++) { 49*95c635efSGarrett D'Amore p = mdoc_macronames[i]; 50*95c635efSGarrett D'Amore 51*95c635efSGarrett D'Amore if (isalpha((unsigned char)p[1])) 52*95c635efSGarrett D'Amore major = 12 * (tolower((unsigned char)p[1]) - 97); 53*95c635efSGarrett D'Amore else 54*95c635efSGarrett D'Amore major = 12 * 26; 55*95c635efSGarrett D'Amore 56*95c635efSGarrett D'Amore for (j = 0; j < 12; j++) 57*95c635efSGarrett D'Amore if (UCHAR_MAX == table[major + j]) { 58*95c635efSGarrett D'Amore table[major + j] = (unsigned char)i; 59*95c635efSGarrett D'Amore break; 60*95c635efSGarrett D'Amore } 61*95c635efSGarrett D'Amore 62*95c635efSGarrett D'Amore assert(j < 12); 63*95c635efSGarrett D'Amore } 64*95c635efSGarrett D'Amore } 65*95c635efSGarrett D'Amore 66*95c635efSGarrett D'Amore enum mdoct 67*95c635efSGarrett D'Amore mdoc_hash_find(const char *p) 68*95c635efSGarrett D'Amore { 69*95c635efSGarrett D'Amore int major, i, j; 70*95c635efSGarrett D'Amore 71*95c635efSGarrett D'Amore if (0 == p[0]) 72*95c635efSGarrett D'Amore return(MDOC_MAX); 73*95c635efSGarrett D'Amore if ( ! isalpha((unsigned char)p[0]) && '%' != p[0]) 74*95c635efSGarrett D'Amore return(MDOC_MAX); 75*95c635efSGarrett D'Amore 76*95c635efSGarrett D'Amore if (isalpha((unsigned char)p[1])) 77*95c635efSGarrett D'Amore major = 12 * (tolower((unsigned char)p[1]) - 97); 78*95c635efSGarrett D'Amore else if ('1' == p[1]) 79*95c635efSGarrett D'Amore major = 12 * 26; 80*95c635efSGarrett D'Amore else 81*95c635efSGarrett D'Amore return(MDOC_MAX); 82*95c635efSGarrett D'Amore 83*95c635efSGarrett D'Amore if (p[2] && p[3]) 84*95c635efSGarrett D'Amore return(MDOC_MAX); 85*95c635efSGarrett D'Amore 86*95c635efSGarrett D'Amore for (j = 0; j < 12; j++) { 87*95c635efSGarrett D'Amore if (UCHAR_MAX == (i = table[major + j])) 88*95c635efSGarrett D'Amore break; 89*95c635efSGarrett D'Amore if (0 == strcmp(p, mdoc_macronames[i])) 90*95c635efSGarrett D'Amore return((enum mdoct)i); 91*95c635efSGarrett D'Amore } 92*95c635efSGarrett D'Amore 93*95c635efSGarrett D'Amore return(MDOC_MAX); 94*95c635efSGarrett D'Amore } 95