WinHelp backend segfaults when it finds a nonexistent cross-
[sgt/halibut] / bk_text.c
CommitLineData
d7482997 1/*
2 * text backend for Halibut
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8#include "halibut.h"
9
10typedef enum { LEFT, LEFTPLUS, CENTRE } alignment;
11typedef struct {
12 alignment align;
13 int just_numbers;
14 wchar_t underline;
63223c78 15 wchar_t *number_suffix;
d7482997 16} alignstruct;
17
18typedef struct {
19 int indent, indent_code;
20 int listindentbefore, listindentafter;
21 int width;
22 alignstruct atitle, achapter, *asect;
23 int nasect;
24 int include_version_id;
25 int indent_preambles;
26 word bullet;
27} textconfig;
28
29static int text_convert(wchar_t *, char **);
30
31static void text_heading(FILE *, word *, word *, word *, alignstruct, int,int);
32static void text_rule(FILE *, int, int);
33static void text_para(FILE *, word *, char *, word *, int, int, int);
34static void text_codepara(FILE *, word *, int, int);
35static void text_versionid(FILE *, word *);
36
37static alignment utoalign(wchar_t *p) {
38 if (!ustricmp(p, L"centre") || !ustricmp(p, L"center"))
39 return CENTRE;
40 if (!ustricmp(p, L"leftplus"))
41 return LEFTPLUS;
42 return LEFT;
43}
44
45static textconfig text_configure(paragraph *source) {
46 textconfig ret;
47
48 /*
49 * Non-negotiables.
50 */
51 ret.bullet.next = NULL;
52 ret.bullet.alt = NULL;
53 ret.bullet.type = word_Normal;
54 ret.atitle.just_numbers = FALSE; /* ignored */
55
56 /*
57 * Defaults.
58 */
59 ret.indent = 7;
60 ret.indent_code = 2;
61 ret.listindentbefore = 1;
62 ret.listindentafter = 3;
63 ret.width = 68;
64 ret.atitle.align = CENTRE;
65 ret.atitle.underline = L'=';
66 ret.achapter.align = LEFT;
67 ret.achapter.just_numbers = FALSE;
63223c78 68 ret.achapter.number_suffix = ustrdup(L": ");
d7482997 69 ret.achapter.underline = L'-';
70 ret.nasect = 1;
71 ret.asect = mknewa(alignstruct, ret.nasect);
72 ret.asect[0].align = LEFTPLUS;
73 ret.asect[0].just_numbers = TRUE;
63223c78 74 ret.asect[0].number_suffix = ustrdup(L" ");
d7482997 75 ret.asect[0].underline = L'\0';
76 ret.include_version_id = TRUE;
77 ret.indent_preambles = FALSE;
78 ret.bullet.text = ustrdup(L"-");
79
80 for (; source; source = source->next) {
81 if (source->type == para_Config) {
82 if (!ustricmp(source->keyword, L"text-indent")) {
83 ret.indent = utoi(uadv(source->keyword));
84 } else if (!ustricmp(source->keyword, L"text-indent-code")) {
85 ret.indent_code = utoi(uadv(source->keyword));
86 } else if (!ustricmp(source->keyword, L"text-width")) {
87 ret.width = utoi(uadv(source->keyword));
88 } else if (!ustricmp(source->keyword, L"text-list-indent")) {
89 ret.listindentbefore = utoi(uadv(source->keyword));
90 } else if (!ustricmp(source->keyword, L"text-listitem-indent")) {
91 ret.listindentafter = utoi(uadv(source->keyword));
92 } else if (!ustricmp(source->keyword, L"text-chapter-align")) {
93 ret.achapter.align = utoalign(uadv(source->keyword));
94 } else if (!ustricmp(source->keyword, L"text-chapter-underline")) {
95 ret.achapter.underline = *uadv(source->keyword);
96 } else if (!ustricmp(source->keyword, L"text-chapter-numeric")) {
c83c6495 97 ret.achapter.just_numbers = utob(uadv(source->keyword));
63223c78 98 } else if (!ustricmp(source->keyword, L"text-chapter-suffix")) {
a0f2c111 99 ret.achapter.number_suffix = ustrdup(uadv(source->keyword));
d7482997 100 } else if (!ustricmp(source->keyword, L"text-section-align")) {
101 wchar_t *p = uadv(source->keyword);
102 int n = 0;
103 if (uisdigit(*p)) {
104 n = utoi(p);
105 p = uadv(p);
106 }
107 if (n >= ret.nasect) {
108 int i;
109 ret.asect = resize(ret.asect, n+1);
110 for (i = ret.nasect; i <= n; i++)
111 ret.asect[i] = ret.asect[ret.nasect-1];
112 ret.nasect = n+1;
113 }
114 ret.asect[n].align = utoalign(p);
115 } else if (!ustricmp(source->keyword, L"text-section-underline")) {
116 wchar_t *p = uadv(source->keyword);
117 int n = 0;
118 if (uisdigit(*p)) {
119 n = utoi(p);
120 p = uadv(p);
121 }
122 if (n >= ret.nasect) {
123 int i;
124 ret.asect = resize(ret.asect, n+1);
125 for (i = ret.nasect; i <= n; i++)
126 ret.asect[i] = ret.asect[ret.nasect-1];
127 ret.nasect = n+1;
128 }
129 ret.asect[n].underline = *p;
130 } else if (!ustricmp(source->keyword, L"text-section-numeric")) {
131 wchar_t *p = uadv(source->keyword);
132 int n = 0;
133 if (uisdigit(*p)) {
134 n = utoi(p);
135 p = uadv(p);
136 }
137 if (n >= ret.nasect) {
138 int i;
139 ret.asect = resize(ret.asect, n+1);
140 for (i = ret.nasect; i <= n; i++)
141 ret.asect[i] = ret.asect[ret.nasect-1];
142 ret.nasect = n+1;
143 }
144 ret.asect[n].just_numbers = utob(p);
63223c78 145 } else if (!ustricmp(source->keyword, L"text-section-suffix")) {
146 wchar_t *p = uadv(source->keyword);
147 int n = 0;
148 if (uisdigit(*p)) {
149 n = utoi(p);
150 p = uadv(p);
151 }
152 if (n >= ret.nasect) {
153 int i;
154 ret.asect = resize(ret.asect, n+1);
155 for (i = ret.nasect; i <= n; i++)
156 ret.asect[i] = ret.asect[ret.nasect-1];
157 ret.nasect = n+1;
158 }
a0f2c111 159 ret.asect[n].number_suffix = ustrdup(p);
d7482997 160 } else if (!ustricmp(source->keyword, L"text-title-align")) {
161 ret.atitle.align = utoalign(uadv(source->keyword));
162 } else if (!ustricmp(source->keyword, L"text-title-underline")) {
163 ret.atitle.underline = *uadv(source->keyword);
164 } else if (!ustricmp(source->keyword, L"text-versionid")) {
165 ret.include_version_id = utob(uadv(source->keyword));
166 } else if (!ustricmp(source->keyword, L"text-indent-preamble")) {
167 ret.indent_preambles = utob(uadv(source->keyword));
168 } else if (!ustricmp(source->keyword, L"text-bullet")) {
169 ret.bullet.text = uadv(source->keyword);
170 }
171 }
172 }
173
174 return ret;
175}
176
177void text_backend(paragraph *sourceform, keywordlist *keywords,
178 indexdata *idx) {
179 paragraph *p;
180 textconfig conf;
181 word *prefix, *body, *wp;
182 word spaceword;
183 FILE *fp;
184 char *prefixextra;
185 int indentb, indenta;
186
187 IGNORE(keywords); /* we don't happen to need this */
188 IGNORE(idx); /* or this */
189
190 conf = text_configure(sourceform);
191
192 /*
193 * Determine the output file name, and open the output file
194 *
195 * FIXME: want configurable output file names here. For the
196 * moment, we'll just call it `output.txt'.
197 */
198 fp = fopen("output.txt", "w");
199 if (!fp) {
200 error(err_cantopenw, "output.txt");
201 return;
202 }
203
204 /* Do the title */
205 for (p = sourceform; p; p = p->next)
206 if (p->type == para_Title)
207 text_heading(fp, NULL, NULL, p->words,
208 conf.atitle, conf.indent, conf.width);
209
210 /* Do the preamble and copyright */
211 for (p = sourceform; p; p = p->next)
212 if (p->type == para_Preamble)
213 text_para(fp, NULL, NULL, p->words,
214 conf.indent_preambles ? conf.indent : 0, 0,
215 conf.width + (conf.indent_preambles ? 0 : conf.indent));
216 for (p = sourceform; p; p = p->next)
217 if (p->type == para_Copyright)
218 text_para(fp, NULL, NULL, p->words,
219 conf.indent_preambles ? conf.indent : 0, 0,
220 conf.width + (conf.indent_preambles ? 0 : conf.indent));
221
222 /* Do the main document */
223 for (p = sourceform; p; p = p->next) switch (p->type) {
224
225 /*
226 * Things we ignore because we've already processed them or
227 * aren't going to touch them in this pass.
228 */
229 case para_IM:
230 case para_BR:
231 case para_Biblio: /* only touch BiblioCited */
232 case para_VersionID:
233 case para_Copyright:
234 case para_Preamble:
235 case para_NoCite:
236 case para_Title:
237 break;
238
239 /*
240 * Chapter titles.
241 */
242 case para_Chapter:
243 case para_Appendix:
244 case para_UnnumberedChapter:
245 text_heading(fp, p->kwtext, p->kwtext2, p->words,
246 conf.achapter, conf.indent, conf.width);
247 break;
248
249 case para_Heading:
250 case para_Subsect:
251 text_heading(fp, p->kwtext, p->kwtext2, p->words,
252 conf.asect[p->aux>=conf.nasect ? conf.nasect-1 : p->aux],
253 conf.indent, conf.width);
254 break;
255
256 case para_Rule:
257 text_rule(fp, conf.indent, conf.width);
258 break;
259
260 case para_Normal:
261 case para_BiblioCited:
262 case para_Bullet:
263 case para_NumberedList:
264 if (p->type == para_Bullet) {
265 prefix = &conf.bullet;
266 prefixextra = NULL;
267 indentb = conf.listindentbefore;
268 indenta = conf.listindentafter;
269 } else if (p->type == para_NumberedList) {
270 prefix = p->kwtext;
271 prefixextra = "."; /* FIXME: configurability */
272 indentb = conf.listindentbefore;
273 indenta = conf.listindentafter;
274 } else {
275 prefix = NULL;
276 prefixextra = NULL;
277 indentb = indenta = 0;
278 }
279 if (p->type == para_BiblioCited) {
280 body = dup_word_list(p->kwtext);
281 for (wp = body; wp->next; wp = wp->next);
282 wp->next = &spaceword;
283 spaceword.next = p->words;
284 spaceword.alt = NULL;
285 spaceword.type = word_WhiteSpace;
286 spaceword.text = NULL;
287 } else {
288 wp = NULL;
289 body = p->words;
290 }
291 text_para(fp, prefix, prefixextra, body,
c83c6495 292 conf.indent + indentb, indenta,
293 conf.width - indentb - indenta);
d7482997 294 if (wp) {
295 wp->next = NULL;
296 free_word_list(body);
297 }
298 break;
299
300 case para_Code:
301 text_codepara(fp, p->words, conf.indent + conf.indent_code, conf.width - 2 * conf.indent_code);
302 break;
303 }
304
305 /* Do the version ID */
306 if (conf.include_version_id) {
307 for (p = sourceform; p; p = p->next)
308 if (p->type == para_VersionID)
309 text_versionid(fp, p->words);
310 }
311
312 /*
313 * Tidy up
314 */
315 fclose(fp);
677e18a2 316 {
317 int i;
318 sfree(conf.achapter.number_suffix);
319 for (i = 0; i < conf.nasect; i++)
320 sfree(conf.asect[i].number_suffix);
321 sfree(conf.asect);
322 sfree(conf.bullet.text);
323 }
d7482997 324}
325
326/*
327 * Convert a wide string into a string of chars. If `result' is
328 * non-NULL, mallocs the resulting string and stores a pointer to
329 * it in `*result'. If `result' is NULL, merely checks whether all
330 * characters in the string are feasible for the output character
331 * set.
332 *
333 * Return is nonzero if all characters are OK. If not all
334 * characters are OK but `result' is non-NULL, a result _will_
335 * still be generated!
336 */
337static int text_convert(wchar_t *s, char **result) {
338 /*
339 * FIXME. Currently this is ISO8859-1 only.
340 */
341 int doing = (result != 0);
342 int ok = TRUE;
343 char *p = NULL;
344 int plen = 0, psize = 0;
345
346 for (; *s; s++) {
347 wchar_t c = *s;
348 char outc;
349
350 if ((c >= 32 && c <= 126) ||
351 (c >= 160 && c <= 255)) {
352 /* Char is OK. */
353 outc = (char)c;
354 } else {
355 /* Char is not OK. */
356 ok = FALSE;
357 outc = 0xBF; /* approximate the good old DEC `uh?' */
358 }
359 if (doing) {
360 if (plen >= psize) {
361 psize = plen + 256;
362 p = resize(p, psize);
363 }
364 p[plen++] = outc;
365 }
366 }
367 if (doing) {
368 p = resize(p, plen+1);
369 p[plen] = '\0';
370 *result = p;
371 }
372 return ok;
373}
374
375static void text_rdaddwc(rdstringc *rs, word *text, word *end) {
376 char *c;
377
378 for (; text && text != end; text = text->next) switch (text->type) {
379 case word_HyperLink:
380 case word_HyperEnd:
381 case word_UpperXref:
382 case word_LowerXref:
383 case word_XrefEnd:
384 case word_IndexRef:
385 break;
386
387 case word_Normal:
388 case word_Emph:
389 case word_Code:
390 case word_WeakCode:
391 case word_WhiteSpace:
392 case word_EmphSpace:
393 case word_CodeSpace:
394 case word_WkCodeSpace:
395 case word_Quote:
396 case word_EmphQuote:
397 case word_CodeQuote:
398 case word_WkCodeQuote:
399 assert(text->type != word_CodeQuote &&
400 text->type != word_WkCodeQuote);
401 if (towordstyle(text->type) == word_Emph &&
402 (attraux(text->aux) == attr_First ||
403 attraux(text->aux) == attr_Only))
404 rdaddc(rs, '_'); /* FIXME: configurability */
405 else if (towordstyle(text->type) == word_Code &&
406 (attraux(text->aux) == attr_First ||
407 attraux(text->aux) == attr_Only))
408 rdaddc(rs, '`'); /* FIXME: configurability */
409 if (removeattr(text->type) == word_Normal) {
410 if (text_convert(text->text, &c))
411 rdaddsc(rs, c);
412 else
413 text_rdaddwc(rs, text->alt, NULL);
414 sfree(c);
415 } else if (removeattr(text->type) == word_WhiteSpace) {
416 rdaddc(rs, ' ');
417 } else if (removeattr(text->type) == word_Quote) {
418 rdaddc(rs, quoteaux(text->aux) == quote_Open ? '`' : '\'');
419 /* FIXME: configurability */
420 }
421 if (towordstyle(text->type) == word_Emph &&
422 (attraux(text->aux) == attr_Last ||
423 attraux(text->aux) == attr_Only))
424 rdaddc(rs, '_'); /* FIXME: configurability */
425 else if (towordstyle(text->type) == word_Code &&
426 (attraux(text->aux) == attr_Last ||
427 attraux(text->aux) == attr_Only))
428 rdaddc(rs, '\''); /* FIXME: configurability */
429 break;
430 }
431}
432
433static int text_width(word *);
434
435static int text_width_list(word *text) {
436 int w = 0;
437 while (text) {
438 w += text_width(text);
439 text = text->next;
440 }
441 return w;
442}
443
444static int text_width(word *text) {
445 switch (text->type) {
446 case word_HyperLink:
447 case word_HyperEnd:
448 case word_UpperXref:
449 case word_LowerXref:
450 case word_XrefEnd:
451 case word_IndexRef:
452 return 0;
453
454 case word_Normal:
455 case word_Emph:
456 case word_Code:
457 case word_WeakCode:
458 return (((text->type == word_Emph ||
459 text->type == word_Code)
460 ? (attraux(text->aux) == attr_Only ? 2 :
461 attraux(text->aux) == attr_Always ? 0 : 1)
462 : 0) +
463 (text_convert(text->text, NULL) ?
464 ustrlen(text->text) :
465 text_width_list(text->alt)));
466
467 case word_WhiteSpace:
468 case word_EmphSpace:
469 case word_CodeSpace:
470 case word_WkCodeSpace:
471 case word_Quote:
472 case word_EmphQuote:
473 case word_CodeQuote:
474 case word_WkCodeQuote:
475 assert(text->type != word_CodeQuote &&
476 text->type != word_WkCodeQuote);
477 return (((towordstyle(text->type) == word_Emph ||
478 towordstyle(text->type) == word_Code)
479 ? (attraux(text->aux) == attr_Only ? 2 :
480 attraux(text->aux) == attr_Always ? 0 : 1)
481 : 0) + 1);
482 }
483 return 0; /* should never happen */
484}
485
486static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text,
487 alignstruct align, int indent, int width) {
488 rdstringc t = { 0, 0, NULL };
489 int margin, length;
490 int firstlinewidth, wrapwidth;
491 wrappedline *wrapping, *p;
492
493 if (align.just_numbers && nprefix) {
63223c78 494 char *c;
d7482997 495 text_rdaddwc(&t, nprefix, NULL);
63223c78 496 if (text_convert(align.number_suffix, &c)) {
497 rdaddsc(&t, c);
498 sfree(c);
499 }
d7482997 500 } else if (!align.just_numbers && tprefix) {
63223c78 501 char *c;
d7482997 502 text_rdaddwc(&t, tprefix, NULL);
63223c78 503 if (text_convert(align.number_suffix, &c)) {
504 rdaddsc(&t, c);
505 sfree(c);
506 }
d7482997 507 }
508 margin = length = (t.text ? strlen(t.text) : 0);
509
510 if (align.align == LEFTPLUS) {
511 margin = indent - margin;
512 if (margin < 0) margin = 0;
513 firstlinewidth = indent + width - margin - length;
514 wrapwidth = width;
515 } else if (align.align == LEFT || align.align == CENTRE) {
516 margin = 0;
517 firstlinewidth = indent + width - length;
518 wrapwidth = indent + width;
519 }
520
521 wrapping = wrap_para(text, firstlinewidth, wrapwidth, text_width);
522 for (p = wrapping; p; p = p->next) {
523 text_rdaddwc(&t, p->begin, p->end);
524 length = (t.text ? strlen(t.text) : 0);
525 if (align.align == CENTRE) {
526 margin = (indent + width - length)/2;
527 if (margin < 0) margin = 0;
528 }
529 fprintf(fp, "%*s%s\n", margin, "", t.text);
530 if (align.underline != L'\0') {
531 char *u, uc;
532 wchar_t uw[2];
533 uw[0] = align.underline; uw[1] = L'\0';
534 text_convert(uw, &u);
535 uc = u[0];
536 sfree(u);
537 fprintf(fp, "%*s", margin, "");
538 while (length--)
539 putc(uc, fp);
540 putc('\n', fp);
541 }
542 if (align.align == LEFTPLUS)
543 margin = indent;
544 else
545 margin = 0;
546 sfree(t.text);
547 t = empty_rdstringc;
548 }
549 wrap_free(wrapping);
550 putc('\n', fp);
551
552 sfree(t.text);
553}
554
555static void text_rule(FILE *fp, int indent, int width) {
556 while (indent--) putc(' ', fp);
557 while (width--) putc('-', fp); /* FIXME: configurability! */
558 putc('\n', fp);
559 putc('\n', fp);
560}
561
562static void text_para(FILE *fp, word *prefix, char *prefixextra, word *text,
563 int indent, int extraindent, int width) {
564 wrappedline *wrapping, *p;
565 rdstringc pfx = { 0, 0, NULL };
566 int e;
567 int firstlinewidth = width;
568
569 if (prefix) {
570 text_rdaddwc(&pfx, prefix, NULL);
571 if (prefixextra)
572 rdaddsc(&pfx, prefixextra);
573 fprintf(fp, "%*s%s", indent, "", pfx.text);
c83c6495 574 /* If the prefix is too long, shorten the first line to fit. */
d7482997 575 e = extraindent - strlen(pfx.text);
576 if (e < 0) {
c83c6495 577 firstlinewidth += e; /* this decreases it, since e < 0 */
d7482997 578 if (firstlinewidth < 0) {
579 e = indent + extraindent;
580 firstlinewidth = width;
581 fprintf(fp, "\n");
c83c6495 582 } else
583 e = 0;
d7482997 584 }
585 sfree(pfx.text);
586 } else
587 e = indent + extraindent;
588
589 wrapping = wrap_para(text, firstlinewidth, width, text_width);
590 for (p = wrapping; p; p = p->next) {
591 rdstringc t = { 0, 0, NULL };
592 text_rdaddwc(&t, p->begin, p->end);
593 fprintf(fp, "%*s%s\n", e, "", t.text);
594 e = indent + extraindent;
595 sfree(t.text);
596 }
597 wrap_free(wrapping);
598 putc('\n', fp);
599}
600
601static void text_codepara(FILE *fp, word *text, int indent, int width) {
602 for (; text; text = text->next) if (text->type == word_WeakCode) {
603 char *c;
604 text_convert(text->text, &c);
605 if (strlen(c) > (size_t)width) {
606 /* FIXME: warn */
607 }
608 fprintf(fp, "%*s%s\n", indent, "", c);
609 sfree(c);
610 }
611
612 putc('\n', fp);
613}
614
615static void text_versionid(FILE *fp, word *text) {
616 rdstringc t = { 0, 0, NULL };
617
618 rdaddc(&t, '['); /* FIXME: configurability */
619 text_rdaddwc(&t, text, NULL);
620 rdaddc(&t, ']'); /* FIXME: configurability */
621
622 fprintf(fp, "%s\n", t.text);
623 sfree(t.text);
624}