2 * info backend for Halibut
4 * Possible future work:
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
20 * * Nodename: Section 1.2. Title of section.
21 * * Section 1.2: Nodename. Title of section.
23 * - might be helpful to diagnose duplicate node names!
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.
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.
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.
53 int listindentbefore
, listindentafter
;
54 int indent_code
, width
, index_width
;
55 alignstruct atitle
, achapter
, *asect
;
57 wchar_t *bullet
, *listsuffix
;
58 wchar_t *startemph
, *endemph
;
59 wchar_t *lquote
, *rquote
;
71 #define EMPTY_INFO_DATA { { 0, 0, NULL }, 0, CHARSET_INIT_STATE, FALSE }
72 static const info_data empty_info_data
= EMPTY_INFO_DATA
;
74 typedef struct node_tag node
;
77 node
*up
, *prev
, *next
, *lastchild
;
78 int pos
, started_menu
, filenum
;
90 static int info_rdadd(info_data
*, wchar_t);
91 static int info_rdadds(info_data
*, wchar_t const *);
92 static int info_rdaddc(info_data
*, char);
93 static int info_rdaddsc(info_data
*, char const *);
95 static void info_heading(info_data
*, word
*, word
*, alignstruct
, int,
97 static void info_rule(info_data
*, int, int, infoconfig
*);
98 static void info_para(info_data
*, word
*, wchar_t *, word
*, keywordlist
*,
99 int, int, int, infoconfig
*);
100 static void info_codepara(info_data
*, word
*, int, int);
101 static void info_versionid(info_data
*, word
*, infoconfig
*);
102 static void info_menu_item(info_data
*, node
*, paragraph
*, infoconfig
*);
103 static word
*info_transform_wordlist(word
*, keywordlist
*);
104 static int info_check_index(word
*, node
*, indexdata
*);
106 static int info_rdaddwc(info_data
*, word
*, word
*, int, infoconfig
*);
108 static node
*info_node_new(char *name
, int charset
);
109 static char *info_node_name_for_para(paragraph
*p
, infoconfig
*);
110 static char *info_node_name_for_text(wchar_t *text
, infoconfig
*);
112 static infoconfig
info_configure(paragraph
*source
) {
120 ret
.filename
= dupstr("output.info");
121 ret
.maxfilesize
= 64 << 10;
122 ret
.charset
= CS_ASCII
;
124 ret
.listindentbefore
= 1;
125 ret
.listindentafter
= 3;
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
": ";
137 * Default underline characters are chosen to match those recognised by
140 ret
.atitle
.underline
= L
"*\0\0";
141 ret
.achapter
.underline
= L
"=\0\0";
143 ret
.asect
= snewn(ret
.nasect
, alignstruct
);
144 ret
.asect
[0].underline
= L
"-\0\0";
145 ret
.asect
[1].underline
= L
".\0\0";
146 ret
.index_text
= L
"Index";
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
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
);
161 } else if (!ustricmp(p
->keyword
, L
"index")) {
162 ret
.index_text
= uadv(p
->keyword
);
167 for (p
= source
; p
; p
= p
->next
) {
168 if (p
->type
== para_Config
) {
169 if (!ustricmp(p
->keyword
, L
"info-filename")) {
171 ret
.filename
= dupstr(adv(p
->origkeyword
));
172 } else if (!ustricmp(p
->keyword
, L
"info-charset")) {
173 ret
.charset
= charset_from_ustr(&p
->fpos
, uadv(p
->keyword
));
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")) {
189 ret
.atitle
.underline
= ret
.achapter
.underline
=
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
);
202 if (n
>= ret
.nasect
) {
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];
209 ret
.asect
[n
].underline
= q
;
210 } else if (!ustricmp(p
->keyword
, L
"text-title-underline")) {
211 ret
.atitle
.underline
= uadv(p
->keyword
);
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
);
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
);
233 * Now process fallbacks on quote characters, underlines, the
234 * rule character, the emphasis characters, and bullets.
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
);
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
);
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
);
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
);
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
);
264 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
265 !cvt_ok(ret
.charset
, ret
.bullet
))
266 ret
.bullet
= uadv(ret
.bullet
);
268 while (*ret
.rule
&& *uadv(ret
.rule
) &&
269 !cvt_ok(ret
.charset
, ret
.rule
))
270 ret
.rule
= uadv(ret
.rule
);
275 paragraph
*info_config_filename(char *filename
)
277 return cmdline_cfg_simple("info-filename", filename
, NULL
);
280 void info_backend(paragraph
*sourceform
, keywordlist
*keywords
,
281 indexdata
*idx
, void *unused
) {
284 word
*prefix
, *body
, *wp
;
286 wchar_t *prefixextra
;
287 int nesting
, nestindent
;
288 int indentb
, indenta
;
291 info_data intro_text
= EMPTY_INFO_DATA
;
292 node
*topnode
, *currnode
;
298 conf
= info_configure(sourceform
);
301 * Go through and create a node for each section.
303 topnode
= info_node_new("Top", conf
.charset
);
305 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
311 case para_UnnumberedChapter
:
315 node
*newnode
, *upnode
;
318 nodename
= info_node_name_for_para(p
, &conf
);
319 newnode
= info_node_new(nodename
, conf
.charset
);
322 p
->private_data
= newnode
;
325 upnode
= (node
*)p
->parent
->private_data
;
329 newnode
->up
= upnode
;
331 currnode
->next
= newnode
;
332 newnode
->prev
= currnode
;
334 currnode
->listnext
= newnode
;
339 p
->private_data
= NULL
;
344 * Set up the display form of each index entry.
350 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
351 info_idx
*ii
= snew(info_idx
);
352 info_data id
= EMPTY_INFO_DATA
;
354 id
.charset
= conf
.charset
;
356 ii
->nnodes
= ii
->nodesize
= 0;
359 ii
->length
= info_rdaddwc(&id
, entry
->text
, NULL
, FALSE
, &conf
);
361 ii
->text
= id
.output
.text
;
363 entry
->backend_data
= ii
;
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
370 * good place to put the copyright notice and the version IDs.
371 * Also, Info directory entries are expected to go here.
373 intro_text
.charset
= conf
.charset
;
375 info_rdaddsc(&intro_text
,
376 "This Info file generated by Halibut, ");
377 info_rdaddsc(&intro_text
, version
);
378 info_rdaddsc(&intro_text
, "\n\n");
380 for (p
= sourceform
; p
; p
= p
->next
)
381 if (p
->type
== para_Config
&&
382 !ustricmp(p
->keyword
, L
"info-dir-entry")) {
383 wchar_t *section
, *shortname
, *longname
, *kw
;
386 section
= uadv(p
->keyword
);
387 shortname
= *section ?
uadv(section
) : L
"";
388 longname
= *shortname ?
uadv(shortname
) : L
"";
389 kw
= *longname ?
uadv(longname
) : L
"";
392 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 3);
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
, ": (");
401 s
= dupstr(conf
.filename
);
402 if (strlen(s
) > 5 && !strcmp(s
+strlen(s
)-5, ".info"))
403 s
[strlen(s
)-5] = '\0';
404 info_rdaddsc(&intro_text
, s
);
406 info_rdaddsc(&intro_text
, ")");
408 keyword
*kwl
= kw_lookup(keywords
, kw
);
409 if (kwl
&& kwl
->para
->private_data
) {
410 node
*n
= (node
*)kwl
->para
->private_data
;
411 info_rdaddsc(&intro_text
, n
->name
);
414 info_rdaddsc(&intro_text
, ". ");
415 info_rdadds(&intro_text
, longname
);
416 info_rdaddsc(&intro_text
, "\nEND-INFO-DIR-ENTRY\n\n");
419 for (p
= sourceform
; p
; p
= p
->next
)
420 if (p
->type
== para_Copyright
)
421 info_para(&intro_text
, NULL
, NULL
, p
->words
, keywords
,
422 0, 0, conf
.width
, &conf
);
424 for (p
= sourceform
; p
; p
= p
->next
)
425 if (p
->type
== para_VersionID
)
426 info_versionid(&intro_text
, p
->words
, &conf
);
428 if (intro_text
.output
.text
[intro_text
.output
.pos
-1] != '\n')
429 info_rdaddc(&intro_text
, '\n');
432 for (p
= sourceform
; p
; p
= p
->next
)
433 if (p
->type
== para_Title
)
434 info_heading(&topnode
->text
, NULL
, p
->words
,
435 conf
.atitle
, conf
.width
, &conf
);
437 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
442 /* Do the main document */
443 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
450 assert(nesting
>= 0);
454 nesting
+= nestindent
;
457 nesting
-= nestindent
;
458 assert(nesting
>= 0);
462 * Things we ignore because we've already processed them or
463 * aren't going to touch them in this pass.
467 case para_Biblio
: /* only touch BiblioCited */
478 case para_UnnumberedChapter
:
481 currnode
= p
->private_data
;
483 assert(currnode
->up
);
485 if (!currnode
->up
->started_menu
) {
486 info_rdaddsc(&currnode
->up
->text
, "* Menu:\n\n");
487 currnode
->up
->started_menu
= TRUE
;
489 info_menu_item(&currnode
->up
->text
, currnode
, p
, &conf
);
491 has_index
|= info_check_index(p
->words
, currnode
, idx
);
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
);
497 info_heading(&currnode
->text
, p
->kwtext
, p
->words
,
498 conf
.asect
[p
->aux
>=conf
.nasect?conf
.nasect
-1:p
->aux
],
504 info_rule(&currnode
->text
, nesting
, conf
.width
- nesting
, &conf
);
509 case para_DescribedThing
:
510 case para_Description
:
511 case para_BiblioCited
:
513 case para_NumberedList
:
514 has_index
|= info_check_index(p
->words
, currnode
, idx
);
515 if (p
->type
== para_Bullet
) {
518 bullet
.type
= word_Normal
;
519 bullet
.text
= conf
.bullet
;
522 indentb
= conf
.listindentbefore
;
523 indenta
= conf
.listindentafter
;
524 } else if (p
->type
== para_NumberedList
) {
526 prefixextra
= conf
.listsuffix
;
527 indentb
= conf
.listindentbefore
;
528 indenta
= conf
.listindentafter
;
529 } else if (p
->type
== para_Description
) {
532 indentb
= conf
.listindentbefore
;
533 indenta
= conf
.listindentafter
;
537 indentb
= indenta
= 0;
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
;
551 info_para(&currnode
->text
, prefix
, prefixextra
, body
, keywords
,
552 nesting
+ indentb
, indenta
,
553 conf
.width
- nesting
- indentb
- indenta
, &conf
);
556 free_word_list(body
);
561 info_codepara(&currnode
->text
, p
->words
,
562 nesting
+ conf
.indent_code
,
563 conf
.width
- nesting
- 2 * conf
.indent_code
);
568 * Create an index node if required.
576 nodename
= info_node_name_for_text(conf
.index_text
, &conf
);
577 newnode
= info_node_new(nodename
, conf
.charset
);
580 newnode
->up
= topnode
;
582 currnode
->next
= newnode
;
583 newnode
->prev
= currnode
;
584 currnode
->listnext
= newnode
;
586 k
= info_rdadds(&newnode
->text
, conf
.index_text
);
587 info_rdaddsc(&newnode
->text
, "\n");
589 info_rdadds(&newnode
->text
, conf
.achapter
.underline
);
590 k
-= ustrwid(conf
.achapter
.underline
, conf
.charset
);
592 info_rdaddsc(&newnode
->text
, "\n\n");
594 info_menu_item(&topnode
->text
, newnode
, NULL
, &conf
);
596 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
597 info_idx
*ii
= (info_idx
*)entry
->backend_data
;
599 for (j
= 0; j
< ii
->nnodes
; j
++) {
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.
607 info_rdaddsc(&newnode
->text
, ii
->text
);
608 for (k
= (j ?
0 : ii
->length
); k
< conf
.index_width
-2; k
++)
609 info_rdaddc(&newnode
->text
, ' ');
610 info_rdaddsc(&newnode
->text
, " *Note ");
611 info_rdaddsc(&newnode
->text
, ii
->nodes
[j
]->name
);
612 info_rdaddsc(&newnode
->text
, "::\n");
618 * Finalise the text of each node, by adding the ^_ delimiter
619 * and the node line at the top.
621 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
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
);
629 if (currnode
->prev
) {
630 info_rdaddsc(&currnode
->text
, ", Prev: ");
631 info_rdaddsc(&currnode
->text
, currnode
->prev
->name
);
633 info_rdaddsc(&currnode
->text
, ", Up: ");
634 info_rdaddsc(&currnode
->text
, (currnode
->up ?
635 currnode
->up
->name
: "(dir)"));
636 if (currnode
->next
) {
637 info_rdaddsc(&currnode
->text
, ", Next: ");
638 info_rdaddsc(&currnode
->text
, currnode
->next
->name
);
640 info_rdaddsc(&currnode
->text
, "\n\n");
641 info_rdaddsc(&currnode
->text
, origtext
);
643 * Just make _absolutely_ sure we end with a newline.
645 if (currnode
->text
.output
.text
[currnode
->text
.output
.pos
-1] != '\n')
646 info_rdaddc(&currnode
->text
, '\n');
652 * Compute the offsets for the tag table.
654 filepos
= intro_text
.output
.pos
;
655 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
656 currnode
->pos
= filepos
;
657 filepos
+= currnode
->text
.output
.pos
;
661 * Split into sub-files.
663 if (conf
.maxfilesize
> 0) {
664 int currfilesize
= intro_text
.output
.pos
, currfilenum
= 1;
665 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
666 if (currfilesize
> intro_text
.output
.pos
&&
667 currfilesize
+ currnode
->text
.output
.pos
> conf
.maxfilesize
) {
669 currfilesize
= intro_text
.output
.pos
;
671 currnode
->filenum
= currfilenum
;
672 currfilesize
+= currnode
->text
.output
.pos
;
677 * Write the primary output file.
679 fp
= fopen(conf
.filename
, "w");
681 error(err_cantopenw
, conf
.filename
);
684 fputs(intro_text
.output
.text
, fp
);
685 if (conf
.maxfilesize
== 0) {
686 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
687 fputs(currnode
->text
.output
.text
, fp
);
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
,
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");
707 * Write the subfiles.
709 if (conf
.maxfilesize
> 0) {
713 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
714 if (filenum
!= currnode
->filenum
) {
717 filenum
= currnode
->filenum
;
721 fname
= snewn(strlen(conf
.filename
) + 40, char);
722 sprintf(fname
, "%s-%d", conf
.filename
, filenum
);
723 fp
= fopen(fname
, "w");
725 error(err_cantopenw
, fname
);
729 fputs(intro_text
.output
.text
, fp
);
731 fputs(currnode
->text
.output
.text
, fp
);
739 static int info_check_index(word
*w
, node
*n
, indexdata
*idx
)
743 for (; w
; w
= w
->next
) {
744 if (w
->type
== word_IndexRef
) {
748 tag
= index_findtag(idx
, w
->text
);
752 for (i
= 0; i
< tag
->nrefs
; i
++) {
753 indexentry
*entry
= tag
->refs
[i
];
754 info_idx
*ii
= (info_idx
*)entry
->backend_data
;
756 if (ii
->nnodes
> 0 && ii
->nodes
[ii
->nnodes
-1] == n
) {
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
766 if (ii
->nnodes
>= ii
->nodesize
) {
768 ii
->nodes
= sresize(ii
->nodes
, ii
->nodesize
, node
*);
771 ii
->nodes
[ii
->nnodes
++] = n
;
781 static word
*info_transform_wordlist(word
*words
, keywordlist
*keywords
)
783 word
*ret
= dup_word_list(words
);
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
);
792 if (kwl
->para
->type
== para_NumberedList
||
793 kwl
->para
->type
== para_BiblioCited
) {
795 * In Info, we do nothing special for xrefs to
796 * numbered list items or bibliography entries.
801 * An xref to a different section has its text
802 * completely replaced.
808 if (w2
->type
== word_XrefEnd
) {
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
828 w
->private_data
= kwl
->para
->private_data
;
829 assert(w
->private_data
);
838 static int info_rdaddwc(info_data
*id
, word
*words
, word
*end
, int xrefs
,
842 for (; words
&& words
!= end
; words
= words
->next
) switch (words
->type
) {
853 case word_WhiteSpace
:
856 case word_WkCodeSpace
:
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
))
866 ret
+= info_rdadds(id
, cfg
->startemph
);
867 else if (towordstyle(words
->type
) == word_Code
&&
868 (attraux(words
->aux
) == attr_First
||
869 attraux(words
->aux
) == attr_Only
))
870 ret
+= info_rdadds(id
, cfg
->lquote
);
871 if (removeattr(words
->type
) == word_Normal
) {
872 if (cvt_ok(id
->charset
, words
->text
) || !words
->alt
)
873 ret
+= info_rdadds(id
, words
->text
);
875 ret
+= info_rdaddwc(id
, words
->alt
, NULL
, FALSE
, cfg
);
876 } else if (removeattr(words
->type
) == word_WhiteSpace
) {
877 ret
+= info_rdadd(id
, L
' ');
878 } else if (removeattr(words
->type
) == word_Quote
) {
879 ret
+= info_rdadds(id
, quoteaux(words
->aux
) == quote_Open ?
880 cfg
->lquote
: cfg
->rquote
);
882 if (towordstyle(words
->type
) == word_Emph
&&
883 (attraux(words
->aux
) == attr_Last
||
884 attraux(words
->aux
) == attr_Only
))
885 ret
+= info_rdadds(id
, cfg
->endemph
);
886 else if (towordstyle(words
->type
) == word_Code
&&
887 (attraux(words
->aux
) == attr_Last
||
888 attraux(words
->aux
) == attr_Only
))
889 ret
+= info_rdadds(id
, cfg
->rquote
);
894 if (xrefs
&& words
->private_data
) {
896 * This bit is structural and so must be done in char
897 * rather than wchar_t.
899 ret
+= info_rdaddsc(id
, "*Note ");
900 ret
+= info_rdaddsc(id
, ((node
*)words
->private_data
)->name
);
901 ret
+= info_rdaddsc(id
, "::");
909 static int info_width_internal(word
*words
, int xrefs
, infoconfig
*cfg
);
911 static int info_width_internal_list(word
*words
, int xrefs
, infoconfig
*cfg
) {
914 w
+= info_width_internal(words
, xrefs
, cfg
);
920 static int info_width_internal(word
*words
, int xrefs
, infoconfig
*cfg
) {
924 switch (words
->type
) {
933 if (xrefs
&& words
->private_data
) {
934 /* "*Note " plus "::" comes to 8 characters */
935 return 8 + strwid(((node
*)words
->private_data
)->name
,
941 assert(words
->type
< word_internal_endattrs
);
944 attr
= towordstyle(words
->type
);
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
,
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
,
959 switch (words
->type
) {
964 if (cvt_ok(cfg
->charset
, words
->text
) || !words
->alt
)
965 wid
+= ustrwid(words
->text
, cfg
->charset
);
967 wid
+= info_width_internal_list(words
->alt
, xrefs
, cfg
);
970 case word_WhiteSpace
:
973 case word_WkCodeSpace
:
977 case word_WkCodeQuote
:
978 assert(words
->type
!= word_CodeQuote
&&
979 words
->type
!= word_WkCodeQuote
);
980 if (removeattr(words
->type
) == word_Quote
) {
981 if (quoteaux(words
->aux
) == quote_Open
)
982 wid
+= ustrwid(cfg
->lquote
, cfg
->charset
);
984 wid
+= ustrwid(cfg
->rquote
, cfg
->charset
);
991 static int info_width_noxrefs(void *ctx
, word
*words
)
993 return info_width_internal(words
, FALSE
, (infoconfig
*)ctx
);
995 static int info_width_xrefs(void *ctx
, word
*words
)
997 return info_width_internal(words
, TRUE
, (infoconfig
*)ctx
);
1000 static void info_heading(info_data
*text
, word
*tprefix
,
1001 word
*words
, alignstruct align
,
1002 int width
, infoconfig
*cfg
) {
1004 int firstlinewidth
, wrapwidth
;
1005 wrappedline
*wrapping
, *p
;
1009 length
+= info_rdaddwc(text
, tprefix
, NULL
, FALSE
, cfg
);
1010 length
+= info_rdadds(text
, cfg
->sectsuffix
);
1014 firstlinewidth
= width
- length
;
1016 wrapping
= wrap_para(words
, firstlinewidth
, wrapwidth
,
1017 info_width_noxrefs
, cfg
, 0);
1018 for (p
= wrapping
; p
; p
= p
->next
) {
1019 length
+= info_rdaddwc(text
, p
->begin
, p
->end
, FALSE
, cfg
);
1020 info_rdadd(text
, L
'\n');
1021 if (*align
.underline
) {
1022 while (length
> 0) {
1023 info_rdadds(text
, align
.underline
);
1024 length
-= ustrwid(align
.underline
, cfg
->charset
);
1026 info_rdadd(text
, L
'\n');
1030 wrap_free(wrapping
);
1031 info_rdadd(text
, L
'\n');
1034 static void info_rule(info_data
*text
, int indent
, int width
, infoconfig
*cfg
)
1036 while (indent
--) info_rdadd(text
, L
' ');
1038 info_rdadds(text
, cfg
->rule
);
1039 width
-= ustrwid(cfg
->rule
, cfg
->charset
);
1041 info_rdadd(text
, L
'\n');
1042 info_rdadd(text
, L
'\n');
1045 static void info_para(info_data
*text
, word
*prefix
, wchar_t *prefixextra
,
1046 word
*input
, keywordlist
*keywords
, int indent
,
1047 int extraindent
, int width
, infoconfig
*cfg
) {
1048 wrappedline
*wrapping
, *p
;
1052 int firstlinewidth
= width
;
1054 words
= info_transform_wordlist(input
, keywords
);
1057 for (i
= 0; i
< indent
; i
++)
1058 info_rdadd(text
, L
' ');
1059 e
= info_rdaddwc(text
, prefix
, NULL
, FALSE
, cfg
);
1061 e
+= info_rdadds(text
, prefixextra
);
1062 /* If the prefix is too long, shorten the first line to fit. */
1063 e
= extraindent
- e
;
1065 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
1066 if (firstlinewidth
< 0) {
1067 e
= indent
+ extraindent
;
1068 firstlinewidth
= width
;
1069 info_rdadd(text
, L
'\n');
1074 e
= indent
+ extraindent
;
1076 wrapping
= wrap_para(words
, firstlinewidth
, width
, info_width_xrefs
,
1078 for (p
= wrapping
; p
; p
= p
->next
) {
1079 for (i
= 0; i
< e
; i
++)
1080 info_rdadd(text
, L
' ');
1081 info_rdaddwc(text
, p
->begin
, p
->end
, TRUE
, cfg
);
1082 info_rdadd(text
, L
'\n');
1083 e
= indent
+ extraindent
;
1085 wrap_free(wrapping
);
1086 info_rdadd(text
, L
'\n');
1088 free_word_list(words
);
1091 static void info_codepara(info_data
*text
, word
*words
,
1092 int indent
, int width
) {
1095 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1096 for (i
= 0; i
< indent
; i
++)
1097 info_rdadd(text
, L
' ');
1098 if (info_rdadds(text
, words
->text
) > width
) {
1101 info_rdadd(text
, L
'\n');
1104 info_rdadd(text
, L
'\n');
1107 static void info_versionid(info_data
*text
, word
*words
, infoconfig
*cfg
) {
1108 info_rdadd(text
, L
'[');
1109 info_rdaddwc(text
, words
, NULL
, FALSE
, cfg
);
1110 info_rdadds(text
, L
"]\n");
1113 static node
*info_node_new(char *name
, int charset
)
1118 n
->text
= empty_info_data
;
1119 n
->text
.charset
= charset
;
1120 n
->up
= n
->next
= n
->prev
= n
->lastchild
= n
->listnext
= NULL
;
1121 n
->name
= dupstr(name
);
1122 n
->started_menu
= FALSE
;
1127 static char *info_node_name_core(info_data
*id
, filepos
*fpos
)
1132 * We cannot have commas, colons or parentheses in a node name.
1133 * Remove any that we find, with a warning.
1135 p
= q
= id
->output
.text
;
1137 if (*p
== ':' || *p
== ',' || *p
== '(' || *p
== ')') {
1138 error(err_infonodechar
, fpos
, *p
);
1146 return id
->output
.text
;
1149 static char *info_node_name_for_para(paragraph
*par
, infoconfig
*cfg
)
1151 info_data id
= EMPTY_INFO_DATA
;
1153 id
.charset
= cfg
->charset
;
1154 info_rdaddwc(&id
, par
->kwtext ? par
->kwtext
: par
->words
,
1156 info_rdaddsc(&id
, NULL
);
1158 return info_node_name_core(&id
, &par
->fpos
);
1161 static char *info_node_name_for_text(wchar_t *text
, infoconfig
*cfg
)
1163 info_data id
= EMPTY_INFO_DATA
;
1165 id
.charset
= cfg
->charset
;
1166 info_rdadds(&id
, text
);
1167 info_rdaddsc(&id
, NULL
);
1169 return info_node_name_core(&id
, NULL
);
1172 static void info_menu_item(info_data
*text
, node
*n
, paragraph
*p
,
1176 * FIXME: Depending on how we're doing node names in this info
1177 * file, we might want to do
1179 * * Node name:: Chapter title
1183 * * Chapter number: Node name.
1185 * This function mostly works in char rather than wchar_t,
1186 * because a menu item is a structural component.
1188 info_rdaddsc(text
, "* ");
1189 info_rdaddsc(text
, n
->name
);
1190 info_rdaddsc(text
, "::");
1192 info_rdaddc(text
, ' ');
1193 info_rdaddwc(text
, p
->words
, NULL
, FALSE
, cfg
);
1195 info_rdaddc(text
, '\n');
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.)
1208 static int info_rdadds(info_data
*d
, wchar_t const *wcs
)
1211 d
->state
= charset_init_state
;
1217 int len
, width
, ret
;
1219 width
= ustrwid(wcs
, d
->charset
);
1225 ret
= charset_from_unicode(&wcs
, &len
, buf
, lenof(buf
),
1226 d
->charset
, &d
->state
, NULL
);
1228 assert(len
< prevlen
);
1232 rdaddsc(&d
->output
, buf
);
1241 static int info_rdaddsc(info_data
*d
, char const *cs
)
1247 ret
= charset_from_unicode(NULL
, 0, buf
, lenof(buf
),
1248 d
->charset
, &d
->state
, NULL
);
1251 rdaddsc(&d
->output
, buf
);
1258 rdaddsc(&d
->output
, cs
);
1259 return strwid(cs
, d
->charset
);
1264 static int info_rdadd(info_data
*d
, wchar_t wc
)
1269 return info_rdadds(d
, wcs
);
1272 static int info_rdaddc(info_data
*d
, char c
)
1277 return info_rdaddsc(d
, cs
);