More portable attempt to get a literal U+0027 than r8321, pinched from pod2man
[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;
2a2375db 16 wchar_t *bullet, *rule, *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
e555d8e3 26/*
27 * My TROFF reference is "NROFF/TROFF User's Manual", Joseph
28 * F. Ossana, October 11 1976.
29 *
30 * not yet used:
31 * \(ru rule
32 * \(pl math plus
33 * \(mi math minus
34 * \(eq math equals
35 * \(ga grave accent
36 * \(ul underrule
37 * \(sl slash (matching bakslash)
38 * \(br box vertical rule
39 * \(br Bell System logo
40 * \(or or
41 * all characters for constructing large brackets
42 */
43
44static struct {
45 unsigned short uni;
46 char const *troff;
47} const man_charmap[] = {
48 {0x00A2, "\\(ct"}, {0x00A7, "\\(sc"}, {0x00A9, "\\(co"}, {0x00AC, "\\(no"},
49 {0x00AE, "\\(rg"}, {0x00B0, "\\(de"}, {0x00B1, "\\(+-"}, {0x00B4, "\\(aa"},
50 {0x00BC, "\\(14"}, {0x00BD, "\\(12"}, {0x00BE, "\\(34"}, {0x00D7, "\\(mu"},
51 {0x00F7, "\\(di"},
52
53 {0x0391, "\\(*A"}, {0x0392, "\\(*B"}, {0x0393, "\\(*G"}, {0x0394, "\\(*D"},
54 {0x0395, "\\(*E"}, {0x0396, "\\(*Z"}, {0x0397, "\\(*Y"}, {0x0398, "\\(*H"},
55 {0x0399, "\\(*I"}, {0x039A, "\\(*K"}, {0x039B, "\\(*L"}, {0x039C, "\\(*M"},
56 {0x039D, "\\(*N"}, {0x039E, "\\(*C"}, {0x039F, "\\(*O"}, {0x03A0, "\\(*P"},
57 {0x03A1, "\\(*R"}, {0x03A3, "\\(*S"}, {0x03A4, "\\(*T"}, {0x03A5, "\\(*U"},
58 {0x03A6, "\\(*F"}, {0x03A7, "\\(*X"}, {0x03A8, "\\(*Q"}, {0x03A9, "\\(*W"},
59 {0x03B1, "\\(*a"}, {0x03B2, "\\(*b"}, {0x03B3, "\\(*g"}, {0x03B4, "\\(*d"},
60 {0x03B5, "\\(*e"}, {0x03B6, "\\(*z"}, {0x03B7, "\\(*y"}, {0x03B8, "\\(*h"},
61 {0x03B9, "\\(*i"}, {0x03BA, "\\(*k"}, {0x03BB, "\\(*l"}, {0x03BC, "\\(*m"},
62 {0x03BD, "\\(*n"}, {0x03BE, "\\(*c"}, {0x03BF, "\\(*o"}, {0x03C0, "\\(*p"},
63 {0x03C1, "\\(*r"}, {0x03C2, "\\(ts"}, {0x03C3, "\\(*s"}, {0x03C4, "\\(*t"},
64 {0x03C5, "\\(*u"}, {0x03C6, "\\(*f"}, {0x03C7, "\\(*x"}, {0x03C8, "\\(*q"},
65 {0x03C9, "\\(*w"},
66
67 {0x2014, "\\(em"}, {0x2018, "`"}, {0x2019, "'"}, {0x2020, "\\(dg"},
68 {0x2021, "\\(dd"}, {0x2022, "\\(bu"}, {0x2032, "\\(fm"},
69
70 {0x2190, "\\(<-"}, {0x2191, "\\(ua"}, {0x2192, "\\(->"}, {0x2193, "\\(da"},
71
72 {0x2202, "\\(pd"}, {0x2205, "\\(es"}, {0x2207, "\\(gr"}, {0x2208, "\\(mo"},
73 {0x2212, "\\-"}, {0x2217, "\\(**"}, {0x221A, "\\(sr"}, {0x221D, "\\(pt"},
74 {0x221E, "\\(if"}, {0x2229, "\\(ca"}, {0x222A, "\\(cu"}, {0x222B, "\\(is"},
75 {0x223C, "\\(ap"}, {0x2245, "\\(~="}, {0x2260, "\\(!="}, {0x2261, "\\(=="},
76 {0x2264, "\\(<="}, {0x2265, "\\(>="}, {0x2282, "\\(sb"}, {0x2283, "\\(sp"},
77 {0x2286, "\\(ib"}, {0x2287, "\\(ip"},
78
79 {0x25A1, "\\(sq"}, {0x25CB, "\\(ci"},
80
81 {0x261C, "\\(lh"}, {0x261E, "\\(rh"},
82};
83
84static char const *troffchar(int unichar) {
85 int i, j, k;
86
87 i = -1;
88 j = lenof(man_charmap);
89 while (j-i > 1) {
90 k = (i + j) / 2;
91 if (man_charmap[k].uni == unichar)
92 return man_charmap[k].troff;
93 else if (man_charmap[k].uni > unichar)
94 j = k;
95 else
96 i = k;
97 }
98 return NULL;
99}
100
101/*
102 * Return TRUE if we can represent the whole of the given string either
103 * in the output charset or as named characters; FALSE otherwise.
104 */
105static int troff_ok(int charset, wchar_t *string) {
106 wchar_t test[2];
107 while (*string) {
108 test[0] = *string;
109 test[1] = 0;
110 if (!cvt_ok(charset, test) && !troffchar(*string))
111 return FALSE;
112 string++;
113 }
114 return TRUE;
115}
116
4b3c5afb 117static manconfig man_configure(paragraph *source) {
b0ea3acd 118 paragraph *p;
4b3c5afb 119 manconfig ret;
120
121 /*
122 * Defaults.
123 */
124 ret.th = NULL;
125 ret.headnumbers = FALSE;
126 ret.mindepth = 0;
50d6b4bd 127 ret.filename = dupstr("output.1");
93688997 128 ret.charset = CS_ASCII;
b0ea3acd 129 ret.bullet = L"\x2022\0o\0\0";
2a2375db 130 ret.rule = L"\x2500\0-\0\0";
b0ea3acd 131 ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0";
132 ret.rquote = uadv(ret.lquote);
4b3c5afb 133
b0ea3acd 134 /*
135 * Two-pass configuration so that we can pick up global config
136 * (e.g. `quotes') before having it overridden by specific
c5546514 137 * config (`man-quotes'), irrespective of the order in which
b0ea3acd 138 * they occur.
139 */
140 for (p = source; p; p = p->next) {
141 if (p->type == para_Config) {
142 if (!ustricmp(p->keyword, L"quotes")) {
143 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
144 ret.lquote = uadv(p->keyword);
145 ret.rquote = uadv(ret.lquote);
146 }
147 }
148 }
149 }
150
151 for (p = source; p; p = p->next) {
152 if (p->type == para_Config) {
153 if (!ustricmp(p->keyword, L"man-identity")) {
4b3c5afb 154 wchar_t *wp, *ep;
155
b0ea3acd 156 wp = uadv(p->keyword);
4b3c5afb 157 ep = wp;
158 while (*ep)
159 ep = uadv(ep);
50d6b4bd 160 sfree(ret.th);
f1530049 161 ret.th = snewn(ep - wp + 1, wchar_t);
4b3c5afb 162 memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t));
b0ea3acd 163 } else if (!ustricmp(p->keyword, L"man-charset")) {
0960a3d8 164 ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword));
b0ea3acd 165 } else if (!ustricmp(p->keyword, L"man-headnumbers")) {
166 ret.headnumbers = utob(uadv(p->keyword));
167 } else if (!ustricmp(p->keyword, L"man-mindepth")) {
168 ret.mindepth = utoi(uadv(p->keyword));
169 } else if (!ustricmp(p->keyword, L"man-filename")) {
50d6b4bd 170 sfree(ret.filename);
b0ea3acd 171 ret.filename = dupstr(adv(p->origkeyword));
172 } else if (!ustricmp(p->keyword, L"man-bullet")) {
173 ret.bullet = uadv(p->keyword);
2a2375db 174 } else if (!ustricmp(p->keyword, L"man-rule")) {
175 ret.rule = uadv(p->keyword);
c5546514 176 } else if (!ustricmp(p->keyword, L"man-quotes")) {
b0ea3acd 177 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
178 ret.lquote = uadv(p->keyword);
179 ret.rquote = uadv(ret.lquote);
180 }
4b3c5afb 181 }
182 }
183 }
184
b0ea3acd 185 /*
2a2375db 186 * Now process fallbacks on quote characters, bullets, and the
187 * rule character.
b0ea3acd 188 */
189 while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
e555d8e3 190 (!troff_ok(ret.charset, ret.lquote) ||
191 !troff_ok(ret.charset, ret.rquote))) {
b0ea3acd 192 ret.lquote = uadv(ret.rquote);
193 ret.rquote = uadv(ret.lquote);
194 }
195
196 while (*ret.bullet && *uadv(ret.bullet) &&
e555d8e3 197 !troff_ok(ret.charset, ret.bullet))
b0ea3acd 198 ret.bullet = uadv(ret.bullet);
199
2a2375db 200 while (*ret.rule && *uadv(ret.rule) &&
201 !troff_ok(ret.charset, ret.rule))
202 ret.rule = uadv(ret.rule);
203
4b3c5afb 204 return ret;
205}
206
207static void man_conf_cleanup(manconfig cf)
208{
209 sfree(cf.th);
50d6b4bd 210 sfree(cf.filename);
4b3c5afb 211}
7136a6c7 212
ba9c1487 213paragraph *man_config_filename(char *filename)
214{
e4ea58f8 215 return cmdline_cfg_simple("man-filename", filename, NULL);
ba9c1487 216}
217
e579728e 218#define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
219#define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
0cef59c1 220#define QUOTE_LITERAL 4 /* defeat special meaning of `, ', - in troff */
7136a6c7 221
222void man_backend(paragraph *sourceform, keywordlist *keywords,
43341922 223 indexdata *idx, void *unused) {
7136a6c7 224 paragraph *p;
225 FILE *fp;
4b3c5afb 226 manconfig conf;
02478c4f 227 int had_described_thing;
7136a6c7 228
43341922 229 IGNORE(unused);
230 IGNORE(keywords);
231 IGNORE(idx);
7136a6c7 232
4b3c5afb 233 conf = man_configure(sourceform);
234
7136a6c7 235 /*
50d6b4bd 236 * Open the output file.
7136a6c7 237 */
50d6b4bd 238 fp = fopen(conf.filename, "w");
7136a6c7 239 if (!fp) {
50d6b4bd 240 error(err_cantopenw, conf.filename);
7136a6c7 241 return;
242 }
243
244 /* Do the version ID */
245 for (p = sourceform; p; p = p->next)
246 if (p->type == para_VersionID) {
247 fprintf(fp, ".\\\" ");
b0ea3acd 248 man_text(fp, p->words, TRUE, 0, &conf);
7136a6c7 249 }
250
7b982bcc 251 /* Standard preamble */
252 /* Dodge to try to get literal U+0027 in output when required,
253 * bypassing groff's Unicode transform; pinched from pod2man */
254 fprintf(fp, ".ie \\n(.g .ds Aq \\(aq\n"
255 ".el .ds Aq '\n");
256
4b3c5afb 257 /* .TH name-of-program manual-section */
22905f72 258 fprintf(fp, ".TH");
259 if (conf.th && *conf.th) {
4b3c5afb 260 char *c;
22905f72 261 wchar_t *wp;
262
263 for (wp = conf.th; *wp; wp = uadv(wp)) {
264 fputs(" \"", fp);
93688997 265 man_convert(wp, 0, &c, QUOTE_QUOTES, conf.charset, NULL);
22905f72 266 fputs(c, fp);
267 sfree(c);
268 fputc('"', fp);
4b3c5afb 269 }
270 }
22905f72 271 fputc('\n', fp);
7136a6c7 272
02478c4f 273 had_described_thing = FALSE;
274#define cleanup_described_thing do { \
275 if (had_described_thing) \
276 fprintf(fp, "\n"); \
277 had_described_thing = FALSE; \
278} while (0)
279
7136a6c7 280 for (p = sourceform; p; p = p->next) switch (p->type) {
281 /*
282 * Things we ignore because we've already processed them or
283 * aren't going to touch them in this pass.
284 */
285 case para_IM:
286 case para_BR:
287 case para_Biblio: /* only touch BiblioCited */
288 case para_VersionID:
7136a6c7 289 case para_NoCite:
290 case para_Title:
291 break;
292
293 /*
294 * Headings.
295 */
296 case para_Chapter:
297 case para_Appendix:
298 case para_UnnumberedChapter:
299 case para_Heading:
300 case para_Subsect:
8902e0ed 301
02478c4f 302 cleanup_described_thing;
4b3c5afb 303 {
304 int depth;
305 if (p->type == para_Subsect)
e4f8c48e 306 depth = p->aux + 1;
4b3c5afb 307 else if (p->type == para_Heading)
308 depth = 1;
309 else
310 depth = 0;
311 if (depth >= conf.mindepth) {
4118fc81 312 if (depth > conf.mindepth)
313 fprintf(fp, ".SS \"");
314 else
315 fprintf(fp, ".SH \"");
4b3c5afb 316 if (conf.headnumbers && p->kwtext) {
b0ea3acd 317 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
4b3c5afb 318 fprintf(fp, " ");
319 }
b0ea3acd 320 man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf);
4b3c5afb 321 fprintf(fp, "\"\n");
322 }
323 break;
324 }
7136a6c7 325
326 /*
327 * Code paragraphs.
328 */
329 case para_Code:
02478c4f 330 cleanup_described_thing;
7136a6c7 331 fprintf(fp, ".PP\n");
93688997 332 man_codepara(fp, p->words, conf.charset);
7136a6c7 333 break;
334
335 /*
336 * Normal paragraphs.
337 */
338 case para_Normal:
9057a0a8 339 case para_Copyright:
02478c4f 340 cleanup_described_thing;
7136a6c7 341 fprintf(fp, ".PP\n");
b0ea3acd 342 man_text(fp, p->words, TRUE, 0, &conf);
7136a6c7 343 break;
344
345 /*
346 * List paragraphs.
347 */
348 case para_Description:
349 case para_BiblioCited:
350 case para_Bullet:
351 case para_NumberedList:
02478c4f 352 if (p->type != para_Description)
353 cleanup_described_thing;
354
7136a6c7 355 if (p->type == para_Bullet) {
b0ea3acd 356 char *bullettext;
357 man_convert(conf.bullet, -1, &bullettext, QUOTE_QUOTES,
358 conf.charset, NULL);
359 fprintf(fp, ".IP \"\\fB%s\\fP\"\n", bullettext);
360 sfree(bullettext);
7136a6c7 361 } else if (p->type == para_NumberedList) {
362 fprintf(fp, ".IP \"");
b0ea3acd 363 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
7136a6c7 364 fprintf(fp, "\"\n");
365 } else if (p->type == para_Description) {
02478c4f 366 if (had_described_thing) {
367 /*
368 * Do nothing; the .xP for this paragraph is the
369 * .IP which has come before it in the
370 * DescribedThing.
371 */
372 } else {
373 /*
374 * A \dd without a preceding \dt is given a blank
375 * one.
376 */
377 fprintf(fp, ".IP \"\"\n");
378 }
7136a6c7 379 } else if (p->type == para_BiblioCited) {
380 fprintf(fp, ".IP \"");
b0ea3acd 381 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf);
7136a6c7 382 fprintf(fp, "\"\n");
383 }
b0ea3acd 384 man_text(fp, p->words, TRUE, 0, &conf);
02478c4f 385 had_described_thing = FALSE;
7136a6c7 386 break;
387
388 case para_DescribedThing:
02478c4f 389 cleanup_described_thing;
7136a6c7 390 fprintf(fp, ".IP \"");
b0ea3acd 391 man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf);
7136a6c7 392 fprintf(fp, "\"\n");
02478c4f 393 had_described_thing = TRUE;
7136a6c7 394 break;
395
396 case para_Rule:
2a2375db 397 {
398 char *ruletext;
399 /*
400 * New paragraph containing a horizontal line 1/2em above
401 * the baseline, or a line of rule characters, whose
402 * length is the line length minus the current indent.
403 */
404 cleanup_described_thing;
405 man_convert(conf.rule, -1, &ruletext, 0, conf.charset, NULL);
406 fprintf(fp, ".PP\n.ie t \\u\\l'\\n(.lu-\\n(.iu'\\d\n"
407 ".el \\l'\\n(.lu-\\n(.iu\\&%s'\n", ruletext);
408 sfree(ruletext);
409 }
7136a6c7 410 break;
411
412 case para_LcontPush:
2614b01d 413 case para_QuotePush:
02478c4f 414 cleanup_described_thing;
7136a6c7 415 fprintf(fp, ".RS\n");
416 break;
417 case para_LcontPop:
2614b01d 418 case para_QuotePop:
02478c4f 419 cleanup_described_thing;
7136a6c7 420 fprintf(fp, ".RE\n");
421 break;
422 }
02478c4f 423 cleanup_described_thing;
7136a6c7 424
425 /*
426 * Tidy up.
427 */
428 fclose(fp);
4b3c5afb 429 man_conf_cleanup(conf);
7136a6c7 430}
431
432/*
93688997 433 * Convert a wide string into a string of chars; mallocs the
434 * resulting string and stores a pointer to it in `*result'.
435 *
436 * If `state' is non-NULL, updates the charset state pointed to. If
437 * `state' is NULL, this function uses its own state, initialises
438 * it from scratch, and cleans it up when finished. If `state' is
439 * non-NULL but _s_ is NULL, cleans up a provided state.
7136a6c7 440 *
441 * Return is nonzero if all characters are OK. If not all
442 * characters are OK but `result' is non-NULL, a result _will_
443 * still be generated!
444 *
93688997 445 * This function also does escaping of groff special characters.
7136a6c7 446 */
93688997 447static int man_convert(wchar_t const *s, int maxlen,
448 char **result, int quote_props,
449 int charset, charset_state *state) {
450 charset_state internal_state = CHARSET_INIT_STATE;
451 int slen, err;
452 char *p = NULL, *q;
7136a6c7 453 int plen = 0, psize = 0;
93688997 454 rdstringc out = {0, 0, NULL};
e555d8e3 455 int anyerr = 0;
7136a6c7 456
93688997 457 if (!state)
458 state = &internal_state;
459
460 slen = (s ? ustrlen(s) : 0);
461
462 if (slen > maxlen && maxlen > 0)
463 slen = maxlen;
464
465 psize = 384;
466 plen = 0;
f1530049 467 p = snewn(psize, char);
93688997 468 err = 0;
469
470 while (slen > 0) {
e555d8e3 471 int ret = charset_from_unicode(&s, &slen, p, psize,
472 charset, state, &err);
473 plen = ret;
474
475 for (q = p; q < p+plen; q++) {
476 if (q == p && (*q == '.' || *q == '\'') &&
477 (quote_props & QUOTE_INITCTRL)) {
478 /*
479 * Control character (. or ') at the start of a
480 * line. Quote it by putting \& (troff zero-width
481 * space) before it.
482 */
483 rdaddc(&out, '\\');
484 rdaddc(&out, '&');
0cef59c1 485 }
486 if (*q == '`' || *q == ' ') {
7de1fb3b 487 /* Quote backticks and nonbreakable spaces always. */
e555d8e3 488 rdaddc(&out, '\\');
7de1fb3b 489 } else if (*q == '\\') {
490 /* Turn backslashes into \e. */
491 rdaddsc(&out, "\\e");
492 continue;
0cef59c1 493 } else if (*q == '-' && !(quote_props & QUOTE_LITERAL)) {
7de1fb3b 494 /* Turn nonbreakable hyphens into \(hy. */
cc746211 495 rdaddsc(&out, "\\(hy");
496 continue;
0cef59c1 497 } else if (*q == '\'' && (quote_props & QUOTE_LITERAL)) {
7b982bcc 498 /* Try to preserve literal U+0027 (using string defined
499 * in preamble) */
500 rdaddsc(&out, "\\*(Aq"); /* "apostrophe quote" */
0cef59c1 501 continue;
e555d8e3 502 } else if (*q == '"' && (quote_props & QUOTE_QUOTES)) {
503 /*
504 * Double quote within double quotes. Quote it by
505 * doubling.
506 */
507 rdaddc(&out, '"');
7136a6c7 508 }
e555d8e3 509 rdaddc(&out, *q);
510 }
511 if (err) {
512 char const *tr = troffchar(*s);
513 if (tr == NULL)
514 anyerr = TRUE;
515 else
516 rdaddsc(&out, tr);
517 s++; slen--;
7136a6c7 518 }
e555d8e3 519 /* Past start of string -- no more quoting needed */
520 quote_props &= ~QUOTE_INITCTRL;
7136a6c7 521 }
93688997 522
523 if (state == &internal_state || s == NULL) {
524 int ret = charset_from_unicode(NULL, 0, p+plen, psize-plen,
525 charset, state, NULL);
526 if (ret > 0)
527 plen += ret;
7136a6c7 528 }
93688997 529
93688997 530 sfree(p);
531
532 if (out.text)
533 *result = rdtrimc(&out);
534 else
535 *result = dupstr("");
536
e555d8e3 537 return !anyerr;
7136a6c7 538}
539
6377de3d 540static int man_rdaddwc_reset(rdstringc *rs, int quote_props, manconfig *conf,
541 charset_state *state) {
542 char *c;
543
544 man_convert(NULL, 0, &c, quote_props, conf->charset, state);
545 rdaddsc(rs, c);
546 if (*c)
547 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
548 sfree(c);
549 *state = charset_init_state;
550 return quote_props;
551}
552
5ddbccdb 553static int man_rdaddctrl(rdstringc *rs, char *c, int quote_props,
554 manconfig *conf, charset_state *state) {
555 quote_props = man_rdaddwc_reset(rs, quote_props, conf, state);
556 rdaddsc(rs, c);
5ddbccdb 557 return quote_props;
558}
559
a1677855 560static int man_rdaddwc(rdstringc *rs, word *text, word *end,
561 int quote_props, manconfig *conf,
562 charset_state *state) {
7136a6c7 563 char *c;
564
565 for (; text && text != end; text = text->next) switch (text->type) {
566 case word_HyperLink:
567 case word_HyperEnd:
568 case word_UpperXref:
569 case word_LowerXref:
570 case word_XrefEnd:
571 case word_IndexRef:
572 break;
573
574 case word_Normal:
575 case word_Emph:
576 case word_Code:
577 case word_WeakCode:
578 case word_WhiteSpace:
579 case word_EmphSpace:
580 case word_CodeSpace:
581 case word_WkCodeSpace:
582 case word_Quote:
583 case word_EmphQuote:
584 case word_CodeQuote:
585 case word_WkCodeQuote:
586 assert(text->type != word_CodeQuote &&
587 text->type != word_WkCodeQuote);
93688997 588
7136a6c7 589 if (towordstyle(text->type) == word_Emph &&
590 (attraux(text->aux) == attr_First ||
93688997 591 attraux(text->aux) == attr_Only)) {
5ddbccdb 592 quote_props = man_rdaddctrl(rs, "\\fI", quote_props, conf, state);
93688997 593 } else if ((towordstyle(text->type) == word_Code ||
594 towordstyle(text->type) == word_WeakCode) &&
595 (attraux(text->aux) == attr_First ||
596 attraux(text->aux) == attr_Only)) {
5ddbccdb 597 quote_props = man_rdaddctrl(rs, "\\fB", quote_props, conf, state);
93688997 598 }
599
e579728e 600 if (towordstyle(text->type) == word_Code ||
601 towordstyle(text->type) == word_WeakCode)
0cef59c1 602 quote_props |= QUOTE_LITERAL;
e579728e 603
7136a6c7 604 if (removeattr(text->type) == word_Normal) {
93688997 605 charset_state s2 = *state;
cc746211 606 int len = ustrlen(text->text), hyphen = FALSE;
93688997 607
cc746211 608 if (text->breaks && text->text[len - 1] == '-') {
609 len--;
610 hyphen = TRUE;
611 }
612 if (len == 0 ||
613 man_convert(text->text, len, &c, quote_props, conf->charset,
614 &s2) ||
93688997 615 !text->alt) {
cc746211 616 if (len != 0) {
617 rdaddsc(rs, c);
618 if (*c)
619 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
620 *state = s2;
621 }
2e78832e 622 if (hyphen) {
6377de3d 623 quote_props =
5ddbccdb 624 man_rdaddctrl(rs, "-", quote_props, conf, state);
2e78832e 625 quote_props &= ~QUOTE_INITCTRL;
626 }
93688997 627 } else {
a1677855 628 quote_props = man_rdaddwc(rs, text->alt, NULL,
629 quote_props, conf, state);
93688997 630 }
cc746211 631 if (len != 0)
632 sfree(c);
7136a6c7 633 } else if (removeattr(text->type) == word_WhiteSpace) {
5ddbccdb 634 quote_props = man_rdaddctrl(rs, " ", quote_props, conf, state);
2e78832e 635 quote_props &= ~QUOTE_INITCTRL;
7136a6c7 636 } else if (removeattr(text->type) == word_Quote) {
b0ea3acd 637 man_convert(quoteaux(text->aux) == quote_Open ?
638 conf->lquote : conf->rquote, 0,
639 &c, quote_props, conf->charset, state);
93688997 640 rdaddsc(rs, c);
a1677855 641 if (*c)
642 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
93688997 643 sfree(c);
7136a6c7 644 }
93688997 645 if (towordstyle(text->type) != word_Normal &&
7136a6c7 646 (attraux(text->aux) == attr_Last ||
93688997 647 attraux(text->aux) == attr_Only)) {
5ddbccdb 648 quote_props = man_rdaddctrl(rs, "\\fP", quote_props, conf, state);
93688997 649 }
7136a6c7 650 break;
651 }
6377de3d 652 quote_props = man_rdaddwc_reset(rs, quote_props, conf, state);
a1677855 653
654 return quote_props;
7136a6c7 655}
656
93688997 657static void man_text(FILE *fp, word *text, int newline,
b0ea3acd 658 int quote_props, manconfig *conf) {
7136a6c7 659 rdstringc t = { 0, 0, NULL };
93688997 660 charset_state state = CHARSET_INIT_STATE;
7136a6c7 661
b0ea3acd 662 man_rdaddwc(&t, text, NULL, quote_props | QUOTE_INITCTRL, conf, &state);
7136a6c7 663 fprintf(fp, "%s", t.text);
664 sfree(t.text);
665 if (newline)
666 fputc('\n', fp);
667}
668
93688997 669static void man_codepara(FILE *fp, word *text, int charset) {
7136a6c7 670 fprintf(fp, ".nf\n");
671 for (; text; text = text->next) if (text->type == word_WeakCode) {
672 char *c;
4b3c5afb 673 wchar_t *t, *e;
0cef59c1 674 int quote_props = QUOTE_INITCTRL | QUOTE_LITERAL;
4b3c5afb 675
676 t = text->text;
677 if (text->next && text->next->type == word_Emph) {
678 e = text->next->text;
679 text = text->next;
680 } else
681 e = NULL;
682
683 while (e && *e && *t) {
684 int n;
685 int ec = *e;
686
687 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
688 if (ec == 'i')
689 fprintf(fp, "\\fI");
690 else if (ec == 'b')
691 fprintf(fp, "\\fB");
93688997 692 man_convert(t, n, &c, quote_props, charset, NULL);
4b3c5afb 693 quote_props &= ~QUOTE_INITCTRL;
694 fprintf(fp, "%s", c);
695 sfree(c);
696 if (ec == 'i' || ec == 'b')
697 fprintf(fp, "\\fP");
698 t += n;
699 e += n;
700 }
93688997 701 man_convert(t, 0, &c, quote_props, charset, NULL);
7136a6c7 702 fprintf(fp, "%s\n", c);
703 sfree(c);
704 }
705 fprintf(fp, ".fi\n");
706}