1 /* $Id: mdoc.c,v 1.91 2012/07/18 11:09:30 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2010, 2012 Ingo Schwarze <schwarze@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #include <sys/types.h> 19 20 #include <assert.h> 21 #include <stdarg.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <time.h> 26 27 #include "mdoc.h" 28 #include "mandoc.h" 29 #include "libmdoc.h" 30 #include "libmandoc.h" 31 32 const char *const __mdoc_macronames[MDOC_MAX] = { 33 "Ap", "Dd", "Dt", "Os", 34 "Sh", "Ss", "Pp", "D1", 35 "Dl", "Bd", "Ed", "Bl", 36 "El", "It", "Ad", "An", 37 "Ar", "Cd", "Cm", "Dv", 38 "Er", "Ev", "Ex", "Fa", 39 "Fd", "Fl", "Fn", "Ft", 40 "Ic", "In", "Li", "Nd", 41 "Nm", "Op", "Ot", "Pa", 42 "Rv", "St", "Va", "Vt", 43 /* LINTED */ 44 "Xr", "%A", "%B", "%D", 45 /* LINTED */ 46 "%I", "%J", "%N", "%O", 47 /* LINTED */ 48 "%P", "%R", "%T", "%V", 49 "Ac", "Ao", "Aq", "At", 50 "Bc", "Bf", "Bo", "Bq", 51 "Bsx", "Bx", "Db", "Dc", 52 "Do", "Dq", "Ec", "Ef", 53 "Em", "Eo", "Fx", "Ms", 54 "No", "Ns", "Nx", "Ox", 55 "Pc", "Pf", "Po", "Pq", 56 "Qc", "Ql", "Qo", "Qq", 57 "Re", "Rs", "Sc", "So", 58 "Sq", "Sm", "Sx", "Sy", 59 "Tn", "Ux", "Xc", "Xo", 60 "Fo", "Fc", "Oo", "Oc", 61 "Bk", "Ek", "Bt", "Hf", 62 "Fr", "Ud", "Lb", "Lp", 63 "Lk", "Mt", "Brq", "Bro", 64 /* LINTED */ 65 "Brc", "%C", "Es", "En", 66 /* LINTED */ 67 "Dx", "%Q", "br", "sp", 68 /* LINTED */ 69 "%U", "Ta" 70 }; 71 72 const char *const __mdoc_argnames[MDOC_ARG_MAX] = { 73 "split", "nosplit", "ragged", 74 "unfilled", "literal", "file", 75 "offset", "bullet", "dash", 76 "hyphen", "item", "enum", 77 "tag", "diag", "hang", 78 "ohang", "inset", "column", 79 "width", "compact", "std", 80 "filled", "words", "emphasis", 81 "symbolic", "nested", "centered" 82 }; 83 84 const char * const *mdoc_macronames = __mdoc_macronames; 85 const char * const *mdoc_argnames = __mdoc_argnames; 86 87 static void mdoc_node_free(struct mdoc_node *); 88 static void mdoc_node_unlink(struct mdoc *, 89 struct mdoc_node *); 90 static void mdoc_free1(struct mdoc *); 91 static void mdoc_alloc1(struct mdoc *); 92 static struct mdoc_node *node_alloc(struct mdoc *, int, int, 93 enum mdoct, enum mdoc_type); 94 static int node_append(struct mdoc *, 95 struct mdoc_node *); 96 #if 0 97 static int mdoc_preptext(struct mdoc *, int, char *, int); 98 #endif 99 static int mdoc_ptext(struct mdoc *, int, char *, int); 100 static int mdoc_pmacro(struct mdoc *, int, char *, int); 101 102 const struct mdoc_node * 103 mdoc_node(const struct mdoc *m) 104 { 105 106 assert( ! (MDOC_HALT & m->flags)); 107 return(m->first); 108 } 109 110 111 const struct mdoc_meta * 112 mdoc_meta(const struct mdoc *m) 113 { 114 115 assert( ! (MDOC_HALT & m->flags)); 116 return(&m->meta); 117 } 118 119 120 /* 121 * Frees volatile resources (parse tree, meta-data, fields). 122 */ 123 static void 124 mdoc_free1(struct mdoc *mdoc) 125 { 126 127 if (mdoc->first) 128 mdoc_node_delete(mdoc, mdoc->first); 129 if (mdoc->meta.title) 130 free(mdoc->meta.title); 131 if (mdoc->meta.os) 132 free(mdoc->meta.os); 133 if (mdoc->meta.name) 134 free(mdoc->meta.name); 135 if (mdoc->meta.arch) 136 free(mdoc->meta.arch); 137 if (mdoc->meta.vol) 138 free(mdoc->meta.vol); 139 if (mdoc->meta.msec) 140 free(mdoc->meta.msec); 141 if (mdoc->meta.date) 142 free(mdoc->meta.date); 143 } 144 145 146 /* 147 * Allocate all volatile resources (parse tree, meta-data, fields). 148 */ 149 static void 150 mdoc_alloc1(struct mdoc *mdoc) 151 { 152 153 memset(&mdoc->meta, 0, sizeof(struct mdoc_meta)); 154 mdoc->flags = 0; 155 mdoc->lastnamed = mdoc->lastsec = SEC_NONE; 156 mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node)); 157 mdoc->first = mdoc->last; 158 mdoc->last->type = MDOC_ROOT; 159 mdoc->last->tok = MDOC_MAX; 160 mdoc->next = MDOC_NEXT_CHILD; 161 } 162 163 164 /* 165 * Free up volatile resources (see mdoc_free1()) then re-initialises the 166 * data with mdoc_alloc1(). After invocation, parse data has been reset 167 * and the parser is ready for re-invocation on a new tree; however, 168 * cross-parse non-volatile data is kept intact. 169 */ 170 void 171 mdoc_reset(struct mdoc *mdoc) 172 { 173 174 mdoc_free1(mdoc); 175 mdoc_alloc1(mdoc); 176 } 177 178 179 /* 180 * Completely free up all volatile and non-volatile parse resources. 181 * After invocation, the pointer is no longer usable. 182 */ 183 void 184 mdoc_free(struct mdoc *mdoc) 185 { 186 187 mdoc_free1(mdoc); 188 free(mdoc); 189 } 190 191 192 /* 193 * Allocate volatile and non-volatile parse resources. 194 */ 195 struct mdoc * 196 mdoc_alloc(struct roff *roff, struct mparse *parse, char *defos) 197 { 198 struct mdoc *p; 199 200 p = mandoc_calloc(1, sizeof(struct mdoc)); 201 202 p->parse = parse; 203 p->defos = defos; 204 p->roff = roff; 205 206 mdoc_hash_init(); 207 mdoc_alloc1(p); 208 return(p); 209 } 210 211 212 /* 213 * Climb back up the parse tree, validating open scopes. Mostly calls 214 * through to macro_end() in macro.c. 215 */ 216 int 217 mdoc_endparse(struct mdoc *m) 218 { 219 220 assert( ! (MDOC_HALT & m->flags)); 221 if (mdoc_macroend(m)) 222 return(1); 223 m->flags |= MDOC_HALT; 224 return(0); 225 } 226 227 int 228 mdoc_addeqn(struct mdoc *m, const struct eqn *ep) 229 { 230 struct mdoc_node *n; 231 232 assert( ! (MDOC_HALT & m->flags)); 233 234 /* No text before an initial macro. */ 235 236 if (SEC_NONE == m->lastnamed) { 237 mdoc_pmsg(m, ep->ln, ep->pos, MANDOCERR_NOTEXT); 238 return(1); 239 } 240 241 n = node_alloc(m, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN); 242 n->eqn = ep; 243 244 if ( ! node_append(m, n)) 245 return(0); 246 247 m->next = MDOC_NEXT_SIBLING; 248 return(1); 249 } 250 251 int 252 mdoc_addspan(struct mdoc *m, const struct tbl_span *sp) 253 { 254 struct mdoc_node *n; 255 256 assert( ! (MDOC_HALT & m->flags)); 257 258 /* No text before an initial macro. */ 259 260 if (SEC_NONE == m->lastnamed) { 261 mdoc_pmsg(m, sp->line, 0, MANDOCERR_NOTEXT); 262 return(1); 263 } 264 265 n = node_alloc(m, sp->line, 0, MDOC_MAX, MDOC_TBL); 266 n->span = sp; 267 268 if ( ! node_append(m, n)) 269 return(0); 270 271 m->next = MDOC_NEXT_SIBLING; 272 return(1); 273 } 274 275 276 /* 277 * Main parse routine. Parses a single line -- really just hands off to 278 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()). 279 */ 280 int 281 mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs) 282 { 283 284 assert( ! (MDOC_HALT & m->flags)); 285 286 m->flags |= MDOC_NEWLINE; 287 288 /* 289 * Let the roff nS register switch SYNOPSIS mode early, 290 * such that the parser knows at all times 291 * whether this mode is on or off. 292 * Note that this mode is also switched by the Sh macro. 293 */ 294 if (roff_regisset(m->roff, REG_nS)) { 295 if (roff_regget(m->roff, REG_nS)) 296 m->flags |= MDOC_SYNOPSIS; 297 else 298 m->flags &= ~MDOC_SYNOPSIS; 299 } 300 301 return(roff_getcontrol(m->roff, buf, &offs) ? 302 mdoc_pmacro(m, ln, buf, offs) : 303 mdoc_ptext(m, ln, buf, offs)); 304 } 305 306 int 307 mdoc_macro(MACRO_PROT_ARGS) 308 { 309 assert(tok < MDOC_MAX); 310 311 /* If we're in the body, deny prologue calls. */ 312 313 if (MDOC_PROLOGUE & mdoc_macros[tok].flags && 314 MDOC_PBODY & m->flags) { 315 mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY); 316 return(1); 317 } 318 319 /* If we're in the prologue, deny "body" macros. */ 320 321 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) && 322 ! (MDOC_PBODY & m->flags)) { 323 mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG); 324 if (NULL == m->meta.msec) 325 m->meta.msec = mandoc_strdup("1"); 326 if (NULL == m->meta.title) 327 m->meta.title = mandoc_strdup("UNKNOWN"); 328 if (NULL == m->meta.vol) 329 m->meta.vol = mandoc_strdup("LOCAL"); 330 if (NULL == m->meta.os) 331 m->meta.os = mandoc_strdup("LOCAL"); 332 if (NULL == m->meta.date) 333 m->meta.date = mandoc_normdate 334 (m->parse, NULL, line, ppos); 335 m->flags |= MDOC_PBODY; 336 } 337 338 return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf)); 339 } 340 341 342 static int 343 node_append(struct mdoc *mdoc, struct mdoc_node *p) 344 { 345 346 assert(mdoc->last); 347 assert(mdoc->first); 348 assert(MDOC_ROOT != p->type); 349 350 switch (mdoc->next) { 351 case (MDOC_NEXT_SIBLING): 352 mdoc->last->next = p; 353 p->prev = mdoc->last; 354 p->parent = mdoc->last->parent; 355 break; 356 case (MDOC_NEXT_CHILD): 357 mdoc->last->child = p; 358 p->parent = mdoc->last; 359 break; 360 default: 361 abort(); 362 /* NOTREACHED */ 363 } 364 365 p->parent->nchild++; 366 367 /* 368 * Copy over the normalised-data pointer of our parent. Not 369 * everybody has one, but copying a null pointer is fine. 370 */ 371 372 switch (p->type) { 373 case (MDOC_BODY): 374 /* FALLTHROUGH */ 375 case (MDOC_TAIL): 376 /* FALLTHROUGH */ 377 case (MDOC_HEAD): 378 p->norm = p->parent->norm; 379 break; 380 default: 381 break; 382 } 383 384 if ( ! mdoc_valid_pre(mdoc, p)) 385 return(0); 386 387 switch (p->type) { 388 case (MDOC_HEAD): 389 assert(MDOC_BLOCK == p->parent->type); 390 p->parent->head = p; 391 break; 392 case (MDOC_TAIL): 393 assert(MDOC_BLOCK == p->parent->type); 394 p->parent->tail = p; 395 break; 396 case (MDOC_BODY): 397 if (p->end) 398 break; 399 assert(MDOC_BLOCK == p->parent->type); 400 p->parent->body = p; 401 break; 402 default: 403 break; 404 } 405 406 mdoc->last = p; 407 408 switch (p->type) { 409 case (MDOC_TBL): 410 /* FALLTHROUGH */ 411 case (MDOC_TEXT): 412 if ( ! mdoc_valid_post(mdoc)) 413 return(0); 414 break; 415 default: 416 break; 417 } 418 419 return(1); 420 } 421 422 423 static struct mdoc_node * 424 node_alloc(struct mdoc *m, int line, int pos, 425 enum mdoct tok, enum mdoc_type type) 426 { 427 struct mdoc_node *p; 428 429 p = mandoc_calloc(1, sizeof(struct mdoc_node)); 430 p->sec = m->lastsec; 431 p->line = line; 432 p->pos = pos; 433 p->tok = tok; 434 p->type = type; 435 436 /* Flag analysis. */ 437 438 if (MDOC_SYNOPSIS & m->flags) 439 p->flags |= MDOC_SYNPRETTY; 440 else 441 p->flags &= ~MDOC_SYNPRETTY; 442 if (MDOC_NEWLINE & m->flags) 443 p->flags |= MDOC_LINE; 444 m->flags &= ~MDOC_NEWLINE; 445 446 return(p); 447 } 448 449 450 int 451 mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok) 452 { 453 struct mdoc_node *p; 454 455 p = node_alloc(m, line, pos, tok, MDOC_TAIL); 456 if ( ! node_append(m, p)) 457 return(0); 458 m->next = MDOC_NEXT_CHILD; 459 return(1); 460 } 461 462 463 int 464 mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok) 465 { 466 struct mdoc_node *p; 467 468 assert(m->first); 469 assert(m->last); 470 471 p = node_alloc(m, line, pos, tok, MDOC_HEAD); 472 if ( ! node_append(m, p)) 473 return(0); 474 m->next = MDOC_NEXT_CHILD; 475 return(1); 476 } 477 478 479 int 480 mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok) 481 { 482 struct mdoc_node *p; 483 484 p = node_alloc(m, line, pos, tok, MDOC_BODY); 485 if ( ! node_append(m, p)) 486 return(0); 487 m->next = MDOC_NEXT_CHILD; 488 return(1); 489 } 490 491 492 int 493 mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok, 494 struct mdoc_node *body, enum mdoc_endbody end) 495 { 496 struct mdoc_node *p; 497 498 p = node_alloc(m, line, pos, tok, MDOC_BODY); 499 p->pending = body; 500 p->end = end; 501 if ( ! node_append(m, p)) 502 return(0); 503 m->next = MDOC_NEXT_SIBLING; 504 return(1); 505 } 506 507 508 int 509 mdoc_block_alloc(struct mdoc *m, int line, int pos, 510 enum mdoct tok, struct mdoc_arg *args) 511 { 512 struct mdoc_node *p; 513 514 p = node_alloc(m, line, pos, tok, MDOC_BLOCK); 515 p->args = args; 516 if (p->args) 517 (args->refcnt)++; 518 519 switch (tok) { 520 case (MDOC_Bd): 521 /* FALLTHROUGH */ 522 case (MDOC_Bf): 523 /* FALLTHROUGH */ 524 case (MDOC_Bl): 525 /* FALLTHROUGH */ 526 case (MDOC_Rs): 527 p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); 528 break; 529 default: 530 break; 531 } 532 533 if ( ! node_append(m, p)) 534 return(0); 535 m->next = MDOC_NEXT_CHILD; 536 return(1); 537 } 538 539 540 int 541 mdoc_elem_alloc(struct mdoc *m, int line, int pos, 542 enum mdoct tok, struct mdoc_arg *args) 543 { 544 struct mdoc_node *p; 545 546 p = node_alloc(m, line, pos, tok, MDOC_ELEM); 547 p->args = args; 548 if (p->args) 549 (args->refcnt)++; 550 551 switch (tok) { 552 case (MDOC_An): 553 p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); 554 break; 555 default: 556 break; 557 } 558 559 if ( ! node_append(m, p)) 560 return(0); 561 m->next = MDOC_NEXT_CHILD; 562 return(1); 563 } 564 565 int 566 mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p) 567 { 568 struct mdoc_node *n; 569 570 n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT); 571 n->string = roff_strdup(m->roff, p); 572 573 if ( ! node_append(m, n)) 574 return(0); 575 576 m->next = MDOC_NEXT_SIBLING; 577 return(1); 578 } 579 580 581 static void 582 mdoc_node_free(struct mdoc_node *p) 583 { 584 585 if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type) 586 free(p->norm); 587 if (p->string) 588 free(p->string); 589 if (p->args) 590 mdoc_argv_free(p->args); 591 free(p); 592 } 593 594 595 static void 596 mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n) 597 { 598 599 /* Adjust siblings. */ 600 601 if (n->prev) 602 n->prev->next = n->next; 603 if (n->next) 604 n->next->prev = n->prev; 605 606 /* Adjust parent. */ 607 608 if (n->parent) { 609 n->parent->nchild--; 610 if (n->parent->child == n) 611 n->parent->child = n->prev ? n->prev : n->next; 612 if (n->parent->last == n) 613 n->parent->last = n->prev ? n->prev : NULL; 614 } 615 616 /* Adjust parse point, if applicable. */ 617 618 if (m && m->last == n) { 619 if (n->prev) { 620 m->last = n->prev; 621 m->next = MDOC_NEXT_SIBLING; 622 } else { 623 m->last = n->parent; 624 m->next = MDOC_NEXT_CHILD; 625 } 626 } 627 628 if (m && m->first == n) 629 m->first = NULL; 630 } 631 632 633 void 634 mdoc_node_delete(struct mdoc *m, struct mdoc_node *p) 635 { 636 637 while (p->child) { 638 assert(p->nchild); 639 mdoc_node_delete(m, p->child); 640 } 641 assert(0 == p->nchild); 642 643 mdoc_node_unlink(m, p); 644 mdoc_node_free(p); 645 } 646 647 int 648 mdoc_node_relink(struct mdoc *m, struct mdoc_node *p) 649 { 650 651 mdoc_node_unlink(m, p); 652 return(node_append(m, p)); 653 } 654 655 #if 0 656 /* 657 * Pre-treat a text line. 658 * Text lines can consist of equations, which must be handled apart from 659 * the regular text. 660 * Thus, use this function to step through a line checking if it has any 661 * equations embedded in it. 662 * This must handle multiple equations AND equations that do not end at 663 * the end-of-line, i.e., will re-enter in the next roff parse. 664 */ 665 static int 666 mdoc_preptext(struct mdoc *m, int line, char *buf, int offs) 667 { 668 char *start, *end; 669 char delim; 670 671 while ('\0' != buf[offs]) { 672 /* Mark starting position if eqn is set. */ 673 start = NULL; 674 if ('\0' != (delim = roff_eqndelim(m->roff))) 675 if (NULL != (start = strchr(buf + offs, delim))) 676 *start++ = '\0'; 677 678 /* Parse text as normal. */ 679 if ( ! mdoc_ptext(m, line, buf, offs)) 680 return(0); 681 682 /* Continue only if an equation exists. */ 683 if (NULL == start) 684 break; 685 686 /* Read past the end of the equation. */ 687 offs += start - (buf + offs); 688 assert(start == &buf[offs]); 689 if (NULL != (end = strchr(buf + offs, delim))) { 690 *end++ = '\0'; 691 while (' ' == *end) 692 end++; 693 } 694 695 /* Parse the equation itself. */ 696 roff_openeqn(m->roff, NULL, line, offs, buf); 697 698 /* Process a finished equation? */ 699 if (roff_closeeqn(m->roff)) 700 if ( ! mdoc_addeqn(m, roff_eqn(m->roff))) 701 return(0); 702 offs += (end - (buf + offs)); 703 } 704 705 return(1); 706 } 707 #endif 708 709 /* 710 * Parse free-form text, that is, a line that does not begin with the 711 * control character. 712 */ 713 static int 714 mdoc_ptext(struct mdoc *m, int line, char *buf, int offs) 715 { 716 char *c, *ws, *end; 717 struct mdoc_node *n; 718 719 /* No text before an initial macro. */ 720 721 if (SEC_NONE == m->lastnamed) { 722 mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT); 723 return(1); 724 } 725 726 assert(m->last); 727 n = m->last; 728 729 /* 730 * Divert directly to list processing if we're encountering a 731 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry 732 * (a MDOC_BODY means it's already open, in which case we should 733 * process within its context in the normal way). 734 */ 735 736 if (MDOC_Bl == n->tok && MDOC_BODY == n->type && 737 LIST_column == n->norm->Bl.type) { 738 /* `Bl' is open without any children. */ 739 m->flags |= MDOC_FREECOL; 740 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf)); 741 } 742 743 if (MDOC_It == n->tok && MDOC_BLOCK == n->type && 744 NULL != n->parent && 745 MDOC_Bl == n->parent->tok && 746 LIST_column == n->parent->norm->Bl.type) { 747 /* `Bl' has block-level `It' children. */ 748 m->flags |= MDOC_FREECOL; 749 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf)); 750 } 751 752 /* 753 * Search for the beginning of unescaped trailing whitespace (ws) 754 * and for the first character not to be output (end). 755 */ 756 757 /* FIXME: replace with strcspn(). */ 758 ws = NULL; 759 for (c = end = buf + offs; *c; c++) { 760 switch (*c) { 761 case ' ': 762 if (NULL == ws) 763 ws = c; 764 continue; 765 case '\t': 766 /* 767 * Always warn about trailing tabs, 768 * even outside literal context, 769 * where they should be put on the next line. 770 */ 771 if (NULL == ws) 772 ws = c; 773 /* 774 * Strip trailing tabs in literal context only; 775 * outside, they affect the next line. 776 */ 777 if (MDOC_LITERAL & m->flags) 778 continue; 779 break; 780 case '\\': 781 /* Skip the escaped character, too, if any. */ 782 if (c[1]) 783 c++; 784 /* FALLTHROUGH */ 785 default: 786 ws = NULL; 787 break; 788 } 789 end = c + 1; 790 } 791 *end = '\0'; 792 793 if (ws) 794 mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE); 795 796 if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) { 797 mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN); 798 799 /* 800 * Insert a `sp' in the case of a blank line. Technically, 801 * blank lines aren't allowed, but enough manuals assume this 802 * behaviour that we want to work around it. 803 */ 804 if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL)) 805 return(0); 806 807 m->next = MDOC_NEXT_SIBLING; 808 809 return(mdoc_valid_post(m)); 810 } 811 812 if ( ! mdoc_word_alloc(m, line, offs, buf+offs)) 813 return(0); 814 815 if (MDOC_LITERAL & m->flags) 816 return(1); 817 818 /* 819 * End-of-sentence check. If the last character is an unescaped 820 * EOS character, then flag the node as being the end of a 821 * sentence. The front-end will know how to interpret this. 822 */ 823 824 assert(buf < end); 825 826 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0)) 827 m->last->flags |= MDOC_EOS; 828 829 return(1); 830 } 831 832 833 /* 834 * Parse a macro line, that is, a line beginning with the control 835 * character. 836 */ 837 static int 838 mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs) 839 { 840 enum mdoct tok; 841 int i, sv; 842 char mac[5]; 843 struct mdoc_node *n; 844 845 /* Empty post-control lines are ignored. */ 846 847 if ('"' == buf[offs]) { 848 mdoc_pmsg(m, ln, offs, MANDOCERR_BADCOMMENT); 849 return(1); 850 } else if ('\0' == buf[offs]) 851 return(1); 852 853 sv = offs; 854 855 /* 856 * Copy the first word into a nil-terminated buffer. 857 * Stop copying when a tab, space, or eoln is encountered. 858 */ 859 860 i = 0; 861 while (i < 4 && '\0' != buf[offs] && 862 ' ' != buf[offs] && '\t' != buf[offs]) 863 mac[i++] = buf[offs++]; 864 865 mac[i] = '\0'; 866 867 tok = (i > 1 || i < 4) ? mdoc_hash_find(mac) : MDOC_MAX; 868 869 if (MDOC_MAX == tok) { 870 mandoc_vmsg(MANDOCERR_MACRO, m->parse, 871 ln, sv, "%s", buf + sv - 1); 872 return(1); 873 } 874 875 /* Disregard the first trailing tab, if applicable. */ 876 877 if ('\t' == buf[offs]) 878 offs++; 879 880 /* Jump to the next non-whitespace word. */ 881 882 while (buf[offs] && ' ' == buf[offs]) 883 offs++; 884 885 /* 886 * Trailing whitespace. Note that tabs are allowed to be passed 887 * into the parser as "text", so we only warn about spaces here. 888 */ 889 890 if ('\0' == buf[offs] && ' ' == buf[offs - 1]) 891 mdoc_pmsg(m, ln, offs - 1, MANDOCERR_EOLNSPACE); 892 893 /* 894 * If an initial macro or a list invocation, divert directly 895 * into macro processing. 896 */ 897 898 if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) { 899 if ( ! mdoc_macro(m, tok, ln, sv, &offs, buf)) 900 goto err; 901 return(1); 902 } 903 904 n = m->last; 905 assert(m->last); 906 907 /* 908 * If the first macro of a `Bl -column', open an `It' block 909 * context around the parsed macro. 910 */ 911 912 if (MDOC_Bl == n->tok && MDOC_BODY == n->type && 913 LIST_column == n->norm->Bl.type) { 914 m->flags |= MDOC_FREECOL; 915 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf)) 916 goto err; 917 return(1); 918 } 919 920 /* 921 * If we're following a block-level `It' within a `Bl -column' 922 * context (perhaps opened in the above block or in ptext()), 923 * then open an `It' block context around the parsed macro. 924 */ 925 926 if (MDOC_It == n->tok && MDOC_BLOCK == n->type && 927 NULL != n->parent && 928 MDOC_Bl == n->parent->tok && 929 LIST_column == n->parent->norm->Bl.type) { 930 m->flags |= MDOC_FREECOL; 931 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf)) 932 goto err; 933 return(1); 934 } 935 936 /* Normal processing of a macro. */ 937 938 if ( ! mdoc_macro(m, tok, ln, sv, &offs, buf)) 939 goto err; 940 941 return(1); 942 943 err: /* Error out. */ 944 945 m->flags |= MDOC_HALT; 946 return(0); 947 } 948 949 enum mdelim 950 mdoc_isdelim(const char *p) 951 { 952 953 if ('\0' == p[0]) 954 return(DELIM_NONE); 955 956 if ('\0' == p[1]) 957 switch (p[0]) { 958 case('('): 959 /* FALLTHROUGH */ 960 case('['): 961 return(DELIM_OPEN); 962 case('|'): 963 return(DELIM_MIDDLE); 964 case('.'): 965 /* FALLTHROUGH */ 966 case(','): 967 /* FALLTHROUGH */ 968 case(';'): 969 /* FALLTHROUGH */ 970 case(':'): 971 /* FALLTHROUGH */ 972 case('?'): 973 /* FALLTHROUGH */ 974 case('!'): 975 /* FALLTHROUGH */ 976 case(')'): 977 /* FALLTHROUGH */ 978 case(']'): 979 return(DELIM_CLOSE); 980 default: 981 return(DELIM_NONE); 982 } 983 984 if ('\\' != p[0]) 985 return(DELIM_NONE); 986 987 if (0 == strcmp(p + 1, ".")) 988 return(DELIM_CLOSE); 989 if (0 == strcmp(p + 1, "fR|\\fP")) 990 return(DELIM_MIDDLE); 991 992 return(DELIM_NONE); 993 } 994