1 /* $OpenBSD: man.c,v 1.93 2014/11/28 06:26:46 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org> 5 * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 #include <sys/types.h> 20 21 #include <assert.h> 22 #include <ctype.h> 23 #include <stdarg.h> 24 #include <stdlib.h> 25 #include <stdio.h> 26 #include <string.h> 27 28 #include "man.h" 29 #include "mandoc.h" 30 #include "mandoc_aux.h" 31 #include "libman.h" 32 #include "libmandoc.h" 33 34 const char *const __man_macronames[MAN_MAX] = { 35 "br", "TH", "SH", "SS", 36 "TP", "LP", "PP", "P", 37 "IP", "HP", "SM", "SB", 38 "BI", "IB", "BR", "RB", 39 "R", "B", "I", "IR", 40 "RI", "na", "sp", "nf", 41 "fi", "RE", "RS", "DT", 42 "UC", "PD", "AT", "in", 43 "ft", "OP", "EX", "EE", 44 "UR", "UE", "ll" 45 }; 46 47 const char * const *man_macronames = __man_macronames; 48 49 static struct man_node *man_node_alloc(struct man *, int, int, 50 enum man_type, enum mant); 51 static void man_node_append(struct man *, struct man_node *); 52 static void man_node_free(struct man_node *); 53 static void man_node_unlink(struct man *, 54 struct man_node *); 55 static int man_ptext(struct man *, int, char *, int); 56 static int man_pmacro(struct man *, int, char *, int); 57 static void man_free1(struct man *); 58 static void man_alloc1(struct man *); 59 static void man_descope(struct man *, int, int); 60 61 62 const struct man_node * 63 man_node(const struct man *man) 64 { 65 66 return(man->first); 67 } 68 69 const struct man_meta * 70 man_meta(const struct man *man) 71 { 72 73 return(&man->meta); 74 } 75 76 void 77 man_reset(struct man *man) 78 { 79 80 man_free1(man); 81 man_alloc1(man); 82 } 83 84 void 85 man_free(struct man *man) 86 { 87 88 man_free1(man); 89 free(man); 90 } 91 92 struct man * 93 man_alloc(struct roff *roff, struct mparse *parse, int quick) 94 { 95 struct man *p; 96 97 p = mandoc_calloc(1, sizeof(struct man)); 98 99 man_hash_init(); 100 p->parse = parse; 101 p->quick = quick; 102 p->roff = roff; 103 104 man_alloc1(p); 105 return(p); 106 } 107 108 int 109 man_endparse(struct man *man) 110 { 111 112 man_macroend(man); 113 return(1); 114 } 115 116 int 117 man_parseln(struct man *man, int ln, char *buf, int offs) 118 { 119 120 if (man->last->type != MAN_EQN || ln > man->last->line) 121 man->flags |= MAN_NEWLINE; 122 123 return (roff_getcontrol(man->roff, buf, &offs) ? 124 man_pmacro(man, ln, buf, offs) : 125 man_ptext(man, ln, buf, offs)); 126 } 127 128 static void 129 man_free1(struct man *man) 130 { 131 132 if (man->first) 133 man_node_delete(man, man->first); 134 free(man->meta.title); 135 free(man->meta.source); 136 free(man->meta.date); 137 free(man->meta.vol); 138 free(man->meta.msec); 139 } 140 141 static void 142 man_alloc1(struct man *man) 143 { 144 145 memset(&man->meta, 0, sizeof(struct man_meta)); 146 man->flags = 0; 147 man->last = mandoc_calloc(1, sizeof(struct man_node)); 148 man->first = man->last; 149 man->last->type = MAN_ROOT; 150 man->last->tok = MAN_MAX; 151 man->next = MAN_NEXT_CHILD; 152 } 153 154 155 static void 156 man_node_append(struct man *man, struct man_node *p) 157 { 158 159 assert(man->last); 160 assert(man->first); 161 assert(p->type != MAN_ROOT); 162 163 switch (man->next) { 164 case MAN_NEXT_SIBLING: 165 man->last->next = p; 166 p->prev = man->last; 167 p->parent = man->last->parent; 168 break; 169 case MAN_NEXT_CHILD: 170 man->last->child = p; 171 p->parent = man->last; 172 break; 173 default: 174 abort(); 175 /* NOTREACHED */ 176 } 177 178 assert(p->parent); 179 p->parent->nchild++; 180 181 switch (p->type) { 182 case MAN_BLOCK: 183 if (p->tok == MAN_SH || p->tok == MAN_SS) 184 man->flags &= ~MAN_LITERAL; 185 break; 186 case MAN_HEAD: 187 assert(p->parent->type == MAN_BLOCK); 188 p->parent->head = p; 189 break; 190 case MAN_BODY: 191 assert(p->parent->type == MAN_BLOCK); 192 p->parent->body = p; 193 break; 194 default: 195 break; 196 } 197 198 man->last = p; 199 200 switch (p->type) { 201 case MAN_TBL: 202 /* FALLTHROUGH */ 203 case MAN_TEXT: 204 man_valid_post(man); 205 break; 206 default: 207 break; 208 } 209 } 210 211 static struct man_node * 212 man_node_alloc(struct man *man, int line, int pos, 213 enum man_type type, enum mant tok) 214 { 215 struct man_node *p; 216 217 p = mandoc_calloc(1, sizeof(struct man_node)); 218 p->line = line; 219 p->pos = pos; 220 p->type = type; 221 p->tok = tok; 222 223 if (man->flags & MAN_NEWLINE) 224 p->flags |= MAN_LINE; 225 man->flags &= ~MAN_NEWLINE; 226 return(p); 227 } 228 229 void 230 man_elem_alloc(struct man *man, int line, int pos, enum mant tok) 231 { 232 struct man_node *p; 233 234 p = man_node_alloc(man, line, pos, MAN_ELEM, tok); 235 man_node_append(man, p); 236 man->next = MAN_NEXT_CHILD; 237 } 238 239 void 240 man_head_alloc(struct man *man, int line, int pos, enum mant tok) 241 { 242 struct man_node *p; 243 244 p = man_node_alloc(man, line, pos, MAN_HEAD, tok); 245 man_node_append(man, p); 246 man->next = MAN_NEXT_CHILD; 247 } 248 249 void 250 man_body_alloc(struct man *man, int line, int pos, enum mant tok) 251 { 252 struct man_node *p; 253 254 p = man_node_alloc(man, line, pos, MAN_BODY, tok); 255 man_node_append(man, p); 256 man->next = MAN_NEXT_CHILD; 257 } 258 259 void 260 man_block_alloc(struct man *man, int line, int pos, enum mant tok) 261 { 262 struct man_node *p; 263 264 p = man_node_alloc(man, line, pos, MAN_BLOCK, tok); 265 man_node_append(man, p); 266 man->next = MAN_NEXT_CHILD; 267 } 268 269 void 270 man_word_alloc(struct man *man, int line, int pos, const char *word) 271 { 272 struct man_node *n; 273 274 n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX); 275 n->string = roff_strdup(man->roff, word); 276 man_node_append(man, n); 277 man->next = MAN_NEXT_SIBLING; 278 } 279 280 void 281 man_word_append(struct man *man, const char *word) 282 { 283 struct man_node *n; 284 char *addstr, *newstr; 285 286 n = man->last; 287 addstr = roff_strdup(man->roff, word); 288 mandoc_asprintf(&newstr, "%s %s", n->string, addstr); 289 free(addstr); 290 free(n->string); 291 n->string = newstr; 292 man->next = MAN_NEXT_SIBLING; 293 } 294 295 /* 296 * Free all of the resources held by a node. This does NOT unlink a 297 * node from its context; for that, see man_node_unlink(). 298 */ 299 static void 300 man_node_free(struct man_node *p) 301 { 302 303 free(p->string); 304 free(p); 305 } 306 307 void 308 man_node_delete(struct man *man, struct man_node *p) 309 { 310 311 while (p->child) 312 man_node_delete(man, p->child); 313 314 man_node_unlink(man, p); 315 man_node_free(p); 316 } 317 318 void 319 man_addeqn(struct man *man, const struct eqn *ep) 320 { 321 struct man_node *n; 322 323 n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX); 324 n->eqn = ep; 325 if (ep->ln > man->last->line) 326 n->flags |= MAN_LINE; 327 man_node_append(man, n); 328 man->next = MAN_NEXT_SIBLING; 329 man_descope(man, ep->ln, ep->pos); 330 } 331 332 void 333 man_addspan(struct man *man, const struct tbl_span *sp) 334 { 335 struct man_node *n; 336 337 n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX); 338 n->span = sp; 339 man_node_append(man, n); 340 man->next = MAN_NEXT_SIBLING; 341 man_descope(man, sp->line, 0); 342 } 343 344 static void 345 man_descope(struct man *man, int line, int offs) 346 { 347 /* 348 * Co-ordinate what happens with having a next-line scope open: 349 * first close out the element scope (if applicable), then close 350 * out the block scope (also if applicable). 351 */ 352 353 if (man->flags & MAN_ELINE) { 354 man->flags &= ~MAN_ELINE; 355 man_unscope(man, man->last->parent); 356 } 357 if ( ! (man->flags & MAN_BLINE)) 358 return; 359 man->flags &= ~MAN_BLINE; 360 man_unscope(man, man->last->parent); 361 man_body_alloc(man, line, offs, man->last->tok); 362 } 363 364 static int 365 man_ptext(struct man *man, int line, char *buf, int offs) 366 { 367 int i; 368 369 /* Literal free-form text whitespace is preserved. */ 370 371 if (man->flags & MAN_LITERAL) { 372 man_word_alloc(man, line, offs, buf + offs); 373 man_descope(man, line, offs); 374 return(1); 375 } 376 377 for (i = offs; buf[i] == ' '; i++) 378 /* Skip leading whitespace. */ ; 379 380 /* 381 * Blank lines are ignored right after headings 382 * but add a single vertical space elsewhere. 383 */ 384 385 if (buf[i] == '\0') { 386 /* Allocate a blank entry. */ 387 if (man->last->tok != MAN_SH && 388 man->last->tok != MAN_SS) { 389 man_elem_alloc(man, line, offs, MAN_sp); 390 man->next = MAN_NEXT_SIBLING; 391 } 392 return(1); 393 } 394 395 /* 396 * Warn if the last un-escaped character is whitespace. Then 397 * strip away the remaining spaces (tabs stay!). 398 */ 399 400 i = (int)strlen(buf); 401 assert(i); 402 403 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) { 404 if (i > 1 && '\\' != buf[i - 2]) 405 mandoc_msg(MANDOCERR_SPACE_EOL, man->parse, 406 line, i - 1, NULL); 407 408 for (--i; i && ' ' == buf[i]; i--) 409 /* Spin back to non-space. */ ; 410 411 /* Jump ahead of escaped whitespace. */ 412 i += '\\' == buf[i] ? 2 : 1; 413 414 buf[i] = '\0'; 415 } 416 man_word_alloc(man, line, offs, buf + offs); 417 418 /* 419 * End-of-sentence check. If the last character is an unescaped 420 * EOS character, then flag the node as being the end of a 421 * sentence. The front-end will know how to interpret this. 422 */ 423 424 assert(i); 425 if (mandoc_eos(buf, (size_t)i)) 426 man->last->flags |= MAN_EOS; 427 428 man_descope(man, line, offs); 429 return(1); 430 } 431 432 static int 433 man_pmacro(struct man *man, int ln, char *buf, int offs) 434 { 435 struct man_node *n; 436 const char *cp; 437 enum mant tok; 438 int i, ppos; 439 int bline; 440 char mac[5]; 441 442 ppos = offs; 443 444 /* 445 * Copy the first word into a nil-terminated buffer. 446 * Stop when a space, tab, escape, or eoln is encountered. 447 */ 448 449 i = 0; 450 while (i < 4 && strchr(" \t\\", buf[offs]) == NULL) 451 mac[i++] = buf[offs++]; 452 453 mac[i] = '\0'; 454 455 tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX; 456 457 if (tok == MAN_MAX) { 458 mandoc_msg(MANDOCERR_MACRO, man->parse, 459 ln, ppos, buf + ppos - 1); 460 return(1); 461 } 462 463 /* Skip a leading escape sequence or tab. */ 464 465 switch (buf[offs]) { 466 case '\\': 467 cp = buf + offs + 1; 468 mandoc_escape(&cp, NULL, NULL); 469 offs = cp - buf; 470 break; 471 case '\t': 472 offs++; 473 break; 474 default: 475 break; 476 } 477 478 /* Jump to the next non-whitespace word. */ 479 480 while (buf[offs] && buf[offs] == ' ') 481 offs++; 482 483 /* 484 * Trailing whitespace. Note that tabs are allowed to be passed 485 * into the parser as "text", so we only warn about spaces here. 486 */ 487 488 if (buf[offs] == '\0' && buf[offs - 1] == ' ') 489 mandoc_msg(MANDOCERR_SPACE_EOL, man->parse, 490 ln, offs - 1, NULL); 491 492 /* 493 * Remove prior ELINE macro, as it's being clobbered by a new 494 * macro. Note that NSCOPED macros do not close out ELINE 495 * macros---they don't print text---so we let those slip by. 496 */ 497 498 if ( ! (man_macros[tok].flags & MAN_NSCOPED) && 499 man->flags & MAN_ELINE) { 500 n = man->last; 501 assert(MAN_TEXT != n->type); 502 503 /* Remove repeated NSCOPED macros causing ELINE. */ 504 505 if (man_macros[n->tok].flags & MAN_NSCOPED) 506 n = n->parent; 507 508 mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, 509 n->pos, "%s breaks %s", man_macronames[tok], 510 man_macronames[n->tok]); 511 512 man_node_delete(man, n); 513 man->flags &= ~MAN_ELINE; 514 } 515 516 /* 517 * Remove prior BLINE macro that is being clobbered. 518 */ 519 if ((man->flags & MAN_BLINE) && 520 (man_macros[tok].flags & MAN_BSCOPE)) { 521 n = man->last; 522 523 /* Might be a text node like 8 in 524 * .TP 8 525 * .SH foo 526 */ 527 if (n->type == MAN_TEXT) 528 n = n->parent; 529 530 /* Remove element that didn't end BLINE, if any. */ 531 if ( ! (man_macros[n->tok].flags & MAN_BSCOPE)) 532 n = n->parent; 533 534 assert(n->type == MAN_HEAD); 535 n = n->parent; 536 assert(n->type == MAN_BLOCK); 537 assert(man_macros[n->tok].flags & MAN_SCOPED); 538 539 mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, 540 n->pos, "%s breaks %s", man_macronames[tok], 541 man_macronames[n->tok]); 542 543 man_node_delete(man, n); 544 man->flags &= ~MAN_BLINE; 545 } 546 547 /* Remember whether we are in next-line scope for a block head. */ 548 549 bline = man->flags & MAN_BLINE; 550 551 /* Call to handler... */ 552 553 assert(man_macros[tok].fp); 554 (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf); 555 556 /* In quick mode (for mandocdb), abort after the NAME section. */ 557 558 if (man->quick && tok == MAN_SH) { 559 n = man->last; 560 if (n->type == MAN_BODY && 561 strcmp(n->prev->child->string, "NAME")) 562 return(2); 563 } 564 565 /* 566 * If we are in a next-line scope for a block head, 567 * close it out now and switch to the body, 568 * unless the next-line scope is allowed to continue. 569 */ 570 571 if ( ! bline || man->flags & MAN_ELINE || 572 man_macros[tok].flags & MAN_NSCOPED) 573 return(1); 574 575 assert(man->flags & MAN_BLINE); 576 man->flags &= ~MAN_BLINE; 577 578 man_unscope(man, man->last->parent); 579 man_body_alloc(man, ln, ppos, man->last->tok); 580 return(1); 581 } 582 583 /* 584 * Unlink a node from its context. If "man" is provided, the last parse 585 * point will also be adjusted accordingly. 586 */ 587 static void 588 man_node_unlink(struct man *man, struct man_node *n) 589 { 590 591 /* Adjust siblings. */ 592 593 if (n->prev) 594 n->prev->next = n->next; 595 if (n->next) 596 n->next->prev = n->prev; 597 598 /* Adjust parent. */ 599 600 if (n->parent) { 601 n->parent->nchild--; 602 if (n->parent->child == n) 603 n->parent->child = n->prev ? n->prev : n->next; 604 } 605 606 /* Adjust parse point, if applicable. */ 607 608 if (man && man->last == n) { 609 /*XXX: this can occur when bailing from validation. */ 610 /*assert(NULL == n->next);*/ 611 if (n->prev) { 612 man->last = n->prev; 613 man->next = MAN_NEXT_SIBLING; 614 } else { 615 man->last = n->parent; 616 man->next = MAN_NEXT_CHILD; 617 } 618 } 619 620 if (man && man->first == n) 621 man->first = NULL; 622 } 623 624 const struct mparse * 625 man_mparse(const struct man *man) 626 { 627 628 assert(man && man->parse); 629 return(man->parse); 630 } 631 632 void 633 man_deroff(char **dest, const struct man_node *n) 634 { 635 char *cp; 636 size_t sz; 637 638 if (n->type != MAN_TEXT) { 639 for (n = n->child; n; n = n->next) 640 man_deroff(dest, n); 641 return; 642 } 643 644 /* Skip leading whitespace and escape sequences. */ 645 646 cp = n->string; 647 while ('\0' != *cp) { 648 if ('\\' == *cp) { 649 cp++; 650 mandoc_escape((const char **)&cp, NULL, NULL); 651 } else if (isspace((unsigned char)*cp)) 652 cp++; 653 else 654 break; 655 } 656 657 /* Skip trailing whitespace. */ 658 659 for (sz = strlen(cp); sz; sz--) 660 if (0 == isspace((unsigned char)cp[sz-1])) 661 break; 662 663 /* Skip empty strings. */ 664 665 if (0 == sz) 666 return; 667 668 if (NULL == *dest) { 669 *dest = mandoc_strndup(cp, sz); 670 return; 671 } 672 673 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp); 674 free(*dest); 675 *dest = cp; 676 } 677