1 /* $OpenBSD: man_validate.c,v 1.124 2020/04/24 11:58:02 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2010, 2012-2020 Ingo Schwarze <schwarze@openbsd.org> 4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 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 AUTHORS DISCLAIM ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 * Validation module for man(7) syntax trees used by mandoc(1). 19 */ 20 #include <sys/types.h> 21 22 #include <assert.h> 23 #include <ctype.h> 24 #include <errno.h> 25 #include <limits.h> 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <time.h> 31 32 #include "mandoc_aux.h" 33 #include "mandoc.h" 34 #include "roff.h" 35 #include "man.h" 36 #include "libmandoc.h" 37 #include "roff_int.h" 38 #include "libman.h" 39 #include "tag.h" 40 41 #define CHKARGS struct roff_man *man, struct roff_node *n 42 43 typedef void (*v_check)(CHKARGS); 44 45 static void check_abort(CHKARGS) __attribute__((__noreturn__)); 46 static void check_par(CHKARGS); 47 static void check_part(CHKARGS); 48 static void check_root(CHKARGS); 49 static void check_tag(struct roff_node *, struct roff_node *); 50 static void check_text(CHKARGS); 51 52 static void post_AT(CHKARGS); 53 static void post_EE(CHKARGS); 54 static void post_EX(CHKARGS); 55 static void post_IP(CHKARGS); 56 static void post_OP(CHKARGS); 57 static void post_SH(CHKARGS); 58 static void post_TH(CHKARGS); 59 static void post_TP(CHKARGS); 60 static void post_UC(CHKARGS); 61 static void post_UR(CHKARGS); 62 static void post_in(CHKARGS); 63 64 static const v_check man_valids[MAN_MAX - MAN_TH] = { 65 post_TH, /* TH */ 66 post_SH, /* SH */ 67 post_SH, /* SS */ 68 post_TP, /* TP */ 69 post_TP, /* TQ */ 70 check_abort,/* LP */ 71 check_par, /* PP */ 72 check_abort,/* P */ 73 post_IP, /* IP */ 74 NULL, /* HP */ 75 NULL, /* SM */ 76 NULL, /* SB */ 77 NULL, /* BI */ 78 NULL, /* IB */ 79 NULL, /* BR */ 80 NULL, /* RB */ 81 NULL, /* R */ 82 NULL, /* B */ 83 NULL, /* I */ 84 NULL, /* IR */ 85 NULL, /* RI */ 86 NULL, /* RE */ 87 check_part, /* RS */ 88 NULL, /* DT */ 89 post_UC, /* UC */ 90 NULL, /* PD */ 91 post_AT, /* AT */ 92 post_in, /* in */ 93 NULL, /* SY */ 94 NULL, /* YS */ 95 post_OP, /* OP */ 96 post_EX, /* EX */ 97 post_EE, /* EE */ 98 post_UR, /* UR */ 99 NULL, /* UE */ 100 post_UR, /* MT */ 101 NULL, /* ME */ 102 }; 103 104 105 /* Validate the subtree rooted at man->last. */ 106 void 107 man_validate(struct roff_man *man) 108 { 109 struct roff_node *n; 110 const v_check *cp; 111 112 /* 113 * Translate obsolete macros such that later code 114 * does not need to look for them. 115 */ 116 117 n = man->last; 118 switch (n->tok) { 119 case MAN_LP: 120 case MAN_P: 121 n->tok = MAN_PP; 122 break; 123 default: 124 break; 125 } 126 127 /* 128 * Iterate over all children, recursing into each one 129 * in turn, depth-first. 130 */ 131 132 man->last = man->last->child; 133 while (man->last != NULL) { 134 man_validate(man); 135 if (man->last == n) 136 man->last = man->last->child; 137 else 138 man->last = man->last->next; 139 } 140 141 /* Finally validate the macro itself. */ 142 143 man->last = n; 144 man->next = ROFF_NEXT_SIBLING; 145 switch (n->type) { 146 case ROFFT_TEXT: 147 check_text(man, n); 148 break; 149 case ROFFT_ROOT: 150 check_root(man, n); 151 break; 152 case ROFFT_COMMENT: 153 case ROFFT_EQN: 154 case ROFFT_TBL: 155 break; 156 default: 157 if (n->tok < ROFF_MAX) { 158 roff_validate(man); 159 break; 160 } 161 assert(n->tok >= MAN_TH && n->tok < MAN_MAX); 162 cp = man_valids + (n->tok - MAN_TH); 163 if (*cp) 164 (*cp)(man, n); 165 if (man->last == n) 166 n->flags |= NODE_VALID; 167 break; 168 } 169 } 170 171 static void 172 check_root(CHKARGS) 173 { 174 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0); 175 176 if (n->last == NULL || n->last->type == ROFFT_COMMENT) 177 mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL); 178 else 179 man->meta.hasbody = 1; 180 181 if (NULL == man->meta.title) { 182 mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL); 183 184 /* 185 * If a title hasn't been set, do so now (by 186 * implication, date and section also aren't set). 187 */ 188 189 man->meta.title = mandoc_strdup(""); 190 man->meta.msec = mandoc_strdup(""); 191 man->meta.date = mandoc_normdate(NULL, NULL); 192 } 193 194 if (man->meta.os_e && 195 (man->meta.rcsids & (1 << man->meta.os_e)) == 0) 196 mandoc_msg(MANDOCERR_RCS_MISSING, 0, 0, 197 man->meta.os_e == MANDOC_OS_OPENBSD ? 198 "(OpenBSD)" : "(NetBSD)"); 199 } 200 201 static void 202 check_abort(CHKARGS) 203 { 204 abort(); 205 } 206 207 /* 208 * Skip leading whitespace, dashes, backslashes, and font escapes, 209 * then create a tag if the first following byte is a letter. 210 * Priority is high unless whitespace is present. 211 */ 212 static void 213 check_tag(struct roff_node *n, struct roff_node *nt) 214 { 215 const char *cp, *arg; 216 int prio, sz; 217 218 if (nt == NULL || nt->type != ROFFT_TEXT) 219 return; 220 221 cp = nt->string; 222 prio = TAG_STRONG; 223 for (;;) { 224 switch (*cp) { 225 case ' ': 226 case '\t': 227 prio = TAG_WEAK; 228 /* FALLTHROUGH */ 229 case '-': 230 cp++; 231 break; 232 case '\\': 233 cp++; 234 switch (mandoc_escape(&cp, &arg, &sz)) { 235 case ESCAPE_FONT: 236 case ESCAPE_FONTBOLD: 237 case ESCAPE_FONTITALIC: 238 case ESCAPE_FONTBI: 239 case ESCAPE_FONTROMAN: 240 case ESCAPE_FONTCW: 241 case ESCAPE_FONTPREV: 242 case ESCAPE_IGNORE: 243 break; 244 case ESCAPE_SPECIAL: 245 if (sz != 1) 246 return; 247 switch (*arg) { 248 case '-': 249 case 'e': 250 break; 251 default: 252 return; 253 } 254 break; 255 default: 256 return; 257 } 258 break; 259 default: 260 if (isalpha((unsigned char)*cp)) 261 tag_put(cp, prio, n); 262 return; 263 } 264 } 265 } 266 267 static void 268 check_text(CHKARGS) 269 { 270 char *cp, *p; 271 272 if (n->flags & NODE_NOFILL) 273 return; 274 275 cp = n->string; 276 for (p = cp; NULL != (p = strchr(p, '\t')); p++) 277 mandoc_msg(MANDOCERR_FI_TAB, 278 n->line, n->pos + (int)(p - cp), NULL); 279 } 280 281 static void 282 post_EE(CHKARGS) 283 { 284 if ((n->flags & NODE_NOFILL) == 0) 285 mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "EE"); 286 } 287 288 static void 289 post_EX(CHKARGS) 290 { 291 if (n->flags & NODE_NOFILL) 292 mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "EX"); 293 } 294 295 static void 296 post_OP(CHKARGS) 297 { 298 299 if (n->child == NULL) 300 mandoc_msg(MANDOCERR_OP_EMPTY, n->line, n->pos, "OP"); 301 else if (n->child->next != NULL && n->child->next->next != NULL) { 302 n = n->child->next->next; 303 mandoc_msg(MANDOCERR_ARG_EXCESS, 304 n->line, n->pos, "OP ... %s", n->string); 305 } 306 } 307 308 static void 309 post_SH(CHKARGS) 310 { 311 struct roff_node *nc; 312 char *cp, *tag; 313 314 nc = n->child; 315 switch (n->type) { 316 case ROFFT_HEAD: 317 tag = NULL; 318 deroff(&tag, n); 319 if (tag != NULL) { 320 for (cp = tag; *cp != '\0'; cp++) 321 if (*cp == ' ') 322 *cp = '_'; 323 if (nc != NULL && nc->type == ROFFT_TEXT && 324 strcmp(nc->string, tag) == 0) 325 tag_put(NULL, TAG_WEAK, n); 326 else 327 tag_put(tag, TAG_FALLBACK, n); 328 free(tag); 329 } 330 return; 331 case ROFFT_BODY: 332 if (nc != NULL) 333 break; 334 return; 335 default: 336 return; 337 } 338 339 if (nc->tok == MAN_PP && nc->body->child != NULL) { 340 while (nc->body->last != NULL) { 341 man->next = ROFF_NEXT_CHILD; 342 roff_node_relink(man, nc->body->last); 343 man->last = n; 344 } 345 } 346 347 if (nc->tok == MAN_PP || nc->tok == ROFF_sp || nc->tok == ROFF_br) { 348 mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos, 349 "%s after %s", roff_name[nc->tok], roff_name[n->tok]); 350 roff_node_delete(man, nc); 351 } 352 353 /* 354 * Trailing PP is empty, so it is deleted by check_par(). 355 * Trailing sp is significant. 356 */ 357 358 if ((nc = n->last) != NULL && nc->tok == ROFF_br) { 359 mandoc_msg(MANDOCERR_PAR_SKIP, 360 nc->line, nc->pos, "%s at the end of %s", 361 roff_name[nc->tok], roff_name[n->tok]); 362 roff_node_delete(man, nc); 363 } 364 } 365 366 static void 367 post_UR(CHKARGS) 368 { 369 if (n->type == ROFFT_HEAD && n->child == NULL) 370 mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos, 371 "%s", roff_name[n->tok]); 372 check_part(man, n); 373 } 374 375 static void 376 check_part(CHKARGS) 377 { 378 379 if (n->type == ROFFT_BODY && n->child == NULL) 380 mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos, 381 "%s", roff_name[n->tok]); 382 } 383 384 static void 385 check_par(CHKARGS) 386 { 387 388 switch (n->type) { 389 case ROFFT_BLOCK: 390 if (n->body->child == NULL) 391 roff_node_delete(man, n); 392 break; 393 case ROFFT_BODY: 394 if (n->child != NULL && 395 (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) { 396 mandoc_msg(MANDOCERR_PAR_SKIP, 397 n->child->line, n->child->pos, 398 "%s after %s", roff_name[n->child->tok], 399 roff_name[n->tok]); 400 roff_node_delete(man, n->child); 401 } 402 if (n->child == NULL) 403 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos, 404 "%s empty", roff_name[n->tok]); 405 break; 406 case ROFFT_HEAD: 407 if (n->child != NULL) 408 mandoc_msg(MANDOCERR_ARG_SKIP, 409 n->line, n->pos, "%s %s%s", 410 roff_name[n->tok], n->child->string, 411 n->child->next != NULL ? " ..." : ""); 412 break; 413 default: 414 break; 415 } 416 } 417 418 static void 419 post_IP(CHKARGS) 420 { 421 switch (n->type) { 422 case ROFFT_BLOCK: 423 if (n->head->child == NULL && n->body->child == NULL) 424 roff_node_delete(man, n); 425 break; 426 case ROFFT_HEAD: 427 check_tag(n, n->child); 428 break; 429 case ROFFT_BODY: 430 if (n->parent->head->child == NULL && n->child == NULL) 431 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos, 432 "%s empty", roff_name[n->tok]); 433 break; 434 default: 435 break; 436 } 437 } 438 439 /* 440 * The first next-line element in the head is the tag. 441 * If that's a font macro, use its first child instead. 442 */ 443 static void 444 post_TP(CHKARGS) 445 { 446 struct roff_node *nt; 447 448 if (n->type != ROFFT_HEAD || (nt = n->child) == NULL) 449 return; 450 451 while ((nt->flags & NODE_LINE) == 0) 452 if ((nt = nt->next) == NULL) 453 return; 454 455 switch (nt->tok) { 456 case MAN_B: 457 case MAN_BI: 458 case MAN_BR: 459 case MAN_I: 460 case MAN_IB: 461 case MAN_IR: 462 nt = nt->child; 463 break; 464 default: 465 break; 466 } 467 check_tag(n, nt); 468 } 469 470 static void 471 post_TH(CHKARGS) 472 { 473 struct roff_node *nb; 474 const char *p; 475 476 free(man->meta.title); 477 free(man->meta.vol); 478 free(man->meta.os); 479 free(man->meta.msec); 480 free(man->meta.date); 481 482 man->meta.title = man->meta.vol = man->meta.date = 483 man->meta.msec = man->meta.os = NULL; 484 485 nb = n; 486 487 /* ->TITLE<- MSEC DATE OS VOL */ 488 489 n = n->child; 490 if (n != NULL && n->string != NULL) { 491 for (p = n->string; *p != '\0'; p++) { 492 /* Only warn about this once... */ 493 if (isalpha((unsigned char)*p) && 494 ! isupper((unsigned char)*p)) { 495 mandoc_msg(MANDOCERR_TITLE_CASE, n->line, 496 n->pos + (int)(p - n->string), 497 "TH %s", n->string); 498 break; 499 } 500 } 501 man->meta.title = mandoc_strdup(n->string); 502 } else { 503 man->meta.title = mandoc_strdup(""); 504 mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH"); 505 } 506 507 /* TITLE ->MSEC<- DATE OS VOL */ 508 509 if (n != NULL) 510 n = n->next; 511 if (n != NULL && n->string != NULL) { 512 man->meta.msec = mandoc_strdup(n->string); 513 if (man->filesec != '\0' && 514 man->filesec != *n->string && 515 *n->string >= '1' && *n->string <= '9') 516 mandoc_msg(MANDOCERR_MSEC_FILE, n->line, n->pos, 517 "*.%c vs TH ... %c", man->filesec, *n->string); 518 } else { 519 man->meta.msec = mandoc_strdup(""); 520 mandoc_msg(MANDOCERR_MSEC_MISSING, 521 nb->line, nb->pos, "TH %s", man->meta.title); 522 } 523 524 /* TITLE MSEC ->DATE<- OS VOL */ 525 526 if (n != NULL) 527 n = n->next; 528 if (man->quick && n != NULL) 529 man->meta.date = mandoc_strdup(""); 530 else 531 man->meta.date = mandoc_normdate(n, nb); 532 533 /* TITLE MSEC DATE ->OS<- VOL */ 534 535 if (n && (n = n->next)) 536 man->meta.os = mandoc_strdup(n->string); 537 else if (man->os_s != NULL) 538 man->meta.os = mandoc_strdup(man->os_s); 539 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) { 540 if (strstr(man->meta.os, "OpenBSD") != NULL) 541 man->meta.os_e = MANDOC_OS_OPENBSD; 542 else if (strstr(man->meta.os, "NetBSD") != NULL) 543 man->meta.os_e = MANDOC_OS_NETBSD; 544 } 545 546 /* TITLE MSEC DATE OS ->VOL<- */ 547 /* If missing, use the default VOL name for MSEC. */ 548 549 if (n && (n = n->next)) 550 man->meta.vol = mandoc_strdup(n->string); 551 else if ('\0' != man->meta.msec[0] && 552 (NULL != (p = mandoc_a2msec(man->meta.msec)))) 553 man->meta.vol = mandoc_strdup(p); 554 555 if (n != NULL && (n = n->next) != NULL) 556 mandoc_msg(MANDOCERR_ARG_EXCESS, 557 n->line, n->pos, "TH ... %s", n->string); 558 559 /* 560 * Remove the `TH' node after we've processed it for our 561 * meta-data. 562 */ 563 roff_node_delete(man, man->last); 564 } 565 566 static void 567 post_UC(CHKARGS) 568 { 569 static const char * const bsd_versions[] = { 570 "3rd Berkeley Distribution", 571 "4th Berkeley Distribution", 572 "4.2 Berkeley Distribution", 573 "4.3 Berkeley Distribution", 574 "4.4 Berkeley Distribution", 575 }; 576 577 const char *p, *s; 578 579 n = n->child; 580 581 if (n == NULL || n->type != ROFFT_TEXT) 582 p = bsd_versions[0]; 583 else { 584 s = n->string; 585 if (0 == strcmp(s, "3")) 586 p = bsd_versions[0]; 587 else if (0 == strcmp(s, "4")) 588 p = bsd_versions[1]; 589 else if (0 == strcmp(s, "5")) 590 p = bsd_versions[2]; 591 else if (0 == strcmp(s, "6")) 592 p = bsd_versions[3]; 593 else if (0 == strcmp(s, "7")) 594 p = bsd_versions[4]; 595 else 596 p = bsd_versions[0]; 597 } 598 599 free(man->meta.os); 600 man->meta.os = mandoc_strdup(p); 601 } 602 603 static void 604 post_AT(CHKARGS) 605 { 606 static const char * const unix_versions[] = { 607 "7th Edition", 608 "System III", 609 "System V", 610 "System V Release 2", 611 }; 612 613 struct roff_node *nn; 614 const char *p, *s; 615 616 n = n->child; 617 618 if (n == NULL || n->type != ROFFT_TEXT) 619 p = unix_versions[0]; 620 else { 621 s = n->string; 622 if (0 == strcmp(s, "3")) 623 p = unix_versions[0]; 624 else if (0 == strcmp(s, "4")) 625 p = unix_versions[1]; 626 else if (0 == strcmp(s, "5")) { 627 nn = n->next; 628 if (nn != NULL && 629 nn->type == ROFFT_TEXT && 630 nn->string[0] != '\0') 631 p = unix_versions[3]; 632 else 633 p = unix_versions[2]; 634 } else 635 p = unix_versions[0]; 636 } 637 638 free(man->meta.os); 639 man->meta.os = mandoc_strdup(p); 640 } 641 642 static void 643 post_in(CHKARGS) 644 { 645 char *s; 646 647 if (n->parent->tok != MAN_TP || 648 n->parent->type != ROFFT_HEAD || 649 n->child == NULL || 650 *n->child->string == '+' || 651 *n->child->string == '-') 652 return; 653 mandoc_asprintf(&s, "+%s", n->child->string); 654 free(n->child->string); 655 n->child->string = s; 656 } 657