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