]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
mdoc(7) already uses the mandoc(1) -Ios argument in the footer line
[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, 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 "man.h"
32 #include "mandoc.h"
33 #include "mandoc_aux.h"
34 #include "libman.h"
35 #include "libmandoc.h"
36
37 #define CHKARGS struct man *man, struct man_node *n
38
39 typedef void (*v_check)(CHKARGS);
40
41 static void check_eq0(CHKARGS);
42 static void check_eq2(CHKARGS);
43 static void check_le1(CHKARGS);
44 static void check_le5(CHKARGS);
45 static void check_par(CHKARGS);
46 static void check_part(CHKARGS);
47 static void check_root(CHKARGS);
48 static void check_text(CHKARGS);
49
50 static void post_AT(CHKARGS);
51 static void post_IP(CHKARGS);
52 static void post_vs(CHKARGS);
53 static void post_fi(CHKARGS);
54 static void post_ft(CHKARGS);
55 static void post_nf(CHKARGS);
56 static void post_TH(CHKARGS);
57 static void post_UC(CHKARGS);
58 static void post_UR(CHKARGS);
59
60 static v_check man_valids[MAN_MAX] = {
61 post_vs, /* br */
62 post_TH, /* TH */
63 NULL, /* SH */
64 NULL, /* SS */
65 NULL, /* TP */
66 check_par, /* LP */
67 check_par, /* PP */
68 check_par, /* P */
69 post_IP, /* IP */
70 NULL, /* HP */
71 NULL, /* SM */
72 NULL, /* SB */
73 NULL, /* BI */
74 NULL, /* IB */
75 NULL, /* BR */
76 NULL, /* RB */
77 NULL, /* R */
78 NULL, /* B */
79 NULL, /* I */
80 NULL, /* IR */
81 NULL, /* RI */
82 check_eq0, /* na */
83 post_vs, /* sp */
84 post_nf, /* nf */
85 post_fi, /* fi */
86 NULL, /* RE */
87 check_part, /* RS */
88 NULL, /* DT */
89 post_UC, /* UC */
90 check_le1, /* PD */
91 post_AT, /* AT */
92 NULL, /* in */
93 post_ft, /* ft */
94 check_eq2, /* OP */
95 post_nf, /* EX */
96 post_fi, /* EE */
97 post_UR, /* UR */
98 NULL, /* UE */
99 NULL, /* ll */
100 };
101
102
103 void
104 man_valid_post(struct man *man)
105 {
106 struct man_node *n;
107 v_check *cp;
108
109 n = man->last;
110 if (n->flags & MAN_VALID)
111 return;
112 n->flags |= MAN_VALID;
113
114 switch (n->type) {
115 case MAN_TEXT:
116 check_text(man, n);
117 break;
118 case MAN_ROOT:
119 check_root(man, n);
120 break;
121 case MAN_EQN:
122 /* FALLTHROUGH */
123 case MAN_TBL:
124 break;
125 default:
126 cp = man_valids + n->tok;
127 if (*cp)
128 (*cp)(man, n);
129 break;
130 }
131 }
132
133 static void
134 check_root(CHKARGS)
135 {
136
137 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
138
139 if (NULL == man->first->child)
140 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
141 n->line, n->pos, NULL);
142 else
143 man->meta.hasbody = 1;
144
145 if (NULL == man->meta.title) {
146 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
147 n->line, n->pos, NULL);
148
149 /*
150 * If a title hasn't been set, do so now (by
151 * implication, date and section also aren't set).
152 */
153
154 man->meta.title = mandoc_strdup("");
155 man->meta.msec = mandoc_strdup("");
156 man->meta.date = man->quick ? mandoc_strdup("") :
157 mandoc_normdate(man->parse, NULL, n->line, n->pos);
158 }
159 }
160
161 static void
162 check_text(CHKARGS)
163 {
164 char *cp, *p;
165
166 if (MAN_LITERAL & man->flags)
167 return;
168
169 cp = n->string;
170 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
171 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
172 n->line, n->pos + (p - cp), NULL);
173 }
174
175 #define INEQ_DEFINE(x, ineq, name) \
176 static void \
177 check_##name(CHKARGS) \
178 { \
179 if (n->nchild ineq (x)) \
180 return; \
181 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
182 "line arguments %s %d (have %d)", \
183 #ineq, (x), n->nchild); \
184 }
185
186 INEQ_DEFINE(0, ==, eq0)
187 INEQ_DEFINE(2, ==, eq2)
188 INEQ_DEFINE(1, <=, le1)
189 INEQ_DEFINE(5, <=, le5)
190
191 static void
192 post_UR(CHKARGS)
193 {
194
195 if (MAN_HEAD == n->type && 1 != n->nchild)
196 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
197 n->pos, "line arguments eq 1 (have %d)", n->nchild);
198 check_part(man, n);
199 }
200
201 static void
202 post_ft(CHKARGS)
203 {
204 char *cp;
205 int ok;
206
207 if (0 == n->nchild)
208 return;
209
210 ok = 0;
211 cp = n->child->string;
212 switch (*cp) {
213 case '1':
214 /* FALLTHROUGH */
215 case '2':
216 /* FALLTHROUGH */
217 case '3':
218 /* FALLTHROUGH */
219 case '4':
220 /* FALLTHROUGH */
221 case 'I':
222 /* FALLTHROUGH */
223 case 'P':
224 /* FALLTHROUGH */
225 case 'R':
226 if ('\0' == cp[1])
227 ok = 1;
228 break;
229 case 'B':
230 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
231 ok = 1;
232 break;
233 case 'C':
234 if ('W' == cp[1] && '\0' == cp[2])
235 ok = 1;
236 break;
237 default:
238 break;
239 }
240
241 if (0 == ok) {
242 mandoc_vmsg(MANDOCERR_FT_BAD, man->parse,
243 n->line, n->pos, "ft %s", cp);
244 *cp = '\0';
245 }
246
247 if (1 < n->nchild)
248 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
249 n->pos, "want one child (have %d)", n->nchild);
250 }
251
252 static void
253 check_part(CHKARGS)
254 {
255
256 if (MAN_BODY == n->type && 0 == n->nchild)
257 mandoc_msg(MANDOCERR_ARGCWARN, man->parse, n->line,
258 n->pos, "want children (have none)");
259 }
260
261 static void
262 check_par(CHKARGS)
263 {
264
265 switch (n->type) {
266 case MAN_BLOCK:
267 if (0 == n->body->nchild)
268 man_node_delete(man, n);
269 break;
270 case MAN_BODY:
271 if (0 == n->nchild)
272 mandoc_vmsg(MANDOCERR_PAR_SKIP,
273 man->parse, n->line, n->pos,
274 "%s empty", man_macronames[n->tok]);
275 break;
276 case MAN_HEAD:
277 if (n->nchild)
278 mandoc_vmsg(MANDOCERR_ARG_SKIP,
279 man->parse, n->line, n->pos,
280 "%s %s%s", man_macronames[n->tok],
281 n->child->string,
282 n->nchild > 1 ? " ..." : "");
283 break;
284 default:
285 break;
286 }
287 }
288
289 static void
290 post_IP(CHKARGS)
291 {
292
293 switch (n->type) {
294 case MAN_BLOCK:
295 if (0 == n->head->nchild && 0 == n->body->nchild)
296 man_node_delete(man, n);
297 break;
298 case MAN_BODY:
299 if (0 == n->parent->head->nchild && 0 == n->nchild)
300 mandoc_vmsg(MANDOCERR_PAR_SKIP,
301 man->parse, n->line, n->pos,
302 "%s empty", man_macronames[n->tok]);
303 break;
304 default:
305 break;
306 }
307 }
308
309 static void
310 post_TH(CHKARGS)
311 {
312 struct man_node *nb;
313 const char *p;
314
315 check_le5(man, n);
316
317 free(man->meta.title);
318 free(man->meta.vol);
319 free(man->meta.source);
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.source = NULL;
325
326 nb = n;
327
328 /* ->TITLE<- MSEC DATE SOURCE 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 SOURCE 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<- SOURCE 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 ->SOURCE<- VOL */
379
380 if (n && (n = n->next))
381 man->meta.source = mandoc_strdup(n->string);
382 else if (man->defos != NULL)
383 man->meta.source = mandoc_strdup(man->defos);
384
385 /* TITLE MSEC DATE SOURCE ->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 /*
395 * Remove the `TH' node after we've processed it for our
396 * meta-data.
397 */
398 man_node_delete(man, man->last);
399 }
400
401 static void
402 post_nf(CHKARGS)
403 {
404
405 check_eq0(man, n);
406
407 if (MAN_LITERAL & man->flags)
408 mandoc_msg(MANDOCERR_NF_SKIP, man->parse,
409 n->line, n->pos, "nf");
410
411 man->flags |= MAN_LITERAL;
412 }
413
414 static void
415 post_fi(CHKARGS)
416 {
417
418 check_eq0(man, n);
419
420 if ( ! (MAN_LITERAL & man->flags))
421 mandoc_msg(MANDOCERR_FI_SKIP, man->parse,
422 n->line, n->pos, "fi");
423
424 man->flags &= ~MAN_LITERAL;
425 }
426
427 static void
428 post_UC(CHKARGS)
429 {
430 static const char * const bsd_versions[] = {
431 "3rd Berkeley Distribution",
432 "4th Berkeley Distribution",
433 "4.2 Berkeley Distribution",
434 "4.3 Berkeley Distribution",
435 "4.4 Berkeley Distribution",
436 };
437
438 const char *p, *s;
439
440 n = n->child;
441
442 if (NULL == n || MAN_TEXT != n->type)
443 p = bsd_versions[0];
444 else {
445 s = n->string;
446 if (0 == strcmp(s, "3"))
447 p = bsd_versions[0];
448 else if (0 == strcmp(s, "4"))
449 p = bsd_versions[1];
450 else if (0 == strcmp(s, "5"))
451 p = bsd_versions[2];
452 else if (0 == strcmp(s, "6"))
453 p = bsd_versions[3];
454 else if (0 == strcmp(s, "7"))
455 p = bsd_versions[4];
456 else
457 p = bsd_versions[0];
458 }
459
460 free(man->meta.source);
461 man->meta.source = mandoc_strdup(p);
462 }
463
464 static void
465 post_AT(CHKARGS)
466 {
467 static const char * const unix_versions[] = {
468 "7th Edition",
469 "System III",
470 "System V",
471 "System V Release 2",
472 };
473
474 const char *p, *s;
475 struct man_node *nn;
476
477 n = n->child;
478
479 if (NULL == n || MAN_TEXT != n->type)
480 p = unix_versions[0];
481 else {
482 s = n->string;
483 if (0 == strcmp(s, "3"))
484 p = unix_versions[0];
485 else if (0 == strcmp(s, "4"))
486 p = unix_versions[1];
487 else if (0 == strcmp(s, "5")) {
488 nn = n->next;
489 if (nn && MAN_TEXT == nn->type && nn->string[0])
490 p = unix_versions[3];
491 else
492 p = unix_versions[2];
493 } else
494 p = unix_versions[0];
495 }
496
497 free(man->meta.source);
498 man->meta.source = mandoc_strdup(p);
499 }
500
501 static void
502 post_vs(CHKARGS)
503 {
504
505 if (n->tok == MAN_br)
506 check_eq0(man, n);
507 else
508 check_le1(man, n);
509
510 if (NULL != n->prev)
511 return;
512
513 switch (n->parent->tok) {
514 case MAN_SH:
515 /* FALLTHROUGH */
516 case MAN_SS:
517 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
518 "%s after %s", man_macronames[n->tok],
519 man_macronames[n->parent->tok]);
520 /* FALLTHROUGH */
521 case MAN_MAX:
522 /*
523 * Don't warn about this because it occurs in pod2man
524 * and would cause considerable (unfixable) warnage.
525 */
526 man_node_delete(man, n);
527 break;
528 default:
529 break;
530 }
531 }