1 /* $Id: mandoc_xr.c,v 1.3 2017/07/02 21:18:29 schwarze Exp $ */
3 * Copyright (c) 2017 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 #include <sys/types.h>
25 #include "mandoc_aux.h"
26 #include "mandoc_ohash.h"
27 #include "mandoc_xr.h"
29 static struct ohash *xr_hash = NULL;
30 static struct mandoc_xr *xr_first = NULL;
31 static struct mandoc_xr *xr_last = NULL;
33 static void mandoc_xr_clear(void);
44 for (xr = ohash_first(xr_hash, &slot); xr != NULL;
45 xr = ohash_next(xr_hash, &slot))
47 ohash_delete(xr_hash);
54 xr_hash = mandoc_malloc(sizeof(*xr_hash));
57 mandoc_ohash_init(xr_hash, 5,
58 offsetof(struct mandoc_xr, hashkey));
59 xr_first = xr_last = NULL;
63 mandoc_xr_add(const char *sec, const char *name, int line, int pos)
65 struct mandoc_xr *xr, *oxr;
75 ssz = strlen(sec) + 1;
76 nsz = strlen(name) + 1;
78 xr = mandoc_malloc(sizeof(*xr) + tsz);
80 xr->sec = xr->hashkey;
81 xr->name = xr->hashkey + ssz;
85 memcpy(xr->sec, sec, ssz);
86 memcpy(xr->name, name, nsz);
88 pend = xr->hashkey + tsz;
89 hv = ohash_interval(xr->hashkey, &pend);
90 slot = ohash_lookup_memory(xr_hash, xr->hashkey, tsz, hv);
91 if ((oxr = ohash_find(xr_hash, slot)) == NULL) {
92 ohash_insert(xr_hash, slot, xr);
102 ret = (oxr->line == -1) ^ (xr->line == -1);