xref: /openbsd-src/usr.bin/mandoc/chars.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /*	$Id: chars.c,v 1.23 2011/11/12 22:43:18 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(const struct mchars *,
51 				const char *, size_t);
52 
53 void
54 mchars_free(struct mchars *arg)
55 {
56 
57 	free(arg->htab);
58 	free(arg);
59 }
60 
61 struct mchars *
62 mchars_alloc(void)
63 {
64 	struct mchars	 *tab;
65 	struct ln	**htab;
66 	struct ln	 *pp;
67 	int		  i, hash;
68 
69 	/*
70 	 * Constructs a very basic chaining hashtable.  The hash routine
71 	 * is simply the integral value of the first character.
72 	 * Subsequent entries are chained in the order they're processed.
73 	 */
74 
75 	tab = mandoc_malloc(sizeof(struct mchars));
76 	htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
77 
78 	for (i = 0; i < LINES_MAX; i++) {
79 		hash = (int)lines[i].code[0] - PRINT_LO;
80 
81 		if (NULL == (pp = htab[hash])) {
82 			htab[hash] = &lines[i];
83 			continue;
84 		}
85 
86 		for ( ; pp->next; pp = pp->next)
87 			/* Scan ahead. */ ;
88 		pp->next = &lines[i];
89 	}
90 
91 	tab->htab = htab;
92 	return(tab);
93 }
94 
95 int
96 mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz)
97 {
98 	const struct ln	*ln;
99 
100 	ln = find(arg, p, sz);
101 	if (NULL == ln)
102 		return(-1);
103 	return(ln->unicode);
104 }
105 
106 char
107 mchars_num2char(const char *p, size_t sz)
108 {
109 	int		  i;
110 
111 	if ((i = mandoc_strntoi(p, sz, 10)) < 0)
112 		return('\0');
113 	return(i > 0 && i < 256 && isprint(i) ?
114 			/* LINTED */ i : '\0');
115 }
116 
117 int
118 mchars_num2uc(const char *p, size_t sz)
119 {
120 	int               i;
121 
122 	if ((i = mandoc_strntoi(p, sz, 16)) < 0)
123 		return('\0');
124 	/* FIXME: make sure we're not in a bogus range. */
125 	return(i > 0x80 && i <= 0x10FFFF ? i : '\0');
126 }
127 
128 const char *
129 mchars_spec2str(const struct mchars *arg,
130 		const char *p, size_t sz, size_t *rsz)
131 {
132 	const struct ln	*ln;
133 
134 	ln = find(arg, p, sz);
135 	if (NULL == ln) {
136 		*rsz = 1;
137 		return(NULL);
138 	}
139 
140 	*rsz = strlen(ln->ascii);
141 	return(ln->ascii);
142 }
143 
144 static const struct ln *
145 find(const struct mchars *tab, const char *p, size_t sz)
146 {
147 	const struct ln	 *pp;
148 	int		  hash;
149 
150 	assert(p);
151 
152 	if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI)
153 		return(NULL);
154 
155 	hash = (int)p[0] - PRINT_LO;
156 
157 	for (pp = tab->htab[hash]; pp; pp = pp->next)
158 		if (0 == strncmp(pp->code, p, sz) &&
159 				'\0' == pp->code[(int)sz])
160 			return(pp);
161 
162 	return(NULL);
163 }
164