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