Annotation of mandoc/tag.c, Revision 1.35
1.35 ! schwarze 1: /* $Id: tag.c,v 1.34 2020/04/08 11:56:04 schwarze Exp $ */
1.1 schwarze 2: /*
1.26 schwarze 3: * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 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.
1.28 schwarze 16: *
17: * Functions to tag syntax tree nodes.
18: * For internal use by mandoc(1) validation modules only.
1.1 schwarze 19: */
1.7 schwarze 20: #include "config.h"
21:
1.1 schwarze 22: #include <sys/types.h>
23:
1.27 schwarze 24: #include <assert.h>
1.20 schwarze 25: #include <limits.h>
1.1 schwarze 26: #include <stddef.h>
1.32 schwarze 27: #include <stdint.h>
1.1 schwarze 28: #include <stdlib.h>
29: #include <string.h>
30:
31: #include "mandoc_aux.h"
1.10 schwarze 32: #include "mandoc_ohash.h"
1.28 schwarze 33: #include "roff.h"
1.33 schwarze 34: #include "mdoc.h"
1.35 ! schwarze 35: #include "roff_int.h"
1.1 schwarze 36: #include "tag.h"
37:
38: struct tag_entry {
1.28 schwarze 39: struct roff_node **nodes;
40: size_t maxnodes;
41: size_t nnodes;
1.4 schwarze 42: int prio;
1.1 schwarze 43: char s[];
44: };
45:
1.33 schwarze 46: static void tag_move_id(struct roff_node *);
47:
1.1 schwarze 48: static struct ohash tag_data;
49:
50:
51: /*
1.28 schwarze 52: * Set up the ohash table to collect nodes
53: * where various marked-up terms are documented.
1.1 schwarze 54: */
1.28 schwarze 55: void
56: tag_alloc(void)
1.1 schwarze 57: {
1.28 schwarze 58: mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
59: }
1.1 schwarze 60:
1.28 schwarze 61: void
62: tag_free(void)
63: {
64: struct tag_entry *entry;
65: unsigned int slot;
1.12 schwarze 66:
1.29 schwarze 67: if (tag_data.info.free == NULL)
68: return;
1.28 schwarze 69: entry = ohash_first(&tag_data, &slot);
70: while (entry != NULL) {
71: free(entry->nodes);
72: free(entry);
73: entry = ohash_next(&tag_data, &slot);
1.22 schwarze 74: }
1.28 schwarze 75: ohash_delete(&tag_data);
1.29 schwarze 76: tag_data.info.free = NULL;
1.1 schwarze 77: }
78:
79: /*
1.28 schwarze 80: * Set a node where a term is defined,
1.20 schwarze 81: * unless it is already defined at a lower priority.
1.1 schwarze 82: */
83: void
1.28 schwarze 84: tag_put(const char *s, int prio, struct roff_node *n)
1.1 schwarze 85: {
86: struct tag_entry *entry;
1.34 schwarze 87: struct roff_node *nold;
1.20 schwarze 88: const char *se;
1.5 schwarze 89: size_t len;
1.1 schwarze 90: unsigned int slot;
91:
1.27 schwarze 92: assert(prio <= TAG_FALLBACK);
1.20 schwarze 93:
1.28 schwarze 94: if (s == NULL) {
95: if (n->child == NULL || n->child->type != ROFFT_TEXT)
96: return;
97: s = n->child->string;
1.30 schwarze 98: switch (s[0]) {
99: case '-':
100: s++;
101: break;
102: case '\\':
103: switch (s[1]) {
104: case '&':
105: case '-':
106: case 'e':
107: s += 2;
108: break;
109: default:
110: break;
111: }
112: break;
113: default:
114: break;
115: }
1.28 schwarze 116: }
1.20 schwarze 117:
118: /*
1.24 schwarze 119: * Skip whitespace and escapes and whatever follows,
1.20 schwarze 120: * and if there is any, downgrade the priority.
121: */
122:
1.24 schwarze 123: len = strcspn(s, " \t\\");
1.20 schwarze 124: if (len == 0)
1.1 schwarze 125: return;
1.14 schwarze 126:
1.20 schwarze 127: se = s + len;
1.27 schwarze 128: if (*se != '\0' && prio < TAG_WEAK)
129: prio = TAG_WEAK;
1.20 schwarze 130:
131: slot = ohash_qlookupi(&tag_data, s, &se);
1.1 schwarze 132: entry = ohash_find(&tag_data, slot);
1.14 schwarze 133:
1.28 schwarze 134: /* Build a new entry. */
135:
1.1 schwarze 136: if (entry == NULL) {
1.20 schwarze 137: entry = mandoc_malloc(sizeof(*entry) + len + 1);
1.1 schwarze 138: memcpy(entry->s, s, len);
1.20 schwarze 139: entry->s[len] = '\0';
1.28 schwarze 140: entry->nodes = NULL;
141: entry->maxnodes = entry->nnodes = 0;
1.1 schwarze 142: ohash_insert(&tag_data, slot, entry);
1.28 schwarze 143: }
1.14 schwarze 144:
1.28 schwarze 145: /*
146: * Lower priority numbers take precedence.
147: * If a better entry is already present, ignore the new one.
148: */
149:
150: else if (entry->prio < prio)
151: return;
152:
153: /*
154: * If the existing entry is worse, clear it.
155: * In addition, a tag with priority TAG_FALLBACK
156: * is only used if the tag occurs exactly once.
157: */
1.14 schwarze 158:
1.28 schwarze 159: else if (entry->prio > prio || prio == TAG_FALLBACK) {
1.34 schwarze 160: while (entry->nnodes > 0) {
161: nold = entry->nodes[--entry->nnodes];
162: nold->flags &= ~NODE_ID;
163: free(nold->tag);
164: nold->tag = NULL;
165: }
1.27 schwarze 166: if (prio == TAG_FALLBACK) {
1.28 schwarze 167: entry->prio = TAG_DELETE;
1.16 schwarze 168: return;
169: }
1.14 schwarze 170: }
171:
1.28 schwarze 172: /* Remember the new node. */
1.14 schwarze 173:
1.28 schwarze 174: if (entry->maxnodes == entry->nnodes) {
175: entry->maxnodes += 4;
176: entry->nodes = mandoc_reallocarray(entry->nodes,
177: entry->maxnodes, sizeof(*entry->nodes));
1.14 schwarze 178: }
1.28 schwarze 179: entry->nodes[entry->nnodes++] = n;
1.4 schwarze 180: entry->prio = prio;
1.28 schwarze 181: n->flags |= NODE_ID;
182: if (n->child == NULL || n->child->string != s || *se != '\0') {
1.34 schwarze 183: assert(n->tag == NULL);
184: n->tag = mandoc_strndup(s, len);
1.28 schwarze 185: }
1.1 schwarze 186: }
187:
1.31 schwarze 188: int
189: tag_exists(const char *tag)
1.1 schwarze 190: {
1.31 schwarze 191: return ohash_find(&tag_data, ohash_qlookup(&tag_data, tag)) != NULL;
1.33 schwarze 192: }
193:
194: /*
195: * For in-line elements, move the link target
196: * to the enclosing paragraph when appropriate.
197: */
198: static void
199: tag_move_id(struct roff_node *n)
200: {
201: struct roff_node *np;
202:
203: np = n;
204: for (;;) {
205: if (np->prev != NULL)
206: np = np->prev;
207: else if ((np = np->parent) == NULL)
208: return;
209: switch (np->tok) {
210: case MDOC_It:
211: switch (np->parent->parent->norm->Bl.type) {
212: case LIST_column:
213: /* Target the ROFFT_BLOCK = <tr>. */
214: np = np->parent;
215: break;
216: case LIST_diag:
217: case LIST_hang:
218: case LIST_inset:
219: case LIST_ohang:
220: case LIST_tag:
221: /* Target the ROFFT_HEAD = <dt>. */
222: np = np->parent->head;
223: break;
224: default:
225: /* Target the ROFF_BODY = <li>. */
226: break;
227: }
228: /* FALLTHROUGH */
229: case MDOC_Pp: /* Target the ROFFT_ELEM = <p>. */
1.34 schwarze 230: if (np->tag == NULL) {
231: np->tag = mandoc_strdup(n->tag == NULL ?
232: n->child->string : n->tag);
1.33 schwarze 233: np->flags |= NODE_ID;
234: n->flags &= ~NODE_ID;
235: }
236: return;
237: case MDOC_Sh:
238: case MDOC_Ss:
239: case MDOC_Bd:
240: case MDOC_Bl:
241: case MDOC_D1:
242: case MDOC_Dl:
243: case MDOC_Rs:
244: /* Do not move past major blocks. */
245: return;
246: default:
247: /*
248: * Move past in-line content and partial
249: * blocks, for example .It Xo or .It Bq Er.
250: */
251: break;
252: }
253: }
254: }
255:
256: /*
257: * When all tags have been set, decide where to put
258: * the associated permalinks, and maybe move some tags
259: * to the beginning of the respective paragraphs.
260: */
261: void
1.35 ! schwarze 262: tag_postprocess(struct roff_man *man, struct roff_node *n)
1.33 schwarze 263: {
1.35 ! schwarze 264: struct roff_node *nn;
! 265: char *cp;
! 266:
1.33 schwarze 267: if (n->flags & NODE_ID) {
268: switch (n->tok) {
1.35 ! schwarze 269: case MDOC_Pp:
! 270: nn = n->next;
! 271: if (nn == NULL || nn->type != ROFFT_TEXT ||
! 272: *nn->string == '\0' || *nn->string == ' ')
! 273: break;
! 274: /* Use the first few letters for the permalink. */
! 275: cp = nn->string;
! 276: while (cp != NULL && cp - nn->string < 5)
! 277: cp = strchr(cp + 1, ' ');
! 278: if (cp != NULL && cp[1] != '\0') {
! 279: /* Split a longer text node. */
! 280: man->last = nn;
! 281: man->next = ROFF_NEXT_SIBLING;
! 282: roff_word_alloc(man, nn->line,
! 283: nn->pos + (cp - nn->string), cp + 1);
! 284: man->last->flags = nn->flags;
! 285: *cp = '\0';
! 286: }
! 287: assert(nn->tag == NULL);
! 288: nn->tag = mandoc_strdup(n->tag);
! 289: nn->flags |= NODE_HREF;
! 290: break;
1.33 schwarze 291: case MDOC_Bd:
292: case MDOC_Bl:
293: /* XXX No permalink for now. */
294: break;
295: default:
296: if (n->type == ROFFT_ELEM || n->tok == MDOC_Fo)
297: tag_move_id(n);
298: if (n->tok != MDOC_Tg)
299: n->flags |= NODE_HREF;
1.34 schwarze 300: else if ((n->flags & NODE_ID) == 0) {
1.33 schwarze 301: n->flags |= NODE_NOPRT;
1.34 schwarze 302: free(n->tag);
303: n->tag = NULL;
304: }
1.33 schwarze 305: break;
306: }
307: }
308: for (n = n->child; n != NULL; n = n->next)
1.35 ! schwarze 309: tag_postprocess(man, n);
1.1 schwarze 310: }
CVSweb