Add tests of ` and ' characters, since they need special handling in the man backend.
[sgt/halibut] / bk_man.c
CommitLineData
7136a6c7 1/*
2 * man page backend for Halibut
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8#include "halibut.h"
9
4b3c5afb 10typedef struct {
11 wchar_t *th;
12 int headnumbers;
13 int mindepth;
50d6b4bd 14 char *filename;
93688997 15 int charset;
b0ea3acd 16 wchar_t *bullet, *lquote, *rquote;
4b3c5afb 17} manconfig;
18
b0ea3acd 19static void man_text(FILE *, word *,
20 int newline, int quote_props, manconfig *conf);
21static void man_codepara(FILE *, word *, int charset);
22static int man_convert(wchar_t const *s, int maxlen,
23 char **result, int quote_props,
24 int charset, charset_state *state);
25
4b3c5afb 26static manconfig man_configure(paragraph *source) {
b0ea3acd 27 paragraph *p;
4b3c5afb 28 manconfig ret;
29
30 /*
31 * Defaults.
32 */
33 ret.th = NULL;
34 ret.headnumbers = FALSE;
35 ret.mindepth = 0;
50d6b4bd 36 ret.filename = dupstr("output.1");
93688997 37 ret.charset = CS_ASCII;
b0ea3acd 38 ret.bullet = L"\x2022\0o\0\0";
39 ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0";
40 ret.rquote = uadv(ret.lquote);
4b3c5afb 41
b0ea3acd 42 /*
43 * Two-pass configuration so that we can pick up global config
44 * (e.g. `quotes') before having it overridden by specific
c5546514 45 * config (`man-quotes'), irrespective of the order in which
b0ea3acd 46 * they occur.
47 */
48 for (p = source; p; p = p->next) {
49 if (p->type == para_Config) {
50 if (!ustricmp(p->keyword, L"quotes")) {
51 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
52 ret.lquote = uadv(p->keyword);
53 ret.rquote = uadv(ret.lquote);
54 }
55 }
56 }
57 }
58
59 for (p = source; p; p = p->next) {
60 if (p->type == para_Config) {
61 if (!ustricmp(p->keyword, L"man-identity")) {
4b3c5afb 62 wchar_t *wp, *ep;
63
b0ea3acd 64 wp = uadv(p->keyword);
4b3c5afb 65 ep = wp;
66 while (*ep)
67 ep = uadv(ep);
50d6b4bd 68 sfree(ret.th);
f1530049 69 ret.th = snewn(ep - wp + 1, wchar_t);
4b3c5afb 70 memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t));
b0ea3acd 71 } else if (!ustricmp(p->keyword, L"man-charset")) {
0960a3d8 72 ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword));
b0ea3acd 73 } else if (!ustricmp(p->keyword, L"man-headnumbers")) {
74 ret.headnumbers = utob(uadv(p->keyword));
75 } else if (!ustricmp(p->keyword, L"man-mindepth")) {
76 ret.mindepth = utoi(uadv(p->keyword));
77 } else if (!ustricmp(p->keyword, L"man-filename")) {
50d6b4bd 78 sfree(ret.filename);
b0ea3acd 79 ret.filename = dupstr(adv(p->origkeyword));
80 } else if (!ustricmp(p->keyword, L"man-bullet")) {
81 ret.bullet = uadv(p->keyword);
c5546514 82 } else if (!ustricmp(p->keyword, L"man-quotes")) {
b0ea3acd 83 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
84 ret.lquote = uadv(p->keyword);
85 ret.rquote = uadv(ret.lquote);
86 }
4b3c5afb 87 }
88 }
89 }
90
b0ea3acd 91 /*
92 * Now process fallbacks on quote characters and bullets.
93 */
94 while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
95 (!cvt_ok(ret.charset, ret.lquote) ||
96 !cvt_ok(ret.charset, ret.rquote))) {
97 ret.lquote = uadv(ret.rquote);
98 ret.rquote = uadv(ret.lquote);
99 }
100
101 while (*ret.bullet && *uadv(ret.bullet) &&
102 !cvt_ok(ret.charset, ret.bullet))
103 ret.bullet = uadv(ret.bullet);
104
4b3c5afb 105 return ret;
106}
107
108static void man_conf_cleanup(manconfig cf)
109{
110 sfree(cf.th);
50d6b4bd 111 sfree(cf.filename);
4b3c5afb 112}
7136a6c7 113
ba9c1487 114paragraph *man_config_filename(char *filename)
115{
e4ea58f8 116 return cmdline_cfg_simple("man-filename", filename, NULL);
ba9c1487 117}
118
7136a6c7 119#define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
120#define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
121
122void man_backend(paragraph *sourceform, keywordlist *keywords,
43341922 123 indexdata *idx, void *unused) {
7136a6c7 124 paragraph *p;
125 FILE *fp;
4b3c5afb 126 manconfig conf;
02478c4f 127 int had_described_thing;
7136a6c7 128
43341922 129 IGNORE(unused);
130 IGNORE(keywords);
131 IGNORE(idx);
7136a6c7 132
4b3c5afb 133 conf = man_configure(sourceform);
134
7136a6c7 135 /*
50d6b4bd 136 * Open the output file.
7136a6c7 137 */
50d6b4bd 138 fp = fopen(conf.filename, "w");
7136a6c7 139 if (!fp) {
50d6b4bd 140 error(err_cantopenw, conf.filename);
7136a6c7 141 return;
142 }
143
144 /* Do the version ID */
145 for (p = sourceform; p; p = p->next)
146 if (p->type == para_VersionID) {
147 fprintf(fp, ".\\\" ");
b0ea3acd 148 man_text(fp, p->words, TRUE, 0, &conf);
7136a6c7 149 }
150
4b3c5afb 151 /* .TH name-of-program manual-section */
22905f72 152 fprintf(fp, ".TH");
153 if (conf.th && *conf.th) {
4b3c5afb 154 char *c;
22905f72 155 wchar_t *wp;
156
157 for (wp = conf.th; *wp; wp = uadv(wp)) {
158 fputs(" \"", fp);
93688997 159 man_convert(wp, 0, &c, QUOTE_QUOTES, conf.charset, NULL);
22905f72 160 fputs(c, fp);
161 sfree(c);
162 fputc('"', fp);
4b3c5afb 163 }
164 }
22905f72 165 fputc('\n', fp);
7136a6c7 166
02478c4f 167 had_described_thing = FALSE;
168#define cleanup_described_thing do { \
169 if (had_described_thing) \
170 fprintf(fp, "\n"); \
171 had_described_thing = FALSE; \
172} while (0)
173
7136a6c7 174 for (p = sourceform; p; p = p->next) switch (p->type) {
175 /*
176 * Things we ignore because we've already processed them or
177 * aren't going to touch them in this pass.
178 */
179 case para_IM:
180 case para_BR:
181 case para_Biblio: /* only touch BiblioCited */
182 case para_VersionID:
7136a6c7 183 case para_NoCite:
184 case para_Title:
185 break;
186
187 /*
188 * Headings.
189 */
190 case para_Chapter:
191 case para_Appendix:
192 case para_UnnumberedChapter:
193 case para_Heading:
194 case para_Subsect:
8902e0ed 195
02478c4f 196 cleanup_described_thing;
4b3c5afb 197 {
198 int depth;
199 if (p->type == para_Subsect)
e4f8c48e 200 depth = p->aux + 1;
4b3c5afb 201 else if (p->type == para_Heading)
202 depth = 1;
203 else
204 depth = 0;
205 if (depth >= conf.mindepth) {
4118fc81 206 if (depth > conf.mindepth)
207 fprintf(fp, ".SS \"");
208 else
209 fprintf(fp, ".SH \"");
4b3c5afb 210 if (conf.headnumbers && p->kwtext) {
b0ea3acd 211 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
4b3c5afb 212 fprintf(fp, " ");
213 }
b0ea3acd 214 man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf);
4b3c5afb 215 fprintf(fp, "\"\n");
216 }
217 break;
218 }
7136a6c7 219
220 /*
221 * Code paragraphs.
222 */
223 case para_Code:
02478c4f 224 cleanup_described_thing;
7136a6c7 225 fprintf(fp, ".PP\n");
93688997 226 man_codepara(fp, p->words, conf.charset);
7136a6c7 227 break;
228
229 /*
230 * Normal paragraphs.
231 */
232 case para_Normal:
9057a0a8 233 case para_Copyright:
02478c4f 234 cleanup_described_thing;
7136a6c7 235 fprintf(fp, ".PP\n");
b0ea3acd 236 man_text(fp, p->words, TRUE, 0, &conf);
7136a6c7 237 break;
238
239 /*
240 * List paragraphs.
241 */
242 case para_Description:
243 case para_BiblioCited:
244 case para_Bullet:
245 case para_NumberedList:
02478c4f 246 if (p->type != para_Description)
247 cleanup_described_thing;
248
7136a6c7 249 if (p->type == para_Bullet) {
b0ea3acd 250 char *bullettext;
251 man_convert(conf.bullet, -1, &bullettext, QUOTE_QUOTES,
252 conf.charset, NULL);
253 fprintf(fp, ".IP \"\\fB%s\\fP\"\n", bullettext);
254 sfree(bullettext);
7136a6c7 255 } else if (p->type == para_NumberedList) {
256 fprintf(fp, ".IP \"");
b0ea3acd 257 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
7136a6c7 258 fprintf(fp, "\"\n");
259 } else if (p->type == para_Description) {
02478c4f 260 if (had_described_thing) {
261 /*
262 * Do nothing; the .xP for this paragraph is the
263 * .IP which has come before it in the
264 * DescribedThing.
265 */
266 } else {
267 /*
268 * A \dd without a preceding \dt is given a blank
269 * one.
270 */
271 fprintf(fp, ".IP \"\"\n");
272 }
7136a6c7 273 } else if (p->type == para_BiblioCited) {
274 fprintf(fp, ".IP \"");
b0ea3acd 275 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
7136a6c7 276 fprintf(fp, "\"\n");
277 }
b0ea3acd 278 man_text(fp, p->words, TRUE, 0, &conf);
02478c4f 279 had_described_thing = FALSE;
7136a6c7 280 break;
281
282 case para_DescribedThing:
02478c4f 283 cleanup_described_thing;
7136a6c7 284 fprintf(fp, ".IP \"");
b0ea3acd 285 man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf);
7136a6c7 286 fprintf(fp, "\"\n");
02478c4f 287 had_described_thing = TRUE;
7136a6c7 288 break;
289
290 case para_Rule:
291 /*
75008d04 292 * New paragraph containing a horizontal line 1/2em above the
293 * baseline whose length is the line length minus the current
294 * indent.
7136a6c7 295 */
02478c4f 296 cleanup_described_thing;
75008d04 297 fprintf(fp, ".PP\n\\u\\l'\\n(.lu-\\n(.iu'\\d\n");
7136a6c7 298 break;
299
300 case para_LcontPush:
2614b01d 301 case para_QuotePush:
02478c4f 302 cleanup_described_thing;
7136a6c7 303 fprintf(fp, ".RS\n");
304 break;
305 case para_LcontPop:
2614b01d 306 case para_QuotePop:
02478c4f 307 cleanup_described_thing;
7136a6c7 308 fprintf(fp, ".RE\n");
309 break;
310 }
02478c4f 311 cleanup_described_thing;
7136a6c7 312
313 /*
314 * Tidy up.
315 */
316 fclose(fp);
4b3c5afb 317 man_conf_cleanup(conf);
7136a6c7 318}
319
320/*
93688997 321 * Convert a wide string into a string of chars; mallocs the
322 * resulting string and stores a pointer to it in `*result'.
323 *
324 * If `state' is non-NULL, updates the charset state pointed to. If
325 * `state' is NULL, this function uses its own state, initialises
326 * it from scratch, and cleans it up when finished. If `state' is
327 * non-NULL but _s_ is NULL, cleans up a provided state.
7136a6c7 328 *
329 * Return is nonzero if all characters are OK. If not all
330 * characters are OK but `result' is non-NULL, a result _will_
331 * still be generated!
332 *
93688997 333 * This function also does escaping of groff special characters.
7136a6c7 334 */
93688997 335static int man_convert(wchar_t const *s, int maxlen,
336 char **result, int quote_props,
337 int charset, charset_state *state) {
338 charset_state internal_state = CHARSET_INIT_STATE;
339 int slen, err;
340 char *p = NULL, *q;
7136a6c7 341 int plen = 0, psize = 0;
93688997 342 rdstringc out = {0, 0, NULL};
7136a6c7 343
93688997 344 if (!state)
345 state = &internal_state;
346
347 slen = (s ? ustrlen(s) : 0);
348
349 if (slen > maxlen && maxlen > 0)
350 slen = maxlen;
351
352 psize = 384;
353 plen = 0;
f1530049 354 p = snewn(psize, char);
93688997 355 err = 0;
356
357 while (slen > 0) {
358 int ret = charset_from_unicode(&s, &slen, p+plen, psize-plen,
359 charset, state, (err ? NULL : &err));
360 if (ret > 0) {
361 plen += ret;
362 if (psize - plen < 256) {
7136a6c7 363 psize = plen + 256;
f1530049 364 p = sresize(p, psize, char);
7136a6c7 365 }
7136a6c7 366 }
367 }
93688997 368
369 if (state == &internal_state || s == NULL) {
370 int ret = charset_from_unicode(NULL, 0, p+plen, psize-plen,
371 charset, state, NULL);
372 if (ret > 0)
373 plen += ret;
7136a6c7 374 }
93688997 375
376 for (q = p; q < p+plen; q++) {
377 if (q == p && (*q == '.' || *q == '\'') &&
378 (quote_props & QUOTE_INITCTRL)) {
379 /*
380 * Control character (. or ') at the start of a
381 * line. Quote it by putting \& (troff zero-width
382 * space) before it.
383 */
384 rdaddc(&out, '\\');
385 rdaddc(&out, '&');
386 } else if (*q == '\\') {
387 /*
388 * Quote backslashes by doubling them, always.
389 */
390 rdaddc(&out, '\\');
391 } else if (*q == '"' && (quote_props & QUOTE_QUOTES)) {
392 /*
393 * Double quote within double quotes. Quote it by
394 * doubling.
395 */
396 rdaddc(&out, '"');
397 }
398 rdaddc(&out, *q);
399 }
400
401 sfree(p);
402
403 if (out.text)
404 *result = rdtrimc(&out);
405 else
406 *result = dupstr("");
407
408 return !err;
7136a6c7 409}
410
a1677855 411static int man_rdaddwc(rdstringc *rs, word *text, word *end,
412 int quote_props, manconfig *conf,
413 charset_state *state) {
7136a6c7 414 char *c;
415
416 for (; text && text != end; text = text->next) switch (text->type) {
417 case word_HyperLink:
418 case word_HyperEnd:
419 case word_UpperXref:
420 case word_LowerXref:
421 case word_XrefEnd:
422 case word_IndexRef:
423 break;
424
425 case word_Normal:
426 case word_Emph:
427 case word_Code:
428 case word_WeakCode:
429 case word_WhiteSpace:
430 case word_EmphSpace:
431 case word_CodeSpace:
432 case word_WkCodeSpace:
433 case word_Quote:
434 case word_EmphQuote:
435 case word_CodeQuote:
436 case word_WkCodeQuote:
437 assert(text->type != word_CodeQuote &&
438 text->type != word_WkCodeQuote);
93688997 439
7136a6c7 440 if (towordstyle(text->type) == word_Emph &&
441 (attraux(text->aux) == attr_First ||
93688997 442 attraux(text->aux) == attr_Only)) {
b0ea3acd 443 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
93688997 444 rdaddsc(rs, c);
a1677855 445 if (*c)
446 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 447 sfree(c);
448 *state = charset_init_state;
7136a6c7 449 rdaddsc(rs, "\\fI");
93688997 450 } else if ((towordstyle(text->type) == word_Code ||
451 towordstyle(text->type) == word_WeakCode) &&
452 (attraux(text->aux) == attr_First ||
453 attraux(text->aux) == attr_Only)) {
b0ea3acd 454 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
93688997 455 rdaddsc(rs, c);
a1677855 456 if (*c)
457 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 458 sfree(c);
459 *state = charset_init_state;
7136a6c7 460 rdaddsc(rs, "\\fB");
93688997 461 }
462
7136a6c7 463 if (removeattr(text->type) == word_Normal) {
93688997 464 charset_state s2 = *state;
465
b0ea3acd 466 if (man_convert(text->text, 0, &c, quote_props, conf->charset, &s2) ||
93688997 467 !text->alt) {
7136a6c7 468 rdaddsc(rs, c);
a1677855 469 if (*c)
470 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 471 *state = s2;
472 } else {
a1677855 473 quote_props = man_rdaddwc(rs, text->alt, NULL,
474 quote_props, conf, state);
93688997 475 }
7136a6c7 476 sfree(c);
477 } else if (removeattr(text->type) == word_WhiteSpace) {
b0ea3acd 478 man_convert(L" ", 1, &c, quote_props, conf->charset, state);
93688997 479 rdaddsc(rs, c);
a1677855 480 if (*c)
481 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 482 sfree(c);
7136a6c7 483 } else if (removeattr(text->type) == word_Quote) {
b0ea3acd 484 man_convert(quoteaux(text->aux) == quote_Open ?
485 conf->lquote : conf->rquote, 0,
486 &c, quote_props, conf->charset, state);
93688997 487 rdaddsc(rs, c);
a1677855 488 if (*c)
489 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 490 sfree(c);
7136a6c7 491 }
93688997 492 if (towordstyle(text->type) != word_Normal &&
7136a6c7 493 (attraux(text->aux) == attr_Last ||
93688997 494 attraux(text->aux) == attr_Only)) {
b0ea3acd 495 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
93688997 496 rdaddsc(rs, c);
a1677855 497 if (*c)
498 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 499 sfree(c);
500 *state = charset_init_state;
7136a6c7 501 rdaddsc(rs, "\\fP");
93688997 502 }
7136a6c7 503 break;
504 }
b0ea3acd 505 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
93688997 506 rdaddsc(rs, c);
a1677855 507 if (*c)
508 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 509 sfree(c);
a1677855 510
511 return quote_props;
7136a6c7 512}
513
93688997 514static void man_text(FILE *fp, word *text, int newline,
b0ea3acd 515 int quote_props, manconfig *conf) {
7136a6c7 516 rdstringc t = { 0, 0, NULL };
93688997 517 charset_state state = CHARSET_INIT_STATE;
7136a6c7 518
b0ea3acd 519 man_rdaddwc(&t, text, NULL, quote_props | QUOTE_INITCTRL, conf, &state);
7136a6c7 520 fprintf(fp, "%s", t.text);
521 sfree(t.text);
522 if (newline)
523 fputc('\n', fp);
524}
525
93688997 526static void man_codepara(FILE *fp, word *text, int charset) {
7136a6c7 527 fprintf(fp, ".nf\n");
528 for (; text; text = text->next) if (text->type == word_WeakCode) {
529 char *c;
4b3c5afb 530 wchar_t *t, *e;
531 int quote_props = QUOTE_INITCTRL;
532
533 t = text->text;
534 if (text->next && text->next->type == word_Emph) {
535 e = text->next->text;
536 text = text->next;
537 } else
538 e = NULL;
539
540 while (e && *e && *t) {
541 int n;
542 int ec = *e;
543
544 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
545 if (ec == 'i')
546 fprintf(fp, "\\fI");
547 else if (ec == 'b')
548 fprintf(fp, "\\fB");
93688997 549 man_convert(t, n, &c, quote_props, charset, NULL);
4b3c5afb 550 quote_props &= ~QUOTE_INITCTRL;
551 fprintf(fp, "%s", c);
552 sfree(c);
553 if (ec == 'i' || ec == 'b')
554 fprintf(fp, "\\fP");
555 t += n;
556 e += n;
557 }
93688997 558 man_convert(t, 0, &c, quote_props, charset, NULL);
7136a6c7 559 fprintf(fp, "%s\n", c);
560 sfree(c);
561 }
562 fprintf(fp, ".fi\n");
563}