]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
set a reasonable default for .Os
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.100 2014/07/07 21:36:20 schwarze Exp $ */
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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
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 <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "man.h"
34 #include "mandoc.h"
35 #include "mandoc_aux.h"
36 #include "libman.h"
37 #include "libmandoc.h"
38
39 #define CHKARGS struct man *man, struct man_node *n
40
41 typedef int (*v_check)(CHKARGS);
42
43 struct man_valid {
44 v_check *pres;
45 v_check *posts;
46 };
47
48 static int check_eq0(CHKARGS);
49 static int check_eq2(CHKARGS);
50 static int check_le1(CHKARGS);
51 static int check_ge2(CHKARGS);
52 static int check_le5(CHKARGS);
53 static int check_head1(CHKARGS);
54 static int check_par(CHKARGS);
55 static int check_part(CHKARGS);
56 static int check_root(CHKARGS);
57 static void check_text(CHKARGS);
58
59 static int post_AT(CHKARGS);
60 static int post_IP(CHKARGS);
61 static int post_vs(CHKARGS);
62 static int post_fi(CHKARGS);
63 static int post_ft(CHKARGS);
64 static int post_nf(CHKARGS);
65 static int post_sec(CHKARGS);
66 static int post_TH(CHKARGS);
67 static int post_UC(CHKARGS);
68 static int pre_sec(CHKARGS);
69
70 static v_check posts_at[] = { post_AT, NULL };
71 static v_check posts_br[] = { post_vs, check_eq0, NULL };
72 static v_check posts_eq0[] = { check_eq0, NULL };
73 static v_check posts_eq2[] = { check_eq2, NULL };
74 static v_check posts_fi[] = { check_eq0, post_fi, NULL };
75 static v_check posts_ft[] = { post_ft, NULL };
76 static v_check posts_ip[] = { post_IP, NULL };
77 static v_check posts_le1[] = { check_le1, NULL };
78 static v_check posts_nf[] = { check_eq0, post_nf, NULL };
79 static v_check posts_par[] = { check_par, NULL };
80 static v_check posts_part[] = { check_part, NULL };
81 static v_check posts_sec[] = { post_sec, NULL };
82 static v_check posts_sp[] = { post_vs, check_le1, NULL };
83 static v_check posts_th[] = { check_ge2, check_le5, post_TH, NULL };
84 static v_check posts_uc[] = { post_UC, NULL };
85 static v_check posts_ur[] = { check_head1, check_part, NULL };
86 static v_check pres_sec[] = { pre_sec, NULL };
87
88 static const struct man_valid man_valids[MAN_MAX] = {
89 { NULL, posts_br }, /* br */
90 { NULL, posts_th }, /* TH */
91 { pres_sec, posts_sec }, /* SH */
92 { pres_sec, posts_sec }, /* SS */
93 { NULL, NULL }, /* TP */
94 { NULL, posts_par }, /* LP */
95 { NULL, posts_par }, /* PP */
96 { NULL, posts_par }, /* P */
97 { NULL, posts_ip }, /* IP */
98 { NULL, NULL }, /* HP */
99 { NULL, NULL }, /* SM */
100 { NULL, NULL }, /* SB */
101 { NULL, NULL }, /* BI */
102 { NULL, NULL }, /* IB */
103 { NULL, NULL }, /* BR */
104 { NULL, NULL }, /* RB */
105 { NULL, NULL }, /* R */
106 { NULL, NULL }, /* B */
107 { NULL, NULL }, /* I */
108 { NULL, NULL }, /* IR */
109 { NULL, NULL }, /* RI */
110 { NULL, posts_eq0 }, /* na */
111 { NULL, posts_sp }, /* sp */
112 { NULL, posts_nf }, /* nf */
113 { NULL, posts_fi }, /* fi */
114 { NULL, NULL }, /* RE */
115 { NULL, posts_part }, /* RS */
116 { NULL, NULL }, /* DT */
117 { NULL, posts_uc }, /* UC */
118 { NULL, posts_le1 }, /* PD */
119 { NULL, posts_at }, /* AT */
120 { NULL, NULL }, /* in */
121 { NULL, posts_ft }, /* ft */
122 { NULL, posts_eq2 }, /* OP */
123 { NULL, posts_nf }, /* EX */
124 { NULL, posts_fi }, /* EE */
125 { NULL, posts_ur }, /* UR */
126 { NULL, NULL }, /* UE */
127 { NULL, NULL }, /* ll */
128 };
129
130
131 int
132 man_valid_pre(struct man *man, struct man_node *n)
133 {
134 v_check *cp;
135
136 switch (n->type) {
137 case MAN_TEXT:
138 /* FALLTHROUGH */
139 case MAN_ROOT:
140 /* FALLTHROUGH */
141 case MAN_EQN:
142 /* FALLTHROUGH */
143 case MAN_TBL:
144 return(1);
145 default:
146 break;
147 }
148
149 if (NULL == (cp = man_valids[n->tok].pres))
150 return(1);
151 for ( ; *cp; cp++)
152 if ( ! (*cp)(man, n))
153 return(0);
154 return(1);
155 }
156
157 int
158 man_valid_post(struct man *man)
159 {
160 v_check *cp;
161
162 if (MAN_VALID & man->last->flags)
163 return(1);
164 man->last->flags |= MAN_VALID;
165
166 switch (man->last->type) {
167 case MAN_TEXT:
168 check_text(man, man->last);
169 return(1);
170 case MAN_ROOT:
171 return(check_root(man, man->last));
172 case MAN_EQN:
173 /* FALLTHROUGH */
174 case MAN_TBL:
175 return(1);
176 default:
177 break;
178 }
179
180 if (NULL == (cp = man_valids[man->last->tok].posts))
181 return(1);
182 for ( ; *cp; cp++)
183 if ( ! (*cp)(man, man->last))
184 return(0);
185
186 return(1);
187 }
188
189 static int
190 check_root(CHKARGS)
191 {
192
193 if ((MAN_BLINE | MAN_ELINE) & man->flags)
194 mandoc_msg(MANDOCERR_BLK_LINE, man->parse,
195 0, 0, "at end of file");
196
197 man->flags &= ~MAN_BLINE;
198 man->flags &= ~MAN_ELINE;
199
200 if (NULL == man->first->child)
201 man_nmsg(man, n, MANDOCERR_DOC_EMPTY);
202 else
203 man->meta.hasbody = 1;
204
205 if (NULL == man->meta.title) {
206 man_nmsg(man, n, MANDOCERR_TH_MISSING);
207
208 /*
209 * If a title hasn't been set, do so now (by
210 * implication, date and section also aren't set).
211 */
212
213 man->meta.title = mandoc_strdup("unknown");
214 man->meta.msec = mandoc_strdup("1");
215 man->meta.date = man->quick ? mandoc_strdup("") :
216 mandoc_normdate(man->parse, NULL, n->line, n->pos);
217 }
218
219 return(1);
220 }
221
222 static void
223 check_text(CHKARGS)
224 {
225 char *cp, *p;
226
227 if (MAN_LITERAL & man->flags)
228 return;
229
230 cp = n->string;
231 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
232 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
233 n->line, n->pos + (p - cp), NULL);
234 }
235
236 #define INEQ_DEFINE(x, ineq, name) \
237 static int \
238 check_##name(CHKARGS) \
239 { \
240 if (n->nchild ineq (x)) \
241 return(1); \
242 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
243 "line arguments %s %d (have %d)", \
244 #ineq, (x), n->nchild); \
245 return(1); \
246 }
247
248 INEQ_DEFINE(0, ==, eq0)
249 INEQ_DEFINE(2, ==, eq2)
250 INEQ_DEFINE(1, <=, le1)
251 INEQ_DEFINE(2, >=, ge2)
252 INEQ_DEFINE(5, <=, le5)
253
254 static int
255 check_head1(CHKARGS)
256 {
257
258 if (MAN_HEAD == n->type && 1 != n->nchild)
259 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
260 n->pos, "line arguments eq 1 (have %d)", n->nchild);
261
262 return(1);
263 }
264
265 static int
266 post_ft(CHKARGS)
267 {
268 char *cp;
269 int ok;
270
271 if (0 == n->nchild)
272 return(1);
273
274 ok = 0;
275 cp = n->child->string;
276 switch (*cp) {
277 case '1':
278 /* FALLTHROUGH */
279 case '2':
280 /* FALLTHROUGH */
281 case '3':
282 /* FALLTHROUGH */
283 case '4':
284 /* FALLTHROUGH */
285 case 'I':
286 /* FALLTHROUGH */
287 case 'P':
288 /* FALLTHROUGH */
289 case 'R':
290 if ('\0' == cp[1])
291 ok = 1;
292 break;
293 case 'B':
294 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
295 ok = 1;
296 break;
297 case 'C':
298 if ('W' == cp[1] && '\0' == cp[2])
299 ok = 1;
300 break;
301 default:
302 break;
303 }
304
305 if (0 == ok) {
306 mandoc_vmsg(MANDOCERR_FT_BAD, man->parse,
307 n->line, n->pos, "ft %s", cp);
308 *cp = '\0';
309 }
310
311 if (1 < n->nchild)
312 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
313 n->pos, "want one child (have %d)", n->nchild);
314
315 return(1);
316 }
317
318 static int
319 pre_sec(CHKARGS)
320 {
321
322 if (MAN_BLOCK == n->type)
323 man->flags &= ~MAN_LITERAL;
324 return(1);
325 }
326
327 static int
328 post_sec(CHKARGS)
329 {
330
331 if ( ! (MAN_HEAD == n->type && 0 == n->nchild))
332 return(1);
333
334 man_nmsg(man, n, MANDOCERR_SYNTARGCOUNT);
335 return(0);
336 }
337
338 static int
339 check_part(CHKARGS)
340 {
341
342 if (MAN_BODY == n->type && 0 == n->nchild)
343 mandoc_msg(MANDOCERR_ARGCWARN, man->parse, n->line,
344 n->pos, "want children (have none)");
345
346 return(1);
347 }
348
349 static int
350 check_par(CHKARGS)
351 {
352
353 switch (n->type) {
354 case MAN_BLOCK:
355 if (0 == n->body->nchild)
356 man_node_delete(man, n);
357 break;
358 case MAN_BODY:
359 if (0 == n->nchild)
360 mandoc_vmsg(MANDOCERR_PAR_SKIP,
361 man->parse, n->line, n->pos,
362 "%s empty", man_macronames[n->tok]);
363 break;
364 case MAN_HEAD:
365 if (n->nchild)
366 mandoc_vmsg(MANDOCERR_ARG_SKIP,
367 man->parse, n->line, n->pos,
368 "%s %s%s", man_macronames[n->tok],
369 n->child->string,
370 n->nchild > 1 ? " ..." : "");
371 break;
372 default:
373 break;
374 }
375
376 return(1);
377 }
378
379 static int
380 post_IP(CHKARGS)
381 {
382
383 switch (n->type) {
384 case MAN_BLOCK:
385 if (0 == n->head->nchild && 0 == n->body->nchild)
386 man_node_delete(man, n);
387 break;
388 case MAN_BODY:
389 if (0 == n->parent->head->nchild && 0 == n->nchild)
390 mandoc_vmsg(MANDOCERR_PAR_SKIP,
391 man->parse, n->line, n->pos,
392 "%s empty", man_macronames[n->tok]);
393 break;
394 default:
395 break;
396 }
397 return(1);
398 }
399
400 static int
401 post_TH(CHKARGS)
402 {
403 struct man_node *nb;
404 const char *p;
405
406 free(man->meta.title);
407 free(man->meta.vol);
408 free(man->meta.source);
409 free(man->meta.msec);
410 free(man->meta.date);
411
412 man->meta.title = man->meta.vol = man->meta.date =
413 man->meta.msec = man->meta.source = NULL;
414
415 nb = n;
416
417 /* ->TITLE<- MSEC DATE SOURCE VOL */
418
419 n = n->child;
420 if (n && n->string) {
421 for (p = n->string; '\0' != *p; p++) {
422 /* Only warn about this once... */
423 if (isalpha((unsigned char)*p) &&
424 ! isupper((unsigned char)*p)) {
425 mandoc_msg(MANDOCERR_TITLE_CASE,
426 man->parse, n->line,
427 n->pos + (p - n->string),
428 n->string);
429 break;
430 }
431 }
432 man->meta.title = mandoc_strdup(n->string);
433 } else
434 man->meta.title = mandoc_strdup("");
435
436 /* TITLE ->MSEC<- DATE SOURCE VOL */
437
438 if (n)
439 n = n->next;
440 if (n && n->string)
441 man->meta.msec = mandoc_strdup(n->string);
442 else
443 man->meta.msec = mandoc_strdup("");
444
445 /* TITLE MSEC ->DATE<- SOURCE VOL */
446
447 if (n)
448 n = n->next;
449 if (n && n->string && '\0' != n->string[0]) {
450 man->meta.date = man->quick ?
451 mandoc_strdup(n->string) :
452 mandoc_normdate(man->parse, n->string,
453 n->line, n->pos);
454 } else {
455 man->meta.date = mandoc_strdup("");
456 man_nmsg(man, n ? n : nb, MANDOCERR_DATE_MISSING);
457 }
458
459 /* TITLE MSEC DATE ->SOURCE<- VOL */
460
461 if (n && (n = n->next))
462 man->meta.source = mandoc_strdup(n->string);
463
464 /* TITLE MSEC DATE SOURCE ->VOL<- */
465 /* If missing, use the default VOL name for MSEC. */
466
467 if (n && (n = n->next))
468 man->meta.vol = mandoc_strdup(n->string);
469 else if ('\0' != man->meta.msec[0] &&
470 (NULL != (p = mandoc_a2msec(man->meta.msec))))
471 man->meta.vol = mandoc_strdup(p);
472
473 /*
474 * Remove the `TH' node after we've processed it for our
475 * meta-data.
476 */
477 man_node_delete(man, man->last);
478 return(1);
479 }
480
481 static int
482 post_nf(CHKARGS)
483 {
484
485 if (MAN_LITERAL & man->flags)
486 man_nmsg(man, n, MANDOCERR_NF_SKIP);
487
488 man->flags |= MAN_LITERAL;
489 return(1);
490 }
491
492 static int
493 post_fi(CHKARGS)
494 {
495
496 if ( ! (MAN_LITERAL & man->flags))
497 man_nmsg(man, n, MANDOCERR_FI_SKIP);
498
499 man->flags &= ~MAN_LITERAL;
500 return(1);
501 }
502
503 static int
504 post_UC(CHKARGS)
505 {
506 static const char * const bsd_versions[] = {
507 "3rd Berkeley Distribution",
508 "4th Berkeley Distribution",
509 "4.2 Berkeley Distribution",
510 "4.3 Berkeley Distribution",
511 "4.4 Berkeley Distribution",
512 };
513
514 const char *p, *s;
515
516 n = n->child;
517
518 if (NULL == n || MAN_TEXT != n->type)
519 p = bsd_versions[0];
520 else {
521 s = n->string;
522 if (0 == strcmp(s, "3"))
523 p = bsd_versions[0];
524 else if (0 == strcmp(s, "4"))
525 p = bsd_versions[1];
526 else if (0 == strcmp(s, "5"))
527 p = bsd_versions[2];
528 else if (0 == strcmp(s, "6"))
529 p = bsd_versions[3];
530 else if (0 == strcmp(s, "7"))
531 p = bsd_versions[4];
532 else
533 p = bsd_versions[0];
534 }
535
536 free(man->meta.source);
537 man->meta.source = mandoc_strdup(p);
538 return(1);
539 }
540
541 static int
542 post_AT(CHKARGS)
543 {
544 static const char * const unix_versions[] = {
545 "7th Edition",
546 "System III",
547 "System V",
548 "System V Release 2",
549 };
550
551 const char *p, *s;
552 struct man_node *nn;
553
554 n = n->child;
555
556 if (NULL == n || MAN_TEXT != n->type)
557 p = unix_versions[0];
558 else {
559 s = n->string;
560 if (0 == strcmp(s, "3"))
561 p = unix_versions[0];
562 else if (0 == strcmp(s, "4"))
563 p = unix_versions[1];
564 else if (0 == strcmp(s, "5")) {
565 nn = n->next;
566 if (nn && MAN_TEXT == nn->type && nn->string[0])
567 p = unix_versions[3];
568 else
569 p = unix_versions[2];
570 } else
571 p = unix_versions[0];
572 }
573
574 free(man->meta.source);
575 man->meta.source = mandoc_strdup(p);
576 return(1);
577 }
578
579 static int
580 post_vs(CHKARGS)
581 {
582
583 if (NULL != n->prev)
584 return(1);
585
586 switch (n->parent->tok) {
587 case MAN_SH:
588 /* FALLTHROUGH */
589 case MAN_SS:
590 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
591 "%s after %s", man_macronames[n->tok],
592 man_macronames[n->parent->tok]);
593 /* FALLTHROUGH */
594 case MAN_MAX:
595 /*
596 * Don't warn about this because it occurs in pod2man
597 * and would cause considerable (unfixable) warnage.
598 */
599 man_node_delete(man, n);
600 break;
601 default:
602 break;
603 }
604
605 return(1);
606 }