1 /* $OpenBSD: man_validate.c,v 1.98 2017/05/05 15:16:25 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2010, 2012-2017 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 <stdlib.h> 26 #include <string.h> 27 #include <time.h> 28 29 #include "mandoc_aux.h" 30 #include "mandoc.h" 31 #include "roff.h" 32 #include "man.h" 33 #include "libmandoc.h" 34 #include "roff_int.h" 35 #include "libman.h" 36 37 #define CHKARGS struct roff_man *man, struct roff_node *n 38 39 typedef void (*v_check)(CHKARGS); 40 41 static void check_par(CHKARGS); 42 static void check_part(CHKARGS); 43 static void check_root(CHKARGS); 44 static void check_text(CHKARGS); 45 46 static void post_AT(CHKARGS); 47 static void post_IP(CHKARGS); 48 static void post_vs(CHKARGS); 49 static void post_OP(CHKARGS); 50 static void post_TH(CHKARGS); 51 static void post_UC(CHKARGS); 52 static void post_UR(CHKARGS); 53 54 static const v_check __man_valids[MAN_MAX - MAN_TH] = { 55 post_TH, /* TH */ 56 NULL, /* SH */ 57 NULL, /* SS */ 58 NULL, /* TP */ 59 check_par, /* LP */ 60 check_par, /* PP */ 61 check_par, /* P */ 62 post_IP, /* IP */ 63 NULL, /* HP */ 64 NULL, /* SM */ 65 NULL, /* SB */ 66 NULL, /* BI */ 67 NULL, /* IB */ 68 NULL, /* BR */ 69 NULL, /* RB */ 70 NULL, /* R */ 71 NULL, /* B */ 72 NULL, /* I */ 73 NULL, /* IR */ 74 NULL, /* RI */ 75 NULL, /* nf */ 76 NULL, /* fi */ 77 NULL, /* RE */ 78 check_part, /* RS */ 79 NULL, /* DT */ 80 post_UC, /* UC */ 81 NULL, /* PD */ 82 post_AT, /* AT */ 83 NULL, /* in */ 84 post_OP, /* OP */ 85 NULL, /* EX */ 86 NULL, /* EE */ 87 post_UR, /* UR */ 88 NULL, /* UE */ 89 }; 90 static const v_check *man_valids = __man_valids - MAN_TH; 91 92 93 void 94 man_node_validate(struct roff_man *man) 95 { 96 struct roff_node *n; 97 const v_check *cp; 98 99 n = man->last; 100 man->last = man->last->child; 101 while (man->last != NULL) { 102 man_node_validate(man); 103 if (man->last == n) 104 man->last = man->last->child; 105 else 106 man->last = man->last->next; 107 } 108 109 man->last = n; 110 man->next = ROFF_NEXT_SIBLING; 111 switch (n->type) { 112 case ROFFT_TEXT: 113 check_text(man, n); 114 break; 115 case ROFFT_ROOT: 116 check_root(man, n); 117 break; 118 case ROFFT_EQN: 119 case ROFFT_TBL: 120 break; 121 default: 122 if (n->tok < ROFF_MAX) { 123 switch (n->tok) { 124 case ROFF_br: 125 case ROFF_sp: 126 post_vs(man, n); 127 break; 128 default: 129 roff_validate(man); 130 break; 131 } 132 break; 133 } 134 assert(n->tok >= MAN_TH && n->tok < MAN_MAX); 135 cp = man_valids + n->tok; 136 if (*cp) 137 (*cp)(man, n); 138 if (man->last == n) 139 man_state(man, n); 140 break; 141 } 142 } 143 144 static void 145 check_root(CHKARGS) 146 { 147 148 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0); 149 150 if (NULL == man->first->child) 151 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse, 152 n->line, n->pos, NULL); 153 else 154 man->meta.hasbody = 1; 155 156 if (NULL == man->meta.title) { 157 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse, 158 n->line, n->pos, NULL); 159 160 /* 161 * If a title hasn't been set, do so now (by 162 * implication, date and section also aren't set). 163 */ 164 165 man->meta.title = mandoc_strdup(""); 166 man->meta.msec = mandoc_strdup(""); 167 man->meta.date = man->quick ? mandoc_strdup("") : 168 mandoc_normdate(man->parse, NULL, n->line, n->pos); 169 } 170 } 171 172 static void 173 check_text(CHKARGS) 174 { 175 char *cp, *p; 176 177 if (MAN_LITERAL & man->flags) 178 return; 179 180 cp = n->string; 181 for (p = cp; NULL != (p = strchr(p, '\t')); p++) 182 mandoc_msg(MANDOCERR_FI_TAB, man->parse, 183 n->line, n->pos + (p - cp), NULL); 184 } 185 186 static void 187 post_OP(CHKARGS) 188 { 189 190 if (n->child == NULL) 191 mandoc_msg(MANDOCERR_OP_EMPTY, man->parse, 192 n->line, n->pos, "OP"); 193 else if (n->child->next != NULL && n->child->next->next != NULL) { 194 n = n->child->next->next; 195 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, 196 n->line, n->pos, "OP ... %s", n->string); 197 } 198 } 199 200 static void 201 post_UR(CHKARGS) 202 { 203 204 if (n->type == ROFFT_HEAD && n->child == NULL) 205 mandoc_vmsg(MANDOCERR_UR_NOHEAD, man->parse, 206 n->line, n->pos, "UR"); 207 check_part(man, n); 208 } 209 210 static void 211 check_part(CHKARGS) 212 { 213 214 if (n->type == ROFFT_BODY && n->child == NULL) 215 mandoc_msg(MANDOCERR_BLK_EMPTY, man->parse, 216 n->line, n->pos, roff_name[n->tok]); 217 } 218 219 static void 220 check_par(CHKARGS) 221 { 222 223 switch (n->type) { 224 case ROFFT_BLOCK: 225 if (n->body->child == NULL) 226 roff_node_delete(man, n); 227 break; 228 case ROFFT_BODY: 229 if (n->child == NULL) 230 mandoc_vmsg(MANDOCERR_PAR_SKIP, 231 man->parse, n->line, n->pos, 232 "%s empty", roff_name[n->tok]); 233 break; 234 case ROFFT_HEAD: 235 if (n->child != NULL) 236 mandoc_vmsg(MANDOCERR_ARG_SKIP, 237 man->parse, n->line, n->pos, "%s %s%s", 238 roff_name[n->tok], n->child->string, 239 n->child->next != NULL ? " ..." : ""); 240 break; 241 default: 242 break; 243 } 244 } 245 246 static void 247 post_IP(CHKARGS) 248 { 249 250 switch (n->type) { 251 case ROFFT_BLOCK: 252 if (n->head->child == NULL && n->body->child == NULL) 253 roff_node_delete(man, n); 254 break; 255 case ROFFT_BODY: 256 if (n->parent->head->child == NULL && n->child == NULL) 257 mandoc_vmsg(MANDOCERR_PAR_SKIP, 258 man->parse, n->line, n->pos, 259 "%s empty", roff_name[n->tok]); 260 break; 261 default: 262 break; 263 } 264 } 265 266 static void 267 post_TH(CHKARGS) 268 { 269 struct roff_node *nb; 270 const char *p; 271 272 free(man->meta.title); 273 free(man->meta.vol); 274 free(man->meta.os); 275 free(man->meta.msec); 276 free(man->meta.date); 277 278 man->meta.title = man->meta.vol = man->meta.date = 279 man->meta.msec = man->meta.os = NULL; 280 281 nb = n; 282 283 /* ->TITLE<- MSEC DATE OS VOL */ 284 285 n = n->child; 286 if (n && n->string) { 287 for (p = n->string; '\0' != *p; p++) { 288 /* Only warn about this once... */ 289 if (isalpha((unsigned char)*p) && 290 ! isupper((unsigned char)*p)) { 291 mandoc_vmsg(MANDOCERR_TITLE_CASE, 292 man->parse, n->line, 293 n->pos + (p - n->string), 294 "TH %s", n->string); 295 break; 296 } 297 } 298 man->meta.title = mandoc_strdup(n->string); 299 } else { 300 man->meta.title = mandoc_strdup(""); 301 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse, 302 nb->line, nb->pos, "TH"); 303 } 304 305 /* TITLE ->MSEC<- DATE OS VOL */ 306 307 if (n) 308 n = n->next; 309 if (n && n->string) 310 man->meta.msec = mandoc_strdup(n->string); 311 else { 312 man->meta.msec = mandoc_strdup(""); 313 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse, 314 nb->line, nb->pos, "TH %s", man->meta.title); 315 } 316 317 /* TITLE MSEC ->DATE<- OS VOL */ 318 319 if (n) 320 n = n->next; 321 if (n && n->string && '\0' != n->string[0]) { 322 man->meta.date = man->quick ? 323 mandoc_strdup(n->string) : 324 mandoc_normdate(man->parse, n->string, 325 n->line, n->pos); 326 } else { 327 man->meta.date = mandoc_strdup(""); 328 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse, 329 n ? n->line : nb->line, 330 n ? n->pos : nb->pos, "TH"); 331 } 332 333 /* TITLE MSEC DATE ->OS<- VOL */ 334 335 if (n && (n = n->next)) 336 man->meta.os = mandoc_strdup(n->string); 337 else if (man->defos != NULL) 338 man->meta.os = mandoc_strdup(man->defos); 339 340 /* TITLE MSEC DATE OS ->VOL<- */ 341 /* If missing, use the default VOL name for MSEC. */ 342 343 if (n && (n = n->next)) 344 man->meta.vol = mandoc_strdup(n->string); 345 else if ('\0' != man->meta.msec[0] && 346 (NULL != (p = mandoc_a2msec(man->meta.msec)))) 347 man->meta.vol = mandoc_strdup(p); 348 349 if (n != NULL && (n = n->next) != NULL) 350 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, 351 n->line, n->pos, "TH ... %s", n->string); 352 353 /* 354 * Remove the `TH' node after we've processed it for our 355 * meta-data. 356 */ 357 roff_node_delete(man, man->last); 358 } 359 360 static void 361 post_UC(CHKARGS) 362 { 363 static const char * const bsd_versions[] = { 364 "3rd Berkeley Distribution", 365 "4th Berkeley Distribution", 366 "4.2 Berkeley Distribution", 367 "4.3 Berkeley Distribution", 368 "4.4 Berkeley Distribution", 369 }; 370 371 const char *p, *s; 372 373 n = n->child; 374 375 if (n == NULL || n->type != ROFFT_TEXT) 376 p = bsd_versions[0]; 377 else { 378 s = n->string; 379 if (0 == strcmp(s, "3")) 380 p = bsd_versions[0]; 381 else if (0 == strcmp(s, "4")) 382 p = bsd_versions[1]; 383 else if (0 == strcmp(s, "5")) 384 p = bsd_versions[2]; 385 else if (0 == strcmp(s, "6")) 386 p = bsd_versions[3]; 387 else if (0 == strcmp(s, "7")) 388 p = bsd_versions[4]; 389 else 390 p = bsd_versions[0]; 391 } 392 393 free(man->meta.os); 394 man->meta.os = mandoc_strdup(p); 395 } 396 397 static void 398 post_AT(CHKARGS) 399 { 400 static const char * const unix_versions[] = { 401 "7th Edition", 402 "System III", 403 "System V", 404 "System V Release 2", 405 }; 406 407 struct roff_node *nn; 408 const char *p, *s; 409 410 n = n->child; 411 412 if (n == NULL || n->type != ROFFT_TEXT) 413 p = unix_versions[0]; 414 else { 415 s = n->string; 416 if (0 == strcmp(s, "3")) 417 p = unix_versions[0]; 418 else if (0 == strcmp(s, "4")) 419 p = unix_versions[1]; 420 else if (0 == strcmp(s, "5")) { 421 nn = n->next; 422 if (nn != NULL && 423 nn->type == ROFFT_TEXT && 424 nn->string[0] != '\0') 425 p = unix_versions[3]; 426 else 427 p = unix_versions[2]; 428 } else 429 p = unix_versions[0]; 430 } 431 432 free(man->meta.os); 433 man->meta.os = mandoc_strdup(p); 434 } 435 436 static void 437 post_vs(CHKARGS) 438 { 439 440 if (NULL != n->prev) 441 return; 442 443 switch (n->parent->tok) { 444 case MAN_SH: 445 case MAN_SS: 446 case MAN_PP: 447 case MAN_LP: 448 case MAN_P: 449 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos, 450 "%s after %s", roff_name[n->tok], 451 roff_name[n->parent->tok]); 452 /* FALLTHROUGH */ 453 case TOKEN_NONE: 454 /* 455 * Don't warn about this because it occurs in pod2man 456 * and would cause considerable (unfixable) warnage. 457 */ 458 roff_node_delete(man, n); 459 break; 460 default: 461 break; 462 } 463 } 464