Debian Codegraph @

Docs/src/bin/halibut/keywords.c

raw
1 /*
2 * keywords.c: keep track of all cross-reference keywords
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include "halibut.h"
9
10 static int kwcmp(void *av, void *bv)
11 {
12 const keyword *a = (const keyword *) av;
13 const keyword *b = (const keyword *) bv;
14 return ustrcmp(a->key, b->key);
15 }
16
17 static int kwfind(void *av, void *bv)
18 {
19 wchar_t *a = (wchar_t *) av;
20 const keyword *b = (const keyword *) bv;
21 return ustrcmp(a, b->key);
22 }
23
24 keyword *kw_lookup(keywordlist * kl, wchar_t * str)
25 {
26 return find234(kl->keys, str, kwfind);
27 }
28
29 /*
30 * This function reads through source form and collects the
31 * keywords. They get collected in a heap, sorted by Unicode
32 * collation, last at the top (so that we can Heapsort them when we
33 * finish).
34 */
35 keywordlist *get_keywords(paragraph * source)
36 {
37 int errors = FALSE;
38 keywordlist *kl = mknew(keywordlist);
39 numberstate *n = number_init();
40 int prevpara = para_NotParaType;
41
42 number_cfg(n, source);
43
44 kl->size = 0;
45 kl->keys = newtree234(kwcmp);
46 kl->nlooseends = kl->looseendssize = 0;
47 kl->looseends = NULL;
48 for (; source; source = source->next)
49 {
50 wchar_t *p, *q;
51 p = q = source->keyword;
52
53 /*
54 * Look for the section type override (`example',
55 * `question' or whatever - to replace `chapter' or
56 * `section' on a per-section basis).
57 */
58 if (q)
59 {
60 q = uadv(q); /* point q at the word beyond */
61 if (!*q)
62 q = NULL;
63 }
64
65 /*
66 * Number the chapter / section / list-item / whatever.
67 * This also sets up the `parent', `child' and `sibling'
68 * links.
69 */
70 source->kwtext = number_mktext(n, source, q, prevpara, &errors);
71 prevpara = source->type;
72
73 if (p && *p)
74 {
75 if (source->kwtext || source->type == para_Biblio)
76 {
77 keyword *kw, *ret;
78
79 kw = mknew(keyword);
80 kw->key = p;
81 kw->text = source->kwtext;
82 kw->para = source;
83 ret = add234(kl->keys, kw);
84 if (ret != kw)
85 {
86 error(err_multikw, &source->fpos, &ret->para->fpos, p);
87 sfree(kw);
88 /* FIXME: what happens to kw->text? Does it leak? */
89 }
90 }
91 } else
92 {
93 if (kl->nlooseends >= kl->looseendssize)
94 {
95 kl->looseendssize = kl->nlooseends + 32;
96 kl->looseends = resize(kl->looseends, kl->looseendssize);
97 }
98 kl->looseends[kl->nlooseends++] = source->kwtext;
99 }
100 }
101
102 number_free(n);
103
104 if (errors)
105 {
106 free_keywords(kl);
107 return NULL;
108 }
109
110 return kl;
111 }
112
113 void free_keywords(keywordlist * kl)
114 {
115 keyword *kw;
116 while (kl->nlooseends)
117 free_word_list(kl->looseends[--kl->nlooseends]);
118 sfree(kl->looseends);
119 while ((kw = index234(kl->keys, 0)) != NULL)
120 {
121 delpos234(kl->keys, 0);
122 free_word_list(kw->text);
123 sfree(kw);
124 }
125 freetree234(kl->keys);
126 sfree(kl);
127 }
128
129 void subst_keywords(paragraph * source, keywordlist * kl)
130 {
131 for (; source; source = source->next)
132 {
133 word *ptr;
134 for (ptr = source->words; ptr; ptr = ptr->next)
135 {
136 if (ptr->type == word_UpperXref || ptr->type == word_LowerXref)
137 {
138 keyword *kw;
139 word **endptr, *close, *subst;
140
141 kw = kw_lookup(kl, ptr->text);
142 if (!kw)
143 {
144 error(err_nosuchkw, &ptr->fpos, ptr->text);
145 subst = NULL;
146 } else
147 subst = dup_word_list(kw->text);
148
149 if (subst && ptr->type == word_LowerXref &&
150 kw->para->type != para_Biblio &&
151 kw->para->type != para_BiblioCited)
152 ustrlow(subst->text);
153
154 close = mknew(word);
155 close->text = NULL;
156 close->alt = NULL;
157 close->type = word_XrefEnd;
158 close->fpos = ptr->fpos;
159 close->breaks = FALSE;
160 close->aux = 0;
161
162 close->next = ptr->next;
163 ptr->next = subst;
164
165 for (endptr = &ptr->next; *endptr; endptr = &(*endptr)->next)
166 (*endptr)->fpos = ptr->fpos;
167
168 *endptr = close;
169 ptr = close;
170 }
171 }
172 }
173 }