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