1 /* $Id: chars.c,v 1.22 2011/09/18 10:25:28 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org> 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 */ 18 #include <assert.h> 19 #include <ctype.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include "mandoc.h" 24 #include "libmandoc.h" 25 26 #define PRINT_HI 126 27 #define PRINT_LO 32 28 29 struct ln { 30 struct ln *next; 31 const char *code; 32 const char *ascii; 33 int unicode; 34 }; 35 36 #define LINES_MAX 328 37 38 #define CHAR(in, ch, code) \ 39 { NULL, (in), (ch), (code) }, 40 41 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = { 42 #define CHAR_TBL_END }; 43 44 #include "chars.in" 45 46 struct mchars { 47 struct ln **htab; 48 }; 49 50 static const struct ln *find(struct mchars *, const char *, size_t); 51 52 void 53 mchars_free(struct mchars *arg) 54 { 55 56 free(arg->htab); 57 free(arg); 58 } 59 60 struct mchars * 61 mchars_alloc(void) 62 { 63 struct mchars *tab; 64 struct ln **htab; 65 struct ln *pp; 66 int i, hash; 67 68 /* 69 * Constructs a very basic chaining hashtable. The hash routine 70 * is simply the integral value of the first character. 71 * Subsequent entries are chained in the order they're processed. 72 */ 73 74 tab = mandoc_malloc(sizeof(struct mchars)); 75 htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **)); 76 77 for (i = 0; i < LINES_MAX; i++) { 78 hash = (int)lines[i].code[0] - PRINT_LO; 79 80 if (NULL == (pp = htab[hash])) { 81 htab[hash] = &lines[i]; 82 continue; 83 } 84 85 for ( ; pp->next; pp = pp->next) 86 /* Scan ahead. */ ; 87 pp->next = &lines[i]; 88 } 89 90 tab->htab = htab; 91 return(tab); 92 } 93 94 int 95 mchars_spec2cp(struct mchars *arg, const char *p, size_t sz) 96 { 97 const struct ln *ln; 98 99 ln = find(arg, p, sz); 100 if (NULL == ln) 101 return(-1); 102 return(ln->unicode); 103 } 104 105 char 106 mchars_num2char(const char *p, size_t sz) 107 { 108 int i; 109 110 if ((i = mandoc_strntoi(p, sz, 10)) < 0) 111 return('\0'); 112 return(i > 0 && i < 256 && isprint(i) ? i : '\0'); 113 } 114 115 int 116 mchars_num2uc(const char *p, size_t sz) 117 { 118 int i; 119 120 if ((i = mandoc_strntoi(p, sz, 16)) < 0) 121 return('\0'); 122 /* FIXME: make sure we're not in a bogus range. */ 123 return(i > 0x80 && i <= 0x10FFFF ? i : '\0'); 124 } 125 126 const char * 127 mchars_spec2str(struct mchars *arg, const char *p, size_t sz, size_t *rsz) 128 { 129 const struct ln *ln; 130 131 ln = find(arg, p, sz); 132 if (NULL == ln) { 133 *rsz = 1; 134 return(NULL); 135 } 136 137 *rsz = strlen(ln->ascii); 138 return(ln->ascii); 139 } 140 141 static const struct ln * 142 find(struct mchars *tab, const char *p, size_t sz) 143 { 144 struct ln *pp; 145 int hash; 146 147 assert(p); 148 149 if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI) 150 return(NULL); 151 152 hash = (int)p[0] - PRINT_LO; 153 154 for (pp = tab->htab[hash]; pp; pp = pp->next) 155 if (0 == strncmp(pp->code, p, sz) && 156 '\0' == pp->code[(int)sz]) 157 return(pp); 158 159 return(NULL); 160 } 161