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