| 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, *rule, *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 | /* |
| 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 | |
| 44 | static 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 | |
| 84 | static 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 | */ |
| 105 | static 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 | |
| 117 | static manconfig man_configure(paragraph *source) { |
| 118 | paragraph *p; |
| 119 | manconfig ret; |
| 120 | |
| 121 | /* |
| 122 | * Defaults. |
| 123 | */ |
| 124 | ret.th = NULL; |
| 125 | ret.headnumbers = FALSE; |
| 126 | ret.mindepth = 0; |
| 127 | ret.filename = dupstr("output.1"); |
| 128 | ret.charset = CS_ASCII; |
| 129 | ret.bullet = L"\x2022\0o\0\0"; |
| 130 | ret.rule = L"\x2500\0-\0\0"; |
| 131 | ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0"; |
| 132 | ret.rquote = uadv(ret.lquote); |
| 133 | |
| 134 | /* |
| 135 | * Two-pass configuration so that we can pick up global config |
| 136 | * (e.g. `quotes') before having it overridden by specific |
| 137 | * config (`man-quotes'), irrespective of the order in which |
| 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")) { |
| 154 | wchar_t *wp, *ep; |
| 155 | |
| 156 | wp = uadv(p->keyword); |
| 157 | ep = wp; |
| 158 | while (*ep) |
| 159 | ep = uadv(ep); |
| 160 | sfree(ret.th); |
| 161 | ret.th = snewn(ep - wp + 1, wchar_t); |
| 162 | memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t)); |
| 163 | } else if (!ustricmp(p->keyword, L"man-charset")) { |
| 164 | ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword)); |
| 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")) { |
| 170 | sfree(ret.filename); |
| 171 | ret.filename = dupstr(adv(p->origkeyword)); |
| 172 | } else if (!ustricmp(p->keyword, L"man-bullet")) { |
| 173 | ret.bullet = uadv(p->keyword); |
| 174 | } else if (!ustricmp(p->keyword, L"man-rule")) { |
| 175 | ret.rule = uadv(p->keyword); |
| 176 | } else if (!ustricmp(p->keyword, L"man-quotes")) { |
| 177 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 178 | ret.lquote = uadv(p->keyword); |
| 179 | ret.rquote = uadv(ret.lquote); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Now process fallbacks on quote characters, bullets, and the |
| 187 | * rule character. |
| 188 | */ |
| 189 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && |
| 190 | (!troff_ok(ret.charset, ret.lquote) || |
| 191 | !troff_ok(ret.charset, ret.rquote))) { |
| 192 | ret.lquote = uadv(ret.rquote); |
| 193 | ret.rquote = uadv(ret.lquote); |
| 194 | } |
| 195 | |
| 196 | while (*ret.bullet && *uadv(ret.bullet) && |
| 197 | !troff_ok(ret.charset, ret.bullet)) |
| 198 | ret.bullet = uadv(ret.bullet); |
| 199 | |
| 200 | while (*ret.rule && *uadv(ret.rule) && |
| 201 | !troff_ok(ret.charset, ret.rule)) |
| 202 | ret.rule = uadv(ret.rule); |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | static void man_conf_cleanup(manconfig cf) |
| 208 | { |
| 209 | sfree(cf.th); |
| 210 | sfree(cf.filename); |
| 211 | } |
| 212 | |
| 213 | paragraph *man_config_filename(char *filename) |
| 214 | { |
| 215 | return cmdline_cfg_simple("man-filename", filename, NULL); |
| 216 | } |
| 217 | |
| 218 | #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */ |
| 219 | #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */ |
| 220 | |
| 221 | void man_backend(paragraph *sourceform, keywordlist *keywords, |
| 222 | indexdata *idx, void *unused) { |
| 223 | paragraph *p; |
| 224 | FILE *fp; |
| 225 | manconfig conf; |
| 226 | int had_described_thing; |
| 227 | |
| 228 | IGNORE(unused); |
| 229 | IGNORE(keywords); |
| 230 | IGNORE(idx); |
| 231 | |
| 232 | conf = man_configure(sourceform); |
| 233 | |
| 234 | /* |
| 235 | * Open the output file. |
| 236 | */ |
| 237 | fp = fopen(conf.filename, "w"); |
| 238 | if (!fp) { |
| 239 | error(err_cantopenw, conf.filename); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | /* Do the version ID */ |
| 244 | for (p = sourceform; p; p = p->next) |
| 245 | if (p->type == para_VersionID) { |
| 246 | fprintf(fp, ".\\\" "); |
| 247 | man_text(fp, p->words, TRUE, 0, &conf); |
| 248 | } |
| 249 | |
| 250 | /* .TH name-of-program manual-section */ |
| 251 | fprintf(fp, ".TH"); |
| 252 | if (conf.th && *conf.th) { |
| 253 | char *c; |
| 254 | wchar_t *wp; |
| 255 | |
| 256 | for (wp = conf.th; *wp; wp = uadv(wp)) { |
| 257 | fputs(" \"", fp); |
| 258 | man_convert(wp, 0, &c, QUOTE_QUOTES, conf.charset, NULL); |
| 259 | fputs(c, fp); |
| 260 | sfree(c); |
| 261 | fputc('"', fp); |
| 262 | } |
| 263 | } |
| 264 | fputc('\n', fp); |
| 265 | |
| 266 | had_described_thing = FALSE; |
| 267 | #define cleanup_described_thing do { \ |
| 268 | if (had_described_thing) \ |
| 269 | fprintf(fp, "\n"); \ |
| 270 | had_described_thing = FALSE; \ |
| 271 | } while (0) |
| 272 | |
| 273 | for (p = sourceform; p; p = p->next) switch (p->type) { |
| 274 | /* |
| 275 | * Things we ignore because we've already processed them or |
| 276 | * aren't going to touch them in this pass. |
| 277 | */ |
| 278 | case para_IM: |
| 279 | case para_BR: |
| 280 | case para_Biblio: /* only touch BiblioCited */ |
| 281 | case para_VersionID: |
| 282 | case para_NoCite: |
| 283 | case para_Title: |
| 284 | break; |
| 285 | |
| 286 | /* |
| 287 | * Headings. |
| 288 | */ |
| 289 | case para_Chapter: |
| 290 | case para_Appendix: |
| 291 | case para_UnnumberedChapter: |
| 292 | case para_Heading: |
| 293 | case para_Subsect: |
| 294 | |
| 295 | cleanup_described_thing; |
| 296 | { |
| 297 | int depth; |
| 298 | if (p->type == para_Subsect) |
| 299 | depth = p->aux + 1; |
| 300 | else if (p->type == para_Heading) |
| 301 | depth = 1; |
| 302 | else |
| 303 | depth = 0; |
| 304 | if (depth >= conf.mindepth) { |
| 305 | if (depth > conf.mindepth) |
| 306 | fprintf(fp, ".SS \""); |
| 307 | else |
| 308 | fprintf(fp, ".SH \""); |
| 309 | if (conf.headnumbers && p->kwtext) { |
| 310 | man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf); |
| 311 | fprintf(fp, " "); |
| 312 | } |
| 313 | man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf); |
| 314 | fprintf(fp, "\"\n"); |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Code paragraphs. |
| 321 | */ |
| 322 | case para_Code: |
| 323 | cleanup_described_thing; |
| 324 | fprintf(fp, ".PP\n"); |
| 325 | man_codepara(fp, p->words, conf.charset); |
| 326 | break; |
| 327 | |
| 328 | /* |
| 329 | * Normal paragraphs. |
| 330 | */ |
| 331 | case para_Normal: |
| 332 | case para_Copyright: |
| 333 | cleanup_described_thing; |
| 334 | fprintf(fp, ".PP\n"); |
| 335 | man_text(fp, p->words, TRUE, 0, &conf); |
| 336 | break; |
| 337 | |
| 338 | /* |
| 339 | * List paragraphs. |
| 340 | */ |
| 341 | case para_Description: |
| 342 | case para_BiblioCited: |
| 343 | case para_Bullet: |
| 344 | case para_NumberedList: |
| 345 | if (p->type != para_Description) |
| 346 | cleanup_described_thing; |
| 347 | |
| 348 | if (p->type == para_Bullet) { |
| 349 | char *bullettext; |
| 350 | man_convert(conf.bullet, -1, &bullettext, QUOTE_QUOTES, |
| 351 | conf.charset, NULL); |
| 352 | fprintf(fp, ".IP \"\\fB%s\\fP\"\n", bullettext); |
| 353 | sfree(bullettext); |
| 354 | } else if (p->type == para_NumberedList) { |
| 355 | fprintf(fp, ".IP \""); |
| 356 | man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf); |
| 357 | fprintf(fp, "\"\n"); |
| 358 | } else if (p->type == para_Description) { |
| 359 | if (had_described_thing) { |
| 360 | /* |
| 361 | * Do nothing; the .xP for this paragraph is the |
| 362 | * .IP which has come before it in the |
| 363 | * DescribedThing. |
| 364 | */ |
| 365 | } else { |
| 366 | /* |
| 367 | * A \dd without a preceding \dt is given a blank |
| 368 | * one. |
| 369 | */ |
| 370 | fprintf(fp, ".IP \"\"\n"); |
| 371 | } |
| 372 | } else if (p->type == para_BiblioCited) { |
| 373 | fprintf(fp, ".IP \""); |
| 374 | man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES, &conf); |
| 375 | fprintf(fp, "\"\n"); |
| 376 | } |
| 377 | man_text(fp, p->words, TRUE, 0, &conf); |
| 378 | had_described_thing = FALSE; |
| 379 | break; |
| 380 | |
| 381 | case para_DescribedThing: |
| 382 | cleanup_described_thing; |
| 383 | fprintf(fp, ".IP \""); |
| 384 | man_text(fp, p->words, FALSE, QUOTE_QUOTES, &conf); |
| 385 | fprintf(fp, "\"\n"); |
| 386 | had_described_thing = TRUE; |
| 387 | break; |
| 388 | |
| 389 | case para_Rule: |
| 390 | { |
| 391 | char *ruletext; |
| 392 | /* |
| 393 | * New paragraph containing a horizontal line 1/2em above |
| 394 | * the baseline, or a line of rule characters, whose |
| 395 | * length is the line length minus the current indent. |
| 396 | */ |
| 397 | cleanup_described_thing; |
| 398 | man_convert(conf.rule, -1, &ruletext, 0, conf.charset, NULL); |
| 399 | fprintf(fp, ".PP\n.ie t \\u\\l'\\n(.lu-\\n(.iu'\\d\n" |
| 400 | ".el \\l'\\n(.lu-\\n(.iu\\&%s'\n", ruletext); |
| 401 | sfree(ruletext); |
| 402 | } |
| 403 | break; |
| 404 | |
| 405 | case para_LcontPush: |
| 406 | case para_QuotePush: |
| 407 | cleanup_described_thing; |
| 408 | fprintf(fp, ".RS\n"); |
| 409 | break; |
| 410 | case para_LcontPop: |
| 411 | case para_QuotePop: |
| 412 | cleanup_described_thing; |
| 413 | fprintf(fp, ".RE\n"); |
| 414 | break; |
| 415 | } |
| 416 | cleanup_described_thing; |
| 417 | |
| 418 | /* |
| 419 | * Tidy up. |
| 420 | */ |
| 421 | fclose(fp); |
| 422 | man_conf_cleanup(conf); |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Convert a wide string into a string of chars; mallocs the |
| 427 | * resulting string and stores a pointer to it in `*result'. |
| 428 | * |
| 429 | * If `state' is non-NULL, updates the charset state pointed to. If |
| 430 | * `state' is NULL, this function uses its own state, initialises |
| 431 | * it from scratch, and cleans it up when finished. If `state' is |
| 432 | * non-NULL but _s_ is NULL, cleans up a provided state. |
| 433 | * |
| 434 | * Return is nonzero if all characters are OK. If not all |
| 435 | * characters are OK but `result' is non-NULL, a result _will_ |
| 436 | * still be generated! |
| 437 | * |
| 438 | * This function also does escaping of groff special characters. |
| 439 | */ |
| 440 | static int man_convert(wchar_t const *s, int maxlen, |
| 441 | char **result, int quote_props, |
| 442 | int charset, charset_state *state) { |
| 443 | charset_state internal_state = CHARSET_INIT_STATE; |
| 444 | int slen, err; |
| 445 | char *p = NULL, *q; |
| 446 | int plen = 0, psize = 0; |
| 447 | rdstringc out = {0, 0, NULL}; |
| 448 | int anyerr = 0; |
| 449 | |
| 450 | if (!state) |
| 451 | state = &internal_state; |
| 452 | |
| 453 | slen = (s ? ustrlen(s) : 0); |
| 454 | |
| 455 | if (slen > maxlen && maxlen > 0) |
| 456 | slen = maxlen; |
| 457 | |
| 458 | psize = 384; |
| 459 | plen = 0; |
| 460 | p = snewn(psize, char); |
| 461 | err = 0; |
| 462 | |
| 463 | while (slen > 0) { |
| 464 | int ret = charset_from_unicode(&s, &slen, p, psize, |
| 465 | charset, state, &err); |
| 466 | plen = ret; |
| 467 | |
| 468 | for (q = p; q < p+plen; q++) { |
| 469 | if (q == p && (*q == '.' || *q == '\'') && |
| 470 | (quote_props & QUOTE_INITCTRL)) { |
| 471 | /* |
| 472 | * Control character (. or ') at the start of a |
| 473 | * line. Quote it by putting \& (troff zero-width |
| 474 | * space) before it. |
| 475 | */ |
| 476 | rdaddc(&out, '\\'); |
| 477 | rdaddc(&out, '&'); |
| 478 | } else if (*q == '`' || *q == ' ') { |
| 479 | /* Quote backticks and nonbreakable spaces always. */ |
| 480 | rdaddc(&out, '\\'); |
| 481 | } else if (*q == '\\') { |
| 482 | /* Turn backslashes into \e. */ |
| 483 | rdaddsc(&out, "\\e"); |
| 484 | continue; |
| 485 | } else if (*q == '-') { |
| 486 | /* Turn nonbreakable hyphens into \(hy. */ |
| 487 | rdaddsc(&out, "\\(hy"); |
| 488 | continue; |
| 489 | } else if (*q == '"' && (quote_props & QUOTE_QUOTES)) { |
| 490 | /* |
| 491 | * Double quote within double quotes. Quote it by |
| 492 | * doubling. |
| 493 | */ |
| 494 | rdaddc(&out, '"'); |
| 495 | } |
| 496 | rdaddc(&out, *q); |
| 497 | } |
| 498 | if (err) { |
| 499 | char const *tr = troffchar(*s); |
| 500 | if (tr == NULL) |
| 501 | anyerr = TRUE; |
| 502 | else |
| 503 | rdaddsc(&out, tr); |
| 504 | s++; slen--; |
| 505 | } |
| 506 | /* Past start of string -- no more quoting needed */ |
| 507 | quote_props &= ~QUOTE_INITCTRL; |
| 508 | } |
| 509 | |
| 510 | if (state == &internal_state || s == NULL) { |
| 511 | int ret = charset_from_unicode(NULL, 0, p+plen, psize-plen, |
| 512 | charset, state, NULL); |
| 513 | if (ret > 0) |
| 514 | plen += ret; |
| 515 | } |
| 516 | |
| 517 | sfree(p); |
| 518 | |
| 519 | if (out.text) |
| 520 | *result = rdtrimc(&out); |
| 521 | else |
| 522 | *result = dupstr(""); |
| 523 | |
| 524 | return !anyerr; |
| 525 | } |
| 526 | |
| 527 | static int man_rdaddwc_reset(rdstringc *rs, int quote_props, manconfig *conf, |
| 528 | charset_state *state) { |
| 529 | char *c; |
| 530 | |
| 531 | man_convert(NULL, 0, &c, quote_props, conf->charset, state); |
| 532 | rdaddsc(rs, c); |
| 533 | if (*c) |
| 534 | quote_props &= ~QUOTE_INITCTRL; /* not at start any more */ |
| 535 | sfree(c); |
| 536 | *state = charset_init_state; |
| 537 | return quote_props; |
| 538 | } |
| 539 | |
| 540 | static int man_rdaddctrl(rdstringc *rs, char *c, int quote_props, |
| 541 | manconfig *conf, charset_state *state) { |
| 542 | quote_props = man_rdaddwc_reset(rs, quote_props, conf, state); |
| 543 | rdaddsc(rs, c); |
| 544 | return quote_props; |
| 545 | } |
| 546 | |
| 547 | static int man_rdaddwc(rdstringc *rs, word *text, word *end, |
| 548 | int quote_props, manconfig *conf, |
| 549 | charset_state *state) { |
| 550 | char *c; |
| 551 | |
| 552 | for (; text && text != end; text = text->next) switch (text->type) { |
| 553 | case word_HyperLink: |
| 554 | case word_HyperEnd: |
| 555 | case word_UpperXref: |
| 556 | case word_LowerXref: |
| 557 | case word_XrefEnd: |
| 558 | case word_IndexRef: |
| 559 | break; |
| 560 | |
| 561 | case word_Normal: |
| 562 | case word_Emph: |
| 563 | case word_Code: |
| 564 | case word_WeakCode: |
| 565 | case word_WhiteSpace: |
| 566 | case word_EmphSpace: |
| 567 | case word_CodeSpace: |
| 568 | case word_WkCodeSpace: |
| 569 | case word_Quote: |
| 570 | case word_EmphQuote: |
| 571 | case word_CodeQuote: |
| 572 | case word_WkCodeQuote: |
| 573 | assert(text->type != word_CodeQuote && |
| 574 | text->type != word_WkCodeQuote); |
| 575 | |
| 576 | if (towordstyle(text->type) == word_Emph && |
| 577 | (attraux(text->aux) == attr_First || |
| 578 | attraux(text->aux) == attr_Only)) { |
| 579 | quote_props = man_rdaddctrl(rs, "\\fI", quote_props, conf, state); |
| 580 | } else if ((towordstyle(text->type) == word_Code || |
| 581 | towordstyle(text->type) == word_WeakCode) && |
| 582 | (attraux(text->aux) == attr_First || |
| 583 | attraux(text->aux) == attr_Only)) { |
| 584 | quote_props = man_rdaddctrl(rs, "\\fB", quote_props, conf, state); |
| 585 | } |
| 586 | |
| 587 | if (removeattr(text->type) == word_Normal) { |
| 588 | charset_state s2 = *state; |
| 589 | int len = ustrlen(text->text), hyphen = FALSE; |
| 590 | |
| 591 | if (text->breaks && text->text[len - 1] == '-') { |
| 592 | len--; |
| 593 | hyphen = TRUE; |
| 594 | } |
| 595 | if (len == 0 || |
| 596 | man_convert(text->text, len, &c, quote_props, conf->charset, |
| 597 | &s2) || |
| 598 | !text->alt) { |
| 599 | if (len != 0) { |
| 600 | rdaddsc(rs, c); |
| 601 | if (*c) |
| 602 | quote_props &= ~QUOTE_INITCTRL; /* not at start any more */ |
| 603 | *state = s2; |
| 604 | } |
| 605 | if (hyphen) { |
| 606 | quote_props = |
| 607 | man_rdaddctrl(rs, "-", quote_props, conf, state); |
| 608 | quote_props &= ~QUOTE_INITCTRL; |
| 609 | } |
| 610 | } else { |
| 611 | quote_props = man_rdaddwc(rs, text->alt, NULL, |
| 612 | quote_props, conf, state); |
| 613 | } |
| 614 | if (len != 0) |
| 615 | sfree(c); |
| 616 | } else if (removeattr(text->type) == word_WhiteSpace) { |
| 617 | quote_props = man_rdaddctrl(rs, " ", quote_props, conf, state); |
| 618 | quote_props &= ~QUOTE_INITCTRL; |
| 619 | } else if (removeattr(text->type) == word_Quote) { |
| 620 | man_convert(quoteaux(text->aux) == quote_Open ? |
| 621 | conf->lquote : conf->rquote, 0, |
| 622 | &c, quote_props, conf->charset, state); |
| 623 | rdaddsc(rs, c); |
| 624 | if (*c) |
| 625 | quote_props &= ~QUOTE_INITCTRL; /* not at start any more */ |
| 626 | sfree(c); |
| 627 | } |
| 628 | if (towordstyle(text->type) != word_Normal && |
| 629 | (attraux(text->aux) == attr_Last || |
| 630 | attraux(text->aux) == attr_Only)) { |
| 631 | quote_props = man_rdaddctrl(rs, "\\fP", quote_props, conf, state); |
| 632 | } |
| 633 | break; |
| 634 | } |
| 635 | quote_props = man_rdaddwc_reset(rs, quote_props, conf, state); |
| 636 | |
| 637 | return quote_props; |
| 638 | } |
| 639 | |
| 640 | static void man_text(FILE *fp, word *text, int newline, |
| 641 | int quote_props, manconfig *conf) { |
| 642 | rdstringc t = { 0, 0, NULL }; |
| 643 | charset_state state = CHARSET_INIT_STATE; |
| 644 | |
| 645 | man_rdaddwc(&t, text, NULL, quote_props | QUOTE_INITCTRL, conf, &state); |
| 646 | fprintf(fp, "%s", t.text); |
| 647 | sfree(t.text); |
| 648 | if (newline) |
| 649 | fputc('\n', fp); |
| 650 | } |
| 651 | |
| 652 | static void man_codepara(FILE *fp, word *text, int charset) { |
| 653 | fprintf(fp, ".nf\n"); |
| 654 | for (; text; text = text->next) if (text->type == word_WeakCode) { |
| 655 | char *c; |
| 656 | wchar_t *t, *e; |
| 657 | int quote_props = QUOTE_INITCTRL; |
| 658 | |
| 659 | t = text->text; |
| 660 | if (text->next && text->next->type == word_Emph) { |
| 661 | e = text->next->text; |
| 662 | text = text->next; |
| 663 | } else |
| 664 | e = NULL; |
| 665 | |
| 666 | while (e && *e && *t) { |
| 667 | int n; |
| 668 | int ec = *e; |
| 669 | |
| 670 | for (n = 0; t[n] && e[n] && e[n] == ec; n++); |
| 671 | if (ec == 'i') |
| 672 | fprintf(fp, "\\fI"); |
| 673 | else if (ec == 'b') |
| 674 | fprintf(fp, "\\fB"); |
| 675 | man_convert(t, n, &c, quote_props, charset, NULL); |
| 676 | quote_props &= ~QUOTE_INITCTRL; |
| 677 | fprintf(fp, "%s", c); |
| 678 | sfree(c); |
| 679 | if (ec == 'i' || ec == 'b') |
| 680 | fprintf(fp, "\\fP"); |
| 681 | t += n; |
| 682 | e += n; |
| 683 | } |
| 684 | man_convert(t, 0, &c, quote_props, charset, NULL); |
| 685 | fprintf(fp, "%s\n", c); |
| 686 | sfree(c); |
| 687 | } |
| 688 | fprintf(fp, ".fi\n"); |
| 689 | } |