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