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