]> git.cameronkatri.com Git - mandoc.git/blob - tag.c
6595c9d09163cfdc3b165d7e4ceea3697b836e7b
[mandoc.git] / tag.c
1 /* $Id: tag.c,v 1.31 2020/04/02 22:12:55 schwarze Exp $ */
2 /*
3 * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * Functions to tag syntax tree nodes.
18 * For internal use by mandoc(1) validation modules only.
19 */
20 #include "config.h"
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "mandoc_aux.h"
31 #include "mandoc_ohash.h"
32 #include "roff.h"
33 #include "tag.h"
34
35 struct tag_entry {
36 struct roff_node **nodes;
37 size_t maxnodes;
38 size_t nnodes;
39 int prio;
40 char s[];
41 };
42
43 static struct ohash tag_data;
44
45
46 /*
47 * Set up the ohash table to collect nodes
48 * where various marked-up terms are documented.
49 */
50 void
51 tag_alloc(void)
52 {
53 mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
54 }
55
56 void
57 tag_free(void)
58 {
59 struct tag_entry *entry;
60 unsigned int slot;
61
62 if (tag_data.info.free == NULL)
63 return;
64 entry = ohash_first(&tag_data, &slot);
65 while (entry != NULL) {
66 free(entry->nodes);
67 free(entry);
68 entry = ohash_next(&tag_data, &slot);
69 }
70 ohash_delete(&tag_data);
71 tag_data.info.free = NULL;
72 }
73
74 /*
75 * Set a node where a term is defined,
76 * unless it is already defined at a lower priority.
77 */
78 void
79 tag_put(const char *s, int prio, struct roff_node *n)
80 {
81 struct tag_entry *entry;
82 const char *se;
83 size_t len;
84 unsigned int slot;
85
86 assert(prio <= TAG_FALLBACK);
87
88 if (s == NULL) {
89 if (n->child == NULL || n->child->type != ROFFT_TEXT)
90 return;
91 s = n->child->string;
92 switch (s[0]) {
93 case '-':
94 s++;
95 break;
96 case '\\':
97 switch (s[1]) {
98 case '&':
99 case '-':
100 case 'e':
101 s += 2;
102 break;
103 default:
104 break;
105 }
106 break;
107 default:
108 break;
109 }
110 }
111
112 /*
113 * Skip whitespace and escapes and whatever follows,
114 * and if there is any, downgrade the priority.
115 */
116
117 len = strcspn(s, " \t\\");
118 if (len == 0)
119 return;
120
121 se = s + len;
122 if (*se != '\0' && prio < TAG_WEAK)
123 prio = TAG_WEAK;
124
125 slot = ohash_qlookupi(&tag_data, s, &se);
126 entry = ohash_find(&tag_data, slot);
127
128 /* Build a new entry. */
129
130 if (entry == NULL) {
131 entry = mandoc_malloc(sizeof(*entry) + len + 1);
132 memcpy(entry->s, s, len);
133 entry->s[len] = '\0';
134 entry->nodes = NULL;
135 entry->maxnodes = entry->nnodes = 0;
136 ohash_insert(&tag_data, slot, entry);
137 }
138
139 /*
140 * Lower priority numbers take precedence.
141 * If a better entry is already present, ignore the new one.
142 */
143
144 else if (entry->prio < prio)
145 return;
146
147 /*
148 * If the existing entry is worse, clear it.
149 * In addition, a tag with priority TAG_FALLBACK
150 * is only used if the tag occurs exactly once.
151 */
152
153 else if (entry->prio > prio || prio == TAG_FALLBACK) {
154 while (entry->nnodes > 0)
155 entry->nodes[--entry->nnodes]->flags &= ~NODE_ID;
156
157 if (prio == TAG_FALLBACK) {
158 entry->prio = TAG_DELETE;
159 return;
160 }
161 }
162
163 /* Remember the new node. */
164
165 if (entry->maxnodes == entry->nnodes) {
166 entry->maxnodes += 4;
167 entry->nodes = mandoc_reallocarray(entry->nodes,
168 entry->maxnodes, sizeof(*entry->nodes));
169 }
170 entry->nodes[entry->nnodes++] = n;
171 entry->prio = prio;
172 n->flags |= NODE_ID;
173 if (n->child == NULL || n->child->string != s || *se != '\0') {
174 assert(n->string == NULL);
175 n->string = mandoc_strndup(s, len);
176 }
177 }
178
179 int
180 tag_exists(const char *tag)
181 {
182 return ohash_find(&tag_data, ohash_qlookup(&tag_data, tag)) != NULL;
183 }