1/* $Id: mandoc_xr.c,v 1.4 2020/06/22 19:20:40 schwarze Exp $ */
2/*
3 * Copyright (c) 2017 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#include "config.h"
18
19#include <sys/types.h>
20
21#include <assert.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "mandoc_aux.h"
28#include "mandoc_ohash.h"
29#include "mandoc_xr.h"
30
31static struct ohash	 *xr_hash = NULL;
32static struct mandoc_xr	 *xr_first = NULL;
33static struct mandoc_xr	 *xr_last = NULL;
34
35static void		  mandoc_xr_clear(void);
36
37
38static void
39mandoc_xr_clear(void)
40{
41	struct mandoc_xr	*xr;
42	unsigned int		 slot;
43
44	if (xr_hash == NULL)
45		return;
46	for (xr = ohash_first(xr_hash, &slot); xr != NULL;
47	     xr = ohash_next(xr_hash, &slot))
48		free(xr);
49	ohash_delete(xr_hash);
50}
51
52void
53mandoc_xr_reset(void)
54{
55	if (xr_hash == NULL)
56		xr_hash = mandoc_malloc(sizeof(*xr_hash));
57	else
58		mandoc_xr_clear();
59	mandoc_ohash_init(xr_hash, 5,
60	    offsetof(struct mandoc_xr, hashkey));
61	xr_first = xr_last = NULL;
62}
63
64int
65mandoc_xr_add(const char *sec, const char *name, int line, int pos)
66{
67	struct mandoc_xr	 *xr, *oxr;
68	const char		 *pend;
69	size_t			  ssz, nsz, tsz;
70	unsigned int		  slot;
71	int			  ret;
72	uint32_t		  hv;
73
74	if (xr_hash == NULL)
75		return 0;
76
77	ssz = strlen(sec) + 1;
78	nsz = strlen(name) + 1;
79	tsz = ssz + nsz;
80	xr = mandoc_malloc(sizeof(*xr) + tsz);
81	xr->next = NULL;
82	xr->sec = xr->hashkey;
83	xr->name = xr->hashkey + ssz;
84	xr->line = line;
85	xr->pos = pos;
86	xr->count = 1;
87	memcpy(xr->sec, sec, ssz);
88	memcpy(xr->name, name, nsz);
89
90	pend = xr->hashkey + tsz;
91	hv = ohash_interval(xr->hashkey, &pend);
92	slot = ohash_lookup_memory(xr_hash, xr->hashkey, tsz, hv);
93	if ((oxr = ohash_find(xr_hash, slot)) == NULL) {
94		ohash_insert(xr_hash, slot, xr);
95		if (xr_first == NULL)
96			xr_first = xr;
97		else
98			xr_last->next = xr;
99		xr_last = xr;
100		return 0;
101	}
102
103	oxr->count++;
104	ret = (oxr->line == -1) ^ (xr->line == -1);
105	if (xr->line == -1)
106		oxr->line = -1;
107	free(xr);
108	return ret;
109}
110
111struct mandoc_xr *
112mandoc_xr_get(void)
113{
114	return xr_first;
115}
116
117void
118mandoc_xr_free(void)
119{
120	mandoc_xr_clear();
121	free(xr_hash);
122	xr_hash = NULL;
123}
124