5dd44dce |
1 | /* |
97b77c9a |
2 | * Info backend for Halibut |
3 | * |
4 | * The Info file format isn't well-specified, and what specification |
5 | * there is is scattered all over the place. Sources include: |
6 | * (info), from GNU Texinfo. |
7 | * (texinfo), also from GNU Texinfo. |
8 | * (Emacs)Misc Help, and (emacs)Info Lookup, from GNU Emacs. |
9 | * info.el, from GNU Emacs. |
5dd44dce |
10 | * |
b921687e |
11 | * Possible future work: |
5dd44dce |
12 | * |
b921687e |
13 | * - configurable choice of how to allocate node names? |
14 | * + possibly a template-like approach, choosing node names to |
15 | * be the full section title or perhaps the internal keyword? |
16 | * + neither of those seems quite right. Perhaps instead a |
17 | * Windows Help-like mechanism, where a magic config |
18 | * directive allows user choice of name for every node. |
19 | * + Only trouble with that is, now what happens to the section |
20 | * numbers? Do they become completely vestigial and just sit |
21 | * in the title text of each node? Or do we keep them in the |
22 | * menus somehow? I think people might occasionally want to |
23 | * go to a section by number, if only because all the _other_ |
24 | * formats of the same document will reference the numbers |
25 | * all the time. So our menu lines could look like one of |
26 | * these: |
27 | * * Nodename: Section 1.2. Title of section. |
28 | * * Section 1.2: Nodename. Title of section. |
5dd44dce |
29 | * |
b921687e |
30 | * - might be helpful to diagnose duplicate node names! |
c2d30a0d |
31 | * |
c2d30a0d |
32 | * - Indices generated by makeinfo use a menu rather than a bunch of |
33 | * cross-references, which reduces visual clutter rather. For |
34 | * singly-referenced items, it looks like: |
35 | * * toner cartridge, replacing: Toner. |
36 | * It does a horrid job on multiply-referenced entries, though, |
37 | * perhaps because the name before the colon is meant to be unique. |
22e33bba |
38 | * Info's 'i' command requires the use of a menu -- it fails to |
39 | * find any index entries at all with Halibut's current index format. |
c2d30a0d |
40 | * |
41 | * - The string "*note" is matched case-insensitively, so we could |
42 | * make things slightly less ugly by using the lower-case version |
43 | * when the user asks for \k. Unfortunately, standalone Info seems |
44 | * to match node names case-sensitively, so we can't downcase that. |
97b77c9a |
45 | * |
46 | * - The character encoding used in an Info file can be configured using |
47 | * an Emacs local variables block at the end, like this: |
48 | * Local Variables: |
49 | * coding: iso-8859-1 |
50 | * End: |
5dd44dce |
51 | */ |
52 | |
53 | #include <stdio.h> |
54 | #include <stdlib.h> |
55 | #include <assert.h> |
56 | #include "halibut.h" |
57 | |
58 | typedef struct { |
e32df3e7 |
59 | wchar_t *underline; |
60 | } alignstruct; |
61 | |
62 | typedef struct { |
5dd44dce |
63 | char *filename; |
64 | int maxfilesize; |
91f93b94 |
65 | int charset; |
5b1d0032 |
66 | int listindentbefore, listindentafter; |
67 | int indent_code, width, index_width; |
e32df3e7 |
68 | alignstruct atitle, achapter, *asect; |
69 | int nasect; |
5b1d0032 |
70 | wchar_t *bullet, *listsuffix; |
71 | wchar_t *startemph, *endemph; |
72 | wchar_t *lquote, *rquote; |
e32df3e7 |
73 | wchar_t *sectsuffix; |
5b1d0032 |
74 | wchar_t *rule; |
f6220253 |
75 | wchar_t *index_text; |
5dd44dce |
76 | } infoconfig; |
77 | |
91f93b94 |
78 | typedef struct { |
79 | rdstringc output; |
80 | int charset; |
81 | charset_state state; |
82 | int wcmode; |
83 | } info_data; |
84 | #define EMPTY_INFO_DATA { { 0, 0, NULL }, 0, CHARSET_INIT_STATE, FALSE } |
85 | static const info_data empty_info_data = EMPTY_INFO_DATA; |
86 | |
5dd44dce |
87 | typedef struct node_tag node; |
88 | struct node_tag { |
89 | node *listnext; |
90 | node *up, *prev, *next, *lastchild; |
91 | int pos, started_menu, filenum; |
92 | char *name; |
91f93b94 |
93 | info_data text; |
5dd44dce |
94 | }; |
95 | |
96 | typedef struct { |
97 | char *text; |
91f93b94 |
98 | int length; |
5dd44dce |
99 | int nnodes, nodesize; |
100 | node **nodes; |
101 | } info_idx; |
102 | |
91f93b94 |
103 | static int info_rdadd(info_data *, wchar_t); |
104 | static int info_rdadds(info_data *, wchar_t const *); |
105 | static int info_rdaddc(info_data *, char); |
106 | static int info_rdaddsc(info_data *, char const *); |
5dd44dce |
107 | |
e32df3e7 |
108 | static void info_heading(info_data *, word *, word *, alignstruct, int, |
109 | infoconfig *); |
5b1d0032 |
110 | static void info_rule(info_data *, int, int, infoconfig *); |
91f93b94 |
111 | static void info_para(info_data *, word *, wchar_t *, word *, keywordlist *, |
5b1d0032 |
112 | int, int, int, infoconfig *); |
91f93b94 |
113 | static void info_codepara(info_data *, word *, int, int); |
5b1d0032 |
114 | static void info_versionid(info_data *, word *, infoconfig *); |
115 | static void info_menu_item(info_data *, node *, paragraph *, infoconfig *); |
5dd44dce |
116 | static word *info_transform_wordlist(word *, keywordlist *); |
117 | static int info_check_index(word *, node *, indexdata *); |
118 | |
5b1d0032 |
119 | static int info_rdaddwc(info_data *, word *, word *, int, infoconfig *); |
5dd44dce |
120 | |
91f93b94 |
121 | static node *info_node_new(char *name, int charset); |
f6220253 |
122 | static char *info_node_name_for_para(paragraph *p, infoconfig *); |
123 | static char *info_node_name_for_text(wchar_t *text, infoconfig *); |
5dd44dce |
124 | |
125 | static infoconfig info_configure(paragraph *source) { |
126 | infoconfig ret; |
5b1d0032 |
127 | paragraph *p; |
e32df3e7 |
128 | int n; |
5dd44dce |
129 | |
130 | /* |
131 | * Defaults. |
132 | */ |
133 | ret.filename = dupstr("output.info"); |
134 | ret.maxfilesize = 64 << 10; |
91f93b94 |
135 | ret.charset = CS_ASCII; |
5b1d0032 |
136 | ret.width = 70; |
137 | ret.listindentbefore = 1; |
138 | ret.listindentafter = 3; |
139 | ret.indent_code = 2; |
140 | ret.index_width = 40; |
141 | ret.listsuffix = L"."; |
142 | ret.bullet = L"\x2022\0-\0\0"; |
143 | ret.rule = L"\x2500\0-\0\0"; |
144 | ret.startemph = L"_\0_\0\0"; |
145 | ret.endemph = uadv(ret.startemph); |
146 | ret.lquote = L"\x2018\0\x2019\0`\0'\0\0"; |
147 | ret.rquote = uadv(ret.lquote); |
148 | ret.sectsuffix = L": "; |
e32df3e7 |
149 | /* |
150 | * Default underline characters are chosen to match those recognised by |
151 | * Info-fontify-node. |
152 | */ |
153 | ret.atitle.underline = L"*\0\0"; |
154 | ret.achapter.underline = L"=\0\0"; |
155 | ret.nasect = 2; |
156 | ret.asect = snewn(ret.nasect, alignstruct); |
157 | ret.asect[0].underline = L"-\0\0"; |
158 | ret.asect[1].underline = L".\0\0"; |
f6220253 |
159 | ret.index_text = L"Index"; |
5b1d0032 |
160 | |
161 | /* |
162 | * Two-pass configuration so that we can pick up global config |
163 | * (e.g. `quotes') before having it overridden by specific |
164 | * config (`info-quotes'), irrespective of the order in which |
165 | * they occur. |
166 | */ |
167 | for (p = source; p; p = p->next) { |
168 | if (p->type == para_Config) { |
169 | if (!ustricmp(p->keyword, L"quotes")) { |
170 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
171 | ret.lquote = uadv(p->keyword); |
172 | ret.rquote = uadv(ret.lquote); |
173 | } |
f6220253 |
174 | } else if (!ustricmp(p->keyword, L"index")) { |
175 | ret.index_text = uadv(p->keyword); |
5b1d0032 |
176 | } |
177 | } |
178 | } |
5dd44dce |
179 | |
5b1d0032 |
180 | for (p = source; p; p = p->next) { |
181 | if (p->type == para_Config) { |
182 | if (!ustricmp(p->keyword, L"info-filename")) { |
5dd44dce |
183 | sfree(ret.filename); |
5b1d0032 |
184 | ret.filename = dupstr(adv(p->origkeyword)); |
185 | } else if (!ustricmp(p->keyword, L"info-charset")) { |
0960a3d8 |
186 | ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword)); |
5b1d0032 |
187 | } else if (!ustricmp(p->keyword, L"info-max-file-size")) { |
188 | ret.maxfilesize = utoi(uadv(p->keyword)); |
189 | } else if (!ustricmp(p->keyword, L"info-width")) { |
190 | ret.width = utoi(uadv(p->keyword)); |
191 | } else if (!ustricmp(p->keyword, L"info-indent-code")) { |
192 | ret.indent_code = utoi(uadv(p->keyword)); |
193 | } else if (!ustricmp(p->keyword, L"info-index-width")) { |
194 | ret.index_width = utoi(uadv(p->keyword)); |
195 | } else if (!ustricmp(p->keyword, L"info-list-indent")) { |
196 | ret.listindentbefore = utoi(uadv(p->keyword)); |
197 | } else if (!ustricmp(p->keyword, L"info-listitem-indent")) { |
198 | ret.listindentafter = utoi(uadv(p->keyword)); |
199 | } else if (!ustricmp(p->keyword, L"info-section-suffix")) { |
200 | ret.sectsuffix = uadv(p->keyword); |
201 | } else if (!ustricmp(p->keyword, L"info-underline")) { |
e32df3e7 |
202 | ret.atitle.underline = ret.achapter.underline = |
203 | uadv(p->keyword); |
204 | for (n = 0; n < ret.nasect; n++) |
205 | ret.asect[n].underline = ret.atitle.underline; |
206 | } else if (!ustricmp(p->keyword, L"info-chapter-underline")) { |
207 | ret.achapter.underline = uadv(p->keyword); |
208 | } else if (!ustricmp(p->keyword, L"info-section-underline")) { |
209 | wchar_t *q = uadv(p->keyword); |
210 | int n = 0; |
211 | if (uisdigit(*q)) { |
212 | n = utoi(q); |
213 | q = uadv(q); |
214 | } |
215 | if (n >= ret.nasect) { |
216 | int i; |
217 | ret.asect = sresize(ret.asect, n+1, alignstruct); |
218 | for (i = ret.nasect; i <= n; i++) |
219 | ret.asect[i] = ret.asect[ret.nasect-1]; |
220 | ret.nasect = n+1; |
221 | } |
222 | ret.asect[n].underline = q; |
223 | } else if (!ustricmp(p->keyword, L"text-title-underline")) { |
224 | ret.atitle.underline = uadv(p->keyword); |
5b1d0032 |
225 | } else if (!ustricmp(p->keyword, L"info-bullet")) { |
226 | ret.bullet = uadv(p->keyword); |
227 | } else if (!ustricmp(p->keyword, L"info-rule")) { |
228 | ret.rule = uadv(p->keyword); |
229 | } else if (!ustricmp(p->keyword, L"info-list-suffix")) { |
230 | ret.listsuffix = uadv(p->keyword); |
231 | } else if (!ustricmp(p->keyword, L"info-emphasis")) { |
232 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
233 | ret.startemph = uadv(p->keyword); |
234 | ret.endemph = uadv(ret.startemph); |
235 | } |
236 | } else if (!ustricmp(p->keyword, L"info-quotes")) { |
237 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
238 | ret.lquote = uadv(p->keyword); |
239 | ret.rquote = uadv(ret.lquote); |
240 | } |
5dd44dce |
241 | } |
242 | } |
243 | } |
244 | |
5b1d0032 |
245 | /* |
246 | * Now process fallbacks on quote characters, underlines, the |
247 | * rule character, the emphasis characters, and bullets. |
248 | */ |
249 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && |
250 | (!cvt_ok(ret.charset, ret.lquote) || |
251 | !cvt_ok(ret.charset, ret.rquote))) { |
252 | ret.lquote = uadv(ret.rquote); |
253 | ret.rquote = uadv(ret.lquote); |
254 | } |
255 | |
256 | while (*uadv(ret.endemph) && *uadv(uadv(ret.endemph)) && |
257 | (!cvt_ok(ret.charset, ret.startemph) || |
258 | !cvt_ok(ret.charset, ret.endemph))) { |
259 | ret.startemph = uadv(ret.endemph); |
260 | ret.endemph = uadv(ret.startemph); |
261 | } |
262 | |
e32df3e7 |
263 | while (*ret.atitle.underline && *uadv(ret.atitle.underline) && |
264 | !cvt_ok(ret.charset, ret.atitle.underline)) |
265 | ret.atitle.underline = uadv(ret.atitle.underline); |
266 | |
267 | while (*ret.achapter.underline && *uadv(ret.achapter.underline) && |
268 | !cvt_ok(ret.charset, ret.achapter.underline)) |
269 | ret.achapter.underline = uadv(ret.achapter.underline); |
270 | |
271 | for (n = 0; n < ret.nasect; n++) { |
272 | while (*ret.asect[n].underline && *uadv(ret.asect[n].underline) && |
273 | !cvt_ok(ret.charset, ret.asect[n].underline)) |
274 | ret.asect[n].underline = uadv(ret.asect[n].underline); |
275 | } |
276 | |
5b1d0032 |
277 | while (*ret.bullet && *uadv(ret.bullet) && |
278 | !cvt_ok(ret.charset, ret.bullet)) |
279 | ret.bullet = uadv(ret.bullet); |
280 | |
281 | while (*ret.rule && *uadv(ret.rule) && |
282 | !cvt_ok(ret.charset, ret.rule)) |
283 | ret.rule = uadv(ret.rule); |
284 | |
5dd44dce |
285 | return ret; |
286 | } |
287 | |
288 | paragraph *info_config_filename(char *filename) |
289 | { |
e4ea58f8 |
290 | return cmdline_cfg_simple("info-filename", filename, NULL); |
5dd44dce |
291 | } |
292 | |
293 | void info_backend(paragraph *sourceform, keywordlist *keywords, |
43341922 |
294 | indexdata *idx, void *unused) { |
5dd44dce |
295 | paragraph *p; |
296 | infoconfig conf; |
297 | word *prefix, *body, *wp; |
298 | word spaceword; |
91f93b94 |
299 | wchar_t *prefixextra; |
5dd44dce |
300 | int nesting, nestindent; |
301 | int indentb, indenta; |
302 | int filepos; |
303 | int has_index; |
91f93b94 |
304 | info_data intro_text = EMPTY_INFO_DATA; |
5dd44dce |
305 | node *topnode, *currnode; |
306 | word bullet; |
307 | FILE *fp; |
308 | |
43341922 |
309 | IGNORE(unused); |
5dd44dce |
310 | |
311 | conf = info_configure(sourceform); |
312 | |
313 | /* |
314 | * Go through and create a node for each section. |
315 | */ |
91f93b94 |
316 | topnode = info_node_new("Top", conf.charset); |
5dd44dce |
317 | currnode = topnode; |
318 | for (p = sourceform; p; p = p->next) switch (p->type) { |
319 | /* |
320 | * Chapter titles. |
321 | */ |
322 | case para_Chapter: |
323 | case para_Appendix: |
324 | case para_UnnumberedChapter: |
325 | case para_Heading: |
326 | case para_Subsect: |
327 | { |
328 | node *newnode, *upnode; |
329 | char *nodename; |
330 | |
f6220253 |
331 | nodename = info_node_name_for_para(p, &conf); |
91f93b94 |
332 | newnode = info_node_new(nodename, conf.charset); |
5dd44dce |
333 | sfree(nodename); |
334 | |
335 | p->private_data = newnode; |
336 | |
337 | if (p->parent) |
338 | upnode = (node *)p->parent->private_data; |
339 | else |
340 | upnode = topnode; |
341 | assert(upnode); |
342 | newnode->up = upnode; |
343 | |
344 | currnode->next = newnode; |
345 | newnode->prev = currnode; |
346 | |
347 | currnode->listnext = newnode; |
348 | currnode = newnode; |
349 | } |
350 | break; |
e0e55d41 |
351 | default: |
352 | p->private_data = NULL; |
353 | break; |
5dd44dce |
354 | } |
355 | |
356 | /* |
357 | * Set up the display form of each index entry. |
358 | */ |
359 | { |
360 | int i; |
361 | indexentry *entry; |
362 | |
363 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
f1530049 |
364 | info_idx *ii = snew(info_idx); |
91f93b94 |
365 | info_data id = EMPTY_INFO_DATA; |
366 | |
367 | id.charset = conf.charset; |
5dd44dce |
368 | |
369 | ii->nnodes = ii->nodesize = 0; |
370 | ii->nodes = NULL; |
371 | |
5b1d0032 |
372 | ii->length = info_rdaddwc(&id, entry->text, NULL, FALSE, &conf); |
f4551933 |
373 | |
91f93b94 |
374 | ii->text = id.output.text; |
5dd44dce |
375 | |
376 | entry->backend_data = ii; |
377 | } |
378 | } |
379 | |
380 | /* |
381 | * An Info file begins with a piece of introductory text which |
382 | * is apparently never shown anywhere. This seems to me to be a |
d4c7e130 |
383 | * good place to put the copyright notice and the version IDs. |
384 | * Also, Info directory entries are expected to go here. |
5dd44dce |
385 | */ |
91f93b94 |
386 | intro_text.charset = conf.charset; |
5dd44dce |
387 | |
91f93b94 |
388 | info_rdaddsc(&intro_text, |
5dd44dce |
389 | "This Info file generated by Halibut, "); |
91f93b94 |
390 | info_rdaddsc(&intro_text, version); |
391 | info_rdaddsc(&intro_text, "\n\n"); |
5dd44dce |
392 | |
393 | for (p = sourceform; p; p = p->next) |
d4c7e130 |
394 | if (p->type == para_Config && |
395 | !ustricmp(p->keyword, L"info-dir-entry")) { |
396 | wchar_t *section, *shortname, *longname, *kw; |
397 | char *s; |
398 | |
399 | section = uadv(p->keyword); |
12f0ee84 |
400 | shortname = *section ? uadv(section) : L""; |
401 | longname = *shortname ? uadv(shortname) : L""; |
402 | kw = *longname ? uadv(longname) : L""; |
d4c7e130 |
403 | |
404 | if (!*longname) { |
12f0ee84 |
405 | error(err_cfginsufarg, &p->fpos, p->origkeyword, 3); |
d4c7e130 |
406 | continue; |
407 | } |
408 | |
91f93b94 |
409 | info_rdaddsc(&intro_text, "INFO-DIR-SECTION "); |
410 | info_rdadds(&intro_text, section); |
411 | info_rdaddsc(&intro_text, "\nSTART-INFO-DIR-ENTRY\n* "); |
412 | info_rdadds(&intro_text, shortname); |
413 | info_rdaddsc(&intro_text, ": ("); |
d4c7e130 |
414 | s = dupstr(conf.filename); |
415 | if (strlen(s) > 5 && !strcmp(s+strlen(s)-5, ".info")) |
416 | s[strlen(s)-5] = '\0'; |
91f93b94 |
417 | info_rdaddsc(&intro_text, s); |
d4c7e130 |
418 | sfree(s); |
91f93b94 |
419 | info_rdaddsc(&intro_text, ")"); |
d4c7e130 |
420 | if (*kw) { |
421 | keyword *kwl = kw_lookup(keywords, kw); |
422 | if (kwl && kwl->para->private_data) { |
423 | node *n = (node *)kwl->para->private_data; |
91f93b94 |
424 | info_rdaddsc(&intro_text, n->name); |
d4c7e130 |
425 | } |
426 | } |
91f93b94 |
427 | info_rdaddsc(&intro_text, ". "); |
428 | info_rdadds(&intro_text, longname); |
429 | info_rdaddsc(&intro_text, "\nEND-INFO-DIR-ENTRY\n\n"); |
d4c7e130 |
430 | } |
431 | |
432 | for (p = sourceform; p; p = p->next) |
5dd44dce |
433 | if (p->type == para_Copyright) |
434 | info_para(&intro_text, NULL, NULL, p->words, keywords, |
5b1d0032 |
435 | 0, 0, conf.width, &conf); |
5dd44dce |
436 | |
437 | for (p = sourceform; p; p = p->next) |
438 | if (p->type == para_VersionID) |
5b1d0032 |
439 | info_versionid(&intro_text, p->words, &conf); |
5dd44dce |
440 | |
91f93b94 |
441 | if (intro_text.output.text[intro_text.output.pos-1] != '\n') |
442 | info_rdaddc(&intro_text, '\n'); |
5dd44dce |
443 | |
444 | /* Do the title */ |
445 | for (p = sourceform; p; p = p->next) |
446 | if (p->type == para_Title) |
e32df3e7 |
447 | info_heading(&topnode->text, NULL, p->words, |
448 | conf.atitle, conf.width, &conf); |
5dd44dce |
449 | |
5b1d0032 |
450 | nestindent = conf.listindentbefore + conf.listindentafter; |
5dd44dce |
451 | nesting = 0; |
452 | |
453 | currnode = topnode; |
454 | |
455 | /* Do the main document */ |
456 | for (p = sourceform; p; p = p->next) switch (p->type) { |
457 | |
458 | case para_QuotePush: |
459 | nesting += 2; |
460 | break; |
461 | case para_QuotePop: |
462 | nesting -= 2; |
463 | assert(nesting >= 0); |
464 | break; |
465 | |
466 | case para_LcontPush: |
467 | nesting += nestindent; |
468 | break; |
469 | case para_LcontPop: |
470 | nesting -= nestindent; |
471 | assert(nesting >= 0); |
472 | break; |
473 | |
474 | /* |
475 | * Things we ignore because we've already processed them or |
476 | * aren't going to touch them in this pass. |
477 | */ |
478 | case para_IM: |
479 | case para_BR: |
480 | case para_Biblio: /* only touch BiblioCited */ |
481 | case para_VersionID: |
482 | case para_NoCite: |
483 | case para_Title: |
484 | break; |
485 | |
486 | /* |
487 | * Chapter titles. |
488 | */ |
489 | case para_Chapter: |
490 | case para_Appendix: |
491 | case para_UnnumberedChapter: |
492 | case para_Heading: |
493 | case para_Subsect: |
494 | currnode = p->private_data; |
495 | assert(currnode); |
496 | assert(currnode->up); |
497 | |
498 | if (!currnode->up->started_menu) { |
91f93b94 |
499 | info_rdaddsc(&currnode->up->text, "* Menu:\n\n"); |
5dd44dce |
500 | currnode->up->started_menu = TRUE; |
501 | } |
5b1d0032 |
502 | info_menu_item(&currnode->up->text, currnode, p, &conf); |
5dd44dce |
503 | |
504 | has_index |= info_check_index(p->words, currnode, idx); |
e32df3e7 |
505 | if (p->type == para_Chapter || p->type == para_Appendix || |
506 | p->type == para_UnnumberedChapter) |
507 | info_heading(&currnode->text, p->kwtext, p->words, |
508 | conf.achapter, conf.width, &conf); |
509 | else |
510 | info_heading(&currnode->text, p->kwtext, p->words, |
511 | conf.asect[p->aux>=conf.nasect?conf.nasect-1:p->aux], |
512 | conf.width, &conf); |
5dd44dce |
513 | nesting = 0; |
514 | break; |
515 | |
516 | case para_Rule: |
5b1d0032 |
517 | info_rule(&currnode->text, nesting, conf.width - nesting, &conf); |
5dd44dce |
518 | break; |
519 | |
520 | case para_Normal: |
521 | case para_Copyright: |
522 | case para_DescribedThing: |
523 | case para_Description: |
524 | case para_BiblioCited: |
525 | case para_Bullet: |
526 | case para_NumberedList: |
527 | has_index |= info_check_index(p->words, currnode, idx); |
528 | if (p->type == para_Bullet) { |
529 | bullet.next = NULL; |
530 | bullet.alt = NULL; |
531 | bullet.type = word_Normal; |
5b1d0032 |
532 | bullet.text = conf.bullet; |
5dd44dce |
533 | prefix = • |
534 | prefixextra = NULL; |
5b1d0032 |
535 | indentb = conf.listindentbefore; |
536 | indenta = conf.listindentafter; |
5dd44dce |
537 | } else if (p->type == para_NumberedList) { |
538 | prefix = p->kwtext; |
5b1d0032 |
539 | prefixextra = conf.listsuffix; |
540 | indentb = conf.listindentbefore; |
541 | indenta = conf.listindentafter; |
5dd44dce |
542 | } else if (p->type == para_Description) { |
543 | prefix = NULL; |
544 | prefixextra = NULL; |
5b1d0032 |
545 | indentb = conf.listindentbefore; |
546 | indenta = conf.listindentafter; |
5dd44dce |
547 | } else { |
548 | prefix = NULL; |
549 | prefixextra = NULL; |
550 | indentb = indenta = 0; |
551 | } |
552 | if (p->type == para_BiblioCited) { |
553 | body = dup_word_list(p->kwtext); |
554 | for (wp = body; wp->next; wp = wp->next); |
555 | wp->next = &spaceword; |
556 | spaceword.next = p->words; |
557 | spaceword.alt = NULL; |
558 | spaceword.type = word_WhiteSpace; |
559 | spaceword.text = NULL; |
560 | } else { |
561 | wp = NULL; |
562 | body = p->words; |
563 | } |
564 | info_para(&currnode->text, prefix, prefixextra, body, keywords, |
565 | nesting + indentb, indenta, |
5b1d0032 |
566 | conf.width - nesting - indentb - indenta, &conf); |
5dd44dce |
567 | if (wp) { |
568 | wp->next = NULL; |
569 | free_word_list(body); |
570 | } |
571 | break; |
572 | |
573 | case para_Code: |
574 | info_codepara(&currnode->text, p->words, |
5b1d0032 |
575 | nesting + conf.indent_code, |
576 | conf.width - nesting - 2 * conf.indent_code); |
5dd44dce |
577 | break; |
578 | } |
579 | |
580 | /* |
581 | * Create an index node if required. |
582 | */ |
583 | if (has_index) { |
584 | node *newnode; |
585 | int i, j, k; |
586 | indexentry *entry; |
f6220253 |
587 | char *nodename; |
588 | |
589 | nodename = info_node_name_for_text(conf.index_text, &conf); |
590 | newnode = info_node_new(nodename, conf.charset); |
591 | sfree(nodename); |
5dd44dce |
592 | |
5dd44dce |
593 | newnode->up = topnode; |
594 | |
595 | currnode->next = newnode; |
596 | newnode->prev = currnode; |
597 | currnode->listnext = newnode; |
598 | |
f6220253 |
599 | k = info_rdadds(&newnode->text, conf.index_text); |
600 | info_rdaddsc(&newnode->text, "\n"); |
601 | while (k > 0) { |
e32df3e7 |
602 | info_rdadds(&newnode->text, conf.achapter.underline); |
603 | k -= ustrwid(conf.achapter.underline, conf.charset); |
f6220253 |
604 | } |
605 | info_rdaddsc(&newnode->text, "\n\n"); |
5dd44dce |
606 | |
5b1d0032 |
607 | info_menu_item(&topnode->text, newnode, NULL, &conf); |
5dd44dce |
608 | |
609 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
610 | info_idx *ii = (info_idx *)entry->backend_data; |
611 | |
612 | for (j = 0; j < ii->nnodes; j++) { |
5dd44dce |
613 | /* |
614 | * When we have multiple references for a single |
615 | * index term, we only display the actual term on |
616 | * the first line, to make it clear that the terms |
617 | * really are the same. |
618 | */ |
619 | if (j == 0) |
91f93b94 |
620 | info_rdaddsc(&newnode->text, ii->text); |
5b1d0032 |
621 | for (k = (j ? 0 : ii->length); k < conf.index_width-2; k++) |
91f93b94 |
622 | info_rdaddc(&newnode->text, ' '); |
5b1d0032 |
623 | info_rdaddsc(&newnode->text, " *Note "); |
91f93b94 |
624 | info_rdaddsc(&newnode->text, ii->nodes[j]->name); |
625 | info_rdaddsc(&newnode->text, "::\n"); |
5dd44dce |
626 | } |
627 | } |
628 | } |
629 | |
630 | /* |
631 | * Finalise the text of each node, by adding the ^_ delimiter |
632 | * and the node line at the top. |
633 | */ |
634 | for (currnode = topnode; currnode; currnode = currnode->listnext) { |
91f93b94 |
635 | char *origtext = currnode->text.output.text; |
636 | currnode->text = empty_info_data; |
637 | currnode->text.charset = conf.charset; |
638 | info_rdaddsc(&currnode->text, "\037\nFile: "); |
639 | info_rdaddsc(&currnode->text, conf.filename); |
640 | info_rdaddsc(&currnode->text, ", Node: "); |
641 | info_rdaddsc(&currnode->text, currnode->name); |
5dd44dce |
642 | if (currnode->prev) { |
91f93b94 |
643 | info_rdaddsc(&currnode->text, ", Prev: "); |
644 | info_rdaddsc(&currnode->text, currnode->prev->name); |
5dd44dce |
645 | } |
91f93b94 |
646 | info_rdaddsc(&currnode->text, ", Up: "); |
647 | info_rdaddsc(&currnode->text, (currnode->up ? |
648 | currnode->up->name : "(dir)")); |
5dd44dce |
649 | if (currnode->next) { |
91f93b94 |
650 | info_rdaddsc(&currnode->text, ", Next: "); |
651 | info_rdaddsc(&currnode->text, currnode->next->name); |
5dd44dce |
652 | } |
91f93b94 |
653 | info_rdaddsc(&currnode->text, "\n\n"); |
654 | info_rdaddsc(&currnode->text, origtext); |
5dd44dce |
655 | /* |
656 | * Just make _absolutely_ sure we end with a newline. |
657 | */ |
91f93b94 |
658 | if (currnode->text.output.text[currnode->text.output.pos-1] != '\n') |
659 | info_rdaddc(&currnode->text, '\n'); |
5dd44dce |
660 | |
661 | sfree(origtext); |
662 | } |
663 | |
664 | /* |
665 | * Compute the offsets for the tag table. |
666 | */ |
91f93b94 |
667 | filepos = intro_text.output.pos; |
5dd44dce |
668 | for (currnode = topnode; currnode; currnode = currnode->listnext) { |
669 | currnode->pos = filepos; |
91f93b94 |
670 | filepos += currnode->text.output.pos; |
5dd44dce |
671 | } |
672 | |
673 | /* |
674 | * Split into sub-files. |
675 | */ |
676 | if (conf.maxfilesize > 0) { |
91f93b94 |
677 | int currfilesize = intro_text.output.pos, currfilenum = 1; |
5dd44dce |
678 | for (currnode = topnode; currnode; currnode = currnode->listnext) { |
91f93b94 |
679 | if (currfilesize > intro_text.output.pos && |
680 | currfilesize + currnode->text.output.pos > conf.maxfilesize) { |
5dd44dce |
681 | currfilenum++; |
91f93b94 |
682 | currfilesize = intro_text.output.pos; |
5dd44dce |
683 | } |
684 | currnode->filenum = currfilenum; |
91f93b94 |
685 | currfilesize += currnode->text.output.pos; |
5dd44dce |
686 | } |
687 | } |
688 | |
689 | /* |
690 | * Write the primary output file. |
691 | */ |
692 | fp = fopen(conf.filename, "w"); |
693 | if (!fp) { |
694 | error(err_cantopenw, conf.filename); |
695 | return; |
696 | } |
91f93b94 |
697 | fputs(intro_text.output.text, fp); |
5dd44dce |
698 | if (conf.maxfilesize == 0) { |
699 | for (currnode = topnode; currnode; currnode = currnode->listnext) |
91f93b94 |
700 | fputs(currnode->text.output.text, fp); |
5dd44dce |
701 | } else { |
702 | int filenum = 0; |
703 | fprintf(fp, "\037\nIndirect:\n"); |
704 | for (currnode = topnode; currnode; currnode = currnode->listnext) |
705 | if (filenum != currnode->filenum) { |
706 | filenum = currnode->filenum; |
707 | fprintf(fp, "%s-%d: %d\n", conf.filename, filenum, |
708 | currnode->pos); |
709 | } |
710 | } |
711 | fprintf(fp, "\037\nTag Table:\n"); |
712 | if (conf.maxfilesize > 0) |
713 | fprintf(fp, "(Indirect)\n"); |
714 | for (currnode = topnode; currnode; currnode = currnode->listnext) |
715 | fprintf(fp, "Node: %s\177%d\n", currnode->name, currnode->pos); |
716 | fprintf(fp, "\037\nEnd Tag Table\n"); |
717 | fclose(fp); |
718 | |
719 | /* |
720 | * Write the subfiles. |
721 | */ |
722 | if (conf.maxfilesize > 0) { |
723 | int filenum = 0; |
724 | fp = NULL; |
725 | |
726 | for (currnode = topnode; currnode; currnode = currnode->listnext) { |
727 | if (filenum != currnode->filenum) { |
728 | char *fname; |
729 | |
730 | filenum = currnode->filenum; |
731 | |
732 | if (fp) |
733 | fclose(fp); |
f1530049 |
734 | fname = snewn(strlen(conf.filename) + 40, char); |
5dd44dce |
735 | sprintf(fname, "%s-%d", conf.filename, filenum); |
736 | fp = fopen(fname, "w"); |
737 | if (!fp) { |
738 | error(err_cantopenw, fname); |
739 | return; |
740 | } |
741 | sfree(fname); |
91f93b94 |
742 | fputs(intro_text.output.text, fp); |
5dd44dce |
743 | } |
91f93b94 |
744 | fputs(currnode->text.output.text, fp); |
5dd44dce |
745 | } |
746 | |
747 | if (fp) |
748 | fclose(fp); |
749 | } |
750 | } |
751 | |
752 | static int info_check_index(word *w, node *n, indexdata *idx) |
753 | { |
754 | int ret = 0; |
755 | |
756 | for (; w; w = w->next) { |
757 | if (w->type == word_IndexRef) { |
758 | indextag *tag; |
759 | int i; |
760 | |
761 | tag = index_findtag(idx, w->text); |
762 | if (!tag) |
763 | break; |
764 | |
765 | for (i = 0; i < tag->nrefs; i++) { |
766 | indexentry *entry = tag->refs[i]; |
767 | info_idx *ii = (info_idx *)entry->backend_data; |
768 | |
769 | if (ii->nnodes > 0 && ii->nodes[ii->nnodes-1] == n) { |
770 | /* |
771 | * If the same index term is indexed twice |
772 | * within the same section, we only want to |
773 | * mention it once in the index. So do nothing |
774 | * here. |
775 | */ |
776 | continue; |
777 | } |
778 | |
779 | if (ii->nnodes >= ii->nodesize) { |
780 | ii->nodesize += 32; |
f1530049 |
781 | ii->nodes = sresize(ii->nodes, ii->nodesize, node *); |
5dd44dce |
782 | } |
783 | |
784 | ii->nodes[ii->nnodes++] = n; |
785 | |
786 | ret = 1; |
787 | } |
788 | } |
789 | } |
790 | |
791 | return ret; |
792 | } |
793 | |
5dd44dce |
794 | static word *info_transform_wordlist(word *words, keywordlist *keywords) |
795 | { |
796 | word *ret = dup_word_list(words); |
797 | word *w; |
798 | keyword *kwl; |
799 | |
800 | for (w = ret; w; w = w->next) { |
801 | w->private_data = NULL; |
802 | if (w->type == word_UpperXref || w->type == word_LowerXref) { |
803 | kwl = kw_lookup(keywords, w->text); |
804 | if (kwl) { |
805 | if (kwl->para->type == para_NumberedList || |
806 | kwl->para->type == para_BiblioCited) { |
807 | /* |
808 | * In Info, we do nothing special for xrefs to |
809 | * numbered list items or bibliography entries. |
810 | */ |
90a0531e |
811 | continue; |
5dd44dce |
812 | } else { |
813 | /* |
814 | * An xref to a different section has its text |
815 | * completely replaced. |
816 | */ |
817 | word *w2, *w3, *w4; |
818 | w2 = w3 = w->next; |
819 | w4 = NULL; |
820 | while (w2) { |
821 | if (w2->type == word_XrefEnd) { |
822 | w4 = w2->next; |
823 | w2->next = NULL; |
824 | break; |
825 | } |
826 | w2 = w2->next; |
827 | } |
828 | free_word_list(w3); |
829 | |
830 | /* |
831 | * Now w is the UpperXref / LowerXref we |
832 | * started with, and w4 is the next word after |
833 | * the corresponding XrefEnd (if any). The |
834 | * simplest thing is just to stick a pointer to |
835 | * the target node structure in the private |
836 | * data field of the xref word, and let |
837 | * info_rdaddwc and friends read the node name |
838 | * out from there. |
839 | */ |
840 | w->next = w4; |
841 | w->private_data = kwl->para->private_data; |
842 | assert(w->private_data); |
843 | } |
844 | } |
845 | } |
846 | } |
847 | |
848 | return ret; |
849 | } |
850 | |
5b1d0032 |
851 | static int info_rdaddwc(info_data *id, word *words, word *end, int xrefs, |
852 | infoconfig *cfg) { |
91f93b94 |
853 | int ret = 0; |
5dd44dce |
854 | |
855 | for (; words && words != end; words = words->next) switch (words->type) { |
856 | case word_HyperLink: |
857 | case word_HyperEnd: |
858 | case word_XrefEnd: |
859 | case word_IndexRef: |
860 | break; |
861 | |
862 | case word_Normal: |
863 | case word_Emph: |
864 | case word_Code: |
865 | case word_WeakCode: |
866 | case word_WhiteSpace: |
867 | case word_EmphSpace: |
868 | case word_CodeSpace: |
869 | case word_WkCodeSpace: |
870 | case word_Quote: |
871 | case word_EmphQuote: |
872 | case word_CodeQuote: |
873 | case word_WkCodeQuote: |
874 | assert(words->type != word_CodeQuote && |
875 | words->type != word_WkCodeQuote); |
876 | if (towordstyle(words->type) == word_Emph && |
877 | (attraux(words->aux) == attr_First || |
878 | attraux(words->aux) == attr_Only)) |
5b1d0032 |
879 | ret += info_rdadds(id, cfg->startemph); |
5dd44dce |
880 | else if (towordstyle(words->type) == word_Code && |
881 | (attraux(words->aux) == attr_First || |
882 | attraux(words->aux) == attr_Only)) |
5b1d0032 |
883 | ret += info_rdadds(id, cfg->lquote); |
5dd44dce |
884 | if (removeattr(words->type) == word_Normal) { |
91f93b94 |
885 | if (cvt_ok(id->charset, words->text) || !words->alt) |
886 | ret += info_rdadds(id, words->text); |
5dd44dce |
887 | else |
5b1d0032 |
888 | ret += info_rdaddwc(id, words->alt, NULL, FALSE, cfg); |
5dd44dce |
889 | } else if (removeattr(words->type) == word_WhiteSpace) { |
91f93b94 |
890 | ret += info_rdadd(id, L' '); |
5dd44dce |
891 | } else if (removeattr(words->type) == word_Quote) { |
5b1d0032 |
892 | ret += info_rdadds(id, quoteaux(words->aux) == quote_Open ? |
893 | cfg->lquote : cfg->rquote); |
5dd44dce |
894 | } |
895 | if (towordstyle(words->type) == word_Emph && |
896 | (attraux(words->aux) == attr_Last || |
897 | attraux(words->aux) == attr_Only)) |
5b1d0032 |
898 | ret += info_rdadds(id, cfg->endemph); |
5dd44dce |
899 | else if (towordstyle(words->type) == word_Code && |
900 | (attraux(words->aux) == attr_Last || |
901 | attraux(words->aux) == attr_Only)) |
5b1d0032 |
902 | ret += info_rdadds(id, cfg->rquote); |
5dd44dce |
903 | break; |
904 | |
905 | case word_UpperXref: |
906 | case word_LowerXref: |
907 | if (xrefs && words->private_data) { |
91f93b94 |
908 | /* |
909 | * This bit is structural and so must be done in char |
910 | * rather than wchar_t. |
911 | */ |
912 | ret += info_rdaddsc(id, "*Note "); |
913 | ret += info_rdaddsc(id, ((node *)words->private_data)->name); |
914 | ret += info_rdaddsc(id, "::"); |
5dd44dce |
915 | } |
916 | break; |
917 | } |
91f93b94 |
918 | |
919 | return ret; |
5dd44dce |
920 | } |
921 | |
5b1d0032 |
922 | static int info_width_internal(word *words, int xrefs, infoconfig *cfg); |
5dd44dce |
923 | |
5b1d0032 |
924 | static int info_width_internal_list(word *words, int xrefs, infoconfig *cfg) { |
5dd44dce |
925 | int w = 0; |
926 | while (words) { |
5b1d0032 |
927 | w += info_width_internal(words, xrefs, cfg); |
5dd44dce |
928 | words = words->next; |
929 | } |
930 | return w; |
931 | } |
932 | |
5b1d0032 |
933 | static int info_width_internal(word *words, int xrefs, infoconfig *cfg) { |
934 | int wid; |
935 | int attr; |
936 | |
5dd44dce |
937 | switch (words->type) { |
938 | case word_HyperLink: |
939 | case word_HyperEnd: |
940 | case word_XrefEnd: |
941 | case word_IndexRef: |
942 | return 0; |
943 | |
5b1d0032 |
944 | case word_UpperXref: |
945 | case word_LowerXref: |
946 | if (xrefs && words->private_data) { |
947 | /* "*Note " plus "::" comes to 8 characters */ |
948 | return 8 + strwid(((node *)words->private_data)->name, |
949 | cfg->charset); |
950 | } else |
951 | return 0; |
952 | } |
953 | |
954 | assert(words->type < word_internal_endattrs); |
955 | |
956 | wid = 0; |
957 | attr = towordstyle(words->type); |
958 | |
959 | if (attr == word_Emph || attr == word_Code) { |
960 | if (attraux(words->aux) == attr_Only || |
961 | attraux(words->aux) == attr_First) |
962 | wid += ustrwid(attr == word_Emph ? cfg->startemph : cfg->lquote, |
963 | cfg->charset); |
964 | } |
965 | if (attr == word_Emph || attr == word_Code) { |
966 | if (attraux(words->aux) == attr_Only || |
967 | attraux(words->aux) == attr_Last) |
968 | wid += ustrwid(attr == word_Emph ? cfg->startemph : cfg->lquote, |
969 | cfg->charset); |
970 | } |
971 | |
972 | switch (words->type) { |
5dd44dce |
973 | case word_Normal: |
974 | case word_Emph: |
975 | case word_Code: |
976 | case word_WeakCode: |
5b1d0032 |
977 | if (cvt_ok(cfg->charset, words->text) || !words->alt) |
978 | wid += ustrwid(words->text, cfg->charset); |
979 | else |
980 | wid += info_width_internal_list(words->alt, xrefs, cfg); |
981 | return wid; |
5dd44dce |
982 | |
983 | case word_WhiteSpace: |
984 | case word_EmphSpace: |
985 | case word_CodeSpace: |
986 | case word_WkCodeSpace: |
987 | case word_Quote: |
988 | case word_EmphQuote: |
989 | case word_CodeQuote: |
990 | case word_WkCodeQuote: |
991 | assert(words->type != word_CodeQuote && |
992 | words->type != word_WkCodeQuote); |
5b1d0032 |
993 | if (removeattr(words->type) == word_Quote) { |
994 | if (quoteaux(words->aux) == quote_Open) |
995 | wid += ustrwid(cfg->lquote, cfg->charset); |
996 | else |
997 | wid += ustrwid(cfg->rquote, cfg->charset); |
998 | } else |
999 | wid++; /* space */ |
5dd44dce |
1000 | } |
5b1d0032 |
1001 | return wid; |
5dd44dce |
1002 | } |
1003 | |
43341922 |
1004 | static int info_width_noxrefs(void *ctx, word *words) |
5dd44dce |
1005 | { |
5b1d0032 |
1006 | return info_width_internal(words, FALSE, (infoconfig *)ctx); |
5dd44dce |
1007 | } |
43341922 |
1008 | static int info_width_xrefs(void *ctx, word *words) |
5dd44dce |
1009 | { |
5b1d0032 |
1010 | return info_width_internal(words, TRUE, (infoconfig *)ctx); |
5dd44dce |
1011 | } |
1012 | |
91f93b94 |
1013 | static void info_heading(info_data *text, word *tprefix, |
e32df3e7 |
1014 | word *words, alignstruct align, |
1015 | int width, infoconfig *cfg) { |
91f93b94 |
1016 | int length; |
5dd44dce |
1017 | int firstlinewidth, wrapwidth; |
5dd44dce |
1018 | wrappedline *wrapping, *p; |
1019 | |
91f93b94 |
1020 | length = 0; |
5dd44dce |
1021 | if (tprefix) { |
5b1d0032 |
1022 | length += info_rdaddwc(text, tprefix, NULL, FALSE, cfg); |
1023 | length += info_rdadds(text, cfg->sectsuffix); |
5dd44dce |
1024 | } |
5dd44dce |
1025 | |
5dd44dce |
1026 | wrapwidth = width; |
91f93b94 |
1027 | firstlinewidth = width - length; |
5dd44dce |
1028 | |
43341922 |
1029 | wrapping = wrap_para(words, firstlinewidth, wrapwidth, |
5b1d0032 |
1030 | info_width_noxrefs, cfg, 0); |
5dd44dce |
1031 | for (p = wrapping; p; p = p->next) { |
5b1d0032 |
1032 | length += info_rdaddwc(text, p->begin, p->end, FALSE, cfg); |
91f93b94 |
1033 | info_rdadd(text, L'\n'); |
e32df3e7 |
1034 | if (*align.underline) { |
1035 | while (length > 0) { |
1036 | info_rdadds(text, align.underline); |
1037 | length -= ustrwid(align.underline, cfg->charset); |
1038 | } |
1039 | info_rdadd(text, L'\n'); |
5b1d0032 |
1040 | } |
91f93b94 |
1041 | length = 0; |
5dd44dce |
1042 | } |
1043 | wrap_free(wrapping); |
91f93b94 |
1044 | info_rdadd(text, L'\n'); |
5dd44dce |
1045 | } |
1046 | |
5b1d0032 |
1047 | static void info_rule(info_data *text, int indent, int width, infoconfig *cfg) |
1048 | { |
91f93b94 |
1049 | while (indent--) info_rdadd(text, L' '); |
5b1d0032 |
1050 | while (width > 0) { |
1051 | info_rdadds(text, cfg->rule); |
1052 | width -= ustrwid(cfg->rule, cfg->charset); |
1053 | } |
91f93b94 |
1054 | info_rdadd(text, L'\n'); |
1055 | info_rdadd(text, L'\n'); |
5dd44dce |
1056 | } |
1057 | |
91f93b94 |
1058 | static void info_para(info_data *text, word *prefix, wchar_t *prefixextra, |
5b1d0032 |
1059 | word *input, keywordlist *keywords, int indent, |
1060 | int extraindent, int width, infoconfig *cfg) { |
5dd44dce |
1061 | wrappedline *wrapping, *p; |
1062 | word *words; |
5dd44dce |
1063 | int e; |
1064 | int i; |
1065 | int firstlinewidth = width; |
1066 | |
1067 | words = info_transform_wordlist(input, keywords); |
1068 | |
1069 | if (prefix) { |
5dd44dce |
1070 | for (i = 0; i < indent; i++) |
91f93b94 |
1071 | info_rdadd(text, L' '); |
5b1d0032 |
1072 | e = info_rdaddwc(text, prefix, NULL, FALSE, cfg); |
91f93b94 |
1073 | if (prefixextra) |
1074 | e += info_rdadds(text, prefixextra); |
5dd44dce |
1075 | /* If the prefix is too long, shorten the first line to fit. */ |
91f93b94 |
1076 | e = extraindent - e; |
5dd44dce |
1077 | if (e < 0) { |
1078 | firstlinewidth += e; /* this decreases it, since e < 0 */ |
1079 | if (firstlinewidth < 0) { |
1080 | e = indent + extraindent; |
1081 | firstlinewidth = width; |
91f93b94 |
1082 | info_rdadd(text, L'\n'); |
5dd44dce |
1083 | } else |
1084 | e = 0; |
1085 | } |
5dd44dce |
1086 | } else |
1087 | e = indent + extraindent; |
1088 | |
43341922 |
1089 | wrapping = wrap_para(words, firstlinewidth, width, info_width_xrefs, |
5b1d0032 |
1090 | cfg, 0); |
5dd44dce |
1091 | for (p = wrapping; p; p = p->next) { |
1092 | for (i = 0; i < e; i++) |
91f93b94 |
1093 | info_rdadd(text, L' '); |
5b1d0032 |
1094 | info_rdaddwc(text, p->begin, p->end, TRUE, cfg); |
91f93b94 |
1095 | info_rdadd(text, L'\n'); |
5dd44dce |
1096 | e = indent + extraindent; |
1097 | } |
1098 | wrap_free(wrapping); |
91f93b94 |
1099 | info_rdadd(text, L'\n'); |
5dd44dce |
1100 | |
1101 | free_word_list(words); |
1102 | } |
1103 | |
91f93b94 |
1104 | static void info_codepara(info_data *text, word *words, |
5dd44dce |
1105 | int indent, int width) { |
1106 | int i; |
1107 | |
1108 | for (; words; words = words->next) if (words->type == word_WeakCode) { |
91f93b94 |
1109 | for (i = 0; i < indent; i++) |
1110 | info_rdadd(text, L' '); |
1111 | if (info_rdadds(text, words->text) > width) { |
5dd44dce |
1112 | /* FIXME: warn */ |
1113 | } |
91f93b94 |
1114 | info_rdadd(text, L'\n'); |
5dd44dce |
1115 | } |
1116 | |
91f93b94 |
1117 | info_rdadd(text, L'\n'); |
5dd44dce |
1118 | } |
1119 | |
5b1d0032 |
1120 | static void info_versionid(info_data *text, word *words, infoconfig *cfg) { |
1121 | info_rdadd(text, L'['); |
1122 | info_rdaddwc(text, words, NULL, FALSE, cfg); |
91f93b94 |
1123 | info_rdadds(text, L"]\n"); |
5dd44dce |
1124 | } |
1125 | |
91f93b94 |
1126 | static node *info_node_new(char *name, int charset) |
5dd44dce |
1127 | { |
1128 | node *n; |
1129 | |
f1530049 |
1130 | n = snew(node); |
91f93b94 |
1131 | n->text = empty_info_data; |
1132 | n->text.charset = charset; |
5dd44dce |
1133 | n->up = n->next = n->prev = n->lastchild = n->listnext = NULL; |
1134 | n->name = dupstr(name); |
1135 | n->started_menu = FALSE; |
1136 | |
1137 | return n; |
1138 | } |
1139 | |
f6220253 |
1140 | static char *info_node_name_core(info_data *id, filepos *fpos) |
5dd44dce |
1141 | { |
f4551933 |
1142 | char *p, *q; |
91f93b94 |
1143 | |
f4551933 |
1144 | /* |
f6220253 |
1145 | * We cannot have commas, colons or parentheses in a node name. |
1146 | * Remove any that we find, with a warning. |
f4551933 |
1147 | */ |
f6220253 |
1148 | p = q = id->output.text; |
f4551933 |
1149 | while (*p) { |
f6220253 |
1150 | if (*p == ':' || *p == ',' || *p == '(' || *p == ')') { |
1151 | error(err_infonodechar, fpos, *p); |
f4551933 |
1152 | } else { |
1153 | *q++ = *p; |
1154 | } |
1155 | p++; |
1156 | } |
f6220253 |
1157 | *q = '\0'; |
1158 | |
1159 | return id->output.text; |
1160 | } |
1161 | |
1162 | static char *info_node_name_for_para(paragraph *par, infoconfig *cfg) |
1163 | { |
1164 | info_data id = EMPTY_INFO_DATA; |
1165 | |
1166 | id.charset = cfg->charset; |
1167 | info_rdaddwc(&id, par->kwtext ? par->kwtext : par->words, |
1168 | NULL, FALSE, cfg); |
1169 | info_rdaddsc(&id, NULL); |
1170 | |
1171 | return info_node_name_core(&id, &par->fpos); |
1172 | } |
1173 | |
1174 | static char *info_node_name_for_text(wchar_t *text, infoconfig *cfg) |
1175 | { |
1176 | info_data id = EMPTY_INFO_DATA; |
1177 | |
1178 | id.charset = cfg->charset; |
1179 | info_rdadds(&id, text); |
1180 | info_rdaddsc(&id, NULL); |
f4551933 |
1181 | |
f6220253 |
1182 | return info_node_name_core(&id, NULL); |
5dd44dce |
1183 | } |
1184 | |
5b1d0032 |
1185 | static void info_menu_item(info_data *text, node *n, paragraph *p, |
1186 | infoconfig *cfg) |
5dd44dce |
1187 | { |
1188 | /* |
1189 | * FIXME: Depending on how we're doing node names in this info |
1190 | * file, we might want to do |
1191 | * |
1192 | * * Node name:: Chapter title |
1193 | * |
1194 | * _or_ |
1195 | * |
1196 | * * Chapter number: Node name. |
1197 | * |
91f93b94 |
1198 | * This function mostly works in char rather than wchar_t, |
1199 | * because a menu item is a structural component. |
5dd44dce |
1200 | */ |
91f93b94 |
1201 | info_rdaddsc(text, "* "); |
1202 | info_rdaddsc(text, n->name); |
1203 | info_rdaddsc(text, "::"); |
5dd44dce |
1204 | if (p) { |
91f93b94 |
1205 | info_rdaddc(text, ' '); |
5b1d0032 |
1206 | info_rdaddwc(text, p->words, NULL, FALSE, cfg); |
5dd44dce |
1207 | } |
91f93b94 |
1208 | info_rdaddc(text, '\n'); |
1209 | } |
1210 | |
1211 | /* |
1212 | * These functions implement my wrapper on the rdadd* calls which |
1213 | * allows me to switch arbitrarily between literal octet-string |
1214 | * text and charset-translated Unicode. (Because no matter what |
1215 | * character set I write the actual text in, I expect info readers |
1216 | * to treat node names and file names literally and to expect |
1217 | * keywords like `*Note' in their canonical form, so I have to take |
1218 | * steps to ensure that those structural elements of the file |
1219 | * aren't messed with.) |
1220 | */ |
1221 | static int info_rdadds(info_data *d, wchar_t const *wcs) |
1222 | { |
1223 | if (!d->wcmode) { |
1224 | d->state = charset_init_state; |
1225 | d->wcmode = TRUE; |
1226 | } |
1227 | |
1228 | if (wcs) { |
1229 | char buf[256]; |
e5cd393f |
1230 | int len, width, ret; |
1231 | |
1232 | width = ustrwid(wcs, d->charset); |
91f93b94 |
1233 | |
e5cd393f |
1234 | len = ustrlen(wcs); |
91f93b94 |
1235 | while (len > 0) { |
1236 | int prevlen = len; |
1237 | |
1238 | ret = charset_from_unicode(&wcs, &len, buf, lenof(buf), |
1239 | d->charset, &d->state, NULL); |
1240 | |
1241 | assert(len < prevlen); |
1242 | |
1243 | if (ret > 0) { |
1244 | buf[ret] = '\0'; |
1245 | rdaddsc(&d->output, buf); |
1246 | } |
1247 | } |
1248 | |
e5cd393f |
1249 | return width; |
91f93b94 |
1250 | } else |
1251 | return 0; |
1252 | } |
1253 | |
1254 | static int info_rdaddsc(info_data *d, char const *cs) |
1255 | { |
1256 | if (d->wcmode) { |
1257 | char buf[256]; |
1258 | int ret; |
1259 | |
1260 | ret = charset_from_unicode(NULL, 0, buf, lenof(buf), |
1261 | d->charset, &d->state, NULL); |
1262 | if (ret > 0) { |
1263 | buf[ret] = '\0'; |
1264 | rdaddsc(&d->output, buf); |
1265 | } |
1266 | |
1267 | d->wcmode = FALSE; |
1268 | } |
1269 | |
1270 | if (cs) { |
1271 | rdaddsc(&d->output, cs); |
5b1d0032 |
1272 | return strwid(cs, d->charset); |
91f93b94 |
1273 | } else |
1274 | return 0; |
1275 | } |
1276 | |
1277 | static int info_rdadd(info_data *d, wchar_t wc) |
1278 | { |
1279 | wchar_t wcs[2]; |
1280 | wcs[0] = wc; |
1281 | wcs[1] = L'\0'; |
1282 | return info_rdadds(d, wcs); |
1283 | } |
1284 | |
1285 | static int info_rdaddc(info_data *d, char c) |
1286 | { |
1287 | char cs[2]; |
1288 | cs[0] = c; |
1289 | cs[1] = '\0'; |
1290 | return info_rdaddsc(d, cs); |
5dd44dce |
1291 | } |