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