xref: /netbsd-src/external/bsd/mdocml/dist/mandoc_xr.c (revision c9bcef0391a5afc0280459d5c16c26ace267c496)
1*c9bcef03Schristos /*	Id: mandoc_xr.c,v 1.3 2017/07/02 21:18:29 schwarze Exp  */
2*c9bcef03Schristos /*
3*c9bcef03Schristos  * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
4*c9bcef03Schristos  *
5*c9bcef03Schristos  * Permission to use, copy, modify, and distribute this software for any
6*c9bcef03Schristos  * purpose with or without fee is hereby granted, provided that the above
7*c9bcef03Schristos  * copyright notice and this permission notice appear in all copies.
8*c9bcef03Schristos  *
9*c9bcef03Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*c9bcef03Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*c9bcef03Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*c9bcef03Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*c9bcef03Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*c9bcef03Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*c9bcef03Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*c9bcef03Schristos  */
17*c9bcef03Schristos #include <sys/types.h>
18*c9bcef03Schristos 
19*c9bcef03Schristos #include <assert.h>
20*c9bcef03Schristos #include <stddef.h>
21*c9bcef03Schristos #include <stdint.h>
22*c9bcef03Schristos #include <stdlib.h>
23*c9bcef03Schristos #include <string.h>
24*c9bcef03Schristos 
25*c9bcef03Schristos #include "mandoc_aux.h"
26*c9bcef03Schristos #include "mandoc_ohash.h"
27*c9bcef03Schristos #include "mandoc_xr.h"
28*c9bcef03Schristos 
29*c9bcef03Schristos static struct ohash	 *xr_hash = NULL;
30*c9bcef03Schristos static struct mandoc_xr	 *xr_first = NULL;
31*c9bcef03Schristos static struct mandoc_xr	 *xr_last = NULL;
32*c9bcef03Schristos 
33*c9bcef03Schristos static void		  mandoc_xr_clear(void);
34*c9bcef03Schristos 
35*c9bcef03Schristos 
36*c9bcef03Schristos static void
mandoc_xr_clear(void)37*c9bcef03Schristos mandoc_xr_clear(void)
38*c9bcef03Schristos {
39*c9bcef03Schristos 	struct mandoc_xr	*xr;
40*c9bcef03Schristos 	unsigned int		 slot;
41*c9bcef03Schristos 
42*c9bcef03Schristos 	if (xr_hash == NULL)
43*c9bcef03Schristos 		return;
44*c9bcef03Schristos 	for (xr = ohash_first(xr_hash, &slot); xr != NULL;
45*c9bcef03Schristos 	     xr = ohash_next(xr_hash, &slot))
46*c9bcef03Schristos 		free(xr);
47*c9bcef03Schristos 	ohash_delete(xr_hash);
48*c9bcef03Schristos }
49*c9bcef03Schristos 
50*c9bcef03Schristos void
mandoc_xr_reset(void)51*c9bcef03Schristos mandoc_xr_reset(void)
52*c9bcef03Schristos {
53*c9bcef03Schristos 	if (xr_hash == NULL)
54*c9bcef03Schristos 		xr_hash = mandoc_malloc(sizeof(*xr_hash));
55*c9bcef03Schristos 	else
56*c9bcef03Schristos 		mandoc_xr_clear();
57*c9bcef03Schristos 	mandoc_ohash_init(xr_hash, 5,
58*c9bcef03Schristos 	    offsetof(struct mandoc_xr, hashkey));
59*c9bcef03Schristos 	xr_first = xr_last = NULL;
60*c9bcef03Schristos }
61*c9bcef03Schristos 
62*c9bcef03Schristos int
mandoc_xr_add(const char * sec,const char * name,int line,int pos)63*c9bcef03Schristos mandoc_xr_add(const char *sec, const char *name, int line, int pos)
64*c9bcef03Schristos {
65*c9bcef03Schristos 	struct mandoc_xr	 *xr, *oxr;
66*c9bcef03Schristos 	const char		 *pend;
67*c9bcef03Schristos 	size_t			  ssz, nsz, tsz;
68*c9bcef03Schristos 	unsigned int		  slot;
69*c9bcef03Schristos 	int			  ret;
70*c9bcef03Schristos 	uint32_t		  hv;
71*c9bcef03Schristos 
72*c9bcef03Schristos 	if (xr_hash == NULL)
73*c9bcef03Schristos 		return 0;
74*c9bcef03Schristos 
75*c9bcef03Schristos 	ssz = strlen(sec) + 1;
76*c9bcef03Schristos 	nsz = strlen(name) + 1;
77*c9bcef03Schristos 	tsz = ssz + nsz;
78*c9bcef03Schristos 	xr = mandoc_malloc(sizeof(*xr) + tsz);
79*c9bcef03Schristos 	xr->next = NULL;
80*c9bcef03Schristos 	xr->sec = xr->hashkey;
81*c9bcef03Schristos 	xr->name = xr->hashkey + ssz;
82*c9bcef03Schristos 	xr->line = line;
83*c9bcef03Schristos 	xr->pos = pos;
84*c9bcef03Schristos 	xr->count = 1;
85*c9bcef03Schristos 	memcpy(xr->sec, sec, ssz);
86*c9bcef03Schristos 	memcpy(xr->name, name, nsz);
87*c9bcef03Schristos 
88*c9bcef03Schristos 	pend = xr->hashkey + tsz;
89*c9bcef03Schristos 	hv = ohash_interval(xr->hashkey, &pend);
90*c9bcef03Schristos 	slot = ohash_lookup_memory(xr_hash, xr->hashkey, tsz, hv);
91*c9bcef03Schristos 	if ((oxr = ohash_find(xr_hash, slot)) == NULL) {
92*c9bcef03Schristos 		ohash_insert(xr_hash, slot, xr);
93*c9bcef03Schristos 		if (xr_first == NULL)
94*c9bcef03Schristos 			xr_first = xr;
95*c9bcef03Schristos 		else
96*c9bcef03Schristos 			xr_last->next = xr;
97*c9bcef03Schristos 		xr_last = xr;
98*c9bcef03Schristos 		return 0;
99*c9bcef03Schristos 	}
100*c9bcef03Schristos 
101*c9bcef03Schristos 	oxr->count++;
102*c9bcef03Schristos 	ret = (oxr->line == -1) ^ (xr->line == -1);
103*c9bcef03Schristos 	if (xr->line == -1)
104*c9bcef03Schristos 		oxr->line = -1;
105*c9bcef03Schristos 	free(xr);
106*c9bcef03Schristos 	return ret;
107*c9bcef03Schristos }
108*c9bcef03Schristos 
109*c9bcef03Schristos struct mandoc_xr *
mandoc_xr_get(void)110*c9bcef03Schristos mandoc_xr_get(void)
111*c9bcef03Schristos {
112*c9bcef03Schristos 	return xr_first;
113*c9bcef03Schristos }
114*c9bcef03Schristos 
115*c9bcef03Schristos void
mandoc_xr_free(void)116*c9bcef03Schristos mandoc_xr_free(void)
117*c9bcef03Schristos {
118*c9bcef03Schristos 	mandoc_xr_clear();
119*c9bcef03Schristos 	free(xr_hash);
120*c9bcef03Schristos 	xr_hash = NULL;
121*c9bcef03Schristos }
122