Ben Hutchings points out that `.UC', which I think I must have
[sgt/halibut] / bk_man.c
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
10 typedef struct {
11 wchar_t *th;
12 int headnumbers;
13 int mindepth;
14 char *filename;
15 int charset;
16 wchar_t *bullet, *lquote, *rquote;
17 } manconfig;
18
19 static void man_text(FILE *, word *,
20 int newline, int quote_props, manconfig *conf);
21 static void man_codepara(FILE *, word *, int charset);
22 static int man_convert(wchar_t const *s, int maxlen,
23 char **result, int quote_props,
24 int charset, charset_state *state);
25
26 static manconfig man_configure(paragraph *source) {
27 paragraph *p;
28 manconfig ret;
29
30 /*
31 * Defaults.
32 */
33 ret.th = NULL;
34 ret.headnumbers = FALSE;
35 ret.mindepth = 0;
36 ret.filename = dupstr("output.1");
37 ret.charset = CS_ASCII;
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);
41
42 /*
43 * Two-pass configuration so that we can pick up global config
44 * (e.g. `quotes') before having it overridden by specific
45 * config (`man-quotes'), irrespective of the order in which
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")) {
62 wchar_t *wp, *ep;
63
64 wp = uadv(p->keyword);
65 ep = wp;
66 while (*ep)
67 ep = uadv(ep);
68 sfree(ret.th);
69 ret.th = snewn(ep - wp + 1, wchar_t);
70 memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t));
71 } else if (!ustricmp(p->keyword, L"man-charset")) {
72 ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword));
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")) {
78 sfree(ret.filename);
79 ret.filename = dupstr(adv(p->origkeyword));
80 } else if (!ustricmp(p->keyword, L"man-bullet")) {
81 ret.bullet = uadv(p->keyword);
82 } else if (!ustricmp(p->keyword, L"man-quotes")) {
83 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
84 ret.lquote = uadv(p->keyword);
85 ret.rquote = uadv(ret.lquote);
86 }
87 }
88 }
89 }
90
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
105 return ret;
106 }
107
108 static void man_conf_cleanup(manconfig cf)
109 {
110 sfree(cf.th);
111 sfree(cf.filename);
112 }
113
114 paragraph *man_config_filename(char *filename)
115 {
116 return cmdline_cfg_simple("man-filename", filename, NULL);
117 }
118
119 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
120 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
121
122 void man_backend(paragraph *sourceform, keywordlist *keywords,
123 indexdata *idx, void *unused) {
124 paragraph *p;
125 FILE *fp;
126 manconfig conf;
127 int had_described_thing;
128
129 IGNORE(unused);
130 IGNORE(keywords);
131 IGNORE(idx);
132
133 conf = man_configure(sourceform);
134
135 /*
136 * Open the output file.
137 */
138 fp = fopen(conf.filename, "w");
139 if (!fp) {
140 error(err_cantopenw, conf.filename);
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, ".\\\" ");
148 man_text(fp, p->words, TRUE, 0, &conf);
149 }
150
151 /* .TH name-of-program manual-section */
152 fprintf(fp, ".TH");
153 if (conf.th && *conf.th) {
154 char *c;
155 wchar_t *wp;
156
157 for (wp = conf.th; *wp; wp = uadv(wp)) {
158 fputs(" \"", fp);
159 man_convert(wp, 0, &c, QUOTE_QUOTES, conf.charset, NULL);
160 fputs(c, fp);
161 sfree(c);
162 fputc('"', fp);
163 }
164 }
165 fputc('\n', fp);
166
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
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:
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:
195
196 cleanup_described_thing;
197 {
198 int depth;
199 if (p->type == para_Subsect)
200 depth = p->aux + 1;
201 else if (p->type == para_Heading)
202 depth = 1;
203 else
204 depth = 0;
205 if (depth >= conf.mindepth) {
206 if (depth > conf.mindepth)
207 fprintf(fp, ".SS \"");
208 else
209 fprintf(fp, ".SH \"");
210 if (conf.headnumbers && p->kwtext) {
211 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
212 fprintf(fp, " ");
213 }
214 man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf);
215 fprintf(fp, "\"\n");
216 }
217 break;
218 }
219
220 /*
221 * Code paragraphs.
222 */
223 case para_Code:
224 cleanup_described_thing;
225 fprintf(fp, ".PP\n");
226 man_codepara(fp, p->words, conf.charset);
227 break;
228
229 /*
230 * Normal paragraphs.
231 */
232 case para_Normal:
233 case para_Copyright:
234 cleanup_described_thing;
235 fprintf(fp, ".PP\n");
236 man_text(fp, p->words, TRUE, 0, &conf);
237 break;
238
239 /*
240 * List paragraphs.
241 */
242 case para_Description:
243 case para_BiblioCited:
244 case para_Bullet:
245 case para_NumberedList:
246 if (p->type != para_Description)
247 cleanup_described_thing;
248
249 if (p->type == para_Bullet) {
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);
255 } else if (p->type == para_NumberedList) {
256 fprintf(fp, ".IP \"");
257 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
258 fprintf(fp, "\"\n");
259 } else if (p->type == para_Description) {
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 }
273 } else if (p->type == para_BiblioCited) {
274 fprintf(fp, ".IP \"");
275 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
276 fprintf(fp, "\"\n");
277 }
278 man_text(fp, p->words, TRUE, 0, &conf);
279 had_described_thing = FALSE;
280 break;
281
282 case para_DescribedThing:
283 cleanup_described_thing;
284 fprintf(fp, ".IP \"");
285 man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf);
286 fprintf(fp, "\"\n");
287 had_described_thing = TRUE;
288 break;
289
290 case para_Rule:
291 /*
292 * This isn't terribly good. Anyone who wants to do better
293 * should feel free!
294 */
295 cleanup_described_thing;
296 fprintf(fp, ".PP\n----------------------------------------\n");
297 break;
298
299 case para_LcontPush:
300 case para_QuotePush:
301 cleanup_described_thing;
302 fprintf(fp, ".RS\n");
303 break;
304 case para_LcontPop:
305 case para_QuotePop:
306 cleanup_described_thing;
307 fprintf(fp, ".RE\n");
308 break;
309 }
310 cleanup_described_thing;
311
312 /*
313 * Tidy up.
314 */
315 fclose(fp);
316 man_conf_cleanup(conf);
317 }
318
319 /*
320 * Convert a wide string into a string of chars; mallocs the
321 * resulting string and stores a pointer to it in `*result'.
322 *
323 * If `state' is non-NULL, updates the charset state pointed to. If
324 * `state' is NULL, this function uses its own state, initialises
325 * it from scratch, and cleans it up when finished. If `state' is
326 * non-NULL but _s_ is NULL, cleans up a provided state.
327 *
328 * Return is nonzero if all characters are OK. If not all
329 * characters are OK but `result' is non-NULL, a result _will_
330 * still be generated!
331 *
332 * This function also does escaping of groff special characters.
333 */
334 static int man_convert(wchar_t const *s, int maxlen,
335 char **result, int quote_props,
336 int charset, charset_state *state) {
337 charset_state internal_state = CHARSET_INIT_STATE;
338 int slen, err;
339 char *p = NULL, *q;
340 int plen = 0, psize = 0;
341 rdstringc out = {0, 0, NULL};
342
343 if (!state)
344 state = &internal_state;
345
346 slen = (s ? ustrlen(s) : 0);
347
348 if (slen > maxlen && maxlen > 0)
349 slen = maxlen;
350
351 psize = 384;
352 plen = 0;
353 p = snewn(psize, char);
354 err = 0;
355
356 while (slen > 0) {
357 int ret = charset_from_unicode(&s, &slen, p+plen, psize-plen,
358 charset, state, (err ? NULL : &err));
359 if (ret > 0) {
360 plen += ret;
361 if (psize - plen < 256) {
362 psize = plen + 256;
363 p = sresize(p, psize, char);
364 }
365 }
366 }
367
368 if (state == &internal_state || s == NULL) {
369 int ret = charset_from_unicode(NULL, 0, p+plen, psize-plen,
370 charset, state, NULL);
371 if (ret > 0)
372 plen += ret;
373 }
374
375 for (q = p; q < p+plen; q++) {
376 if (q == p && (*q == '.' || *q == '\'') &&
377 (quote_props & QUOTE_INITCTRL)) {
378 /*
379 * Control character (. or ') at the start of a
380 * line. Quote it by putting \& (troff zero-width
381 * space) before it.
382 */
383 rdaddc(&out, '\\');
384 rdaddc(&out, '&');
385 } else if (*q == '\\') {
386 /*
387 * Quote backslashes by doubling them, always.
388 */
389 rdaddc(&out, '\\');
390 } else if (*q == '"' && (quote_props & QUOTE_QUOTES)) {
391 /*
392 * Double quote within double quotes. Quote it by
393 * doubling.
394 */
395 rdaddc(&out, '"');
396 }
397 rdaddc(&out, *q);
398 }
399
400 sfree(p);
401
402 if (out.text)
403 *result = rdtrimc(&out);
404 else
405 *result = dupstr("");
406
407 return !err;
408 }
409
410 static int man_rdaddwc(rdstringc *rs, word *text, word *end,
411 int quote_props, manconfig *conf,
412 charset_state *state) {
413 char *c;
414
415 for (; text && text != end; text = text->next) switch (text->type) {
416 case word_HyperLink:
417 case word_HyperEnd:
418 case word_UpperXref:
419 case word_LowerXref:
420 case word_XrefEnd:
421 case word_IndexRef:
422 break;
423
424 case word_Normal:
425 case word_Emph:
426 case word_Code:
427 case word_WeakCode:
428 case word_WhiteSpace:
429 case word_EmphSpace:
430 case word_CodeSpace:
431 case word_WkCodeSpace:
432 case word_Quote:
433 case word_EmphQuote:
434 case word_CodeQuote:
435 case word_WkCodeQuote:
436 assert(text->type != word_CodeQuote &&
437 text->type != word_WkCodeQuote);
438
439 if (towordstyle(text->type) == word_Emph &&
440 (attraux(text->aux) == attr_First ||
441 attraux(text->aux) == attr_Only)) {
442 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
443 rdaddsc(rs, c);
444 if (*c)
445 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
446 sfree(c);
447 *state = charset_init_state;
448 rdaddsc(rs, "\\fI");
449 } else if ((towordstyle(text->type) == word_Code ||
450 towordstyle(text->type) == word_WeakCode) &&
451 (attraux(text->aux) == attr_First ||
452 attraux(text->aux) == attr_Only)) {
453 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
454 rdaddsc(rs, c);
455 if (*c)
456 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
457 sfree(c);
458 *state = charset_init_state;
459 rdaddsc(rs, "\\fB");
460 }
461
462 if (removeattr(text->type) == word_Normal) {
463 charset_state s2 = *state;
464
465 if (man_convert(text->text, 0, &c, quote_props, conf->charset, &s2) ||
466 !text->alt) {
467 rdaddsc(rs, c);
468 if (*c)
469 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
470 *state = s2;
471 } else {
472 quote_props = man_rdaddwc(rs, text->alt, NULL,
473 quote_props, conf, state);
474 }
475 sfree(c);
476 } else if (removeattr(text->type) == word_WhiteSpace) {
477 man_convert(L" ", 1, &c, quote_props, conf->charset, state);
478 rdaddsc(rs, c);
479 if (*c)
480 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
481 sfree(c);
482 } else if (removeattr(text->type) == word_Quote) {
483 man_convert(quoteaux(text->aux) == quote_Open ?
484 conf->lquote : conf->rquote, 0,
485 &c, quote_props, conf->charset, state);
486 rdaddsc(rs, c);
487 if (*c)
488 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
489 sfree(c);
490 }
491 if (towordstyle(text->type) != word_Normal &&
492 (attraux(text->aux) == attr_Last ||
493 attraux(text->aux) == attr_Only)) {
494 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
495 rdaddsc(rs, c);
496 if (*c)
497 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
498 sfree(c);
499 *state = charset_init_state;
500 rdaddsc(rs, "\\fP");
501 }
502 break;
503 }
504 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
505 rdaddsc(rs, c);
506 if (*c)
507 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
508 sfree(c);
509
510 return quote_props;
511 }
512
513 static void man_text(FILE *fp, word *text, int newline,
514 int quote_props, manconfig *conf) {
515 rdstringc t = { 0, 0, NULL };
516 charset_state state = CHARSET_INIT_STATE;
517
518 man_rdaddwc(&t, text, NULL, quote_props | QUOTE_INITCTRL, conf, &state);
519 fprintf(fp, "%s", t.text);
520 sfree(t.text);
521 if (newline)
522 fputc('\n', fp);
523 }
524
525 static void man_codepara(FILE *fp, word *text, int charset) {
526 fprintf(fp, ".nf\n");
527 for (; text; text = text->next) if (text->type == word_WeakCode) {
528 char *c;
529 wchar_t *t, *e;
530 int quote_props = QUOTE_INITCTRL;
531
532 t = text->text;
533 if (text->next && text->next->type == word_Emph) {
534 e = text->next->text;
535 text = text->next;
536 } else
537 e = NULL;
538
539 while (e && *e && *t) {
540 int n;
541 int ec = *e;
542
543 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
544 if (ec == 'i')
545 fprintf(fp, "\\fI");
546 else if (ec == 'b')
547 fprintf(fp, "\\fB");
548 man_convert(t, n, &c, quote_props, charset, NULL);
549 quote_props &= ~QUOTE_INITCTRL;
550 fprintf(fp, "%s", c);
551 sfree(c);
552 if (ec == 'i' || ec == 'b')
553 fprintf(fp, "\\fP");
554 t += n;
555 e += n;
556 }
557 man_convert(t, 0, &c, quote_props, charset, NULL);
558 fprintf(fp, "%s\n", c);
559 sfree(c);
560 }
561 fprintf(fp, ".fi\n");
562 }