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