- if (tag_fd == -1)
- return;
- stream = fdopen(tag_fd, "w");
- entry = ohash_first(&tag_data, &slot);
- while (entry != NULL) {
- if (stream != NULL)
- fprintf(stream, "%s - %zu\n", entry->s, entry->line);
- free(entry);
- entry = ohash_next(&tag_data, &slot);
+ else if (entry->prio < prio)
+ return;
+
+ /*
+ * If the existing entry is worse, clear it.
+ * In addition, a tag with priority TAG_FALLBACK
+ * is only used if the tag occurs exactly once.
+ */
+
+ else if (entry->prio > prio || prio == TAG_FALLBACK) {
+ while (entry->nnodes > 0) {
+ nold = entry->nodes[--entry->nnodes];
+ nold->flags &= ~NODE_ID;
+ free(nold->tag);
+ nold->tag = NULL;
+ }
+ if (prio == TAG_FALLBACK) {
+ entry->prio = TAG_DELETE;
+ return;
+ }
+ }
+
+ /* Remember the new node. */
+
+ if (entry->maxnodes == entry->nnodes) {
+ entry->maxnodes += 4;
+ entry->nodes = mandoc_reallocarray(entry->nodes,
+ entry->maxnodes, sizeof(*entry->nodes));
+ }
+ entry->nodes[entry->nnodes++] = n;
+ entry->prio = prio;
+ n->flags |= NODE_ID;
+ if (n->child == NULL || n->child->string != s || *se != '\0') {
+ assert(n->tag == NULL);
+ n->tag = mandoc_strndup(s, len);