1*260e9a87SYuri Pankov /* $Id: mdoc_hash.c,v 1.21 2014/08/10 23:54:41 schwarze Exp $ */ 295c635efSGarrett D'Amore /* 395c635efSGarrett D'Amore * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv> 495c635efSGarrett D'Amore * 595c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any 695c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above 795c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies. 895c635efSGarrett D'Amore * 995c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1095c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1195c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1295c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1395c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1495c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1595c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1695c635efSGarrett D'Amore */ 1795c635efSGarrett D'Amore #include "config.h" 1895c635efSGarrett D'Amore 1995c635efSGarrett D'Amore #include <sys/types.h> 2095c635efSGarrett D'Amore 2195c635efSGarrett D'Amore #include <assert.h> 2295c635efSGarrett D'Amore #include <ctype.h> 2395c635efSGarrett D'Amore #include <limits.h> 2495c635efSGarrett D'Amore #include <stdlib.h> 2595c635efSGarrett D'Amore #include <stdio.h> 2695c635efSGarrett D'Amore #include <string.h> 2795c635efSGarrett D'Amore 2895c635efSGarrett D'Amore #include "mdoc.h" 2995c635efSGarrett D'Amore #include "libmdoc.h" 3095c635efSGarrett D'Amore 3195c635efSGarrett D'Amore static unsigned char table[27 * 12]; 3295c635efSGarrett D'Amore 33*260e9a87SYuri Pankov 3495c635efSGarrett D'Amore /* 3595c635efSGarrett D'Amore * XXX - this hash has global scope, so if intended for use as a library 3695c635efSGarrett D'Amore * with multiple callers, it will need re-invocation protection. 3795c635efSGarrett D'Amore */ 3895c635efSGarrett D'Amore void 3995c635efSGarrett D'Amore mdoc_hash_init(void) 4095c635efSGarrett D'Amore { 4195c635efSGarrett D'Amore int i, j, major; 4295c635efSGarrett D'Amore const char *p; 4395c635efSGarrett D'Amore 4495c635efSGarrett D'Amore memset(table, UCHAR_MAX, sizeof(table)); 4595c635efSGarrett D'Amore 4695c635efSGarrett D'Amore for (i = 0; i < (int)MDOC_MAX; i++) { 4795c635efSGarrett D'Amore p = mdoc_macronames[i]; 4895c635efSGarrett D'Amore 4995c635efSGarrett D'Amore if (isalpha((unsigned char)p[1])) 5095c635efSGarrett D'Amore major = 12 * (tolower((unsigned char)p[1]) - 97); 5195c635efSGarrett D'Amore else 5295c635efSGarrett D'Amore major = 12 * 26; 5395c635efSGarrett D'Amore 5495c635efSGarrett D'Amore for (j = 0; j < 12; j++) 5595c635efSGarrett D'Amore if (UCHAR_MAX == table[major + j]) { 5695c635efSGarrett D'Amore table[major + j] = (unsigned char)i; 5795c635efSGarrett D'Amore break; 5895c635efSGarrett D'Amore } 5995c635efSGarrett D'Amore 6095c635efSGarrett D'Amore assert(j < 12); 6195c635efSGarrett D'Amore } 6295c635efSGarrett D'Amore } 6395c635efSGarrett D'Amore 6495c635efSGarrett D'Amore enum mdoct 6595c635efSGarrett D'Amore mdoc_hash_find(const char *p) 6695c635efSGarrett D'Amore { 6795c635efSGarrett D'Amore int major, i, j; 6895c635efSGarrett D'Amore 6995c635efSGarrett D'Amore if (0 == p[0]) 7095c635efSGarrett D'Amore return(MDOC_MAX); 7195c635efSGarrett D'Amore if ( ! isalpha((unsigned char)p[0]) && '%' != p[0]) 7295c635efSGarrett D'Amore return(MDOC_MAX); 7395c635efSGarrett D'Amore 7495c635efSGarrett D'Amore if (isalpha((unsigned char)p[1])) 7595c635efSGarrett D'Amore major = 12 * (tolower((unsigned char)p[1]) - 97); 7695c635efSGarrett D'Amore else if ('1' == p[1]) 7795c635efSGarrett D'Amore major = 12 * 26; 7895c635efSGarrett D'Amore else 7995c635efSGarrett D'Amore return(MDOC_MAX); 8095c635efSGarrett D'Amore 8195c635efSGarrett D'Amore if (p[2] && p[3]) 8295c635efSGarrett D'Amore return(MDOC_MAX); 8395c635efSGarrett D'Amore 8495c635efSGarrett D'Amore for (j = 0; j < 12; j++) { 8595c635efSGarrett D'Amore if (UCHAR_MAX == (i = table[major + j])) 8695c635efSGarrett D'Amore break; 8795c635efSGarrett D'Amore if (0 == strcmp(p, mdoc_macronames[i])) 8895c635efSGarrett D'Amore return((enum mdoct)i); 8995c635efSGarrett D'Amore } 9095c635efSGarrett D'Amore 9195c635efSGarrett D'Amore return(MDOC_MAX); 9295c635efSGarrett D'Amore } 93