fstat - Fix path construction for "wd".
[dragonfly.git] / usr.bin / mandoc / mdoc_validate.c
1 /*      $Id: mdoc_validate.c,v 1.58 2010/02/17 19:28:11 kristaps Exp $ */
2 /*
3  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "libmdoc.h"
27 #include "libmandoc.h"
28
29 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
30 /* TODO: ignoring Pp (it's superfluous in some invocations). */
31
32 #define PRE_ARGS  struct mdoc *mdoc, const struct mdoc_node *n
33 #define POST_ARGS struct mdoc *mdoc
34
35 typedef int     (*v_pre)(PRE_ARGS);
36 typedef int     (*v_post)(POST_ARGS);
37
38 struct  valids {
39         v_pre   *pre;
40         v_post  *post;
41 };
42
43 static  int      check_parent(PRE_ARGS, int, enum mdoc_type);
44 static  int      check_msec(PRE_ARGS, ...);
45 static  int      check_sec(PRE_ARGS, ...);
46 static  int      check_stdarg(PRE_ARGS);
47 static  int      check_text(struct mdoc *, int, int, const char *);
48 static  int      check_argv(struct mdoc *,
49                         const struct mdoc_node *,
50                         const struct mdoc_argv *);
51 static  int      check_args(struct mdoc *,
52                         const struct mdoc_node *);
53 static  int      err_child_lt(struct mdoc *, const char *, int);
54 static  int      warn_child_lt(struct mdoc *, const char *, int);
55 static  int      err_child_gt(struct mdoc *, const char *, int);
56 static  int      warn_child_gt(struct mdoc *, const char *, int);
57 static  int      err_child_eq(struct mdoc *, const char *, int);
58 static  int      warn_child_eq(struct mdoc *, const char *, int);
59 static  int      warn_print(struct mdoc *, int, int);
60 static  int      warn_count(struct mdoc *, const char *,
61                         int, const char *, int);
62 static  int      err_count(struct mdoc *, const char *,
63                         int, const char *, int);
64
65 static  int      berr_ge1(POST_ARGS);
66 static  int      bwarn_ge1(POST_ARGS);
67 static  int      ebool(POST_ARGS);
68 static  int      eerr_eq0(POST_ARGS);
69 static  int      eerr_eq1(POST_ARGS);
70 static  int      eerr_ge1(POST_ARGS);
71 static  int      eerr_le1(POST_ARGS);
72 static  int      ewarn_ge1(POST_ARGS);
73 static  int      herr_eq0(POST_ARGS);
74 static  int      herr_ge1(POST_ARGS);
75 static  int      hwarn_eq1(POST_ARGS);
76 static  int      hwarn_le1(POST_ARGS);
77
78 static  int      post_an(POST_ARGS);
79 static  int      post_at(POST_ARGS);
80 static  int      post_bf(POST_ARGS);
81 static  int      post_bl(POST_ARGS);
82 static  int      post_bl_head(POST_ARGS);
83 static  int      post_it(POST_ARGS);
84 static  int      post_lb(POST_ARGS);
85 static  int      post_nm(POST_ARGS);
86 static  int      post_root(POST_ARGS);
87 static  int      post_rs(POST_ARGS);
88 static  int      post_sh(POST_ARGS);
89 static  int      post_sh_body(POST_ARGS);
90 static  int      post_sh_head(POST_ARGS);
91 static  int      post_st(POST_ARGS);
92 static  int      post_vt(POST_ARGS);
93 static  int      pre_an(PRE_ARGS);
94 static  int      pre_bd(PRE_ARGS);
95 static  int      pre_bl(PRE_ARGS);
96 static  int      pre_cd(PRE_ARGS);
97 static  int      pre_dd(PRE_ARGS);
98 static  int      pre_display(PRE_ARGS);
99 static  int      pre_dt(PRE_ARGS);
100 static  int      pre_er(PRE_ARGS);
101 static  int      pre_ex(PRE_ARGS);
102 static  int      pre_fd(PRE_ARGS);
103 static  int      pre_it(PRE_ARGS);
104 static  int      pre_lb(PRE_ARGS);
105 static  int      pre_os(PRE_ARGS);
106 static  int      pre_rv(PRE_ARGS);
107 static  int      pre_sh(PRE_ARGS);
108 static  int      pre_ss(PRE_ARGS);
109
110 static  v_post   posts_an[] = { post_an, NULL };
111 static  v_post   posts_at[] = { post_at, NULL };
112 static  v_post   posts_bd[] = { herr_eq0, bwarn_ge1, NULL };
113 static  v_post   posts_bf[] = { hwarn_le1, post_bf, NULL };
114 static  v_post   posts_bl[] = { bwarn_ge1, post_bl, NULL };
115 static  v_post   posts_bool[] = { eerr_eq1, ebool, NULL };
116 static  v_post   posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
117 static  v_post   posts_it[] = { post_it, NULL };
118 static  v_post   posts_lb[] = { eerr_eq1, post_lb, NULL };
119 static  v_post   posts_nd[] = { berr_ge1, NULL };
120 static  v_post   posts_nm[] = { post_nm, NULL };
121 static  v_post   posts_notext[] = { eerr_eq0, NULL };
122 static  v_post   posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
123 static  v_post   posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
124 static  v_post   posts_sp[] = { eerr_le1, NULL };
125 static  v_post   posts_ss[] = { herr_ge1, NULL };
126 static  v_post   posts_st[] = { eerr_eq1, post_st, NULL };
127 static  v_post   posts_text[] = { eerr_ge1, NULL };
128 static  v_post   posts_text1[] = { eerr_eq1, NULL };
129 static  v_post   posts_vt[] = { post_vt, NULL };
130 static  v_post   posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
131 static  v_post   posts_wtext[] = { ewarn_ge1, NULL };
132 static  v_post   posts_xr[] = { eerr_ge1, NULL };
133 static  v_pre    pres_an[] = { pre_an, NULL };
134 static  v_pre    pres_bd[] = { pre_display, pre_bd, NULL };
135 static  v_pre    pres_bl[] = { pre_bl, NULL };
136 static  v_pre    pres_cd[] = { pre_cd, NULL };
137 static  v_pre    pres_d1[] = { pre_display, NULL };
138 static  v_pre    pres_dd[] = { pre_dd, NULL };
139 static  v_pre    pres_dt[] = { pre_dt, NULL };
140 static  v_pre    pres_er[] = { pre_er, NULL };
141 static  v_pre    pres_ex[] = { pre_ex, NULL };
142 static  v_pre    pres_fd[] = { pre_fd, NULL };
143 static  v_pre    pres_it[] = { pre_it, NULL };
144 static  v_pre    pres_lb[] = { pre_lb, NULL };
145 static  v_pre    pres_os[] = { pre_os, NULL };
146 static  v_pre    pres_rv[] = { pre_rv, NULL };
147 static  v_pre    pres_sh[] = { pre_sh, NULL };
148 static  v_pre    pres_ss[] = { pre_ss, NULL };
149
150 const   struct valids mdoc_valids[MDOC_MAX] = {
151         { NULL, NULL },                         /* Ap */
152         { pres_dd, posts_text },                /* Dd */
153         { pres_dt, NULL },                      /* Dt */
154         { pres_os, NULL },                      /* Os */
155         { pres_sh, posts_sh },                  /* Sh */
156         { pres_ss, posts_ss },                  /* Ss */
157         { NULL, posts_notext },                 /* Pp */
158         { pres_d1, posts_wline },               /* D1 */
159         { pres_d1, posts_wline },               /* Dl */
160         { pres_bd, posts_bd },                  /* Bd */
161         { NULL, NULL },                         /* Ed */
162         { pres_bl, posts_bl },                  /* Bl */
163         { NULL, NULL },                         /* El */
164         { pres_it, posts_it },                  /* It */
165         { NULL, posts_text },                   /* Ad */
166         { pres_an, posts_an },                  /* An */
167         { NULL, NULL },                         /* Ar */
168         { pres_cd, posts_text },                /* Cd */
169         { NULL, NULL },                         /* Cm */
170         { NULL, NULL },                         /* Dv */
171         { pres_er, posts_text },                /* Er */
172         { NULL, NULL },                         /* Ev */
173         { pres_ex, NULL },                      /* Ex */
174         { NULL, NULL },                         /* Fa */
175         { pres_fd, posts_wtext },               /* Fd */
176         { NULL, NULL },                         /* Fl */
177         { NULL, posts_text },                   /* Fn */
178         { NULL, posts_wtext },                  /* Ft */
179         { NULL, posts_text },                   /* Ic */
180         { NULL, posts_text1 },                  /* In */
181         { NULL, NULL },                         /* Li */
182         { NULL, posts_nd },                     /* Nd */
183         { NULL, posts_nm },                     /* Nm */
184         { NULL, posts_wline },                  /* Op */
185         { NULL, NULL },                         /* Ot */
186         { NULL, NULL },                         /* Pa */
187         { pres_rv, NULL },                      /* Rv */
188         { NULL, posts_st },                     /* St */
189         { NULL, NULL },                         /* Va */
190         { NULL, posts_vt },                     /* Vt */
191         { NULL, posts_xr },                     /* Xr */
192         { NULL, posts_text },                   /* %A */
193         { NULL, posts_text },                   /* %B */ /* FIXME: can be used outside Rs/Re. */
194         { NULL, posts_text },                   /* %D */ /* FIXME: check date with mandoc_a2time(). */
195         { NULL, posts_text },                   /* %I */
196         { NULL, posts_text },                   /* %J */
197         { NULL, posts_text },                   /* %N */
198         { NULL, posts_text },                   /* %O */
199         { NULL, posts_text },                   /* %P */
200         { NULL, posts_text },                   /* %R */
201         { NULL, posts_text },                   /* %T */ /* FIXME: can be used outside Rs/Re. */
202         { NULL, posts_text },                   /* %V */
203         { NULL, NULL },                         /* Ac */
204         { NULL, NULL },                         /* Ao */
205         { NULL, posts_wline },                  /* Aq */
206         { NULL, posts_at },                     /* At */
207         { NULL, NULL },                         /* Bc */
208         { NULL, posts_bf },                     /* Bf */
209         { NULL, NULL },                         /* Bo */
210         { NULL, posts_wline },                  /* Bq */
211         { NULL, NULL },                         /* Bsx */
212         { NULL, NULL },                         /* Bx */
213         { NULL, posts_bool },                   /* Db */
214         { NULL, NULL },                         /* Dc */
215         { NULL, NULL },                         /* Do */
216         { NULL, posts_wline },                  /* Dq */
217         { NULL, NULL },                         /* Ec */
218         { NULL, NULL },                         /* Ef */
219         { NULL, NULL },                         /* Em */
220         { NULL, NULL },                         /* Eo */
221         { NULL, NULL },                         /* Fx */
222         { NULL, posts_text },                   /* Ms */
223         { NULL, posts_notext },                 /* No */
224         { NULL, posts_notext },                 /* Ns */
225         { NULL, NULL },                         /* Nx */
226         { NULL, NULL },                         /* Ox */
227         { NULL, NULL },                         /* Pc */
228         { NULL, posts_text1 },                  /* Pf */
229         { NULL, NULL },                         /* Po */
230         { NULL, posts_wline },                  /* Pq */
231         { NULL, NULL },                         /* Qc */
232         { NULL, posts_wline },                  /* Ql */
233         { NULL, NULL },                         /* Qo */
234         { NULL, posts_wline },                  /* Qq */
235         { NULL, NULL },                         /* Re */
236         { NULL, posts_rs },                     /* Rs */
237         { NULL, NULL },                         /* Sc */
238         { NULL, NULL },                         /* So */
239         { NULL, posts_wline },                  /* Sq */
240         { NULL, posts_bool },                   /* Sm */
241         { NULL, posts_text },                   /* Sx */
242         { NULL, posts_text },                   /* Sy */
243         { NULL, posts_text },                   /* Tn */
244         { NULL, NULL },                         /* Ux */
245         { NULL, NULL },                         /* Xc */
246         { NULL, NULL },                         /* Xo */
247         { NULL, posts_fo },                     /* Fo */
248         { NULL, NULL },                         /* Fc */
249         { NULL, NULL },                         /* Oo */
250         { NULL, NULL },                         /* Oc */
251         { NULL, posts_wline },                  /* Bk */
252         { NULL, NULL },                         /* Ek */
253         { NULL, posts_notext },                 /* Bt */
254         { NULL, NULL },                         /* Hf */
255         { NULL, NULL },                         /* Fr */
256         { NULL, posts_notext },                 /* Ud */
257         { pres_lb, posts_lb },                  /* Lb */
258         { NULL, posts_notext },                 /* Lp */
259         { NULL, posts_text },                   /* Lk */
260         { NULL, posts_text },                   /* Mt */
261         { NULL, posts_wline },                  /* Brq */
262         { NULL, NULL },                         /* Bro */
263         { NULL, NULL },                         /* Brc */
264         { NULL, posts_text },                   /* %C */
265         { NULL, NULL },                         /* Es */
266         { NULL, NULL },                         /* En */
267         { NULL, NULL },                         /* Dx */
268         { NULL, posts_text },                   /* %Q */
269         { NULL, posts_notext },                 /* br */
270         { NULL, posts_sp },                     /* sp */
271         { NULL, posts_text1 },                  /* %U */
272 };
273
274
275 int
276 mdoc_valid_pre(struct mdoc *mdoc, const struct mdoc_node *n)
277 {
278         v_pre           *p;
279         int              line, pos;
280         const char      *tp;
281
282         if (MDOC_TEXT == n->type) {
283                 tp = n->string;
284                 line = n->line;
285                 pos = n->pos;
286                 return(check_text(mdoc, line, pos, tp));
287         }
288
289         if ( ! check_args(mdoc, n))
290                 return(0);
291         if (NULL == mdoc_valids[n->tok].pre)
292                 return(1);
293         for (p = mdoc_valids[n->tok].pre; *p; p++)
294                 if ( ! (*p)(mdoc, n))
295                         return(0);
296         return(1);
297 }
298
299
300 int
301 mdoc_valid_post(struct mdoc *mdoc)
302 {
303         v_post          *p;
304
305         if (MDOC_VALID & mdoc->last->flags)
306                 return(1);
307         mdoc->last->flags |= MDOC_VALID;
308
309         if (MDOC_TEXT == mdoc->last->type)
310                 return(1);
311         if (MDOC_ROOT == mdoc->last->type)
312                 return(post_root(mdoc));
313
314         if (NULL == mdoc_valids[mdoc->last->tok].post)
315                 return(1);
316         for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
317                 if ( ! (*p)(mdoc))
318                         return(0);
319
320         return(1);
321 }
322
323
324 static int
325 warn_print(struct mdoc *m, int ln, int pos)
326 {
327
328         if (MDOC_IGN_CHARS & m->pflags)
329                 return(mdoc_pwarn(m, ln, pos, EPRINT));
330         return(mdoc_perr(m, ln, pos, EPRINT));
331 }
332
333
334 static inline int
335 warn_count(struct mdoc *m, const char *k,
336                 int want, const char *v, int has)
337 {
338
339         return(mdoc_vwarn(m, m->last->line, m->last->pos,
340                 "suggests %s %s %d (has %d)", v, k, want, has));
341 }
342
343
344 static inline int
345 err_count(struct mdoc *m, const char *k,
346                 int want, const char *v, int has)
347 {
348
349         return(mdoc_verr(m, m->last->line, m->last->pos,
350                 "requires %s %s %d (has %d)", v, k, want, has));
351 }
352
353
354 /*
355  * Build these up with macros because they're basically the same check
356  * for different inequalities.  Yes, this could be done with functions,
357  * but this is reasonable for now.
358  */
359
360 #define CHECK_CHILD_DEFN(lvl, name, ineq)                       \
361 static int                                                      \
362 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz)    \
363 {                                                               \
364         if (mdoc->last->nchild ineq sz)                         \
365                 return(1);                                      \
366         return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
367 }
368
369 #define CHECK_BODY_DEFN(name, lvl, func, num)                   \
370 static int                                                      \
371 b##lvl##_##name(POST_ARGS)                                      \
372 {                                                               \
373         if (MDOC_BODY != mdoc->last->type)                      \
374                 return(1);                                      \
375         return(func(mdoc, "multi-line arguments", (num)));      \
376 }
377
378 #define CHECK_ELEM_DEFN(name, lvl, func, num)                   \
379 static int                                                      \
380 e##lvl##_##name(POST_ARGS)                                      \
381 {                                                               \
382         assert(MDOC_ELEM == mdoc->last->type);                  \
383         return(func(mdoc, "line arguments", (num)));            \
384 }
385
386 #define CHECK_HEAD_DEFN(name, lvl, func, num)                   \
387 static int                                                      \
388 h##lvl##_##name(POST_ARGS)                                      \
389 {                                                               \
390         if (MDOC_HEAD != mdoc->last->type)                      \
391                 return(1);                                      \
392         return(func(mdoc, "line arguments", (num)));            \
393 }
394
395
396 CHECK_CHILD_DEFN(warn, gt, >)                   /* warn_child_gt() */
397 CHECK_CHILD_DEFN(err, gt, >)                    /* err_child_gt() */
398 CHECK_CHILD_DEFN(warn, eq, ==)                  /* warn_child_eq() */
399 CHECK_CHILD_DEFN(err, eq, ==)                   /* err_child_eq() */
400 CHECK_CHILD_DEFN(err, lt, <)                    /* err_child_lt() */
401 CHECK_CHILD_DEFN(warn, lt, <)                   /* warn_child_lt() */
402 CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0)    /* bwarn_ge1() */
403 CHECK_BODY_DEFN(ge1, err, err_child_gt, 0)      /* berr_ge1() */
404 CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0)    /* ewarn_gt1() */
405 CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1)      /* eerr_eq1() */
406 CHECK_ELEM_DEFN(le1, err, err_child_lt, 2)      /* eerr_le1() */
407 CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0)      /* eerr_eq0() */
408 CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0)      /* eerr_ge1() */
409 CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0)      /* herr_eq0() */
410 CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2)    /* hwarn_le1() */
411 CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0)      /* herr_ge1() */
412 CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1)    /* hwarn_eq1() */
413
414
415 static int
416 check_stdarg(PRE_ARGS)
417 {
418
419         if (n->args && 1 == n->args->argc)
420                 if (MDOC_Std == n->args->argv[0].arg)
421                         return(1);
422         return(mdoc_nwarn(mdoc, n, EARGVAL));
423 }
424
425
426 static int
427 check_sec(PRE_ARGS, ...)
428 {
429         enum mdoc_sec    sec;
430         va_list          ap;
431
432         va_start(ap, n);
433
434         for (;;) {
435                 /* LINTED */
436                 sec = (enum mdoc_sec)va_arg(ap, int);
437                 if (SEC_CUSTOM == sec)
438                         break;
439                 if (sec != mdoc->lastsec)
440                         continue;
441                 va_end(ap);
442                 return(1);
443         }
444
445         va_end(ap);
446         return(mdoc_nwarn(mdoc, n, EBADSEC));
447 }
448
449
450 static int
451 check_msec(PRE_ARGS, ...)
452 {
453         va_list          ap;
454         int              msec;
455
456         va_start(ap, n);
457         for (;;) {
458                 /* LINTED */
459                 if (0 == (msec = va_arg(ap, int)))
460                         break;
461                 if (msec != mdoc->meta.msec)
462                         continue;
463                 va_end(ap);
464                 return(1);
465         }
466
467         va_end(ap);
468         return(mdoc_nwarn(mdoc, n, EBADMSEC));
469 }
470
471
472 static int
473 check_args(struct mdoc *m, const struct mdoc_node *n)
474 {
475         int              i;
476
477         if (NULL == n->args)
478                 return(1);
479
480         assert(n->args->argc);
481         for (i = 0; i < (int)n->args->argc; i++)
482                 if ( ! check_argv(m, n, &n->args->argv[i]))
483                         return(0);
484
485         return(1);
486 }
487
488
489 static int
490 check_argv(struct mdoc *m, const struct mdoc_node *n,
491                 const struct mdoc_argv *v)
492 {
493         int              i;
494
495         for (i = 0; i < (int)v->sz; i++)
496                 if ( ! check_text(m, v->line, v->pos, v->value[i]))
497                         return(0);
498
499         if (MDOC_Std == v->arg) {
500                 /* `Nm' name must be set. */
501                 if (v->sz || m->meta.name)
502                         return(1);
503                 return(mdoc_nerr(m, n, ENAME));
504         }
505
506         return(1);
507 }
508
509
510 static int
511 check_text(struct mdoc *mdoc, int line, int pos, const char *p)
512 {
513         int              c;
514
515         for ( ; *p; p++, pos++) {
516                 if ('\t' == *p) {
517                         if ( ! (MDOC_LITERAL & mdoc->flags))
518                                 if ( ! warn_print(mdoc, line, pos))
519                                         return(0);
520                 } else if ( ! isprint((u_char)*p))
521                         if ( ! warn_print(mdoc, line, pos))
522                                 return(0);
523
524                 if ('\\' != *p)
525                         continue;
526
527                 c = mandoc_special(p);
528                 if (c) {
529                         p += c - 1;
530                         pos += c - 1;
531                         continue;
532                 }
533                 if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
534                         return(mdoc_perr(mdoc, line, pos, EESCAPE));
535                 if ( ! mdoc_pwarn(mdoc, line, pos, EESCAPE))
536                         return(0);
537         }
538
539         return(1);
540 }
541
542
543
544
545 static int
546 check_parent(PRE_ARGS, int tok, enum mdoc_type t)
547 {
548
549         assert(n->parent);
550         if ((MDOC_ROOT == t || tok == n->parent->tok) &&
551                         (t == n->parent->type))
552                 return(1);
553
554         return(mdoc_verr(mdoc, n->line, n->pos, "require parent %s",
555                 MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
556 }
557
558
559
560 static int
561 pre_display(PRE_ARGS)
562 {
563         struct mdoc_node *node;
564
565         /* Display elements (`Bd', `D1'...) cannot be nested. */
566
567         if (MDOC_BLOCK != n->type)
568                 return(1);
569
570         /* LINTED */
571         for (node = mdoc->last->parent; node; node = node->parent)
572                 if (MDOC_BLOCK == node->type)
573                         if (MDOC_Bd == node->tok)
574                                 break;
575         if (NULL == node)
576                 return(1);
577
578         return(mdoc_nerr(mdoc, n, ENESTDISP));
579 }
580
581
582 static int
583 pre_bl(PRE_ARGS)
584 {
585         int              pos, type, width, offset;
586
587         if (MDOC_BLOCK != n->type)
588                 return(1);
589         if (NULL == n->args)
590                 return(mdoc_nerr(mdoc, n, ELISTTYPE));
591
592         /* Make sure that only one type of list is specified.  */
593
594         type = offset = width = -1;
595
596         /* LINTED */
597         for (pos = 0; pos < (int)n->args->argc; pos++)
598                 switch (n->args->argv[pos].arg) {
599                 case (MDOC_Bullet):
600                         /* FALLTHROUGH */
601                 case (MDOC_Dash):
602                         /* FALLTHROUGH */
603                 case (MDOC_Enum):
604                         /* FALLTHROUGH */
605                 case (MDOC_Hyphen):
606                         /* FALLTHROUGH */
607                 case (MDOC_Item):
608                         /* FALLTHROUGH */
609                 case (MDOC_Tag):
610                         /* FALLTHROUGH */
611                 case (MDOC_Diag):
612                         /* FALLTHROUGH */
613                 case (MDOC_Hang):
614                         /* FALLTHROUGH */
615                 case (MDOC_Ohang):
616                         /* FALLTHROUGH */
617                 case (MDOC_Inset):
618                         /* FALLTHROUGH */
619                 case (MDOC_Column):
620                         if (type >= 0)
621                                 return(mdoc_nerr(mdoc, n, EMULTILIST));
622                         type = n->args->argv[pos].arg;
623                         break;
624                 case (MDOC_Compact):
625                         if (type < 0 && ! mdoc_nwarn(mdoc, n, ENOTYPE))
626                                 return(0);
627                         break;
628                 case (MDOC_Width):
629                         if (width >= 0)
630                                 return(mdoc_nerr(mdoc, n, EARGREP));
631                         if (type < 0 && ! mdoc_nwarn(mdoc, n, ENOTYPE))
632                                 return(0);
633                         width = n->args->argv[pos].arg;
634                         break;
635                 case (MDOC_Offset):
636                         if (offset >= 0)
637                                 return(mdoc_nerr(mdoc, n, EARGREP));
638                         if (type < 0 && ! mdoc_nwarn(mdoc, n, ENOTYPE))
639                                 return(0);
640                         offset = n->args->argv[pos].arg;
641                         break;
642                 default:
643                         break;
644                 }
645
646         if (type < 0)
647                 return(mdoc_nerr(mdoc, n, ELISTTYPE));
648
649         /*
650          * Validate the width field.  Some list types don't need width
651          * types and should be warned about them.  Others should have it
652          * and must also be warned.
653          */
654
655         switch (type) {
656         case (MDOC_Tag):
657                 if (width < 0 && ! mdoc_nwarn(mdoc, n, EMISSWIDTH))
658                         return(0);
659                 break;
660         case (MDOC_Column):
661                 /* FALLTHROUGH */
662         case (MDOC_Diag):
663                 /* FALLTHROUGH */
664         case (MDOC_Ohang):
665                 /* FALLTHROUGH */
666         case (MDOC_Inset):
667                 /* FALLTHROUGH */
668         case (MDOC_Item):
669                 if (width >= 0 && ! mdoc_nwarn(mdoc, n, ENOWIDTH))
670                         return(0);
671                 break;
672         default:
673                 break;
674         }
675
676         return(1);
677 }
678
679
680 static int
681 pre_bd(PRE_ARGS)
682 {
683         int              i, type, err;
684
685         if (MDOC_BLOCK != n->type)
686                 return(1);
687         if (NULL == n->args)
688                 return(mdoc_nerr(mdoc, n, EDISPTYPE));
689
690         /* Make sure that only one type of display is specified.  */
691
692         /* LINTED */
693         for (i = 0, err = type = 0; ! err &&
694                         i < (int)n->args->argc; i++)
695                 switch (n->args->argv[i].arg) {
696                 case (MDOC_Centred):
697                         /* FALLTHROUGH */
698                 case (MDOC_Ragged):
699                         /* FALLTHROUGH */
700                 case (MDOC_Unfilled):
701                         /* FALLTHROUGH */
702                 case (MDOC_Filled):
703                         /* FALLTHROUGH */
704                 case (MDOC_Literal):
705                         if (0 == type++)
706                                 break;
707                         return(mdoc_nerr(mdoc, n, EMULTIDISP));
708                 default:
709                         break;
710                 }
711
712         if (type)
713                 return(1);
714         return(mdoc_nerr(mdoc, n, EDISPTYPE));
715 }
716
717
718 static int
719 pre_ss(PRE_ARGS)
720 {
721
722         if (MDOC_BLOCK != n->type)
723                 return(1);
724         return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
725 }
726
727
728 static int
729 pre_sh(PRE_ARGS)
730 {
731
732         if (MDOC_BLOCK != n->type)
733                 return(1);
734         return(check_parent(mdoc, n, -1, MDOC_ROOT));
735 }
736
737
738 static int
739 pre_it(PRE_ARGS)
740 {
741
742         if (MDOC_BLOCK != n->type)
743                 return(1);
744         return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
745 }
746
747
748 static int
749 pre_an(PRE_ARGS)
750 {
751
752         if (NULL == n->args || 1 == n->args->argc)
753                 return(1);
754         return(mdoc_verr(mdoc, n->line, n->pos,
755                                 "only one argument allowed"));
756 }
757
758
759 static int
760 pre_lb(PRE_ARGS)
761 {
762
763         return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
764 }
765
766
767 static int
768 pre_rv(PRE_ARGS)
769 {
770
771         if ( ! check_msec(mdoc, n, 2, 3, 0))
772                 return(0);
773         return(check_stdarg(mdoc, n));
774 }
775
776
777 static int
778 pre_ex(PRE_ARGS)
779 {
780
781         if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
782                 return(0);
783         return(check_stdarg(mdoc, n));
784 }
785
786
787 static int
788 pre_er(PRE_ARGS)
789 {
790
791         return(check_msec(mdoc, n, 2, 3, 9, 0));
792 }
793
794
795 static int
796 pre_cd(PRE_ARGS)
797 {
798
799         return(check_msec(mdoc, n, 4, 0));
800 }
801
802
803 static int
804 pre_dt(PRE_ARGS)
805 {
806
807         /* FIXME: make sure is capitalised. */
808
809         if (0 == mdoc->meta.date || mdoc->meta.os)
810                 if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
811                         return(0);
812         if (mdoc->meta.title)
813                 if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
814                         return(0);
815         return(1);
816 }
817
818
819 static int
820 pre_os(PRE_ARGS)
821 {
822
823         if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
824                 if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
825                         return(0);
826         if (mdoc->meta.os)
827                 if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
828                         return(0);
829         return(1);
830 }
831
832
833 static int
834 pre_dd(PRE_ARGS)
835 {
836
837         if (mdoc->meta.title || mdoc->meta.os)
838                 if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
839                         return(0);
840         if (mdoc->meta.date)
841                 if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
842                         return(0);
843         return(1);
844 }
845
846
847 static int
848 post_bf(POST_ARGS)
849 {
850         char             *p;
851         struct mdoc_node *head;
852
853         if (MDOC_BLOCK != mdoc->last->type)
854                 return(1);
855
856         head = mdoc->last->head;
857
858         if (mdoc->last->args && head->child)
859                 return(mdoc_nerr(mdoc, mdoc->last, ELINE));
860         else if (mdoc->last->args)
861                 return(1);
862
863         if (NULL == head->child || MDOC_TEXT != head->child->type)
864                 return(mdoc_nerr(mdoc, mdoc->last, ELINE));
865
866         p = head->child->string;
867
868         if (0 == strcmp(p, "Em"))
869                 return(1);
870         else if (0 == strcmp(p, "Li"))
871                 return(1);
872         else if (0 == strcmp(p, "Sy"))
873                 return(1);
874
875         return(mdoc_nerr(mdoc, head, EFONT));
876 }
877
878
879 static int
880 post_lb(POST_ARGS)
881 {
882
883         if (mdoc_a2lib(mdoc->last->child->string))
884                 return(1);
885         return(mdoc_nwarn(mdoc, mdoc->last, ELIB));
886 }
887
888
889 static int
890 post_vt(POST_ARGS)
891 {
892         const struct mdoc_node *n;
893
894         /*
895          * The Vt macro comes in both ELEM and BLOCK form, both of which
896          * have different syntaxes (yet more context-sensitive
897          * behaviour).  ELEM types must have a child; BLOCK types,
898          * specifically the BODY, should only have TEXT children.
899          */
900
901         if (MDOC_ELEM == mdoc->last->type)
902                 return(eerr_ge1(mdoc));
903         if (MDOC_BODY != mdoc->last->type)
904                 return(1);
905
906         for (n = mdoc->last->child; n; n = n->next)
907                 if (MDOC_TEXT != n->type)
908                         if ( ! mdoc_nwarn(mdoc, n, EBADCHILD))
909                                 return(0);
910
911         return(1);
912 }
913
914
915 static int
916 post_nm(POST_ARGS)
917 {
918
919         if (mdoc->last->child)
920                 return(1);
921         if (mdoc->meta.name)
922                 return(1);
923         return(mdoc_nerr(mdoc, mdoc->last, ENAME));
924 }
925
926
927 static int
928 post_at(POST_ARGS)
929 {
930
931         if (NULL == mdoc->last->child)
932                 return(1);
933         if (MDOC_TEXT != mdoc->last->child->type)
934                 return(mdoc_nerr(mdoc, mdoc->last, EATT));
935         if (mdoc_a2att(mdoc->last->child->string))
936                 return(1);
937         return(mdoc_nerr(mdoc, mdoc->last, EATT));
938 }
939
940
941 static int
942 post_an(POST_ARGS)
943 {
944
945         if (mdoc->last->args) {
946                 if (NULL == mdoc->last->child)
947                         return(1);
948                 return(mdoc_nerr(mdoc, mdoc->last, ENOLINE));
949         }
950
951         if (mdoc->last->child)
952                 return(1);
953         return(mdoc_nerr(mdoc, mdoc->last, ELINE));
954 }
955
956
957 static int
958 post_it(POST_ARGS)
959 {
960         int               type, i, cols;
961         struct mdoc_node *n, *c;
962
963         if (MDOC_BLOCK != mdoc->last->type)
964                 return(1);
965
966         n = mdoc->last->parent->parent;
967         if (NULL == n->args)
968                 return(mdoc_nerr(mdoc, mdoc->last, ELISTTYPE));
969
970         /* Some types require block-head, some not. */
971
972         /* LINTED */
973         for (cols = type = -1, i = 0; -1 == type &&
974                         i < (int)n->args->argc; i++)
975                 switch (n->args->argv[i].arg) {
976                 case (MDOC_Tag):
977                         /* FALLTHROUGH */
978                 case (MDOC_Diag):
979                         /* FALLTHROUGH */
980                 case (MDOC_Hang):
981                         /* FALLTHROUGH */
982                 case (MDOC_Ohang):
983                         /* FALLTHROUGH */
984                 case (MDOC_Inset):
985                         /* FALLTHROUGH */
986                 case (MDOC_Bullet):
987                         /* FALLTHROUGH */
988                 case (MDOC_Dash):
989                         /* FALLTHROUGH */
990                 case (MDOC_Enum):
991                         /* FALLTHROUGH */
992                 case (MDOC_Hyphen):
993                         /* FALLTHROUGH */
994                 case (MDOC_Item):
995                         type = n->args->argv[i].arg;
996                         break;
997                 case (MDOC_Column):
998                         type = n->args->argv[i].arg;
999                         cols = (int)n->args->argv[i].sz;
1000                         break;
1001                 default:
1002                         break;
1003                 }
1004
1005         if (-1 == type)
1006                 return(mdoc_nerr(mdoc, mdoc->last, ELISTTYPE));
1007
1008         switch (type) {
1009         case (MDOC_Tag):
1010                 if (NULL == mdoc->last->head->child)
1011                         if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1012                                 return(0);
1013                 break;
1014         case (MDOC_Hang):
1015                 /* FALLTHROUGH */
1016         case (MDOC_Ohang):
1017                 /* FALLTHROUGH */
1018         case (MDOC_Inset):
1019                 /* FALLTHROUGH */
1020         case (MDOC_Diag):
1021                 if (NULL == mdoc->last->head->child)
1022                         if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1023                                 return(0);
1024                 if (NULL == mdoc->last->body->child)
1025                         if ( ! mdoc_nwarn(mdoc, mdoc->last, EMULTILINE))
1026                                 return(0);
1027                 break;
1028         case (MDOC_Bullet):
1029                 /* FALLTHROUGH */
1030         case (MDOC_Dash):
1031                 /* FALLTHROUGH */
1032         case (MDOC_Enum):
1033                 /* FALLTHROUGH */
1034         case (MDOC_Hyphen):
1035                 /* FALLTHROUGH */
1036         case (MDOC_Item):
1037                 if (mdoc->last->head->child)
1038                         if ( ! mdoc_nwarn(mdoc, mdoc->last, ENOLINE))
1039                                 return(0);
1040                 if (NULL == mdoc->last->body->child)
1041                         if ( ! mdoc_nwarn(mdoc, mdoc->last, EMULTILINE))
1042                                 return(0);
1043                 break;
1044         case (MDOC_Column):
1045                 if (NULL == mdoc->last->head->child)
1046                         if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1047                                 return(0);
1048                 if (mdoc->last->body->child)
1049                         if ( ! mdoc_nwarn(mdoc, mdoc->last, ENOMULTILINE))
1050                                 return(0);
1051                 c = mdoc->last->child;
1052                 for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
1053                         i++;
1054
1055                 if (i < cols || i == (cols + 1)) {
1056                         if ( ! mdoc_vwarn(mdoc, mdoc->last->line,
1057                                         mdoc->last->pos, "column "
1058                                         "mismatch: have %d, want %d",
1059                                         i, cols))
1060                                 return(0);
1061                         break;
1062                 } else if (i == cols)
1063                         break;
1064
1065                 return(mdoc_verr(mdoc, mdoc->last->line,
1066                                 mdoc->last->pos, "column mismatch: "
1067                                 "have %d, want %d", i, cols));
1068         default:
1069                 break;
1070         }
1071
1072         return(1);
1073 }
1074
1075
1076 static int
1077 post_bl_head(POST_ARGS)
1078 {
1079         int                     i;
1080         const struct mdoc_node *n;
1081
1082         n = mdoc->last->parent;
1083         assert(n->args);
1084
1085         for (i = 0; i < (int)n->args->argc; i++)
1086                 if (n->args->argv[i].arg == MDOC_Column)
1087                         break;
1088
1089         if (i == (int)n->args->argc)
1090                 return(1);
1091
1092         if (n->args->argv[i].sz && mdoc->last->child)
1093                 return(mdoc_nerr(mdoc, n, ECOLMIS));
1094
1095         return(1);
1096 }
1097
1098
1099 static int
1100 post_bl(POST_ARGS)
1101 {
1102         struct mdoc_node        *n;
1103
1104         if (MDOC_HEAD == mdoc->last->type)
1105                 return(post_bl_head(mdoc));
1106         if (MDOC_BODY != mdoc->last->type)
1107                 return(1);
1108         if (NULL == mdoc->last->child)
1109                 return(1);
1110
1111         /*
1112          * We only allow certain children of `Bl'.  This is usually on
1113          * `It', but apparently `Sm' occurs here and there, so we let
1114          * that one through, too.
1115          */
1116
1117         /* LINTED */
1118         for (n = mdoc->last->child; n; n = n->next) {
1119                 if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
1120                         continue;
1121                 if (MDOC_Sm == n->tok)
1122                         continue;
1123                 return(mdoc_nerr(mdoc, n, EBADCHILD));
1124         }
1125
1126         return(1);
1127 }
1128
1129
1130 static int
1131 ebool(struct mdoc *mdoc)
1132 {
1133         struct mdoc_node *n;
1134
1135         /* LINTED */
1136         for (n = mdoc->last->child; n; n = n->next) {
1137                 if (MDOC_TEXT != n->type)
1138                         break;
1139                 if (0 == strcmp(n->string, "on"))
1140                         continue;
1141                 if (0 == strcmp(n->string, "off"))
1142                         continue;
1143                 break;
1144         }
1145
1146         if (NULL == n)
1147                 return(1);
1148         return(mdoc_nerr(mdoc, n, EBOOL));
1149 }
1150
1151
1152 static int
1153 post_root(POST_ARGS)
1154 {
1155
1156         if (NULL == mdoc->first->child)
1157                 return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1158         if ( ! (MDOC_PBODY & mdoc->flags))
1159                 return(mdoc_nerr(mdoc, mdoc->first, ENOPROLOGUE));
1160
1161         if (MDOC_BLOCK != mdoc->first->child->type)
1162                 return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1163         if (MDOC_Sh != mdoc->first->child->tok)
1164                 return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1165
1166         return(1);
1167 }
1168
1169
1170 static int
1171 post_st(POST_ARGS)
1172 {
1173
1174         if (mdoc_a2st(mdoc->last->child->string))
1175                 return(1);
1176         return(mdoc_nerr(mdoc, mdoc->last, EBADSTAND));
1177 }
1178
1179
1180 static int
1181 post_rs(POST_ARGS)
1182 {
1183         struct mdoc_node        *nn;
1184
1185         if (MDOC_BODY != mdoc->last->type)
1186                 return(1);
1187
1188         for (nn = mdoc->last->child; nn; nn = nn->next)
1189                 switch (nn->tok) {
1190                 case(MDOC__U):
1191                         /* FALLTHROUGH */
1192                 case(MDOC__Q):
1193                         /* FALLTHROUGH */
1194                 case(MDOC__C):
1195                         /* FALLTHROUGH */
1196                 case(MDOC__A):
1197                         /* FALLTHROUGH */
1198                 case(MDOC__B):
1199                         /* FALLTHROUGH */
1200                 case(MDOC__D):
1201                         /* FALLTHROUGH */
1202                 case(MDOC__I):
1203                         /* FALLTHROUGH */
1204                 case(MDOC__J):
1205                         /* FALLTHROUGH */
1206                 case(MDOC__N):
1207                         /* FALLTHROUGH */
1208                 case(MDOC__O):
1209                         /* FALLTHROUGH */
1210                 case(MDOC__P):
1211                         /* FALLTHROUGH */
1212                 case(MDOC__R):
1213                         /* FALLTHROUGH */
1214                 case(MDOC__T):
1215                         /* FALLTHROUGH */
1216                 case(MDOC__V):
1217                         break;
1218                 default:
1219                         return(mdoc_nerr(mdoc, nn, EBADCHILD));
1220                 }
1221
1222         return(1);
1223 }
1224
1225
1226 static int
1227 post_sh(POST_ARGS)
1228 {
1229
1230         if (MDOC_HEAD == mdoc->last->type)
1231                 return(post_sh_head(mdoc));
1232         if (MDOC_BODY == mdoc->last->type)
1233                 return(post_sh_body(mdoc));
1234
1235         return(1);
1236 }
1237
1238
1239 static int
1240 post_sh_body(POST_ARGS)
1241 {
1242         struct mdoc_node *n;
1243
1244         if (SEC_NAME != mdoc->lastsec)
1245                 return(1);
1246
1247         /*
1248          * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1249          * macros (can have multiple `Nm' and one `Nd').  Note that the
1250          * children of the BODY declaration can also be "text".
1251          */
1252
1253         if (NULL == (n = mdoc->last->child))
1254                 return(mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC));
1255
1256         for ( ; n && n->next; n = n->next) {
1257                 if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1258                         continue;
1259                 if (MDOC_TEXT == n->type)
1260                         continue;
1261                 if ( ! mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC))
1262                         return(0);
1263         }
1264
1265         assert(n);
1266         if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1267                 return(1);
1268         return(mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC));
1269 }
1270
1271
1272 static int
1273 post_sh_head(POST_ARGS)
1274 {
1275         char                    buf[64];
1276         enum mdoc_sec           sec;
1277         const struct mdoc_node *n;
1278
1279         /*
1280          * Process a new section.  Sections are either "named" or
1281          * "custom"; custom sections are user-defined, while named ones
1282          * usually follow a conventional order and may only appear in
1283          * certain manual sections.
1284          */
1285
1286         buf[0] = 0;
1287
1288         for (n = mdoc->last->child; n; n = n->next) {
1289                 /* XXX - copied from compact(). */
1290                 assert(MDOC_TEXT == n->type);
1291
1292                 if (strlcat(buf, n->string, 64) >= 64)
1293                         return(mdoc_nerr(mdoc, n, ETOOLONG));
1294                 if (NULL == n->next)
1295                         continue;
1296                 if (strlcat(buf, " ", 64) >= 64)
1297                         return(mdoc_nerr(mdoc, n, ETOOLONG));
1298         }
1299
1300         sec = mdoc_atosec(buf);
1301
1302         /*
1303          * Check: NAME should always be first, CUSTOM has no roles,
1304          * non-CUSTOM has a conventional order to be followed.
1305          */
1306
1307         if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1308                 return(mdoc_nerr(mdoc, mdoc->last, ESECNAME));
1309         if (SEC_CUSTOM == sec)
1310                 return(1);
1311         if (sec == mdoc->lastnamed)
1312                 if ( ! mdoc_nwarn(mdoc, mdoc->last, ESECREP))
1313                         return(0);
1314         if (sec < mdoc->lastnamed)
1315                 if ( ! mdoc_nwarn(mdoc, mdoc->last, ESECOOO))
1316                         return(0);
1317
1318         /*
1319          * Check particular section/manual conventions.  LIBRARY can
1320          * only occur in msec 2, 3 (TODO: are there more of these?).
1321          */
1322
1323         switch (sec) {
1324         case (SEC_LIBRARY):
1325                 switch (mdoc->meta.msec) {
1326                 case (2):
1327                         /* FALLTHROUGH */
1328                 case (3):
1329                         break;
1330                 default:
1331                         return(mdoc_nwarn(mdoc, mdoc->last, EWRONGMSEC));
1332                 }
1333                 break;
1334         default:
1335                 break;
1336         }
1337
1338         return(1);
1339 }
1340
1341
1342 static int
1343 pre_fd(PRE_ARGS)
1344 {
1345
1346         return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1347 }