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