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