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