1 /* $OpenBSD: man_validate.c,v 1.118 2019/03/13 18:29:26 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2010, 2012-2018 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 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 #include <sys/types.h> 19 20 #include <assert.h> 21 #include <ctype.h> 22 #include <errno.h> 23 #include <limits.h> 24 #include <stdarg.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <time.h> 29 30 #include "mandoc_aux.h" 31 #include "mandoc.h" 32 #include "roff.h" 33 #include "man.h" 34 #include "libmandoc.h" 35 #include "roff_int.h" 36 #include "libman.h" 37 38 #define CHKARGS struct roff_man *man, struct roff_node *n 39 40 typedef void (*v_check)(CHKARGS); 41 42 static void check_abort(CHKARGS) __attribute__((__noreturn__)); 43 static void check_par(CHKARGS); 44 static void check_part(CHKARGS); 45 static void check_root(CHKARGS); 46 static void check_text(CHKARGS); 47 48 static void post_AT(CHKARGS); 49 static void post_EE(CHKARGS); 50 static void post_EX(CHKARGS); 51 static void post_IP(CHKARGS); 52 static void post_OP(CHKARGS); 53 static void post_SH(CHKARGS); 54 static void post_TH(CHKARGS); 55 static void post_UC(CHKARGS); 56 static void post_UR(CHKARGS); 57 static void post_in(CHKARGS); 58 59 static const v_check man_valids[MAN_MAX - MAN_TH] = { 60 post_TH, /* TH */ 61 post_SH, /* SH */ 62 post_SH, /* SS */ 63 NULL, /* TP */ 64 NULL, /* TQ */ 65 check_abort,/* LP */ 66 check_par, /* PP */ 67 check_abort,/* P */ 68 post_IP, /* IP */ 69 NULL, /* HP */ 70 NULL, /* SM */ 71 NULL, /* SB */ 72 NULL, /* BI */ 73 NULL, /* IB */ 74 NULL, /* BR */ 75 NULL, /* RB */ 76 NULL, /* R */ 77 NULL, /* B */ 78 NULL, /* I */ 79 NULL, /* IR */ 80 NULL, /* RI */ 81 NULL, /* RE */ 82 check_part, /* RS */ 83 NULL, /* DT */ 84 post_UC, /* UC */ 85 NULL, /* PD */ 86 post_AT, /* AT */ 87 post_in, /* in */ 88 NULL, /* SY */ 89 NULL, /* YS */ 90 post_OP, /* OP */ 91 post_EX, /* EX */ 92 post_EE, /* EE */ 93 post_UR, /* UR */ 94 NULL, /* UE */ 95 post_UR, /* MT */ 96 NULL, /* ME */ 97 }; 98 99 100 /* Validate the subtree rooted at man->last. */ 101 void 102 man_validate(struct roff_man *man) 103 { 104 struct roff_node *n; 105 const v_check *cp; 106 107 /* 108 * Translate obsolete macros such that later code 109 * does not need to look for them. 110 */ 111 112 n = man->last; 113 switch (n->tok) { 114 case MAN_LP: 115 case MAN_P: 116 n->tok = MAN_PP; 117 break; 118 default: 119 break; 120 } 121 122 /* 123 * Iterate over all children, recursing into each one 124 * in turn, depth-first. 125 */ 126 127 man->last = man->last->child; 128 while (man->last != NULL) { 129 man_validate(man); 130 if (man->last == n) 131 man->last = man->last->child; 132 else 133 man->last = man->last->next; 134 } 135 136 /* Finally validate the macro itself. */ 137 138 man->last = n; 139 man->next = ROFF_NEXT_SIBLING; 140 switch (n->type) { 141 case ROFFT_TEXT: 142 check_text(man, n); 143 break; 144 case ROFFT_ROOT: 145 check_root(man, n); 146 break; 147 case ROFFT_COMMENT: 148 case ROFFT_EQN: 149 case ROFFT_TBL: 150 break; 151 default: 152 if (n->tok < ROFF_MAX) { 153 roff_validate(man); 154 break; 155 } 156 assert(n->tok >= MAN_TH && n->tok < MAN_MAX); 157 cp = man_valids + (n->tok - MAN_TH); 158 if (*cp) 159 (*cp)(man, n); 160 if (man->last == n) 161 n->flags |= NODE_VALID; 162 break; 163 } 164 } 165 166 static void 167 check_root(CHKARGS) 168 { 169 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0); 170 171 if (n->last == NULL || n->last->type == ROFFT_COMMENT) 172 mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL); 173 else 174 man->meta.hasbody = 1; 175 176 if (NULL == man->meta.title) { 177 mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL); 178 179 /* 180 * If a title hasn't been set, do so now (by 181 * implication, date and section also aren't set). 182 */ 183 184 man->meta.title = mandoc_strdup(""); 185 man->meta.msec = mandoc_strdup(""); 186 man->meta.date = man->quick ? mandoc_strdup("") : 187 mandoc_normdate(man, NULL, n->line, n->pos); 188 } 189 190 if (man->meta.os_e && 191 (man->meta.rcsids & (1 << man->meta.os_e)) == 0) 192 mandoc_msg(MANDOCERR_RCS_MISSING, 0, 0, 193 man->meta.os_e == MANDOC_OS_OPENBSD ? 194 "(OpenBSD)" : "(NetBSD)"); 195 } 196 197 static void 198 check_abort(CHKARGS) 199 { 200 abort(); 201 } 202 203 static void 204 check_text(CHKARGS) 205 { 206 char *cp, *p; 207 208 if (n->flags & NODE_NOFILL) 209 return; 210 211 cp = n->string; 212 for (p = cp; NULL != (p = strchr(p, '\t')); p++) 213 mandoc_msg(MANDOCERR_FI_TAB, 214 n->line, n->pos + (int)(p - cp), NULL); 215 } 216 217 static void 218 post_EE(CHKARGS) 219 { 220 if ((n->flags & NODE_NOFILL) == 0) 221 mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "EE"); 222 } 223 224 static void 225 post_EX(CHKARGS) 226 { 227 if (n->flags & NODE_NOFILL) 228 mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "EX"); 229 } 230 231 static void 232 post_OP(CHKARGS) 233 { 234 235 if (n->child == NULL) 236 mandoc_msg(MANDOCERR_OP_EMPTY, n->line, n->pos, "OP"); 237 else if (n->child->next != NULL && n->child->next->next != NULL) { 238 n = n->child->next->next; 239 mandoc_msg(MANDOCERR_ARG_EXCESS, 240 n->line, n->pos, "OP ... %s", n->string); 241 } 242 } 243 244 static void 245 post_SH(CHKARGS) 246 { 247 struct roff_node *nc; 248 249 if (n->type != ROFFT_BODY || (nc = n->child) == NULL) 250 return; 251 252 if (nc->tok == MAN_PP && nc->body->child != NULL) { 253 while (nc->body->last != NULL) { 254 man->next = ROFF_NEXT_CHILD; 255 roff_node_relink(man, nc->body->last); 256 man->last = n; 257 } 258 } 259 260 if (nc->tok == MAN_PP || nc->tok == ROFF_sp || nc->tok == ROFF_br) { 261 mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos, 262 "%s after %s", roff_name[nc->tok], roff_name[n->tok]); 263 roff_node_delete(man, nc); 264 } 265 266 /* 267 * Trailing PP is empty, so it is deleted by check_par(). 268 * Trailing sp is significant. 269 */ 270 271 if ((nc = n->last) != NULL && nc->tok == ROFF_br) { 272 mandoc_msg(MANDOCERR_PAR_SKIP, 273 nc->line, nc->pos, "%s at the end of %s", 274 roff_name[nc->tok], roff_name[n->tok]); 275 roff_node_delete(man, nc); 276 } 277 } 278 279 static void 280 post_UR(CHKARGS) 281 { 282 if (n->type == ROFFT_HEAD && n->child == NULL) 283 mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos, 284 "%s", roff_name[n->tok]); 285 check_part(man, n); 286 } 287 288 static void 289 check_part(CHKARGS) 290 { 291 292 if (n->type == ROFFT_BODY && n->child == NULL) 293 mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos, 294 "%s", roff_name[n->tok]); 295 } 296 297 static void 298 check_par(CHKARGS) 299 { 300 301 switch (n->type) { 302 case ROFFT_BLOCK: 303 if (n->body->child == NULL) 304 roff_node_delete(man, n); 305 break; 306 case ROFFT_BODY: 307 if (n->child != NULL && 308 (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) { 309 mandoc_msg(MANDOCERR_PAR_SKIP, 310 n->child->line, n->child->pos, 311 "%s after %s", roff_name[n->child->tok], 312 roff_name[n->tok]); 313 roff_node_delete(man, n->child); 314 } 315 if (n->child == NULL) 316 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos, 317 "%s empty", roff_name[n->tok]); 318 break; 319 case ROFFT_HEAD: 320 if (n->child != NULL) 321 mandoc_msg(MANDOCERR_ARG_SKIP, 322 n->line, n->pos, "%s %s%s", 323 roff_name[n->tok], n->child->string, 324 n->child->next != NULL ? " ..." : ""); 325 break; 326 default: 327 break; 328 } 329 } 330 331 static void 332 post_IP(CHKARGS) 333 { 334 335 switch (n->type) { 336 case ROFFT_BLOCK: 337 if (n->head->child == NULL && n->body->child == NULL) 338 roff_node_delete(man, n); 339 break; 340 case ROFFT_BODY: 341 if (n->parent->head->child == NULL && n->child == NULL) 342 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos, 343 "%s empty", roff_name[n->tok]); 344 break; 345 default: 346 break; 347 } 348 } 349 350 static void 351 post_TH(CHKARGS) 352 { 353 struct roff_node *nb; 354 const char *p; 355 356 free(man->meta.title); 357 free(man->meta.vol); 358 free(man->meta.os); 359 free(man->meta.msec); 360 free(man->meta.date); 361 362 man->meta.title = man->meta.vol = man->meta.date = 363 man->meta.msec = man->meta.os = NULL; 364 365 nb = n; 366 367 /* ->TITLE<- MSEC DATE OS VOL */ 368 369 n = n->child; 370 if (n && n->string) { 371 for (p = n->string; '\0' != *p; p++) { 372 /* Only warn about this once... */ 373 if (isalpha((unsigned char)*p) && 374 ! isupper((unsigned char)*p)) { 375 mandoc_msg(MANDOCERR_TITLE_CASE, n->line, 376 n->pos + (int)(p - n->string), 377 "TH %s", n->string); 378 break; 379 } 380 } 381 man->meta.title = mandoc_strdup(n->string); 382 } else { 383 man->meta.title = mandoc_strdup(""); 384 mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH"); 385 } 386 387 /* TITLE ->MSEC<- DATE OS VOL */ 388 389 if (n) 390 n = n->next; 391 if (n && n->string) 392 man->meta.msec = mandoc_strdup(n->string); 393 else { 394 man->meta.msec = mandoc_strdup(""); 395 mandoc_msg(MANDOCERR_MSEC_MISSING, 396 nb->line, nb->pos, "TH %s", man->meta.title); 397 } 398 399 /* TITLE MSEC ->DATE<- OS VOL */ 400 401 if (n) 402 n = n->next; 403 if (n && n->string && '\0' != n->string[0]) { 404 man->meta.date = man->quick ? 405 mandoc_strdup(n->string) : 406 mandoc_normdate(man, n->string, n->line, n->pos); 407 } else { 408 man->meta.date = mandoc_strdup(""); 409 mandoc_msg(MANDOCERR_DATE_MISSING, 410 n ? n->line : nb->line, 411 n ? n->pos : nb->pos, "TH"); 412 } 413 414 /* TITLE MSEC DATE ->OS<- VOL */ 415 416 if (n && (n = n->next)) 417 man->meta.os = mandoc_strdup(n->string); 418 else if (man->os_s != NULL) 419 man->meta.os = mandoc_strdup(man->os_s); 420 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) { 421 if (strstr(man->meta.os, "OpenBSD") != NULL) 422 man->meta.os_e = MANDOC_OS_OPENBSD; 423 else if (strstr(man->meta.os, "NetBSD") != NULL) 424 man->meta.os_e = MANDOC_OS_NETBSD; 425 } 426 427 /* TITLE MSEC DATE OS ->VOL<- */ 428 /* If missing, use the default VOL name for MSEC. */ 429 430 if (n && (n = n->next)) 431 man->meta.vol = mandoc_strdup(n->string); 432 else if ('\0' != man->meta.msec[0] && 433 (NULL != (p = mandoc_a2msec(man->meta.msec)))) 434 man->meta.vol = mandoc_strdup(p); 435 436 if (n != NULL && (n = n->next) != NULL) 437 mandoc_msg(MANDOCERR_ARG_EXCESS, 438 n->line, n->pos, "TH ... %s", n->string); 439 440 /* 441 * Remove the `TH' node after we've processed it for our 442 * meta-data. 443 */ 444 roff_node_delete(man, man->last); 445 } 446 447 static void 448 post_UC(CHKARGS) 449 { 450 static const char * const bsd_versions[] = { 451 "3rd Berkeley Distribution", 452 "4th Berkeley Distribution", 453 "4.2 Berkeley Distribution", 454 "4.3 Berkeley Distribution", 455 "4.4 Berkeley Distribution", 456 }; 457 458 const char *p, *s; 459 460 n = n->child; 461 462 if (n == NULL || n->type != ROFFT_TEXT) 463 p = bsd_versions[0]; 464 else { 465 s = n->string; 466 if (0 == strcmp(s, "3")) 467 p = bsd_versions[0]; 468 else if (0 == strcmp(s, "4")) 469 p = bsd_versions[1]; 470 else if (0 == strcmp(s, "5")) 471 p = bsd_versions[2]; 472 else if (0 == strcmp(s, "6")) 473 p = bsd_versions[3]; 474 else if (0 == strcmp(s, "7")) 475 p = bsd_versions[4]; 476 else 477 p = bsd_versions[0]; 478 } 479 480 free(man->meta.os); 481 man->meta.os = mandoc_strdup(p); 482 } 483 484 static void 485 post_AT(CHKARGS) 486 { 487 static const char * const unix_versions[] = { 488 "7th Edition", 489 "System III", 490 "System V", 491 "System V Release 2", 492 }; 493 494 struct roff_node *nn; 495 const char *p, *s; 496 497 n = n->child; 498 499 if (n == NULL || n->type != ROFFT_TEXT) 500 p = unix_versions[0]; 501 else { 502 s = n->string; 503 if (0 == strcmp(s, "3")) 504 p = unix_versions[0]; 505 else if (0 == strcmp(s, "4")) 506 p = unix_versions[1]; 507 else if (0 == strcmp(s, "5")) { 508 nn = n->next; 509 if (nn != NULL && 510 nn->type == ROFFT_TEXT && 511 nn->string[0] != '\0') 512 p = unix_versions[3]; 513 else 514 p = unix_versions[2]; 515 } else 516 p = unix_versions[0]; 517 } 518 519 free(man->meta.os); 520 man->meta.os = mandoc_strdup(p); 521 } 522 523 static void 524 post_in(CHKARGS) 525 { 526 char *s; 527 528 if (n->parent->tok != MAN_TP || 529 n->parent->type != ROFFT_HEAD || 530 n->child == NULL || 531 *n->child->string == '+' || 532 *n->child->string == '-') 533 return; 534 mandoc_asprintf(&s, "+%s", n->child->string); 535 free(n->child->string); 536 n->child->string = s; 537 } 538