Print this page
Update to 1.12.3.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/chars.c
+++ new/usr/src/cmd/mandoc/chars.c
1 -/* $Id: chars.c,v 1.52 2011/11/08 00:15:23 kristaps Exp $ */
1 +/* $Id: chars.c,v 1.54 2013/06/20 22:39:30 schwarze Exp $ */
2 2 /*
3 3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
5 5 *
6 6 * Permission to use, copy, modify, and distribute this software for any
7 7 * purpose with or without fee is hereby granted, provided that the above
8 8 * copyright notice and this permission notice appear in all copies.
9 9 *
10 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 17 */
18 18 #ifdef HAVE_CONFIG_H
19 19 #include "config.h"
20 20 #endif
21 21
22 22 #include <assert.h>
23 23 #include <ctype.h>
24 24 #include <stdlib.h>
25 25 #include <string.h>
26 26
27 27 #include "mandoc.h"
28 28 #include "libmandoc.h"
29 29
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
30 30 #define PRINT_HI 126
31 31 #define PRINT_LO 32
32 32
33 33 struct ln {
34 34 struct ln *next;
35 35 const char *code;
36 36 const char *ascii;
37 37 int unicode;
38 38 };
39 39
40 -#define LINES_MAX 328
40 +#define LINES_MAX 329
41 41
42 42 #define CHAR(in, ch, code) \
43 43 { NULL, (in), (ch), (code) },
44 44
45 45 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
46 46 #define CHAR_TBL_END };
47 47
48 48 #include "chars.in"
49 49
50 50 struct mchars {
51 51 struct ln **htab;
52 52 };
53 53
54 54 static const struct ln *find(const struct mchars *,
55 55 const char *, size_t);
56 56
57 57 void
58 58 mchars_free(struct mchars *arg)
59 59 {
60 60
61 61 free(arg->htab);
62 62 free(arg);
63 63 }
64 64
65 65 struct mchars *
66 66 mchars_alloc(void)
67 67 {
68 68 struct mchars *tab;
69 69 struct ln **htab;
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
70 70 struct ln *pp;
71 71 int i, hash;
72 72
73 73 /*
74 74 * Constructs a very basic chaining hashtable. The hash routine
75 75 * is simply the integral value of the first character.
76 76 * Subsequent entries are chained in the order they're processed.
77 77 */
78 78
79 79 tab = mandoc_malloc(sizeof(struct mchars));
80 - htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
80 + htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln *));
81 81
82 82 for (i = 0; i < LINES_MAX; i++) {
83 83 hash = (int)lines[i].code[0] - PRINT_LO;
84 84
85 85 if (NULL == (pp = htab[hash])) {
86 86 htab[hash] = &lines[i];
87 87 continue;
88 88 }
89 89
90 90 for ( ; pp->next; pp = pp->next)
91 91 /* Scan ahead. */ ;
92 92 pp->next = &lines[i];
93 93 }
94 94
95 95 tab->htab = htab;
96 96 return(tab);
97 97 }
98 98
99 99 int
100 100 mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz)
101 101 {
102 102 const struct ln *ln;
103 103
104 104 ln = find(arg, p, sz);
105 105 if (NULL == ln)
106 106 return(-1);
107 107 return(ln->unicode);
108 108 }
109 109
110 110 char
111 111 mchars_num2char(const char *p, size_t sz)
112 112 {
113 113 int i;
114 114
115 115 if ((i = mandoc_strntoi(p, sz, 10)) < 0)
116 116 return('\0');
117 117 return(i > 0 && i < 256 && isprint(i) ?
118 118 /* LINTED */ i : '\0');
119 119 }
120 120
121 121 int
122 122 mchars_num2uc(const char *p, size_t sz)
123 123 {
124 124 int i;
125 125
126 126 if ((i = mandoc_strntoi(p, sz, 16)) < 0)
127 127 return('\0');
128 128 /* FIXME: make sure we're not in a bogus range. */
129 129 return(i > 0x80 && i <= 0x10FFFF ? i : '\0');
130 130 }
131 131
132 132 const char *
133 133 mchars_spec2str(const struct mchars *arg,
134 134 const char *p, size_t sz, size_t *rsz)
135 135 {
136 136 const struct ln *ln;
137 137
138 138 ln = find(arg, p, sz);
139 139 if (NULL == ln) {
140 140 *rsz = 1;
141 141 return(NULL);
142 142 }
143 143
144 144 *rsz = strlen(ln->ascii);
145 145 return(ln->ascii);
146 146 }
147 147
148 148 static const struct ln *
149 149 find(const struct mchars *tab, const char *p, size_t sz)
150 150 {
151 151 const struct ln *pp;
152 152 int hash;
153 153
154 154 assert(p);
155 155
156 156 if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI)
157 157 return(NULL);
158 158
159 159 hash = (int)p[0] - PRINT_LO;
160 160
161 161 for (pp = tab->htab[hash]; pp; pp = pp->next)
162 162 if (0 == strncmp(pp->code, p, sz) &&
163 163 '\0' == pp->code[(int)sz])
164 164 return(pp);
165 165
166 166 return(NULL);
167 167 }
↓ open down ↓ |
77 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX