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