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