]>
git.cameronkatri.com Git - mandoc.git/blob - tag.c
6595c9d09163cfdc3b165d7e4ceea3697b836e7b
1 /* $Id: tag.c,v 1.31 2020/04/02 22:12:55 schwarze Exp $ */
3 * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 * Functions to tag syntax tree nodes.
18 * For internal use by mandoc(1) validation modules only.
22 #include <sys/types.h>
30 #include "mandoc_aux.h"
31 #include "mandoc_ohash.h"
36 struct roff_node
**nodes
;
43 static struct ohash tag_data
;
47 * Set up the ohash table to collect nodes
48 * where various marked-up terms are documented.
53 mandoc_ohash_init(&tag_data
, 4, offsetof(struct tag_entry
, s
));
59 struct tag_entry
*entry
;
62 if (tag_data
.info
.free
== NULL
)
64 entry
= ohash_first(&tag_data
, &slot
);
65 while (entry
!= NULL
) {
68 entry
= ohash_next(&tag_data
, &slot
);
70 ohash_delete(&tag_data
);
71 tag_data
.info
.free
= NULL
;
75 * Set a node where a term is defined,
76 * unless it is already defined at a lower priority.
79 tag_put(const char *s
, int prio
, struct roff_node
*n
)
81 struct tag_entry
*entry
;
86 assert(prio
<= TAG_FALLBACK
);
89 if (n
->child
== NULL
|| n
->child
->type
!= ROFFT_TEXT
)
113 * Skip whitespace and escapes and whatever follows,
114 * and if there is any, downgrade the priority.
117 len
= strcspn(s
, " \t\\");
122 if (*se
!= '\0' && prio
< TAG_WEAK
)
125 slot
= ohash_qlookupi(&tag_data
, s
, &se
);
126 entry
= ohash_find(&tag_data
, slot
);
128 /* Build a new entry. */
131 entry
= mandoc_malloc(sizeof(*entry
) + len
+ 1);
132 memcpy(entry
->s
, s
, len
);
133 entry
->s
[len
] = '\0';
135 entry
->maxnodes
= entry
->nnodes
= 0;
136 ohash_insert(&tag_data
, slot
, entry
);
140 * Lower priority numbers take precedence.
141 * If a better entry is already present, ignore the new one.
144 else if (entry
->prio
< prio
)
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.
153 else if (entry
->prio
> prio
|| prio
== TAG_FALLBACK
) {
154 while (entry
->nnodes
> 0)
155 entry
->nodes
[--entry
->nnodes
]->flags
&= ~NODE_ID
;
157 if (prio
== TAG_FALLBACK
) {
158 entry
->prio
= TAG_DELETE
;
163 /* Remember the new node. */
165 if (entry
->maxnodes
== entry
->nnodes
) {
166 entry
->maxnodes
+= 4;
167 entry
->nodes
= mandoc_reallocarray(entry
->nodes
,
168 entry
->maxnodes
, sizeof(*entry
->nodes
));
170 entry
->nodes
[entry
->nnodes
++] = n
;
173 if (n
->child
== NULL
|| n
->child
->string
!= s
|| *se
!= '\0') {
174 assert(n
->string
== NULL
);
175 n
->string
= mandoc_strndup(s
, len
);
180 tag_exists(const char *tag
)
182 return ohash_find(&tag_data
, ohash_qlookup(&tag_data
, tag
)) != NULL
;