1 /* $Id: man_validate.c,v 1.159 2023/10/24 20:53:12 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 "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_HEAD: 303 tag = NULL; 304 deroff(&tag, n); 305 if (tag != NULL) { 306 for (cp = tag; *cp != '\0'; cp++) 307 if (*cp == ' ') 308 *cp = '_'; 309 if (nc != NULL && nc->type == ROFFT_TEXT && 310 strcmp(nc->string, tag) == 0) 311 tag_put(NULL, TAG_STRONG, n); 312 else 313 tag_put(tag, TAG_FALLBACK, n); 314 free(tag); 315 } 316 return; 317 case ROFFT_BODY: 318 if (nc != NULL) 319 break; 320 return; 321 default: 322 return; 323 } 324 325 if ((nc->tok == MAN_LP || nc->tok == MAN_PP || nc->tok == MAN_P) && 326 nc->body->child != NULL) { 327 while (nc->body->last != NULL) { 328 man->next = ROFF_NEXT_CHILD; 329 roff_node_relink(man, nc->body->last); 330 man->last = n; 331 } 332 } 333 334 if (nc->tok == MAN_LP || nc->tok == MAN_PP || nc->tok == MAN_P || 335 nc->tok == ROFF_sp || nc->tok == ROFF_br) { 336 mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos, 337 "%s after %s", roff_name[nc->tok], roff_name[n->tok]); 338 roff_node_delete(man, nc); 339 } 340 341 /* 342 * Trailing PP is empty, so it is deleted by check_par(). 343 * Trailing sp is significant. 344 */ 345 346 if ((nc = n->last) != NULL && nc->tok == ROFF_br) { 347 mandoc_msg(MANDOCERR_PAR_SKIP, 348 nc->line, nc->pos, "%s at the end of %s", 349 roff_name[nc->tok], roff_name[n->tok]); 350 roff_node_delete(man, nc); 351 } 352 } 353 354 static void 355 post_UR(CHKARGS) 356 { 357 if (n->type == ROFFT_HEAD && n->child == NULL) 358 mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos, 359 "%s", roff_name[n->tok]); 360 } 361 362 static void 363 check_part(CHKARGS) 364 { 365 if (n->type == ROFFT_BODY && n->child == NULL) 366 mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos, 367 "%s", roff_name[n->tok]); 368 } 369 370 static void 371 check_par(CHKARGS) 372 { 373 374 switch (n->type) { 375 case ROFFT_BLOCK: 376 if (n->body->child == NULL) 377 roff_node_delete(man, n); 378 break; 379 case ROFFT_BODY: 380 if (n->child != NULL && 381 (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) { 382 mandoc_msg(MANDOCERR_PAR_SKIP, 383 n->child->line, n->child->pos, 384 "%s after %s", roff_name[n->child->tok], 385 roff_name[n->tok]); 386 roff_node_delete(man, n->child); 387 } 388 if (n->child == NULL) 389 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos, 390 "%s empty", roff_name[n->tok]); 391 break; 392 case ROFFT_HEAD: 393 if (n->child != NULL) 394 mandoc_msg(MANDOCERR_ARG_SKIP, 395 n->line, n->pos, "%s %s%s", 396 roff_name[n->tok], n->child->string, 397 n->child->next != NULL ? " ..." : ""); 398 break; 399 default: 400 break; 401 } 402 } 403 404 static void 405 post_IP(CHKARGS) 406 { 407 switch (n->type) { 408 case ROFFT_BLOCK: 409 if (n->head->child == NULL && n->body->child == NULL) 410 roff_node_delete(man, n); 411 break; 412 case ROFFT_HEAD: 413 check_tag(n, n->child); 414 break; 415 case ROFFT_BODY: 416 if (n->parent->head->child == NULL && n->child == NULL) 417 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos, 418 "%s empty", roff_name[n->tok]); 419 break; 420 default: 421 break; 422 } 423 } 424 425 /* 426 * The first next-line element in the head is the tag. 427 * If that's a font macro, use its first child instead. 428 */ 429 static void 430 post_TP(CHKARGS) 431 { 432 struct roff_node *nt; 433 434 if (n->type != ROFFT_HEAD || (nt = n->child) == NULL) 435 return; 436 437 while ((nt->flags & NODE_LINE) == 0) 438 if ((nt = nt->next) == NULL) 439 return; 440 441 switch (nt->tok) { 442 case MAN_B: 443 case MAN_BI: 444 case MAN_BR: 445 case MAN_I: 446 case MAN_IB: 447 case MAN_IR: 448 nt = nt->child; 449 break; 450 default: 451 break; 452 } 453 check_tag(n, nt); 454 } 455 456 static void 457 post_TH(CHKARGS) 458 { 459 struct roff_node *nb; 460 const char *p; 461 462 free(man->meta.title); 463 free(man->meta.vol); 464 free(man->meta.os); 465 free(man->meta.msec); 466 free(man->meta.date); 467 468 man->meta.title = man->meta.vol = man->meta.date = 469 man->meta.msec = man->meta.os = NULL; 470 471 nb = n; 472 473 /* ->TITLE<- MSEC DATE OS VOL */ 474 475 n = n->child; 476 if (n != NULL && n->string != NULL) { 477 for (p = n->string; *p != '\0'; p++) { 478 /* Only warn about this once... */ 479 if (isalpha((unsigned char)*p) && 480 ! isupper((unsigned char)*p)) { 481 mandoc_msg(MANDOCERR_TITLE_CASE, n->line, 482 n->pos + (int)(p - n->string), 483 "TH %s", n->string); 484 break; 485 } 486 } 487 man->meta.title = mandoc_strdup(n->string); 488 } else { 489 man->meta.title = mandoc_strdup(""); 490 mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH"); 491 } 492 493 /* TITLE ->MSEC<- DATE OS VOL */ 494 495 if (n != NULL) 496 n = n->next; 497 if (n != NULL && n->string != NULL) { 498 man->meta.msec = mandoc_strdup(n->string); 499 if (man->filesec != '\0' && 500 man->filesec != *n->string && 501 *n->string >= '1' && *n->string <= '9') 502 mandoc_msg(MANDOCERR_MSEC_FILE, n->line, n->pos, 503 "*.%c vs TH ... %c", man->filesec, *n->string); 504 } else { 505 man->meta.msec = mandoc_strdup(""); 506 mandoc_msg(MANDOCERR_MSEC_MISSING, 507 nb->line, nb->pos, "TH %s", man->meta.title); 508 } 509 510 /* TITLE MSEC ->DATE<- OS VOL */ 511 512 if (n != NULL) 513 n = n->next; 514 if (man->quick && n != NULL) 515 man->meta.date = mandoc_strdup(""); 516 else 517 man->meta.date = mandoc_normdate(n, nb); 518 519 /* TITLE MSEC DATE ->OS<- VOL */ 520 521 if (n && (n = n->next)) 522 man->meta.os = mandoc_strdup(n->string); 523 else if (man->os_s != NULL) 524 man->meta.os = mandoc_strdup(man->os_s); 525 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) { 526 if (strstr(man->meta.os, "OpenBSD") != NULL) 527 man->meta.os_e = MANDOC_OS_OPENBSD; 528 else if (strstr(man->meta.os, "NetBSD") != NULL) 529 man->meta.os_e = MANDOC_OS_NETBSD; 530 } 531 532 /* TITLE MSEC DATE OS ->VOL<- */ 533 /* If missing, use the default VOL name for MSEC. */ 534 535 if (n && (n = n->next)) 536 man->meta.vol = mandoc_strdup(n->string); 537 else if ('\0' != man->meta.msec[0] && 538 (NULL != (p = mandoc_a2msec(man->meta.msec)))) 539 man->meta.vol = mandoc_strdup(p); 540 541 if (n != NULL && (n = n->next) != NULL) 542 mandoc_msg(MANDOCERR_ARG_EXCESS, 543 n->line, n->pos, "TH ... %s", n->string); 544 545 /* 546 * Remove the `TH' node after we've processed it for our 547 * meta-data. 548 */ 549 roff_node_delete(man, man->last); 550 } 551 552 static void 553 post_MR(CHKARGS) 554 { 555 struct roff_node *nch; 556 557 if ((nch = n->child) == NULL) { 558 mandoc_msg(MANDOCERR_NM_NONAME, n->line, n->pos, "MR"); 559 return; 560 } 561 if (nch->next == NULL) { 562 mandoc_msg(MANDOCERR_XR_NOSEC, 563 n->line, n->pos, "MR %s", nch->string); 564 return; 565 } 566 if (mandoc_xr_add(nch->next->string, nch->string, nch->line, nch->pos)) 567 mandoc_msg(MANDOCERR_XR_SELF, nch->line, nch->pos, 568 "MR %s %s", nch->string, nch->next->string); 569 if ((nch = nch->next->next) == NULL || nch->next == NULL) 570 return; 571 572 mandoc_msg(MANDOCERR_ARG_EXCESS, nch->next->line, nch->next->pos, 573 "MR ... %s", nch->next->string); 574 while (nch->next != NULL) 575 roff_node_delete(man, nch->next); 576 } 577 578 static void 579 post_UC(CHKARGS) 580 { 581 static const char * const bsd_versions[] = { 582 "3rd Berkeley Distribution", 583 "4th Berkeley Distribution", 584 "4.2 Berkeley Distribution", 585 "4.3 Berkeley Distribution", 586 "4.4 Berkeley Distribution", 587 }; 588 589 const char *p, *s; 590 591 n = n->child; 592 593 if (n == NULL || n->type != ROFFT_TEXT) 594 p = bsd_versions[0]; 595 else { 596 s = n->string; 597 if (0 == strcmp(s, "3")) 598 p = bsd_versions[0]; 599 else if (0 == strcmp(s, "4")) 600 p = bsd_versions[1]; 601 else if (0 == strcmp(s, "5")) 602 p = bsd_versions[2]; 603 else if (0 == strcmp(s, "6")) 604 p = bsd_versions[3]; 605 else if (0 == strcmp(s, "7")) 606 p = bsd_versions[4]; 607 else 608 p = bsd_versions[0]; 609 } 610 611 free(man->meta.os); 612 man->meta.os = mandoc_strdup(p); 613 } 614 615 static void 616 post_AT(CHKARGS) 617 { 618 static const char * const unix_versions[] = { 619 "7th Edition", 620 "System III", 621 "System V", 622 "System V Release 2", 623 }; 624 625 struct roff_node *nn; 626 const char *p, *s; 627 628 n = n->child; 629 630 if (n == NULL || n->type != ROFFT_TEXT) 631 p = unix_versions[0]; 632 else { 633 s = n->string; 634 if (0 == strcmp(s, "3")) 635 p = unix_versions[0]; 636 else if (0 == strcmp(s, "4")) 637 p = unix_versions[1]; 638 else if (0 == strcmp(s, "5")) { 639 nn = n->next; 640 if (nn != NULL && 641 nn->type == ROFFT_TEXT && 642 nn->string[0] != '\0') 643 p = unix_versions[3]; 644 else 645 p = unix_versions[2]; 646 } else 647 p = unix_versions[0]; 648 } 649 650 free(man->meta.os); 651 man->meta.os = mandoc_strdup(p); 652 } 653 654 static void 655 post_in(CHKARGS) 656 { 657 char *s; 658 659 if (n->parent->tok != MAN_TP || 660 n->parent->type != ROFFT_HEAD || 661 n->child == NULL || 662 *n->child->string == '+' || 663 *n->child->string == '-') 664 return; 665 mandoc_asprintf(&s, "+%s", n->child->string); 666 free(n->child->string); 667 n->child->string = s; 668 } 669