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