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