Debian Codegraph @

Docs/src/bin/halibut/biblio.c

raw
1 /*
2 * biblio.c: process the bibliography
3 */
4
5 #include <assert.h>
6 #include "halibut.h"
7
8 static wchar_t *gentext(int num)
9 {
10 wchar_t text[22];
11 wchar_t *p = text + sizeof(text);
12 *--p = L'\0';
13 *--p = L']';
14 while (num != 0)
15 {
16 assert(p > text);
17 *--p = L"0123456789"[num % 10];
18 num /= 10;
19 }
20 assert(p > text);
21 *--p = L'[';
22 return ustrdup(p);
23 }
24
25 static void cite_biblio(keywordlist * kl, wchar_t * key, filepos fpos)
26 {
27 keyword *kw = kw_lookup(kl, key);
28 if (!kw)
29 error(err_nosuchkw, &fpos, key);
30 else
31 {
32 /*
33 * We've found a \k reference. If it's a
34 * bibliography entry ...
35 */
36 if (kw->para->type == para_Biblio)
37 {
38 /*
39 * ... then mark the paragraph as cited.
40 */
41 kw->para->type = para_BiblioCited;
42 }
43 }
44 }
45
46 /*
47 * Make a pass through the source form, generating citation formats
48 * for bibliography entries and also marking which bibliography
49 * entries are actually cited (or \nocite-ed).
50 */
51
52 void gen_citations(paragraph * source, keywordlist * kl)
53 {
54 paragraph *para;
55 int bibnum = 0;
56
57 for (para = source; para; para = para->next)
58 {
59 word *ptr;
60
61 /*
62 * \BR and \nocite paragraphs get special processing here.
63 */
64 if (para->type == para_BR)
65 {
66 keyword *kw = kw_lookup(kl, para->keyword);
67 if (!kw)
68 {
69 error(err_nosuchkw, &para->fpos, para->keyword);
70 } else if (kw->text)
71 {
72 error(err_multiBR, &para->fpos, para->keyword);
73 } else
74 {
75 kw->text = dup_word_list(para->words);
76 }
77 } else if (para->type == para_NoCite)
78 {
79 wchar_t *wp = para->keyword;
80 while (*wp)
81 {
82 cite_biblio(kl, wp, para->fpos);
83 wp = uadv(wp);
84 }
85 }
86
87 /*
88 * Scan for keyword references.
89 */
90 for (ptr = para->words; ptr; ptr = ptr->next)
91 {
92 if (ptr->type == word_UpperXref || ptr->type == word_LowerXref
93 || ptr->type == word_FreeTextXref)
94 cite_biblio(kl, ptr->text, ptr->fpos);
95 }
96 }
97
98 /*
99 * We're now almost done; all that remains is to scan through
100 * the cited bibliography entries and invent default citation
101 * texts for the ones that don't already have explicitly
102 * provided \BR text.
103 */
104 for (para = source; para; para = para->next)
105 {
106 if (para->type == para_BiblioCited)
107 {
108 keyword *kw = kw_lookup(kl, para->keyword);
109 assert(kw != NULL);
110 if (!kw->text)
111 {
112 word *wd = smalloc(sizeof(word));
113 wd->text = gentext(++bibnum);
114 wd->type = word_Normal;
115 wd->breaks = FALSE;
116 wd->alt = NULL;
117 wd->next = NULL;
118 wd->aux = 0;
119 kw->text = wd;
120 }
121 para->kwtext = kw->text;
122 }
123 }
124 }