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