1 /* $OpenBSD: mdoc_man.c,v 1.103 2017/03/04 21:41:13 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2011-2017 Ingo Schwarze <schwarze@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 #include <sys/types.h> 18 19 #include <assert.h> 20 #include <stdio.h> 21 #include <string.h> 22 23 #include "mandoc_aux.h" 24 #include "mandoc.h" 25 #include "roff.h" 26 #include "mdoc.h" 27 #include "man.h" 28 #include "out.h" 29 #include "main.h" 30 31 #define DECL_ARGS const struct roff_meta *meta, struct roff_node *n 32 33 struct manact { 34 int (*cond)(DECL_ARGS); /* DON'T run actions */ 35 int (*pre)(DECL_ARGS); /* pre-node action */ 36 void (*post)(DECL_ARGS); /* post-node action */ 37 const char *prefix; /* pre-node string constant */ 38 const char *suffix; /* post-node string constant */ 39 }; 40 41 static int cond_body(DECL_ARGS); 42 static int cond_head(DECL_ARGS); 43 static void font_push(char); 44 static void font_pop(void); 45 static void mid_it(void); 46 static void post__t(DECL_ARGS); 47 static void post_aq(DECL_ARGS); 48 static void post_bd(DECL_ARGS); 49 static void post_bf(DECL_ARGS); 50 static void post_bk(DECL_ARGS); 51 static void post_bl(DECL_ARGS); 52 static void post_dl(DECL_ARGS); 53 static void post_en(DECL_ARGS); 54 static void post_enc(DECL_ARGS); 55 static void post_eo(DECL_ARGS); 56 static void post_fa(DECL_ARGS); 57 static void post_fd(DECL_ARGS); 58 static void post_fl(DECL_ARGS); 59 static void post_fn(DECL_ARGS); 60 static void post_fo(DECL_ARGS); 61 static void post_font(DECL_ARGS); 62 static void post_in(DECL_ARGS); 63 static void post_it(DECL_ARGS); 64 static void post_lb(DECL_ARGS); 65 static void post_nm(DECL_ARGS); 66 static void post_percent(DECL_ARGS); 67 static void post_pf(DECL_ARGS); 68 static void post_sect(DECL_ARGS); 69 static void post_sp(DECL_ARGS); 70 static void post_vt(DECL_ARGS); 71 static int pre__t(DECL_ARGS); 72 static int pre_an(DECL_ARGS); 73 static int pre_ap(DECL_ARGS); 74 static int pre_aq(DECL_ARGS); 75 static int pre_bd(DECL_ARGS); 76 static int pre_bf(DECL_ARGS); 77 static int pre_bk(DECL_ARGS); 78 static int pre_bl(DECL_ARGS); 79 static int pre_br(DECL_ARGS); 80 static int pre_dl(DECL_ARGS); 81 static int pre_en(DECL_ARGS); 82 static int pre_enc(DECL_ARGS); 83 static int pre_em(DECL_ARGS); 84 static int pre_skip(DECL_ARGS); 85 static int pre_eo(DECL_ARGS); 86 static int pre_ex(DECL_ARGS); 87 static int pre_fa(DECL_ARGS); 88 static int pre_fd(DECL_ARGS); 89 static int pre_fl(DECL_ARGS); 90 static int pre_fn(DECL_ARGS); 91 static int pre_fo(DECL_ARGS); 92 static int pre_ft(DECL_ARGS); 93 static int pre_in(DECL_ARGS); 94 static int pre_it(DECL_ARGS); 95 static int pre_lk(DECL_ARGS); 96 static int pre_li(DECL_ARGS); 97 static int pre_ll(DECL_ARGS); 98 static int pre_nm(DECL_ARGS); 99 static int pre_no(DECL_ARGS); 100 static int pre_ns(DECL_ARGS); 101 static int pre_pp(DECL_ARGS); 102 static int pre_rs(DECL_ARGS); 103 static int pre_sm(DECL_ARGS); 104 static int pre_sp(DECL_ARGS); 105 static int pre_sect(DECL_ARGS); 106 static int pre_sy(DECL_ARGS); 107 static void pre_syn(const struct roff_node *); 108 static int pre_vt(DECL_ARGS); 109 static int pre_xr(DECL_ARGS); 110 static void print_word(const char *); 111 static void print_line(const char *, int); 112 static void print_block(const char *, int); 113 static void print_offs(const char *, int); 114 static void print_width(const struct mdoc_bl *, 115 const struct roff_node *); 116 static void print_count(int *); 117 static void print_node(DECL_ARGS); 118 119 static const struct manact manacts[MDOC_MAX + 1] = { 120 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */ 121 { NULL, NULL, NULL, NULL, NULL }, /* Dd */ 122 { NULL, NULL, NULL, NULL, NULL }, /* Dt */ 123 { NULL, NULL, NULL, NULL, NULL }, /* Os */ 124 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */ 125 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */ 126 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */ 127 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */ 128 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */ 129 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */ 130 { NULL, NULL, NULL, NULL, NULL }, /* Ed */ 131 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */ 132 { NULL, NULL, NULL, NULL, NULL }, /* El */ 133 { NULL, pre_it, post_it, NULL, NULL }, /* It */ 134 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */ 135 { NULL, pre_an, NULL, NULL, NULL }, /* An */ 136 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */ 137 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */ 138 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */ 139 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */ 140 { NULL, pre_li, post_font, NULL, NULL }, /* Er */ 141 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */ 142 { NULL, pre_ex, NULL, NULL, NULL }, /* Ex */ 143 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */ 144 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */ 145 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */ 146 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */ 147 { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */ 148 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */ 149 { NULL, pre_in, post_in, NULL, NULL }, /* In */ 150 { NULL, pre_li, post_font, NULL, NULL }, /* Li */ 151 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */ 152 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */ 153 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */ 154 { NULL, pre_ft, post_font, NULL, NULL }, /* Ot */ 155 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */ 156 { NULL, pre_ex, NULL, NULL, NULL }, /* Rv */ 157 { NULL, NULL, NULL, NULL, NULL }, /* St */ 158 { NULL, pre_em, post_font, NULL, NULL }, /* Va */ 159 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */ 160 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */ 161 { NULL, NULL, post_percent, NULL, NULL }, /* %A */ 162 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */ 163 { NULL, NULL, post_percent, NULL, NULL }, /* %D */ 164 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */ 165 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */ 166 { NULL, NULL, post_percent, NULL, NULL }, /* %N */ 167 { NULL, NULL, post_percent, NULL, NULL }, /* %O */ 168 { NULL, NULL, post_percent, NULL, NULL }, /* %P */ 169 { NULL, NULL, post_percent, NULL, NULL }, /* %R */ 170 { NULL, pre__t, post__t, NULL, NULL }, /* %T */ 171 { NULL, NULL, post_percent, NULL, NULL }, /* %V */ 172 { NULL, NULL, NULL, NULL, NULL }, /* Ac */ 173 { cond_body, pre_aq, post_aq, NULL, NULL }, /* Ao */ 174 { cond_body, pre_aq, post_aq, NULL, NULL }, /* Aq */ 175 { NULL, NULL, NULL, NULL, NULL }, /* At */ 176 { NULL, NULL, NULL, NULL, NULL }, /* Bc */ 177 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */ 178 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */ 179 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */ 180 { NULL, NULL, NULL, NULL, NULL }, /* Bsx */ 181 { NULL, NULL, NULL, NULL, NULL }, /* Bx */ 182 { NULL, pre_skip, NULL, NULL, NULL }, /* Db */ 183 { NULL, NULL, NULL, NULL, NULL }, /* Dc */ 184 { cond_body, pre_enc, post_enc, "\\(Lq", "\\(Rq" }, /* Do */ 185 { cond_body, pre_enc, post_enc, "\\(Lq", "\\(Rq" }, /* Dq */ 186 { NULL, NULL, NULL, NULL, NULL }, /* Ec */ 187 { NULL, NULL, NULL, NULL, NULL }, /* Ef */ 188 { NULL, pre_em, post_font, NULL, NULL }, /* Em */ 189 { cond_body, pre_eo, post_eo, NULL, NULL }, /* Eo */ 190 { NULL, NULL, NULL, NULL, NULL }, /* Fx */ 191 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */ 192 { NULL, pre_no, NULL, NULL, NULL }, /* No */ 193 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */ 194 { NULL, NULL, NULL, NULL, NULL }, /* Nx */ 195 { NULL, NULL, NULL, NULL, NULL }, /* Ox */ 196 { NULL, NULL, NULL, NULL, NULL }, /* Pc */ 197 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */ 198 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */ 199 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */ 200 { NULL, NULL, NULL, NULL, NULL }, /* Qc */ 201 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */ 202 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */ 203 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */ 204 { NULL, NULL, NULL, NULL, NULL }, /* Re */ 205 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */ 206 { NULL, NULL, NULL, NULL, NULL }, /* Sc */ 207 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */ 208 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */ 209 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */ 210 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */ 211 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */ 212 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */ 213 { NULL, NULL, NULL, NULL, NULL }, /* Ux */ 214 { NULL, NULL, NULL, NULL, NULL }, /* Xc */ 215 { NULL, NULL, NULL, NULL, NULL }, /* Xo */ 216 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */ 217 { NULL, NULL, NULL, NULL, NULL }, /* Fc */ 218 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */ 219 { NULL, NULL, NULL, NULL, NULL }, /* Oc */ 220 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */ 221 { NULL, NULL, NULL, NULL, NULL }, /* Ek */ 222 { NULL, NULL, NULL, NULL, NULL }, /* Bt */ 223 { NULL, NULL, NULL, NULL, NULL }, /* Hf */ 224 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */ 225 { NULL, NULL, NULL, NULL, NULL }, /* Ud */ 226 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */ 227 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */ 228 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */ 229 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */ 230 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */ 231 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */ 232 { NULL, NULL, NULL, NULL, NULL }, /* Brc */ 233 { NULL, NULL, post_percent, NULL, NULL }, /* %C */ 234 { NULL, pre_skip, NULL, NULL, NULL }, /* Es */ 235 { cond_body, pre_en, post_en, NULL, NULL }, /* En */ 236 { NULL, NULL, NULL, NULL, NULL }, /* Dx */ 237 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */ 238 { NULL, pre_br, NULL, NULL, NULL }, /* br */ 239 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */ 240 { NULL, NULL, post_percent, NULL, NULL }, /* %U */ 241 { NULL, NULL, NULL, NULL, NULL }, /* Ta */ 242 { NULL, pre_ll, post_sp, NULL, NULL }, /* ll */ 243 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */ 244 }; 245 246 static int outflags; 247 #define MMAN_spc (1 << 0) /* blank character before next word */ 248 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */ 249 #define MMAN_nl (1 << 2) /* break man(7) code line */ 250 #define MMAN_br (1 << 3) /* break output line */ 251 #define MMAN_sp (1 << 4) /* insert a blank output line */ 252 #define MMAN_PP (1 << 5) /* reset indentation etc. */ 253 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */ 254 #define MMAN_Bk (1 << 7) /* word keep mode */ 255 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */ 256 #define MMAN_An_split (1 << 9) /* author mode is "split" */ 257 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */ 258 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */ 259 #define MMAN_nbrword (1 << 12) /* do not break the next word */ 260 261 #define BL_STACK_MAX 32 262 263 static int Bl_stack[BL_STACK_MAX]; /* offsets [chars] */ 264 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */ 265 static int Bl_stack_len; /* number of nested Bl blocks */ 266 static int TPremain; /* characters before tag is full */ 267 268 static struct { 269 char *head; 270 char *tail; 271 size_t size; 272 } fontqueue; 273 274 275 static void 276 font_push(char newfont) 277 { 278 279 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) { 280 fontqueue.size += 8; 281 fontqueue.head = mandoc_realloc(fontqueue.head, 282 fontqueue.size); 283 } 284 *fontqueue.tail = newfont; 285 print_word(""); 286 printf("\\f"); 287 putchar(newfont); 288 outflags &= ~MMAN_spc; 289 } 290 291 static void 292 font_pop(void) 293 { 294 295 if (fontqueue.tail > fontqueue.head) 296 fontqueue.tail--; 297 outflags &= ~MMAN_spc; 298 print_word(""); 299 printf("\\f"); 300 putchar(*fontqueue.tail); 301 } 302 303 static void 304 print_word(const char *s) 305 { 306 307 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) { 308 /* 309 * If we need a newline, print it now and start afresh. 310 */ 311 if (MMAN_PP & outflags) { 312 if (MMAN_sp & outflags) { 313 if (MMAN_PD & outflags) { 314 printf("\n.PD"); 315 outflags &= ~MMAN_PD; 316 } 317 } else if ( ! (MMAN_PD & outflags)) { 318 printf("\n.PD 0"); 319 outflags |= MMAN_PD; 320 } 321 printf("\n.PP\n"); 322 } else if (MMAN_sp & outflags) 323 printf("\n.sp\n"); 324 else if (MMAN_br & outflags) 325 printf("\n.br\n"); 326 else if (MMAN_nl & outflags) 327 putchar('\n'); 328 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc); 329 if (1 == TPremain) 330 printf(".br\n"); 331 TPremain = 0; 332 } else if (MMAN_spc & outflags) { 333 /* 334 * If we need a space, only print it if 335 * (1) it is forced by `No' or 336 * (2) what follows is not terminating punctuation or 337 * (3) what follows is longer than one character. 338 */ 339 if (MMAN_spc_force & outflags || '\0' == s[0] || 340 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) { 341 if (MMAN_Bk & outflags && 342 ! (MMAN_Bk_susp & outflags)) 343 putchar('\\'); 344 putchar(' '); 345 if (TPremain) 346 TPremain--; 347 } 348 } 349 350 /* 351 * Reassign needing space if we're not following opening 352 * punctuation. 353 */ 354 if (MMAN_Sm & outflags && ('\0' == s[0] || 355 (('(' != s[0] && '[' != s[0]) || '\0' != s[1]))) 356 outflags |= MMAN_spc; 357 else 358 outflags &= ~MMAN_spc; 359 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp); 360 361 for ( ; *s; s++) { 362 switch (*s) { 363 case ASCII_NBRSP: 364 printf("\\ "); 365 break; 366 case ASCII_HYPH: 367 putchar('-'); 368 break; 369 case ASCII_BREAK: 370 printf("\\:"); 371 break; 372 case ' ': 373 if (MMAN_nbrword & outflags) { 374 printf("\\ "); 375 break; 376 } 377 /* FALLTHROUGH */ 378 default: 379 putchar((unsigned char)*s); 380 break; 381 } 382 if (TPremain) 383 TPremain--; 384 } 385 outflags &= ~MMAN_nbrword; 386 } 387 388 static void 389 print_line(const char *s, int newflags) 390 { 391 392 outflags &= ~MMAN_br; 393 outflags |= MMAN_nl; 394 print_word(s); 395 outflags |= newflags; 396 } 397 398 static void 399 print_block(const char *s, int newflags) 400 { 401 402 outflags &= ~MMAN_PP; 403 if (MMAN_sp & outflags) { 404 outflags &= ~(MMAN_sp | MMAN_br); 405 if (MMAN_PD & outflags) { 406 print_line(".PD", 0); 407 outflags &= ~MMAN_PD; 408 } 409 } else if (! (MMAN_PD & outflags)) 410 print_line(".PD 0", MMAN_PD); 411 outflags |= MMAN_nl; 412 print_word(s); 413 outflags |= MMAN_Bk_susp | newflags; 414 } 415 416 static void 417 print_offs(const char *v, int keywords) 418 { 419 char buf[24]; 420 struct roffsu su; 421 int sz; 422 423 print_line(".RS", MMAN_Bk_susp); 424 425 /* Convert v into a number (of characters). */ 426 if (NULL == v || '\0' == *v || (keywords && !strcmp(v, "left"))) 427 sz = 0; 428 else if (keywords && !strcmp(v, "indent")) 429 sz = 6; 430 else if (keywords && !strcmp(v, "indent-two")) 431 sz = 12; 432 else if (a2roffsu(v, &su, SCALE_EN) > 1) { 433 if (SCALE_EN == su.unit) 434 sz = su.scale; 435 else { 436 /* 437 * XXX 438 * If we are inside an enclosing list, 439 * there is no easy way to add the two 440 * indentations because they are provided 441 * in terms of different units. 442 */ 443 print_word(v); 444 outflags |= MMAN_nl; 445 return; 446 } 447 } else 448 sz = strlen(v); 449 450 /* 451 * We are inside an enclosing list. 452 * Add the two indentations. 453 */ 454 if (Bl_stack_len) 455 sz += Bl_stack[Bl_stack_len - 1]; 456 457 (void)snprintf(buf, sizeof(buf), "%dn", sz); 458 print_word(buf); 459 outflags |= MMAN_nl; 460 } 461 462 /* 463 * Set up the indentation for a list item; used from pre_it(). 464 */ 465 static void 466 print_width(const struct mdoc_bl *bl, const struct roff_node *child) 467 { 468 char buf[24]; 469 struct roffsu su; 470 int numeric, remain, sz, chsz; 471 472 numeric = 1; 473 remain = 0; 474 475 /* Convert the width into a number (of characters). */ 476 if (bl->width == NULL) 477 sz = (bl->type == LIST_hang) ? 6 : 0; 478 else if (a2roffsu(bl->width, &su, SCALE_MAX) > 1) { 479 if (SCALE_EN == su.unit) 480 sz = su.scale; 481 else { 482 sz = 0; 483 numeric = 0; 484 } 485 } else 486 sz = strlen(bl->width); 487 488 /* XXX Rough estimation, might have multiple parts. */ 489 if (bl->type == LIST_enum) 490 chsz = (bl->count > 8) + 1; 491 else if (child != NULL && child->type == ROFFT_TEXT) 492 chsz = strlen(child->string); 493 else 494 chsz = 0; 495 496 /* Maybe we are inside an enclosing list? */ 497 mid_it(); 498 499 /* 500 * Save our own indentation, 501 * such that child lists can use it. 502 */ 503 Bl_stack[Bl_stack_len++] = sz + 2; 504 505 /* Set up the current list. */ 506 if (chsz > sz && bl->type != LIST_tag) 507 print_block(".HP", 0); 508 else { 509 print_block(".TP", 0); 510 remain = sz + 2; 511 } 512 if (numeric) { 513 (void)snprintf(buf, sizeof(buf), "%dn", sz + 2); 514 print_word(buf); 515 } else 516 print_word(bl->width); 517 TPremain = remain; 518 } 519 520 static void 521 print_count(int *count) 522 { 523 char buf[24]; 524 525 (void)snprintf(buf, sizeof(buf), "%d.\\&", ++*count); 526 print_word(buf); 527 } 528 529 void 530 man_man(void *arg, const struct roff_man *man) 531 { 532 533 /* 534 * Dump the keep buffer. 535 * We're guaranteed by now that this exists (is non-NULL). 536 * Flush stdout afterward, just in case. 537 */ 538 fputs(mparse_getkeep(man_mparse(man)), stdout); 539 fflush(stdout); 540 } 541 542 void 543 man_mdoc(void *arg, const struct roff_man *mdoc) 544 { 545 struct roff_node *n; 546 547 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n", 548 mdoc->meta.title, 549 (mdoc->meta.msec == NULL ? "" : mdoc->meta.msec), 550 mdoc->meta.date, mdoc->meta.os, mdoc->meta.vol); 551 552 /* Disable hyphenation and if nroff, disable justification. */ 553 printf(".nh\n.if n .ad l"); 554 555 outflags = MMAN_nl | MMAN_Sm; 556 if (0 == fontqueue.size) { 557 fontqueue.size = 8; 558 fontqueue.head = fontqueue.tail = mandoc_malloc(8); 559 *fontqueue.tail = 'R'; 560 } 561 for (n = mdoc->first->child; n != NULL; n = n->next) 562 print_node(&mdoc->meta, n); 563 putchar('\n'); 564 } 565 566 static void 567 print_node(DECL_ARGS) 568 { 569 const struct manact *act; 570 struct roff_node *sub; 571 int cond, do_sub; 572 573 if (n->flags & NODE_NOPRT) 574 return; 575 576 /* 577 * Break the line if we were parsed subsequent the current node. 578 * This makes the page structure be more consistent. 579 */ 580 if (MMAN_spc & outflags && NODE_LINE & n->flags) 581 outflags |= MMAN_nl; 582 583 act = NULL; 584 cond = 0; 585 do_sub = 1; 586 n->flags &= ~NODE_ENDED; 587 588 if (n->type == ROFFT_TEXT) { 589 /* 590 * Make sure that we don't happen to start with a 591 * control character at the start of a line. 592 */ 593 if (MMAN_nl & outflags && 594 ('.' == *n->string || '\'' == *n->string)) { 595 print_word(""); 596 printf("\\&"); 597 outflags &= ~MMAN_spc; 598 } 599 if (n->flags & NODE_DELIMC) 600 outflags &= ~(MMAN_spc | MMAN_spc_force); 601 else if (outflags & MMAN_Sm) 602 outflags |= MMAN_spc_force; 603 print_word(n->string); 604 if (n->flags & NODE_DELIMO) 605 outflags &= ~(MMAN_spc | MMAN_spc_force); 606 else if (outflags & MMAN_Sm) 607 outflags |= MMAN_spc; 608 } else { 609 /* 610 * Conditionally run the pre-node action handler for a 611 * node. 612 */ 613 act = manacts + n->tok; 614 cond = act->cond == NULL || (*act->cond)(meta, n); 615 if (cond && act->pre != NULL && 616 (n->end == ENDBODY_NOT || n->child != NULL)) 617 do_sub = (*act->pre)(meta, n); 618 } 619 620 /* 621 * Conditionally run all child nodes. 622 * Note that this iterates over children instead of using 623 * recursion. This prevents unnecessary depth in the stack. 624 */ 625 if (do_sub) 626 for (sub = n->child; sub; sub = sub->next) 627 print_node(meta, sub); 628 629 /* 630 * Lastly, conditionally run the post-node handler. 631 */ 632 if (NODE_ENDED & n->flags) 633 return; 634 635 if (cond && act->post) 636 (*act->post)(meta, n); 637 638 if (ENDBODY_NOT != n->end) 639 n->body->flags |= NODE_ENDED; 640 } 641 642 static int 643 cond_head(DECL_ARGS) 644 { 645 646 return n->type == ROFFT_HEAD; 647 } 648 649 static int 650 cond_body(DECL_ARGS) 651 { 652 653 return n->type == ROFFT_BODY; 654 } 655 656 static int 657 pre_enc(DECL_ARGS) 658 { 659 const char *prefix; 660 661 prefix = manacts[n->tok].prefix; 662 if (NULL == prefix) 663 return 1; 664 print_word(prefix); 665 outflags &= ~MMAN_spc; 666 return 1; 667 } 668 669 static void 670 post_enc(DECL_ARGS) 671 { 672 const char *suffix; 673 674 suffix = manacts[n->tok].suffix; 675 if (NULL == suffix) 676 return; 677 outflags &= ~(MMAN_spc | MMAN_nl); 678 print_word(suffix); 679 } 680 681 static int 682 pre_ex(DECL_ARGS) 683 { 684 outflags |= MMAN_br | MMAN_nl; 685 return 1; 686 } 687 688 static void 689 post_font(DECL_ARGS) 690 { 691 692 font_pop(); 693 } 694 695 static void 696 post_percent(DECL_ARGS) 697 { 698 699 if (pre_em == manacts[n->tok].pre) 700 font_pop(); 701 if (n->next) { 702 print_word(","); 703 if (n->prev && n->prev->tok == n->tok && 704 n->next->tok == n->tok) 705 print_word("and"); 706 } else { 707 print_word("."); 708 outflags |= MMAN_nl; 709 } 710 } 711 712 static int 713 pre__t(DECL_ARGS) 714 { 715 716 if (n->parent->tok == MDOC_Rs && n->parent->norm->Rs.quote_T) { 717 print_word("\\(lq"); 718 outflags &= ~MMAN_spc; 719 } else 720 font_push('I'); 721 return 1; 722 } 723 724 static void 725 post__t(DECL_ARGS) 726 { 727 728 if (n->parent->tok == MDOC_Rs && n->parent->norm->Rs.quote_T) { 729 outflags &= ~MMAN_spc; 730 print_word("\\(rq"); 731 } else 732 font_pop(); 733 post_percent(meta, n); 734 } 735 736 /* 737 * Print before a section header. 738 */ 739 static int 740 pre_sect(DECL_ARGS) 741 { 742 743 if (n->type == ROFFT_HEAD) { 744 outflags |= MMAN_sp; 745 print_block(manacts[n->tok].prefix, 0); 746 print_word(""); 747 putchar('\"'); 748 outflags &= ~MMAN_spc; 749 } 750 return 1; 751 } 752 753 /* 754 * Print subsequent a section header. 755 */ 756 static void 757 post_sect(DECL_ARGS) 758 { 759 760 if (n->type != ROFFT_HEAD) 761 return; 762 outflags &= ~MMAN_spc; 763 print_word(""); 764 putchar('\"'); 765 outflags |= MMAN_nl; 766 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec) 767 outflags &= ~(MMAN_An_split | MMAN_An_nosplit); 768 } 769 770 /* See mdoc_term.c, synopsis_pre() for comments. */ 771 static void 772 pre_syn(const struct roff_node *n) 773 { 774 775 if (NULL == n->prev || ! (NODE_SYNPRETTY & n->flags)) 776 return; 777 778 if (n->prev->tok == n->tok && 779 MDOC_Ft != n->tok && 780 MDOC_Fo != n->tok && 781 MDOC_Fn != n->tok) { 782 outflags |= MMAN_br; 783 return; 784 } 785 786 switch (n->prev->tok) { 787 case MDOC_Fd: 788 case MDOC_Fn: 789 case MDOC_Fo: 790 case MDOC_In: 791 case MDOC_Vt: 792 outflags |= MMAN_sp; 793 break; 794 case MDOC_Ft: 795 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) { 796 outflags |= MMAN_sp; 797 break; 798 } 799 /* FALLTHROUGH */ 800 default: 801 outflags |= MMAN_br; 802 break; 803 } 804 } 805 806 static int 807 pre_an(DECL_ARGS) 808 { 809 810 switch (n->norm->An.auth) { 811 case AUTH_split: 812 outflags &= ~MMAN_An_nosplit; 813 outflags |= MMAN_An_split; 814 return 0; 815 case AUTH_nosplit: 816 outflags &= ~MMAN_An_split; 817 outflags |= MMAN_An_nosplit; 818 return 0; 819 default: 820 if (MMAN_An_split & outflags) 821 outflags |= MMAN_br; 822 else if (SEC_AUTHORS == n->sec && 823 ! (MMAN_An_nosplit & outflags)) 824 outflags |= MMAN_An_split; 825 return 1; 826 } 827 } 828 829 static int 830 pre_ap(DECL_ARGS) 831 { 832 833 outflags &= ~MMAN_spc; 834 print_word("'"); 835 outflags &= ~MMAN_spc; 836 return 0; 837 } 838 839 static int 840 pre_aq(DECL_ARGS) 841 { 842 843 print_word(n->child != NULL && n->child->next == NULL && 844 n->child->tok == MDOC_Mt ? "<" : "\\(la"); 845 outflags &= ~MMAN_spc; 846 return 1; 847 } 848 849 static void 850 post_aq(DECL_ARGS) 851 { 852 853 outflags &= ~(MMAN_spc | MMAN_nl); 854 print_word(n->child != NULL && n->child->next == NULL && 855 n->child->tok == MDOC_Mt ? ">" : "\\(ra"); 856 } 857 858 static int 859 pre_bd(DECL_ARGS) 860 { 861 862 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br); 863 864 if (DISP_unfilled == n->norm->Bd.type || 865 DISP_literal == n->norm->Bd.type) 866 print_line(".nf", 0); 867 if (0 == n->norm->Bd.comp && NULL != n->parent->prev) 868 outflags |= MMAN_sp; 869 print_offs(n->norm->Bd.offs, 1); 870 return 1; 871 } 872 873 static void 874 post_bd(DECL_ARGS) 875 { 876 877 /* Close out this display. */ 878 print_line(".RE", MMAN_nl); 879 if (DISP_unfilled == n->norm->Bd.type || 880 DISP_literal == n->norm->Bd.type) 881 print_line(".fi", MMAN_nl); 882 883 /* Maybe we are inside an enclosing list? */ 884 if (NULL != n->parent->next) 885 mid_it(); 886 } 887 888 static int 889 pre_bf(DECL_ARGS) 890 { 891 892 switch (n->type) { 893 case ROFFT_BLOCK: 894 return 1; 895 case ROFFT_BODY: 896 break; 897 default: 898 return 0; 899 } 900 switch (n->norm->Bf.font) { 901 case FONT_Em: 902 font_push('I'); 903 break; 904 case FONT_Sy: 905 font_push('B'); 906 break; 907 default: 908 font_push('R'); 909 break; 910 } 911 return 1; 912 } 913 914 static void 915 post_bf(DECL_ARGS) 916 { 917 918 if (n->type == ROFFT_BODY) 919 font_pop(); 920 } 921 922 static int 923 pre_bk(DECL_ARGS) 924 { 925 926 switch (n->type) { 927 case ROFFT_BLOCK: 928 return 1; 929 case ROFFT_BODY: 930 outflags |= MMAN_Bk; 931 return 1; 932 default: 933 return 0; 934 } 935 } 936 937 static void 938 post_bk(DECL_ARGS) 939 { 940 941 if (n->type == ROFFT_BODY) 942 outflags &= ~MMAN_Bk; 943 } 944 945 static int 946 pre_bl(DECL_ARGS) 947 { 948 size_t icol; 949 950 /* 951 * print_offs() will increase the -offset to account for 952 * a possible enclosing .It, but any enclosed .It blocks 953 * just nest and do not add up their indentation. 954 */ 955 if (n->norm->Bl.offs) { 956 print_offs(n->norm->Bl.offs, 0); 957 Bl_stack[Bl_stack_len++] = 0; 958 } 959 960 switch (n->norm->Bl.type) { 961 case LIST_enum: 962 n->norm->Bl.count = 0; 963 return 1; 964 case LIST_column: 965 break; 966 default: 967 return 1; 968 } 969 970 if (n->child != NULL) { 971 print_line(".TS", MMAN_nl); 972 for (icol = 0; icol < n->norm->Bl.ncols; icol++) 973 print_word("l"); 974 print_word("."); 975 } 976 outflags |= MMAN_nl; 977 return 1; 978 } 979 980 static void 981 post_bl(DECL_ARGS) 982 { 983 984 switch (n->norm->Bl.type) { 985 case LIST_column: 986 if (n->child != NULL) 987 print_line(".TE", 0); 988 break; 989 case LIST_enum: 990 n->norm->Bl.count = 0; 991 break; 992 default: 993 break; 994 } 995 996 if (n->norm->Bl.offs) { 997 print_line(".RE", MMAN_nl); 998 assert(Bl_stack_len); 999 Bl_stack_len--; 1000 assert(0 == Bl_stack[Bl_stack_len]); 1001 } else { 1002 outflags |= MMAN_PP | MMAN_nl; 1003 outflags &= ~(MMAN_sp | MMAN_br); 1004 } 1005 1006 /* Maybe we are inside an enclosing list? */ 1007 if (NULL != n->parent->next) 1008 mid_it(); 1009 1010 } 1011 1012 static int 1013 pre_br(DECL_ARGS) 1014 { 1015 1016 outflags |= MMAN_br; 1017 return 0; 1018 } 1019 1020 static int 1021 pre_dl(DECL_ARGS) 1022 { 1023 1024 print_offs("6n", 0); 1025 return 1; 1026 } 1027 1028 static void 1029 post_dl(DECL_ARGS) 1030 { 1031 1032 print_line(".RE", MMAN_nl); 1033 1034 /* Maybe we are inside an enclosing list? */ 1035 if (NULL != n->parent->next) 1036 mid_it(); 1037 } 1038 1039 static int 1040 pre_em(DECL_ARGS) 1041 { 1042 1043 font_push('I'); 1044 return 1; 1045 } 1046 1047 static int 1048 pre_en(DECL_ARGS) 1049 { 1050 1051 if (NULL == n->norm->Es || 1052 NULL == n->norm->Es->child) 1053 return 1; 1054 1055 print_word(n->norm->Es->child->string); 1056 outflags &= ~MMAN_spc; 1057 return 1; 1058 } 1059 1060 static void 1061 post_en(DECL_ARGS) 1062 { 1063 1064 if (NULL == n->norm->Es || 1065 NULL == n->norm->Es->child || 1066 NULL == n->norm->Es->child->next) 1067 return; 1068 1069 outflags &= ~MMAN_spc; 1070 print_word(n->norm->Es->child->next->string); 1071 return; 1072 } 1073 1074 static int 1075 pre_eo(DECL_ARGS) 1076 { 1077 1078 if (n->end == ENDBODY_NOT && 1079 n->parent->head->child == NULL && 1080 n->child != NULL && 1081 n->child->end != ENDBODY_NOT) 1082 print_word("\\&"); 1083 else if (n->end != ENDBODY_NOT ? n->child != NULL : 1084 n->parent->head->child != NULL && (n->child != NULL || 1085 (n->parent->tail != NULL && n->parent->tail->child != NULL))) 1086 outflags &= ~(MMAN_spc | MMAN_nl); 1087 return 1; 1088 } 1089 1090 static void 1091 post_eo(DECL_ARGS) 1092 { 1093 int body, tail; 1094 1095 if (n->end != ENDBODY_NOT) { 1096 outflags |= MMAN_spc; 1097 return; 1098 } 1099 1100 body = n->child != NULL || n->parent->head->child != NULL; 1101 tail = n->parent->tail != NULL && n->parent->tail->child != NULL; 1102 1103 if (body && tail) 1104 outflags &= ~MMAN_spc; 1105 else if ( ! (body || tail)) 1106 print_word("\\&"); 1107 else if ( ! tail) 1108 outflags |= MMAN_spc; 1109 } 1110 1111 static int 1112 pre_fa(DECL_ARGS) 1113 { 1114 int am_Fa; 1115 1116 am_Fa = MDOC_Fa == n->tok; 1117 1118 if (am_Fa) 1119 n = n->child; 1120 1121 while (NULL != n) { 1122 font_push('I'); 1123 if (am_Fa || NODE_SYNPRETTY & n->flags) 1124 outflags |= MMAN_nbrword; 1125 print_node(meta, n); 1126 font_pop(); 1127 if (NULL != (n = n->next)) 1128 print_word(","); 1129 } 1130 return 0; 1131 } 1132 1133 static void 1134 post_fa(DECL_ARGS) 1135 { 1136 1137 if (NULL != n->next && MDOC_Fa == n->next->tok) 1138 print_word(","); 1139 } 1140 1141 static int 1142 pre_fd(DECL_ARGS) 1143 { 1144 1145 pre_syn(n); 1146 font_push('B'); 1147 return 1; 1148 } 1149 1150 static void 1151 post_fd(DECL_ARGS) 1152 { 1153 1154 font_pop(); 1155 outflags |= MMAN_br; 1156 } 1157 1158 static int 1159 pre_fl(DECL_ARGS) 1160 { 1161 1162 font_push('B'); 1163 print_word("\\-"); 1164 if (n->child != NULL) 1165 outflags &= ~MMAN_spc; 1166 return 1; 1167 } 1168 1169 static void 1170 post_fl(DECL_ARGS) 1171 { 1172 1173 font_pop(); 1174 if (!(n->child != NULL || 1175 n->next == NULL || 1176 n->next->type == ROFFT_TEXT || 1177 n->next->flags & NODE_LINE)) 1178 outflags &= ~MMAN_spc; 1179 } 1180 1181 static int 1182 pre_fn(DECL_ARGS) 1183 { 1184 1185 pre_syn(n); 1186 1187 n = n->child; 1188 if (NULL == n) 1189 return 0; 1190 1191 if (NODE_SYNPRETTY & n->flags) 1192 print_block(".HP 4n", MMAN_nl); 1193 1194 font_push('B'); 1195 print_node(meta, n); 1196 font_pop(); 1197 outflags &= ~MMAN_spc; 1198 print_word("("); 1199 outflags &= ~MMAN_spc; 1200 1201 n = n->next; 1202 if (NULL != n) 1203 pre_fa(meta, n); 1204 return 0; 1205 } 1206 1207 static void 1208 post_fn(DECL_ARGS) 1209 { 1210 1211 print_word(")"); 1212 if (NODE_SYNPRETTY & n->flags) { 1213 print_word(";"); 1214 outflags |= MMAN_PP; 1215 } 1216 } 1217 1218 static int 1219 pre_fo(DECL_ARGS) 1220 { 1221 1222 switch (n->type) { 1223 case ROFFT_BLOCK: 1224 pre_syn(n); 1225 break; 1226 case ROFFT_HEAD: 1227 if (n->child == NULL) 1228 return 0; 1229 if (NODE_SYNPRETTY & n->flags) 1230 print_block(".HP 4n", MMAN_nl); 1231 font_push('B'); 1232 break; 1233 case ROFFT_BODY: 1234 outflags &= ~(MMAN_spc | MMAN_nl); 1235 print_word("("); 1236 outflags &= ~MMAN_spc; 1237 break; 1238 default: 1239 break; 1240 } 1241 return 1; 1242 } 1243 1244 static void 1245 post_fo(DECL_ARGS) 1246 { 1247 1248 switch (n->type) { 1249 case ROFFT_HEAD: 1250 if (n->child != NULL) 1251 font_pop(); 1252 break; 1253 case ROFFT_BODY: 1254 post_fn(meta, n); 1255 break; 1256 default: 1257 break; 1258 } 1259 } 1260 1261 static int 1262 pre_ft(DECL_ARGS) 1263 { 1264 1265 pre_syn(n); 1266 font_push('I'); 1267 return 1; 1268 } 1269 1270 static int 1271 pre_in(DECL_ARGS) 1272 { 1273 1274 if (NODE_SYNPRETTY & n->flags) { 1275 pre_syn(n); 1276 font_push('B'); 1277 print_word("#include <"); 1278 outflags &= ~MMAN_spc; 1279 } else { 1280 print_word("<"); 1281 outflags &= ~MMAN_spc; 1282 font_push('I'); 1283 } 1284 return 1; 1285 } 1286 1287 static void 1288 post_in(DECL_ARGS) 1289 { 1290 1291 if (NODE_SYNPRETTY & n->flags) { 1292 outflags &= ~MMAN_spc; 1293 print_word(">"); 1294 font_pop(); 1295 outflags |= MMAN_br; 1296 } else { 1297 font_pop(); 1298 outflags &= ~MMAN_spc; 1299 print_word(">"); 1300 } 1301 } 1302 1303 static int 1304 pre_it(DECL_ARGS) 1305 { 1306 const struct roff_node *bln; 1307 1308 switch (n->type) { 1309 case ROFFT_HEAD: 1310 outflags |= MMAN_PP | MMAN_nl; 1311 bln = n->parent->parent; 1312 if (0 == bln->norm->Bl.comp || 1313 (NULL == n->parent->prev && 1314 NULL == bln->parent->prev)) 1315 outflags |= MMAN_sp; 1316 outflags &= ~MMAN_br; 1317 switch (bln->norm->Bl.type) { 1318 case LIST_item: 1319 return 0; 1320 case LIST_inset: 1321 case LIST_diag: 1322 case LIST_ohang: 1323 if (bln->norm->Bl.type == LIST_diag) 1324 print_line(".B \"", 0); 1325 else 1326 print_line(".R \"", 0); 1327 outflags &= ~MMAN_spc; 1328 return 1; 1329 case LIST_bullet: 1330 case LIST_dash: 1331 case LIST_hyphen: 1332 print_width(&bln->norm->Bl, NULL); 1333 TPremain = 0; 1334 outflags |= MMAN_nl; 1335 font_push('B'); 1336 if (LIST_bullet == bln->norm->Bl.type) 1337 print_word("\\(bu"); 1338 else 1339 print_word("-"); 1340 font_pop(); 1341 outflags |= MMAN_nl; 1342 return 0; 1343 case LIST_enum: 1344 print_width(&bln->norm->Bl, NULL); 1345 TPremain = 0; 1346 outflags |= MMAN_nl; 1347 print_count(&bln->norm->Bl.count); 1348 outflags |= MMAN_nl; 1349 return 0; 1350 case LIST_hang: 1351 print_width(&bln->norm->Bl, n->child); 1352 TPremain = 0; 1353 outflags |= MMAN_nl; 1354 return 1; 1355 case LIST_tag: 1356 print_width(&bln->norm->Bl, n->child); 1357 putchar('\n'); 1358 outflags &= ~MMAN_spc; 1359 return 1; 1360 default: 1361 return 1; 1362 } 1363 default: 1364 break; 1365 } 1366 return 1; 1367 } 1368 1369 /* 1370 * This function is called after closing out an indented block. 1371 * If we are inside an enclosing list, restore its indentation. 1372 */ 1373 static void 1374 mid_it(void) 1375 { 1376 char buf[24]; 1377 1378 /* Nothing to do outside a list. */ 1379 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1]) 1380 return; 1381 1382 /* The indentation has already been set up. */ 1383 if (Bl_stack_post[Bl_stack_len - 1]) 1384 return; 1385 1386 /* Restore the indentation of the enclosing list. */ 1387 print_line(".RS", MMAN_Bk_susp); 1388 (void)snprintf(buf, sizeof(buf), "%dn", 1389 Bl_stack[Bl_stack_len - 1]); 1390 print_word(buf); 1391 1392 /* Remeber to close out this .RS block later. */ 1393 Bl_stack_post[Bl_stack_len - 1] = 1; 1394 } 1395 1396 static void 1397 post_it(DECL_ARGS) 1398 { 1399 const struct roff_node *bln; 1400 1401 bln = n->parent->parent; 1402 1403 switch (n->type) { 1404 case ROFFT_HEAD: 1405 switch (bln->norm->Bl.type) { 1406 case LIST_diag: 1407 outflags &= ~MMAN_spc; 1408 print_word("\\ "); 1409 break; 1410 case LIST_ohang: 1411 outflags |= MMAN_br; 1412 break; 1413 default: 1414 break; 1415 } 1416 break; 1417 case ROFFT_BODY: 1418 switch (bln->norm->Bl.type) { 1419 case LIST_bullet: 1420 case LIST_dash: 1421 case LIST_hyphen: 1422 case LIST_enum: 1423 case LIST_hang: 1424 case LIST_tag: 1425 assert(Bl_stack_len); 1426 Bl_stack[--Bl_stack_len] = 0; 1427 1428 /* 1429 * Our indentation had to be restored 1430 * after a child display or child list. 1431 * Close out that indentation block now. 1432 */ 1433 if (Bl_stack_post[Bl_stack_len]) { 1434 print_line(".RE", MMAN_nl); 1435 Bl_stack_post[Bl_stack_len] = 0; 1436 } 1437 break; 1438 case LIST_column: 1439 if (NULL != n->next) { 1440 putchar('\t'); 1441 outflags &= ~MMAN_spc; 1442 } 1443 break; 1444 default: 1445 break; 1446 } 1447 break; 1448 default: 1449 break; 1450 } 1451 } 1452 1453 static void 1454 post_lb(DECL_ARGS) 1455 { 1456 1457 if (SEC_LIBRARY == n->sec) 1458 outflags |= MMAN_br; 1459 } 1460 1461 static int 1462 pre_lk(DECL_ARGS) 1463 { 1464 const struct roff_node *link, *descr; 1465 1466 if (NULL == (link = n->child)) 1467 return 0; 1468 1469 if (NULL != (descr = link->next)) { 1470 font_push('I'); 1471 while (NULL != descr) { 1472 print_word(descr->string); 1473 descr = descr->next; 1474 } 1475 print_word(":"); 1476 font_pop(); 1477 } 1478 1479 font_push('B'); 1480 print_word(link->string); 1481 font_pop(); 1482 return 0; 1483 } 1484 1485 static int 1486 pre_ll(DECL_ARGS) 1487 { 1488 1489 print_line(".ll", 0); 1490 return 1; 1491 } 1492 1493 static int 1494 pre_li(DECL_ARGS) 1495 { 1496 1497 font_push('R'); 1498 return 1; 1499 } 1500 1501 static int 1502 pre_nm(DECL_ARGS) 1503 { 1504 char *name; 1505 1506 if (n->type == ROFFT_BLOCK) { 1507 outflags |= MMAN_Bk; 1508 pre_syn(n); 1509 } 1510 if (n->type != ROFFT_ELEM && n->type != ROFFT_HEAD) 1511 return 1; 1512 name = n->child == NULL ? NULL : n->child->string; 1513 if (NULL == name) 1514 return 0; 1515 if (n->type == ROFFT_HEAD) { 1516 if (NULL == n->parent->prev) 1517 outflags |= MMAN_sp; 1518 print_block(".HP", 0); 1519 printf(" %zun", strlen(name) + 1); 1520 outflags |= MMAN_nl; 1521 } 1522 font_push('B'); 1523 return 1; 1524 } 1525 1526 static void 1527 post_nm(DECL_ARGS) 1528 { 1529 1530 switch (n->type) { 1531 case ROFFT_BLOCK: 1532 outflags &= ~MMAN_Bk; 1533 break; 1534 case ROFFT_HEAD: 1535 case ROFFT_ELEM: 1536 if (n->child != NULL && n->child->string != NULL) 1537 font_pop(); 1538 break; 1539 default: 1540 break; 1541 } 1542 } 1543 1544 static int 1545 pre_no(DECL_ARGS) 1546 { 1547 1548 outflags |= MMAN_spc_force; 1549 return 1; 1550 } 1551 1552 static int 1553 pre_ns(DECL_ARGS) 1554 { 1555 1556 outflags &= ~MMAN_spc; 1557 return 0; 1558 } 1559 1560 static void 1561 post_pf(DECL_ARGS) 1562 { 1563 1564 if ( ! (n->next == NULL || n->next->flags & NODE_LINE)) 1565 outflags &= ~MMAN_spc; 1566 } 1567 1568 static int 1569 pre_pp(DECL_ARGS) 1570 { 1571 1572 if (MDOC_It != n->parent->tok) 1573 outflags |= MMAN_PP; 1574 outflags |= MMAN_sp | MMAN_nl; 1575 outflags &= ~MMAN_br; 1576 return 0; 1577 } 1578 1579 static int 1580 pre_rs(DECL_ARGS) 1581 { 1582 1583 if (SEC_SEE_ALSO == n->sec) { 1584 outflags |= MMAN_PP | MMAN_sp | MMAN_nl; 1585 outflags &= ~MMAN_br; 1586 } 1587 return 1; 1588 } 1589 1590 static int 1591 pre_skip(DECL_ARGS) 1592 { 1593 1594 return 0; 1595 } 1596 1597 static int 1598 pre_sm(DECL_ARGS) 1599 { 1600 1601 if (NULL == n->child) 1602 outflags ^= MMAN_Sm; 1603 else if (0 == strcmp("on", n->child->string)) 1604 outflags |= MMAN_Sm; 1605 else 1606 outflags &= ~MMAN_Sm; 1607 1608 if (MMAN_Sm & outflags) 1609 outflags |= MMAN_spc; 1610 1611 return 0; 1612 } 1613 1614 static int 1615 pre_sp(DECL_ARGS) 1616 { 1617 1618 if (MMAN_PP & outflags) { 1619 outflags &= ~MMAN_PP; 1620 print_line(".PP", 0); 1621 } else 1622 print_line(".sp", 0); 1623 return 1; 1624 } 1625 1626 static void 1627 post_sp(DECL_ARGS) 1628 { 1629 1630 outflags |= MMAN_nl; 1631 } 1632 1633 static int 1634 pre_sy(DECL_ARGS) 1635 { 1636 1637 font_push('B'); 1638 return 1; 1639 } 1640 1641 static int 1642 pre_vt(DECL_ARGS) 1643 { 1644 1645 if (NODE_SYNPRETTY & n->flags) { 1646 switch (n->type) { 1647 case ROFFT_BLOCK: 1648 pre_syn(n); 1649 return 1; 1650 case ROFFT_BODY: 1651 break; 1652 default: 1653 return 0; 1654 } 1655 } 1656 font_push('I'); 1657 return 1; 1658 } 1659 1660 static void 1661 post_vt(DECL_ARGS) 1662 { 1663 1664 if (n->flags & NODE_SYNPRETTY && n->type != ROFFT_BODY) 1665 return; 1666 font_pop(); 1667 } 1668 1669 static int 1670 pre_xr(DECL_ARGS) 1671 { 1672 1673 n = n->child; 1674 if (NULL == n) 1675 return 0; 1676 print_node(meta, n); 1677 n = n->next; 1678 if (NULL == n) 1679 return 0; 1680 outflags &= ~MMAN_spc; 1681 print_word("("); 1682 print_node(meta, n); 1683 print_word(")"); 1684 return 0; 1685 } 1686