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